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