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