xref: /openbmc/linux/net/ethtool/pause.c (revision 7f59fb32)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "netlink.h"
4 #include "common.h"
5 
6 struct pause_req_info {
7 	struct ethnl_req_info		base;
8 };
9 
10 struct pause_reply_data {
11 	struct ethnl_reply_data		base;
12 	struct ethtool_pauseparam	pauseparam;
13 };
14 
15 #define PAUSE_REPDATA(__reply_base) \
16 	container_of(__reply_base, struct pause_reply_data, base)
17 
18 static const struct nla_policy
19 pause_get_policy[ETHTOOL_A_PAUSE_MAX + 1] = {
20 	[ETHTOOL_A_PAUSE_UNSPEC]		= { .type = NLA_REJECT },
21 	[ETHTOOL_A_PAUSE_HEADER]		= { .type = NLA_NESTED },
22 	[ETHTOOL_A_PAUSE_AUTONEG]		= { .type = NLA_REJECT },
23 	[ETHTOOL_A_PAUSE_RX]			= { .type = NLA_REJECT },
24 	[ETHTOOL_A_PAUSE_TX]			= { .type = NLA_REJECT },
25 };
26 
27 static int pause_prepare_data(const struct ethnl_req_info *req_base,
28 			      struct ethnl_reply_data *reply_base,
29 			      struct genl_info *info)
30 {
31 	struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
32 	struct net_device *dev = reply_base->dev;
33 	int ret;
34 
35 	if (!dev->ethtool_ops->get_pauseparam)
36 		return -EOPNOTSUPP;
37 	ret = ethnl_ops_begin(dev);
38 	if (ret < 0)
39 		return ret;
40 	dev->ethtool_ops->get_pauseparam(dev, &data->pauseparam);
41 	ethnl_ops_complete(dev);
42 
43 	return 0;
44 }
45 
46 static int pause_reply_size(const struct ethnl_req_info *req_base,
47 			    const struct ethnl_reply_data *reply_base)
48 {
49 	return nla_total_size(sizeof(u8)) +	/* _PAUSE_AUTONEG */
50 		nla_total_size(sizeof(u8)) +	/* _PAUSE_RX */
51 		nla_total_size(sizeof(u8));	/* _PAUSE_TX */
52 }
53 
54 static int pause_fill_reply(struct sk_buff *skb,
55 			    const struct ethnl_req_info *req_base,
56 			    const struct ethnl_reply_data *reply_base)
57 {
58 	const struct pause_reply_data *data = PAUSE_REPDATA(reply_base);
59 	const struct ethtool_pauseparam *pauseparam = &data->pauseparam;
60 
61 	if (nla_put_u8(skb, ETHTOOL_A_PAUSE_AUTONEG, !!pauseparam->autoneg) ||
62 	    nla_put_u8(skb, ETHTOOL_A_PAUSE_RX, !!pauseparam->rx_pause) ||
63 	    nla_put_u8(skb, ETHTOOL_A_PAUSE_TX, !!pauseparam->tx_pause))
64 		return -EMSGSIZE;
65 
66 	return 0;
67 }
68 
69 const struct ethnl_request_ops ethnl_pause_request_ops = {
70 	.request_cmd		= ETHTOOL_MSG_PAUSE_GET,
71 	.reply_cmd		= ETHTOOL_MSG_PAUSE_GET_REPLY,
72 	.hdr_attr		= ETHTOOL_A_PAUSE_HEADER,
73 	.max_attr		= ETHTOOL_A_PAUSE_MAX,
74 	.req_info_size		= sizeof(struct pause_req_info),
75 	.reply_data_size	= sizeof(struct pause_reply_data),
76 	.request_policy		= pause_get_policy,
77 
78 	.prepare_data		= pause_prepare_data,
79 	.reply_size		= pause_reply_size,
80 	.fill_reply		= pause_fill_reply,
81 };
82