xref: /openbmc/linux/net/ethtool/rings.c (revision 5ec82c49a21a3e50b0011d8c85c119e16c6413a0)
1e4a1717bSMichal Kubecek // SPDX-License-Identifier: GPL-2.0-only
2e4a1717bSMichal Kubecek 
3e4a1717bSMichal Kubecek #include "netlink.h"
4e4a1717bSMichal Kubecek #include "common.h"
5e4a1717bSMichal Kubecek 
6e4a1717bSMichal Kubecek struct rings_req_info {
7e4a1717bSMichal Kubecek 	struct ethnl_req_info		base;
8e4a1717bSMichal Kubecek };
9e4a1717bSMichal Kubecek 
10e4a1717bSMichal Kubecek struct rings_reply_data {
11e4a1717bSMichal Kubecek 	struct ethnl_reply_data		base;
12e4a1717bSMichal Kubecek 	struct ethtool_ringparam	ringparam;
13e4a1717bSMichal Kubecek };
14e4a1717bSMichal Kubecek 
15e4a1717bSMichal Kubecek #define RINGS_REPDATA(__reply_base) \
16e4a1717bSMichal Kubecek 	container_of(__reply_base, struct rings_reply_data, base)
17e4a1717bSMichal Kubecek 
18e4a1717bSMichal Kubecek static const struct nla_policy
19e4a1717bSMichal Kubecek rings_get_policy[ETHTOOL_A_RINGS_MAX + 1] = {
20e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_UNSPEC]		= { .type = NLA_REJECT },
21e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_HEADER]		= { .type = NLA_NESTED },
22e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_MAX]		= { .type = NLA_REJECT },
23e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_MINI_MAX]		= { .type = NLA_REJECT },
24e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_JUMBO_MAX]		= { .type = NLA_REJECT },
25e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_TX_MAX]		= { .type = NLA_REJECT },
26e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_RX]			= { .type = NLA_REJECT },
27e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_MINI]		= { .type = NLA_REJECT },
28e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_JUMBO]		= { .type = NLA_REJECT },
29e4a1717bSMichal Kubecek 	[ETHTOOL_A_RINGS_TX]			= { .type = NLA_REJECT },
30e4a1717bSMichal Kubecek };
31e4a1717bSMichal Kubecek 
32e4a1717bSMichal Kubecek static int rings_prepare_data(const struct ethnl_req_info *req_base,
33e4a1717bSMichal Kubecek 			      struct ethnl_reply_data *reply_base,
34e4a1717bSMichal Kubecek 			      struct genl_info *info)
35e4a1717bSMichal Kubecek {
36e4a1717bSMichal Kubecek 	struct rings_reply_data *data = RINGS_REPDATA(reply_base);
37e4a1717bSMichal Kubecek 	struct net_device *dev = reply_base->dev;
38e4a1717bSMichal Kubecek 	int ret;
39e4a1717bSMichal Kubecek 
40e4a1717bSMichal Kubecek 	if (!dev->ethtool_ops->get_ringparam)
41e4a1717bSMichal Kubecek 		return -EOPNOTSUPP;
42e4a1717bSMichal Kubecek 	ret = ethnl_ops_begin(dev);
43e4a1717bSMichal Kubecek 	if (ret < 0)
44e4a1717bSMichal Kubecek 		return ret;
45e4a1717bSMichal Kubecek 	dev->ethtool_ops->get_ringparam(dev, &data->ringparam);
46e4a1717bSMichal Kubecek 	ethnl_ops_complete(dev);
47e4a1717bSMichal Kubecek 
48e4a1717bSMichal Kubecek 	return 0;
49e4a1717bSMichal Kubecek }
50e4a1717bSMichal Kubecek 
51e4a1717bSMichal Kubecek static int rings_reply_size(const struct ethnl_req_info *req_base,
52e4a1717bSMichal Kubecek 			    const struct ethnl_reply_data *reply_base)
53e4a1717bSMichal Kubecek {
54e4a1717bSMichal Kubecek 	return nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MAX */
55e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI_MAX */
56e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO_MAX */
57e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX_MAX */
58e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX */
59e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI */
60e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO */
61e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32));	/* _RINGS_TX */
62e4a1717bSMichal Kubecek }
63e4a1717bSMichal Kubecek 
64e4a1717bSMichal Kubecek static int rings_fill_reply(struct sk_buff *skb,
65e4a1717bSMichal Kubecek 			    const struct ethnl_req_info *req_base,
66e4a1717bSMichal Kubecek 			    const struct ethnl_reply_data *reply_base)
67e4a1717bSMichal Kubecek {
68e4a1717bSMichal Kubecek 	const struct rings_reply_data *data = RINGS_REPDATA(reply_base);
69e4a1717bSMichal Kubecek 	const struct ethtool_ringparam *ringparam = &data->ringparam;
70e4a1717bSMichal Kubecek 
71e4a1717bSMichal Kubecek 	if ((ringparam->rx_max_pending &&
72e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MAX,
73e4a1717bSMichal Kubecek 			  ringparam->rx_max_pending) ||
74e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX,
75e4a1717bSMichal Kubecek 			  ringparam->rx_pending))) ||
76e4a1717bSMichal Kubecek 	    (ringparam->rx_mini_max_pending &&
77e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI_MAX,
78e4a1717bSMichal Kubecek 			  ringparam->rx_mini_max_pending) ||
79e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI,
80e4a1717bSMichal Kubecek 			  ringparam->rx_mini_pending))) ||
81e4a1717bSMichal Kubecek 	    (ringparam->rx_jumbo_max_pending &&
82e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO_MAX,
83e4a1717bSMichal Kubecek 			  ringparam->rx_jumbo_max_pending) ||
84e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO,
85e4a1717bSMichal Kubecek 			  ringparam->rx_jumbo_pending))) ||
86e4a1717bSMichal Kubecek 	    (ringparam->tx_max_pending &&
87e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_MAX,
88e4a1717bSMichal Kubecek 			  ringparam->tx_max_pending) ||
89e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_TX,
90e4a1717bSMichal Kubecek 			  ringparam->tx_pending))))
91e4a1717bSMichal Kubecek 		return -EMSGSIZE;
92e4a1717bSMichal Kubecek 
93e4a1717bSMichal Kubecek 	return 0;
94e4a1717bSMichal Kubecek }
95e4a1717bSMichal Kubecek 
96e4a1717bSMichal Kubecek const struct ethnl_request_ops ethnl_rings_request_ops = {
97e4a1717bSMichal Kubecek 	.request_cmd		= ETHTOOL_MSG_RINGS_GET,
98e4a1717bSMichal Kubecek 	.reply_cmd		= ETHTOOL_MSG_RINGS_GET_REPLY,
99e4a1717bSMichal Kubecek 	.hdr_attr		= ETHTOOL_A_RINGS_HEADER,
100e4a1717bSMichal Kubecek 	.max_attr		= ETHTOOL_A_RINGS_MAX,
101e4a1717bSMichal Kubecek 	.req_info_size		= sizeof(struct rings_req_info),
102e4a1717bSMichal Kubecek 	.reply_data_size	= sizeof(struct rings_reply_data),
103e4a1717bSMichal Kubecek 	.request_policy		= rings_get_policy,
104e4a1717bSMichal Kubecek 
105e4a1717bSMichal Kubecek 	.prepare_data		= rings_prepare_data,
106e4a1717bSMichal Kubecek 	.reply_size		= rings_reply_size,
107e4a1717bSMichal Kubecek 	.fill_reply		= rings_fill_reply,
108e4a1717bSMichal Kubecek };
1092fc2929eSMichal Kubecek 
1102fc2929eSMichal Kubecek /* RINGS_SET */
1112fc2929eSMichal Kubecek 
1122fc2929eSMichal Kubecek static const struct nla_policy
1132fc2929eSMichal Kubecek rings_set_policy[ETHTOOL_A_RINGS_MAX + 1] = {
1142fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_UNSPEC]		= { .type = NLA_REJECT },
1152fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_HEADER]		= { .type = NLA_NESTED },
1162fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_MAX]		= { .type = NLA_REJECT },
1172fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_MINI_MAX]		= { .type = NLA_REJECT },
1182fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_JUMBO_MAX]		= { .type = NLA_REJECT },
1192fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_TX_MAX]		= { .type = NLA_REJECT },
1202fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX]			= { .type = NLA_U32 },
1212fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_MINI]		= { .type = NLA_U32 },
1222fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_JUMBO]		= { .type = NLA_U32 },
1232fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_TX]			= { .type = NLA_U32 },
1242fc2929eSMichal Kubecek };
1252fc2929eSMichal Kubecek 
1262fc2929eSMichal Kubecek int ethnl_set_rings(struct sk_buff *skb, struct genl_info *info)
1272fc2929eSMichal Kubecek {
1282fc2929eSMichal Kubecek 	struct nlattr *tb[ETHTOOL_A_RINGS_MAX + 1];
1292fc2929eSMichal Kubecek 	struct ethtool_ringparam ringparam = {};
1302fc2929eSMichal Kubecek 	struct ethnl_req_info req_info = {};
1312fc2929eSMichal Kubecek 	const struct nlattr *err_attr;
1322fc2929eSMichal Kubecek 	const struct ethtool_ops *ops;
1332fc2929eSMichal Kubecek 	struct net_device *dev;
1342fc2929eSMichal Kubecek 	bool mod = false;
1352fc2929eSMichal Kubecek 	int ret;
1362fc2929eSMichal Kubecek 
1372fc2929eSMichal Kubecek 	ret = nlmsg_parse(info->nlhdr, GENL_HDRLEN, tb,
1382fc2929eSMichal Kubecek 			  ETHTOOL_A_RINGS_MAX, rings_set_policy,
1392fc2929eSMichal Kubecek 			  info->extack);
1402fc2929eSMichal Kubecek 	if (ret < 0)
1412fc2929eSMichal Kubecek 		return ret;
1422fc2929eSMichal Kubecek 	ret = ethnl_parse_header_dev_get(&req_info,
1432fc2929eSMichal Kubecek 					 tb[ETHTOOL_A_RINGS_HEADER],
1442fc2929eSMichal Kubecek 					 genl_info_net(info), info->extack,
1452fc2929eSMichal Kubecek 					 true);
1462fc2929eSMichal Kubecek 	if (ret < 0)
1472fc2929eSMichal Kubecek 		return ret;
1482fc2929eSMichal Kubecek 	dev = req_info.dev;
1492fc2929eSMichal Kubecek 	ops = dev->ethtool_ops;
1502fc2929eSMichal Kubecek 	ret = -EOPNOTSUPP;
1512fc2929eSMichal Kubecek 	if (!ops->get_ringparam || !ops->set_ringparam)
1522fc2929eSMichal Kubecek 		goto out_dev;
1532fc2929eSMichal Kubecek 
1542fc2929eSMichal Kubecek 	rtnl_lock();
1552fc2929eSMichal Kubecek 	ret = ethnl_ops_begin(dev);
1562fc2929eSMichal Kubecek 	if (ret < 0)
1572fc2929eSMichal Kubecek 		goto out_rtnl;
1582fc2929eSMichal Kubecek 	ops->get_ringparam(dev, &ringparam);
1592fc2929eSMichal Kubecek 
1602fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.rx_pending, tb[ETHTOOL_A_RINGS_RX], &mod);
1612fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.rx_mini_pending,
1622fc2929eSMichal Kubecek 			 tb[ETHTOOL_A_RINGS_RX_MINI], &mod);
1632fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.rx_jumbo_pending,
1642fc2929eSMichal Kubecek 			 tb[ETHTOOL_A_RINGS_RX_JUMBO], &mod);
1652fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.tx_pending, tb[ETHTOOL_A_RINGS_TX], &mod);
1662fc2929eSMichal Kubecek 	ret = 0;
1672fc2929eSMichal Kubecek 	if (!mod)
1682fc2929eSMichal Kubecek 		goto out_ops;
1692fc2929eSMichal Kubecek 
1702fc2929eSMichal Kubecek 	/* ensure new ring parameters are within limits */
1712fc2929eSMichal Kubecek 	if (ringparam.rx_pending > ringparam.rx_max_pending)
1722fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_RX];
1732fc2929eSMichal Kubecek 	else if (ringparam.rx_mini_pending > ringparam.rx_mini_max_pending)
1742fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_RX_MINI];
1752fc2929eSMichal Kubecek 	else if (ringparam.rx_jumbo_pending > ringparam.rx_jumbo_max_pending)
1762fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_RX_JUMBO];
1772fc2929eSMichal Kubecek 	else if (ringparam.tx_pending > ringparam.tx_max_pending)
1782fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_TX];
1792fc2929eSMichal Kubecek 	else
1802fc2929eSMichal Kubecek 		err_attr = NULL;
1812fc2929eSMichal Kubecek 	if (err_attr) {
1822fc2929eSMichal Kubecek 		ret = -EINVAL;
1832fc2929eSMichal Kubecek 		NL_SET_ERR_MSG_ATTR(info->extack, err_attr,
184*5ec82c49SColin Ian King 				    "requested ring size exceeds maximum");
1852fc2929eSMichal Kubecek 		goto out_ops;
1862fc2929eSMichal Kubecek 	}
1872fc2929eSMichal Kubecek 
1882fc2929eSMichal Kubecek 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
189bc9d1c99SMichal Kubecek 	if (ret < 0)
190bc9d1c99SMichal Kubecek 		goto out_ops;
191bc9d1c99SMichal Kubecek 	ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1922fc2929eSMichal Kubecek 
1932fc2929eSMichal Kubecek out_ops:
1942fc2929eSMichal Kubecek 	ethnl_ops_complete(dev);
1952fc2929eSMichal Kubecek out_rtnl:
1962fc2929eSMichal Kubecek 	rtnl_unlock();
1972fc2929eSMichal Kubecek out_dev:
1982fc2929eSMichal Kubecek 	dev_put(dev);
1992fc2929eSMichal Kubecek 	return ret;
2002fc2929eSMichal Kubecek }
201