Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2
  3#include "netlink.h"
  4#include "common.h"
  5#include <linux/phy.h>
  6
  7struct linkstate_req_info {
  8	struct ethnl_req_info		base;
  9};
 10
 11struct linkstate_reply_data {
 12	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;
 18};
 19
 20#define LINKSTATE_REPDATA(__reply_base) \
 21	container_of(__reply_base, struct linkstate_reply_data, base)
 22
 23const struct nla_policy ethnl_linkstate_get_policy[] = {
 24	[ETHTOOL_A_LINKSTATE_HEADER]		=
 25		NLA_POLICY_NESTED(ethnl_header_policy),
 26};
 27
 28static 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;
 62};
 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;
 79}
 80
 81static 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);
 93
 94	ret = linkstate_get_sqi(dev);
 95	if (ret < 0 && ret != -EOPNOTSUPP)
 96		goto out;
 97	data->sqi = ret;
 98
 99	ret = linkstate_get_sqi_max(dev);
100	if (ret < 0 && ret != -EOPNOTSUPP)
101		goto out;
102	data->sqi_max = ret;
103
104	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
 
 
 
 
 
 
 
 
 
 
 
 
 
110	ret = 0;
111out:
112	ethnl_ops_complete(dev);
113	return ret;
114}
115
116static int linkstate_reply_size(const struct ethnl_req_info *req_base,
117				const struct ethnl_reply_data *reply_base)
118{
119	struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
120	int len;
121
122	len = nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
123		+ 0;
124
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
131	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
 
 
 
137	return len;
138}
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
150	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
158	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
169	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,
176	.req_info_size		= sizeof(struct linkstate_req_info),
177	.reply_data_size	= sizeof(struct linkstate_reply_data),
178
179	.prepare_data		= linkstate_prepare_data,
180	.reply_size		= linkstate_reply_size,
181	.fill_reply		= linkstate_fill_reply,
182};
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2
  3#include "netlink.h"
  4#include "common.h"
  5#include <linux/phy.h>
  6
  7struct linkstate_req_info {
  8	struct ethnl_req_info		base;
  9};
 10
 11struct linkstate_reply_data {
 12	struct ethnl_reply_data			base;
 13	int					link;
 14	int					sqi;
 15	int					sqi_max;
 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;
 19};
 20
 21#define LINKSTATE_REPDATA(__reply_base) \
 22	container_of(__reply_base, struct linkstate_reply_data, base)
 23
 24const struct nla_policy ethnl_linkstate_get_policy[] = {
 25	[ETHTOOL_A_LINKSTATE_HEADER]		=
 26		NLA_POLICY_NESTED(ethnl_header_policy_stats),
 27};
 28
 29static int linkstate_get_sqi(struct net_device *dev)
 30{
 31	struct phy_device *phydev = dev->phydev;
 32	int ret;
 33
 34	if (!phydev)
 35		return -EOPNOTSUPP;
 36
 37	mutex_lock(&phydev->lock);
 38	if (!phydev->drv || !phydev->drv->get_sqi)
 39		ret = -EOPNOTSUPP;
 40	else
 41		ret = phydev->drv->get_sqi(phydev);
 42	mutex_unlock(&phydev->lock);
 43
 44	return ret;
 45}
 46
 47static int linkstate_get_sqi_max(struct net_device *dev)
 48{
 49	struct phy_device *phydev = dev->phydev;
 50	int ret;
 51
 52	if (!phydev)
 53		return -EOPNOTSUPP;
 54
 55	mutex_lock(&phydev->lock);
 56	if (!phydev->drv || !phydev->drv->get_sqi_max)
 57		ret = -EOPNOTSUPP;
 58	else
 59		ret = phydev->drv->get_sqi_max(phydev);
 60	mutex_unlock(&phydev->lock);
 61
 62	return ret;
 63};
 64
 65static int linkstate_get_link_ext_state(struct net_device *dev,
 66					struct linkstate_reply_data *data)
 67{
 68	int err;
 69
 70	if (!dev->ethtool_ops->get_link_ext_state)
 71		return -EOPNOTSUPP;
 72
 73	err = dev->ethtool_ops->get_link_ext_state(dev, &data->ethtool_link_ext_state_info);
 74	if (err)
 75		return err;
 76
 77	data->link_ext_state_provided = true;
 78
 79	return 0;
 80}
 81
 82static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
 83				  struct ethnl_reply_data *reply_base,
 84				  struct genl_info *info)
 85{
 86	struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
 87	struct net_device *dev = reply_base->dev;
 88	int ret;
 89
 90	ret = ethnl_ops_begin(dev);
 91	if (ret < 0)
 92		return ret;
 93	data->link = __ethtool_get_link(dev);
 94
 95	ret = linkstate_get_sqi(dev);
 96	if (ret < 0 && ret != -EOPNOTSUPP)
 97		goto out;
 98	data->sqi = ret;
 99
100	ret = linkstate_get_sqi_max(dev);
101	if (ret < 0 && ret != -EOPNOTSUPP)
102		goto out;
103	data->sqi_max = ret;
104
105	if (dev->flags & IFF_UP) {
106		ret = linkstate_get_link_ext_state(dev, data);
107		if (ret < 0 && ret != -EOPNOTSUPP && ret != -ENODATA)
108			goto out;
109	}
110
111	ethtool_stats_init((u64 *)&data->link_stats,
112			   sizeof(data->link_stats) / 8);
113
114	if (req_base->flags & ETHTOOL_FLAG_STATS) {
115		if (dev->phydev)
116			data->link_stats.link_down_events =
117				READ_ONCE(dev->phydev->link_down_events);
118
119		if (dev->ethtool_ops->get_link_ext_stats)
120			dev->ethtool_ops->get_link_ext_stats(dev,
121							     &data->link_stats);
122	}
123
124	ret = 0;
125out:
126	ethnl_ops_complete(dev);
127	return ret;
128}
129
130static int linkstate_reply_size(const struct ethnl_req_info *req_base,
131				const struct ethnl_reply_data *reply_base)
132{
133	struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
134	int len;
135
136	len = nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
137		+ 0;
138
139	if (data->sqi != -EOPNOTSUPP)
140		len += nla_total_size(sizeof(u32));
141
142	if (data->sqi_max != -EOPNOTSUPP)
143		len += nla_total_size(sizeof(u32));
144
145	if (data->link_ext_state_provided)
146		len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_STATE */
147
148	if (data->ethtool_link_ext_state_info.__link_ext_substate)
149		len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_SUBSTATE */
150
151	if (data->link_stats.link_down_events != ETHTOOL_STAT_NOT_SET)
152		len += nla_total_size(sizeof(u32));
153
154	return len;
155}
156
157static int linkstate_fill_reply(struct sk_buff *skb,
158				const struct ethnl_req_info *req_base,
159				const struct ethnl_reply_data *reply_base)
160{
161	struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
162
163	if (data->link >= 0 &&
164	    nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link))
165		return -EMSGSIZE;
166
167	if (data->sqi != -EOPNOTSUPP &&
168	    nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI, data->sqi))
169		return -EMSGSIZE;
170
171	if (data->sqi_max != -EOPNOTSUPP &&
172	    nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI_MAX, data->sqi_max))
173		return -EMSGSIZE;
174
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))
178			return -EMSGSIZE;
179
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))
183			return -EMSGSIZE;
184	}
185
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))
189			return -EMSGSIZE;
190
191	return 0;
192}
193
194const 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),
200
201	.prepare_data		= linkstate_prepare_data,
202	.reply_size		= linkstate_reply_size,
203	.fill_reply		= linkstate_fill_reply,
204};