1 // SPDX-License-Identifier: GPL-2.0-only
7 struct linkstate_req_info {
8 struct ethnl_req_info base;
11 struct linkstate_reply_data {
12 struct ethnl_reply_data base;
16 struct ethtool_link_ext_stats link_stats;
17 bool link_ext_state_provided;
18 struct ethtool_link_ext_state_info ethtool_link_ext_state_info;
21 #define LINKSTATE_REPDATA(__reply_base) \
22 container_of(__reply_base, struct linkstate_reply_data, base)
24 const struct nla_policy ethnl_linkstate_get_policy[] = {
25 [ETHTOOL_A_LINKSTATE_HEADER] =
26 NLA_POLICY_NESTED(ethnl_header_policy_stats),
29 static int linkstate_get_sqi(struct net_device *dev)
31 struct phy_device *phydev = dev->phydev;
37 mutex_lock(&phydev->lock);
38 if (!phydev->drv || !phydev->drv->get_sqi)
41 ret = phydev->drv->get_sqi(phydev);
42 mutex_unlock(&phydev->lock);
47 static int linkstate_get_sqi_max(struct net_device *dev)
49 struct phy_device *phydev = dev->phydev;
55 mutex_lock(&phydev->lock);
56 if (!phydev->drv || !phydev->drv->get_sqi_max)
59 ret = phydev->drv->get_sqi_max(phydev);
60 mutex_unlock(&phydev->lock);
65 static int linkstate_get_link_ext_state(struct net_device *dev,
66 struct linkstate_reply_data *data)
70 if (!dev->ethtool_ops->get_link_ext_state)
73 err = dev->ethtool_ops->get_link_ext_state(dev, &data->ethtool_link_ext_state_info);
77 data->link_ext_state_provided = true;
82 static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
83 struct ethnl_reply_data *reply_base,
84 const struct genl_info *info)
86 struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
87 struct net_device *dev = reply_base->dev;
90 ret = ethnl_ops_begin(dev);
93 data->link = __ethtool_get_link(dev);
95 ret = linkstate_get_sqi(dev);
96 if (ret < 0 && ret != -EOPNOTSUPP)
100 ret = linkstate_get_sqi_max(dev);
101 if (ret < 0 && ret != -EOPNOTSUPP)
105 if (dev->flags & IFF_UP) {
106 ret = linkstate_get_link_ext_state(dev, data);
107 if (ret < 0 && ret != -EOPNOTSUPP && ret != -ENODATA)
111 ethtool_stats_init((u64 *)&data->link_stats,
112 sizeof(data->link_stats) / 8);
114 if (req_base->flags & ETHTOOL_FLAG_STATS) {
116 data->link_stats.link_down_events =
117 READ_ONCE(dev->phydev->link_down_events);
119 if (dev->ethtool_ops->get_link_ext_stats)
120 dev->ethtool_ops->get_link_ext_stats(dev,
126 ethnl_ops_complete(dev);
130 static int linkstate_reply_size(const struct ethnl_req_info *req_base,
131 const struct ethnl_reply_data *reply_base)
133 struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
136 len = nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
139 if (data->sqi != -EOPNOTSUPP)
140 len += nla_total_size(sizeof(u32));
142 if (data->sqi_max != -EOPNOTSUPP)
143 len += nla_total_size(sizeof(u32));
145 if (data->link_ext_state_provided)
146 len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_STATE */
148 if (data->ethtool_link_ext_state_info.__link_ext_substate)
149 len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_SUBSTATE */
151 if (data->link_stats.link_down_events != ETHTOOL_STAT_NOT_SET)
152 len += nla_total_size(sizeof(u32));
157 static int linkstate_fill_reply(struct sk_buff *skb,
158 const struct ethnl_req_info *req_base,
159 const struct ethnl_reply_data *reply_base)
161 struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
163 if (data->link >= 0 &&
164 nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link))
167 if (data->sqi != -EOPNOTSUPP &&
168 nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI, data->sqi))
171 if (data->sqi_max != -EOPNOTSUPP &&
172 nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI_MAX, data->sqi_max))
175 if (data->link_ext_state_provided) {
176 if (nla_put_u8(skb, ETHTOOL_A_LINKSTATE_EXT_STATE,
177 data->ethtool_link_ext_state_info.link_ext_state))
180 if (data->ethtool_link_ext_state_info.__link_ext_substate &&
181 nla_put_u8(skb, ETHTOOL_A_LINKSTATE_EXT_SUBSTATE,
182 data->ethtool_link_ext_state_info.__link_ext_substate))
186 if (data->link_stats.link_down_events != ETHTOOL_STAT_NOT_SET)
187 if (nla_put_u32(skb, ETHTOOL_A_LINKSTATE_EXT_DOWN_CNT,
188 data->link_stats.link_down_events))
194 const struct ethnl_request_ops ethnl_linkstate_request_ops = {
195 .request_cmd = ETHTOOL_MSG_LINKSTATE_GET,
196 .reply_cmd = ETHTOOL_MSG_LINKSTATE_GET_REPLY,
197 .hdr_attr = ETHTOOL_A_LINKSTATE_HEADER,
198 .req_info_size = sizeof(struct linkstate_req_info),
199 .reply_data_size = sizeof(struct linkstate_reply_data),
201 .prepare_data = linkstate_prepare_data,
202 .reply_size = linkstate_reply_size,
203 .fill_reply = linkstate_fill_reply,