1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2019 Pensando Systems, Inc */
3 
4 #include <linux/module.h>
5 #include <linux/netdevice.h>
6 
7 #include "ionic.h"
8 #include "ionic_bus.h"
9 #include "ionic_lif.h"
10 #include "ionic_devlink.h"
11 
12 static int ionic_dl_info_get(struct devlink *dl, struct devlink_info_req *req,
13 			     struct netlink_ext_ack *extack)
14 {
15 	struct ionic *ionic = devlink_priv(dl);
16 	struct ionic_dev *idev = &ionic->idev;
17 	char buf[16];
18 	int err = 0;
19 
20 	err = devlink_info_driver_name_put(req, IONIC_DRV_NAME);
21 	if (err)
22 		return err;
23 
24 	err = devlink_info_version_running_put(req,
25 					       DEVLINK_INFO_VERSION_GENERIC_FW,
26 					       idev->dev_info.fw_version);
27 	if (err)
28 		return err;
29 
30 	snprintf(buf, sizeof(buf), "0x%x", idev->dev_info.asic_type);
31 	err = devlink_info_version_fixed_put(req,
32 					     DEVLINK_INFO_VERSION_GENERIC_ASIC_ID,
33 					     buf);
34 	if (err)
35 		return err;
36 
37 	snprintf(buf, sizeof(buf), "0x%x", idev->dev_info.asic_rev);
38 	err = devlink_info_version_fixed_put(req,
39 					     DEVLINK_INFO_VERSION_GENERIC_ASIC_REV,
40 					     buf);
41 	if (err)
42 		return err;
43 
44 	err = devlink_info_serial_number_put(req, idev->dev_info.serial_num);
45 
46 	return err;
47 }
48 
49 static const struct devlink_ops ionic_dl_ops = {
50 	.info_get	= ionic_dl_info_get,
51 };
52 
53 struct ionic *ionic_devlink_alloc(struct device *dev)
54 {
55 	struct devlink *dl;
56 
57 	dl = devlink_alloc(&ionic_dl_ops, sizeof(struct ionic));
58 
59 	return devlink_priv(dl);
60 }
61 
62 void ionic_devlink_free(struct ionic *ionic)
63 {
64 	struct devlink *dl = priv_to_devlink(ionic);
65 
66 	devlink_free(dl);
67 }
68 
69 int ionic_devlink_register(struct ionic *ionic)
70 {
71 	struct devlink *dl = priv_to_devlink(ionic);
72 	struct devlink_port_attrs attrs = {};
73 	int err;
74 
75 	err = devlink_register(dl, ionic->dev);
76 	if (err) {
77 		dev_warn(ionic->dev, "devlink_register failed: %d\n", err);
78 		return err;
79 	}
80 
81 	attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
82 	devlink_port_attrs_set(&ionic->dl_port, &attrs);
83 	err = devlink_port_register(dl, &ionic->dl_port, 0);
84 	if (err)
85 		dev_err(ionic->dev, "devlink_port_register failed: %d\n", err);
86 	else
87 		devlink_port_type_eth_set(&ionic->dl_port,
88 					  ionic->master_lif->netdev);
89 
90 	return err;
91 }
92 
93 void ionic_devlink_unregister(struct ionic *ionic)
94 {
95 	struct devlink *dl = priv_to_devlink(ionic);
96 
97 	if (ionic->dl_port.registered)
98 		devlink_port_unregister(&ionic->dl_port);
99 	devlink_unregister(dl);
100 }
101