xref: /openbmc/linux/net/ethtool/rings.c (revision 98b37770)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include "netlink.h"
4 #include "common.h"
5 
6 struct rings_req_info {
7 	struct ethnl_req_info		base;
8 };
9 
10 struct rings_reply_data {
11 	struct ethnl_reply_data		base;
12 	struct ethtool_ringparam	ringparam;
13 	struct kernel_ethtool_ringparam	kernel_ringparam;
14 	u32				supported_ring_params;
15 };
16 
17 #define RINGS_REPDATA(__reply_base) \
18 	container_of(__reply_base, struct rings_reply_data, base)
19 
20 const struct nla_policy ethnl_rings_get_policy[] = {
21 	[ETHTOOL_A_RINGS_HEADER]		=
22 		NLA_POLICY_NESTED(ethnl_header_policy),
23 };
24 
25 static int rings_prepare_data(const struct ethnl_req_info *req_base,
26 			      struct ethnl_reply_data *reply_base,
27 			      const struct genl_info *info)
28 {
29 	struct rings_reply_data *data = RINGS_REPDATA(reply_base);
30 	struct net_device *dev = reply_base->dev;
31 	int ret;
32 
33 	if (!dev->ethtool_ops->get_ringparam)
34 		return -EOPNOTSUPP;
35 
36 	data->supported_ring_params = dev->ethtool_ops->supported_ring_params;
37 	ret = ethnl_ops_begin(dev);
38 	if (ret < 0)
39 		return ret;
40 	dev->ethtool_ops->get_ringparam(dev, &data->ringparam,
41 					&data->kernel_ringparam, info->extack);
42 	ethnl_ops_complete(dev);
43 
44 	return 0;
45 }
46 
47 static int rings_reply_size(const struct ethnl_req_info *req_base,
48 			    const struct ethnl_reply_data *reply_base)
49 {
50 	return nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MAX */
51 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI_MAX */
52 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO_MAX */
53 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX_MAX */
54 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX */
55 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_MINI */
56 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_JUMBO */
57 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX */
58 	       nla_total_size(sizeof(u32)) +	/* _RINGS_RX_BUF_LEN */
59 	       nla_total_size(sizeof(u8))  +	/* _RINGS_TCP_DATA_SPLIT */
60 	       nla_total_size(sizeof(u32)  +	/* _RINGS_CQE_SIZE */
61 	       nla_total_size(sizeof(u8))  +	/* _RINGS_TX_PUSH */
62 	       nla_total_size(sizeof(u8))) +	/* _RINGS_RX_PUSH */
63 	       nla_total_size(sizeof(u32)) +	/* _RINGS_TX_PUSH_BUF_LEN */
64 	       nla_total_size(sizeof(u32));	/* _RINGS_TX_PUSH_BUF_LEN_MAX */
65 }
66 
67 static int rings_fill_reply(struct sk_buff *skb,
68 			    const struct ethnl_req_info *req_base,
69 			    const struct ethnl_reply_data *reply_base)
70 {
71 	const struct rings_reply_data *data = RINGS_REPDATA(reply_base);
72 	const struct kernel_ethtool_ringparam *kr = &data->kernel_ringparam;
73 	const struct ethtool_ringparam *ringparam = &data->ringparam;
74 	u32 supported_ring_params = data->supported_ring_params;
75 
76 	WARN_ON(kr->tcp_data_split > ETHTOOL_TCP_DATA_SPLIT_ENABLED);
77 
78 	if ((ringparam->rx_max_pending &&
79 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MAX,
80 			  ringparam->rx_max_pending) ||
81 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX,
82 			  ringparam->rx_pending))) ||
83 	    (ringparam->rx_mini_max_pending &&
84 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI_MAX,
85 			  ringparam->rx_mini_max_pending) ||
86 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI,
87 			  ringparam->rx_mini_pending))) ||
88 	    (ringparam->rx_jumbo_max_pending &&
89 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO_MAX,
90 			  ringparam->rx_jumbo_max_pending) ||
91 	      nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO,
92 			  ringparam->rx_jumbo_pending))) ||
93 	    (ringparam->tx_max_pending &&
94 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_MAX,
95 			  ringparam->tx_max_pending) ||
96 	      nla_put_u32(skb, ETHTOOL_A_RINGS_TX,
97 			  ringparam->tx_pending)))  ||
98 	    (kr->rx_buf_len &&
99 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_BUF_LEN, kr->rx_buf_len))) ||
100 	    (kr->tcp_data_split &&
101 	     (nla_put_u8(skb, ETHTOOL_A_RINGS_TCP_DATA_SPLIT,
102 			 kr->tcp_data_split))) ||
103 	    (kr->cqe_size &&
104 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_CQE_SIZE, kr->cqe_size))) ||
105 	    nla_put_u8(skb, ETHTOOL_A_RINGS_TX_PUSH, !!kr->tx_push) ||
106 	    nla_put_u8(skb, ETHTOOL_A_RINGS_RX_PUSH, !!kr->rx_push) ||
107 	    ((supported_ring_params & ETHTOOL_RING_USE_TX_PUSH_BUF_LEN) &&
108 	     (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN_MAX,
109 			  kr->tx_push_buf_max_len) ||
110 	      nla_put_u32(skb, ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN,
111 			  kr->tx_push_buf_len))))
112 		return -EMSGSIZE;
113 
114 	return 0;
115 }
116 
117 /* RINGS_SET */
118 
119 const struct nla_policy ethnl_rings_set_policy[] = {
120 	[ETHTOOL_A_RINGS_HEADER]		=
121 		NLA_POLICY_NESTED(ethnl_header_policy),
122 	[ETHTOOL_A_RINGS_RX]			= { .type = NLA_U32 },
123 	[ETHTOOL_A_RINGS_RX_MINI]		= { .type = NLA_U32 },
124 	[ETHTOOL_A_RINGS_RX_JUMBO]		= { .type = NLA_U32 },
125 	[ETHTOOL_A_RINGS_TX]			= { .type = NLA_U32 },
126 	[ETHTOOL_A_RINGS_RX_BUF_LEN]            = NLA_POLICY_MIN(NLA_U32, 1),
127 	[ETHTOOL_A_RINGS_CQE_SIZE]		= NLA_POLICY_MIN(NLA_U32, 1),
128 	[ETHTOOL_A_RINGS_TX_PUSH]		= NLA_POLICY_MAX(NLA_U8, 1),
129 	[ETHTOOL_A_RINGS_RX_PUSH]		= NLA_POLICY_MAX(NLA_U8, 1),
130 	[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN]	= { .type = NLA_U32 },
131 };
132 
133 static int
134 ethnl_set_rings_validate(struct ethnl_req_info *req_info,
135 			 struct genl_info *info)
136 {
137 	const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
138 	struct nlattr **tb = info->attrs;
139 
140 	if (tb[ETHTOOL_A_RINGS_RX_BUF_LEN] &&
141 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_RX_BUF_LEN)) {
142 		NL_SET_ERR_MSG_ATTR(info->extack,
143 				    tb[ETHTOOL_A_RINGS_RX_BUF_LEN],
144 				    "setting rx buf len not supported");
145 		return -EOPNOTSUPP;
146 	}
147 
148 	if (tb[ETHTOOL_A_RINGS_CQE_SIZE] &&
149 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_CQE_SIZE)) {
150 		NL_SET_ERR_MSG_ATTR(info->extack,
151 				    tb[ETHTOOL_A_RINGS_CQE_SIZE],
152 				    "setting cqe size not supported");
153 		return -EOPNOTSUPP;
154 	}
155 
156 	if (tb[ETHTOOL_A_RINGS_TX_PUSH] &&
157 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_TX_PUSH)) {
158 		NL_SET_ERR_MSG_ATTR(info->extack,
159 				    tb[ETHTOOL_A_RINGS_TX_PUSH],
160 				    "setting tx push not supported");
161 		return -EOPNOTSUPP;
162 	}
163 
164 	if (tb[ETHTOOL_A_RINGS_RX_PUSH] &&
165 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_RX_PUSH)) {
166 		NL_SET_ERR_MSG_ATTR(info->extack,
167 				    tb[ETHTOOL_A_RINGS_RX_PUSH],
168 				    "setting rx push not supported");
169 		return -EOPNOTSUPP;
170 	}
171 
172 	if (tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN] &&
173 	    !(ops->supported_ring_params & ETHTOOL_RING_USE_TX_PUSH_BUF_LEN)) {
174 		NL_SET_ERR_MSG_ATTR(info->extack,
175 				    tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN],
176 				    "setting tx push buf len is not supported");
177 		return -EOPNOTSUPP;
178 	}
179 
180 	return ops->get_ringparam && ops->set_ringparam ? 1 : -EOPNOTSUPP;
181 }
182 
183 static int
184 ethnl_set_rings(struct ethnl_req_info *req_info, struct genl_info *info)
185 {
186 	struct kernel_ethtool_ringparam kernel_ringparam = {};
187 	struct ethtool_ringparam ringparam = {};
188 	struct net_device *dev = req_info->dev;
189 	struct nlattr **tb = info->attrs;
190 	const struct nlattr *err_attr;
191 	bool mod = false;
192 	int ret;
193 
194 	dev->ethtool_ops->get_ringparam(dev, &ringparam,
195 					&kernel_ringparam, info->extack);
196 
197 	ethnl_update_u32(&ringparam.rx_pending, tb[ETHTOOL_A_RINGS_RX], &mod);
198 	ethnl_update_u32(&ringparam.rx_mini_pending,
199 			 tb[ETHTOOL_A_RINGS_RX_MINI], &mod);
200 	ethnl_update_u32(&ringparam.rx_jumbo_pending,
201 			 tb[ETHTOOL_A_RINGS_RX_JUMBO], &mod);
202 	ethnl_update_u32(&ringparam.tx_pending, tb[ETHTOOL_A_RINGS_TX], &mod);
203 	ethnl_update_u32(&kernel_ringparam.rx_buf_len,
204 			 tb[ETHTOOL_A_RINGS_RX_BUF_LEN], &mod);
205 	ethnl_update_u32(&kernel_ringparam.cqe_size,
206 			 tb[ETHTOOL_A_RINGS_CQE_SIZE], &mod);
207 	ethnl_update_u8(&kernel_ringparam.tx_push,
208 			tb[ETHTOOL_A_RINGS_TX_PUSH], &mod);
209 	ethnl_update_u8(&kernel_ringparam.rx_push,
210 			tb[ETHTOOL_A_RINGS_RX_PUSH], &mod);
211 	ethnl_update_u32(&kernel_ringparam.tx_push_buf_len,
212 			 tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN], &mod);
213 	if (!mod)
214 		return 0;
215 
216 	/* ensure new ring parameters are within limits */
217 	if (ringparam.rx_pending > ringparam.rx_max_pending)
218 		err_attr = tb[ETHTOOL_A_RINGS_RX];
219 	else if (ringparam.rx_mini_pending > ringparam.rx_mini_max_pending)
220 		err_attr = tb[ETHTOOL_A_RINGS_RX_MINI];
221 	else if (ringparam.rx_jumbo_pending > ringparam.rx_jumbo_max_pending)
222 		err_attr = tb[ETHTOOL_A_RINGS_RX_JUMBO];
223 	else if (ringparam.tx_pending > ringparam.tx_max_pending)
224 		err_attr = tb[ETHTOOL_A_RINGS_TX];
225 	else
226 		err_attr = NULL;
227 	if (err_attr) {
228 		NL_SET_ERR_MSG_ATTR(info->extack, err_attr,
229 				    "requested ring size exceeds maximum");
230 		return -EINVAL;
231 	}
232 
233 	if (kernel_ringparam.tx_push_buf_len > kernel_ringparam.tx_push_buf_max_len) {
234 		NL_SET_ERR_MSG_ATTR_FMT(info->extack, tb[ETHTOOL_A_RINGS_TX_PUSH_BUF_LEN],
235 					"Requested TX push buffer exceeds the maximum of %u",
236 					kernel_ringparam.tx_push_buf_max_len);
237 
238 		return -EINVAL;
239 	}
240 
241 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
242 					      &kernel_ringparam, info->extack);
243 	return ret < 0 ? ret : 1;
244 }
245 
246 const struct ethnl_request_ops ethnl_rings_request_ops = {
247 	.request_cmd		= ETHTOOL_MSG_RINGS_GET,
248 	.reply_cmd		= ETHTOOL_MSG_RINGS_GET_REPLY,
249 	.hdr_attr		= ETHTOOL_A_RINGS_HEADER,
250 	.req_info_size		= sizeof(struct rings_req_info),
251 	.reply_data_size	= sizeof(struct rings_reply_data),
252 
253 	.prepare_data		= rings_prepare_data,
254 	.reply_size		= rings_reply_size,
255 	.fill_reply		= rings_fill_reply,
256 
257 	.set_validate		= ethnl_set_rings_validate,
258 	.set			= ethnl_set_rings,
259 	.set_ntf_cmd		= ETHTOOL_MSG_RINGS_NTF,
260 };
261