xref: /openbmc/linux/net/ethtool/linkstate.c (revision dc6a81c3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "netlink.h"
4 #include "common.h"
5 
6 struct linkstate_req_info {
7 	struct ethnl_req_info		base;
8 };
9 
10 struct linkstate_reply_data {
11 	struct ethnl_reply_data		base;
12 	int				link;
13 };
14 
15 #define LINKSTATE_REPDATA(__reply_base) \
16 	container_of(__reply_base, struct linkstate_reply_data, base)
17 
18 static const struct nla_policy
19 linkstate_get_policy[ETHTOOL_A_LINKSTATE_MAX + 1] = {
20 	[ETHTOOL_A_LINKSTATE_UNSPEC]		= { .type = NLA_REJECT },
21 	[ETHTOOL_A_LINKSTATE_HEADER]		= { .type = NLA_NESTED },
22 	[ETHTOOL_A_LINKSTATE_LINK]		= { .type = NLA_REJECT },
23 };
24 
25 static int linkstate_prepare_data(const struct ethnl_req_info *req_base,
26 				  struct ethnl_reply_data *reply_base,
27 				  struct genl_info *info)
28 {
29 	struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
30 	struct net_device *dev = reply_base->dev;
31 	int ret;
32 
33 	ret = ethnl_ops_begin(dev);
34 	if (ret < 0)
35 		return ret;
36 	data->link = __ethtool_get_link(dev);
37 	ethnl_ops_complete(dev);
38 
39 	return 0;
40 }
41 
42 static int linkstate_reply_size(const struct ethnl_req_info *req_base,
43 				const struct ethnl_reply_data *reply_base)
44 {
45 	return nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */
46 		+ 0;
47 }
48 
49 static int linkstate_fill_reply(struct sk_buff *skb,
50 				const struct ethnl_req_info *req_base,
51 				const struct ethnl_reply_data *reply_base)
52 {
53 	struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base);
54 
55 	if (data->link >= 0 &&
56 	    nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link))
57 		return -EMSGSIZE;
58 
59 	return 0;
60 }
61 
62 const struct ethnl_request_ops ethnl_linkstate_request_ops = {
63 	.request_cmd		= ETHTOOL_MSG_LINKSTATE_GET,
64 	.reply_cmd		= ETHTOOL_MSG_LINKSTATE_GET_REPLY,
65 	.hdr_attr		= ETHTOOL_A_LINKSTATE_HEADER,
66 	.max_attr		= ETHTOOL_A_LINKSTATE_MAX,
67 	.req_info_size		= sizeof(struct linkstate_req_info),
68 	.reply_data_size	= sizeof(struct linkstate_reply_data),
69 	.request_policy		= linkstate_get_policy,
70 
71 	.prepare_data		= linkstate_prepare_data,
72 	.reply_size		= linkstate_reply_size,
73 	.fill_reply		= linkstate_fill_reply,
74 };
75