1 /*
2  * Copyright (c) 2015, 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/mlx5/driver.h>
35 #include <linux/mlx5/mlx5_ifc.h>
36 #include <linux/mlx5/vport.h>
37 #include <linux/mlx5/fs.h>
38 #include <linux/mlx5/mpfs.h>
39 #include <linux/debugfs.h>
40 #include "esw/acl/lgcy.h"
41 #include "esw/legacy.h"
42 #include "esw/qos.h"
43 #include "mlx5_core.h"
44 #include "lib/eq.h"
45 #include "eswitch.h"
46 #include "fs_core.h"
47 #include "devlink.h"
48 #include "ecpf.h"
49 #include "en/mod_hdr.h"
50 
51 enum {
52 	MLX5_ACTION_NONE = 0,
53 	MLX5_ACTION_ADD  = 1,
54 	MLX5_ACTION_DEL  = 2,
55 };
56 
57 /* Vport UC/MC hash node */
58 struct vport_addr {
59 	struct l2addr_node     node;
60 	u8                     action;
61 	u16                    vport;
62 	struct mlx5_flow_handle *flow_rule;
63 	bool mpfs; /* UC MAC was added to MPFs */
64 	/* A flag indicating that mac was added due to mc promiscuous vport */
65 	bool mc_promisc;
66 };
67 
mlx5_eswitch_check(const struct mlx5_core_dev * dev)68 static int mlx5_eswitch_check(const struct mlx5_core_dev *dev)
69 {
70 	if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
71 		return -EOPNOTSUPP;
72 
73 	if (!MLX5_ESWITCH_MANAGER(dev))
74 		return -EOPNOTSUPP;
75 
76 	return 0;
77 }
78 
mlx5_devlink_eswitch_get(struct devlink * devlink)79 struct mlx5_eswitch *mlx5_devlink_eswitch_get(struct devlink *devlink)
80 {
81 	struct mlx5_core_dev *dev = devlink_priv(devlink);
82 	int err;
83 
84 	err = mlx5_eswitch_check(dev);
85 	if (err)
86 		return ERR_PTR(err);
87 
88 	return dev->priv.eswitch;
89 }
90 
91 struct mlx5_vport *__must_check
mlx5_eswitch_get_vport(struct mlx5_eswitch * esw,u16 vport_num)92 mlx5_eswitch_get_vport(struct mlx5_eswitch *esw, u16 vport_num)
93 {
94 	struct mlx5_vport *vport;
95 
96 	if (!esw || !MLX5_CAP_GEN(esw->dev, vport_group_manager))
97 		return ERR_PTR(-EPERM);
98 
99 	vport = xa_load(&esw->vports, vport_num);
100 	if (!vport) {
101 		esw_debug(esw->dev, "vport out of range: num(0x%x)\n", vport_num);
102 		return ERR_PTR(-EINVAL);
103 	}
104 	return vport;
105 }
106 
arm_vport_context_events_cmd(struct mlx5_core_dev * dev,u16 vport,u32 events_mask)107 static int arm_vport_context_events_cmd(struct mlx5_core_dev *dev, u16 vport,
108 					u32 events_mask)
109 {
110 	u32 in[MLX5_ST_SZ_DW(modify_nic_vport_context_in)] = {};
111 	void *nic_vport_ctx;
112 
113 	MLX5_SET(modify_nic_vport_context_in, in,
114 		 opcode, MLX5_CMD_OP_MODIFY_NIC_VPORT_CONTEXT);
115 	MLX5_SET(modify_nic_vport_context_in, in, field_select.change_event, 1);
116 	MLX5_SET(modify_nic_vport_context_in, in, vport_number, vport);
117 	MLX5_SET(modify_nic_vport_context_in, in, other_vport, 1);
118 	nic_vport_ctx = MLX5_ADDR_OF(modify_nic_vport_context_in,
119 				     in, nic_vport_context);
120 
121 	MLX5_SET(nic_vport_context, nic_vport_ctx, arm_change_event, 1);
122 
123 	if (events_mask & MLX5_VPORT_UC_ADDR_CHANGE)
124 		MLX5_SET(nic_vport_context, nic_vport_ctx,
125 			 event_on_uc_address_change, 1);
126 	if (events_mask & MLX5_VPORT_MC_ADDR_CHANGE)
127 		MLX5_SET(nic_vport_context, nic_vport_ctx,
128 			 event_on_mc_address_change, 1);
129 	if (events_mask & MLX5_VPORT_PROMISC_CHANGE)
130 		MLX5_SET(nic_vport_context, nic_vport_ctx,
131 			 event_on_promisc_change, 1);
132 
133 	return mlx5_cmd_exec_in(dev, modify_nic_vport_context, in);
134 }
135 
136 /* E-Switch vport context HW commands */
mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev * dev,u16 vport,bool other_vport,void * in)137 int mlx5_eswitch_modify_esw_vport_context(struct mlx5_core_dev *dev, u16 vport,
138 					  bool other_vport, void *in)
139 {
140 	MLX5_SET(modify_esw_vport_context_in, in, opcode,
141 		 MLX5_CMD_OP_MODIFY_ESW_VPORT_CONTEXT);
142 	MLX5_SET(modify_esw_vport_context_in, in, vport_number, vport);
143 	MLX5_SET(modify_esw_vport_context_in, in, other_vport, other_vport);
144 	return mlx5_cmd_exec_in(dev, modify_esw_vport_context, in);
145 }
146 
modify_esw_vport_cvlan(struct mlx5_core_dev * dev,u16 vport,u16 vlan,u8 qos,u8 set_flags)147 static int modify_esw_vport_cvlan(struct mlx5_core_dev *dev, u16 vport,
148 				  u16 vlan, u8 qos, u8 set_flags)
149 {
150 	u32 in[MLX5_ST_SZ_DW(modify_esw_vport_context_in)] = {};
151 
152 	if (!MLX5_CAP_ESW(dev, vport_cvlan_strip) ||
153 	    !MLX5_CAP_ESW(dev, vport_cvlan_insert_if_not_exist))
154 		return -EOPNOTSUPP;
155 
156 	esw_debug(dev, "Set Vport[%d] VLAN %d qos %d set=%x\n",
157 		  vport, vlan, qos, set_flags);
158 
159 	if (set_flags & SET_VLAN_STRIP)
160 		MLX5_SET(modify_esw_vport_context_in, in,
161 			 esw_vport_context.vport_cvlan_strip, 1);
162 
163 	if (set_flags & SET_VLAN_INSERT) {
164 		if (MLX5_CAP_ESW(dev, vport_cvlan_insert_always)) {
165 			/* insert either if vlan exist in packet or not */
166 			MLX5_SET(modify_esw_vport_context_in, in,
167 				 esw_vport_context.vport_cvlan_insert,
168 				 MLX5_VPORT_CVLAN_INSERT_ALWAYS);
169 		} else {
170 			/* insert only if no vlan in packet */
171 			MLX5_SET(modify_esw_vport_context_in, in,
172 				 esw_vport_context.vport_cvlan_insert,
173 				 MLX5_VPORT_CVLAN_INSERT_WHEN_NO_CVLAN);
174 		}
175 		MLX5_SET(modify_esw_vport_context_in, in,
176 			 esw_vport_context.cvlan_pcp, qos);
177 		MLX5_SET(modify_esw_vport_context_in, in,
178 			 esw_vport_context.cvlan_id, vlan);
179 	}
180 
181 	MLX5_SET(modify_esw_vport_context_in, in,
182 		 field_select.vport_cvlan_strip, 1);
183 	MLX5_SET(modify_esw_vport_context_in, in,
184 		 field_select.vport_cvlan_insert, 1);
185 
186 	return mlx5_eswitch_modify_esw_vport_context(dev, vport, true, in);
187 }
188 
189 /* E-Switch FDB */
190 static struct mlx5_flow_handle *
__esw_fdb_set_vport_rule(struct mlx5_eswitch * esw,u16 vport,bool rx_rule,u8 mac_c[ETH_ALEN],u8 mac_v[ETH_ALEN])191 __esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u16 vport, bool rx_rule,
192 			 u8 mac_c[ETH_ALEN], u8 mac_v[ETH_ALEN])
193 {
194 	int match_header = (is_zero_ether_addr(mac_c) ? 0 :
195 			    MLX5_MATCH_OUTER_HEADERS);
196 	struct mlx5_flow_handle *flow_rule = NULL;
197 	struct mlx5_flow_act flow_act = {0};
198 	struct mlx5_flow_destination dest = {};
199 	struct mlx5_flow_spec *spec;
200 	void *mv_misc = NULL;
201 	void *mc_misc = NULL;
202 	u8 *dmac_v = NULL;
203 	u8 *dmac_c = NULL;
204 
205 	if (rx_rule)
206 		match_header |= MLX5_MATCH_MISC_PARAMETERS;
207 
208 	spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
209 	if (!spec)
210 		return NULL;
211 
212 	dmac_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
213 			      outer_headers.dmac_47_16);
214 	dmac_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
215 			      outer_headers.dmac_47_16);
216 
217 	if (match_header & MLX5_MATCH_OUTER_HEADERS) {
218 		ether_addr_copy(dmac_v, mac_v);
219 		ether_addr_copy(dmac_c, mac_c);
220 	}
221 
222 	if (match_header & MLX5_MATCH_MISC_PARAMETERS) {
223 		mv_misc  = MLX5_ADDR_OF(fte_match_param, spec->match_value,
224 					misc_parameters);
225 		mc_misc  = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
226 					misc_parameters);
227 		MLX5_SET(fte_match_set_misc, mv_misc, source_port, MLX5_VPORT_UPLINK);
228 		MLX5_SET_TO_ONES(fte_match_set_misc, mc_misc, source_port);
229 	}
230 
231 	dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
232 	dest.vport.num = vport;
233 
234 	esw_debug(esw->dev,
235 		  "\tFDB add rule dmac_v(%pM) dmac_c(%pM) -> vport(%d)\n",
236 		  dmac_v, dmac_c, vport);
237 	spec->match_criteria_enable = match_header;
238 	flow_act.action =  MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
239 	flow_rule =
240 		mlx5_add_flow_rules(esw->fdb_table.legacy.fdb, spec,
241 				    &flow_act, &dest, 1);
242 	if (IS_ERR(flow_rule)) {
243 		esw_warn(esw->dev,
244 			 "FDB: Failed to add flow rule: dmac_v(%pM) dmac_c(%pM) -> vport(%d), err(%ld)\n",
245 			 dmac_v, dmac_c, vport, PTR_ERR(flow_rule));
246 		flow_rule = NULL;
247 	}
248 
249 	kvfree(spec);
250 	return flow_rule;
251 }
252 
253 static struct mlx5_flow_handle *
esw_fdb_set_vport_rule(struct mlx5_eswitch * esw,u8 mac[ETH_ALEN],u16 vport)254 esw_fdb_set_vport_rule(struct mlx5_eswitch *esw, u8 mac[ETH_ALEN], u16 vport)
255 {
256 	u8 mac_c[ETH_ALEN];
257 
258 	eth_broadcast_addr(mac_c);
259 	return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac);
260 }
261 
262 static struct mlx5_flow_handle *
esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch * esw,u16 vport)263 esw_fdb_set_vport_allmulti_rule(struct mlx5_eswitch *esw, u16 vport)
264 {
265 	u8 mac_c[ETH_ALEN];
266 	u8 mac_v[ETH_ALEN];
267 
268 	eth_zero_addr(mac_c);
269 	eth_zero_addr(mac_v);
270 	mac_c[0] = 0x01;
271 	mac_v[0] = 0x01;
272 	return __esw_fdb_set_vport_rule(esw, vport, false, mac_c, mac_v);
273 }
274 
275 static struct mlx5_flow_handle *
esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch * esw,u16 vport)276 esw_fdb_set_vport_promisc_rule(struct mlx5_eswitch *esw, u16 vport)
277 {
278 	u8 mac_c[ETH_ALEN];
279 	u8 mac_v[ETH_ALEN];
280 
281 	eth_zero_addr(mac_c);
282 	eth_zero_addr(mac_v);
283 	return __esw_fdb_set_vport_rule(esw, vport, true, mac_c, mac_v);
284 }
285 
286 /* E-Switch vport UC/MC lists management */
287 typedef int (*vport_addr_action)(struct mlx5_eswitch *esw,
288 				 struct vport_addr *vaddr);
289 
esw_add_uc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)290 static int esw_add_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
291 {
292 	u8 *mac = vaddr->node.addr;
293 	u16 vport = vaddr->vport;
294 	int err;
295 
296 	/* Skip mlx5_mpfs_add_mac for eswitch_managers,
297 	 * it is already done by its netdev in mlx5e_execute_l2_action
298 	 */
299 	if (mlx5_esw_is_manager_vport(esw, vport))
300 		goto fdb_add;
301 
302 	err = mlx5_mpfs_add_mac(esw->dev, mac);
303 	if (err) {
304 		esw_warn(esw->dev,
305 			 "Failed to add L2 table mac(%pM) for vport(0x%x), err(%d)\n",
306 			 mac, vport, err);
307 		return err;
308 	}
309 	vaddr->mpfs = true;
310 
311 fdb_add:
312 	/* SRIOV is enabled: Forward UC MAC to vport */
313 	if (esw->fdb_table.legacy.fdb && esw->mode == MLX5_ESWITCH_LEGACY)
314 		vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
315 
316 	esw_debug(esw->dev, "\tADDED UC MAC: vport[%d] %pM fr(%p)\n",
317 		  vport, mac, vaddr->flow_rule);
318 
319 	return 0;
320 }
321 
esw_del_uc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)322 static int esw_del_uc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
323 {
324 	u8 *mac = vaddr->node.addr;
325 	u16 vport = vaddr->vport;
326 	int err = 0;
327 
328 	/* Skip mlx5_mpfs_del_mac for eswitch managers,
329 	 * it is already done by its netdev in mlx5e_execute_l2_action
330 	 */
331 	if (!vaddr->mpfs || mlx5_esw_is_manager_vport(esw, vport))
332 		goto fdb_del;
333 
334 	err = mlx5_mpfs_del_mac(esw->dev, mac);
335 	if (err)
336 		esw_warn(esw->dev,
337 			 "Failed to del L2 table mac(%pM) for vport(%d), err(%d)\n",
338 			 mac, vport, err);
339 	vaddr->mpfs = false;
340 
341 fdb_del:
342 	if (vaddr->flow_rule)
343 		mlx5_del_flow_rules(vaddr->flow_rule);
344 	vaddr->flow_rule = NULL;
345 
346 	return 0;
347 }
348 
update_allmulti_vports(struct mlx5_eswitch * esw,struct vport_addr * vaddr,struct esw_mc_addr * esw_mc)349 static void update_allmulti_vports(struct mlx5_eswitch *esw,
350 				   struct vport_addr *vaddr,
351 				   struct esw_mc_addr *esw_mc)
352 {
353 	u8 *mac = vaddr->node.addr;
354 	struct mlx5_vport *vport;
355 	unsigned long i;
356 	u16 vport_num;
357 
358 	mlx5_esw_for_each_vport(esw, i, vport) {
359 		struct hlist_head *vport_hash = vport->mc_list;
360 		struct vport_addr *iter_vaddr =
361 					l2addr_hash_find(vport_hash,
362 							 mac,
363 							 struct vport_addr);
364 		vport_num = vport->vport;
365 		if (IS_ERR_OR_NULL(vport->allmulti_rule) ||
366 		    vaddr->vport == vport_num)
367 			continue;
368 		switch (vaddr->action) {
369 		case MLX5_ACTION_ADD:
370 			if (iter_vaddr)
371 				continue;
372 			iter_vaddr = l2addr_hash_add(vport_hash, mac,
373 						     struct vport_addr,
374 						     GFP_KERNEL);
375 			if (!iter_vaddr) {
376 				esw_warn(esw->dev,
377 					 "ALL-MULTI: Failed to add MAC(%pM) to vport[%d] DB\n",
378 					 mac, vport_num);
379 				continue;
380 			}
381 			iter_vaddr->vport = vport_num;
382 			iter_vaddr->flow_rule =
383 					esw_fdb_set_vport_rule(esw,
384 							       mac,
385 							       vport_num);
386 			iter_vaddr->mc_promisc = true;
387 			break;
388 		case MLX5_ACTION_DEL:
389 			if (!iter_vaddr)
390 				continue;
391 			mlx5_del_flow_rules(iter_vaddr->flow_rule);
392 			l2addr_hash_del(iter_vaddr);
393 			break;
394 		}
395 	}
396 }
397 
esw_add_mc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)398 static int esw_add_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
399 {
400 	struct hlist_head *hash = esw->mc_table;
401 	struct esw_mc_addr *esw_mc;
402 	u8 *mac = vaddr->node.addr;
403 	u16 vport = vaddr->vport;
404 
405 	if (!esw->fdb_table.legacy.fdb)
406 		return 0;
407 
408 	esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
409 	if (esw_mc)
410 		goto add;
411 
412 	esw_mc = l2addr_hash_add(hash, mac, struct esw_mc_addr, GFP_KERNEL);
413 	if (!esw_mc)
414 		return -ENOMEM;
415 
416 	esw_mc->uplink_rule = /* Forward MC MAC to Uplink */
417 		esw_fdb_set_vport_rule(esw, mac, MLX5_VPORT_UPLINK);
418 
419 	/* Add this multicast mac to all the mc promiscuous vports */
420 	update_allmulti_vports(esw, vaddr, esw_mc);
421 
422 add:
423 	/* If the multicast mac is added as a result of mc promiscuous vport,
424 	 * don't increment the multicast ref count
425 	 */
426 	if (!vaddr->mc_promisc)
427 		esw_mc->refcnt++;
428 
429 	/* Forward MC MAC to vport */
430 	vaddr->flow_rule = esw_fdb_set_vport_rule(esw, mac, vport);
431 	esw_debug(esw->dev,
432 		  "\tADDED MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
433 		  vport, mac, vaddr->flow_rule,
434 		  esw_mc->refcnt, esw_mc->uplink_rule);
435 	return 0;
436 }
437 
esw_del_mc_addr(struct mlx5_eswitch * esw,struct vport_addr * vaddr)438 static int esw_del_mc_addr(struct mlx5_eswitch *esw, struct vport_addr *vaddr)
439 {
440 	struct hlist_head *hash = esw->mc_table;
441 	struct esw_mc_addr *esw_mc;
442 	u8 *mac = vaddr->node.addr;
443 	u16 vport = vaddr->vport;
444 
445 	if (!esw->fdb_table.legacy.fdb)
446 		return 0;
447 
448 	esw_mc = l2addr_hash_find(hash, mac, struct esw_mc_addr);
449 	if (!esw_mc) {
450 		esw_warn(esw->dev,
451 			 "Failed to find eswitch MC addr for MAC(%pM) vport(%d)",
452 			 mac, vport);
453 		return -EINVAL;
454 	}
455 	esw_debug(esw->dev,
456 		  "\tDELETE MC MAC: vport[%d] %pM fr(%p) refcnt(%d) uplinkfr(%p)\n",
457 		  vport, mac, vaddr->flow_rule, esw_mc->refcnt,
458 		  esw_mc->uplink_rule);
459 
460 	if (vaddr->flow_rule)
461 		mlx5_del_flow_rules(vaddr->flow_rule);
462 	vaddr->flow_rule = NULL;
463 
464 	/* If the multicast mac is added as a result of mc promiscuous vport,
465 	 * don't decrement the multicast ref count.
466 	 */
467 	if (vaddr->mc_promisc || (--esw_mc->refcnt > 0))
468 		return 0;
469 
470 	/* Remove this multicast mac from all the mc promiscuous vports */
471 	update_allmulti_vports(esw, vaddr, esw_mc);
472 
473 	if (esw_mc->uplink_rule)
474 		mlx5_del_flow_rules(esw_mc->uplink_rule);
475 
476 	l2addr_hash_del(esw_mc);
477 	return 0;
478 }
479 
480 /* Apply vport UC/MC list to HW l2 table and FDB table */
esw_apply_vport_addr_list(struct mlx5_eswitch * esw,struct mlx5_vport * vport,int list_type)481 static void esw_apply_vport_addr_list(struct mlx5_eswitch *esw,
482 				      struct mlx5_vport *vport, int list_type)
483 {
484 	bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
485 	vport_addr_action vport_addr_add;
486 	vport_addr_action vport_addr_del;
487 	struct vport_addr *addr;
488 	struct l2addr_node *node;
489 	struct hlist_head *hash;
490 	struct hlist_node *tmp;
491 	int hi;
492 
493 	vport_addr_add = is_uc ? esw_add_uc_addr :
494 				 esw_add_mc_addr;
495 	vport_addr_del = is_uc ? esw_del_uc_addr :
496 				 esw_del_mc_addr;
497 
498 	hash = is_uc ? vport->uc_list : vport->mc_list;
499 	for_each_l2hash_node(node, tmp, hash, hi) {
500 		addr = container_of(node, struct vport_addr, node);
501 		switch (addr->action) {
502 		case MLX5_ACTION_ADD:
503 			vport_addr_add(esw, addr);
504 			addr->action = MLX5_ACTION_NONE;
505 			break;
506 		case MLX5_ACTION_DEL:
507 			vport_addr_del(esw, addr);
508 			l2addr_hash_del(addr);
509 			break;
510 		}
511 	}
512 }
513 
514 /* Sync vport UC/MC list from vport context */
esw_update_vport_addr_list(struct mlx5_eswitch * esw,struct mlx5_vport * vport,int list_type)515 static void esw_update_vport_addr_list(struct mlx5_eswitch *esw,
516 				       struct mlx5_vport *vport, int list_type)
517 {
518 	bool is_uc = list_type == MLX5_NVPRT_LIST_TYPE_UC;
519 	u8 (*mac_list)[ETH_ALEN];
520 	struct l2addr_node *node;
521 	struct vport_addr *addr;
522 	struct hlist_head *hash;
523 	struct hlist_node *tmp;
524 	int size;
525 	int err;
526 	int hi;
527 	int i;
528 
529 	size = is_uc ? MLX5_MAX_UC_PER_VPORT(esw->dev) :
530 		       MLX5_MAX_MC_PER_VPORT(esw->dev);
531 
532 	mac_list = kcalloc(size, ETH_ALEN, GFP_KERNEL);
533 	if (!mac_list)
534 		return;
535 
536 	hash = is_uc ? vport->uc_list : vport->mc_list;
537 
538 	for_each_l2hash_node(node, tmp, hash, hi) {
539 		addr = container_of(node, struct vport_addr, node);
540 		addr->action = MLX5_ACTION_DEL;
541 	}
542 
543 	if (!vport->enabled)
544 		goto out;
545 
546 	err = mlx5_query_nic_vport_mac_list(esw->dev, vport->vport, list_type,
547 					    mac_list, &size);
548 	if (err)
549 		goto out;
550 	esw_debug(esw->dev, "vport[%d] context update %s list size (%d)\n",
551 		  vport->vport, is_uc ? "UC" : "MC", size);
552 
553 	for (i = 0; i < size; i++) {
554 		if (is_uc && !is_valid_ether_addr(mac_list[i]))
555 			continue;
556 
557 		if (!is_uc && !is_multicast_ether_addr(mac_list[i]))
558 			continue;
559 
560 		addr = l2addr_hash_find(hash, mac_list[i], struct vport_addr);
561 		if (addr) {
562 			addr->action = MLX5_ACTION_NONE;
563 			/* If this mac was previously added because of allmulti
564 			 * promiscuous rx mode, its now converted to be original
565 			 * vport mac.
566 			 */
567 			if (addr->mc_promisc) {
568 				struct esw_mc_addr *esw_mc =
569 					l2addr_hash_find(esw->mc_table,
570 							 mac_list[i],
571 							 struct esw_mc_addr);
572 				if (!esw_mc) {
573 					esw_warn(esw->dev,
574 						 "Failed to MAC(%pM) in mcast DB\n",
575 						 mac_list[i]);
576 					continue;
577 				}
578 				esw_mc->refcnt++;
579 				addr->mc_promisc = false;
580 			}
581 			continue;
582 		}
583 
584 		addr = l2addr_hash_add(hash, mac_list[i], struct vport_addr,
585 				       GFP_KERNEL);
586 		if (!addr) {
587 			esw_warn(esw->dev,
588 				 "Failed to add MAC(%pM) to vport[%d] DB\n",
589 				 mac_list[i], vport->vport);
590 			continue;
591 		}
592 		addr->vport = vport->vport;
593 		addr->action = MLX5_ACTION_ADD;
594 	}
595 out:
596 	kfree(mac_list);
597 }
598 
599 /* Sync vport UC/MC list from vport context
600  * Must be called after esw_update_vport_addr_list
601  */
esw_update_vport_mc_promisc(struct mlx5_eswitch * esw,struct mlx5_vport * vport)602 static void esw_update_vport_mc_promisc(struct mlx5_eswitch *esw,
603 					struct mlx5_vport *vport)
604 {
605 	struct l2addr_node *node;
606 	struct vport_addr *addr;
607 	struct hlist_head *hash;
608 	struct hlist_node *tmp;
609 	int hi;
610 
611 	hash = vport->mc_list;
612 
613 	for_each_l2hash_node(node, tmp, esw->mc_table, hi) {
614 		u8 *mac = node->addr;
615 
616 		addr = l2addr_hash_find(hash, mac, struct vport_addr);
617 		if (addr) {
618 			if (addr->action == MLX5_ACTION_DEL)
619 				addr->action = MLX5_ACTION_NONE;
620 			continue;
621 		}
622 		addr = l2addr_hash_add(hash, mac, struct vport_addr,
623 				       GFP_KERNEL);
624 		if (!addr) {
625 			esw_warn(esw->dev,
626 				 "Failed to add allmulti MAC(%pM) to vport[%d] DB\n",
627 				 mac, vport->vport);
628 			continue;
629 		}
630 		addr->vport = vport->vport;
631 		addr->action = MLX5_ACTION_ADD;
632 		addr->mc_promisc = true;
633 	}
634 }
635 
636 /* Apply vport rx mode to HW FDB table */
esw_apply_vport_rx_mode(struct mlx5_eswitch * esw,struct mlx5_vport * vport,bool promisc,bool mc_promisc)637 static void esw_apply_vport_rx_mode(struct mlx5_eswitch *esw,
638 				    struct mlx5_vport *vport,
639 				    bool promisc, bool mc_promisc)
640 {
641 	struct esw_mc_addr *allmulti_addr = &esw->mc_promisc;
642 
643 	if (IS_ERR_OR_NULL(vport->allmulti_rule) != mc_promisc)
644 		goto promisc;
645 
646 	if (mc_promisc) {
647 		vport->allmulti_rule =
648 			esw_fdb_set_vport_allmulti_rule(esw, vport->vport);
649 		if (!allmulti_addr->uplink_rule)
650 			allmulti_addr->uplink_rule =
651 				esw_fdb_set_vport_allmulti_rule(esw,
652 								MLX5_VPORT_UPLINK);
653 		allmulti_addr->refcnt++;
654 	} else if (vport->allmulti_rule) {
655 		mlx5_del_flow_rules(vport->allmulti_rule);
656 		vport->allmulti_rule = NULL;
657 
658 		if (--allmulti_addr->refcnt > 0)
659 			goto promisc;
660 
661 		if (allmulti_addr->uplink_rule)
662 			mlx5_del_flow_rules(allmulti_addr->uplink_rule);
663 		allmulti_addr->uplink_rule = NULL;
664 	}
665 
666 promisc:
667 	if (IS_ERR_OR_NULL(vport->promisc_rule) != promisc)
668 		return;
669 
670 	if (promisc) {
671 		vport->promisc_rule =
672 			esw_fdb_set_vport_promisc_rule(esw, vport->vport);
673 	} else if (vport->promisc_rule) {
674 		mlx5_del_flow_rules(vport->promisc_rule);
675 		vport->promisc_rule = NULL;
676 	}
677 }
678 
679 /* Sync vport rx mode from vport context */
esw_update_vport_rx_mode(struct mlx5_eswitch * esw,struct mlx5_vport * vport)680 static void esw_update_vport_rx_mode(struct mlx5_eswitch *esw,
681 				     struct mlx5_vport *vport)
682 {
683 	int promisc_all = 0;
684 	int promisc_uc = 0;
685 	int promisc_mc = 0;
686 	int err;
687 
688 	err = mlx5_query_nic_vport_promisc(esw->dev,
689 					   vport->vport,
690 					   &promisc_uc,
691 					   &promisc_mc,
692 					   &promisc_all);
693 	if (err)
694 		return;
695 	esw_debug(esw->dev, "vport[%d] context update rx mode promisc_all=%d, all_multi=%d\n",
696 		  vport->vport, promisc_all, promisc_mc);
697 
698 	if (!vport->info.trusted || !vport->enabled) {
699 		promisc_uc = 0;
700 		promisc_mc = 0;
701 		promisc_all = 0;
702 	}
703 
704 	esw_apply_vport_rx_mode(esw, vport, promisc_all,
705 				(promisc_all || promisc_mc));
706 }
707 
esw_vport_change_handle_locked(struct mlx5_vport * vport)708 void esw_vport_change_handle_locked(struct mlx5_vport *vport)
709 {
710 	struct mlx5_core_dev *dev = vport->dev;
711 	struct mlx5_eswitch *esw = dev->priv.eswitch;
712 	u8 mac[ETH_ALEN];
713 
714 	mlx5_query_nic_vport_mac_address(dev, vport->vport, true, mac);
715 	esw_debug(dev, "vport[%d] Context Changed: perm mac: %pM\n",
716 		  vport->vport, mac);
717 
718 	if (vport->enabled_events & MLX5_VPORT_UC_ADDR_CHANGE) {
719 		esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
720 		esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_UC);
721 	}
722 
723 	if (vport->enabled_events & MLX5_VPORT_MC_ADDR_CHANGE)
724 		esw_update_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
725 
726 	if (vport->enabled_events & MLX5_VPORT_PROMISC_CHANGE) {
727 		esw_update_vport_rx_mode(esw, vport);
728 		if (!IS_ERR_OR_NULL(vport->allmulti_rule))
729 			esw_update_vport_mc_promisc(esw, vport);
730 	}
731 
732 	if (vport->enabled_events & (MLX5_VPORT_PROMISC_CHANGE | MLX5_VPORT_MC_ADDR_CHANGE))
733 		esw_apply_vport_addr_list(esw, vport, MLX5_NVPRT_LIST_TYPE_MC);
734 
735 	esw_debug(esw->dev, "vport[%d] Context Changed: Done\n", vport->vport);
736 	if (vport->enabled)
737 		arm_vport_context_events_cmd(dev, vport->vport,
738 					     vport->enabled_events);
739 }
740 
esw_vport_change_handler(struct work_struct * work)741 static void esw_vport_change_handler(struct work_struct *work)
742 {
743 	struct mlx5_vport *vport =
744 		container_of(work, struct mlx5_vport, vport_change_handler);
745 	struct mlx5_eswitch *esw = vport->dev->priv.eswitch;
746 
747 	mutex_lock(&esw->state_lock);
748 	esw_vport_change_handle_locked(vport);
749 	mutex_unlock(&esw->state_lock);
750 }
751 
node_guid_gen_from_mac(u64 * node_guid,const u8 * mac)752 static void node_guid_gen_from_mac(u64 *node_guid, const u8 *mac)
753 {
754 	((u8 *)node_guid)[7] = mac[0];
755 	((u8 *)node_guid)[6] = mac[1];
756 	((u8 *)node_guid)[5] = mac[2];
757 	((u8 *)node_guid)[4] = 0xff;
758 	((u8 *)node_guid)[3] = 0xfe;
759 	((u8 *)node_guid)[2] = mac[3];
760 	((u8 *)node_guid)[1] = mac[4];
761 	((u8 *)node_guid)[0] = mac[5];
762 }
763 
esw_vport_setup_acl(struct mlx5_eswitch * esw,struct mlx5_vport * vport)764 static int esw_vport_setup_acl(struct mlx5_eswitch *esw,
765 			       struct mlx5_vport *vport)
766 {
767 	if (esw->mode == MLX5_ESWITCH_LEGACY)
768 		return esw_legacy_vport_acl_setup(esw, vport);
769 	else
770 		return esw_vport_create_offloads_acl_tables(esw, vport);
771 }
772 
esw_vport_cleanup_acl(struct mlx5_eswitch * esw,struct mlx5_vport * vport)773 static void esw_vport_cleanup_acl(struct mlx5_eswitch *esw,
774 				  struct mlx5_vport *vport)
775 {
776 	if (esw->mode == MLX5_ESWITCH_LEGACY)
777 		esw_legacy_vport_acl_cleanup(esw, vport);
778 	else
779 		esw_vport_destroy_offloads_acl_tables(esw, vport);
780 }
781 
mlx5_esw_vport_caps_get(struct mlx5_eswitch * esw,struct mlx5_vport * vport)782 static int mlx5_esw_vport_caps_get(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
783 {
784 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
785 	void *query_ctx;
786 	void *hca_caps;
787 	int err;
788 
789 	if (!MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
790 		return 0;
791 
792 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
793 	if (!query_ctx)
794 		return -ENOMEM;
795 
796 	err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
797 					    MLX5_CAP_GENERAL);
798 	if (err)
799 		goto out_free;
800 
801 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
802 	vport->info.roce_enabled = MLX5_GET(cmd_hca_cap, hca_caps, roce);
803 
804 	memset(query_ctx, 0, query_out_sz);
805 	err = mlx5_vport_get_other_func_cap(esw->dev, vport->vport, query_ctx,
806 					    MLX5_CAP_GENERAL_2);
807 	if (err)
808 		goto out_free;
809 
810 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
811 	vport->info.mig_enabled = MLX5_GET(cmd_hca_cap_2, hca_caps, migratable);
812 out_free:
813 	kfree(query_ctx);
814 	return err;
815 }
816 
esw_vport_setup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)817 static int esw_vport_setup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
818 {
819 	bool vst_mode_steering = esw_vst_mode_is_steering(esw);
820 	u16 vport_num = vport->vport;
821 	int flags;
822 	int err;
823 
824 	err = esw_vport_setup_acl(esw, vport);
825 	if (err)
826 		return err;
827 
828 	if (mlx5_esw_is_manager_vport(esw, vport_num))
829 		return 0;
830 
831 	err = mlx5_esw_vport_caps_get(esw, vport);
832 	if (err)
833 		goto err_caps;
834 
835 	mlx5_modify_vport_admin_state(esw->dev,
836 				      MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
837 				      vport_num, 1,
838 				      vport->info.link_state);
839 
840 	/* Host PF has its own mac/guid. */
841 	if (vport_num) {
842 		mlx5_modify_nic_vport_mac_address(esw->dev, vport_num,
843 						  vport->info.mac);
844 		mlx5_modify_nic_vport_node_guid(esw->dev, vport_num,
845 						vport->info.node_guid);
846 	}
847 
848 	flags = (vport->info.vlan || vport->info.qos) ?
849 		SET_VLAN_STRIP | SET_VLAN_INSERT : 0;
850 	if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering)
851 		modify_esw_vport_cvlan(esw->dev, vport_num, vport->info.vlan,
852 				       vport->info.qos, flags);
853 
854 	return 0;
855 
856 err_caps:
857 	esw_vport_cleanup_acl(esw, vport);
858 	return err;
859 }
860 
861 /* Don't cleanup vport->info, it's needed to restore vport configuration */
esw_vport_cleanup(struct mlx5_eswitch * esw,struct mlx5_vport * vport)862 static void esw_vport_cleanup(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
863 {
864 	u16 vport_num = vport->vport;
865 
866 	if (!mlx5_esw_is_manager_vport(esw, vport_num))
867 		mlx5_modify_vport_admin_state(esw->dev,
868 					      MLX5_VPORT_STATE_OP_MOD_ESW_VPORT,
869 					      vport_num, 1,
870 					      MLX5_VPORT_ADMIN_STATE_DOWN);
871 
872 	mlx5_esw_qos_vport_disable(esw, vport);
873 	esw_vport_cleanup_acl(esw, vport);
874 }
875 
mlx5_esw_vport_enable(struct mlx5_eswitch * esw,u16 vport_num,enum mlx5_eswitch_vport_event enabled_events)876 int mlx5_esw_vport_enable(struct mlx5_eswitch *esw, u16 vport_num,
877 			  enum mlx5_eswitch_vport_event enabled_events)
878 {
879 	struct mlx5_vport *vport;
880 	int ret;
881 
882 	vport = mlx5_eswitch_get_vport(esw, vport_num);
883 	if (IS_ERR(vport))
884 		return PTR_ERR(vport);
885 
886 	mutex_lock(&esw->state_lock);
887 	WARN_ON(vport->enabled);
888 
889 	esw_debug(esw->dev, "Enabling VPORT(%d)\n", vport_num);
890 
891 	ret = esw_vport_setup(esw, vport);
892 	if (ret)
893 		goto done;
894 
895 	/* Sync with current vport context */
896 	vport->enabled_events = enabled_events;
897 	vport->enabled = true;
898 
899 	/* Esw manager is trusted by default. Host PF (vport 0) is trusted as well
900 	 * in smartNIC as it's a vport group manager.
901 	 */
902 	if (mlx5_esw_is_manager_vport(esw, vport_num) ||
903 	    (!vport_num && mlx5_core_is_ecpf(esw->dev)))
904 		vport->info.trusted = true;
905 
906 	if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
907 	    MLX5_CAP_GEN(esw->dev, vhca_resource_manager)) {
908 		ret = mlx5_esw_vport_vhca_id_set(esw, vport_num);
909 		if (ret)
910 			goto err_vhca_mapping;
911 	}
912 
913 	/* External controller host PF has factory programmed MAC.
914 	 * Read it from the device.
915 	 */
916 	if (mlx5_core_is_ecpf(esw->dev) && vport_num == MLX5_VPORT_PF)
917 		mlx5_query_nic_vport_mac_address(esw->dev, vport_num, true, vport->info.mac);
918 
919 	esw_vport_change_handle_locked(vport);
920 
921 	esw->enabled_vports++;
922 	esw_debug(esw->dev, "Enabled VPORT(%d)\n", vport_num);
923 done:
924 	mutex_unlock(&esw->state_lock);
925 	return ret;
926 
927 err_vhca_mapping:
928 	esw_vport_cleanup(esw, vport);
929 	mutex_unlock(&esw->state_lock);
930 	return ret;
931 }
932 
mlx5_esw_vport_disable(struct mlx5_eswitch * esw,u16 vport_num)933 void mlx5_esw_vport_disable(struct mlx5_eswitch *esw, u16 vport_num)
934 {
935 	struct mlx5_vport *vport;
936 
937 	vport = mlx5_eswitch_get_vport(esw, vport_num);
938 	if (IS_ERR(vport))
939 		return;
940 
941 	mutex_lock(&esw->state_lock);
942 	if (!vport->enabled)
943 		goto done;
944 
945 	esw_debug(esw->dev, "Disabling vport(%d)\n", vport_num);
946 	/* Mark this vport as disabled to discard new events */
947 	vport->enabled = false;
948 
949 	/* Disable events from this vport */
950 	arm_vport_context_events_cmd(esw->dev, vport->vport, 0);
951 
952 	if (!mlx5_esw_is_manager_vport(esw, vport->vport) &&
953 	    MLX5_CAP_GEN(esw->dev, vhca_resource_manager))
954 		mlx5_esw_vport_vhca_id_clear(esw, vport_num);
955 
956 	/* We don't assume VFs will cleanup after themselves.
957 	 * Calling vport change handler while vport is disabled will cleanup
958 	 * the vport resources.
959 	 */
960 	esw_vport_change_handle_locked(vport);
961 	vport->enabled_events = 0;
962 	esw_vport_cleanup(esw, vport);
963 	esw->enabled_vports--;
964 
965 done:
966 	mutex_unlock(&esw->state_lock);
967 }
968 
eswitch_vport_event(struct notifier_block * nb,unsigned long type,void * data)969 static int eswitch_vport_event(struct notifier_block *nb,
970 			       unsigned long type, void *data)
971 {
972 	struct mlx5_eswitch *esw = mlx5_nb_cof(nb, struct mlx5_eswitch, nb);
973 	struct mlx5_eqe *eqe = data;
974 	struct mlx5_vport *vport;
975 	u16 vport_num;
976 
977 	vport_num = be16_to_cpu(eqe->data.vport_change.vport_num);
978 	vport = mlx5_eswitch_get_vport(esw, vport_num);
979 	if (!IS_ERR(vport))
980 		queue_work(esw->work_queue, &vport->vport_change_handler);
981 	return NOTIFY_OK;
982 }
983 
984 /**
985  * mlx5_esw_query_functions - Returns raw output about functions state
986  * @dev:	Pointer to device to query
987  *
988  * mlx5_esw_query_functions() allocates and returns functions changed
989  * raw output memory pointer from device on success. Otherwise returns ERR_PTR.
990  * Caller must free the memory using kvfree() when valid pointer is returned.
991  */
mlx5_esw_query_functions(struct mlx5_core_dev * dev)992 const u32 *mlx5_esw_query_functions(struct mlx5_core_dev *dev)
993 {
994 	int outlen = MLX5_ST_SZ_BYTES(query_esw_functions_out);
995 	u32 in[MLX5_ST_SZ_DW(query_esw_functions_in)] = {};
996 	u32 *out;
997 	int err;
998 
999 	out = kvzalloc(outlen, GFP_KERNEL);
1000 	if (!out)
1001 		return ERR_PTR(-ENOMEM);
1002 
1003 	MLX5_SET(query_esw_functions_in, in, opcode,
1004 		 MLX5_CMD_OP_QUERY_ESW_FUNCTIONS);
1005 
1006 	err = mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
1007 	if (!err)
1008 		return out;
1009 
1010 	kvfree(out);
1011 	return ERR_PTR(err);
1012 }
1013 
mlx5_eswitch_event_handlers_register(struct mlx5_eswitch * esw)1014 static void mlx5_eswitch_event_handlers_register(struct mlx5_eswitch *esw)
1015 {
1016 	MLX5_NB_INIT(&esw->nb, eswitch_vport_event, NIC_VPORT_CHANGE);
1017 	mlx5_eq_notifier_register(esw->dev, &esw->nb);
1018 
1019 	if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev)) {
1020 		MLX5_NB_INIT(&esw->esw_funcs.nb, mlx5_esw_funcs_changed_handler,
1021 			     ESW_FUNCTIONS_CHANGED);
1022 		mlx5_eq_notifier_register(esw->dev, &esw->esw_funcs.nb);
1023 	}
1024 }
1025 
mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch * esw)1026 static void mlx5_eswitch_event_handlers_unregister(struct mlx5_eswitch *esw)
1027 {
1028 	if (esw->mode == MLX5_ESWITCH_OFFLOADS && mlx5_eswitch_is_funcs_handler(esw->dev))
1029 		mlx5_eq_notifier_unregister(esw->dev, &esw->esw_funcs.nb);
1030 
1031 	mlx5_eq_notifier_unregister(esw->dev, &esw->nb);
1032 
1033 	flush_workqueue(esw->work_queue);
1034 }
1035 
mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch * esw)1036 static void mlx5_eswitch_clear_vf_vports_info(struct mlx5_eswitch *esw)
1037 {
1038 	struct mlx5_vport *vport;
1039 	unsigned long i;
1040 
1041 	mlx5_esw_for_each_vf_vport(esw, i, vport, esw->esw_funcs.num_vfs) {
1042 		memset(&vport->qos, 0, sizeof(vport->qos));
1043 		memset(&vport->info, 0, sizeof(vport->info));
1044 		vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1045 	}
1046 }
1047 
1048 /* Public E-Switch API */
mlx5_eswitch_load_vport(struct mlx5_eswitch * esw,u16 vport_num,enum mlx5_eswitch_vport_event enabled_events)1049 int mlx5_eswitch_load_vport(struct mlx5_eswitch *esw, u16 vport_num,
1050 			    enum mlx5_eswitch_vport_event enabled_events)
1051 {
1052 	int err;
1053 
1054 	err = mlx5_esw_vport_enable(esw, vport_num, enabled_events);
1055 	if (err)
1056 		return err;
1057 
1058 	mlx5_esw_vport_debugfs_create(esw, vport_num, false, 0);
1059 	err = esw_offloads_load_rep(esw, vport_num);
1060 	if (err)
1061 		goto err_rep;
1062 
1063 	return err;
1064 
1065 err_rep:
1066 	mlx5_esw_vport_debugfs_destroy(esw, vport_num);
1067 	mlx5_esw_vport_disable(esw, vport_num);
1068 	return err;
1069 }
1070 
mlx5_eswitch_unload_vport(struct mlx5_eswitch * esw,u16 vport_num)1071 void mlx5_eswitch_unload_vport(struct mlx5_eswitch *esw, u16 vport_num)
1072 {
1073 	esw_offloads_unload_rep(esw, vport_num);
1074 	mlx5_esw_vport_debugfs_destroy(esw, vport_num);
1075 	mlx5_esw_vport_disable(esw, vport_num);
1076 }
1077 
mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch * esw,u16 num_vfs)1078 void mlx5_eswitch_unload_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs)
1079 {
1080 	struct mlx5_vport *vport;
1081 	unsigned long i;
1082 
1083 	mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1084 		if (!vport->enabled)
1085 			continue;
1086 		mlx5_eswitch_unload_vport(esw, vport->vport);
1087 	}
1088 }
1089 
mlx5_eswitch_load_vf_vports(struct mlx5_eswitch * esw,u16 num_vfs,enum mlx5_eswitch_vport_event enabled_events)1090 int mlx5_eswitch_load_vf_vports(struct mlx5_eswitch *esw, u16 num_vfs,
1091 				enum mlx5_eswitch_vport_event enabled_events)
1092 {
1093 	struct mlx5_vport *vport;
1094 	unsigned long i;
1095 	int err;
1096 
1097 	mlx5_esw_for_each_vf_vport(esw, i, vport, num_vfs) {
1098 		err = mlx5_eswitch_load_vport(esw, vport->vport, enabled_events);
1099 		if (err)
1100 			goto vf_err;
1101 	}
1102 
1103 	return 0;
1104 
1105 vf_err:
1106 	mlx5_eswitch_unload_vf_vports(esw, num_vfs);
1107 	return err;
1108 }
1109 
host_pf_enable_hca(struct mlx5_core_dev * dev)1110 static int host_pf_enable_hca(struct mlx5_core_dev *dev)
1111 {
1112 	if (!mlx5_core_is_ecpf(dev))
1113 		return 0;
1114 
1115 	/* Once vport and representor are ready, take out the external host PF
1116 	 * out of initializing state. Enabling HCA clears the iser->initializing
1117 	 * bit and host PF driver loading can progress.
1118 	 */
1119 	return mlx5_cmd_host_pf_enable_hca(dev);
1120 }
1121 
host_pf_disable_hca(struct mlx5_core_dev * dev)1122 static void host_pf_disable_hca(struct mlx5_core_dev *dev)
1123 {
1124 	if (!mlx5_core_is_ecpf(dev))
1125 		return;
1126 
1127 	mlx5_cmd_host_pf_disable_hca(dev);
1128 }
1129 
1130 /* mlx5_eswitch_enable_pf_vf_vports() enables vports of PF, ECPF and VFs
1131  * whichever are present on the eswitch.
1132  */
1133 int
mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch * esw,enum mlx5_eswitch_vport_event enabled_events)1134 mlx5_eswitch_enable_pf_vf_vports(struct mlx5_eswitch *esw,
1135 				 enum mlx5_eswitch_vport_event enabled_events)
1136 {
1137 	int ret;
1138 
1139 	/* Enable PF vport */
1140 	ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_PF, enabled_events);
1141 	if (ret)
1142 		return ret;
1143 
1144 	/* Enable external host PF HCA */
1145 	ret = host_pf_enable_hca(esw->dev);
1146 	if (ret)
1147 		goto pf_hca_err;
1148 
1149 	/* Enable ECPF vport */
1150 	if (mlx5_ecpf_vport_exists(esw->dev)) {
1151 		ret = mlx5_eswitch_load_vport(esw, MLX5_VPORT_ECPF, enabled_events);
1152 		if (ret)
1153 			goto ecpf_err;
1154 	}
1155 
1156 	/* Enable VF vports */
1157 	ret = mlx5_eswitch_load_vf_vports(esw, esw->esw_funcs.num_vfs,
1158 					  enabled_events);
1159 	if (ret)
1160 		goto vf_err;
1161 	return 0;
1162 
1163 vf_err:
1164 	if (mlx5_ecpf_vport_exists(esw->dev))
1165 		mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1166 ecpf_err:
1167 	host_pf_disable_hca(esw->dev);
1168 pf_hca_err:
1169 	mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1170 	return ret;
1171 }
1172 
1173 /* mlx5_eswitch_disable_pf_vf_vports() disables vports of PF, ECPF and VFs
1174  * whichever are previously enabled on the eswitch.
1175  */
mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch * esw)1176 void mlx5_eswitch_disable_pf_vf_vports(struct mlx5_eswitch *esw)
1177 {
1178 	mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1179 
1180 	if (mlx5_ecpf_vport_exists(esw->dev))
1181 		mlx5_eswitch_unload_vport(esw, MLX5_VPORT_ECPF);
1182 
1183 	host_pf_disable_hca(esw->dev);
1184 	mlx5_eswitch_unload_vport(esw, MLX5_VPORT_PF);
1185 }
1186 
mlx5_eswitch_get_devlink_param(struct mlx5_eswitch * esw)1187 static void mlx5_eswitch_get_devlink_param(struct mlx5_eswitch *esw)
1188 {
1189 	struct devlink *devlink = priv_to_devlink(esw->dev);
1190 	union devlink_param_value val;
1191 	int err;
1192 
1193 	err = devl_param_driverinit_value_get(devlink,
1194 					      MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
1195 					      &val);
1196 	if (!err) {
1197 		esw->params.large_group_num = val.vu32;
1198 	} else {
1199 		esw_warn(esw->dev,
1200 			 "Devlink can't get param fdb_large_groups, uses default (%d).\n",
1201 			 ESW_OFFLOADS_DEFAULT_NUM_GROUPS);
1202 		esw->params.large_group_num = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
1203 	}
1204 }
1205 
1206 static void
mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch * esw,int num_vfs)1207 mlx5_eswitch_update_num_of_vfs(struct mlx5_eswitch *esw, int num_vfs)
1208 {
1209 	const u32 *out;
1210 
1211 	if (num_vfs < 0)
1212 		return;
1213 
1214 	if (!mlx5_core_is_ecpf_esw_manager(esw->dev)) {
1215 		esw->esw_funcs.num_vfs = num_vfs;
1216 		return;
1217 	}
1218 
1219 	out = mlx5_esw_query_functions(esw->dev);
1220 	if (IS_ERR(out))
1221 		return;
1222 
1223 	esw->esw_funcs.num_vfs = MLX5_GET(query_esw_functions_out, out,
1224 					  host_params_context.host_num_of_vfs);
1225 	kvfree(out);
1226 }
1227 
mlx5_esw_mode_change_notify(struct mlx5_eswitch * esw,u16 mode)1228 static void mlx5_esw_mode_change_notify(struct mlx5_eswitch *esw, u16 mode)
1229 {
1230 	struct mlx5_esw_event_info info = {};
1231 
1232 	info.new_mode = mode;
1233 
1234 	blocking_notifier_call_chain(&esw->n_head, 0, &info);
1235 }
1236 
mlx5_esw_acls_ns_init(struct mlx5_eswitch * esw)1237 static int mlx5_esw_acls_ns_init(struct mlx5_eswitch *esw)
1238 {
1239 	struct mlx5_core_dev *dev = esw->dev;
1240 	int total_vports;
1241 	int err;
1242 
1243 	if (esw->flags & MLX5_ESWITCH_VPORT_ACL_NS_CREATED)
1244 		return 0;
1245 
1246 	total_vports = mlx5_eswitch_get_total_vports(dev);
1247 
1248 	if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support)) {
1249 		err = mlx5_fs_egress_acls_init(dev, total_vports);
1250 		if (err)
1251 			return err;
1252 	} else {
1253 		esw_warn(dev, "egress ACL is not supported by FW\n");
1254 	}
1255 
1256 	if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support)) {
1257 		err = mlx5_fs_ingress_acls_init(dev, total_vports);
1258 		if (err)
1259 			goto err;
1260 	} else {
1261 		esw_warn(dev, "ingress ACL is not supported by FW\n");
1262 	}
1263 	esw->flags |= MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1264 	return 0;
1265 
1266 err:
1267 	if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1268 		mlx5_fs_egress_acls_cleanup(dev);
1269 	return err;
1270 }
1271 
mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch * esw)1272 static void mlx5_esw_acls_ns_cleanup(struct mlx5_eswitch *esw)
1273 {
1274 	struct mlx5_core_dev *dev = esw->dev;
1275 
1276 	esw->flags &= ~MLX5_ESWITCH_VPORT_ACL_NS_CREATED;
1277 	if (MLX5_CAP_ESW_INGRESS_ACL(dev, ft_support))
1278 		mlx5_fs_ingress_acls_cleanup(dev);
1279 	if (MLX5_CAP_ESW_EGRESS_ACL(dev, ft_support))
1280 		mlx5_fs_egress_acls_cleanup(dev);
1281 }
1282 
1283 /**
1284  * mlx5_eswitch_enable_locked - Enable eswitch
1285  * @esw:	Pointer to eswitch
1286  * @num_vfs:	Enable eswitch for given number of VFs. This is optional.
1287  *		Valid value are 0, > 0 and MLX5_ESWITCH_IGNORE_NUM_VFS.
1288  *		Caller should pass num_vfs > 0 when enabling eswitch for
1289  *		vf vports. Caller should pass num_vfs = 0, when eswitch
1290  *		is enabled without sriov VFs or when caller
1291  *		is unaware of the sriov state of the host PF on ECPF based
1292  *		eswitch. Caller should pass < 0 when num_vfs should be
1293  *		completely ignored. This is typically the case when eswitch
1294  *		is enabled without sriov regardless of PF/ECPF system.
1295  * mlx5_eswitch_enable_locked() Enables eswitch in either legacy or offloads
1296  * mode. If num_vfs >=0 is provided, it setup VF related eswitch vports.
1297  * It returns 0 on success or error code on failure.
1298  */
mlx5_eswitch_enable_locked(struct mlx5_eswitch * esw,int num_vfs)1299 int mlx5_eswitch_enable_locked(struct mlx5_eswitch *esw, int num_vfs)
1300 {
1301 	int err;
1302 
1303 	lockdep_assert_held(&esw->mode_lock);
1304 
1305 	if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ft_support)) {
1306 		esw_warn(esw->dev, "FDB is not supported, aborting ...\n");
1307 		return -EOPNOTSUPP;
1308 	}
1309 
1310 	mlx5_eswitch_get_devlink_param(esw);
1311 
1312 	err = mlx5_esw_acls_ns_init(esw);
1313 	if (err)
1314 		return err;
1315 
1316 	mlx5_eswitch_update_num_of_vfs(esw, num_vfs);
1317 
1318 	if (esw->mode == MLX5_ESWITCH_LEGACY) {
1319 		err = esw_legacy_enable(esw);
1320 	} else {
1321 		mlx5_rescan_drivers(esw->dev);
1322 		err = esw_offloads_enable(esw);
1323 	}
1324 
1325 	if (err)
1326 		goto abort;
1327 
1328 	esw->fdb_table.flags |= MLX5_ESW_FDB_CREATED;
1329 
1330 	mlx5_eswitch_event_handlers_register(esw);
1331 
1332 	esw_info(esw->dev, "Enable: mode(%s), nvfs(%d), active vports(%d)\n",
1333 		 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1334 		 esw->esw_funcs.num_vfs, esw->enabled_vports);
1335 
1336 	mlx5_esw_mode_change_notify(esw, esw->mode);
1337 
1338 	return 0;
1339 
1340 abort:
1341 	mlx5_esw_acls_ns_cleanup(esw);
1342 	return err;
1343 }
1344 
1345 /**
1346  * mlx5_eswitch_enable - Enable eswitch
1347  * @esw:	Pointer to eswitch
1348  * @num_vfs:	Enable eswitch switch for given number of VFs.
1349  *		Caller must pass num_vfs > 0 when enabling eswitch for
1350  *		vf vports.
1351  * mlx5_eswitch_enable() returns 0 on success or error code on failure.
1352  */
mlx5_eswitch_enable(struct mlx5_eswitch * esw,int num_vfs)1353 int mlx5_eswitch_enable(struct mlx5_eswitch *esw, int num_vfs)
1354 {
1355 	bool toggle_lag;
1356 	int ret;
1357 
1358 	if (!mlx5_esw_allowed(esw))
1359 		return 0;
1360 
1361 	devl_assert_locked(priv_to_devlink(esw->dev));
1362 
1363 	toggle_lag = !mlx5_esw_is_fdb_created(esw);
1364 
1365 	if (toggle_lag)
1366 		mlx5_lag_disable_change(esw->dev);
1367 
1368 	down_write(&esw->mode_lock);
1369 	if (!mlx5_esw_is_fdb_created(esw)) {
1370 		ret = mlx5_eswitch_enable_locked(esw, num_vfs);
1371 	} else {
1372 		enum mlx5_eswitch_vport_event vport_events;
1373 
1374 		vport_events = (esw->mode == MLX5_ESWITCH_LEGACY) ?
1375 					MLX5_LEGACY_SRIOV_VPORT_EVENTS : MLX5_VPORT_UC_ADDR_CHANGE;
1376 		ret = mlx5_eswitch_load_vf_vports(esw, num_vfs, vport_events);
1377 		if (!ret)
1378 			esw->esw_funcs.num_vfs = num_vfs;
1379 	}
1380 	up_write(&esw->mode_lock);
1381 
1382 	if (toggle_lag)
1383 		mlx5_lag_enable_change(esw->dev);
1384 
1385 	return ret;
1386 }
1387 
1388 /* When disabling sriov, free driver level resources. */
mlx5_eswitch_disable_sriov(struct mlx5_eswitch * esw,bool clear_vf)1389 void mlx5_eswitch_disable_sriov(struct mlx5_eswitch *esw, bool clear_vf)
1390 {
1391 	if (!mlx5_esw_allowed(esw))
1392 		return;
1393 
1394 	devl_assert_locked(priv_to_devlink(esw->dev));
1395 	down_write(&esw->mode_lock);
1396 	/* If driver is unloaded, this function is called twice by remove_one()
1397 	 * and mlx5_unload(). Prevent the second call.
1398 	 */
1399 	if (!esw->esw_funcs.num_vfs && !clear_vf)
1400 		goto unlock;
1401 
1402 	esw_info(esw->dev, "Unload vfs: mode(%s), nvfs(%d), active vports(%d)\n",
1403 		 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1404 		 esw->esw_funcs.num_vfs, esw->enabled_vports);
1405 
1406 	mlx5_eswitch_unload_vf_vports(esw, esw->esw_funcs.num_vfs);
1407 	if (clear_vf)
1408 		mlx5_eswitch_clear_vf_vports_info(esw);
1409 
1410 	if (esw->mode == MLX5_ESWITCH_OFFLOADS) {
1411 		struct devlink *devlink = priv_to_devlink(esw->dev);
1412 
1413 		devl_rate_nodes_destroy(devlink);
1414 	}
1415 	/* Destroy legacy fdb when disabling sriov in legacy mode. */
1416 	if (esw->mode == MLX5_ESWITCH_LEGACY)
1417 		mlx5_eswitch_disable_locked(esw);
1418 
1419 	esw->esw_funcs.num_vfs = 0;
1420 
1421 unlock:
1422 	up_write(&esw->mode_lock);
1423 }
1424 
1425 /* Free resources for corresponding eswitch mode. It is called by devlink
1426  * when changing eswitch mode or modprobe when unloading driver.
1427  */
mlx5_eswitch_disable_locked(struct mlx5_eswitch * esw)1428 void mlx5_eswitch_disable_locked(struct mlx5_eswitch *esw)
1429 {
1430 	struct devlink *devlink = priv_to_devlink(esw->dev);
1431 
1432 	/* Notify eswitch users that it is exiting from current mode.
1433 	 * So that it can do necessary cleanup before the eswitch is disabled.
1434 	 */
1435 	mlx5_esw_mode_change_notify(esw, MLX5_ESWITCH_LEGACY);
1436 
1437 	mlx5_eswitch_event_handlers_unregister(esw);
1438 
1439 	esw_info(esw->dev, "Disable: mode(%s), nvfs(%d), active vports(%d)\n",
1440 		 esw->mode == MLX5_ESWITCH_LEGACY ? "LEGACY" : "OFFLOADS",
1441 		 esw->esw_funcs.num_vfs, esw->enabled_vports);
1442 
1443 	if (esw->fdb_table.flags & MLX5_ESW_FDB_CREATED) {
1444 		esw->fdb_table.flags &= ~MLX5_ESW_FDB_CREATED;
1445 		if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1446 			esw_offloads_disable(esw);
1447 		else if (esw->mode == MLX5_ESWITCH_LEGACY)
1448 			esw_legacy_disable(esw);
1449 		mlx5_esw_acls_ns_cleanup(esw);
1450 	}
1451 
1452 	if (esw->mode == MLX5_ESWITCH_OFFLOADS)
1453 		devl_rate_nodes_destroy(devlink);
1454 }
1455 
mlx5_eswitch_disable(struct mlx5_eswitch * esw)1456 void mlx5_eswitch_disable(struct mlx5_eswitch *esw)
1457 {
1458 	if (!mlx5_esw_allowed(esw))
1459 		return;
1460 
1461 	devl_assert_locked(priv_to_devlink(esw->dev));
1462 	mlx5_lag_disable_change(esw->dev);
1463 	down_write(&esw->mode_lock);
1464 	mlx5_eswitch_disable_locked(esw);
1465 	esw->mode = MLX5_ESWITCH_LEGACY;
1466 	up_write(&esw->mode_lock);
1467 	mlx5_lag_enable_change(esw->dev);
1468 }
1469 
mlx5_query_hca_cap_host_pf(struct mlx5_core_dev * dev,void * out)1470 static int mlx5_query_hca_cap_host_pf(struct mlx5_core_dev *dev, void *out)
1471 {
1472 	u16 opmod = (MLX5_CAP_GENERAL << 1) | (HCA_CAP_OPMOD_GET_MAX & 0x01);
1473 	u8 in[MLX5_ST_SZ_BYTES(query_hca_cap_in)] = {};
1474 
1475 	MLX5_SET(query_hca_cap_in, in, opcode, MLX5_CMD_OP_QUERY_HCA_CAP);
1476 	MLX5_SET(query_hca_cap_in, in, op_mod, opmod);
1477 	MLX5_SET(query_hca_cap_in, in, function_id, MLX5_VPORT_PF);
1478 	MLX5_SET(query_hca_cap_in, in, other_function, true);
1479 	return mlx5_cmd_exec_inout(dev, query_hca_cap, in, out);
1480 }
1481 
mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev * dev,u16 * max_sfs,u16 * sf_base_id)1482 int mlx5_esw_sf_max_hpf_functions(struct mlx5_core_dev *dev, u16 *max_sfs, u16 *sf_base_id)
1483 
1484 {
1485 	int query_out_sz = MLX5_ST_SZ_BYTES(query_hca_cap_out);
1486 	void *query_ctx;
1487 	void *hca_caps;
1488 	int err;
1489 
1490 	if (!mlx5_core_is_ecpf(dev) || mlx5_core_is_management_pf(dev)) {
1491 		*max_sfs = 0;
1492 		return 0;
1493 	}
1494 
1495 	query_ctx = kzalloc(query_out_sz, GFP_KERNEL);
1496 	if (!query_ctx)
1497 		return -ENOMEM;
1498 
1499 	err = mlx5_query_hca_cap_host_pf(dev, query_ctx);
1500 	if (err)
1501 		goto out_free;
1502 
1503 	hca_caps = MLX5_ADDR_OF(query_hca_cap_out, query_ctx, capability);
1504 	*max_sfs = MLX5_GET(cmd_hca_cap, hca_caps, max_num_sf);
1505 	*sf_base_id = MLX5_GET(cmd_hca_cap, hca_caps, sf_base_id);
1506 
1507 out_free:
1508 	kfree(query_ctx);
1509 	return err;
1510 }
1511 
mlx5_esw_vport_alloc(struct mlx5_eswitch * esw,struct mlx5_core_dev * dev,int index,u16 vport_num)1512 static int mlx5_esw_vport_alloc(struct mlx5_eswitch *esw, struct mlx5_core_dev *dev,
1513 				int index, u16 vport_num)
1514 {
1515 	struct mlx5_vport *vport;
1516 	int err;
1517 
1518 	vport = kzalloc(sizeof(*vport), GFP_KERNEL);
1519 	if (!vport)
1520 		return -ENOMEM;
1521 
1522 	vport->dev = esw->dev;
1523 	vport->vport = vport_num;
1524 	vport->index = index;
1525 	vport->info.link_state = MLX5_VPORT_ADMIN_STATE_AUTO;
1526 	INIT_WORK(&vport->vport_change_handler, esw_vport_change_handler);
1527 	err = xa_insert(&esw->vports, vport_num, vport, GFP_KERNEL);
1528 	if (err)
1529 		goto insert_err;
1530 
1531 	esw->total_vports++;
1532 	return 0;
1533 
1534 insert_err:
1535 	kfree(vport);
1536 	return err;
1537 }
1538 
mlx5_esw_vport_free(struct mlx5_eswitch * esw,struct mlx5_vport * vport)1539 static void mlx5_esw_vport_free(struct mlx5_eswitch *esw, struct mlx5_vport *vport)
1540 {
1541 	xa_erase(&esw->vports, vport->vport);
1542 	kfree(vport);
1543 }
1544 
mlx5_esw_vports_cleanup(struct mlx5_eswitch * esw)1545 static void mlx5_esw_vports_cleanup(struct mlx5_eswitch *esw)
1546 {
1547 	struct mlx5_vport *vport;
1548 	unsigned long i;
1549 
1550 	mlx5_esw_for_each_vport(esw, i, vport)
1551 		mlx5_esw_vport_free(esw, vport);
1552 	xa_destroy(&esw->vports);
1553 }
1554 
mlx5_esw_vports_init(struct mlx5_eswitch * esw)1555 static int mlx5_esw_vports_init(struct mlx5_eswitch *esw)
1556 {
1557 	struct mlx5_core_dev *dev = esw->dev;
1558 	u16 max_host_pf_sfs;
1559 	u16 base_sf_num;
1560 	int idx = 0;
1561 	int err;
1562 	int i;
1563 
1564 	xa_init(&esw->vports);
1565 
1566 	err = mlx5_esw_vport_alloc(esw, dev, idx, MLX5_VPORT_PF);
1567 	if (err)
1568 		goto err;
1569 	if (esw->first_host_vport == MLX5_VPORT_PF)
1570 		xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1571 	idx++;
1572 
1573 	for (i = 0; i < mlx5_core_max_vfs(dev); i++) {
1574 		err = mlx5_esw_vport_alloc(esw, dev, idx, idx);
1575 		if (err)
1576 			goto err;
1577 		xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_VF);
1578 		xa_set_mark(&esw->vports, idx, MLX5_ESW_VPT_HOST_FN);
1579 		idx++;
1580 	}
1581 	base_sf_num = mlx5_sf_start_function_id(dev);
1582 	for (i = 0; i < mlx5_sf_max_functions(dev); i++) {
1583 		err = mlx5_esw_vport_alloc(esw, dev, idx, base_sf_num + i);
1584 		if (err)
1585 			goto err;
1586 		xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1587 		idx++;
1588 	}
1589 
1590 	err = mlx5_esw_sf_max_hpf_functions(dev, &max_host_pf_sfs, &base_sf_num);
1591 	if (err)
1592 		goto err;
1593 	for (i = 0; i < max_host_pf_sfs; i++) {
1594 		err = mlx5_esw_vport_alloc(esw, dev, idx, base_sf_num + i);
1595 		if (err)
1596 			goto err;
1597 		xa_set_mark(&esw->vports, base_sf_num + i, MLX5_ESW_VPT_SF);
1598 		idx++;
1599 	}
1600 
1601 	if (mlx5_ecpf_vport_exists(dev)) {
1602 		err = mlx5_esw_vport_alloc(esw, dev, idx, MLX5_VPORT_ECPF);
1603 		if (err)
1604 			goto err;
1605 		idx++;
1606 	}
1607 	err = mlx5_esw_vport_alloc(esw, dev, idx, MLX5_VPORT_UPLINK);
1608 	if (err)
1609 		goto err;
1610 	return 0;
1611 
1612 err:
1613 	mlx5_esw_vports_cleanup(esw);
1614 	return err;
1615 }
1616 
mlx5_eswitch_init(struct mlx5_core_dev * dev)1617 int mlx5_eswitch_init(struct mlx5_core_dev *dev)
1618 {
1619 	struct mlx5_eswitch *esw;
1620 	int err;
1621 
1622 	if (!MLX5_VPORT_MANAGER(dev))
1623 		return 0;
1624 
1625 	esw = kzalloc(sizeof(*esw), GFP_KERNEL);
1626 	if (!esw)
1627 		return -ENOMEM;
1628 
1629 	esw->dev = dev;
1630 	esw->manager_vport = mlx5_eswitch_manager_vport(dev);
1631 	esw->first_host_vport = mlx5_eswitch_first_host_vport_num(dev);
1632 
1633 	esw->work_queue = create_singlethread_workqueue("mlx5_esw_wq");
1634 	if (!esw->work_queue) {
1635 		err = -ENOMEM;
1636 		goto abort;
1637 	}
1638 
1639 	err = mlx5_esw_vports_init(esw);
1640 	if (err)
1641 		goto abort;
1642 
1643 	err = esw_offloads_init(esw);
1644 	if (err)
1645 		goto reps_err;
1646 
1647 	mutex_init(&esw->offloads.encap_tbl_lock);
1648 	hash_init(esw->offloads.encap_tbl);
1649 	mutex_init(&esw->offloads.decap_tbl_lock);
1650 	hash_init(esw->offloads.decap_tbl);
1651 	mlx5e_mod_hdr_tbl_init(&esw->offloads.mod_hdr);
1652 	atomic64_set(&esw->offloads.num_flows, 0);
1653 	ida_init(&esw->offloads.vport_metadata_ida);
1654 	xa_init_flags(&esw->offloads.vhca_map, XA_FLAGS_ALLOC);
1655 	mutex_init(&esw->state_lock);
1656 	init_rwsem(&esw->mode_lock);
1657 	refcount_set(&esw->qos.refcnt, 0);
1658 
1659 	esw->enabled_vports = 0;
1660 	esw->mode = MLX5_ESWITCH_LEGACY;
1661 	esw->offloads.inline_mode = MLX5_INLINE_MODE_NONE;
1662 	if (MLX5_CAP_ESW_FLOWTABLE_FDB(dev, reformat) &&
1663 	    MLX5_CAP_ESW_FLOWTABLE_FDB(dev, decap))
1664 		esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_BASIC;
1665 	else
1666 		esw->offloads.encap = DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1667 	if (MLX5_ESWITCH_MANAGER(dev) &&
1668 	    mlx5_esw_vport_match_metadata_supported(esw))
1669 		esw->flags |= MLX5_ESWITCH_VPORT_MATCH_METADATA;
1670 
1671 	dev->priv.eswitch = esw;
1672 	BLOCKING_INIT_NOTIFIER_HEAD(&esw->n_head);
1673 
1674 	esw->dbgfs = debugfs_create_dir("esw", mlx5_debugfs_get_dev_root(esw->dev));
1675 	esw_info(dev,
1676 		 "Total vports %d, per vport: max uc(%d) max mc(%d)\n",
1677 		 esw->total_vports,
1678 		 MLX5_MAX_UC_PER_VPORT(dev),
1679 		 MLX5_MAX_MC_PER_VPORT(dev));
1680 	return 0;
1681 
1682 reps_err:
1683 	mlx5_esw_vports_cleanup(esw);
1684 abort:
1685 	if (esw->work_queue)
1686 		destroy_workqueue(esw->work_queue);
1687 	kfree(esw);
1688 	return err;
1689 }
1690 
mlx5_eswitch_cleanup(struct mlx5_eswitch * esw)1691 void mlx5_eswitch_cleanup(struct mlx5_eswitch *esw)
1692 {
1693 	if (!esw || !MLX5_VPORT_MANAGER(esw->dev))
1694 		return;
1695 
1696 	esw_info(esw->dev, "cleanup\n");
1697 
1698 	debugfs_remove_recursive(esw->dbgfs);
1699 	esw->dev->priv.eswitch = NULL;
1700 	destroy_workqueue(esw->work_queue);
1701 	WARN_ON(refcount_read(&esw->qos.refcnt));
1702 	mutex_destroy(&esw->state_lock);
1703 	WARN_ON(!xa_empty(&esw->offloads.vhca_map));
1704 	xa_destroy(&esw->offloads.vhca_map);
1705 	ida_destroy(&esw->offloads.vport_metadata_ida);
1706 	mlx5e_mod_hdr_tbl_destroy(&esw->offloads.mod_hdr);
1707 	mutex_destroy(&esw->offloads.encap_tbl_lock);
1708 	mutex_destroy(&esw->offloads.decap_tbl_lock);
1709 	esw_offloads_cleanup(esw);
1710 	mlx5_esw_vports_cleanup(esw);
1711 	kfree(esw);
1712 }
1713 
1714 /* Vport Administration */
1715 static int
mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch * esw,struct mlx5_vport * evport,const u8 * mac)1716 mlx5_esw_set_vport_mac_locked(struct mlx5_eswitch *esw,
1717 			      struct mlx5_vport *evport, const u8 *mac)
1718 {
1719 	u16 vport_num = evport->vport;
1720 	u64 node_guid;
1721 	int err = 0;
1722 
1723 	if (is_multicast_ether_addr(mac))
1724 		return -EINVAL;
1725 
1726 	if (evport->info.spoofchk && !is_valid_ether_addr(mac))
1727 		mlx5_core_warn(esw->dev,
1728 			       "Set invalid MAC while spoofchk is on, vport(%d)\n",
1729 			       vport_num);
1730 
1731 	err = mlx5_modify_nic_vport_mac_address(esw->dev, vport_num, mac);
1732 	if (err) {
1733 		mlx5_core_warn(esw->dev,
1734 			       "Failed to mlx5_modify_nic_vport_mac vport(%d) err=(%d)\n",
1735 			       vport_num, err);
1736 		return err;
1737 	}
1738 
1739 	node_guid_gen_from_mac(&node_guid, mac);
1740 	err = mlx5_modify_nic_vport_node_guid(esw->dev, vport_num, node_guid);
1741 	if (err)
1742 		mlx5_core_warn(esw->dev,
1743 			       "Failed to set vport %d node guid, err = %d. RDMA_CM will not function properly for this VF.\n",
1744 			       vport_num, err);
1745 
1746 	ether_addr_copy(evport->info.mac, mac);
1747 	evport->info.node_guid = node_guid;
1748 	if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY)
1749 		err = esw_acl_ingress_lgcy_setup(esw, evport);
1750 
1751 	return err;
1752 }
1753 
mlx5_eswitch_set_vport_mac(struct mlx5_eswitch * esw,u16 vport,const u8 * mac)1754 int mlx5_eswitch_set_vport_mac(struct mlx5_eswitch *esw,
1755 			       u16 vport, const u8 *mac)
1756 {
1757 	struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1758 	int err = 0;
1759 
1760 	if (IS_ERR(evport))
1761 		return PTR_ERR(evport);
1762 
1763 	mutex_lock(&esw->state_lock);
1764 	err = mlx5_esw_set_vport_mac_locked(esw, evport, mac);
1765 	mutex_unlock(&esw->state_lock);
1766 	return err;
1767 }
1768 
mlx5_esw_check_port_type(struct mlx5_eswitch * esw,u16 vport_num,xa_mark_t mark)1769 static bool mlx5_esw_check_port_type(struct mlx5_eswitch *esw, u16 vport_num, xa_mark_t mark)
1770 {
1771 	struct mlx5_vport *vport;
1772 
1773 	vport = mlx5_eswitch_get_vport(esw, vport_num);
1774 	if (IS_ERR(vport))
1775 		return false;
1776 
1777 	return xa_get_mark(&esw->vports, vport_num, mark);
1778 }
1779 
mlx5_eswitch_is_vf_vport(struct mlx5_eswitch * esw,u16 vport_num)1780 bool mlx5_eswitch_is_vf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1781 {
1782 	return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_VF);
1783 }
1784 
mlx5_esw_is_sf_vport(struct mlx5_eswitch * esw,u16 vport_num)1785 bool mlx5_esw_is_sf_vport(struct mlx5_eswitch *esw, u16 vport_num)
1786 {
1787 	return mlx5_esw_check_port_type(esw, vport_num, MLX5_ESW_VPT_SF);
1788 }
1789 
mlx5_eswitch_set_vport_state(struct mlx5_eswitch * esw,u16 vport,int link_state)1790 int mlx5_eswitch_set_vport_state(struct mlx5_eswitch *esw,
1791 				 u16 vport, int link_state)
1792 {
1793 	struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1794 	int opmod = MLX5_VPORT_STATE_OP_MOD_ESW_VPORT;
1795 	int other_vport = 1;
1796 	int err = 0;
1797 
1798 	if (!mlx5_esw_allowed(esw))
1799 		return -EPERM;
1800 	if (IS_ERR(evport))
1801 		return PTR_ERR(evport);
1802 
1803 	if (vport == MLX5_VPORT_UPLINK) {
1804 		opmod = MLX5_VPORT_STATE_OP_MOD_UPLINK;
1805 		other_vport = 0;
1806 		vport = 0;
1807 	}
1808 	mutex_lock(&esw->state_lock);
1809 	if (esw->mode != MLX5_ESWITCH_LEGACY) {
1810 		err = -EOPNOTSUPP;
1811 		goto unlock;
1812 	}
1813 
1814 	err = mlx5_modify_vport_admin_state(esw->dev, opmod, vport, other_vport, link_state);
1815 	if (err) {
1816 		mlx5_core_warn(esw->dev, "Failed to set vport %d link state, opmod = %d, err = %d",
1817 			       vport, opmod, err);
1818 		goto unlock;
1819 	}
1820 
1821 	evport->info.link_state = link_state;
1822 
1823 unlock:
1824 	mutex_unlock(&esw->state_lock);
1825 	return err;
1826 }
1827 
mlx5_eswitch_get_vport_config(struct mlx5_eswitch * esw,u16 vport,struct ifla_vf_info * ivi)1828 int mlx5_eswitch_get_vport_config(struct mlx5_eswitch *esw,
1829 				  u16 vport, struct ifla_vf_info *ivi)
1830 {
1831 	struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1832 
1833 	if (IS_ERR(evport))
1834 		return PTR_ERR(evport);
1835 
1836 	memset(ivi, 0, sizeof(*ivi));
1837 	ivi->vf = vport - 1;
1838 
1839 	mutex_lock(&esw->state_lock);
1840 	ether_addr_copy(ivi->mac, evport->info.mac);
1841 	ivi->linkstate = evport->info.link_state;
1842 	ivi->vlan = evport->info.vlan;
1843 	ivi->qos = evport->info.qos;
1844 	ivi->spoofchk = evport->info.spoofchk;
1845 	ivi->trusted = evport->info.trusted;
1846 	if (evport->qos.enabled) {
1847 		ivi->min_tx_rate = evport->qos.min_rate;
1848 		ivi->max_tx_rate = evport->qos.max_rate;
1849 	}
1850 	mutex_unlock(&esw->state_lock);
1851 
1852 	return 0;
1853 }
1854 
__mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch * esw,u16 vport,u16 vlan,u8 qos,u8 set_flags)1855 int __mlx5_eswitch_set_vport_vlan(struct mlx5_eswitch *esw,
1856 				  u16 vport, u16 vlan, u8 qos, u8 set_flags)
1857 {
1858 	struct mlx5_vport *evport = mlx5_eswitch_get_vport(esw, vport);
1859 	bool vst_mode_steering = esw_vst_mode_is_steering(esw);
1860 	int err = 0;
1861 
1862 	if (IS_ERR(evport))
1863 		return PTR_ERR(evport);
1864 	if (vlan > 4095 || qos > 7)
1865 		return -EINVAL;
1866 
1867 	if (esw->mode == MLX5_ESWITCH_OFFLOADS || !vst_mode_steering) {
1868 		err = modify_esw_vport_cvlan(esw->dev, vport, vlan, qos, set_flags);
1869 		if (err)
1870 			return err;
1871 	}
1872 
1873 	evport->info.vlan = vlan;
1874 	evport->info.qos = qos;
1875 	if (evport->enabled && esw->mode == MLX5_ESWITCH_LEGACY) {
1876 		err = esw_acl_ingress_lgcy_setup(esw, evport);
1877 		if (err)
1878 			return err;
1879 		err = esw_acl_egress_lgcy_setup(esw, evport);
1880 	}
1881 
1882 	return err;
1883 }
1884 
mlx5_eswitch_get_vport_stats(struct mlx5_eswitch * esw,u16 vport_num,struct ifla_vf_stats * vf_stats)1885 int mlx5_eswitch_get_vport_stats(struct mlx5_eswitch *esw,
1886 				 u16 vport_num,
1887 				 struct ifla_vf_stats *vf_stats)
1888 {
1889 	struct mlx5_vport *vport = mlx5_eswitch_get_vport(esw, vport_num);
1890 	int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
1891 	u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {};
1892 	struct mlx5_vport_drop_stats stats = {};
1893 	int err = 0;
1894 	u32 *out;
1895 
1896 	if (IS_ERR(vport))
1897 		return PTR_ERR(vport);
1898 
1899 	out = kvzalloc(outlen, GFP_KERNEL);
1900 	if (!out)
1901 		return -ENOMEM;
1902 
1903 	MLX5_SET(query_vport_counter_in, in, opcode,
1904 		 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
1905 	MLX5_SET(query_vport_counter_in, in, op_mod, 0);
1906 	MLX5_SET(query_vport_counter_in, in, vport_number, vport->vport);
1907 	MLX5_SET(query_vport_counter_in, in, other_vport, 1);
1908 
1909 	err = mlx5_cmd_exec_inout(esw->dev, query_vport_counter, in, out);
1910 	if (err)
1911 		goto free_out;
1912 
1913 	#define MLX5_GET_CTR(p, x) \
1914 		MLX5_GET64(query_vport_counter_out, p, x)
1915 
1916 	memset(vf_stats, 0, sizeof(*vf_stats));
1917 	vf_stats->rx_packets =
1918 		MLX5_GET_CTR(out, received_eth_unicast.packets) +
1919 		MLX5_GET_CTR(out, received_ib_unicast.packets) +
1920 		MLX5_GET_CTR(out, received_eth_multicast.packets) +
1921 		MLX5_GET_CTR(out, received_ib_multicast.packets) +
1922 		MLX5_GET_CTR(out, received_eth_broadcast.packets);
1923 
1924 	vf_stats->rx_bytes =
1925 		MLX5_GET_CTR(out, received_eth_unicast.octets) +
1926 		MLX5_GET_CTR(out, received_ib_unicast.octets) +
1927 		MLX5_GET_CTR(out, received_eth_multicast.octets) +
1928 		MLX5_GET_CTR(out, received_ib_multicast.octets) +
1929 		MLX5_GET_CTR(out, received_eth_broadcast.octets);
1930 
1931 	vf_stats->tx_packets =
1932 		MLX5_GET_CTR(out, transmitted_eth_unicast.packets) +
1933 		MLX5_GET_CTR(out, transmitted_ib_unicast.packets) +
1934 		MLX5_GET_CTR(out, transmitted_eth_multicast.packets) +
1935 		MLX5_GET_CTR(out, transmitted_ib_multicast.packets) +
1936 		MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
1937 
1938 	vf_stats->tx_bytes =
1939 		MLX5_GET_CTR(out, transmitted_eth_unicast.octets) +
1940 		MLX5_GET_CTR(out, transmitted_ib_unicast.octets) +
1941 		MLX5_GET_CTR(out, transmitted_eth_multicast.octets) +
1942 		MLX5_GET_CTR(out, transmitted_ib_multicast.octets) +
1943 		MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
1944 
1945 	vf_stats->multicast =
1946 		MLX5_GET_CTR(out, received_eth_multicast.packets) +
1947 		MLX5_GET_CTR(out, received_ib_multicast.packets);
1948 
1949 	vf_stats->broadcast =
1950 		MLX5_GET_CTR(out, received_eth_broadcast.packets);
1951 
1952 	err = mlx5_esw_query_vport_drop_stats(esw->dev, vport, &stats);
1953 	if (err)
1954 		goto free_out;
1955 	vf_stats->rx_dropped = stats.rx_dropped;
1956 	vf_stats->tx_dropped = stats.tx_dropped;
1957 
1958 free_out:
1959 	kvfree(out);
1960 	return err;
1961 }
1962 
mlx5_eswitch_mode(const struct mlx5_core_dev * dev)1963 u8 mlx5_eswitch_mode(const struct mlx5_core_dev *dev)
1964 {
1965 	struct mlx5_eswitch *esw = dev->priv.eswitch;
1966 
1967 	return mlx5_esw_allowed(esw) ? esw->mode : MLX5_ESWITCH_LEGACY;
1968 }
1969 EXPORT_SYMBOL_GPL(mlx5_eswitch_mode);
1970 
1971 enum devlink_eswitch_encap_mode
mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev * dev)1972 mlx5_eswitch_get_encap_mode(const struct mlx5_core_dev *dev)
1973 {
1974 	struct mlx5_eswitch *esw;
1975 
1976 	esw = dev->priv.eswitch;
1977 	return (mlx5_eswitch_mode(dev) == MLX5_ESWITCH_OFFLOADS)  ? esw->offloads.encap :
1978 		DEVLINK_ESWITCH_ENCAP_MODE_NONE;
1979 }
1980 EXPORT_SYMBOL(mlx5_eswitch_get_encap_mode);
1981 
mlx5_esw_multipath_prereq(struct mlx5_core_dev * dev0,struct mlx5_core_dev * dev1)1982 bool mlx5_esw_multipath_prereq(struct mlx5_core_dev *dev0,
1983 			       struct mlx5_core_dev *dev1)
1984 {
1985 	return (dev0->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS &&
1986 		dev1->priv.eswitch->mode == MLX5_ESWITCH_OFFLOADS);
1987 }
1988 
mlx5_esw_event_notifier_register(struct mlx5_eswitch * esw,struct notifier_block * nb)1989 int mlx5_esw_event_notifier_register(struct mlx5_eswitch *esw, struct notifier_block *nb)
1990 {
1991 	return blocking_notifier_chain_register(&esw->n_head, nb);
1992 }
1993 
mlx5_esw_event_notifier_unregister(struct mlx5_eswitch * esw,struct notifier_block * nb)1994 void mlx5_esw_event_notifier_unregister(struct mlx5_eswitch *esw, struct notifier_block *nb)
1995 {
1996 	blocking_notifier_chain_unregister(&esw->n_head, nb);
1997 }
1998 
1999 /**
2000  * mlx5_esw_hold() - Try to take a read lock on esw mode lock.
2001  * @mdev: mlx5 core device.
2002  *
2003  * Should be called by esw resources callers.
2004  *
2005  * Return: true on success or false.
2006  */
mlx5_esw_hold(struct mlx5_core_dev * mdev)2007 bool mlx5_esw_hold(struct mlx5_core_dev *mdev)
2008 {
2009 	struct mlx5_eswitch *esw = mdev->priv.eswitch;
2010 
2011 	/* e.g. VF doesn't have eswitch so nothing to do */
2012 	if (!mlx5_esw_allowed(esw))
2013 		return true;
2014 
2015 	if (down_read_trylock(&esw->mode_lock) != 0)
2016 		return true;
2017 
2018 	return false;
2019 }
2020 
2021 /**
2022  * mlx5_esw_release() - Release a read lock on esw mode lock.
2023  * @mdev: mlx5 core device.
2024  */
mlx5_esw_release(struct mlx5_core_dev * mdev)2025 void mlx5_esw_release(struct mlx5_core_dev *mdev)
2026 {
2027 	struct mlx5_eswitch *esw = mdev->priv.eswitch;
2028 
2029 	if (mlx5_esw_allowed(esw))
2030 		up_read(&esw->mode_lock);
2031 }
2032 
2033 /**
2034  * mlx5_esw_get() - Increase esw user count.
2035  * @mdev: mlx5 core device.
2036  */
mlx5_esw_get(struct mlx5_core_dev * mdev)2037 void mlx5_esw_get(struct mlx5_core_dev *mdev)
2038 {
2039 	struct mlx5_eswitch *esw = mdev->priv.eswitch;
2040 
2041 	if (mlx5_esw_allowed(esw))
2042 		atomic64_inc(&esw->user_count);
2043 }
2044 
2045 /**
2046  * mlx5_esw_put() - Decrease esw user count.
2047  * @mdev: mlx5 core device.
2048  */
mlx5_esw_put(struct mlx5_core_dev * mdev)2049 void mlx5_esw_put(struct mlx5_core_dev *mdev)
2050 {
2051 	struct mlx5_eswitch *esw = mdev->priv.eswitch;
2052 
2053 	if (mlx5_esw_allowed(esw))
2054 		atomic64_dec_if_positive(&esw->user_count);
2055 }
2056 
2057 /**
2058  * mlx5_esw_try_lock() - Take a write lock on esw mode lock.
2059  * @esw: eswitch device.
2060  *
2061  * Should be called by esw mode change routine.
2062  *
2063  * Return:
2064  * * 0       - esw mode if successfully locked and refcount is 0.
2065  * * -EBUSY  - refcount is not 0.
2066  * * -EINVAL - In the middle of switching mode or lock is already held.
2067  */
mlx5_esw_try_lock(struct mlx5_eswitch * esw)2068 int mlx5_esw_try_lock(struct mlx5_eswitch *esw)
2069 {
2070 	if (down_write_trylock(&esw->mode_lock) == 0)
2071 		return -EINVAL;
2072 
2073 	if (atomic64_read(&esw->user_count) > 0) {
2074 		up_write(&esw->mode_lock);
2075 		return -EBUSY;
2076 	}
2077 
2078 	return esw->mode;
2079 }
2080 
2081 /**
2082  * mlx5_esw_unlock() - Release write lock on esw mode lock
2083  * @esw: eswitch device.
2084  */
mlx5_esw_unlock(struct mlx5_eswitch * esw)2085 void mlx5_esw_unlock(struct mlx5_eswitch *esw)
2086 {
2087 	up_write(&esw->mode_lock);
2088 }
2089 
2090 /**
2091  * mlx5_eswitch_get_total_vports - Get total vports of the eswitch
2092  *
2093  * @dev: Pointer to core device
2094  *
2095  * mlx5_eswitch_get_total_vports returns total number of eswitch vports.
2096  */
mlx5_eswitch_get_total_vports(const struct mlx5_core_dev * dev)2097 u16 mlx5_eswitch_get_total_vports(const struct mlx5_core_dev *dev)
2098 {
2099 	struct mlx5_eswitch *esw;
2100 
2101 	esw = dev->priv.eswitch;
2102 	return mlx5_esw_allowed(esw) ? esw->total_vports : 0;
2103 }
2104 EXPORT_SYMBOL_GPL(mlx5_eswitch_get_total_vports);
2105 
2106 /**
2107  * mlx5_eswitch_get_core_dev - Get the mdev device
2108  * @esw : eswitch device.
2109  *
2110  * Return the mellanox core device which manages the eswitch.
2111  */
mlx5_eswitch_get_core_dev(struct mlx5_eswitch * esw)2112 struct mlx5_core_dev *mlx5_eswitch_get_core_dev(struct mlx5_eswitch *esw)
2113 {
2114 	return mlx5_esw_allowed(esw) ? esw->dev : NULL;
2115 }
2116 EXPORT_SYMBOL(mlx5_eswitch_get_core_dev);
2117