blob: fb676f349455aed52ebc5cc5d8c5d42bf9c2b9f1 [file] [log] [blame]
Michal Kubecek3d2b8472019-12-27 15:56:23 +01001// SPDX-License-Identifier: GPL-2.0-only
2
3#include "netlink.h"
4#include "common.h"
Oleksij Rempel80660212020-05-20 08:29:14 +02005#include <linux/phy.h>
Michal Kubecek3d2b8472019-12-27 15:56:23 +01006
7struct linkstate_req_info {
8 struct ethnl_req_info base;
9};
10
11struct linkstate_reply_data {
Amit Cohenecc31c62020-06-29 23:46:16 +030012 struct ethnl_reply_data base;
13 int link;
14 int sqi;
15 int sqi_max;
16 bool link_ext_state_provided;
17 struct ethtool_link_ext_state_info ethtool_link_ext_state_info;
Michal Kubecek3d2b8472019-12-27 15:56:23 +010018};
19
20#define LINKSTATE_REPDATA(__reply_base) \
21 container_of(__reply_base, struct linkstate_reply_data, base)
22
Jakub Kicinskiff419af2020-10-05 15:07:35 -070023const struct nla_policy ethnl_linkstate_get_policy[] = {
Jakub Kicinski329d9c32020-10-05 15:07:36 -070024 [ETHTOOL_A_LINKSTATE_HEADER] =
25 NLA_POLICY_NESTED(ethnl_header_policy),
Michal Kubecek3d2b8472019-12-27 15:56:23 +010026};
27
Oleksij Rempel80660212020-05-20 08:29:14 +020028static int linkstate_get_sqi(struct net_device *dev)
29{
30 struct phy_device *phydev = dev->phydev;
31 int ret;
32
33 if (!phydev)
34 return -EOPNOTSUPP;
35
36 mutex_lock(&phydev->lock);
37 if (!phydev->drv || !phydev->drv->get_sqi)
38 ret = -EOPNOTSUPP;
39 else
40 ret = phydev->drv->get_sqi(phydev);
41 mutex_unlock(&phydev->lock);
42
43 return ret;
44}
45
46static int linkstate_get_sqi_max(struct net_device *dev)
47{
48 struct phy_device *phydev = dev->phydev;
49 int ret;
50
51 if (!phydev)
52 return -EOPNOTSUPP;
53
54 mutex_lock(&phydev->lock);
55 if (!phydev->drv || !phydev->drv->get_sqi_max)
56 ret = -EOPNOTSUPP;
57 else
58 ret = phydev->drv->get_sqi_max(phydev);
59 mutex_unlock(&phydev->lock);
60
61 return ret;
Amit Cohenecc31c62020-06-29 23:46:16 +030062};
63
64static int linkstate_get_link_ext_state(struct net_device *dev,
65 struct linkstate_reply_data *data)
66{
67 int err;
68
69 if (!dev->ethtool_ops->get_link_ext_state)
70 return -EOPNOTSUPP;
71
72 err = dev->ethtool_ops->get_link_ext_state(dev, &data->ethtool_link_ext_state_info);
73 if (err)
74 return err;
75
76 data->link_ext_state_provided = true;
77
78 return 0;
Oleksij Rempel80660212020-05-20 08:29:14 +020079}
80
Michal Kubecek3d2b8472019-12-27 15:56:23 +010081static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
82 struct ethnl_reply_data *reply_base,
83 struct genl_info *info)
84{
85 struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
86 struct net_device *dev = reply_base->dev;
87 int ret;
88
89 ret = ethnl_ops_begin(dev);
90 if (ret < 0)
91 return ret;
92 data->link = __ethtool_get_link(dev);
Oleksij Rempel80660212020-05-20 08:29:14 +020093
94 ret = linkstate_get_sqi(dev);
95 if (ret < 0 && ret != -EOPNOTSUPP)
Michal Kubecek1ae71d92020-06-25 00:09:08 +020096 goto out;
Oleksij Rempel80660212020-05-20 08:29:14 +020097 data->sqi = ret;
98
99 ret = linkstate_get_sqi_max(dev);
100 if (ret < 0 && ret != -EOPNOTSUPP)
Michal Kubecek1ae71d92020-06-25 00:09:08 +0200101 goto out;
Oleksij Rempel80660212020-05-20 08:29:14 +0200102 data->sqi_max = ret;
103
Amit Cohenecc31c62020-06-29 23:46:16 +0300104 if (dev->flags & IFF_UP) {
105 ret = linkstate_get_link_ext_state(dev, data);
106 if (ret < 0 && ret != -EOPNOTSUPP && ret != -ENODATA)
107 goto out;
108 }
109
Michal Kubecek1ae71d92020-06-25 00:09:08 +0200110 ret = 0;
111out:
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100112 ethnl_ops_complete(dev);
Michal Kubecek1ae71d92020-06-25 00:09:08 +0200113 return ret;
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100114}
115
116static int linkstate_reply_size(const struct ethnl_req_info *req_base,
117 const struct ethnl_reply_data *reply_base)
118{
Oleksij Rempel80660212020-05-20 08:29:14 +0200119 struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
120 int len;
121
122 len = nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100123 + 0;
Oleksij Rempel80660212020-05-20 08:29:14 +0200124
125 if (data->sqi != -EOPNOTSUPP)
126 len += nla_total_size(sizeof(u32));
127
128 if (data->sqi_max != -EOPNOTSUPP)
129 len += nla_total_size(sizeof(u32));
130
Amit Cohenecc31c62020-06-29 23:46:16 +0300131 if (data->link_ext_state_provided)
132 len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_STATE */
133
134 if (data->ethtool_link_ext_state_info.__link_ext_substate)
135 len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_SUBSTATE */
136
Oleksij Rempel80660212020-05-20 08:29:14 +0200137 return len;
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100138}
139
140static int linkstate_fill_reply(struct sk_buff *skb,
141 const struct ethnl_req_info *req_base,
142 const struct ethnl_reply_data *reply_base)
143{
144 struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
145
146 if (data->link >= 0 &&
147 nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link))
148 return -EMSGSIZE;
149
Oleksij Rempel80660212020-05-20 08:29:14 +0200150 if (data->sqi != -EOPNOTSUPP &&
151 nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI, data->sqi))
152 return -EMSGSIZE;
153
154 if (data->sqi_max != -EOPNOTSUPP &&
155 nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI_MAX, data->sqi_max))
156 return -EMSGSIZE;
157
Amit Cohenecc31c62020-06-29 23:46:16 +0300158 if (data->link_ext_state_provided) {
159 if (nla_put_u8(skb, ETHTOOL_A_LINKSTATE_EXT_STATE,
160 data->ethtool_link_ext_state_info.link_ext_state))
161 return -EMSGSIZE;
162
163 if (data->ethtool_link_ext_state_info.__link_ext_substate &&
164 nla_put_u8(skb, ETHTOOL_A_LINKSTATE_EXT_SUBSTATE,
165 data->ethtool_link_ext_state_info.__link_ext_substate))
166 return -EMSGSIZE;
167 }
168
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100169 return 0;
170}
171
172const struct ethnl_request_ops ethnl_linkstate_request_ops = {
173 .request_cmd = ETHTOOL_MSG_LINKSTATE_GET,
174 .reply_cmd = ETHTOOL_MSG_LINKSTATE_GET_REPLY,
175 .hdr_attr = ETHTOOL_A_LINKSTATE_HEADER,
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100176 .req_info_size = sizeof(struct linkstate_req_info),
177 .reply_data_size = sizeof(struct linkstate_reply_data),
Michal Kubecek3d2b8472019-12-27 15:56:23 +0100178
179 .prepare_data = linkstate_prepare_data,
180 .reply_size = linkstate_reply_size,
181 .fill_reply = linkstate_fill_reply,
182};