1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020, Mellanox Technologies inc.  All rights reserved. */
3 
4 #include "en/devlink.h"
5 #include "eswitch.h"
6 
7 static void
mlx5e_devlink_get_port_parent_id(struct mlx5_core_dev * dev,struct netdev_phys_item_id * ppid)8 mlx5e_devlink_get_port_parent_id(struct mlx5_core_dev *dev, struct netdev_phys_item_id *ppid)
9 {
10 	u64 parent_id;
11 
12 	parent_id = mlx5_query_nic_system_image_guid(dev);
13 	ppid->id_len = sizeof(parent_id);
14 	memcpy(ppid->id, &parent_id, sizeof(parent_id));
15 }
16 
mlx5e_devlink_port_register(struct mlx5e_priv * priv)17 int mlx5e_devlink_port_register(struct mlx5e_priv *priv)
18 {
19 	struct devlink *devlink = priv_to_devlink(priv->mdev);
20 	struct devlink_port_attrs attrs = {};
21 	struct netdev_phys_item_id ppid = {};
22 	struct devlink_port *dl_port;
23 	unsigned int dl_port_index;
24 	int ret;
25 
26 	if (mlx5_core_is_pf(priv->mdev)) {
27 		attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
28 		attrs.phys.port_number = mlx5_get_dev_index(priv->mdev);
29 		if (MLX5_ESWITCH_MANAGER(priv->mdev)) {
30 			mlx5e_devlink_get_port_parent_id(priv->mdev, &ppid);
31 			memcpy(attrs.switch_id.id, ppid.id, ppid.id_len);
32 			attrs.switch_id.id_len = ppid.id_len;
33 		}
34 		dl_port_index = mlx5_esw_vport_to_devlink_port_index(priv->mdev,
35 								     MLX5_VPORT_UPLINK);
36 	} else {
37 		attrs.flavour = DEVLINK_PORT_FLAVOUR_VIRTUAL;
38 		dl_port_index = mlx5_esw_vport_to_devlink_port_index(priv->mdev, 0);
39 	}
40 
41 	dl_port = mlx5e_devlink_get_dl_port(priv);
42 	memset(dl_port, 0, sizeof(*dl_port));
43 	devlink_port_attrs_set(dl_port, &attrs);
44 
45 	if (!(priv->mdev->priv.flags & MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW))
46 		devl_lock(devlink);
47 	ret = devl_port_register(devlink, dl_port, dl_port_index);
48 	if (!(priv->mdev->priv.flags & MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW))
49 		devl_unlock(devlink);
50 
51 	return ret;
52 }
53 
mlx5e_devlink_port_type_eth_set(struct mlx5e_priv * priv)54 void mlx5e_devlink_port_type_eth_set(struct mlx5e_priv *priv)
55 {
56 	struct devlink_port *dl_port = mlx5e_devlink_get_dl_port(priv);
57 
58 	devlink_port_type_eth_set(dl_port, priv->netdev);
59 }
60 
mlx5e_devlink_port_unregister(struct mlx5e_priv * priv)61 void mlx5e_devlink_port_unregister(struct mlx5e_priv *priv)
62 {
63 	struct devlink_port *dl_port = mlx5e_devlink_get_dl_port(priv);
64 	struct devlink *devlink = priv_to_devlink(priv->mdev);
65 
66 	if (!(priv->mdev->priv.flags & MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW))
67 		devl_lock(devlink);
68 	devl_port_unregister(dl_port);
69 	if (!(priv->mdev->priv.flags & MLX5_PRIV_FLAGS_MLX5E_LOCKED_FLOW))
70 		devl_unlock(devlink);
71 }
72 
mlx5e_get_devlink_port(struct net_device * dev)73 struct devlink_port *mlx5e_get_devlink_port(struct net_device *dev)
74 {
75 	struct mlx5e_priv *priv = netdev_priv(dev);
76 
77 	if (!netif_device_present(dev))
78 		return NULL;
79 
80 	return mlx5e_devlink_get_dl_port(priv);
81 }
82