xref: /openbmc/linux/net/ethtool/rings.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
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;
130b70c256SHao Chen 	struct kernel_ethtool_ringparam	kernel_ringparam;
14233eb4e7SShay Agroskin 	u32				supported_ring_params;
15e4a1717bSMichal Kubecek };
16e4a1717bSMichal Kubecek 
17e4a1717bSMichal Kubecek #define RINGS_REPDATA(__reply_base) \
18e4a1717bSMichal Kubecek 	container_of(__reply_base, struct rings_reply_data, base)
19e4a1717bSMichal Kubecek 
20ff419afaSJakub Kicinski const struct nla_policy ethnl_rings_get_policy[] = {
21329d9c33SJakub Kicinski 	[ETHTOOL_A_RINGS_HEADER]		=
22329d9c33SJakub Kicinski 		NLA_POLICY_NESTED(ethnl_header_policy),
23e4a1717bSMichal Kubecek };
24e4a1717bSMichal Kubecek 
rings_prepare_data(const struct ethnl_req_info * req_base,struct ethnl_reply_data * reply_base,const struct genl_info * info)25e4a1717bSMichal Kubecek static int rings_prepare_data(const struct ethnl_req_info *req_base,
26e4a1717bSMichal Kubecek 			      struct ethnl_reply_data *reply_base,
27*f946270dSJakub Kicinski 			      const struct genl_info *info)
28e4a1717bSMichal Kubecek {
29e4a1717bSMichal Kubecek 	struct rings_reply_data *data = RINGS_REPDATA(reply_base);
30e4a1717bSMichal Kubecek 	struct net_device *dev = reply_base->dev;
31e4a1717bSMichal Kubecek 	int ret;
32e4a1717bSMichal Kubecek 
33e4a1717bSMichal Kubecek 	if (!dev->ethtool_ops->get_ringparam)
34e4a1717bSMichal Kubecek 		return -EOPNOTSUPP;
35233eb4e7SShay Agroskin 
36233eb4e7SShay Agroskin 	data->supported_ring_params = dev->ethtool_ops->supported_ring_params;
37e4a1717bSMichal Kubecek 	ret = ethnl_ops_begin(dev);
38e4a1717bSMichal Kubecek 	if (ret < 0)
39e4a1717bSMichal Kubecek 		return ret;
4074624944SHao Chen 	dev->ethtool_ops->get_ringparam(dev, &data->ringparam,
41*f946270dSJakub Kicinski 					&data->kernel_ringparam, info->extack);
42e4a1717bSMichal Kubecek 	ethnl_ops_complete(dev);
43e4a1717bSMichal Kubecek 
44e4a1717bSMichal Kubecek 	return 0;
45e4a1717bSMichal Kubecek }
46e4a1717bSMichal Kubecek 
rings_reply_size(const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)47e4a1717bSMichal Kubecek static int rings_reply_size(const struct ethnl_req_info *req_base,
48e4a1717bSMichal Kubecek 			    const struct ethnl_reply_data *reply_base)
49e4a1717bSMichal Kubecek {
50e4a1717bSMichal Kubecek 	return nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MAX */
51e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI_MAX */
52e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO_MAX */
53e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX_MAX */
54e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX */
55e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI */
56e4a1717bSMichal Kubecek 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO */
570b70c256SHao Chen 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX */
589690ae60SJakub Kicinski 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_BUF_LEN */
591241e329SSubbaraya Sundeep 	       nla_total_size(sizeof(u8))  +	/* _RINGS_TCP_DATA_SPLIT */
604dc84c06SJie Wang 	       nla_total_size(sizeof(u32)  +	/* _RINGS_CQE_SIZE */
615b4e9a7aSShannon Nelson 	       nla_total_size(sizeof(u8))  +	/* _RINGS_TX_PUSH */
62233eb4e7SShay Agroskin 	       nla_total_size(sizeof(u8))) +	/* _RINGS_RX_PUSH */
63233eb4e7SShay Agroskin 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX_PUSH_BUF_LEN */
64233eb4e7SShay Agroskin 	       nla_total_size(sizeof(u32));	/* _RINGS_TX_PUSH_BUF_LEN_MAX */
65e4a1717bSMichal Kubecek }
66e4a1717bSMichal Kubecek 
rings_fill_reply(struct sk_buff * skb,const struct ethnl_req_info * req_base,const struct ethnl_reply_data * reply_base)67e4a1717bSMichal Kubecek static int rings_fill_reply(struct sk_buff *skb,
68e4a1717bSMichal Kubecek 			    const struct ethnl_req_info *req_base,
69e4a1717bSMichal Kubecek 			    const struct ethnl_reply_data *reply_base)
70e4a1717bSMichal Kubecek {
71e4a1717bSMichal Kubecek 	const struct rings_reply_data *data = RINGS_REPDATA(reply_base);
729690ae60SJakub Kicinski 	const struct kernel_ethtool_ringparam *kr = &data->kernel_ringparam;
73e4a1717bSMichal Kubecek 	const struct ethtool_ringparam *ringparam = &data->ringparam;
74233eb4e7SShay Agroskin 	u32 supported_ring_params = data->supported_ring_params;
75e4a1717bSMichal Kubecek 
769690ae60SJakub Kicinski 	WARN_ON(kr->tcp_data_split > ETHTOOL_TCP_DATA_SPLIT_ENABLED);
779690ae60SJakub Kicinski 
78e4a1717bSMichal Kubecek 	if ((ringparam->rx_max_pending &&
79e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MAX,
80e4a1717bSMichal Kubecek 			  ringparam->rx_max_pending) ||
81e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX,
82e4a1717bSMichal Kubecek 			  ringparam->rx_pending))) ||
83e4a1717bSMichal Kubecek 	    (ringparam->rx_mini_max_pending &&
84e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI_MAX,
85e4a1717bSMichal Kubecek 			  ringparam->rx_mini_max_pending) ||
86e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI,
87e4a1717bSMichal Kubecek 			  ringparam->rx_mini_pending))) ||
88e4a1717bSMichal Kubecek 	    (ringparam->rx_jumbo_max_pending &&
89e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO_MAX,
90e4a1717bSMichal Kubecek 			  ringparam->rx_jumbo_max_pending) ||
91e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO,
92e4a1717bSMichal Kubecek 			  ringparam->rx_jumbo_pending))) ||
93e4a1717bSMichal Kubecek 	    (ringparam->tx_max_pending &&
94e4a1717bSMichal Kubecek 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_MAX,
95e4a1717bSMichal Kubecek 			  ringparam->tx_max_pending) ||
96e4a1717bSMichal Kubecek 	      nla_put_u32(skb, ETHTOOL_A_RINGS_TX,
970b70c256SHao Chen 			  ringparam->tx_pending)))  ||
989690ae60SJakub Kicinski 	    (kr->rx_buf_len &&
999690ae60SJakub Kicinski 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_BUF_LEN, kr->rx_buf_len))) ||
1009690ae60SJakub Kicinski 	    (kr->tcp_data_split &&
1019690ae60SJakub Kicinski 	     (nla_put_u8(skb, ETHTOOL_A_RINGS_TCP_DATA_SPLIT,
1021241e329SSubbaraya Sundeep 			 kr->tcp_data_split))) ||
1031241e329SSubbaraya Sundeep 	    (kr->cqe_size &&
1044dc84c06SJie Wang 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_CQE_SIZE, kr->cqe_size))) ||
1055b4e9a7aSShannon Nelson 	    nla_put_u8(skb, ETHTOOL_A_RINGS_TX_PUSH, !!kr->tx_push) ||
106233eb4e7SShay Agroskin 	    nla_put_u8(skb, ETHTOOL_A_RINGS_RX_PUSH, !!kr->rx_push) ||
107233eb4e7SShay Agroskin 	    ((supported_ring_params & ETHTOOL_RING_USE_TX_PUSH_BUF_LEN) &&
108233eb4e7SShay Agroskin 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN_MAX,
109233eb4e7SShay Agroskin 			  kr->tx_push_buf_max_len) ||
110233eb4e7SShay Agroskin 	      nla_put_u32(skb, ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN,
111233eb4e7SShay Agroskin 			  kr->tx_push_buf_len))))
112e4a1717bSMichal Kubecek 		return -EMSGSIZE;
113e4a1717bSMichal Kubecek 
114e4a1717bSMichal Kubecek 	return 0;
115e4a1717bSMichal Kubecek }
116e4a1717bSMichal Kubecek 
1172fc2929eSMichal Kubecek /* RINGS_SET */
1182fc2929eSMichal Kubecek 
119ff419afaSJakub Kicinski const struct nla_policy ethnl_rings_set_policy[] = {
120329d9c33SJakub Kicinski 	[ETHTOOL_A_RINGS_HEADER]		=
121329d9c33SJakub Kicinski 		NLA_POLICY_NESTED(ethnl_header_policy),
1222fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX]			= { .type = NLA_U32 },
1232fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_MINI]		= { .type = NLA_U32 },
1242fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_RX_JUMBO]		= { .type = NLA_U32 },
1252fc2929eSMichal Kubecek 	[ETHTOOL_A_RINGS_TX]			= { .type = NLA_U32 },
1260b70c256SHao Chen 	[ETHTOOL_A_RINGS_RX_BUF_LEN]            = NLA_POLICY_MIN(NLA_U32, 1),
1271241e329SSubbaraya Sundeep 	[ETHTOOL_A_RINGS_CQE_SIZE]		= NLA_POLICY_MIN(NLA_U32, 1),
1284dc84c06SJie Wang 	[ETHTOOL_A_RINGS_TX_PUSH]		= NLA_POLICY_MAX(NLA_U8, 1),
1295b4e9a7aSShannon Nelson 	[ETHTOOL_A_RINGS_RX_PUSH]		= NLA_POLICY_MAX(NLA_U8, 1),
130233eb4e7SShay Agroskin 	[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN]	= { .type = NLA_U32 },
1312fc2929eSMichal Kubecek };
1322fc2929eSMichal Kubecek 
13304007961SJakub Kicinski static int
ethnl_set_rings_validate(struct ethnl_req_info * req_info,struct genl_info * info)13404007961SJakub Kicinski ethnl_set_rings_validate(struct ethnl_req_info *req_info,
13504007961SJakub Kicinski 			 struct genl_info *info)
1362fc2929eSMichal Kubecek {
13704007961SJakub Kicinski 	const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
1385028588bSJakub Kicinski 	struct nlattr **tb = info->attrs;
1392fc2929eSMichal Kubecek 
140bde292c0SJie Wang 	if (tb[ETHTOOL_A_RINGS_RX_BUF_LEN] &&
141bde292c0SJie Wang 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_RX_BUF_LEN)) {
142bde292c0SJie Wang 		NL_SET_ERR_MSG_ATTR(info->extack,
143bde292c0SJie Wang 				    tb[ETHTOOL_A_RINGS_RX_BUF_LEN],
144bde292c0SJie Wang 				    "setting rx buf len not supported");
14504007961SJakub Kicinski 		return -EOPNOTSUPP;
146bde292c0SJie Wang 	}
147bde292c0SJie Wang 
148bde292c0SJie Wang 	if (tb[ETHTOOL_A_RINGS_CQE_SIZE] &&
149bde292c0SJie Wang 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_CQE_SIZE)) {
150bde292c0SJie Wang 		NL_SET_ERR_MSG_ATTR(info->extack,
151bde292c0SJie Wang 				    tb[ETHTOOL_A_RINGS_CQE_SIZE],
152bde292c0SJie Wang 				    "setting cqe size not supported");
15304007961SJakub Kicinski 		return -EOPNOTSUPP;
154bde292c0SJie Wang 	}
155bde292c0SJie Wang 
1564dc84c06SJie Wang 	if (tb[ETHTOOL_A_RINGS_TX_PUSH] &&
1574dc84c06SJie Wang 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_TX_PUSH)) {
1584dc84c06SJie Wang 		NL_SET_ERR_MSG_ATTR(info->extack,
1594dc84c06SJie Wang 				    tb[ETHTOOL_A_RINGS_TX_PUSH],
1604dc84c06SJie Wang 				    "setting tx push not supported");
16104007961SJakub Kicinski 		return -EOPNOTSUPP;
1624dc84c06SJie Wang 	}
1634dc84c06SJie Wang 
1645b4e9a7aSShannon Nelson 	if (tb[ETHTOOL_A_RINGS_RX_PUSH] &&
1655b4e9a7aSShannon Nelson 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_RX_PUSH)) {
1665b4e9a7aSShannon Nelson 		NL_SET_ERR_MSG_ATTR(info->extack,
1675b4e9a7aSShannon Nelson 				    tb[ETHTOOL_A_RINGS_RX_PUSH],
1685b4e9a7aSShannon Nelson 				    "setting rx push not supported");
1695b4e9a7aSShannon Nelson 		return -EOPNOTSUPP;
1705b4e9a7aSShannon Nelson 	}
1715b4e9a7aSShannon Nelson 
172233eb4e7SShay Agroskin 	if (tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN] &&
173233eb4e7SShay Agroskin 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_TX_PUSH_BUF_LEN)) {
174233eb4e7SShay Agroskin 		NL_SET_ERR_MSG_ATTR(info->extack,
175233eb4e7SShay Agroskin 				    tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN],
176233eb4e7SShay Agroskin 				    "setting tx push buf len is not supported");
177233eb4e7SShay Agroskin 		return -EOPNOTSUPP;
178233eb4e7SShay Agroskin 	}
179233eb4e7SShay Agroskin 
18004007961SJakub Kicinski 	return ops->get_ringparam && ops->set_ringparam ? 1 : -EOPNOTSUPP;
18104007961SJakub Kicinski }
18204007961SJakub Kicinski 
18304007961SJakub Kicinski static int
ethnl_set_rings(struct ethnl_req_info * req_info,struct genl_info * info)18404007961SJakub Kicinski ethnl_set_rings(struct ethnl_req_info *req_info, struct genl_info *info)
18504007961SJakub Kicinski {
18604007961SJakub Kicinski 	struct kernel_ethtool_ringparam kernel_ringparam = {};
18704007961SJakub Kicinski 	struct ethtool_ringparam ringparam = {};
18804007961SJakub Kicinski 	struct net_device *dev = req_info->dev;
18904007961SJakub Kicinski 	struct nlattr **tb = info->attrs;
19004007961SJakub Kicinski 	const struct nlattr *err_attr;
19104007961SJakub Kicinski 	bool mod = false;
19204007961SJakub Kicinski 	int ret;
19304007961SJakub Kicinski 
19404007961SJakub Kicinski 	dev->ethtool_ops->get_ringparam(dev, &ringparam,
19504007961SJakub Kicinski 					&kernel_ringparam, info->extack);
1962fc2929eSMichal Kubecek 
1972fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.rx_pending, tb[ETHTOOL_A_RINGS_RX], &mod);
1982fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.rx_mini_pending,
1992fc2929eSMichal Kubecek 			 tb[ETHTOOL_A_RINGS_RX_MINI], &mod);
2002fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.rx_jumbo_pending,
2012fc2929eSMichal Kubecek 			 tb[ETHTOOL_A_RINGS_RX_JUMBO], &mod);
2022fc2929eSMichal Kubecek 	ethnl_update_u32(&ringparam.tx_pending, tb[ETHTOOL_A_RINGS_TX], &mod);
2030b70c256SHao Chen 	ethnl_update_u32(&kernel_ringparam.rx_buf_len,
2040b70c256SHao Chen 			 tb[ETHTOOL_A_RINGS_RX_BUF_LEN], &mod);
2051241e329SSubbaraya Sundeep 	ethnl_update_u32(&kernel_ringparam.cqe_size,
2061241e329SSubbaraya Sundeep 			 tb[ETHTOOL_A_RINGS_CQE_SIZE], &mod);
2074dc84c06SJie Wang 	ethnl_update_u8(&kernel_ringparam.tx_push,
2084dc84c06SJie Wang 			tb[ETHTOOL_A_RINGS_TX_PUSH], &mod);
2095b4e9a7aSShannon Nelson 	ethnl_update_u8(&kernel_ringparam.rx_push,
2105b4e9a7aSShannon Nelson 			tb[ETHTOOL_A_RINGS_RX_PUSH], &mod);
211233eb4e7SShay Agroskin 	ethnl_update_u32(&kernel_ringparam.tx_push_buf_len,
212233eb4e7SShay Agroskin 			 tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN], &mod);
2132fc2929eSMichal Kubecek 	if (!mod)
21404007961SJakub Kicinski 		return 0;
2152fc2929eSMichal Kubecek 
2162fc2929eSMichal Kubecek 	/* ensure new ring parameters are within limits */
2172fc2929eSMichal Kubecek 	if (ringparam.rx_pending > ringparam.rx_max_pending)
2182fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_RX];
2192fc2929eSMichal Kubecek 	else if (ringparam.rx_mini_pending > ringparam.rx_mini_max_pending)
2202fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_RX_MINI];
2212fc2929eSMichal Kubecek 	else if (ringparam.rx_jumbo_pending > ringparam.rx_jumbo_max_pending)
2222fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_RX_JUMBO];
2232fc2929eSMichal Kubecek 	else if (ringparam.tx_pending > ringparam.tx_max_pending)
2242fc2929eSMichal Kubecek 		err_attr = tb[ETHTOOL_A_RINGS_TX];
2252fc2929eSMichal Kubecek 	else
2262fc2929eSMichal Kubecek 		err_attr = NULL;
2272fc2929eSMichal Kubecek 	if (err_attr) {
2282fc2929eSMichal Kubecek 		NL_SET_ERR_MSG_ATTR(info->extack, err_attr,
2295ec82c49SColin Ian King 				    "requested ring size exceeds maximum");
23004007961SJakub Kicinski 		return -EINVAL;
2312fc2929eSMichal Kubecek 	}
2322fc2929eSMichal Kubecek 
233233eb4e7SShay Agroskin 	if (kernel_ringparam.tx_push_buf_len > kernel_ringparam.tx_push_buf_max_len) {
234233eb4e7SShay Agroskin 		NL_SET_ERR_MSG_ATTR_FMT(info->extack, tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN],
235233eb4e7SShay Agroskin 					"Requested TX push buffer exceeds the maximum of %u",
236233eb4e7SShay Agroskin 					kernel_ringparam.tx_push_buf_max_len);
237233eb4e7SShay Agroskin 
238233eb4e7SShay Agroskin 		return -EINVAL;
239233eb4e7SShay Agroskin 	}
240233eb4e7SShay Agroskin 
24174624944SHao Chen 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
24274624944SHao Chen 					      &kernel_ringparam, info->extack);
24304007961SJakub Kicinski 	return ret < 0 ? ret : 1;
2442fc2929eSMichal Kubecek }
24504007961SJakub Kicinski 
24604007961SJakub Kicinski const struct ethnl_request_ops ethnl_rings_request_ops = {
24704007961SJakub Kicinski 	.request_cmd		= ETHTOOL_MSG_RINGS_GET,
24804007961SJakub Kicinski 	.reply_cmd		= ETHTOOL_MSG_RINGS_GET_REPLY,
24904007961SJakub Kicinski 	.hdr_attr		= ETHTOOL_A_RINGS_HEADER,
25004007961SJakub Kicinski 	.req_info_size		= sizeof(struct rings_req_info),
25104007961SJakub Kicinski 	.reply_data_size	= sizeof(struct rings_reply_data),
25204007961SJakub Kicinski 
25304007961SJakub Kicinski 	.prepare_data		= rings_prepare_data,
25404007961SJakub Kicinski 	.reply_size		= rings_reply_size,
25504007961SJakub Kicinski 	.fill_reply		= rings_fill_reply,
25604007961SJakub Kicinski 
25704007961SJakub Kicinski 	.set_validate		= ethnl_set_rings_validate,
25804007961SJakub Kicinski 	.set			= ethnl_set_rings,
25904007961SJakub Kicinski 	.set_ntf_cmd		= ETHTOOL_MSG_RINGS_NTF,
26004007961SJakub Kicinski };
261