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 const struct devlink_ops mlx5e_devlink_ops = {
8 };
9 
mlx5e_create_devlink(struct device * dev,struct mlx5_core_dev * mdev)10 struct mlx5e_dev *mlx5e_create_devlink(struct device *dev,
11 				       struct mlx5_core_dev *mdev)
12 {
13 	struct mlx5e_dev *mlx5e_dev;
14 	struct devlink *devlink;
15 
16 	devlink = devlink_alloc_ns(&mlx5e_devlink_ops, sizeof(*mlx5e_dev),
17 				   devlink_net(priv_to_devlink(mdev)), dev);
18 	if (!devlink)
19 		return ERR_PTR(-ENOMEM);
20 	devlink_register(devlink);
21 	return devlink_priv(devlink);
22 }
23 
mlx5e_destroy_devlink(struct mlx5e_dev * mlx5e_dev)24 void mlx5e_destroy_devlink(struct mlx5e_dev *mlx5e_dev)
25 {
26 	struct devlink *devlink = priv_to_devlink(mlx5e_dev);
27 
28 	devlink_unregister(devlink);
29 	devlink_free(devlink);
30 }
31 
32 static void
mlx5e_devlink_get_port_parent_id(struct mlx5_core_dev * dev,struct netdev_phys_item_id * ppid)33 mlx5e_devlink_get_port_parent_id(struct mlx5_core_dev *dev, struct netdev_phys_item_id *ppid)
34 {
35 	u64 parent_id;
36 
37 	parent_id = mlx5_query_nic_system_image_guid(dev);
38 	ppid->id_len = sizeof(parent_id);
39 	memcpy(ppid->id, &parent_id, sizeof(parent_id));
40 }
41 
mlx5e_devlink_port_register(struct mlx5e_dev * mlx5e_dev,struct mlx5_core_dev * mdev)42 int mlx5e_devlink_port_register(struct mlx5e_dev *mlx5e_dev,
43 				struct mlx5_core_dev *mdev)
44 {
45 	struct devlink *devlink = priv_to_devlink(mlx5e_dev);
46 	struct devlink_port_attrs attrs = {};
47 	struct netdev_phys_item_id ppid = {};
48 	unsigned int dl_port_index;
49 
50 	if (mlx5_core_is_pf(mdev)) {
51 		attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
52 		attrs.phys.port_number = mlx5_get_dev_index(mdev);
53 		if (MLX5_ESWITCH_MANAGER(mdev)) {
54 			mlx5e_devlink_get_port_parent_id(mdev, &ppid);
55 			memcpy(attrs.switch_id.id, ppid.id, ppid.id_len);
56 			attrs.switch_id.id_len = ppid.id_len;
57 		}
58 		dl_port_index = mlx5_esw_vport_to_devlink_port_index(mdev,
59 								     MLX5_VPORT_UPLINK);
60 	} else {
61 		attrs.flavour = DEVLINK_PORT_FLAVOUR_VIRTUAL;
62 		dl_port_index = mlx5_esw_vport_to_devlink_port_index(mdev, 0);
63 	}
64 
65 	devlink_port_attrs_set(&mlx5e_dev->dl_port, &attrs);
66 
67 	return devlink_port_register(devlink, &mlx5e_dev->dl_port,
68 				     dl_port_index);
69 }
70 
mlx5e_devlink_port_unregister(struct mlx5e_dev * mlx5e_dev)71 void mlx5e_devlink_port_unregister(struct mlx5e_dev *mlx5e_dev)
72 {
73 	devlink_port_unregister(&mlx5e_dev->dl_port);
74 }
75