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