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