Merge tag 'nfs-for-4.17-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[linux-2.6-block.git] / net / core / ethtool.c
CommitLineData
1da177e4
LT
1/*
2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
4 *
5 * This file is where we call all the ethtool_ops commands to get
61a44b9c 6 * the information ethtool needs.
1da177e4 7 *
61a44b9c
MW
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
1da177e4
LT
12 */
13
14#include <linux/module.h>
15#include <linux/types.h>
4fc268d2 16#include <linux/capability.h>
1da177e4
LT
17#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
c8f3a8c3
RC
20#include <linux/net_tstamp.h>
21#include <linux/phy.h>
d17792eb 22#include <linux/bitops.h>
97f8aefb 23#include <linux/uaccess.h>
73da16c2 24#include <linux/vmalloc.h>
e679c9c1 25#include <linux/sfp.h>
5a0e3ad6 26#include <linux/slab.h>
68f512f2 27#include <linux/rtnetlink.h>
174cd4b1 28#include <linux/sched/signal.h>
960fb622 29#include <linux/net.h>
1da177e4 30
4ec93edb 31/*
1da177e4
LT
32 * Some useful ethtool_ops methods that're device independent.
33 * If we find that all drivers want to do the same thing here,
34 * we can turn these into dev_() function calls.
35 */
36
37u32 ethtool_op_get_link(struct net_device *dev)
38{
39 return netif_carrier_ok(dev) ? 1 : 0;
40}
97f8aefb 41EXPORT_SYMBOL(ethtool_op_get_link);
1da177e4 42
02eacbd0
RC
43int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
44{
45 info->so_timestamping =
46 SOF_TIMESTAMPING_TX_SOFTWARE |
47 SOF_TIMESTAMPING_RX_SOFTWARE |
48 SOF_TIMESTAMPING_SOFTWARE;
49 info->phc_index = -1;
50 return 0;
51}
52EXPORT_SYMBOL(ethtool_op_get_ts_info);
53
1da177e4
LT
54/* Handlers for each ethtool command */
55
475414f6 56#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
5455c699 57
9d921549
MM
58static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
59 [NETIF_F_SG_BIT] = "tx-scatter-gather",
60 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
9d921549
MM
61 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
62 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
63 [NETIF_F_HIGHDMA_BIT] = "highdma",
64 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
788dfcac 65 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
9d921549 66
788dfcac
FLVC
67 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
2d6577f1
DM
69 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
70 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
9d921549
MM
72 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
73 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
74 [NETIF_F_LLTX_BIT] = "tx-lockless",
75 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
76 [NETIF_F_GRO_BIT] = "rx-gro",
fb1f5f79 77 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
9d921549
MM
78 [NETIF_F_LRO_BIT] = "rx-lro",
79
80 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
9d921549
MM
81 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
82 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
cbc53e08 83 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
9d921549
MM
84 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
85 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
68c33163 86 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
518f213d 87 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
7e13318d
TH
88 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
89 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
73136267 90 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
518f213d 91 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
802ab55a 92 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
90017acc 93 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
c7ef8f0c 94 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
9d921549
MM
95
96 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
53692b1d 97 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
9d921549
MM
98 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
99 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
100 [NETIF_F_RXHASH_BIT] = "rx-hashing",
101 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
102 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
103 [NETIF_F_LOOPBACK_BIT] = "loopback",
36eabda3 104 [NETIF_F_RXFCS_BIT] = "rx-fcs",
5e0c03c8 105 [NETIF_F_RXALL_BIT] = "rx-all",
a6cc0cfa 106 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
1c78c64e 107 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
c7ef8f0c
SK
108 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
109 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
d764a122 110 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
e0be6bea 111 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
9d921549
MM
112};
113
892311f6
EP
114static const char
115rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
116 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
117 [ETH_RSS_HASH_XOR_BIT] = "xor",
abb521e3 118 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
892311f6
EP
119};
120
a4244b0c
HHZ
121static const char
122tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
123 [ETHTOOL_ID_UNSPEC] = "Unspec",
124 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
125 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
e1577c1c 126 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
a4244b0c
HHZ
127};
128
968ad9da
RL
129static const char
130phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
131 [ETHTOOL_ID_UNSPEC] = "Unspec",
65feddd5 132 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
968ad9da
RL
133};
134
5455c699
MM
135static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
136{
137 struct ethtool_gfeatures cmd = {
138 .cmd = ETHTOOL_GFEATURES,
139 .size = ETHTOOL_DEV_FEATURE_WORDS,
140 };
475414f6 141 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
5455c699
MM
142 u32 __user *sizeaddr;
143 u32 copy_size;
475414f6
MM
144 int i;
145
146 /* in case feature bits run out again */
09da71b1 147 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
475414f6
MM
148
149 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
150 features[i].available = (u32)(dev->hw_features >> (32 * i));
151 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
152 features[i].active = (u32)(dev->features >> (32 * i));
153 features[i].never_changed =
154 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
475414f6 155 }
5455c699
MM
156
157 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
158 if (get_user(copy_size, sizeaddr))
159 return -EFAULT;
160
161 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
162 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
163
164 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
165 return -EFAULT;
166 useraddr += sizeof(cmd);
167 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
168 return -EFAULT;
169
170 return 0;
171}
172
173static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
174{
175 struct ethtool_sfeatures cmd;
176 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
475414f6
MM
177 netdev_features_t wanted = 0, valid = 0;
178 int i, ret = 0;
5455c699
MM
179
180 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
181 return -EFAULT;
182 useraddr += sizeof(cmd);
183
184 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
185 return -EINVAL;
186
187 if (copy_from_user(features, useraddr, sizeof(features)))
188 return -EFAULT;
189
475414f6 190 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
09da71b1
MM
191 valid |= (netdev_features_t)features[i].valid << (32 * i);
192 wanted |= (netdev_features_t)features[i].requested << (32 * i);
475414f6
MM
193 }
194
195 if (valid & ~NETIF_F_ETHTOOL_BITS)
5455c699
MM
196 return -EINVAL;
197
475414f6
MM
198 if (valid & ~dev->hw_features) {
199 valid &= dev->hw_features;
5455c699
MM
200 ret |= ETHTOOL_F_UNSUPPORTED;
201 }
202
475414f6
MM
203 dev->wanted_features &= ~valid;
204 dev->wanted_features |= wanted & valid;
6cb6a27c 205 __netdev_update_features(dev);
5455c699 206
475414f6 207 if ((dev->wanted_features ^ dev->features) & valid)
5455c699
MM
208 ret |= ETHTOOL_F_WISH;
209
210 return ret;
211}
212
f3a40945
AL
213static int phy_get_sset_count(struct phy_device *phydev)
214{
215 int ret;
216
217 if (phydev->drv->get_sset_count &&
218 phydev->drv->get_strings &&
219 phydev->drv->get_stats) {
220 mutex_lock(&phydev->lock);
221 ret = phydev->drv->get_sset_count(phydev);
222 mutex_unlock(&phydev->lock);
223
224 return ret;
225 }
226
227 return -EOPNOTSUPP;
228}
229
340ae165
MM
230static int __ethtool_get_sset_count(struct net_device *dev, int sset)
231{
232 const struct ethtool_ops *ops = dev->ethtool_ops;
233
5455c699
MM
234 if (sset == ETH_SS_FEATURES)
235 return ARRAY_SIZE(netdev_features_strings);
236
892311f6
EP
237 if (sset == ETH_SS_RSS_HASH_FUNCS)
238 return ARRAY_SIZE(rss_hash_func_strings);
239
a4244b0c
HHZ
240 if (sset == ETH_SS_TUNABLES)
241 return ARRAY_SIZE(tunable_strings);
242
968ad9da
RL
243 if (sset == ETH_SS_PHY_TUNABLES)
244 return ARRAY_SIZE(phy_tunable_strings);
245
f3a40945
AL
246 if (sset == ETH_SS_PHY_STATS) {
247 if (dev->phydev)
248 return phy_get_sset_count(dev->phydev);
249 else
250 return -EOPNOTSUPP;
251 }
252
c03a14e8 253 if (ops->get_sset_count && ops->get_strings)
340ae165
MM
254 return ops->get_sset_count(dev, sset);
255 else
256 return -EOPNOTSUPP;
257}
258
259static void __ethtool_get_strings(struct net_device *dev,
260 u32 stringset, u8 *data)
261{
262 const struct ethtool_ops *ops = dev->ethtool_ops;
263
5455c699
MM
264 if (stringset == ETH_SS_FEATURES)
265 memcpy(data, netdev_features_strings,
266 sizeof(netdev_features_strings));
892311f6
EP
267 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
268 memcpy(data, rss_hash_func_strings,
269 sizeof(rss_hash_func_strings));
a4244b0c
HHZ
270 else if (stringset == ETH_SS_TUNABLES)
271 memcpy(data, tunable_strings, sizeof(tunable_strings));
968ad9da
RL
272 else if (stringset == ETH_SS_PHY_TUNABLES)
273 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
f3a40945
AL
274 else if (stringset == ETH_SS_PHY_STATS) {
275 struct phy_device *phydev = dev->phydev;
276
277 if (phydev) {
278 mutex_lock(&phydev->lock);
279 phydev->drv->get_strings(phydev, data);
280 mutex_unlock(&phydev->lock);
281 } else {
282 return;
283 }
284 } else
5455c699
MM
285 /* ops->get_strings is valid because checked earlier */
286 ops->get_strings(dev, stringset, data);
340ae165
MM
287}
288
c8f44aff 289static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
0a417704
MM
290{
291 /* feature masks of legacy discrete ethtool ops */
292
293 switch (eth_cmd) {
294 case ETHTOOL_GTXCSUM:
295 case ETHTOOL_STXCSUM:
a188222b 296 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
e83d360d
MM
297 case ETHTOOL_GRXCSUM:
298 case ETHTOOL_SRXCSUM:
299 return NETIF_F_RXCSUM;
0a417704
MM
300 case ETHTOOL_GSG:
301 case ETHTOOL_SSG:
302 return NETIF_F_SG;
303 case ETHTOOL_GTSO:
304 case ETHTOOL_STSO:
305 return NETIF_F_ALL_TSO;
0a417704
MM
306 case ETHTOOL_GGSO:
307 case ETHTOOL_SGSO:
308 return NETIF_F_GSO;
309 case ETHTOOL_GGRO:
310 case ETHTOOL_SGRO:
311 return NETIF_F_GRO;
312 default:
313 BUG();
314 }
315}
316
0a417704
MM
317static int ethtool_get_one_feature(struct net_device *dev,
318 char __user *useraddr, u32 ethcmd)
319{
c8f44aff 320 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
0a417704
MM
321 struct ethtool_value edata = {
322 .cmd = ethcmd,
86794881 323 .data = !!(dev->features & mask),
0a417704 324 };
0a417704 325
0a417704
MM
326 if (copy_to_user(useraddr, &edata, sizeof(edata)))
327 return -EFAULT;
328 return 0;
329}
330
0a417704
MM
331static int ethtool_set_one_feature(struct net_device *dev,
332 void __user *useraddr, u32 ethcmd)
333{
334 struct ethtool_value edata;
c8f44aff 335 netdev_features_t mask;
0a417704
MM
336
337 if (copy_from_user(&edata, useraddr, sizeof(edata)))
338 return -EFAULT;
339
86794881
MM
340 mask = ethtool_get_feature_mask(ethcmd);
341 mask &= dev->hw_features;
bc5787c6
MM
342 if (!mask)
343 return -EOPNOTSUPP;
86794881 344
bc5787c6
MM
345 if (edata.data)
346 dev->wanted_features |= mask;
347 else
348 dev->wanted_features &= ~mask;
86794881 349
bc5787c6 350 __netdev_update_features(dev);
86794881 351
bc5787c6
MM
352 return 0;
353}
354
02b3a552
MM
355#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
356 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
f646968f
PM
357#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
358 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
359 NETIF_F_RXHASH)
bc5787c6
MM
360
361static u32 __ethtool_get_flags(struct net_device *dev)
362{
02b3a552
MM
363 u32 flags = 0;
364
8a73125c
DF
365 if (dev->features & NETIF_F_LRO)
366 flags |= ETH_FLAG_LRO;
367 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
368 flags |= ETH_FLAG_RXVLAN;
369 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
370 flags |= ETH_FLAG_TXVLAN;
371 if (dev->features & NETIF_F_NTUPLE)
372 flags |= ETH_FLAG_NTUPLE;
373 if (dev->features & NETIF_F_RXHASH)
374 flags |= ETH_FLAG_RXHASH;
02b3a552
MM
375
376 return flags;
0a417704
MM
377}
378
bc5787c6 379static int __ethtool_set_flags(struct net_device *dev, u32 data)
da8ac86c 380{
c8f44aff 381 netdev_features_t features = 0, changed;
da8ac86c 382
02b3a552 383 if (data & ~ETH_ALL_FLAGS)
da8ac86c
MM
384 return -EINVAL;
385
8a73125c
DF
386 if (data & ETH_FLAG_LRO)
387 features |= NETIF_F_LRO;
388 if (data & ETH_FLAG_RXVLAN)
389 features |= NETIF_F_HW_VLAN_CTAG_RX;
390 if (data & ETH_FLAG_TXVLAN)
391 features |= NETIF_F_HW_VLAN_CTAG_TX;
392 if (data & ETH_FLAG_NTUPLE)
393 features |= NETIF_F_NTUPLE;
394 if (data & ETH_FLAG_RXHASH)
395 features |= NETIF_F_RXHASH;
02b3a552 396
da8ac86c 397 /* allow changing only bits set in hw_features */
02b3a552 398 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
da8ac86c
MM
399 if (changed & ~dev->hw_features)
400 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
401
402 dev->wanted_features =
02b3a552 403 (dev->wanted_features & ~changed) | (features & changed);
da8ac86c 404
6cb6a27c 405 __netdev_update_features(dev);
da8ac86c
MM
406
407 return 0;
408}
409
5a6cd6de
AB
410/* Given two link masks, AND them together and save the result in dst. */
411void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
412 struct ethtool_link_ksettings *src)
413{
414 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
415 unsigned int idx = 0;
416
417 for (; idx < size; idx++) {
418 dst->link_modes.supported[idx] &=
419 src->link_modes.supported[idx];
420 dst->link_modes.advertising[idx] &=
421 src->link_modes.advertising[idx];
422 }
423}
424EXPORT_SYMBOL(ethtool_intersect_link_masks);
425
6d62b4d5
PR
426void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
427 u32 legacy_u32)
3f1ac7a7
DD
428{
429 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
430 dst[0] = legacy_u32;
431}
6d62b4d5 432EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
3f1ac7a7
DD
433
434/* return false if src had higher bits set. lower bits always updated. */
6d62b4d5
PR
435bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
436 const unsigned long *src)
3f1ac7a7
DD
437{
438 bool retval = true;
439
440 /* TODO: following test will soon always be true */
441 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
442 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
443
444 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
445 bitmap_fill(ext, 32);
446 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
447 if (bitmap_intersects(ext, src,
448 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
449 /* src mask goes beyond bit 31 */
450 retval = false;
451 }
452 }
453 *legacy_u32 = src[0];
454 return retval;
455}
6d62b4d5 456EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
3f1ac7a7
DD
457
458/* return false if legacy contained non-0 deprecated fields
95491e3c 459 * maxtxpkt/maxrxpkt. rest of ksettings always updated
3f1ac7a7
DD
460 */
461static bool
462convert_legacy_settings_to_link_ksettings(
463 struct ethtool_link_ksettings *link_ksettings,
464 const struct ethtool_cmd *legacy_settings)
465{
466 bool retval = true;
467
468 memset(link_ksettings, 0, sizeof(*link_ksettings));
469
470 /* This is used to tell users that driver is still using these
471 * deprecated legacy fields, and they should not use
472 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
473 */
95491e3c 474 if (legacy_settings->maxtxpkt ||
3f1ac7a7
DD
475 legacy_settings->maxrxpkt)
476 retval = false;
477
6d62b4d5 478 ethtool_convert_legacy_u32_to_link_mode(
3f1ac7a7
DD
479 link_ksettings->link_modes.supported,
480 legacy_settings->supported);
6d62b4d5 481 ethtool_convert_legacy_u32_to_link_mode(
3f1ac7a7
DD
482 link_ksettings->link_modes.advertising,
483 legacy_settings->advertising);
6d62b4d5 484 ethtool_convert_legacy_u32_to_link_mode(
3f1ac7a7
DD
485 link_ksettings->link_modes.lp_advertising,
486 legacy_settings->lp_advertising);
487 link_ksettings->base.speed
488 = ethtool_cmd_speed(legacy_settings);
489 link_ksettings->base.duplex
490 = legacy_settings->duplex;
491 link_ksettings->base.port
492 = legacy_settings->port;
493 link_ksettings->base.phy_address
494 = legacy_settings->phy_address;
495 link_ksettings->base.autoneg
496 = legacy_settings->autoneg;
497 link_ksettings->base.mdio_support
498 = legacy_settings->mdio_support;
499 link_ksettings->base.eth_tp_mdix
500 = legacy_settings->eth_tp_mdix;
501 link_ksettings->base.eth_tp_mdix_ctrl
502 = legacy_settings->eth_tp_mdix_ctrl;
503 return retval;
504}
505
506/* return false if ksettings link modes had higher bits
507 * set. legacy_settings always updated (best effort)
508 */
509static bool
510convert_link_ksettings_to_legacy_settings(
511 struct ethtool_cmd *legacy_settings,
512 const struct ethtool_link_ksettings *link_ksettings)
513{
514 bool retval = true;
515
516 memset(legacy_settings, 0, sizeof(*legacy_settings));
517 /* this also clears the deprecated fields in legacy structure:
518 * __u8 transceiver;
519 * __u32 maxtxpkt;
520 * __u32 maxrxpkt;
521 */
522
6d62b4d5 523 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
524 &legacy_settings->supported,
525 link_ksettings->link_modes.supported);
6d62b4d5 526 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
527 &legacy_settings->advertising,
528 link_ksettings->link_modes.advertising);
6d62b4d5 529 retval &= ethtool_convert_link_mode_to_legacy_u32(
3f1ac7a7
DD
530 &legacy_settings->lp_advertising,
531 link_ksettings->link_modes.lp_advertising);
532 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
533 legacy_settings->duplex
534 = link_ksettings->base.duplex;
535 legacy_settings->port
536 = link_ksettings->base.port;
537 legacy_settings->phy_address
538 = link_ksettings->base.phy_address;
539 legacy_settings->autoneg
540 = link_ksettings->base.autoneg;
541 legacy_settings->mdio_support
542 = link_ksettings->base.mdio_support;
543 legacy_settings->eth_tp_mdix
544 = link_ksettings->base.eth_tp_mdix;
545 legacy_settings->eth_tp_mdix_ctrl
546 = link_ksettings->base.eth_tp_mdix_ctrl;
19cab887
FF
547 legacy_settings->transceiver
548 = link_ksettings->base.transceiver;
3f1ac7a7
DD
549 return retval;
550}
551
552/* number of 32-bit words to store the user's link mode bitmaps */
553#define __ETHTOOL_LINK_MODE_MASK_NU32 \
554 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
555
556/* layout of the struct passed from/to userland */
557struct ethtool_link_usettings {
558 struct ethtool_link_settings base;
559 struct {
560 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
561 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
562 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
563 } link_modes;
564};
565
566/* Internal kernel helper to query a device ethtool_link_settings.
567 *
568 * Backward compatibility note: for compatibility with legacy drivers
569 * that implement only the ethtool_cmd API, this has to work with both
570 * drivers implementing get_link_ksettings API and drivers
571 * implementing get_settings API. When drivers implement get_settings
572 * and report ethtool_cmd deprecated fields
573 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
574 * because the resulting struct ethtool_link_settings does not report them.
575 */
576int __ethtool_get_link_ksettings(struct net_device *dev,
577 struct ethtool_link_ksettings *link_ksettings)
578{
579 int err;
580 struct ethtool_cmd cmd;
581
582 ASSERT_RTNL();
583
584 if (dev->ethtool_ops->get_link_ksettings) {
585 memset(link_ksettings, 0, sizeof(*link_ksettings));
586 return dev->ethtool_ops->get_link_ksettings(dev,
587 link_ksettings);
588 }
589
590 /* driver doesn't support %ethtool_link_ksettings API. revert to
591 * legacy %ethtool_cmd API, unless it's not supported either.
592 * TODO: remove when ethtool_ops::get_settings disappears internally
593 */
3237fc63
DD
594 if (!dev->ethtool_ops->get_settings)
595 return -EOPNOTSUPP;
596
597 memset(&cmd, 0, sizeof(cmd));
598 cmd.cmd = ETHTOOL_GSET;
599 err = dev->ethtool_ops->get_settings(dev, &cmd);
3f1ac7a7
DD
600 if (err < 0)
601 return err;
602
603 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
604 */
605 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
606 return err;
607}
608EXPORT_SYMBOL(__ethtool_get_link_ksettings);
609
610/* convert ethtool_link_usettings in user space to a kernel internal
611 * ethtool_link_ksettings. return 0 on success, errno on error.
612 */
613static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
614 const void __user *from)
615{
616 struct ethtool_link_usettings link_usettings;
617
618 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
619 return -EFAULT;
620
621 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
3aa56885
YN
622 bitmap_from_arr32(to->link_modes.supported,
623 link_usettings.link_modes.supported,
624 __ETHTOOL_LINK_MODE_MASK_NBITS);
625 bitmap_from_arr32(to->link_modes.advertising,
626 link_usettings.link_modes.advertising,
627 __ETHTOOL_LINK_MODE_MASK_NBITS);
628 bitmap_from_arr32(to->link_modes.lp_advertising,
629 link_usettings.link_modes.lp_advertising,
630 __ETHTOOL_LINK_MODE_MASK_NBITS);
3f1ac7a7
DD
631
632 return 0;
633}
634
635/* convert a kernel internal ethtool_link_ksettings to
636 * ethtool_link_usettings in user space. return 0 on success, errno on
637 * error.
638 */
639static int
640store_link_ksettings_for_user(void __user *to,
641 const struct ethtool_link_ksettings *from)
642{
643 struct ethtool_link_usettings link_usettings;
644
645 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
3aa56885
YN
646 bitmap_to_arr32(link_usettings.link_modes.supported,
647 from->link_modes.supported,
648 __ETHTOOL_LINK_MODE_MASK_NBITS);
649 bitmap_to_arr32(link_usettings.link_modes.advertising,
650 from->link_modes.advertising,
651 __ETHTOOL_LINK_MODE_MASK_NBITS);
652 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
653 from->link_modes.lp_advertising,
654 __ETHTOOL_LINK_MODE_MASK_NBITS);
3f1ac7a7
DD
655
656 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
657 return -EFAULT;
658
659 return 0;
660}
661
662/* Query device for its ethtool_link_settings.
663 *
664 * Backward compatibility note: this function must fail when driver
665 * does not implement ethtool::get_link_ksettings, even if legacy
666 * ethtool_ops::get_settings is implemented. This tells new versions
667 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
668 * this driver, so that they can correctly access the ethtool_cmd
669 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
670 * implements ethtool_ops::get_settings anymore.
671 */
672static int ethtool_get_link_ksettings(struct net_device *dev,
673 void __user *useraddr)
674{
675 int err = 0;
676 struct ethtool_link_ksettings link_ksettings;
677
678 ASSERT_RTNL();
679
680 if (!dev->ethtool_ops->get_link_ksettings)
681 return -EOPNOTSUPP;
682
683 /* handle bitmap nbits handshake */
684 if (copy_from_user(&link_ksettings.base, useraddr,
685 sizeof(link_ksettings.base)))
686 return -EFAULT;
687
688 if (__ETHTOOL_LINK_MODE_MASK_NU32
689 != link_ksettings.base.link_mode_masks_nwords) {
690 /* wrong link mode nbits requested */
691 memset(&link_ksettings, 0, sizeof(link_ksettings));
793cf87d 692 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
3f1ac7a7
DD
693 /* send back number of words required as negative val */
694 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
695 "need too many bits for link modes!");
696 link_ksettings.base.link_mode_masks_nwords
697 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
698
699 /* copy the base fields back to user, not the link
700 * mode bitmaps
701 */
702 if (copy_to_user(useraddr, &link_ksettings.base,
703 sizeof(link_ksettings.base)))
704 return -EFAULT;
705
706 return 0;
707 }
708
709 /* handshake successful: user/kernel agree on
710 * link_mode_masks_nwords
711 */
712
713 memset(&link_ksettings, 0, sizeof(link_ksettings));
714 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
715 if (err < 0)
716 return err;
717
718 /* make sure we tell the right values to user */
719 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
720 link_ksettings.base.link_mode_masks_nwords
721 = __ETHTOOL_LINK_MODE_MASK_NU32;
722
723 return store_link_ksettings_for_user(useraddr, &link_ksettings);
724}
725
726/* Update device ethtool_link_settings.
727 *
728 * Backward compatibility note: this function must fail when driver
729 * does not implement ethtool::set_link_ksettings, even if legacy
730 * ethtool_ops::set_settings is implemented. This tells new versions
731 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
732 * this driver, so that they can correctly update the ethtool_cmd
733 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
734 * implements ethtool_ops::get_settings anymore.
735 */
736static int ethtool_set_link_ksettings(struct net_device *dev,
737 void __user *useraddr)
738{
739 int err;
740 struct ethtool_link_ksettings link_ksettings;
741
742 ASSERT_RTNL();
743
744 if (!dev->ethtool_ops->set_link_ksettings)
745 return -EOPNOTSUPP;
746
747 /* make sure nbits field has expected value */
748 if (copy_from_user(&link_ksettings.base, useraddr,
749 sizeof(link_ksettings.base)))
750 return -EFAULT;
751
752 if (__ETHTOOL_LINK_MODE_MASK_NU32
753 != link_ksettings.base.link_mode_masks_nwords)
754 return -EINVAL;
755
756 /* copy the whole structure, now that we know it has expected
757 * format
758 */
759 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
760 if (err)
761 return err;
762
763 /* re-check nwords field, just in case */
764 if (__ETHTOOL_LINK_MODE_MASK_NU32
765 != link_ksettings.base.link_mode_masks_nwords)
766 return -EINVAL;
767
768 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
769}
770
3f1ac7a7
DD
771/* Query device for its ethtool_cmd settings.
772 *
773 * Backward compatibility note: for compatibility with legacy ethtool,
774 * this has to work with both drivers implementing get_link_ksettings
775 * API and drivers implementing get_settings API. When drivers
776 * implement get_link_ksettings and report higher link mode bits, a
777 * kernel warning is logged once (with name of 1st driver/device) to
778 * recommend user to upgrade ethtool, but the command is successful
779 * (only the lower link mode bits reported back to user).
780 */
4bc71cb9
JP
781static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
782{
4bc71cb9
JP
783 struct ethtool_cmd cmd;
784
3f1ac7a7
DD
785 ASSERT_RTNL();
786
787 if (dev->ethtool_ops->get_link_ksettings) {
788 /* First, use link_ksettings API if it is supported */
789 int err;
790 struct ethtool_link_ksettings link_ksettings;
791
792 memset(&link_ksettings, 0, sizeof(link_ksettings));
793 err = dev->ethtool_ops->get_link_ksettings(dev,
794 &link_ksettings);
795 if (err < 0)
796 return err;
71891e2d
SH
797 convert_link_ksettings_to_legacy_settings(&cmd,
798 &link_ksettings);
3f1ac7a7
DD
799
800 /* send a sensible cmd tag back to user */
801 cmd.cmd = ETHTOOL_GSET;
802 } else {
3f1ac7a7
DD
803 /* driver doesn't support %ethtool_link_ksettings
804 * API. revert to legacy %ethtool_cmd API, unless it's
805 * not supported either.
806 */
3237fc63
DD
807 int err;
808
809 if (!dev->ethtool_ops->get_settings)
810 return -EOPNOTSUPP;
811
812 memset(&cmd, 0, sizeof(cmd));
813 cmd.cmd = ETHTOOL_GSET;
814 err = dev->ethtool_ops->get_settings(dev, &cmd);
3f1ac7a7
DD
815 if (err < 0)
816 return err;
817 }
1da177e4
LT
818
819 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
820 return -EFAULT;
3f1ac7a7 821
1da177e4
LT
822 return 0;
823}
824
3f1ac7a7
DD
825/* Update device link settings with given ethtool_cmd.
826 *
827 * Backward compatibility note: for compatibility with legacy ethtool,
828 * this has to work with both drivers implementing set_link_ksettings
829 * API and drivers implementing set_settings API. When drivers
830 * implement set_link_ksettings and user's request updates deprecated
831 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
832 * warning is logged once (with name of 1st driver/device) to
833 * recommend user to upgrade ethtool, and the request is rejected.
834 */
1da177e4
LT
835static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
836{
837 struct ethtool_cmd cmd;
838
3f1ac7a7 839 ASSERT_RTNL();
1da177e4
LT
840
841 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
842 return -EFAULT;
843
3f1ac7a7
DD
844 /* first, try new %ethtool_link_ksettings API. */
845 if (dev->ethtool_ops->set_link_ksettings) {
846 struct ethtool_link_ksettings link_ksettings;
847
848 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
849 &cmd))
850 return -EINVAL;
851
852 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
853 link_ksettings.base.link_mode_masks_nwords
854 = __ETHTOOL_LINK_MODE_MASK_NU32;
855 return dev->ethtool_ops->set_link_ksettings(dev,
856 &link_ksettings);
857 }
858
859 /* legacy %ethtool_cmd API */
860
861 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
862 * disappears internally
863 */
864
865 if (!dev->ethtool_ops->set_settings)
866 return -EOPNOTSUPP;
867
1da177e4
LT
868 return dev->ethtool_ops->set_settings(dev, &cmd);
869}
870
97f8aefb 871static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
872 void __user *useraddr)
1da177e4
LT
873{
874 struct ethtool_drvinfo info;
76fd8593 875 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 876
1da177e4
LT
877 memset(&info, 0, sizeof(info));
878 info.cmd = ETHTOOL_GDRVINFO;
c03a14e8 879 if (ops->get_drvinfo) {
01414802
BH
880 ops->get_drvinfo(dev, &info);
881 } else if (dev->dev.parent && dev->dev.parent->driver) {
882 strlcpy(info.bus_info, dev_name(dev->dev.parent),
883 sizeof(info.bus_info));
884 strlcpy(info.driver, dev->dev.parent->driver->name,
885 sizeof(info.driver));
886 } else {
887 return -EOPNOTSUPP;
888 }
1da177e4 889
723b2f57
JG
890 /*
891 * this method of obtaining string set info is deprecated;
d17792eb 892 * Use ETHTOOL_GSSET_INFO instead.
723b2f57 893 */
c03a14e8 894 if (ops->get_sset_count) {
ff03d49f
JG
895 int rc;
896
897 rc = ops->get_sset_count(dev, ETH_SS_TEST);
898 if (rc >= 0)
899 info.testinfo_len = rc;
900 rc = ops->get_sset_count(dev, ETH_SS_STATS);
901 if (rc >= 0)
902 info.n_stats = rc;
339bf024
JG
903 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
904 if (rc >= 0)
905 info.n_priv_flags = rc;
ff03d49f 906 }
c03a14e8 907 if (ops->get_regs_len)
1da177e4 908 info.regdump_len = ops->get_regs_len(dev);
c03a14e8 909 if (ops->get_eeprom_len)
1da177e4
LT
910 info.eedump_len = ops->get_eeprom_len(dev);
911
912 if (copy_to_user(useraddr, &info, sizeof(info)))
913 return -EFAULT;
914 return 0;
915}
916
f5c445ed 917static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
97f8aefb 918 void __user *useraddr)
723b2f57
JG
919{
920 struct ethtool_sset_info info;
723b2f57
JG
921 u64 sset_mask;
922 int i, idx = 0, n_bits = 0, ret, rc;
923 u32 *info_buf = NULL;
924
723b2f57
JG
925 if (copy_from_user(&info, useraddr, sizeof(info)))
926 return -EFAULT;
927
928 /* store copy of mask, because we zero struct later on */
929 sset_mask = info.sset_mask;
930 if (!sset_mask)
931 return 0;
932
933 /* calculate size of return buffer */
d17792eb 934 n_bits = hweight64(sset_mask);
723b2f57
JG
935
936 memset(&info, 0, sizeof(info));
937 info.cmd = ETHTOOL_GSSET_INFO;
938
939 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
940 if (!info_buf)
941 return -ENOMEM;
942
943 /*
944 * fill return buffer based on input bitmask and successful
945 * get_sset_count return
946 */
947 for (i = 0; i < 64; i++) {
948 if (!(sset_mask & (1ULL << i)))
949 continue;
950
340ae165 951 rc = __ethtool_get_sset_count(dev, i);
723b2f57
JG
952 if (rc >= 0) {
953 info.sset_mask |= (1ULL << i);
954 info_buf[idx++] = rc;
955 }
956 }
957
958 ret = -EFAULT;
959 if (copy_to_user(useraddr, &info, sizeof(info)))
960 goto out;
961
962 useraddr += offsetof(struct ethtool_sset_info, data);
963 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
964 goto out;
965
966 ret = 0;
967
968out:
969 kfree(info_buf);
970 return ret;
971}
972
97f8aefb 973static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
bf988435 974 u32 cmd, void __user *useraddr)
0853ad66 975{
bf988435
BH
976 struct ethtool_rxnfc info;
977 size_t info_size = sizeof(info);
55664f32 978 int rc;
0853ad66 979
59089d8d 980 if (!dev->ethtool_ops->set_rxnfc)
0853ad66
SB
981 return -EOPNOTSUPP;
982
bf988435
BH
983 /* struct ethtool_rxnfc was originally defined for
984 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
985 * members. User-space might still be using that
986 * definition. */
987 if (cmd == ETHTOOL_SRXFH)
988 info_size = (offsetof(struct ethtool_rxnfc, data) +
989 sizeof(info.data));
990
991 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
992 return -EFAULT;
993
55664f32
BH
994 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
995 if (rc)
996 return rc;
997
998 if (cmd == ETHTOOL_SRXCLSRLINS &&
999 copy_to_user(useraddr, &info, info_size))
1000 return -EFAULT;
1001
1002 return 0;
0853ad66
SB
1003}
1004
97f8aefb 1005static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
bf988435 1006 u32 cmd, void __user *useraddr)
0853ad66
SB
1007{
1008 struct ethtool_rxnfc info;
bf988435 1009 size_t info_size = sizeof(info);
59089d8d
SB
1010 const struct ethtool_ops *ops = dev->ethtool_ops;
1011 int ret;
1012 void *rule_buf = NULL;
0853ad66 1013
59089d8d 1014 if (!ops->get_rxnfc)
0853ad66
SB
1015 return -EOPNOTSUPP;
1016
bf988435
BH
1017 /* struct ethtool_rxnfc was originally defined for
1018 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1019 * members. User-space might still be using that
1020 * definition. */
1021 if (cmd == ETHTOOL_GRXFH)
1022 info_size = (offsetof(struct ethtool_rxnfc, data) +
1023 sizeof(info.data));
1024
1025 if (copy_from_user(&info, useraddr, info_size))
0853ad66
SB
1026 return -EFAULT;
1027
84a1d9c4
EC
1028 /* If FLOW_RSS was requested then user-space must be using the
1029 * new definition, as FLOW_RSS is newer.
1030 */
1031 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1032 info_size = sizeof(info);
1033 if (copy_from_user(&info, useraddr, info_size))
1034 return -EFAULT;
1035 }
1036
59089d8d
SB
1037 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1038 if (info.rule_cnt > 0) {
db048b69 1039 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
ae6df5f9 1040 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
db048b69 1041 GFP_USER);
59089d8d
SB
1042 if (!rule_buf)
1043 return -ENOMEM;
1044 }
1045 }
0853ad66 1046
59089d8d
SB
1047 ret = ops->get_rxnfc(dev, &info, rule_buf);
1048 if (ret < 0)
1049 goto err_out;
1050
1051 ret = -EFAULT;
bf988435 1052 if (copy_to_user(useraddr, &info, info_size))
59089d8d
SB
1053 goto err_out;
1054
1055 if (rule_buf) {
1056 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1057 if (copy_to_user(useraddr, rule_buf,
1058 info.rule_cnt * sizeof(u32)))
1059 goto err_out;
1060 }
1061 ret = 0;
1062
1063err_out:
c9caceca 1064 kfree(rule_buf);
59089d8d
SB
1065
1066 return ret;
0853ad66
SB
1067}
1068
3de0b592
VD
1069static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1070 struct ethtool_rxnfc *rx_rings,
1071 u32 size)
1072{
fb95cd8d 1073 int i;
3de0b592
VD
1074
1075 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
fb95cd8d 1076 return -EFAULT;
3de0b592
VD
1077
1078 /* Validate ring indices */
fb95cd8d
BH
1079 for (i = 0; i < size; i++)
1080 if (indir[i] >= rx_rings->data)
1081 return -EINVAL;
1082
1083 return 0;
3de0b592
VD
1084}
1085
ba905f5e 1086u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
960fb622
ED
1087
1088void netdev_rss_key_fill(void *buffer, size_t len)
1089{
1090 BUG_ON(len > sizeof(netdev_rss_key));
1091 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1092 memcpy(buffer, netdev_rss_key, len);
1093}
1094EXPORT_SYMBOL(netdev_rss_key_fill);
1095
d4ab4286
KJ
1096static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1097{
1098 u32 dev_size, current_max = 0;
1099 u32 *indir;
1100 int ret;
1101
1102 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1103 !dev->ethtool_ops->get_rxfh)
1104 return -EOPNOTSUPP;
1105 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1106 if (dev_size == 0)
1107 return -EOPNOTSUPP;
1108
1109 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1110 if (!indir)
1111 return -ENOMEM;
1112
1113 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1114 if (ret)
1115 goto out;
1116
1117 while (dev_size--)
1118 current_max = max(current_max, indir[dev_size]);
1119
1120 *max = current_max;
1121
1122out:
1123 kfree(indir);
1124 return ret;
1125}
1126
a5b6ee29
BH
1127static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1128 void __user *useraddr)
1129{
7850f63f
BH
1130 u32 user_size, dev_size;
1131 u32 *indir;
a5b6ee29
BH
1132 int ret;
1133
7850f63f 1134 if (!dev->ethtool_ops->get_rxfh_indir_size ||
fe62d001 1135 !dev->ethtool_ops->get_rxfh)
7850f63f
BH
1136 return -EOPNOTSUPP;
1137 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1138 if (dev_size == 0)
a5b6ee29
BH
1139 return -EOPNOTSUPP;
1140
7850f63f 1141 if (copy_from_user(&user_size,
a5b6ee29 1142 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 1143 sizeof(user_size)))
a5b6ee29
BH
1144 return -EFAULT;
1145
7850f63f
BH
1146 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1147 &dev_size, sizeof(dev_size)))
1148 return -EFAULT;
1149
1150 /* If the user buffer size is 0, this is just a query for the
1151 * device table size. Otherwise, if it's smaller than the
1152 * device table size it's an error.
1153 */
1154 if (user_size < dev_size)
1155 return user_size == 0 ? 0 : -EINVAL;
1156
1157 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
1158 if (!indir)
1159 return -ENOMEM;
1160
892311f6 1161 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
a5b6ee29
BH
1162 if (ret)
1163 goto out;
1164
7850f63f
BH
1165 if (copy_to_user(useraddr +
1166 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1167 indir, dev_size * sizeof(indir[0])))
a5b6ee29
BH
1168 ret = -EFAULT;
1169
1170out:
1171 kfree(indir);
1172 return ret;
1173}
1174
1175static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1176 void __user *useraddr)
1177{
7850f63f
BH
1178 struct ethtool_rxnfc rx_rings;
1179 u32 user_size, dev_size, i;
1180 u32 *indir;
c03a14e8 1181 const struct ethtool_ops *ops = dev->ethtool_ops;
a5b6ee29 1182 int ret;
3de0b592 1183 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
a5b6ee29 1184
fe62d001 1185 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
c03a14e8 1186 !ops->get_rxnfc)
7850f63f 1187 return -EOPNOTSUPP;
c03a14e8
JP
1188
1189 dev_size = ops->get_rxfh_indir_size(dev);
7850f63f 1190 if (dev_size == 0)
a5b6ee29
BH
1191 return -EOPNOTSUPP;
1192
7850f63f 1193 if (copy_from_user(&user_size,
a5b6ee29 1194 useraddr + offsetof(struct ethtool_rxfh_indir, size),
7850f63f 1195 sizeof(user_size)))
a5b6ee29
BH
1196 return -EFAULT;
1197
278bc429 1198 if (user_size != 0 && user_size != dev_size)
7850f63f
BH
1199 return -EINVAL;
1200
1201 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
a5b6ee29
BH
1202 if (!indir)
1203 return -ENOMEM;
1204
7850f63f 1205 rx_rings.cmd = ETHTOOL_GRXRINGS;
c03a14e8 1206 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
7850f63f
BH
1207 if (ret)
1208 goto out;
278bc429
BH
1209
1210 if (user_size == 0) {
1211 for (i = 0; i < dev_size; i++)
1212 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1213 } else {
3de0b592
VD
1214 ret = ethtool_copy_validate_indir(indir,
1215 useraddr + ringidx_offset,
1216 &rx_rings,
1217 dev_size);
1218 if (ret)
1219 goto out;
1220 }
1221
892311f6 1222 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
d4ab4286
KJ
1223 if (ret)
1224 goto out;
1225
1226 /* indicate whether rxfh was set to default */
1227 if (user_size == 0)
1228 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1229 else
1230 dev->priv_flags |= IFF_RXFH_CONFIGURED;
3de0b592
VD
1231
1232out:
1233 kfree(indir);
1234 return ret;
1235}
1236
1237static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1238 void __user *useraddr)
1239{
1240 int ret;
1241 const struct ethtool_ops *ops = dev->ethtool_ops;
f062a384 1242 u32 user_indir_size, user_key_size;
3de0b592 1243 u32 dev_indir_size = 0, dev_key_size = 0;
f062a384 1244 struct ethtool_rxfh rxfh;
3de0b592 1245 u32 total_size;
f062a384 1246 u32 indir_bytes;
3de0b592 1247 u32 *indir = NULL;
892311f6 1248 u8 dev_hfunc = 0;
3de0b592
VD
1249 u8 *hkey = NULL;
1250 u8 *rss_config;
1251
892311f6 1252 if (!ops->get_rxfh)
3de0b592
VD
1253 return -EOPNOTSUPP;
1254
1255 if (ops->get_rxfh_indir_size)
1256 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592
VD
1257 if (ops->get_rxfh_key_size)
1258 dev_key_size = ops->get_rxfh_key_size(dev);
1259
f062a384 1260 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592 1261 return -EFAULT;
f062a384
BH
1262 user_indir_size = rxfh.indir_size;
1263 user_key_size = rxfh.key_size;
3de0b592 1264
f062a384 1265 /* Check that reserved fields are 0 for now */
84a1d9c4 1266 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384 1267 return -EINVAL;
84a1d9c4
EC
1268 /* Most drivers don't handle rss_context, check it's 0 as well */
1269 if (rxfh.rss_context && !ops->get_rxfh_context)
1270 return -EOPNOTSUPP;
f062a384
BH
1271
1272 rxfh.indir_size = dev_indir_size;
1273 rxfh.key_size = dev_key_size;
1274 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
3de0b592
VD
1275 return -EFAULT;
1276
3de0b592
VD
1277 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1278 (user_key_size && (user_key_size != dev_key_size)))
1279 return -EINVAL;
1280
1281 indir_bytes = user_indir_size * sizeof(indir[0]);
1282 total_size = indir_bytes + user_key_size;
1283 rss_config = kzalloc(total_size, GFP_USER);
1284 if (!rss_config)
1285 return -ENOMEM;
1286
1287 if (user_indir_size)
1288 indir = (u32 *)rss_config;
1289
1290 if (user_key_size)
1291 hkey = rss_config + indir_bytes;
1292
84a1d9c4
EC
1293 if (rxfh.rss_context)
1294 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1295 &dev_hfunc,
1296 rxfh.rss_context);
1297 else
1298 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
892311f6
EP
1299 if (ret)
1300 goto out;
3de0b592 1301
892311f6
EP
1302 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1303 &dev_hfunc, sizeof(rxfh.hfunc))) {
1304 ret = -EFAULT;
1305 } else if (copy_to_user(useraddr +
1306 offsetof(struct ethtool_rxfh, rss_config[0]),
1307 rss_config, total_size)) {
1308 ret = -EFAULT;
1309 }
1310out:
3de0b592
VD
1311 kfree(rss_config);
1312
1313 return ret;
1314}
1315
1316static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1317 void __user *useraddr)
1318{
1319 int ret;
1320 const struct ethtool_ops *ops = dev->ethtool_ops;
1321 struct ethtool_rxnfc rx_rings;
f062a384
BH
1322 struct ethtool_rxfh rxfh;
1323 u32 dev_indir_size = 0, dev_key_size = 0, i;
3de0b592
VD
1324 u32 *indir = NULL, indir_bytes = 0;
1325 u8 *hkey = NULL;
1326 u8 *rss_config;
3de0b592 1327 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
84a1d9c4 1328 bool delete = false;
3de0b592 1329
892311f6 1330 if (!ops->get_rxnfc || !ops->set_rxfh)
3de0b592
VD
1331 return -EOPNOTSUPP;
1332
1333 if (ops->get_rxfh_indir_size)
1334 dev_indir_size = ops->get_rxfh_indir_size(dev);
3de0b592 1335 if (ops->get_rxfh_key_size)
d340c862 1336 dev_key_size = ops->get_rxfh_key_size(dev);
3de0b592 1337
f062a384 1338 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
3de0b592
VD
1339 return -EFAULT;
1340
f062a384 1341 /* Check that reserved fields are 0 for now */
84a1d9c4 1342 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
f062a384 1343 return -EINVAL;
84a1d9c4
EC
1344 /* Most drivers don't handle rss_context, check it's 0 as well */
1345 if (rxfh.rss_context && !ops->set_rxfh_context)
1346 return -EOPNOTSUPP;
f062a384 1347
892311f6
EP
1348 /* If either indir, hash key or function is valid, proceed further.
1349 * Must request at least one change: indir size, hash key or function.
3de0b592 1350 */
f062a384
BH
1351 if ((rxfh.indir_size &&
1352 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1353 rxfh.indir_size != dev_indir_size) ||
1354 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1355 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
892311f6 1356 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
3de0b592
VD
1357 return -EINVAL;
1358
f062a384 1359 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
3de0b592
VD
1360 indir_bytes = dev_indir_size * sizeof(indir[0]);
1361
f062a384 1362 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
3de0b592
VD
1363 if (!rss_config)
1364 return -ENOMEM;
1365
1366 rx_rings.cmd = ETHTOOL_GRXRINGS;
1367 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1368 if (ret)
1369 goto out;
1370
84a1d9c4
EC
1371 /* rxfh.indir_size == 0 means reset the indir table to default (master
1372 * context) or delete the context (other RSS contexts).
f062a384 1373 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
3de0b592 1374 */
f062a384
BH
1375 if (rxfh.indir_size &&
1376 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
3de0b592
VD
1377 indir = (u32 *)rss_config;
1378 ret = ethtool_copy_validate_indir(indir,
1379 useraddr + rss_cfg_offset,
1380 &rx_rings,
f062a384 1381 rxfh.indir_size);
3de0b592 1382 if (ret)
7850f63f 1383 goto out;
f062a384 1384 } else if (rxfh.indir_size == 0) {
84a1d9c4
EC
1385 if (rxfh.rss_context == 0) {
1386 indir = (u32 *)rss_config;
1387 for (i = 0; i < dev_indir_size; i++)
1388 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1389 } else {
1390 delete = true;
1391 }
3de0b592 1392 }
278bc429 1393
f062a384 1394 if (rxfh.key_size) {
3de0b592
VD
1395 hkey = rss_config + indir_bytes;
1396 if (copy_from_user(hkey,
1397 useraddr + rss_cfg_offset + indir_bytes,
f062a384 1398 rxfh.key_size)) {
3de0b592
VD
1399 ret = -EFAULT;
1400 goto out;
278bc429 1401 }
7850f63f
BH
1402 }
1403
84a1d9c4
EC
1404 if (rxfh.rss_context)
1405 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1406 &rxfh.rss_context, delete);
1407 else
1408 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
d4ab4286
KJ
1409 if (ret)
1410 goto out;
1411
84a1d9c4
EC
1412 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1413 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1414 ret = -EFAULT;
1415
1416 if (!rxfh.rss_context) {
1417 /* indicate whether rxfh was set to default */
1418 if (rxfh.indir_size == 0)
1419 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1420 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1421 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1422 }
a5b6ee29
BH
1423
1424out:
3de0b592 1425 kfree(rss_config);
a5b6ee29
BH
1426 return ret;
1427}
1428
1da177e4
LT
1429static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1430{
1431 struct ethtool_regs regs;
76fd8593 1432 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4
LT
1433 void *regbuf;
1434 int reglen, ret;
1435
1436 if (!ops->get_regs || !ops->get_regs_len)
1437 return -EOPNOTSUPP;
1438
1439 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1440 return -EFAULT;
1441
1442 reglen = ops->get_regs_len(dev);
1443 if (regs.len > reglen)
1444 regs.len = reglen;
1445
3808d348
SG
1446 regbuf = NULL;
1447 if (reglen) {
1448 regbuf = vzalloc(reglen);
1449 if (!regbuf)
1450 return -ENOMEM;
1451 }
1da177e4
LT
1452
1453 ops->get_regs(dev, &regs, regbuf);
1454
1455 ret = -EFAULT;
1456 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1457 goto out;
1458 useraddr += offsetof(struct ethtool_regs, data);
67ae7cf1 1459 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1da177e4
LT
1460 goto out;
1461 ret = 0;
1462
1463 out:
a77f5db3 1464 vfree(regbuf);
1da177e4
LT
1465 return ret;
1466}
1467
d73d3a8c
BH
1468static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1469{
1470 struct ethtool_value reset;
1471 int ret;
1472
1473 if (!dev->ethtool_ops->reset)
1474 return -EOPNOTSUPP;
1475
1476 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1477 return -EFAULT;
1478
1479 ret = dev->ethtool_ops->reset(dev, &reset.data);
1480 if (ret)
1481 return ret;
1482
1483 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1484 return -EFAULT;
1485 return 0;
1486}
1487
1da177e4
LT
1488static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1489{
8e557421 1490 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1da177e4
LT
1491
1492 if (!dev->ethtool_ops->get_wol)
1493 return -EOPNOTSUPP;
1494
1495 dev->ethtool_ops->get_wol(dev, &wol);
1496
1497 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1498 return -EFAULT;
1499 return 0;
1500}
1501
1502static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1503{
1504 struct ethtool_wolinfo wol;
1505
1506 if (!dev->ethtool_ops->set_wol)
1507 return -EOPNOTSUPP;
1508
1509 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1510 return -EFAULT;
1511
1512 return dev->ethtool_ops->set_wol(dev, &wol);
1513}
1514
80f12ecc
YM
1515static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1516{
1517 struct ethtool_eee edata;
1518 int rc;
1519
1520 if (!dev->ethtool_ops->get_eee)
1521 return -EOPNOTSUPP;
1522
1523 memset(&edata, 0, sizeof(struct ethtool_eee));
1524 edata.cmd = ETHTOOL_GEEE;
1525 rc = dev->ethtool_ops->get_eee(dev, &edata);
1526
1527 if (rc)
1528 return rc;
1529
1530 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1531 return -EFAULT;
1532
1533 return 0;
1534}
1535
1536static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1537{
1538 struct ethtool_eee edata;
1539
1540 if (!dev->ethtool_ops->set_eee)
1541 return -EOPNOTSUPP;
1542
1543 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1544 return -EFAULT;
1545
1546 return dev->ethtool_ops->set_eee(dev, &edata);
1547}
1548
1da177e4
LT
1549static int ethtool_nway_reset(struct net_device *dev)
1550{
1551 if (!dev->ethtool_ops->nway_reset)
1552 return -EOPNOTSUPP;
1553
1554 return dev->ethtool_ops->nway_reset(dev);
1555}
1556
e596e6e4
BH
1557static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1558{
1559 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1560
1561 if (!dev->ethtool_ops->get_link)
1562 return -EOPNOTSUPP;
1563
1564 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1565
1566 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1567 return -EFAULT;
1568 return 0;
1569}
1570
081d094e
BH
1571static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1572 int (*getter)(struct net_device *,
1573 struct ethtool_eeprom *, u8 *),
1574 u32 total_len)
1da177e4
LT
1575{
1576 struct ethtool_eeprom eeprom;
b131dd5d
MSB
1577 void __user *userbuf = useraddr + sizeof(eeprom);
1578 u32 bytes_remaining;
1da177e4 1579 u8 *data;
b131dd5d 1580 int ret = 0;
1da177e4 1581
1da177e4
LT
1582 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1583 return -EFAULT;
1584
1585 /* Check for wrap and zero */
1586 if (eeprom.offset + eeprom.len <= eeprom.offset)
1587 return -EINVAL;
1588
1589 /* Check for exceeding total eeprom len */
081d094e 1590 if (eeprom.offset + eeprom.len > total_len)
1da177e4
LT
1591 return -EINVAL;
1592
b131dd5d 1593 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1594 if (!data)
1595 return -ENOMEM;
1596
b131dd5d
MSB
1597 bytes_remaining = eeprom.len;
1598 while (bytes_remaining > 0) {
1599 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1600
081d094e 1601 ret = getter(dev, &eeprom, data);
b131dd5d
MSB
1602 if (ret)
1603 break;
1604 if (copy_to_user(userbuf, data, eeprom.len)) {
1605 ret = -EFAULT;
1606 break;
1607 }
1608 userbuf += eeprom.len;
1609 eeprom.offset += eeprom.len;
1610 bytes_remaining -= eeprom.len;
1611 }
1da177e4 1612
c5835df9
MSB
1613 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1614 eeprom.offset -= eeprom.len;
1615 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1616 ret = -EFAULT;
1617
1da177e4
LT
1618 kfree(data);
1619 return ret;
1620}
1621
081d094e
BH
1622static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1623{
1624 const struct ethtool_ops *ops = dev->ethtool_ops;
1625
e0fb6fb6
GR
1626 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1627 !ops->get_eeprom_len(dev))
081d094e
BH
1628 return -EOPNOTSUPP;
1629
1630 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1631 ops->get_eeprom_len(dev));
1632}
1633
1da177e4
LT
1634static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1635{
1636 struct ethtool_eeprom eeprom;
76fd8593 1637 const struct ethtool_ops *ops = dev->ethtool_ops;
b131dd5d
MSB
1638 void __user *userbuf = useraddr + sizeof(eeprom);
1639 u32 bytes_remaining;
1da177e4 1640 u8 *data;
b131dd5d 1641 int ret = 0;
1da177e4 1642
e0fb6fb6
GR
1643 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1644 !ops->get_eeprom_len(dev))
1da177e4
LT
1645 return -EOPNOTSUPP;
1646
1647 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1648 return -EFAULT;
1649
1650 /* Check for wrap and zero */
1651 if (eeprom.offset + eeprom.len <= eeprom.offset)
1652 return -EINVAL;
1653
1654 /* Check for exceeding total eeprom len */
1655 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1656 return -EINVAL;
1657
b131dd5d 1658 data = kmalloc(PAGE_SIZE, GFP_USER);
1da177e4
LT
1659 if (!data)
1660 return -ENOMEM;
1661
b131dd5d
MSB
1662 bytes_remaining = eeprom.len;
1663 while (bytes_remaining > 0) {
1664 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1665
1666 if (copy_from_user(data, userbuf, eeprom.len)) {
1667 ret = -EFAULT;
1668 break;
1669 }
1670 ret = ops->set_eeprom(dev, &eeprom, data);
1671 if (ret)
1672 break;
1673 userbuf += eeprom.len;
1674 eeprom.offset += eeprom.len;
1675 bytes_remaining -= eeprom.len;
1676 }
1da177e4 1677
1da177e4
LT
1678 kfree(data);
1679 return ret;
1680}
1681
97f8aefb 1682static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1683 void __user *useraddr)
1da177e4 1684{
8e557421 1685 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1da177e4
LT
1686
1687 if (!dev->ethtool_ops->get_coalesce)
1688 return -EOPNOTSUPP;
1689
1690 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1691
1692 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1693 return -EFAULT;
1694 return 0;
1695}
1696
97f8aefb 1697static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1698 void __user *useraddr)
1da177e4
LT
1699{
1700 struct ethtool_coalesce coalesce;
1701
fa04ae5c 1702 if (!dev->ethtool_ops->set_coalesce)
1da177e4
LT
1703 return -EOPNOTSUPP;
1704
1705 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1706 return -EFAULT;
1707
1708 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1709}
1710
1711static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1712{
8e557421 1713 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4
LT
1714
1715 if (!dev->ethtool_ops->get_ringparam)
1716 return -EOPNOTSUPP;
1717
1718 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1719
1720 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1721 return -EFAULT;
1722 return 0;
1723}
1724
1725static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1726{
37e2d99b 1727 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1da177e4 1728
37e2d99b 1729 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1da177e4
LT
1730 return -EOPNOTSUPP;
1731
1732 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1733 return -EFAULT;
1734
37e2d99b
EE
1735 dev->ethtool_ops->get_ringparam(dev, &max);
1736
1737 /* ensure new ring parameters are within the maximums */
1738 if (ringparam.rx_pending > max.rx_max_pending ||
1739 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1740 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1741 ringparam.tx_pending > max.tx_max_pending)
1742 return -EINVAL;
1743
1da177e4
LT
1744 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1745}
1746
8b5933c3 1747static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1748 void __user *useraddr)
1749{
1750 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1751
1752 if (!dev->ethtool_ops->get_channels)
1753 return -EOPNOTSUPP;
1754
1755 dev->ethtool_ops->get_channels(dev, &channels);
1756
1757 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1758 return -EFAULT;
1759 return 0;
1760}
1761
1762static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1763 void __user *useraddr)
1764{
31a86d13 1765 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
d4ab4286 1766 u32 max_rx_in_use = 0;
8b5933c3 1767
8bf36862 1768 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
8b5933c3 1769 return -EOPNOTSUPP;
1770
1771 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1772 return -EFAULT;
1773
8bf36862
KJ
1774 dev->ethtool_ops->get_channels(dev, &max);
1775
1776 /* ensure new counts are within the maximums */
1777 if ((channels.rx_count > max.max_rx) ||
1778 (channels.tx_count > max.max_tx) ||
1779 (channels.combined_count > max.max_combined) ||
1780 (channels.other_count > max.max_other))
1781 return -EINVAL;
1782
d4ab4286
KJ
1783 /* ensure the new Rx count fits within the configured Rx flow
1784 * indirection table settings */
1785 if (netif_is_rxfh_configured(dev) &&
1786 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1787 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1788 return -EINVAL;
1789
8b5933c3 1790 return dev->ethtool_ops->set_channels(dev, &channels);
1791}
1792
1da177e4
LT
1793static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1794{
1795 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1796
1797 if (!dev->ethtool_ops->get_pauseparam)
1798 return -EOPNOTSUPP;
1799
1800 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1801
1802 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1803 return -EFAULT;
1804 return 0;
1805}
1806
1807static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1808{
1809 struct ethtool_pauseparam pauseparam;
1810
e1b90c41 1811 if (!dev->ethtool_ops->set_pauseparam)
1da177e4
LT
1812 return -EOPNOTSUPP;
1813
1814 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1815 return -EFAULT;
1816
1817 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1818}
1819
1da177e4
LT
1820static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1821{
1822 struct ethtool_test test;
76fd8593 1823 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1824 u64 *data;
ff03d49f 1825 int ret, test_len;
1da177e4 1826
a9828ec6 1827 if (!ops->self_test || !ops->get_sset_count)
1da177e4
LT
1828 return -EOPNOTSUPP;
1829
a9828ec6 1830 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
ff03d49f
JG
1831 if (test_len < 0)
1832 return test_len;
1833 WARN_ON(test_len == 0);
1834
1da177e4
LT
1835 if (copy_from_user(&test, useraddr, sizeof(test)))
1836 return -EFAULT;
1837
ff03d49f
JG
1838 test.len = test_len;
1839 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1da177e4
LT
1840 if (!data)
1841 return -ENOMEM;
1842
1843 ops->self_test(dev, &test, data);
1844
1845 ret = -EFAULT;
1846 if (copy_to_user(useraddr, &test, sizeof(test)))
1847 goto out;
1848 useraddr += sizeof(test);
1849 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1850 goto out;
1851 ret = 0;
1852
1853 out:
1854 kfree(data);
1855 return ret;
1856}
1857
1858static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1859{
1860 struct ethtool_gstrings gstrings;
1da177e4
LT
1861 u8 *data;
1862 int ret;
1863
1da177e4
LT
1864 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1865 return -EFAULT;
1866
340ae165 1867 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
a9828ec6
BH
1868 if (ret < 0)
1869 return ret;
4d1ceea8
AS
1870 if (ret > S32_MAX / ETH_GSTRING_LEN)
1871 return -ENOMEM;
1872 WARN_ON_ONCE(!ret);
a9828ec6
BH
1873
1874 gstrings.len = ret;
4d1ceea8
AS
1875 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1876 if (gstrings.len && !data)
1da177e4
LT
1877 return -ENOMEM;
1878
340ae165 1879 __ethtool_get_strings(dev, gstrings.string_set, data);
1da177e4
LT
1880
1881 ret = -EFAULT;
1882 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1883 goto out;
1884 useraddr += sizeof(gstrings);
4d1ceea8
AS
1885 if (gstrings.len &&
1886 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1da177e4
LT
1887 goto out;
1888 ret = 0;
1889
340ae165 1890out:
4d1ceea8 1891 vfree(data);
1da177e4
LT
1892 return ret;
1893}
1894
1895static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1896{
1897 struct ethtool_value id;
68f512f2 1898 static bool busy;
c03a14e8 1899 const struct ethtool_ops *ops = dev->ethtool_ops;
68f512f2 1900 int rc;
1da177e4 1901
c03a14e8 1902 if (!ops->set_phys_id)
1da177e4
LT
1903 return -EOPNOTSUPP;
1904
68f512f2
BH
1905 if (busy)
1906 return -EBUSY;
1907
1da177e4
LT
1908 if (copy_from_user(&id, useraddr, sizeof(id)))
1909 return -EFAULT;
1910
c03a14e8 1911 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
fce55922 1912 if (rc < 0)
68f512f2
BH
1913 return rc;
1914
1915 /* Drop the RTNL lock while waiting, but prevent reentry or
1916 * removal of the device.
1917 */
1918 busy = true;
1919 dev_hold(dev);
1920 rtnl_unlock();
1921
1922 if (rc == 0) {
1923 /* Driver will handle this itself */
1924 schedule_timeout_interruptible(
143780c6 1925 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
68f512f2 1926 } else {
fce55922
AB
1927 /* Driver expects to be called at twice the frequency in rc */
1928 int n = rc * 2, i, interval = HZ / n;
1929
1930 /* Count down seconds */
68f512f2 1931 do {
fce55922
AB
1932 /* Count down iterations per second */
1933 i = n;
1934 do {
1935 rtnl_lock();
c03a14e8 1936 rc = ops->set_phys_id(dev,
fce55922
AB
1937 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1938 rtnl_unlock();
1939 if (rc)
1940 break;
1941 schedule_timeout_interruptible(interval);
1942 } while (!signal_pending(current) && --i != 0);
68f512f2
BH
1943 } while (!signal_pending(current) &&
1944 (id.data == 0 || --id.data != 0));
1945 }
1946
1947 rtnl_lock();
1948 dev_put(dev);
1949 busy = false;
1950
c03a14e8 1951 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
68f512f2 1952 return rc;
1da177e4
LT
1953}
1954
1955static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1956{
1957 struct ethtool_stats stats;
76fd8593 1958 const struct ethtool_ops *ops = dev->ethtool_ops;
1da177e4 1959 u64 *data;
ff03d49f 1960 int ret, n_stats;
1da177e4 1961
a9828ec6 1962 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1da177e4
LT
1963 return -EOPNOTSUPP;
1964
a9828ec6 1965 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
ff03d49f
JG
1966 if (n_stats < 0)
1967 return n_stats;
4d1ceea8
AS
1968 if (n_stats > S32_MAX / sizeof(u64))
1969 return -ENOMEM;
1970 WARN_ON_ONCE(!n_stats);
1da177e4
LT
1971 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1972 return -EFAULT;
1973
ff03d49f 1974 stats.n_stats = n_stats;
4d1ceea8
AS
1975 data = vzalloc(n_stats * sizeof(u64));
1976 if (n_stats && !data)
1da177e4
LT
1977 return -ENOMEM;
1978
1979 ops->get_ethtool_stats(dev, &stats, data);
1980
1981 ret = -EFAULT;
1982 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1983 goto out;
1984 useraddr += sizeof(stats);
4d1ceea8 1985 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1da177e4
LT
1986 goto out;
1987 ret = 0;
1988
1989 out:
4d1ceea8 1990 vfree(data);
1da177e4
LT
1991 return ret;
1992}
1993
f3a40945
AL
1994static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1995{
1996 struct ethtool_stats stats;
1997 struct phy_device *phydev = dev->phydev;
1998 u64 *data;
1999 int ret, n_stats;
2000
2001 if (!phydev)
2002 return -EOPNOTSUPP;
2003
2004 n_stats = phy_get_sset_count(phydev);
f3a40945
AL
2005 if (n_stats < 0)
2006 return n_stats;
4d1ceea8
AS
2007 if (n_stats > S32_MAX / sizeof(u64))
2008 return -ENOMEM;
2009 WARN_ON_ONCE(!n_stats);
f3a40945
AL
2010
2011 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2012 return -EFAULT;
2013
2014 stats.n_stats = n_stats;
4d1ceea8
AS
2015 data = vzalloc(n_stats * sizeof(u64));
2016 if (n_stats && !data)
f3a40945
AL
2017 return -ENOMEM;
2018
2019 mutex_lock(&phydev->lock);
2020 phydev->drv->get_stats(phydev, &stats, data);
2021 mutex_unlock(&phydev->lock);
2022
2023 ret = -EFAULT;
2024 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2025 goto out;
2026 useraddr += sizeof(stats);
4d1ceea8 2027 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
f3a40945
AL
2028 goto out;
2029 ret = 0;
2030
2031 out:
4d1ceea8 2032 vfree(data);
f3a40945
AL
2033 return ret;
2034}
2035
0bf0519d 2036static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
a6f9a705
JW
2037{
2038 struct ethtool_perm_addr epaddr;
a6f9a705 2039
313674af 2040 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
a6f9a705
JW
2041 return -EFAULT;
2042
313674af
MW
2043 if (epaddr.size < dev->addr_len)
2044 return -ETOOSMALL;
2045 epaddr.size = dev->addr_len;
a6f9a705 2046
a6f9a705 2047 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
313674af 2048 return -EFAULT;
a6f9a705 2049 useraddr += sizeof(epaddr);
313674af
MW
2050 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2051 return -EFAULT;
2052 return 0;
a6f9a705
JW
2053}
2054
13c99b24
JG
2055static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2056 u32 cmd, u32 (*actor)(struct net_device *))
3ae7c0b2 2057{
8e557421 2058 struct ethtool_value edata = { .cmd = cmd };
3ae7c0b2 2059
13c99b24 2060 if (!actor)
3ae7c0b2
JG
2061 return -EOPNOTSUPP;
2062
13c99b24 2063 edata.data = actor(dev);
3ae7c0b2
JG
2064
2065 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2066 return -EFAULT;
2067 return 0;
2068}
2069
13c99b24
JG
2070static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2071 void (*actor)(struct net_device *, u32))
3ae7c0b2
JG
2072{
2073 struct ethtool_value edata;
2074
13c99b24 2075 if (!actor)
3ae7c0b2
JG
2076 return -EOPNOTSUPP;
2077
2078 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2079 return -EFAULT;
2080
13c99b24 2081 actor(dev, edata.data);
339bf024
JG
2082 return 0;
2083}
2084
13c99b24
JG
2085static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2086 int (*actor)(struct net_device *, u32))
339bf024
JG
2087{
2088 struct ethtool_value edata;
2089
13c99b24 2090 if (!actor)
339bf024
JG
2091 return -EOPNOTSUPP;
2092
2093 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2094 return -EFAULT;
2095
13c99b24 2096 return actor(dev, edata.data);
339bf024
JG
2097}
2098
97f8aefb 2099static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2100 char __user *useraddr)
05c6a8d7
AK
2101{
2102 struct ethtool_flash efl;
2103
2104 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2105 return -EFAULT;
2106
2107 if (!dev->ethtool_ops->flash_device)
2108 return -EOPNOTSUPP;
2109
786f5281
BH
2110 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2111
05c6a8d7
AK
2112 return dev->ethtool_ops->flash_device(dev, &efl);
2113}
2114
29dd54b7
AC
2115static int ethtool_set_dump(struct net_device *dev,
2116 void __user *useraddr)
2117{
2118 struct ethtool_dump dump;
2119
2120 if (!dev->ethtool_ops->set_dump)
2121 return -EOPNOTSUPP;
2122
2123 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2124 return -EFAULT;
2125
2126 return dev->ethtool_ops->set_dump(dev, &dump);
2127}
2128
2129static int ethtool_get_dump_flag(struct net_device *dev,
2130 void __user *useraddr)
2131{
2132 int ret;
2133 struct ethtool_dump dump;
2134 const struct ethtool_ops *ops = dev->ethtool_ops;
2135
c03a14e8 2136 if (!ops->get_dump_flag)
29dd54b7
AC
2137 return -EOPNOTSUPP;
2138
2139 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2140 return -EFAULT;
2141
2142 ret = ops->get_dump_flag(dev, &dump);
2143 if (ret)
2144 return ret;
2145
2146 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2147 return -EFAULT;
2148 return 0;
2149}
2150
2151static int ethtool_get_dump_data(struct net_device *dev,
2152 void __user *useraddr)
2153{
2154 int ret;
2155 __u32 len;
2156 struct ethtool_dump dump, tmp;
2157 const struct ethtool_ops *ops = dev->ethtool_ops;
2158 void *data = NULL;
2159
c03a14e8 2160 if (!ops->get_dump_data || !ops->get_dump_flag)
29dd54b7
AC
2161 return -EOPNOTSUPP;
2162
2163 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2164 return -EFAULT;
2165
2166 memset(&tmp, 0, sizeof(tmp));
2167 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2168 ret = ops->get_dump_flag(dev, &tmp);
2169 if (ret)
2170 return ret;
2171
c590b5e2 2172 len = min(tmp.len, dump.len);
29dd54b7
AC
2173 if (!len)
2174 return -EFAULT;
2175
c590b5e2
MS
2176 /* Don't ever let the driver think there's more space available
2177 * than it requested with .get_dump_flag().
2178 */
2179 dump.len = len;
2180
2181 /* Always allocate enough space to hold the whole thing so that the
2182 * driver does not need to check the length and bother with partial
2183 * dumping.
2184 */
29dd54b7
AC
2185 data = vzalloc(tmp.len);
2186 if (!data)
2187 return -ENOMEM;
2188 ret = ops->get_dump_data(dev, &dump, data);
2189 if (ret)
2190 goto out;
2191
c590b5e2
MS
2192 /* There are two sane possibilities:
2193 * 1. The driver's .get_dump_data() does not touch dump.len.
2194 * 2. Or it may set dump.len to how much it really writes, which
2195 * should be tmp.len (or len if it can do a partial dump).
2196 * In any case respond to userspace with the actual length of data
2197 * it's receiving.
2198 */
2199 WARN_ON(dump.len != len && dump.len != tmp.len);
2200 dump.len = len;
2201
29dd54b7
AC
2202 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2203 ret = -EFAULT;
2204 goto out;
2205 }
2206 useraddr += offsetof(struct ethtool_dump, data);
2207 if (copy_to_user(useraddr, data, len))
2208 ret = -EFAULT;
2209out:
2210 vfree(data);
2211 return ret;
2212}
2213
c8f3a8c3
RC
2214static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2215{
2216 int err = 0;
2217 struct ethtool_ts_info info;
2218 const struct ethtool_ops *ops = dev->ethtool_ops;
2219 struct phy_device *phydev = dev->phydev;
2220
2221 memset(&info, 0, sizeof(info));
2222 info.cmd = ETHTOOL_GET_TS_INFO;
2223
2224 if (phydev && phydev->drv && phydev->drv->ts_info) {
c8f3a8c3 2225 err = phydev->drv->ts_info(phydev, &info);
c03a14e8 2226 } else if (ops->get_ts_info) {
c8f3a8c3 2227 err = ops->get_ts_info(dev, &info);
c8f3a8c3
RC
2228 } else {
2229 info.so_timestamping =
2230 SOF_TIMESTAMPING_RX_SOFTWARE |
2231 SOF_TIMESTAMPING_SOFTWARE;
2232 info.phc_index = -1;
2233 }
2234
2235 if (err)
2236 return err;
2237
2238 if (copy_to_user(useraddr, &info, sizeof(info)))
2239 err = -EFAULT;
2240
2241 return err;
2242}
2243
2f438366
ES
2244static int __ethtool_get_module_info(struct net_device *dev,
2245 struct ethtool_modinfo *modinfo)
2246{
2247 const struct ethtool_ops *ops = dev->ethtool_ops;
2248 struct phy_device *phydev = dev->phydev;
2249
e679c9c1
RK
2250 if (dev->sfp_bus)
2251 return sfp_get_module_info(dev->sfp_bus, modinfo);
2252
2f438366
ES
2253 if (phydev && phydev->drv && phydev->drv->module_info)
2254 return phydev->drv->module_info(phydev, modinfo);
2255
2256 if (ops->get_module_info)
2257 return ops->get_module_info(dev, modinfo);
2258
2259 return -EOPNOTSUPP;
2260}
2261
41c3cb6d
SH
2262static int ethtool_get_module_info(struct net_device *dev,
2263 void __user *useraddr)
2264{
2265 int ret;
2266 struct ethtool_modinfo modinfo;
41c3cb6d
SH
2267
2268 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2269 return -EFAULT;
2270
2f438366 2271 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
2272 if (ret)
2273 return ret;
2274
2275 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2276 return -EFAULT;
2277
2278 return 0;
2279}
2280
2f438366
ES
2281static int __ethtool_get_module_eeprom(struct net_device *dev,
2282 struct ethtool_eeprom *ee, u8 *data)
2283{
2284 const struct ethtool_ops *ops = dev->ethtool_ops;
2285 struct phy_device *phydev = dev->phydev;
2286
e679c9c1
RK
2287 if (dev->sfp_bus)
2288 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2289
2f438366
ES
2290 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2291 return phydev->drv->module_eeprom(phydev, ee, data);
2292
2293 if (ops->get_module_eeprom)
2294 return ops->get_module_eeprom(dev, ee, data);
2295
2296 return -EOPNOTSUPP;
2297}
2298
41c3cb6d
SH
2299static int ethtool_get_module_eeprom(struct net_device *dev,
2300 void __user *useraddr)
2301{
2302 int ret;
2303 struct ethtool_modinfo modinfo;
41c3cb6d 2304
2f438366 2305 ret = __ethtool_get_module_info(dev, &modinfo);
41c3cb6d
SH
2306 if (ret)
2307 return ret;
2308
2f438366
ES
2309 return ethtool_get_any_eeprom(dev, useraddr,
2310 __ethtool_get_module_eeprom,
41c3cb6d
SH
2311 modinfo.eeprom_len);
2312}
2313
f0db9b07
GV
2314static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2315{
2316 switch (tuna->id) {
2317 case ETHTOOL_RX_COPYBREAK:
1255a505 2318 case ETHTOOL_TX_COPYBREAK:
f0db9b07
GV
2319 if (tuna->len != sizeof(u32) ||
2320 tuna->type_id != ETHTOOL_TUNABLE_U32)
2321 return -EINVAL;
2322 break;
e1577c1c
IK
2323 case ETHTOOL_PFC_PREVENTION_TOUT:
2324 if (tuna->len != sizeof(u16) ||
2325 tuna->type_id != ETHTOOL_TUNABLE_U16)
2326 return -EINVAL;
2327 break;
f0db9b07
GV
2328 default:
2329 return -EINVAL;
2330 }
2331
2332 return 0;
2333}
2334
2335static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2336{
2337 int ret;
2338 struct ethtool_tunable tuna;
2339 const struct ethtool_ops *ops = dev->ethtool_ops;
2340 void *data;
2341
2342 if (!ops->get_tunable)
2343 return -EOPNOTSUPP;
2344 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2345 return -EFAULT;
2346 ret = ethtool_tunable_valid(&tuna);
2347 if (ret)
2348 return ret;
2349 data = kmalloc(tuna.len, GFP_USER);
2350 if (!data)
2351 return -ENOMEM;
2352 ret = ops->get_tunable(dev, &tuna, data);
2353 if (ret)
2354 goto out;
2355 useraddr += sizeof(tuna);
2356 ret = -EFAULT;
2357 if (copy_to_user(useraddr, data, tuna.len))
2358 goto out;
2359 ret = 0;
2360
2361out:
2362 kfree(data);
2363 return ret;
2364}
2365
2366static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2367{
2368 int ret;
2369 struct ethtool_tunable tuna;
2370 const struct ethtool_ops *ops = dev->ethtool_ops;
2371 void *data;
2372
2373 if (!ops->set_tunable)
2374 return -EOPNOTSUPP;
2375 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2376 return -EFAULT;
2377 ret = ethtool_tunable_valid(&tuna);
2378 if (ret)
2379 return ret;
f0db9b07 2380 useraddr += sizeof(tuna);
30e7e3ec
AV
2381 data = memdup_user(useraddr, tuna.len);
2382 if (IS_ERR(data))
2383 return PTR_ERR(data);
f0db9b07
GV
2384 ret = ops->set_tunable(dev, &tuna, data);
2385
f0db9b07
GV
2386 kfree(data);
2387 return ret;
2388}
2389
421797b1
KL
2390static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2391 void __user *useraddr,
2392 struct ethtool_per_queue_op *per_queue_opt)
2393{
2394 u32 bit;
2395 int ret;
2396 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2397
2398 if (!dev->ethtool_ops->get_per_queue_coalesce)
2399 return -EOPNOTSUPP;
2400
2401 useraddr += sizeof(*per_queue_opt);
2402
3aa56885
YN
2403 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2404 MAX_NUM_QUEUE);
421797b1
KL
2405
2406 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2407 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2408
2409 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2410 if (ret != 0)
2411 return ret;
2412 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2413 return -EFAULT;
2414 useraddr += sizeof(coalesce);
2415 }
2416
2417 return 0;
2418}
2419
f38d138a
KL
2420static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2421 void __user *useraddr,
2422 struct ethtool_per_queue_op *per_queue_opt)
2423{
2424 u32 bit;
2425 int i, ret = 0;
2426 int n_queue;
2427 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2428 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2429
2430 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2431 (!dev->ethtool_ops->get_per_queue_coalesce))
2432 return -EOPNOTSUPP;
2433
2434 useraddr += sizeof(*per_queue_opt);
2435
3aa56885 2436 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
f38d138a
KL
2437 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2438 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2439 if (!backup)
2440 return -ENOMEM;
2441
2442 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2443 struct ethtool_coalesce coalesce;
2444
2445 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2446 if (ret != 0)
2447 goto roll_back;
2448
2449 tmp++;
2450
2451 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2452 ret = -EFAULT;
2453 goto roll_back;
2454 }
2455
2456 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2457 if (ret != 0)
2458 goto roll_back;
2459
2460 useraddr += sizeof(coalesce);
2461 }
2462
2463roll_back:
2464 if (ret != 0) {
2465 tmp = backup;
2466 for_each_set_bit(i, queue_mask, bit) {
2467 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2468 tmp++;
2469 }
2470 }
2471 kfree(backup);
2472
2473 return ret;
2474}
2475
ac2c7ad0
KL
2476static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2477{
2478 struct ethtool_per_queue_op per_queue_opt;
2479
2480 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2481 return -EFAULT;
2482
2483 switch (per_queue_opt.sub_command) {
421797b1
KL
2484 case ETHTOOL_GCOALESCE:
2485 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
f38d138a
KL
2486 case ETHTOOL_SCOALESCE:
2487 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
ac2c7ad0
KL
2488 default:
2489 return -EOPNOTSUPP;
2490 };
2491}
2492
968ad9da
RL
2493static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2494{
2495 switch (tuna->id) {
65feddd5
RL
2496 case ETHTOOL_PHY_DOWNSHIFT:
2497 if (tuna->len != sizeof(u8) ||
2498 tuna->type_id != ETHTOOL_TUNABLE_U8)
2499 return -EINVAL;
2500 break;
968ad9da
RL
2501 default:
2502 return -EINVAL;
2503 }
2504
2505 return 0;
2506}
2507
2508static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2509{
2510 int ret;
2511 struct ethtool_tunable tuna;
2512 struct phy_device *phydev = dev->phydev;
2513 void *data;
2514
2515 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2516 return -EOPNOTSUPP;
2517
2518 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2519 return -EFAULT;
2520 ret = ethtool_phy_tunable_valid(&tuna);
2521 if (ret)
2522 return ret;
2523 data = kmalloc(tuna.len, GFP_USER);
2524 if (!data)
2525 return -ENOMEM;
4b65246b 2526 mutex_lock(&phydev->lock);
968ad9da 2527 ret = phydev->drv->get_tunable(phydev, &tuna, data);
4b65246b 2528 mutex_unlock(&phydev->lock);
968ad9da
RL
2529 if (ret)
2530 goto out;
2531 useraddr += sizeof(tuna);
2532 ret = -EFAULT;
2533 if (copy_to_user(useraddr, data, tuna.len))
2534 goto out;
2535 ret = 0;
2536
2537out:
2538 kfree(data);
2539 return ret;
2540}
2541
2542static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2543{
2544 int ret;
2545 struct ethtool_tunable tuna;
2546 struct phy_device *phydev = dev->phydev;
2547 void *data;
2548
2549 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2550 return -EOPNOTSUPP;
2551 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2552 return -EFAULT;
2553 ret = ethtool_phy_tunable_valid(&tuna);
2554 if (ret)
2555 return ret;
968ad9da 2556 useraddr += sizeof(tuna);
30e7e3ec
AV
2557 data = memdup_user(useraddr, tuna.len);
2558 if (IS_ERR(data))
2559 return PTR_ERR(data);
4b65246b 2560 mutex_lock(&phydev->lock);
968ad9da 2561 ret = phydev->drv->set_tunable(phydev, &tuna, data);
4b65246b 2562 mutex_unlock(&phydev->lock);
968ad9da 2563
968ad9da
RL
2564 kfree(data);
2565 return ret;
2566}
2567
1a5f3da2
VSR
2568static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2569{
2570 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
a6d50512 2571 int rc;
1a5f3da2
VSR
2572
2573 if (!dev->ethtool_ops->get_fecparam)
2574 return -EOPNOTSUPP;
2575
a6d50512
EC
2576 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2577 if (rc)
2578 return rc;
1a5f3da2
VSR
2579
2580 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2581 return -EFAULT;
2582 return 0;
2583}
2584
2585static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2586{
2587 struct ethtool_fecparam fecparam;
2588
2589 if (!dev->ethtool_ops->set_fecparam)
2590 return -EOPNOTSUPP;
2591
2592 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2593 return -EFAULT;
2594
2595 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2596}
2597
600fed5e 2598/* The main entry point in this file. Called from net/core/dev_ioctl.c */
1da177e4 2599
881d966b 2600int dev_ethtool(struct net *net, struct ifreq *ifr)
1da177e4 2601{
881d966b 2602 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
1da177e4 2603 void __user *useraddr = ifr->ifr_data;
ac2c7ad0 2604 u32 ethcmd, sub_cmd;
1da177e4 2605 int rc;
b29d3145 2606 netdev_features_t old_features;
1da177e4 2607
1da177e4
LT
2608 if (!dev || !netif_device_present(dev))
2609 return -ENODEV;
2610
97f8aefb 2611 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1da177e4
LT
2612 return -EFAULT;
2613
ac2c7ad0
KL
2614 if (ethcmd == ETHTOOL_PERQUEUE) {
2615 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2616 return -EFAULT;
2617 } else {
2618 sub_cmd = ethcmd;
2619 }
75f3123c 2620 /* Allow some commands to be done by anyone */
ac2c7ad0 2621 switch (sub_cmd) {
0fdc100b 2622 case ETHTOOL_GSET:
75f3123c 2623 case ETHTOOL_GDRVINFO:
75f3123c 2624 case ETHTOOL_GMSGLVL:
2da45db2 2625 case ETHTOOL_GLINK:
75f3123c
SH
2626 case ETHTOOL_GCOALESCE:
2627 case ETHTOOL_GRINGPARAM:
2628 case ETHTOOL_GPAUSEPARAM:
2629 case ETHTOOL_GRXCSUM:
2630 case ETHTOOL_GTXCSUM:
2631 case ETHTOOL_GSG:
f80400a2 2632 case ETHTOOL_GSSET_INFO:
75f3123c 2633 case ETHTOOL_GSTRINGS:
2da45db2 2634 case ETHTOOL_GSTATS:
f3a40945 2635 case ETHTOOL_GPHYSTATS:
75f3123c
SH
2636 case ETHTOOL_GTSO:
2637 case ETHTOOL_GPERMADDR:
75f3123c 2638 case ETHTOOL_GGSO:
1cab819b 2639 case ETHTOOL_GGRO:
339bf024
JG
2640 case ETHTOOL_GFLAGS:
2641 case ETHTOOL_GPFLAGS:
0853ad66 2642 case ETHTOOL_GRXFH:
59089d8d
SB
2643 case ETHTOOL_GRXRINGS:
2644 case ETHTOOL_GRXCLSRLCNT:
2645 case ETHTOOL_GRXCLSRULE:
2646 case ETHTOOL_GRXCLSRLALL:
2da45db2 2647 case ETHTOOL_GRXFHINDIR:
3de0b592 2648 case ETHTOOL_GRSSH:
5455c699 2649 case ETHTOOL_GFEATURES:
2da45db2 2650 case ETHTOOL_GCHANNELS:
c8f3a8c3 2651 case ETHTOOL_GET_TS_INFO:
2da45db2 2652 case ETHTOOL_GEEE:
f0db9b07 2653 case ETHTOOL_GTUNABLE:
968ad9da 2654 case ETHTOOL_PHY_GTUNABLE:
8006f6bf 2655 case ETHTOOL_GLINKSETTINGS:
1a5f3da2 2656 case ETHTOOL_GFECPARAM:
75f3123c
SH
2657 break;
2658 default:
5e1fccc0 2659 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
75f3123c
SH
2660 return -EPERM;
2661 }
2662
97f8aefb 2663 if (dev->ethtool_ops->begin) {
2664 rc = dev->ethtool_ops->begin(dev);
2665 if (rc < 0)
1da177e4 2666 return rc;
97f8aefb 2667 }
d8a33ac4
SH
2668 old_features = dev->features;
2669
1da177e4
LT
2670 switch (ethcmd) {
2671 case ETHTOOL_GSET:
2672 rc = ethtool_get_settings(dev, useraddr);
2673 break;
2674 case ETHTOOL_SSET:
2675 rc = ethtool_set_settings(dev, useraddr);
2676 break;
2677 case ETHTOOL_GDRVINFO:
2678 rc = ethtool_get_drvinfo(dev, useraddr);
1da177e4
LT
2679 break;
2680 case ETHTOOL_GREGS:
2681 rc = ethtool_get_regs(dev, useraddr);
2682 break;
2683 case ETHTOOL_GWOL:
2684 rc = ethtool_get_wol(dev, useraddr);
2685 break;
2686 case ETHTOOL_SWOL:
2687 rc = ethtool_set_wol(dev, useraddr);
2688 break;
2689 case ETHTOOL_GMSGLVL:
13c99b24
JG
2690 rc = ethtool_get_value(dev, useraddr, ethcmd,
2691 dev->ethtool_ops->get_msglevel);
1da177e4
LT
2692 break;
2693 case ETHTOOL_SMSGLVL:
13c99b24
JG
2694 rc = ethtool_set_value_void(dev, useraddr,
2695 dev->ethtool_ops->set_msglevel);
1da177e4 2696 break;
80f12ecc
YM
2697 case ETHTOOL_GEEE:
2698 rc = ethtool_get_eee(dev, useraddr);
2699 break;
2700 case ETHTOOL_SEEE:
2701 rc = ethtool_set_eee(dev, useraddr);
2702 break;
1da177e4
LT
2703 case ETHTOOL_NWAY_RST:
2704 rc = ethtool_nway_reset(dev);
2705 break;
2706 case ETHTOOL_GLINK:
e596e6e4 2707 rc = ethtool_get_link(dev, useraddr);
1da177e4
LT
2708 break;
2709 case ETHTOOL_GEEPROM:
2710 rc = ethtool_get_eeprom(dev, useraddr);
2711 break;
2712 case ETHTOOL_SEEPROM:
2713 rc = ethtool_set_eeprom(dev, useraddr);
2714 break;
2715 case ETHTOOL_GCOALESCE:
2716 rc = ethtool_get_coalesce(dev, useraddr);
2717 break;
2718 case ETHTOOL_SCOALESCE:
2719 rc = ethtool_set_coalesce(dev, useraddr);
2720 break;
2721 case ETHTOOL_GRINGPARAM:
2722 rc = ethtool_get_ringparam(dev, useraddr);
2723 break;
2724 case ETHTOOL_SRINGPARAM:
2725 rc = ethtool_set_ringparam(dev, useraddr);
2726 break;
2727 case ETHTOOL_GPAUSEPARAM:
2728 rc = ethtool_get_pauseparam(dev, useraddr);
2729 break;
2730 case ETHTOOL_SPAUSEPARAM:
2731 rc = ethtool_set_pauseparam(dev, useraddr);
2732 break;
1da177e4
LT
2733 case ETHTOOL_TEST:
2734 rc = ethtool_self_test(dev, useraddr);
2735 break;
2736 case ETHTOOL_GSTRINGS:
2737 rc = ethtool_get_strings(dev, useraddr);
2738 break;
2739 case ETHTOOL_PHYS_ID:
2740 rc = ethtool_phys_id(dev, useraddr);
2741 break;
2742 case ETHTOOL_GSTATS:
2743 rc = ethtool_get_stats(dev, useraddr);
2744 break;
a6f9a705
JW
2745 case ETHTOOL_GPERMADDR:
2746 rc = ethtool_get_perm_addr(dev, useraddr);
2747 break;
3ae7c0b2 2748 case ETHTOOL_GFLAGS:
13c99b24 2749 rc = ethtool_get_value(dev, useraddr, ethcmd,
bc5787c6 2750 __ethtool_get_flags);
3ae7c0b2
JG
2751 break;
2752 case ETHTOOL_SFLAGS:
da8ac86c 2753 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
3ae7c0b2 2754 break;
339bf024 2755 case ETHTOOL_GPFLAGS:
13c99b24
JG
2756 rc = ethtool_get_value(dev, useraddr, ethcmd,
2757 dev->ethtool_ops->get_priv_flags);
339bf024
JG
2758 break;
2759 case ETHTOOL_SPFLAGS:
13c99b24
JG
2760 rc = ethtool_set_value(dev, useraddr,
2761 dev->ethtool_ops->set_priv_flags);
339bf024 2762 break;
0853ad66 2763 case ETHTOOL_GRXFH:
59089d8d
SB
2764 case ETHTOOL_GRXRINGS:
2765 case ETHTOOL_GRXCLSRLCNT:
2766 case ETHTOOL_GRXCLSRULE:
2767 case ETHTOOL_GRXCLSRLALL:
bf988435 2768 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
0853ad66
SB
2769 break;
2770 case ETHTOOL_SRXFH:
59089d8d
SB
2771 case ETHTOOL_SRXCLSRLDEL:
2772 case ETHTOOL_SRXCLSRLINS:
bf988435 2773 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
0853ad66 2774 break;
05c6a8d7
AK
2775 case ETHTOOL_FLASHDEV:
2776 rc = ethtool_flash_device(dev, useraddr);
2777 break;
d73d3a8c
BH
2778 case ETHTOOL_RESET:
2779 rc = ethtool_reset(dev, useraddr);
2780 break;
723b2f57
JG
2781 case ETHTOOL_GSSET_INFO:
2782 rc = ethtool_get_sset_info(dev, useraddr);
2783 break;
a5b6ee29
BH
2784 case ETHTOOL_GRXFHINDIR:
2785 rc = ethtool_get_rxfh_indir(dev, useraddr);
2786 break;
2787 case ETHTOOL_SRXFHINDIR:
2788 rc = ethtool_set_rxfh_indir(dev, useraddr);
2789 break;
3de0b592
VD
2790 case ETHTOOL_GRSSH:
2791 rc = ethtool_get_rxfh(dev, useraddr);
2792 break;
2793 case ETHTOOL_SRSSH:
2794 rc = ethtool_set_rxfh(dev, useraddr);
2795 break;
5455c699
MM
2796 case ETHTOOL_GFEATURES:
2797 rc = ethtool_get_features(dev, useraddr);
2798 break;
2799 case ETHTOOL_SFEATURES:
2800 rc = ethtool_set_features(dev, useraddr);
2801 break;
0a417704 2802 case ETHTOOL_GTXCSUM:
e83d360d 2803 case ETHTOOL_GRXCSUM:
0a417704
MM
2804 case ETHTOOL_GSG:
2805 case ETHTOOL_GTSO:
0a417704
MM
2806 case ETHTOOL_GGSO:
2807 case ETHTOOL_GGRO:
2808 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2809 break;
2810 case ETHTOOL_STXCSUM:
e83d360d 2811 case ETHTOOL_SRXCSUM:
0a417704
MM
2812 case ETHTOOL_SSG:
2813 case ETHTOOL_STSO:
0a417704
MM
2814 case ETHTOOL_SGSO:
2815 case ETHTOOL_SGRO:
2816 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2817 break;
8b5933c3 2818 case ETHTOOL_GCHANNELS:
2819 rc = ethtool_get_channels(dev, useraddr);
2820 break;
2821 case ETHTOOL_SCHANNELS:
2822 rc = ethtool_set_channels(dev, useraddr);
2823 break;
29dd54b7
AC
2824 case ETHTOOL_SET_DUMP:
2825 rc = ethtool_set_dump(dev, useraddr);
2826 break;
2827 case ETHTOOL_GET_DUMP_FLAG:
2828 rc = ethtool_get_dump_flag(dev, useraddr);
2829 break;
2830 case ETHTOOL_GET_DUMP_DATA:
2831 rc = ethtool_get_dump_data(dev, useraddr);
2832 break;
c8f3a8c3
RC
2833 case ETHTOOL_GET_TS_INFO:
2834 rc = ethtool_get_ts_info(dev, useraddr);
2835 break;
41c3cb6d
SH
2836 case ETHTOOL_GMODULEINFO:
2837 rc = ethtool_get_module_info(dev, useraddr);
2838 break;
2839 case ETHTOOL_GMODULEEEPROM:
2840 rc = ethtool_get_module_eeprom(dev, useraddr);
2841 break;
f0db9b07
GV
2842 case ETHTOOL_GTUNABLE:
2843 rc = ethtool_get_tunable(dev, useraddr);
2844 break;
2845 case ETHTOOL_STUNABLE:
2846 rc = ethtool_set_tunable(dev, useraddr);
2847 break;
f3a40945
AL
2848 case ETHTOOL_GPHYSTATS:
2849 rc = ethtool_get_phy_stats(dev, useraddr);
2850 break;
ac2c7ad0
KL
2851 case ETHTOOL_PERQUEUE:
2852 rc = ethtool_set_per_queue(dev, useraddr);
2853 break;
3f1ac7a7
DD
2854 case ETHTOOL_GLINKSETTINGS:
2855 rc = ethtool_get_link_ksettings(dev, useraddr);
2856 break;
2857 case ETHTOOL_SLINKSETTINGS:
2858 rc = ethtool_set_link_ksettings(dev, useraddr);
2859 break;
968ad9da
RL
2860 case ETHTOOL_PHY_GTUNABLE:
2861 rc = get_phy_tunable(dev, useraddr);
2862 break;
2863 case ETHTOOL_PHY_STUNABLE:
2864 rc = set_phy_tunable(dev, useraddr);
2865 break;
1a5f3da2
VSR
2866 case ETHTOOL_GFECPARAM:
2867 rc = ethtool_get_fecparam(dev, useraddr);
2868 break;
2869 case ETHTOOL_SFECPARAM:
2870 rc = ethtool_set_fecparam(dev, useraddr);
2871 break;
1da177e4 2872 default:
61a44b9c 2873 rc = -EOPNOTSUPP;
1da177e4 2874 }
4ec93edb 2875
e71a4783 2876 if (dev->ethtool_ops->complete)
1da177e4 2877 dev->ethtool_ops->complete(dev);
d8a33ac4
SH
2878
2879 if (old_features != dev->features)
2880 netdev_features_change(dev);
2881
1da177e4 2882 return rc;
1da177e4 2883}