Commit | Line | Data |
---|---|---|
e4a1717b MK |
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; | |
0b70c256 | 13 | struct kernel_ethtool_ringparam kernel_ringparam; |
e4a1717b MK |
14 | }; |
15 | ||
16 | #define RINGS_REPDATA(__reply_base) \ | |
17 | container_of(__reply_base, struct rings_reply_data, base) | |
18 | ||
ff419afa | 19 | const struct nla_policy ethnl_rings_get_policy[] = { |
329d9c33 JK |
20 | [ETHTOOL_A_RINGS_HEADER] = |
21 | NLA_POLICY_NESTED(ethnl_header_policy), | |
e4a1717b MK |
22 | }; |
23 | ||
24 | static int rings_prepare_data(const struct ethnl_req_info *req_base, | |
25 | struct ethnl_reply_data *reply_base, | |
26 | struct genl_info *info) | |
27 | { | |
28 | struct rings_reply_data *data = RINGS_REPDATA(reply_base); | |
74624944 | 29 | struct netlink_ext_ack *extack = info ? info->extack : NULL; |
e4a1717b MK |
30 | struct net_device *dev = reply_base->dev; |
31 | int ret; | |
32 | ||
33 | if (!dev->ethtool_ops->get_ringparam) | |
34 | return -EOPNOTSUPP; | |
35 | ret = ethnl_ops_begin(dev); | |
36 | if (ret < 0) | |
37 | return ret; | |
74624944 HC |
38 | dev->ethtool_ops->get_ringparam(dev, &data->ringparam, |
39 | &data->kernel_ringparam, extack); | |
e4a1717b MK |
40 | ethnl_ops_complete(dev); |
41 | ||
42 | return 0; | |
43 | } | |
44 | ||
45 | static int rings_reply_size(const struct ethnl_req_info *req_base, | |
46 | const struct ethnl_reply_data *reply_base) | |
47 | { | |
48 | return nla_total_size(sizeof(u32)) + /* _RINGS_RX_MAX */ | |
49 | nla_total_size(sizeof(u32)) + /* _RINGS_RX_MINI_MAX */ | |
50 | nla_total_size(sizeof(u32)) + /* _RINGS_RX_JUMBO_MAX */ | |
51 | nla_total_size(sizeof(u32)) + /* _RINGS_TX_MAX */ | |
52 | nla_total_size(sizeof(u32)) + /* _RINGS_RX */ | |
53 | nla_total_size(sizeof(u32)) + /* _RINGS_RX_MINI */ | |
54 | nla_total_size(sizeof(u32)) + /* _RINGS_RX_JUMBO */ | |
0b70c256 | 55 | nla_total_size(sizeof(u32)) + /* _RINGS_TX */ |
9690ae60 | 56 | nla_total_size(sizeof(u32)) + /* _RINGS_RX_BUF_LEN */ |
1241e329 | 57 | nla_total_size(sizeof(u8)) + /* _RINGS_TCP_DATA_SPLIT */ |
4dc84c06 | 58 | nla_total_size(sizeof(u32) + /* _RINGS_CQE_SIZE */ |
5b4e9a7a SN |
59 | nla_total_size(sizeof(u8)) + /* _RINGS_TX_PUSH */ |
60 | nla_total_size(sizeof(u8))); /* _RINGS_RX_PUSH */ | |
e4a1717b MK |
61 | } |
62 | ||
63 | static int rings_fill_reply(struct sk_buff *skb, | |
64 | const struct ethnl_req_info *req_base, | |
65 | const struct ethnl_reply_data *reply_base) | |
66 | { | |
67 | const struct rings_reply_data *data = RINGS_REPDATA(reply_base); | |
9690ae60 | 68 | const struct kernel_ethtool_ringparam *kr = &data->kernel_ringparam; |
e4a1717b MK |
69 | const struct ethtool_ringparam *ringparam = &data->ringparam; |
70 | ||
9690ae60 JK |
71 | WARN_ON(kr->tcp_data_split > ETHTOOL_TCP_DATA_SPLIT_ENABLED); |
72 | ||
e4a1717b MK |
73 | if ((ringparam->rx_max_pending && |
74 | (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MAX, | |
75 | ringparam->rx_max_pending) || | |
76 | nla_put_u32(skb, ETHTOOL_A_RINGS_RX, | |
77 | ringparam->rx_pending))) || | |
78 | (ringparam->rx_mini_max_pending && | |
79 | (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI_MAX, | |
80 | ringparam->rx_mini_max_pending) || | |
81 | nla_put_u32(skb, ETHTOOL_A_RINGS_RX_MINI, | |
82 | ringparam->rx_mini_pending))) || | |
83 | (ringparam->rx_jumbo_max_pending && | |
84 | (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO_MAX, | |
85 | ringparam->rx_jumbo_max_pending) || | |
86 | nla_put_u32(skb, ETHTOOL_A_RINGS_RX_JUMBO, | |
87 | ringparam->rx_jumbo_pending))) || | |
88 | (ringparam->tx_max_pending && | |
89 | (nla_put_u32(skb, ETHTOOL_A_RINGS_TX_MAX, | |
90 | ringparam->tx_max_pending) || | |
91 | nla_put_u32(skb, ETHTOOL_A_RINGS_TX, | |
0b70c256 | 92 | ringparam->tx_pending))) || |
9690ae60 JK |
93 | (kr->rx_buf_len && |
94 | (nla_put_u32(skb, ETHTOOL_A_RINGS_RX_BUF_LEN, kr->rx_buf_len))) || | |
95 | (kr->tcp_data_split && | |
96 | (nla_put_u8(skb, ETHTOOL_A_RINGS_TCP_DATA_SPLIT, | |
1241e329 SS |
97 | kr->tcp_data_split))) || |
98 | (kr->cqe_size && | |
4dc84c06 | 99 | (nla_put_u32(skb, ETHTOOL_A_RINGS_CQE_SIZE, kr->cqe_size))) || |
5b4e9a7a SN |
100 | nla_put_u8(skb, ETHTOOL_A_RINGS_TX_PUSH, !!kr->tx_push) || |
101 | nla_put_u8(skb, ETHTOOL_A_RINGS_RX_PUSH, !!kr->rx_push)) | |
e4a1717b MK |
102 | return -EMSGSIZE; |
103 | ||
104 | return 0; | |
105 | } | |
106 | ||
2fc2929e MK |
107 | /* RINGS_SET */ |
108 | ||
ff419afa | 109 | const struct nla_policy ethnl_rings_set_policy[] = { |
329d9c33 JK |
110 | [ETHTOOL_A_RINGS_HEADER] = |
111 | NLA_POLICY_NESTED(ethnl_header_policy), | |
2fc2929e MK |
112 | [ETHTOOL_A_RINGS_RX] = { .type = NLA_U32 }, |
113 | [ETHTOOL_A_RINGS_RX_MINI] = { .type = NLA_U32 }, | |
114 | [ETHTOOL_A_RINGS_RX_JUMBO] = { .type = NLA_U32 }, | |
115 | [ETHTOOL_A_RINGS_TX] = { .type = NLA_U32 }, | |
0b70c256 | 116 | [ETHTOOL_A_RINGS_RX_BUF_LEN] = NLA_POLICY_MIN(NLA_U32, 1), |
1241e329 | 117 | [ETHTOOL_A_RINGS_CQE_SIZE] = NLA_POLICY_MIN(NLA_U32, 1), |
4dc84c06 | 118 | [ETHTOOL_A_RINGS_TX_PUSH] = NLA_POLICY_MAX(NLA_U8, 1), |
5b4e9a7a | 119 | [ETHTOOL_A_RINGS_RX_PUSH] = NLA_POLICY_MAX(NLA_U8, 1), |
2fc2929e MK |
120 | }; |
121 | ||
04007961 JK |
122 | static int |
123 | ethnl_set_rings_validate(struct ethnl_req_info *req_info, | |
124 | struct genl_info *info) | |
2fc2929e | 125 | { |
04007961 | 126 | const struct ethtool_ops *ops = req_info->dev->ethtool_ops; |
5028588b | 127 | struct nlattr **tb = info->attrs; |
2fc2929e | 128 | |
bde292c0 JW |
129 | if (tb[ETHTOOL_A_RINGS_RX_BUF_LEN] && |
130 | !(ops->supported_ring_params & ETHTOOL_RING_USE_RX_BUF_LEN)) { | |
bde292c0 JW |
131 | NL_SET_ERR_MSG_ATTR(info->extack, |
132 | tb[ETHTOOL_A_RINGS_RX_BUF_LEN], | |
133 | "setting rx buf len not supported"); | |
04007961 | 134 | return -EOPNOTSUPP; |
bde292c0 JW |
135 | } |
136 | ||
137 | if (tb[ETHTOOL_A_RINGS_CQE_SIZE] && | |
138 | !(ops->supported_ring_params & ETHTOOL_RING_USE_CQE_SIZE)) { | |
bde292c0 JW |
139 | NL_SET_ERR_MSG_ATTR(info->extack, |
140 | tb[ETHTOOL_A_RINGS_CQE_SIZE], | |
141 | "setting cqe size not supported"); | |
04007961 | 142 | return -EOPNOTSUPP; |
bde292c0 JW |
143 | } |
144 | ||
4dc84c06 JW |
145 | if (tb[ETHTOOL_A_RINGS_TX_PUSH] && |
146 | !(ops->supported_ring_params & ETHTOOL_RING_USE_TX_PUSH)) { | |
4dc84c06 JW |
147 | NL_SET_ERR_MSG_ATTR(info->extack, |
148 | tb[ETHTOOL_A_RINGS_TX_PUSH], | |
149 | "setting tx push not supported"); | |
04007961 | 150 | return -EOPNOTSUPP; |
4dc84c06 JW |
151 | } |
152 | ||
5b4e9a7a SN |
153 | if (tb[ETHTOOL_A_RINGS_RX_PUSH] && |
154 | !(ops->supported_ring_params & ETHTOOL_RING_USE_RX_PUSH)) { | |
155 | NL_SET_ERR_MSG_ATTR(info->extack, | |
156 | tb[ETHTOOL_A_RINGS_RX_PUSH], | |
157 | "setting rx push not supported"); | |
158 | return -EOPNOTSUPP; | |
159 | } | |
160 | ||
04007961 JK |
161 | return ops->get_ringparam && ops->set_ringparam ? 1 : -EOPNOTSUPP; |
162 | } | |
163 | ||
164 | static int | |
165 | ethnl_set_rings(struct ethnl_req_info *req_info, struct genl_info *info) | |
166 | { | |
167 | struct kernel_ethtool_ringparam kernel_ringparam = {}; | |
168 | struct ethtool_ringparam ringparam = {}; | |
169 | struct net_device *dev = req_info->dev; | |
170 | struct nlattr **tb = info->attrs; | |
171 | const struct nlattr *err_attr; | |
172 | bool mod = false; | |
173 | int ret; | |
174 | ||
175 | dev->ethtool_ops->get_ringparam(dev, &ringparam, | |
176 | &kernel_ringparam, info->extack); | |
2fc2929e MK |
177 | |
178 | ethnl_update_u32(&ringparam.rx_pending, tb[ETHTOOL_A_RINGS_RX], &mod); | |
179 | ethnl_update_u32(&ringparam.rx_mini_pending, | |
180 | tb[ETHTOOL_A_RINGS_RX_MINI], &mod); | |
181 | ethnl_update_u32(&ringparam.rx_jumbo_pending, | |
182 | tb[ETHTOOL_A_RINGS_RX_JUMBO], &mod); | |
183 | ethnl_update_u32(&ringparam.tx_pending, tb[ETHTOOL_A_RINGS_TX], &mod); | |
0b70c256 HC |
184 | ethnl_update_u32(&kernel_ringparam.rx_buf_len, |
185 | tb[ETHTOOL_A_RINGS_RX_BUF_LEN], &mod); | |
1241e329 SS |
186 | ethnl_update_u32(&kernel_ringparam.cqe_size, |
187 | tb[ETHTOOL_A_RINGS_CQE_SIZE], &mod); | |
4dc84c06 JW |
188 | ethnl_update_u8(&kernel_ringparam.tx_push, |
189 | tb[ETHTOOL_A_RINGS_TX_PUSH], &mod); | |
5b4e9a7a SN |
190 | ethnl_update_u8(&kernel_ringparam.rx_push, |
191 | tb[ETHTOOL_A_RINGS_RX_PUSH], &mod); | |
2fc2929e | 192 | if (!mod) |
04007961 | 193 | return 0; |
2fc2929e MK |
194 | |
195 | /* ensure new ring parameters are within limits */ | |
196 | if (ringparam.rx_pending > ringparam.rx_max_pending) | |
197 | err_attr = tb[ETHTOOL_A_RINGS_RX]; | |
198 | else if (ringparam.rx_mini_pending > ringparam.rx_mini_max_pending) | |
199 | err_attr = tb[ETHTOOL_A_RINGS_RX_MINI]; | |
200 | else if (ringparam.rx_jumbo_pending > ringparam.rx_jumbo_max_pending) | |
201 | err_attr = tb[ETHTOOL_A_RINGS_RX_JUMBO]; | |
202 | else if (ringparam.tx_pending > ringparam.tx_max_pending) | |
203 | err_attr = tb[ETHTOOL_A_RINGS_TX]; | |
204 | else | |
205 | err_attr = NULL; | |
206 | if (err_attr) { | |
2fc2929e | 207 | NL_SET_ERR_MSG_ATTR(info->extack, err_attr, |
5ec82c49 | 208 | "requested ring size exceeds maximum"); |
04007961 | 209 | return -EINVAL; |
2fc2929e MK |
210 | } |
211 | ||
74624944 HC |
212 | ret = dev->ethtool_ops->set_ringparam(dev, &ringparam, |
213 | &kernel_ringparam, info->extack); | |
04007961 | 214 | return ret < 0 ? ret : 1; |
2fc2929e | 215 | } |
04007961 JK |
216 | |
217 | const struct ethnl_request_ops ethnl_rings_request_ops = { | |
218 | .request_cmd = ETHTOOL_MSG_RINGS_GET, | |
219 | .reply_cmd = ETHTOOL_MSG_RINGS_GET_REPLY, | |
220 | .hdr_attr = ETHTOOL_A_RINGS_HEADER, | |
221 | .req_info_size = sizeof(struct rings_req_info), | |
222 | .reply_data_size = sizeof(struct rings_reply_data), | |
223 | ||
224 | .prepare_data = rings_prepare_data, | |
225 | .reply_size = rings_reply_size, | |
226 | .fill_reply = rings_fill_reply, | |
227 | ||
228 | .set_validate = ethnl_set_rings_validate, | |
229 | .set = ethnl_set_rings, | |
230 | .set_ntf_cmd = ETHTOOL_MSG_RINGS_NTF, | |
231 | }; |