2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
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.
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/signal.h>
28 #include <linux/net.h>
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.
36 u32 ethtool_op_get_link(struct net_device *dev)
38 return netif_carrier_ok(dev) ? 1 : 0;
40 EXPORT_SYMBOL(ethtool_op_get_link);
42 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
44 info->so_timestamping =
45 SOF_TIMESTAMPING_TX_SOFTWARE |
46 SOF_TIMESTAMPING_RX_SOFTWARE |
47 SOF_TIMESTAMPING_SOFTWARE;
51 EXPORT_SYMBOL(ethtool_op_get_ts_info);
53 /* Handlers for each ethtool command */
55 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
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",
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",
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_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
83 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
84 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
85 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
86 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
87 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
88 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
89 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
90 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
91 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
92 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
93 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
95 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
96 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
97 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
98 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
99 [NETIF_F_RXHASH_BIT] = "rx-hashing",
100 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
101 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
102 [NETIF_F_LOOPBACK_BIT] = "loopback",
103 [NETIF_F_RXFCS_BIT] = "rx-fcs",
104 [NETIF_F_RXALL_BIT] = "rx-all",
105 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
106 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
107 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
108 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
112 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
113 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
114 [ETH_RSS_HASH_XOR_BIT] = "xor",
115 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
119 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
120 [ETHTOOL_ID_UNSPEC] = "Unspec",
121 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
122 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
126 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
127 [ETHTOOL_ID_UNSPEC] = "Unspec",
128 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
131 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
133 struct ethtool_gfeatures cmd = {
134 .cmd = ETHTOOL_GFEATURES,
135 .size = ETHTOOL_DEV_FEATURE_WORDS,
137 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
138 u32 __user *sizeaddr;
142 /* in case feature bits run out again */
143 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
145 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
146 features[i].available = (u32)(dev->hw_features >> (32 * i));
147 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
148 features[i].active = (u32)(dev->features >> (32 * i));
149 features[i].never_changed =
150 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
153 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
154 if (get_user(copy_size, sizeaddr))
157 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
158 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
160 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
162 useraddr += sizeof(cmd);
163 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
169 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
171 struct ethtool_sfeatures cmd;
172 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
173 netdev_features_t wanted = 0, valid = 0;
176 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
178 useraddr += sizeof(cmd);
180 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
183 if (copy_from_user(features, useraddr, sizeof(features)))
186 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
187 valid |= (netdev_features_t)features[i].valid << (32 * i);
188 wanted |= (netdev_features_t)features[i].requested << (32 * i);
191 if (valid & ~NETIF_F_ETHTOOL_BITS)
194 if (valid & ~dev->hw_features) {
195 valid &= dev->hw_features;
196 ret |= ETHTOOL_F_UNSUPPORTED;
199 dev->wanted_features &= ~valid;
200 dev->wanted_features |= wanted & valid;
201 __netdev_update_features(dev);
203 if ((dev->wanted_features ^ dev->features) & valid)
204 ret |= ETHTOOL_F_WISH;
209 static int phy_get_sset_count(struct phy_device *phydev)
213 if (phydev->drv->get_sset_count &&
214 phydev->drv->get_strings &&
215 phydev->drv->get_stats) {
216 mutex_lock(&phydev->lock);
217 ret = phydev->drv->get_sset_count(phydev);
218 mutex_unlock(&phydev->lock);
226 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
228 const struct ethtool_ops *ops = dev->ethtool_ops;
230 if (sset == ETH_SS_FEATURES)
231 return ARRAY_SIZE(netdev_features_strings);
233 if (sset == ETH_SS_RSS_HASH_FUNCS)
234 return ARRAY_SIZE(rss_hash_func_strings);
236 if (sset == ETH_SS_TUNABLES)
237 return ARRAY_SIZE(tunable_strings);
239 if (sset == ETH_SS_PHY_TUNABLES)
240 return ARRAY_SIZE(phy_tunable_strings);
242 if (sset == ETH_SS_PHY_STATS) {
244 return phy_get_sset_count(dev->phydev);
249 if (ops->get_sset_count && ops->get_strings)
250 return ops->get_sset_count(dev, sset);
255 static void __ethtool_get_strings(struct net_device *dev,
256 u32 stringset, u8 *data)
258 const struct ethtool_ops *ops = dev->ethtool_ops;
260 if (stringset == ETH_SS_FEATURES)
261 memcpy(data, netdev_features_strings,
262 sizeof(netdev_features_strings));
263 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
264 memcpy(data, rss_hash_func_strings,
265 sizeof(rss_hash_func_strings));
266 else if (stringset == ETH_SS_TUNABLES)
267 memcpy(data, tunable_strings, sizeof(tunable_strings));
268 else if (stringset == ETH_SS_PHY_TUNABLES)
269 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
270 else if (stringset == ETH_SS_PHY_STATS) {
271 struct phy_device *phydev = dev->phydev;
274 mutex_lock(&phydev->lock);
275 phydev->drv->get_strings(phydev, data);
276 mutex_unlock(&phydev->lock);
281 /* ops->get_strings is valid because checked earlier */
282 ops->get_strings(dev, stringset, data);
285 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
287 /* feature masks of legacy discrete ethtool ops */
290 case ETHTOOL_GTXCSUM:
291 case ETHTOOL_STXCSUM:
292 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
293 case ETHTOOL_GRXCSUM:
294 case ETHTOOL_SRXCSUM:
295 return NETIF_F_RXCSUM;
301 return NETIF_F_ALL_TSO;
316 static int ethtool_get_one_feature(struct net_device *dev,
317 char __user *useraddr, u32 ethcmd)
319 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
320 struct ethtool_value edata = {
322 .data = !!(dev->features & mask),
325 if (copy_to_user(useraddr, &edata, sizeof(edata)))
330 static int ethtool_set_one_feature(struct net_device *dev,
331 void __user *useraddr, u32 ethcmd)
333 struct ethtool_value edata;
334 netdev_features_t mask;
336 if (copy_from_user(&edata, useraddr, sizeof(edata)))
339 mask = ethtool_get_feature_mask(ethcmd);
340 mask &= dev->hw_features;
345 dev->wanted_features |= mask;
347 dev->wanted_features &= ~mask;
349 __netdev_update_features(dev);
354 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
355 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
356 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
357 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
360 static u32 __ethtool_get_flags(struct net_device *dev)
364 if (dev->features & NETIF_F_LRO)
365 flags |= ETH_FLAG_LRO;
366 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
367 flags |= ETH_FLAG_RXVLAN;
368 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
369 flags |= ETH_FLAG_TXVLAN;
370 if (dev->features & NETIF_F_NTUPLE)
371 flags |= ETH_FLAG_NTUPLE;
372 if (dev->features & NETIF_F_RXHASH)
373 flags |= ETH_FLAG_RXHASH;
378 static int __ethtool_set_flags(struct net_device *dev, u32 data)
380 netdev_features_t features = 0, changed;
382 if (data & ~ETH_ALL_FLAGS)
385 if (data & ETH_FLAG_LRO)
386 features |= NETIF_F_LRO;
387 if (data & ETH_FLAG_RXVLAN)
388 features |= NETIF_F_HW_VLAN_CTAG_RX;
389 if (data & ETH_FLAG_TXVLAN)
390 features |= NETIF_F_HW_VLAN_CTAG_TX;
391 if (data & ETH_FLAG_NTUPLE)
392 features |= NETIF_F_NTUPLE;
393 if (data & ETH_FLAG_RXHASH)
394 features |= NETIF_F_RXHASH;
396 /* allow changing only bits set in hw_features */
397 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
398 if (changed & ~dev->hw_features)
399 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
401 dev->wanted_features =
402 (dev->wanted_features & ~changed) | (features & changed);
404 __netdev_update_features(dev);
409 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
412 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
415 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
417 /* return false if src had higher bits set. lower bits always updated. */
418 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
419 const unsigned long *src)
423 /* TODO: following test will soon always be true */
424 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
425 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
427 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
428 bitmap_fill(ext, 32);
429 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
430 if (bitmap_intersects(ext, src,
431 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
432 /* src mask goes beyond bit 31 */
436 *legacy_u32 = src[0];
439 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
441 /* return false if legacy contained non-0 deprecated fields
442 * transceiver/maxtxpkt/maxrxpkt. rest of ksettings always updated
445 convert_legacy_settings_to_link_ksettings(
446 struct ethtool_link_ksettings *link_ksettings,
447 const struct ethtool_cmd *legacy_settings)
451 memset(link_ksettings, 0, sizeof(*link_ksettings));
453 /* This is used to tell users that driver is still using these
454 * deprecated legacy fields, and they should not use
455 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
457 if (legacy_settings->transceiver ||
458 legacy_settings->maxtxpkt ||
459 legacy_settings->maxrxpkt)
462 ethtool_convert_legacy_u32_to_link_mode(
463 link_ksettings->link_modes.supported,
464 legacy_settings->supported);
465 ethtool_convert_legacy_u32_to_link_mode(
466 link_ksettings->link_modes.advertising,
467 legacy_settings->advertising);
468 ethtool_convert_legacy_u32_to_link_mode(
469 link_ksettings->link_modes.lp_advertising,
470 legacy_settings->lp_advertising);
471 link_ksettings->base.speed
472 = ethtool_cmd_speed(legacy_settings);
473 link_ksettings->base.duplex
474 = legacy_settings->duplex;
475 link_ksettings->base.port
476 = legacy_settings->port;
477 link_ksettings->base.phy_address
478 = legacy_settings->phy_address;
479 link_ksettings->base.autoneg
480 = legacy_settings->autoneg;
481 link_ksettings->base.mdio_support
482 = legacy_settings->mdio_support;
483 link_ksettings->base.eth_tp_mdix
484 = legacy_settings->eth_tp_mdix;
485 link_ksettings->base.eth_tp_mdix_ctrl
486 = legacy_settings->eth_tp_mdix_ctrl;
490 /* return false if ksettings link modes had higher bits
491 * set. legacy_settings always updated (best effort)
494 convert_link_ksettings_to_legacy_settings(
495 struct ethtool_cmd *legacy_settings,
496 const struct ethtool_link_ksettings *link_ksettings)
500 memset(legacy_settings, 0, sizeof(*legacy_settings));
501 /* this also clears the deprecated fields in legacy structure:
507 retval &= ethtool_convert_link_mode_to_legacy_u32(
508 &legacy_settings->supported,
509 link_ksettings->link_modes.supported);
510 retval &= ethtool_convert_link_mode_to_legacy_u32(
511 &legacy_settings->advertising,
512 link_ksettings->link_modes.advertising);
513 retval &= ethtool_convert_link_mode_to_legacy_u32(
514 &legacy_settings->lp_advertising,
515 link_ksettings->link_modes.lp_advertising);
516 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
517 legacy_settings->duplex
518 = link_ksettings->base.duplex;
519 legacy_settings->port
520 = link_ksettings->base.port;
521 legacy_settings->phy_address
522 = link_ksettings->base.phy_address;
523 legacy_settings->autoneg
524 = link_ksettings->base.autoneg;
525 legacy_settings->mdio_support
526 = link_ksettings->base.mdio_support;
527 legacy_settings->eth_tp_mdix
528 = link_ksettings->base.eth_tp_mdix;
529 legacy_settings->eth_tp_mdix_ctrl
530 = link_ksettings->base.eth_tp_mdix_ctrl;
534 /* number of 32-bit words to store the user's link mode bitmaps */
535 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
536 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
538 /* layout of the struct passed from/to userland */
539 struct ethtool_link_usettings {
540 struct ethtool_link_settings base;
542 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
543 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
544 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
548 /* Internal kernel helper to query a device ethtool_link_settings.
550 * Backward compatibility note: for compatibility with legacy drivers
551 * that implement only the ethtool_cmd API, this has to work with both
552 * drivers implementing get_link_ksettings API and drivers
553 * implementing get_settings API. When drivers implement get_settings
554 * and report ethtool_cmd deprecated fields
555 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
556 * because the resulting struct ethtool_link_settings does not report them.
558 int __ethtool_get_link_ksettings(struct net_device *dev,
559 struct ethtool_link_ksettings *link_ksettings)
562 struct ethtool_cmd cmd;
566 if (dev->ethtool_ops->get_link_ksettings) {
567 memset(link_ksettings, 0, sizeof(*link_ksettings));
568 return dev->ethtool_ops->get_link_ksettings(dev,
572 /* driver doesn't support %ethtool_link_ksettings API. revert to
573 * legacy %ethtool_cmd API, unless it's not supported either.
574 * TODO: remove when ethtool_ops::get_settings disappears internally
576 if (!dev->ethtool_ops->get_settings)
579 memset(&cmd, 0, sizeof(cmd));
580 cmd.cmd = ETHTOOL_GSET;
581 err = dev->ethtool_ops->get_settings(dev, &cmd);
585 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
587 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
590 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
592 /* convert ethtool_link_usettings in user space to a kernel internal
593 * ethtool_link_ksettings. return 0 on success, errno on error.
595 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
596 const void __user *from)
598 struct ethtool_link_usettings link_usettings;
600 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
603 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
604 bitmap_from_u32array(to->link_modes.supported,
605 __ETHTOOL_LINK_MODE_MASK_NBITS,
606 link_usettings.link_modes.supported,
607 __ETHTOOL_LINK_MODE_MASK_NU32);
608 bitmap_from_u32array(to->link_modes.advertising,
609 __ETHTOOL_LINK_MODE_MASK_NBITS,
610 link_usettings.link_modes.advertising,
611 __ETHTOOL_LINK_MODE_MASK_NU32);
612 bitmap_from_u32array(to->link_modes.lp_advertising,
613 __ETHTOOL_LINK_MODE_MASK_NBITS,
614 link_usettings.link_modes.lp_advertising,
615 __ETHTOOL_LINK_MODE_MASK_NU32);
620 /* convert a kernel internal ethtool_link_ksettings to
621 * ethtool_link_usettings in user space. return 0 on success, errno on
625 store_link_ksettings_for_user(void __user *to,
626 const struct ethtool_link_ksettings *from)
628 struct ethtool_link_usettings link_usettings;
630 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
631 bitmap_to_u32array(link_usettings.link_modes.supported,
632 __ETHTOOL_LINK_MODE_MASK_NU32,
633 from->link_modes.supported,
634 __ETHTOOL_LINK_MODE_MASK_NBITS);
635 bitmap_to_u32array(link_usettings.link_modes.advertising,
636 __ETHTOOL_LINK_MODE_MASK_NU32,
637 from->link_modes.advertising,
638 __ETHTOOL_LINK_MODE_MASK_NBITS);
639 bitmap_to_u32array(link_usettings.link_modes.lp_advertising,
640 __ETHTOOL_LINK_MODE_MASK_NU32,
641 from->link_modes.lp_advertising,
642 __ETHTOOL_LINK_MODE_MASK_NBITS);
644 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
650 /* Query device for its ethtool_link_settings.
652 * Backward compatibility note: this function must fail when driver
653 * does not implement ethtool::get_link_ksettings, even if legacy
654 * ethtool_ops::get_settings is implemented. This tells new versions
655 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
656 * this driver, so that they can correctly access the ethtool_cmd
657 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
658 * implements ethtool_ops::get_settings anymore.
660 static int ethtool_get_link_ksettings(struct net_device *dev,
661 void __user *useraddr)
664 struct ethtool_link_ksettings link_ksettings;
668 if (!dev->ethtool_ops->get_link_ksettings)
671 /* handle bitmap nbits handshake */
672 if (copy_from_user(&link_ksettings.base, useraddr,
673 sizeof(link_ksettings.base)))
676 if (__ETHTOOL_LINK_MODE_MASK_NU32
677 != link_ksettings.base.link_mode_masks_nwords) {
678 /* wrong link mode nbits requested */
679 memset(&link_ksettings, 0, sizeof(link_ksettings));
680 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
681 /* send back number of words required as negative val */
682 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
683 "need too many bits for link modes!");
684 link_ksettings.base.link_mode_masks_nwords
685 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
687 /* copy the base fields back to user, not the link
690 if (copy_to_user(useraddr, &link_ksettings.base,
691 sizeof(link_ksettings.base)))
697 /* handshake successful: user/kernel agree on
698 * link_mode_masks_nwords
701 memset(&link_ksettings, 0, sizeof(link_ksettings));
702 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
706 /* make sure we tell the right values to user */
707 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
708 link_ksettings.base.link_mode_masks_nwords
709 = __ETHTOOL_LINK_MODE_MASK_NU32;
711 return store_link_ksettings_for_user(useraddr, &link_ksettings);
714 /* Update device ethtool_link_settings.
716 * Backward compatibility note: this function must fail when driver
717 * does not implement ethtool::set_link_ksettings, even if legacy
718 * ethtool_ops::set_settings is implemented. This tells new versions
719 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
720 * this driver, so that they can correctly update the ethtool_cmd
721 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
722 * implements ethtool_ops::get_settings anymore.
724 static int ethtool_set_link_ksettings(struct net_device *dev,
725 void __user *useraddr)
728 struct ethtool_link_ksettings link_ksettings;
732 if (!dev->ethtool_ops->set_link_ksettings)
735 /* make sure nbits field has expected value */
736 if (copy_from_user(&link_ksettings.base, useraddr,
737 sizeof(link_ksettings.base)))
740 if (__ETHTOOL_LINK_MODE_MASK_NU32
741 != link_ksettings.base.link_mode_masks_nwords)
744 /* copy the whole structure, now that we know it has expected
747 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
751 /* re-check nwords field, just in case */
752 if (__ETHTOOL_LINK_MODE_MASK_NU32
753 != link_ksettings.base.link_mode_masks_nwords)
756 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
760 warn_incomplete_ethtool_legacy_settings_conversion(const char *details)
762 char name[sizeof(current->comm)];
764 pr_info_once("warning: `%s' uses legacy ethtool link settings API, %s\n",
765 get_task_comm(name, current), details);
768 /* Query device for its ethtool_cmd settings.
770 * Backward compatibility note: for compatibility with legacy ethtool,
771 * this has to work with both drivers implementing get_link_ksettings
772 * API and drivers implementing get_settings API. When drivers
773 * implement get_link_ksettings and report higher link mode bits, a
774 * kernel warning is logged once (with name of 1st driver/device) to
775 * recommend user to upgrade ethtool, but the command is successful
776 * (only the lower link mode bits reported back to user).
778 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
780 struct ethtool_cmd cmd;
784 if (dev->ethtool_ops->get_link_ksettings) {
785 /* First, use link_ksettings API if it is supported */
787 struct ethtool_link_ksettings link_ksettings;
789 memset(&link_ksettings, 0, sizeof(link_ksettings));
790 err = dev->ethtool_ops->get_link_ksettings(dev,
794 if (!convert_link_ksettings_to_legacy_settings(&cmd,
796 warn_incomplete_ethtool_legacy_settings_conversion(
797 "link modes are only partially reported");
799 /* send a sensible cmd tag back to user */
800 cmd.cmd = ETHTOOL_GSET;
802 /* driver doesn't support %ethtool_link_ksettings
803 * API. revert to legacy %ethtool_cmd API, unless it's
804 * not supported either.
808 if (!dev->ethtool_ops->get_settings)
811 memset(&cmd, 0, sizeof(cmd));
812 cmd.cmd = ETHTOOL_GSET;
813 err = dev->ethtool_ops->get_settings(dev, &cmd);
818 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
824 /* Update device link settings with given ethtool_cmd.
826 * Backward compatibility note: for compatibility with legacy ethtool,
827 * this has to work with both drivers implementing set_link_ksettings
828 * API and drivers implementing set_settings API. When drivers
829 * implement set_link_ksettings and user's request updates deprecated
830 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
831 * warning is logged once (with name of 1st driver/device) to
832 * recommend user to upgrade ethtool, and the request is rejected.
834 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
836 struct ethtool_cmd cmd;
840 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
843 /* first, try new %ethtool_link_ksettings API. */
844 if (dev->ethtool_ops->set_link_ksettings) {
845 struct ethtool_link_ksettings link_ksettings;
847 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
851 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
852 link_ksettings.base.link_mode_masks_nwords
853 = __ETHTOOL_LINK_MODE_MASK_NU32;
854 return dev->ethtool_ops->set_link_ksettings(dev,
858 /* legacy %ethtool_cmd API */
860 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
861 * disappears internally
864 if (!dev->ethtool_ops->set_settings)
867 return dev->ethtool_ops->set_settings(dev, &cmd);
870 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
871 void __user *useraddr)
873 struct ethtool_drvinfo info;
874 const struct ethtool_ops *ops = dev->ethtool_ops;
876 memset(&info, 0, sizeof(info));
877 info.cmd = ETHTOOL_GDRVINFO;
878 if (ops->get_drvinfo) {
879 ops->get_drvinfo(dev, &info);
880 } else if (dev->dev.parent && dev->dev.parent->driver) {
881 strlcpy(info.bus_info, dev_name(dev->dev.parent),
882 sizeof(info.bus_info));
883 strlcpy(info.driver, dev->dev.parent->driver->name,
884 sizeof(info.driver));
890 * this method of obtaining string set info is deprecated;
891 * Use ETHTOOL_GSSET_INFO instead.
893 if (ops->get_sset_count) {
896 rc = ops->get_sset_count(dev, ETH_SS_TEST);
898 info.testinfo_len = rc;
899 rc = ops->get_sset_count(dev, ETH_SS_STATS);
902 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
904 info.n_priv_flags = rc;
906 if (ops->get_regs_len)
907 info.regdump_len = ops->get_regs_len(dev);
908 if (ops->get_eeprom_len)
909 info.eedump_len = ops->get_eeprom_len(dev);
911 if (copy_to_user(useraddr, &info, sizeof(info)))
916 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
917 void __user *useraddr)
919 struct ethtool_sset_info info;
921 int i, idx = 0, n_bits = 0, ret, rc;
922 u32 *info_buf = NULL;
924 if (copy_from_user(&info, useraddr, sizeof(info)))
927 /* store copy of mask, because we zero struct later on */
928 sset_mask = info.sset_mask;
932 /* calculate size of return buffer */
933 n_bits = hweight64(sset_mask);
935 memset(&info, 0, sizeof(info));
936 info.cmd = ETHTOOL_GSSET_INFO;
938 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
943 * fill return buffer based on input bitmask and successful
944 * get_sset_count return
946 for (i = 0; i < 64; i++) {
947 if (!(sset_mask & (1ULL << i)))
950 rc = __ethtool_get_sset_count(dev, i);
952 info.sset_mask |= (1ULL << i);
953 info_buf[idx++] = rc;
958 if (copy_to_user(useraddr, &info, sizeof(info)))
961 useraddr += offsetof(struct ethtool_sset_info, data);
962 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
972 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
973 u32 cmd, void __user *useraddr)
975 struct ethtool_rxnfc info;
976 size_t info_size = sizeof(info);
979 if (!dev->ethtool_ops->set_rxnfc)
982 /* struct ethtool_rxnfc was originally defined for
983 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
984 * members. User-space might still be using that
986 if (cmd == ETHTOOL_SRXFH)
987 info_size = (offsetof(struct ethtool_rxnfc, data) +
990 if (copy_from_user(&info, useraddr, info_size))
993 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
997 if (cmd == ETHTOOL_SRXCLSRLINS &&
998 copy_to_user(useraddr, &info, info_size))
1004 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
1005 u32 cmd, void __user *useraddr)
1007 struct ethtool_rxnfc info;
1008 size_t info_size = sizeof(info);
1009 const struct ethtool_ops *ops = dev->ethtool_ops;
1011 void *rule_buf = NULL;
1013 if (!ops->get_rxnfc)
1016 /* struct ethtool_rxnfc was originally defined for
1017 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1018 * members. User-space might still be using that
1020 if (cmd == ETHTOOL_GRXFH)
1021 info_size = (offsetof(struct ethtool_rxnfc, data) +
1024 if (copy_from_user(&info, useraddr, info_size))
1027 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1028 if (info.rule_cnt > 0) {
1029 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1030 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
1037 ret = ops->get_rxnfc(dev, &info, rule_buf);
1042 if (copy_to_user(useraddr, &info, info_size))
1046 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1047 if (copy_to_user(useraddr, rule_buf,
1048 info.rule_cnt * sizeof(u32)))
1059 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1060 struct ethtool_rxnfc *rx_rings,
1065 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1068 /* Validate ring indices */
1069 for (i = 0; i < size; i++)
1070 if (indir[i] >= rx_rings->data)
1076 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1078 void netdev_rss_key_fill(void *buffer, size_t len)
1080 BUG_ON(len > sizeof(netdev_rss_key));
1081 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1082 memcpy(buffer, netdev_rss_key, len);
1084 EXPORT_SYMBOL(netdev_rss_key_fill);
1086 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1088 u32 dev_size, current_max = 0;
1092 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1093 !dev->ethtool_ops->get_rxfh)
1095 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1099 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1103 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1108 current_max = max(current_max, indir[dev_size]);
1117 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1118 void __user *useraddr)
1120 u32 user_size, dev_size;
1124 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1125 !dev->ethtool_ops->get_rxfh)
1127 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1131 if (copy_from_user(&user_size,
1132 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1136 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1137 &dev_size, sizeof(dev_size)))
1140 /* If the user buffer size is 0, this is just a query for the
1141 * device table size. Otherwise, if it's smaller than the
1142 * device table size it's an error.
1144 if (user_size < dev_size)
1145 return user_size == 0 ? 0 : -EINVAL;
1147 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1151 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1155 if (copy_to_user(useraddr +
1156 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1157 indir, dev_size * sizeof(indir[0])))
1165 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1166 void __user *useraddr)
1168 struct ethtool_rxnfc rx_rings;
1169 u32 user_size, dev_size, i;
1171 const struct ethtool_ops *ops = dev->ethtool_ops;
1173 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1175 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1179 dev_size = ops->get_rxfh_indir_size(dev);
1183 if (copy_from_user(&user_size,
1184 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1188 if (user_size != 0 && user_size != dev_size)
1191 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1195 rx_rings.cmd = ETHTOOL_GRXRINGS;
1196 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1200 if (user_size == 0) {
1201 for (i = 0; i < dev_size; i++)
1202 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1204 ret = ethtool_copy_validate_indir(indir,
1205 useraddr + ringidx_offset,
1212 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1216 /* indicate whether rxfh was set to default */
1218 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1220 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1227 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1228 void __user *useraddr)
1231 const struct ethtool_ops *ops = dev->ethtool_ops;
1232 u32 user_indir_size, user_key_size;
1233 u32 dev_indir_size = 0, dev_key_size = 0;
1234 struct ethtool_rxfh rxfh;
1245 if (ops->get_rxfh_indir_size)
1246 dev_indir_size = ops->get_rxfh_indir_size(dev);
1247 if (ops->get_rxfh_key_size)
1248 dev_key_size = ops->get_rxfh_key_size(dev);
1250 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1252 user_indir_size = rxfh.indir_size;
1253 user_key_size = rxfh.key_size;
1255 /* Check that reserved fields are 0 for now */
1256 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1257 rxfh.rsvd8[2] || rxfh.rsvd32)
1260 rxfh.indir_size = dev_indir_size;
1261 rxfh.key_size = dev_key_size;
1262 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1265 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1266 (user_key_size && (user_key_size != dev_key_size)))
1269 indir_bytes = user_indir_size * sizeof(indir[0]);
1270 total_size = indir_bytes + user_key_size;
1271 rss_config = kzalloc(total_size, GFP_USER);
1275 if (user_indir_size)
1276 indir = (u32 *)rss_config;
1279 hkey = rss_config + indir_bytes;
1281 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1285 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1286 &dev_hfunc, sizeof(rxfh.hfunc))) {
1288 } else if (copy_to_user(useraddr +
1289 offsetof(struct ethtool_rxfh, rss_config[0]),
1290 rss_config, total_size)) {
1299 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1300 void __user *useraddr)
1303 const struct ethtool_ops *ops = dev->ethtool_ops;
1304 struct ethtool_rxnfc rx_rings;
1305 struct ethtool_rxfh rxfh;
1306 u32 dev_indir_size = 0, dev_key_size = 0, i;
1307 u32 *indir = NULL, indir_bytes = 0;
1310 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1312 if (!ops->get_rxnfc || !ops->set_rxfh)
1315 if (ops->get_rxfh_indir_size)
1316 dev_indir_size = ops->get_rxfh_indir_size(dev);
1317 if (ops->get_rxfh_key_size)
1318 dev_key_size = ops->get_rxfh_key_size(dev);
1320 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1323 /* Check that reserved fields are 0 for now */
1324 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1325 rxfh.rsvd8[2] || rxfh.rsvd32)
1328 /* If either indir, hash key or function is valid, proceed further.
1329 * Must request at least one change: indir size, hash key or function.
1331 if ((rxfh.indir_size &&
1332 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1333 rxfh.indir_size != dev_indir_size) ||
1334 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1335 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1336 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1339 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1340 indir_bytes = dev_indir_size * sizeof(indir[0]);
1342 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1346 rx_rings.cmd = ETHTOOL_GRXRINGS;
1347 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1351 /* rxfh.indir_size == 0 means reset the indir table to default.
1352 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1354 if (rxfh.indir_size &&
1355 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1356 indir = (u32 *)rss_config;
1357 ret = ethtool_copy_validate_indir(indir,
1358 useraddr + rss_cfg_offset,
1363 } else if (rxfh.indir_size == 0) {
1364 indir = (u32 *)rss_config;
1365 for (i = 0; i < dev_indir_size; i++)
1366 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1369 if (rxfh.key_size) {
1370 hkey = rss_config + indir_bytes;
1371 if (copy_from_user(hkey,
1372 useraddr + rss_cfg_offset + indir_bytes,
1379 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1383 /* indicate whether rxfh was set to default */
1384 if (rxfh.indir_size == 0)
1385 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1386 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1387 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1394 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1396 struct ethtool_regs regs;
1397 const struct ethtool_ops *ops = dev->ethtool_ops;
1401 if (!ops->get_regs || !ops->get_regs_len)
1404 if (copy_from_user(®s, useraddr, sizeof(regs)))
1407 reglen = ops->get_regs_len(dev);
1408 if (regs.len > reglen)
1413 regbuf = vzalloc(reglen);
1418 ops->get_regs(dev, ®s, regbuf);
1421 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1423 useraddr += offsetof(struct ethtool_regs, data);
1424 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1433 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1435 struct ethtool_value reset;
1438 if (!dev->ethtool_ops->reset)
1441 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1444 ret = dev->ethtool_ops->reset(dev, &reset.data);
1448 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1453 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1455 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1457 if (!dev->ethtool_ops->get_wol)
1460 dev->ethtool_ops->get_wol(dev, &wol);
1462 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1467 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1469 struct ethtool_wolinfo wol;
1471 if (!dev->ethtool_ops->set_wol)
1474 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1477 return dev->ethtool_ops->set_wol(dev, &wol);
1480 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1482 struct ethtool_eee edata;
1485 if (!dev->ethtool_ops->get_eee)
1488 memset(&edata, 0, sizeof(struct ethtool_eee));
1489 edata.cmd = ETHTOOL_GEEE;
1490 rc = dev->ethtool_ops->get_eee(dev, &edata);
1495 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1501 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1503 struct ethtool_eee edata;
1505 if (!dev->ethtool_ops->set_eee)
1508 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1511 return dev->ethtool_ops->set_eee(dev, &edata);
1514 static int ethtool_nway_reset(struct net_device *dev)
1516 if (!dev->ethtool_ops->nway_reset)
1519 return dev->ethtool_ops->nway_reset(dev);
1522 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1524 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1526 if (!dev->ethtool_ops->get_link)
1529 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1531 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1536 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1537 int (*getter)(struct net_device *,
1538 struct ethtool_eeprom *, u8 *),
1541 struct ethtool_eeprom eeprom;
1542 void __user *userbuf = useraddr + sizeof(eeprom);
1543 u32 bytes_remaining;
1547 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1550 /* Check for wrap and zero */
1551 if (eeprom.offset + eeprom.len <= eeprom.offset)
1554 /* Check for exceeding total eeprom len */
1555 if (eeprom.offset + eeprom.len > total_len)
1558 data = kmalloc(PAGE_SIZE, GFP_USER);
1562 bytes_remaining = eeprom.len;
1563 while (bytes_remaining > 0) {
1564 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1566 ret = getter(dev, &eeprom, data);
1569 if (copy_to_user(userbuf, data, eeprom.len)) {
1573 userbuf += eeprom.len;
1574 eeprom.offset += eeprom.len;
1575 bytes_remaining -= eeprom.len;
1578 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1579 eeprom.offset -= eeprom.len;
1580 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1587 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1589 const struct ethtool_ops *ops = dev->ethtool_ops;
1591 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1592 !ops->get_eeprom_len(dev))
1595 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1596 ops->get_eeprom_len(dev));
1599 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1601 struct ethtool_eeprom eeprom;
1602 const struct ethtool_ops *ops = dev->ethtool_ops;
1603 void __user *userbuf = useraddr + sizeof(eeprom);
1604 u32 bytes_remaining;
1608 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1609 !ops->get_eeprom_len(dev))
1612 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1615 /* Check for wrap and zero */
1616 if (eeprom.offset + eeprom.len <= eeprom.offset)
1619 /* Check for exceeding total eeprom len */
1620 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1623 data = kmalloc(PAGE_SIZE, GFP_USER);
1627 bytes_remaining = eeprom.len;
1628 while (bytes_remaining > 0) {
1629 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1631 if (copy_from_user(data, userbuf, eeprom.len)) {
1635 ret = ops->set_eeprom(dev, &eeprom, data);
1638 userbuf += eeprom.len;
1639 eeprom.offset += eeprom.len;
1640 bytes_remaining -= eeprom.len;
1647 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1648 void __user *useraddr)
1650 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1652 if (!dev->ethtool_ops->get_coalesce)
1655 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1657 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1662 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1663 void __user *useraddr)
1665 struct ethtool_coalesce coalesce;
1667 if (!dev->ethtool_ops->set_coalesce)
1670 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1673 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1676 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1678 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1680 if (!dev->ethtool_ops->get_ringparam)
1683 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1685 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1690 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1692 struct ethtool_ringparam ringparam;
1694 if (!dev->ethtool_ops->set_ringparam)
1697 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1700 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1703 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1704 void __user *useraddr)
1706 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1708 if (!dev->ethtool_ops->get_channels)
1711 dev->ethtool_ops->get_channels(dev, &channels);
1713 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1718 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1719 void __user *useraddr)
1721 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
1722 u32 max_rx_in_use = 0;
1724 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1727 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1730 dev->ethtool_ops->get_channels(dev, &max);
1732 /* ensure new counts are within the maximums */
1733 if ((channels.rx_count > max.max_rx) ||
1734 (channels.tx_count > max.max_tx) ||
1735 (channels.combined_count > max.max_combined) ||
1736 (channels.other_count > max.max_other))
1739 /* ensure the new Rx count fits within the configured Rx flow
1740 * indirection table settings */
1741 if (netif_is_rxfh_configured(dev) &&
1742 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1743 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1746 return dev->ethtool_ops->set_channels(dev, &channels);
1749 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1751 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1753 if (!dev->ethtool_ops->get_pauseparam)
1756 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1758 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1763 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1765 struct ethtool_pauseparam pauseparam;
1767 if (!dev->ethtool_ops->set_pauseparam)
1770 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1773 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1776 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1778 struct ethtool_test test;
1779 const struct ethtool_ops *ops = dev->ethtool_ops;
1783 if (!ops->self_test || !ops->get_sset_count)
1786 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1789 WARN_ON(test_len == 0);
1791 if (copy_from_user(&test, useraddr, sizeof(test)))
1794 test.len = test_len;
1795 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1799 ops->self_test(dev, &test, data);
1802 if (copy_to_user(useraddr, &test, sizeof(test)))
1804 useraddr += sizeof(test);
1805 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1814 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1816 struct ethtool_gstrings gstrings;
1820 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1823 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1826 if (ret > S32_MAX / ETH_GSTRING_LEN)
1831 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1832 if (gstrings.len && !data)
1835 __ethtool_get_strings(dev, gstrings.string_set, data);
1838 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1840 useraddr += sizeof(gstrings);
1842 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1851 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1853 struct ethtool_value id;
1855 const struct ethtool_ops *ops = dev->ethtool_ops;
1858 if (!ops->set_phys_id)
1864 if (copy_from_user(&id, useraddr, sizeof(id)))
1867 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1871 /* Drop the RTNL lock while waiting, but prevent reentry or
1872 * removal of the device.
1879 /* Driver will handle this itself */
1880 schedule_timeout_interruptible(
1881 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1883 /* Driver expects to be called at twice the frequency in rc */
1884 int n = rc * 2, i, interval = HZ / n;
1886 /* Count down seconds */
1888 /* Count down iterations per second */
1892 rc = ops->set_phys_id(dev,
1893 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1897 schedule_timeout_interruptible(interval);
1898 } while (!signal_pending(current) && --i != 0);
1899 } while (!signal_pending(current) &&
1900 (id.data == 0 || --id.data != 0));
1907 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1911 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1913 struct ethtool_stats stats;
1914 const struct ethtool_ops *ops = dev->ethtool_ops;
1918 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1921 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1924 if (n_stats > S32_MAX / sizeof(u64))
1926 WARN_ON_ONCE(!n_stats);
1927 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1930 stats.n_stats = n_stats;
1931 data = vzalloc(n_stats * sizeof(u64));
1932 if (n_stats && !data)
1935 ops->get_ethtool_stats(dev, &stats, data);
1938 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1940 useraddr += sizeof(stats);
1941 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1950 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1952 struct ethtool_stats stats;
1953 struct phy_device *phydev = dev->phydev;
1960 n_stats = phy_get_sset_count(phydev);
1963 if (n_stats > S32_MAX / sizeof(u64))
1965 WARN_ON_ONCE(!n_stats);
1967 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1970 stats.n_stats = n_stats;
1971 data = vzalloc(n_stats * sizeof(u64));
1972 if (n_stats && !data)
1975 mutex_lock(&phydev->lock);
1976 phydev->drv->get_stats(phydev, &stats, data);
1977 mutex_unlock(&phydev->lock);
1980 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1982 useraddr += sizeof(stats);
1983 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1992 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
1994 struct ethtool_perm_addr epaddr;
1996 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
1999 if (epaddr.size < dev->addr_len)
2001 epaddr.size = dev->addr_len;
2003 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2005 useraddr += sizeof(epaddr);
2006 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2011 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2012 u32 cmd, u32 (*actor)(struct net_device *))
2014 struct ethtool_value edata = { .cmd = cmd };
2019 edata.data = actor(dev);
2021 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2026 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2027 void (*actor)(struct net_device *, u32))
2029 struct ethtool_value edata;
2034 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2037 actor(dev, edata.data);
2041 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2042 int (*actor)(struct net_device *, u32))
2044 struct ethtool_value edata;
2049 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2052 return actor(dev, edata.data);
2055 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2056 char __user *useraddr)
2058 struct ethtool_flash efl;
2060 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2063 if (!dev->ethtool_ops->flash_device)
2066 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2068 return dev->ethtool_ops->flash_device(dev, &efl);
2071 static int ethtool_set_dump(struct net_device *dev,
2072 void __user *useraddr)
2074 struct ethtool_dump dump;
2076 if (!dev->ethtool_ops->set_dump)
2079 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2082 return dev->ethtool_ops->set_dump(dev, &dump);
2085 static int ethtool_get_dump_flag(struct net_device *dev,
2086 void __user *useraddr)
2089 struct ethtool_dump dump;
2090 const struct ethtool_ops *ops = dev->ethtool_ops;
2092 if (!ops->get_dump_flag)
2095 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2098 ret = ops->get_dump_flag(dev, &dump);
2102 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2107 static int ethtool_get_dump_data(struct net_device *dev,
2108 void __user *useraddr)
2112 struct ethtool_dump dump, tmp;
2113 const struct ethtool_ops *ops = dev->ethtool_ops;
2116 if (!ops->get_dump_data || !ops->get_dump_flag)
2119 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2122 memset(&tmp, 0, sizeof(tmp));
2123 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2124 ret = ops->get_dump_flag(dev, &tmp);
2128 len = min(tmp.len, dump.len);
2132 /* Don't ever let the driver think there's more space available
2133 * than it requested with .get_dump_flag().
2137 /* Always allocate enough space to hold the whole thing so that the
2138 * driver does not need to check the length and bother with partial
2141 data = vzalloc(tmp.len);
2144 ret = ops->get_dump_data(dev, &dump, data);
2148 /* There are two sane possibilities:
2149 * 1. The driver's .get_dump_data() does not touch dump.len.
2150 * 2. Or it may set dump.len to how much it really writes, which
2151 * should be tmp.len (or len if it can do a partial dump).
2152 * In any case respond to userspace with the actual length of data
2155 WARN_ON(dump.len != len && dump.len != tmp.len);
2158 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2162 useraddr += offsetof(struct ethtool_dump, data);
2163 if (copy_to_user(useraddr, data, len))
2170 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2173 struct ethtool_ts_info info;
2174 const struct ethtool_ops *ops = dev->ethtool_ops;
2175 struct phy_device *phydev = dev->phydev;
2177 memset(&info, 0, sizeof(info));
2178 info.cmd = ETHTOOL_GET_TS_INFO;
2180 if (phydev && phydev->drv && phydev->drv->ts_info) {
2181 err = phydev->drv->ts_info(phydev, &info);
2182 } else if (ops->get_ts_info) {
2183 err = ops->get_ts_info(dev, &info);
2185 info.so_timestamping =
2186 SOF_TIMESTAMPING_RX_SOFTWARE |
2187 SOF_TIMESTAMPING_SOFTWARE;
2188 info.phc_index = -1;
2194 if (copy_to_user(useraddr, &info, sizeof(info)))
2200 static int __ethtool_get_module_info(struct net_device *dev,
2201 struct ethtool_modinfo *modinfo)
2203 const struct ethtool_ops *ops = dev->ethtool_ops;
2204 struct phy_device *phydev = dev->phydev;
2206 if (phydev && phydev->drv && phydev->drv->module_info)
2207 return phydev->drv->module_info(phydev, modinfo);
2209 if (ops->get_module_info)
2210 return ops->get_module_info(dev, modinfo);
2215 static int ethtool_get_module_info(struct net_device *dev,
2216 void __user *useraddr)
2219 struct ethtool_modinfo modinfo;
2221 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2224 ret = __ethtool_get_module_info(dev, &modinfo);
2228 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2234 static int __ethtool_get_module_eeprom(struct net_device *dev,
2235 struct ethtool_eeprom *ee, u8 *data)
2237 const struct ethtool_ops *ops = dev->ethtool_ops;
2238 struct phy_device *phydev = dev->phydev;
2240 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2241 return phydev->drv->module_eeprom(phydev, ee, data);
2243 if (ops->get_module_eeprom)
2244 return ops->get_module_eeprom(dev, ee, data);
2249 static int ethtool_get_module_eeprom(struct net_device *dev,
2250 void __user *useraddr)
2253 struct ethtool_modinfo modinfo;
2255 ret = __ethtool_get_module_info(dev, &modinfo);
2259 return ethtool_get_any_eeprom(dev, useraddr,
2260 __ethtool_get_module_eeprom,
2261 modinfo.eeprom_len);
2264 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2267 case ETHTOOL_RX_COPYBREAK:
2268 case ETHTOOL_TX_COPYBREAK:
2269 if (tuna->len != sizeof(u32) ||
2270 tuna->type_id != ETHTOOL_TUNABLE_U32)
2280 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2283 struct ethtool_tunable tuna;
2284 const struct ethtool_ops *ops = dev->ethtool_ops;
2287 if (!ops->get_tunable)
2289 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2291 ret = ethtool_tunable_valid(&tuna);
2294 data = kmalloc(tuna.len, GFP_USER);
2297 ret = ops->get_tunable(dev, &tuna, data);
2300 useraddr += sizeof(tuna);
2302 if (copy_to_user(useraddr, data, tuna.len))
2311 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2314 struct ethtool_tunable tuna;
2315 const struct ethtool_ops *ops = dev->ethtool_ops;
2318 if (!ops->set_tunable)
2320 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2322 ret = ethtool_tunable_valid(&tuna);
2325 data = kmalloc(tuna.len, GFP_USER);
2328 useraddr += sizeof(tuna);
2330 if (copy_from_user(data, useraddr, tuna.len))
2332 ret = ops->set_tunable(dev, &tuna, data);
2339 static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2340 void __user *useraddr,
2341 struct ethtool_per_queue_op *per_queue_opt)
2345 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2347 if (!dev->ethtool_ops->get_per_queue_coalesce)
2350 useraddr += sizeof(*per_queue_opt);
2352 bitmap_from_u32array(queue_mask,
2354 per_queue_opt->queue_mask,
2355 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2357 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2358 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2360 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2363 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2365 useraddr += sizeof(coalesce);
2371 static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2372 void __user *useraddr,
2373 struct ethtool_per_queue_op *per_queue_opt)
2378 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2379 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2381 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2382 (!dev->ethtool_ops->get_per_queue_coalesce))
2385 useraddr += sizeof(*per_queue_opt);
2387 bitmap_from_u32array(queue_mask,
2389 per_queue_opt->queue_mask,
2390 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2391 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2392 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2396 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2397 struct ethtool_coalesce coalesce;
2399 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2405 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2410 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2414 useraddr += sizeof(coalesce);
2420 for_each_set_bit(i, queue_mask, bit) {
2421 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2430 static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2432 struct ethtool_per_queue_op per_queue_opt;
2434 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2437 switch (per_queue_opt.sub_command) {
2438 case ETHTOOL_GCOALESCE:
2439 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2440 case ETHTOOL_SCOALESCE:
2441 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2447 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2450 case ETHTOOL_PHY_DOWNSHIFT:
2451 if (tuna->len != sizeof(u8) ||
2452 tuna->type_id != ETHTOOL_TUNABLE_U8)
2462 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2465 struct ethtool_tunable tuna;
2466 struct phy_device *phydev = dev->phydev;
2469 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2472 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2474 ret = ethtool_phy_tunable_valid(&tuna);
2477 data = kmalloc(tuna.len, GFP_USER);
2480 mutex_lock(&phydev->lock);
2481 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2482 mutex_unlock(&phydev->lock);
2485 useraddr += sizeof(tuna);
2487 if (copy_to_user(useraddr, data, tuna.len))
2496 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2499 struct ethtool_tunable tuna;
2500 struct phy_device *phydev = dev->phydev;
2503 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2505 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2507 ret = ethtool_phy_tunable_valid(&tuna);
2510 data = kmalloc(tuna.len, GFP_USER);
2513 useraddr += sizeof(tuna);
2515 if (copy_from_user(data, useraddr, tuna.len))
2517 mutex_lock(&phydev->lock);
2518 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2519 mutex_unlock(&phydev->lock);
2526 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2528 int dev_ethtool(struct net *net, struct ifreq *ifr)
2530 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2531 void __user *useraddr = ifr->ifr_data;
2532 u32 ethcmd, sub_cmd;
2534 netdev_features_t old_features;
2536 if (!dev || !netif_device_present(dev))
2539 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2542 if (ethcmd == ETHTOOL_PERQUEUE) {
2543 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2548 /* Allow some commands to be done by anyone */
2551 case ETHTOOL_GDRVINFO:
2552 case ETHTOOL_GMSGLVL:
2554 case ETHTOOL_GCOALESCE:
2555 case ETHTOOL_GRINGPARAM:
2556 case ETHTOOL_GPAUSEPARAM:
2557 case ETHTOOL_GRXCSUM:
2558 case ETHTOOL_GTXCSUM:
2560 case ETHTOOL_GSSET_INFO:
2561 case ETHTOOL_GSTRINGS:
2562 case ETHTOOL_GSTATS:
2563 case ETHTOOL_GPHYSTATS:
2565 case ETHTOOL_GPERMADDR:
2569 case ETHTOOL_GFLAGS:
2570 case ETHTOOL_GPFLAGS:
2572 case ETHTOOL_GRXRINGS:
2573 case ETHTOOL_GRXCLSRLCNT:
2574 case ETHTOOL_GRXCLSRULE:
2575 case ETHTOOL_GRXCLSRLALL:
2576 case ETHTOOL_GRXFHINDIR:
2578 case ETHTOOL_GFEATURES:
2579 case ETHTOOL_GCHANNELS:
2580 case ETHTOOL_GET_TS_INFO:
2582 case ETHTOOL_GTUNABLE:
2583 case ETHTOOL_PHY_GTUNABLE:
2584 case ETHTOOL_GLINKSETTINGS:
2587 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2591 if (dev->ethtool_ops->begin) {
2592 rc = dev->ethtool_ops->begin(dev);
2596 old_features = dev->features;
2600 rc = ethtool_get_settings(dev, useraddr);
2603 rc = ethtool_set_settings(dev, useraddr);
2605 case ETHTOOL_GDRVINFO:
2606 rc = ethtool_get_drvinfo(dev, useraddr);
2609 rc = ethtool_get_regs(dev, useraddr);
2612 rc = ethtool_get_wol(dev, useraddr);
2615 rc = ethtool_set_wol(dev, useraddr);
2617 case ETHTOOL_GMSGLVL:
2618 rc = ethtool_get_value(dev, useraddr, ethcmd,
2619 dev->ethtool_ops->get_msglevel);
2621 case ETHTOOL_SMSGLVL:
2622 rc = ethtool_set_value_void(dev, useraddr,
2623 dev->ethtool_ops->set_msglevel);
2626 rc = ethtool_get_eee(dev, useraddr);
2629 rc = ethtool_set_eee(dev, useraddr);
2631 case ETHTOOL_NWAY_RST:
2632 rc = ethtool_nway_reset(dev);
2635 rc = ethtool_get_link(dev, useraddr);
2637 case ETHTOOL_GEEPROM:
2638 rc = ethtool_get_eeprom(dev, useraddr);
2640 case ETHTOOL_SEEPROM:
2641 rc = ethtool_set_eeprom(dev, useraddr);
2643 case ETHTOOL_GCOALESCE:
2644 rc = ethtool_get_coalesce(dev, useraddr);
2646 case ETHTOOL_SCOALESCE:
2647 rc = ethtool_set_coalesce(dev, useraddr);
2649 case ETHTOOL_GRINGPARAM:
2650 rc = ethtool_get_ringparam(dev, useraddr);
2652 case ETHTOOL_SRINGPARAM:
2653 rc = ethtool_set_ringparam(dev, useraddr);
2655 case ETHTOOL_GPAUSEPARAM:
2656 rc = ethtool_get_pauseparam(dev, useraddr);
2658 case ETHTOOL_SPAUSEPARAM:
2659 rc = ethtool_set_pauseparam(dev, useraddr);
2662 rc = ethtool_self_test(dev, useraddr);
2664 case ETHTOOL_GSTRINGS:
2665 rc = ethtool_get_strings(dev, useraddr);
2667 case ETHTOOL_PHYS_ID:
2668 rc = ethtool_phys_id(dev, useraddr);
2670 case ETHTOOL_GSTATS:
2671 rc = ethtool_get_stats(dev, useraddr);
2673 case ETHTOOL_GPERMADDR:
2674 rc = ethtool_get_perm_addr(dev, useraddr);
2676 case ETHTOOL_GFLAGS:
2677 rc = ethtool_get_value(dev, useraddr, ethcmd,
2678 __ethtool_get_flags);
2680 case ETHTOOL_SFLAGS:
2681 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2683 case ETHTOOL_GPFLAGS:
2684 rc = ethtool_get_value(dev, useraddr, ethcmd,
2685 dev->ethtool_ops->get_priv_flags);
2687 case ETHTOOL_SPFLAGS:
2688 rc = ethtool_set_value(dev, useraddr,
2689 dev->ethtool_ops->set_priv_flags);
2692 case ETHTOOL_GRXRINGS:
2693 case ETHTOOL_GRXCLSRLCNT:
2694 case ETHTOOL_GRXCLSRULE:
2695 case ETHTOOL_GRXCLSRLALL:
2696 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2699 case ETHTOOL_SRXCLSRLDEL:
2700 case ETHTOOL_SRXCLSRLINS:
2701 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2703 case ETHTOOL_FLASHDEV:
2704 rc = ethtool_flash_device(dev, useraddr);
2707 rc = ethtool_reset(dev, useraddr);
2709 case ETHTOOL_GSSET_INFO:
2710 rc = ethtool_get_sset_info(dev, useraddr);
2712 case ETHTOOL_GRXFHINDIR:
2713 rc = ethtool_get_rxfh_indir(dev, useraddr);
2715 case ETHTOOL_SRXFHINDIR:
2716 rc = ethtool_set_rxfh_indir(dev, useraddr);
2719 rc = ethtool_get_rxfh(dev, useraddr);
2722 rc = ethtool_set_rxfh(dev, useraddr);
2724 case ETHTOOL_GFEATURES:
2725 rc = ethtool_get_features(dev, useraddr);
2727 case ETHTOOL_SFEATURES:
2728 rc = ethtool_set_features(dev, useraddr);
2730 case ETHTOOL_GTXCSUM:
2731 case ETHTOOL_GRXCSUM:
2737 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2739 case ETHTOOL_STXCSUM:
2740 case ETHTOOL_SRXCSUM:
2746 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2748 case ETHTOOL_GCHANNELS:
2749 rc = ethtool_get_channels(dev, useraddr);
2751 case ETHTOOL_SCHANNELS:
2752 rc = ethtool_set_channels(dev, useraddr);
2754 case ETHTOOL_SET_DUMP:
2755 rc = ethtool_set_dump(dev, useraddr);
2757 case ETHTOOL_GET_DUMP_FLAG:
2758 rc = ethtool_get_dump_flag(dev, useraddr);
2760 case ETHTOOL_GET_DUMP_DATA:
2761 rc = ethtool_get_dump_data(dev, useraddr);
2763 case ETHTOOL_GET_TS_INFO:
2764 rc = ethtool_get_ts_info(dev, useraddr);
2766 case ETHTOOL_GMODULEINFO:
2767 rc = ethtool_get_module_info(dev, useraddr);
2769 case ETHTOOL_GMODULEEEPROM:
2770 rc = ethtool_get_module_eeprom(dev, useraddr);
2772 case ETHTOOL_GTUNABLE:
2773 rc = ethtool_get_tunable(dev, useraddr);
2775 case ETHTOOL_STUNABLE:
2776 rc = ethtool_set_tunable(dev, useraddr);
2778 case ETHTOOL_GPHYSTATS:
2779 rc = ethtool_get_phy_stats(dev, useraddr);
2781 case ETHTOOL_PERQUEUE:
2782 rc = ethtool_set_per_queue(dev, useraddr);
2784 case ETHTOOL_GLINKSETTINGS:
2785 rc = ethtool_get_link_ksettings(dev, useraddr);
2787 case ETHTOOL_SLINKSETTINGS:
2788 rc = ethtool_set_link_ksettings(dev, useraddr);
2790 case ETHTOOL_PHY_GTUNABLE:
2791 rc = get_phy_tunable(dev, useraddr);
2793 case ETHTOOL_PHY_STUNABLE:
2794 rc = set_phy_tunable(dev, useraddr);
2800 if (dev->ethtool_ops->complete)
2801 dev->ethtool_ops->complete(dev);
2803 if (old_features != dev->features)
2804 netdev_features_change(dev);