xref: /linux/net/ethtool/pause.c (revision 24ce659dcc02c21f8d6c0a7589c3320a4dfa8152)
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 
83 /* PAUSE_SET */
84 
85 static const struct nla_policy
86 pause_set_policy[ETHTOOL_A_PAUSE_MAX + 1] = {
87 	[ETHTOOL_A_PAUSE_UNSPEC]		= { .type = NLA_REJECT },
88 	[ETHTOOL_A_PAUSE_HEADER]		= { .type = NLA_NESTED },
89 	[ETHTOOL_A_PAUSE_AUTONEG]		= { .type = NLA_U8 },
90 	[ETHTOOL_A_PAUSE_RX]			= { .type = NLA_U8 },
91 	[ETHTOOL_A_PAUSE_TX]			= { .type = NLA_U8 },
92 };
93 
94 int ethnl_set_pause(struct sk_buff *skb, struct genl_info *info)
95 {
96 	struct nlattr *tb[ETHTOOL_A_PAUSE_MAX + 1];
97 	struct ethtool_pauseparam params = {};
98 	struct ethnl_req_info req_info = {};
99 	const struct ethtool_ops *ops;
100 	struct net_device *dev;
101 	bool mod = false;
102 	int ret;
103 
104 	ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb, ETHTOOL_A_PAUSE_MAX,
105 			  pause_set_policy, info->extack);
106 	if (ret < 0)
107 		return ret;
108 	ret = ethnl_parse_header_dev_get(&req_info,
109 					 tb[ETHTOOL_A_PAUSE_HEADER],
110 					 genl_info_net(info), info->extack,
111 					 true);
112 	if (ret < 0)
113 		return ret;
114 	dev = req_info.dev;
115 	ops = dev->ethtool_ops;
116 	ret = -EOPNOTSUPP;
117 	if (!ops->get_pauseparam || !ops->set_pauseparam)
118 		goto out_dev;
119 
120 	rtnl_lock();
121 	ret = ethnl_ops_begin(dev);
122 	if (ret < 0)
123 		goto out_rtnl;
124 	ops->get_pauseparam(dev, &params);
125 
126 	ethnl_update_bool32(&params.autoneg, tb[ETHTOOL_A_PAUSE_AUTONEG], &mod);
127 	ethnl_update_bool32(&params.rx_pause, tb[ETHTOOL_A_PAUSE_RX], &mod);
128 	ethnl_update_bool32(&params.tx_pause, tb[ETHTOOL_A_PAUSE_TX], &mod);
129 	ret = 0;
130 	if (!mod)
131 		goto out_ops;
132 
133 	ret = dev->ethtool_ops->set_pauseparam(dev, &params);
134 	if (ret < 0)
135 		goto out_ops;
136 	ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
137 
138 out_ops:
139 	ethnl_ops_complete(dev);
140 out_rtnl:
141 	rtnl_unlock();
142 out_dev:
143 	dev_put(dev);
144 	return ret;
145 }
146