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