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_GRO_HW_BIT] = "rx-gro-hw",
77 [NETIF_F_LRO_BIT] = "rx-lro",
79 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
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",
109 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
113 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
114 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
115 [ETH_RSS_HASH_XOR_BIT] = "xor",
116 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
120 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
121 [ETHTOOL_ID_UNSPEC] = "Unspec",
122 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
123 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
124 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
128 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
129 [ETHTOOL_ID_UNSPEC] = "Unspec",
130 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
133 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
135 struct ethtool_gfeatures cmd = {
136 .cmd = ETHTOOL_GFEATURES,
137 .size = ETHTOOL_DEV_FEATURE_WORDS,
139 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
140 u32 __user *sizeaddr;
144 /* in case feature bits run out again */
145 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
147 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
148 features[i].available = (u32)(dev->hw_features >> (32 * i));
149 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
150 features[i].active = (u32)(dev->features >> (32 * i));
151 features[i].never_changed =
152 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
155 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
156 if (get_user(copy_size, sizeaddr))
159 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
160 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
162 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
164 useraddr += sizeof(cmd);
165 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
171 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
173 struct ethtool_sfeatures cmd;
174 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
175 netdev_features_t wanted = 0, valid = 0;
178 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
180 useraddr += sizeof(cmd);
182 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
185 if (copy_from_user(features, useraddr, sizeof(features)))
188 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
189 valid |= (netdev_features_t)features[i].valid << (32 * i);
190 wanted |= (netdev_features_t)features[i].requested << (32 * i);
193 if (valid & ~NETIF_F_ETHTOOL_BITS)
196 if (valid & ~dev->hw_features) {
197 valid &= dev->hw_features;
198 ret |= ETHTOOL_F_UNSUPPORTED;
201 dev->wanted_features &= ~valid;
202 dev->wanted_features |= wanted & valid;
203 __netdev_update_features(dev);
205 if ((dev->wanted_features ^ dev->features) & valid)
206 ret |= ETHTOOL_F_WISH;
211 static int phy_get_sset_count(struct phy_device *phydev)
215 if (phydev->drv->get_sset_count &&
216 phydev->drv->get_strings &&
217 phydev->drv->get_stats) {
218 mutex_lock(&phydev->lock);
219 ret = phydev->drv->get_sset_count(phydev);
220 mutex_unlock(&phydev->lock);
228 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
230 const struct ethtool_ops *ops = dev->ethtool_ops;
232 if (sset == ETH_SS_FEATURES)
233 return ARRAY_SIZE(netdev_features_strings);
235 if (sset == ETH_SS_RSS_HASH_FUNCS)
236 return ARRAY_SIZE(rss_hash_func_strings);
238 if (sset == ETH_SS_TUNABLES)
239 return ARRAY_SIZE(tunable_strings);
241 if (sset == ETH_SS_PHY_TUNABLES)
242 return ARRAY_SIZE(phy_tunable_strings);
244 if (sset == ETH_SS_PHY_STATS) {
246 return phy_get_sset_count(dev->phydev);
251 if (ops->get_sset_count && ops->get_strings)
252 return ops->get_sset_count(dev, sset);
257 static void __ethtool_get_strings(struct net_device *dev,
258 u32 stringset, u8 *data)
260 const struct ethtool_ops *ops = dev->ethtool_ops;
262 if (stringset == ETH_SS_FEATURES)
263 memcpy(data, netdev_features_strings,
264 sizeof(netdev_features_strings));
265 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
266 memcpy(data, rss_hash_func_strings,
267 sizeof(rss_hash_func_strings));
268 else if (stringset == ETH_SS_TUNABLES)
269 memcpy(data, tunable_strings, sizeof(tunable_strings));
270 else if (stringset == ETH_SS_PHY_TUNABLES)
271 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
272 else if (stringset == ETH_SS_PHY_STATS) {
273 struct phy_device *phydev = dev->phydev;
276 mutex_lock(&phydev->lock);
277 phydev->drv->get_strings(phydev, data);
278 mutex_unlock(&phydev->lock);
283 /* ops->get_strings is valid because checked earlier */
284 ops->get_strings(dev, stringset, data);
287 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
289 /* feature masks of legacy discrete ethtool ops */
292 case ETHTOOL_GTXCSUM:
293 case ETHTOOL_STXCSUM:
294 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
295 case ETHTOOL_GRXCSUM:
296 case ETHTOOL_SRXCSUM:
297 return NETIF_F_RXCSUM;
303 return NETIF_F_ALL_TSO;
315 static int ethtool_get_one_feature(struct net_device *dev,
316 char __user *useraddr, u32 ethcmd)
318 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
319 struct ethtool_value edata = {
321 .data = !!(dev->features & mask),
324 if (copy_to_user(useraddr, &edata, sizeof(edata)))
329 static int ethtool_set_one_feature(struct net_device *dev,
330 void __user *useraddr, u32 ethcmd)
332 struct ethtool_value edata;
333 netdev_features_t mask;
335 if (copy_from_user(&edata, useraddr, sizeof(edata)))
338 mask = ethtool_get_feature_mask(ethcmd);
339 mask &= dev->hw_features;
344 dev->wanted_features |= mask;
346 dev->wanted_features &= ~mask;
348 __netdev_update_features(dev);
353 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
354 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
355 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
356 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
359 static u32 __ethtool_get_flags(struct net_device *dev)
363 if (dev->features & NETIF_F_LRO)
364 flags |= ETH_FLAG_LRO;
365 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
366 flags |= ETH_FLAG_RXVLAN;
367 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
368 flags |= ETH_FLAG_TXVLAN;
369 if (dev->features & NETIF_F_NTUPLE)
370 flags |= ETH_FLAG_NTUPLE;
371 if (dev->features & NETIF_F_RXHASH)
372 flags |= ETH_FLAG_RXHASH;
377 static int __ethtool_set_flags(struct net_device *dev, u32 data)
379 netdev_features_t features = 0, changed;
381 if (data & ~ETH_ALL_FLAGS)
384 if (data & ETH_FLAG_LRO)
385 features |= NETIF_F_LRO;
386 if (data & ETH_FLAG_RXVLAN)
387 features |= NETIF_F_HW_VLAN_CTAG_RX;
388 if (data & ETH_FLAG_TXVLAN)
389 features |= NETIF_F_HW_VLAN_CTAG_TX;
390 if (data & ETH_FLAG_NTUPLE)
391 features |= NETIF_F_NTUPLE;
392 if (data & ETH_FLAG_RXHASH)
393 features |= NETIF_F_RXHASH;
395 /* allow changing only bits set in hw_features */
396 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
397 if (changed & ~dev->hw_features)
398 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
400 dev->wanted_features =
401 (dev->wanted_features & ~changed) | (features & changed);
403 __netdev_update_features(dev);
408 /* Given two link masks, AND them together and save the result in dst. */
409 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
410 struct ethtool_link_ksettings *src)
412 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
413 unsigned int idx = 0;
415 for (; idx < size; idx++) {
416 dst->link_modes.supported[idx] &=
417 src->link_modes.supported[idx];
418 dst->link_modes.advertising[idx] &=
419 src->link_modes.advertising[idx];
422 EXPORT_SYMBOL(ethtool_intersect_link_masks);
424 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
427 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
430 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
432 /* return false if src had higher bits set. lower bits always updated. */
433 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
434 const unsigned long *src)
438 /* TODO: following test will soon always be true */
439 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
440 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
442 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
443 bitmap_fill(ext, 32);
444 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
445 if (bitmap_intersects(ext, src,
446 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
447 /* src mask goes beyond bit 31 */
451 *legacy_u32 = src[0];
454 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
456 /* return false if legacy contained non-0 deprecated fields
457 * maxtxpkt/maxrxpkt. rest of ksettings always updated
460 convert_legacy_settings_to_link_ksettings(
461 struct ethtool_link_ksettings *link_ksettings,
462 const struct ethtool_cmd *legacy_settings)
466 memset(link_ksettings, 0, sizeof(*link_ksettings));
468 /* This is used to tell users that driver is still using these
469 * deprecated legacy fields, and they should not use
470 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
472 if (legacy_settings->maxtxpkt ||
473 legacy_settings->maxrxpkt)
476 ethtool_convert_legacy_u32_to_link_mode(
477 link_ksettings->link_modes.supported,
478 legacy_settings->supported);
479 ethtool_convert_legacy_u32_to_link_mode(
480 link_ksettings->link_modes.advertising,
481 legacy_settings->advertising);
482 ethtool_convert_legacy_u32_to_link_mode(
483 link_ksettings->link_modes.lp_advertising,
484 legacy_settings->lp_advertising);
485 link_ksettings->base.speed
486 = ethtool_cmd_speed(legacy_settings);
487 link_ksettings->base.duplex
488 = legacy_settings->duplex;
489 link_ksettings->base.port
490 = legacy_settings->port;
491 link_ksettings->base.phy_address
492 = legacy_settings->phy_address;
493 link_ksettings->base.autoneg
494 = legacy_settings->autoneg;
495 link_ksettings->base.mdio_support
496 = legacy_settings->mdio_support;
497 link_ksettings->base.eth_tp_mdix
498 = legacy_settings->eth_tp_mdix;
499 link_ksettings->base.eth_tp_mdix_ctrl
500 = legacy_settings->eth_tp_mdix_ctrl;
504 /* return false if ksettings link modes had higher bits
505 * set. legacy_settings always updated (best effort)
508 convert_link_ksettings_to_legacy_settings(
509 struct ethtool_cmd *legacy_settings,
510 const struct ethtool_link_ksettings *link_ksettings)
514 memset(legacy_settings, 0, sizeof(*legacy_settings));
515 /* this also clears the deprecated fields in legacy structure:
521 retval &= ethtool_convert_link_mode_to_legacy_u32(
522 &legacy_settings->supported,
523 link_ksettings->link_modes.supported);
524 retval &= ethtool_convert_link_mode_to_legacy_u32(
525 &legacy_settings->advertising,
526 link_ksettings->link_modes.advertising);
527 retval &= ethtool_convert_link_mode_to_legacy_u32(
528 &legacy_settings->lp_advertising,
529 link_ksettings->link_modes.lp_advertising);
530 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
531 legacy_settings->duplex
532 = link_ksettings->base.duplex;
533 legacy_settings->port
534 = link_ksettings->base.port;
535 legacy_settings->phy_address
536 = link_ksettings->base.phy_address;
537 legacy_settings->autoneg
538 = link_ksettings->base.autoneg;
539 legacy_settings->mdio_support
540 = link_ksettings->base.mdio_support;
541 legacy_settings->eth_tp_mdix
542 = link_ksettings->base.eth_tp_mdix;
543 legacy_settings->eth_tp_mdix_ctrl
544 = link_ksettings->base.eth_tp_mdix_ctrl;
545 legacy_settings->transceiver
546 = link_ksettings->base.transceiver;
550 /* number of 32-bit words to store the user's link mode bitmaps */
551 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
552 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
554 /* layout of the struct passed from/to userland */
555 struct ethtool_link_usettings {
556 struct ethtool_link_settings base;
558 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
559 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
560 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
564 /* Internal kernel helper to query a device ethtool_link_settings.
566 * Backward compatibility note: for compatibility with legacy drivers
567 * that implement only the ethtool_cmd API, this has to work with both
568 * drivers implementing get_link_ksettings API and drivers
569 * implementing get_settings API. When drivers implement get_settings
570 * and report ethtool_cmd deprecated fields
571 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
572 * because the resulting struct ethtool_link_settings does not report them.
574 int __ethtool_get_link_ksettings(struct net_device *dev,
575 struct ethtool_link_ksettings *link_ksettings)
578 struct ethtool_cmd cmd;
582 if (dev->ethtool_ops->get_link_ksettings) {
583 memset(link_ksettings, 0, sizeof(*link_ksettings));
584 return dev->ethtool_ops->get_link_ksettings(dev,
588 /* driver doesn't support %ethtool_link_ksettings API. revert to
589 * legacy %ethtool_cmd API, unless it's not supported either.
590 * TODO: remove when ethtool_ops::get_settings disappears internally
592 if (!dev->ethtool_ops->get_settings)
595 memset(&cmd, 0, sizeof(cmd));
596 cmd.cmd = ETHTOOL_GSET;
597 err = dev->ethtool_ops->get_settings(dev, &cmd);
601 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
603 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
606 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
608 /* convert ethtool_link_usettings in user space to a kernel internal
609 * ethtool_link_ksettings. return 0 on success, errno on error.
611 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
612 const void __user *from)
614 struct ethtool_link_usettings link_usettings;
616 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
619 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
620 bitmap_from_arr32(to->link_modes.supported,
621 link_usettings.link_modes.supported,
622 __ETHTOOL_LINK_MODE_MASK_NBITS);
623 bitmap_from_arr32(to->link_modes.advertising,
624 link_usettings.link_modes.advertising,
625 __ETHTOOL_LINK_MODE_MASK_NBITS);
626 bitmap_from_arr32(to->link_modes.lp_advertising,
627 link_usettings.link_modes.lp_advertising,
628 __ETHTOOL_LINK_MODE_MASK_NBITS);
633 /* convert a kernel internal ethtool_link_ksettings to
634 * ethtool_link_usettings in user space. return 0 on success, errno on
638 store_link_ksettings_for_user(void __user *to,
639 const struct ethtool_link_ksettings *from)
641 struct ethtool_link_usettings link_usettings;
643 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
644 bitmap_to_arr32(link_usettings.link_modes.supported,
645 from->link_modes.supported,
646 __ETHTOOL_LINK_MODE_MASK_NBITS);
647 bitmap_to_arr32(link_usettings.link_modes.advertising,
648 from->link_modes.advertising,
649 __ETHTOOL_LINK_MODE_MASK_NBITS);
650 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
651 from->link_modes.lp_advertising,
652 __ETHTOOL_LINK_MODE_MASK_NBITS);
654 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
660 /* Query device for its ethtool_link_settings.
662 * Backward compatibility note: this function must fail when driver
663 * does not implement ethtool::get_link_ksettings, even if legacy
664 * ethtool_ops::get_settings is implemented. This tells new versions
665 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
666 * this driver, so that they can correctly access the ethtool_cmd
667 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
668 * implements ethtool_ops::get_settings anymore.
670 static int ethtool_get_link_ksettings(struct net_device *dev,
671 void __user *useraddr)
674 struct ethtool_link_ksettings link_ksettings;
678 if (!dev->ethtool_ops->get_link_ksettings)
681 /* handle bitmap nbits handshake */
682 if (copy_from_user(&link_ksettings.base, useraddr,
683 sizeof(link_ksettings.base)))
686 if (__ETHTOOL_LINK_MODE_MASK_NU32
687 != link_ksettings.base.link_mode_masks_nwords) {
688 /* wrong link mode nbits requested */
689 memset(&link_ksettings, 0, sizeof(link_ksettings));
690 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
691 /* send back number of words required as negative val */
692 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
693 "need too many bits for link modes!");
694 link_ksettings.base.link_mode_masks_nwords
695 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
697 /* copy the base fields back to user, not the link
700 if (copy_to_user(useraddr, &link_ksettings.base,
701 sizeof(link_ksettings.base)))
707 /* handshake successful: user/kernel agree on
708 * link_mode_masks_nwords
711 memset(&link_ksettings, 0, sizeof(link_ksettings));
712 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
716 /* make sure we tell the right values to user */
717 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
718 link_ksettings.base.link_mode_masks_nwords
719 = __ETHTOOL_LINK_MODE_MASK_NU32;
721 return store_link_ksettings_for_user(useraddr, &link_ksettings);
724 /* Update device ethtool_link_settings.
726 * Backward compatibility note: this function must fail when driver
727 * does not implement ethtool::set_link_ksettings, even if legacy
728 * ethtool_ops::set_settings is implemented. This tells new versions
729 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
730 * this driver, so that they can correctly update the ethtool_cmd
731 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
732 * implements ethtool_ops::get_settings anymore.
734 static int ethtool_set_link_ksettings(struct net_device *dev,
735 void __user *useraddr)
738 struct ethtool_link_ksettings link_ksettings;
742 if (!dev->ethtool_ops->set_link_ksettings)
745 /* make sure nbits field has expected value */
746 if (copy_from_user(&link_ksettings.base, useraddr,
747 sizeof(link_ksettings.base)))
750 if (__ETHTOOL_LINK_MODE_MASK_NU32
751 != link_ksettings.base.link_mode_masks_nwords)
754 /* copy the whole structure, now that we know it has expected
757 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
761 /* re-check nwords field, just in case */
762 if (__ETHTOOL_LINK_MODE_MASK_NU32
763 != link_ksettings.base.link_mode_masks_nwords)
766 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
769 /* Query device for its ethtool_cmd settings.
771 * Backward compatibility note: for compatibility with legacy ethtool,
772 * this has to work with both drivers implementing get_link_ksettings
773 * API and drivers implementing get_settings API. When drivers
774 * implement get_link_ksettings and report higher link mode bits, a
775 * kernel warning is logged once (with name of 1st driver/device) to
776 * recommend user to upgrade ethtool, but the command is successful
777 * (only the lower link mode bits reported back to user).
779 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
781 struct ethtool_cmd cmd;
785 if (dev->ethtool_ops->get_link_ksettings) {
786 /* First, use link_ksettings API if it is supported */
788 struct ethtool_link_ksettings link_ksettings;
790 memset(&link_ksettings, 0, sizeof(link_ksettings));
791 err = dev->ethtool_ops->get_link_ksettings(dev,
795 convert_link_ksettings_to_legacy_settings(&cmd,
798 /* send a sensible cmd tag back to user */
799 cmd.cmd = ETHTOOL_GSET;
801 /* driver doesn't support %ethtool_link_ksettings
802 * API. revert to legacy %ethtool_cmd API, unless it's
803 * not supported either.
807 if (!dev->ethtool_ops->get_settings)
810 memset(&cmd, 0, sizeof(cmd));
811 cmd.cmd = ETHTOOL_GSET;
812 err = dev->ethtool_ops->get_settings(dev, &cmd);
817 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
823 /* Update device link settings with given ethtool_cmd.
825 * Backward compatibility note: for compatibility with legacy ethtool,
826 * this has to work with both drivers implementing set_link_ksettings
827 * API and drivers implementing set_settings API. When drivers
828 * implement set_link_ksettings and user's request updates deprecated
829 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
830 * warning is logged once (with name of 1st driver/device) to
831 * recommend user to upgrade ethtool, and the request is rejected.
833 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
835 struct ethtool_cmd cmd;
839 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
842 /* first, try new %ethtool_link_ksettings API. */
843 if (dev->ethtool_ops->set_link_ksettings) {
844 struct ethtool_link_ksettings link_ksettings;
846 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
850 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
851 link_ksettings.base.link_mode_masks_nwords
852 = __ETHTOOL_LINK_MODE_MASK_NU32;
853 return dev->ethtool_ops->set_link_ksettings(dev,
857 /* legacy %ethtool_cmd API */
859 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
860 * disappears internally
863 if (!dev->ethtool_ops->set_settings)
866 return dev->ethtool_ops->set_settings(dev, &cmd);
869 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
870 void __user *useraddr)
872 struct ethtool_drvinfo info;
873 const struct ethtool_ops *ops = dev->ethtool_ops;
875 memset(&info, 0, sizeof(info));
876 info.cmd = ETHTOOL_GDRVINFO;
877 if (ops->get_drvinfo) {
878 ops->get_drvinfo(dev, &info);
879 } else if (dev->dev.parent && dev->dev.parent->driver) {
880 strlcpy(info.bus_info, dev_name(dev->dev.parent),
881 sizeof(info.bus_info));
882 strlcpy(info.driver, dev->dev.parent->driver->name,
883 sizeof(info.driver));
889 * this method of obtaining string set info is deprecated;
890 * Use ETHTOOL_GSSET_INFO instead.
892 if (ops->get_sset_count) {
895 rc = ops->get_sset_count(dev, ETH_SS_TEST);
897 info.testinfo_len = rc;
898 rc = ops->get_sset_count(dev, ETH_SS_STATS);
901 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
903 info.n_priv_flags = rc;
905 if (ops->get_regs_len)
906 info.regdump_len = ops->get_regs_len(dev);
907 if (ops->get_eeprom_len)
908 info.eedump_len = ops->get_eeprom_len(dev);
910 if (copy_to_user(useraddr, &info, sizeof(info)))
915 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
916 void __user *useraddr)
918 struct ethtool_sset_info info;
920 int i, idx = 0, n_bits = 0, ret, rc;
921 u32 *info_buf = NULL;
923 if (copy_from_user(&info, useraddr, sizeof(info)))
926 /* store copy of mask, because we zero struct later on */
927 sset_mask = info.sset_mask;
931 /* calculate size of return buffer */
932 n_bits = hweight64(sset_mask);
934 memset(&info, 0, sizeof(info));
935 info.cmd = ETHTOOL_GSSET_INFO;
937 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
942 * fill return buffer based on input bitmask and successful
943 * get_sset_count return
945 for (i = 0; i < 64; i++) {
946 if (!(sset_mask & (1ULL << i)))
949 rc = __ethtool_get_sset_count(dev, i);
951 info.sset_mask |= (1ULL << i);
952 info_buf[idx++] = rc;
957 if (copy_to_user(useraddr, &info, sizeof(info)))
960 useraddr += offsetof(struct ethtool_sset_info, data);
961 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
971 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
972 u32 cmd, void __user *useraddr)
974 struct ethtool_rxnfc info;
975 size_t info_size = sizeof(info);
978 if (!dev->ethtool_ops->set_rxnfc)
981 /* struct ethtool_rxnfc was originally defined for
982 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
983 * members. User-space might still be using that
985 if (cmd == ETHTOOL_SRXFH)
986 info_size = (offsetof(struct ethtool_rxnfc, data) +
989 if (copy_from_user(&info, useraddr, info_size))
992 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
996 if (cmd == ETHTOOL_SRXCLSRLINS &&
997 copy_to_user(useraddr, &info, info_size))
1003 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
1004 u32 cmd, void __user *useraddr)
1006 struct ethtool_rxnfc info;
1007 size_t info_size = sizeof(info);
1008 const struct ethtool_ops *ops = dev->ethtool_ops;
1010 void *rule_buf = NULL;
1012 if (!ops->get_rxnfc)
1015 /* struct ethtool_rxnfc was originally defined for
1016 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1017 * members. User-space might still be using that
1019 if (cmd == ETHTOOL_GRXFH)
1020 info_size = (offsetof(struct ethtool_rxnfc, data) +
1023 if (copy_from_user(&info, useraddr, info_size))
1026 /* If FLOW_RSS was requested then user-space must be using the
1027 * new definition, as FLOW_RSS is newer.
1029 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1030 info_size = sizeof(info);
1031 if (copy_from_user(&info, useraddr, info_size))
1035 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1036 if (info.rule_cnt > 0) {
1037 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1038 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
1045 ret = ops->get_rxnfc(dev, &info, rule_buf);
1050 if (copy_to_user(useraddr, &info, info_size))
1054 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1055 if (copy_to_user(useraddr, rule_buf,
1056 info.rule_cnt * sizeof(u32)))
1067 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1068 struct ethtool_rxnfc *rx_rings,
1073 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1076 /* Validate ring indices */
1077 for (i = 0; i < size; i++)
1078 if (indir[i] >= rx_rings->data)
1084 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1086 void netdev_rss_key_fill(void *buffer, size_t len)
1088 BUG_ON(len > sizeof(netdev_rss_key));
1089 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1090 memcpy(buffer, netdev_rss_key, len);
1092 EXPORT_SYMBOL(netdev_rss_key_fill);
1094 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1096 u32 dev_size, current_max = 0;
1100 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1101 !dev->ethtool_ops->get_rxfh)
1103 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1107 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1111 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1116 current_max = max(current_max, indir[dev_size]);
1125 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1126 void __user *useraddr)
1128 u32 user_size, dev_size;
1132 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1133 !dev->ethtool_ops->get_rxfh)
1135 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1139 if (copy_from_user(&user_size,
1140 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1144 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1145 &dev_size, sizeof(dev_size)))
1148 /* If the user buffer size is 0, this is just a query for the
1149 * device table size. Otherwise, if it's smaller than the
1150 * device table size it's an error.
1152 if (user_size < dev_size)
1153 return user_size == 0 ? 0 : -EINVAL;
1155 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1159 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1163 if (copy_to_user(useraddr +
1164 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1165 indir, dev_size * sizeof(indir[0])))
1173 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1174 void __user *useraddr)
1176 struct ethtool_rxnfc rx_rings;
1177 u32 user_size, dev_size, i;
1179 const struct ethtool_ops *ops = dev->ethtool_ops;
1181 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1183 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1187 dev_size = ops->get_rxfh_indir_size(dev);
1191 if (copy_from_user(&user_size,
1192 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1196 if (user_size != 0 && user_size != dev_size)
1199 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1203 rx_rings.cmd = ETHTOOL_GRXRINGS;
1204 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1208 if (user_size == 0) {
1209 for (i = 0; i < dev_size; i++)
1210 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1212 ret = ethtool_copy_validate_indir(indir,
1213 useraddr + ringidx_offset,
1220 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1224 /* indicate whether rxfh was set to default */
1226 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1228 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1235 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1236 void __user *useraddr)
1239 const struct ethtool_ops *ops = dev->ethtool_ops;
1240 u32 user_indir_size, user_key_size;
1241 u32 dev_indir_size = 0, dev_key_size = 0;
1242 struct ethtool_rxfh rxfh;
1253 if (ops->get_rxfh_indir_size)
1254 dev_indir_size = ops->get_rxfh_indir_size(dev);
1255 if (ops->get_rxfh_key_size)
1256 dev_key_size = ops->get_rxfh_key_size(dev);
1258 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1260 user_indir_size = rxfh.indir_size;
1261 user_key_size = rxfh.key_size;
1263 /* Check that reserved fields are 0 for now */
1264 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1266 /* Most drivers don't handle rss_context, check it's 0 as well */
1267 if (rxfh.rss_context && !ops->get_rxfh_context)
1270 rxfh.indir_size = dev_indir_size;
1271 rxfh.key_size = dev_key_size;
1272 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1275 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1276 (user_key_size && (user_key_size != dev_key_size)))
1279 indir_bytes = user_indir_size * sizeof(indir[0]);
1280 total_size = indir_bytes + user_key_size;
1281 rss_config = kzalloc(total_size, GFP_USER);
1285 if (user_indir_size)
1286 indir = (u32 *)rss_config;
1289 hkey = rss_config + indir_bytes;
1291 if (rxfh.rss_context)
1292 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1296 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1300 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1301 &dev_hfunc, sizeof(rxfh.hfunc))) {
1303 } else if (copy_to_user(useraddr +
1304 offsetof(struct ethtool_rxfh, rss_config[0]),
1305 rss_config, total_size)) {
1314 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1315 void __user *useraddr)
1318 const struct ethtool_ops *ops = dev->ethtool_ops;
1319 struct ethtool_rxnfc rx_rings;
1320 struct ethtool_rxfh rxfh;
1321 u32 dev_indir_size = 0, dev_key_size = 0, i;
1322 u32 *indir = NULL, indir_bytes = 0;
1325 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1326 bool delete = false;
1328 if (!ops->get_rxnfc || !ops->set_rxfh)
1331 if (ops->get_rxfh_indir_size)
1332 dev_indir_size = ops->get_rxfh_indir_size(dev);
1333 if (ops->get_rxfh_key_size)
1334 dev_key_size = ops->get_rxfh_key_size(dev);
1336 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1339 /* Check that reserved fields are 0 for now */
1340 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1342 /* Most drivers don't handle rss_context, check it's 0 as well */
1343 if (rxfh.rss_context && !ops->set_rxfh_context)
1346 /* If either indir, hash key or function is valid, proceed further.
1347 * Must request at least one change: indir size, hash key or function.
1349 if ((rxfh.indir_size &&
1350 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1351 rxfh.indir_size != dev_indir_size) ||
1352 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1353 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1354 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1357 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1358 indir_bytes = dev_indir_size * sizeof(indir[0]);
1360 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1364 rx_rings.cmd = ETHTOOL_GRXRINGS;
1365 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1369 /* rxfh.indir_size == 0 means reset the indir table to default (master
1370 * context) or delete the context (other RSS contexts).
1371 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1373 if (rxfh.indir_size &&
1374 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1375 indir = (u32 *)rss_config;
1376 ret = ethtool_copy_validate_indir(indir,
1377 useraddr + rss_cfg_offset,
1382 } else if (rxfh.indir_size == 0) {
1383 if (rxfh.rss_context == 0) {
1384 indir = (u32 *)rss_config;
1385 for (i = 0; i < dev_indir_size; i++)
1386 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1392 if (rxfh.key_size) {
1393 hkey = rss_config + indir_bytes;
1394 if (copy_from_user(hkey,
1395 useraddr + rss_cfg_offset + indir_bytes,
1402 if (rxfh.rss_context)
1403 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1404 &rxfh.rss_context, delete);
1406 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1410 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1411 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1414 if (!rxfh.rss_context) {
1415 /* indicate whether rxfh was set to default */
1416 if (rxfh.indir_size == 0)
1417 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1418 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1419 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1427 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1429 struct ethtool_regs regs;
1430 const struct ethtool_ops *ops = dev->ethtool_ops;
1434 if (!ops->get_regs || !ops->get_regs_len)
1437 if (copy_from_user(®s, useraddr, sizeof(regs)))
1440 reglen = ops->get_regs_len(dev);
1441 if (regs.len > reglen)
1446 regbuf = vzalloc(reglen);
1451 ops->get_regs(dev, ®s, regbuf);
1454 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1456 useraddr += offsetof(struct ethtool_regs, data);
1457 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1466 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1468 struct ethtool_value reset;
1471 if (!dev->ethtool_ops->reset)
1474 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1477 ret = dev->ethtool_ops->reset(dev, &reset.data);
1481 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1486 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1488 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1490 if (!dev->ethtool_ops->get_wol)
1493 dev->ethtool_ops->get_wol(dev, &wol);
1495 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1500 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1502 struct ethtool_wolinfo wol;
1504 if (!dev->ethtool_ops->set_wol)
1507 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1510 return dev->ethtool_ops->set_wol(dev, &wol);
1513 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1515 struct ethtool_eee edata;
1518 if (!dev->ethtool_ops->get_eee)
1521 memset(&edata, 0, sizeof(struct ethtool_eee));
1522 edata.cmd = ETHTOOL_GEEE;
1523 rc = dev->ethtool_ops->get_eee(dev, &edata);
1528 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1534 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1536 struct ethtool_eee edata;
1538 if (!dev->ethtool_ops->set_eee)
1541 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1544 return dev->ethtool_ops->set_eee(dev, &edata);
1547 static int ethtool_nway_reset(struct net_device *dev)
1549 if (!dev->ethtool_ops->nway_reset)
1552 return dev->ethtool_ops->nway_reset(dev);
1555 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1557 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1559 if (!dev->ethtool_ops->get_link)
1562 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1564 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1569 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1570 int (*getter)(struct net_device *,
1571 struct ethtool_eeprom *, u8 *),
1574 struct ethtool_eeprom eeprom;
1575 void __user *userbuf = useraddr + sizeof(eeprom);
1576 u32 bytes_remaining;
1580 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1583 /* Check for wrap and zero */
1584 if (eeprom.offset + eeprom.len <= eeprom.offset)
1587 /* Check for exceeding total eeprom len */
1588 if (eeprom.offset + eeprom.len > total_len)
1591 data = kmalloc(PAGE_SIZE, GFP_USER);
1595 bytes_remaining = eeprom.len;
1596 while (bytes_remaining > 0) {
1597 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1599 ret = getter(dev, &eeprom, data);
1602 if (copy_to_user(userbuf, data, eeprom.len)) {
1606 userbuf += eeprom.len;
1607 eeprom.offset += eeprom.len;
1608 bytes_remaining -= eeprom.len;
1611 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1612 eeprom.offset -= eeprom.len;
1613 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1620 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1622 const struct ethtool_ops *ops = dev->ethtool_ops;
1624 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1625 !ops->get_eeprom_len(dev))
1628 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1629 ops->get_eeprom_len(dev));
1632 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1634 struct ethtool_eeprom eeprom;
1635 const struct ethtool_ops *ops = dev->ethtool_ops;
1636 void __user *userbuf = useraddr + sizeof(eeprom);
1637 u32 bytes_remaining;
1641 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1642 !ops->get_eeprom_len(dev))
1645 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1648 /* Check for wrap and zero */
1649 if (eeprom.offset + eeprom.len <= eeprom.offset)
1652 /* Check for exceeding total eeprom len */
1653 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1656 data = kmalloc(PAGE_SIZE, GFP_USER);
1660 bytes_remaining = eeprom.len;
1661 while (bytes_remaining > 0) {
1662 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1664 if (copy_from_user(data, userbuf, eeprom.len)) {
1668 ret = ops->set_eeprom(dev, &eeprom, data);
1671 userbuf += eeprom.len;
1672 eeprom.offset += eeprom.len;
1673 bytes_remaining -= eeprom.len;
1680 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1681 void __user *useraddr)
1683 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1685 if (!dev->ethtool_ops->get_coalesce)
1688 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1690 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1695 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1696 void __user *useraddr)
1698 struct ethtool_coalesce coalesce;
1700 if (!dev->ethtool_ops->set_coalesce)
1703 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1706 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1709 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1711 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1713 if (!dev->ethtool_ops->get_ringparam)
1716 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1718 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1723 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1725 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1727 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1730 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1733 dev->ethtool_ops->get_ringparam(dev, &max);
1735 /* ensure new ring parameters are within the maximums */
1736 if (ringparam.rx_pending > max.rx_max_pending ||
1737 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1738 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1739 ringparam.tx_pending > max.tx_max_pending)
1742 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1745 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1746 void __user *useraddr)
1748 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1750 if (!dev->ethtool_ops->get_channels)
1753 dev->ethtool_ops->get_channels(dev, &channels);
1755 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1760 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1761 void __user *useraddr)
1763 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
1764 u32 max_rx_in_use = 0;
1766 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1769 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1772 dev->ethtool_ops->get_channels(dev, &max);
1774 /* ensure new counts are within the maximums */
1775 if ((channels.rx_count > max.max_rx) ||
1776 (channels.tx_count > max.max_tx) ||
1777 (channels.combined_count > max.max_combined) ||
1778 (channels.other_count > max.max_other))
1781 /* ensure the new Rx count fits within the configured Rx flow
1782 * indirection table settings */
1783 if (netif_is_rxfh_configured(dev) &&
1784 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1785 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1788 return dev->ethtool_ops->set_channels(dev, &channels);
1791 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1793 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1795 if (!dev->ethtool_ops->get_pauseparam)
1798 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1800 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1805 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1807 struct ethtool_pauseparam pauseparam;
1809 if (!dev->ethtool_ops->set_pauseparam)
1812 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1815 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1818 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1820 struct ethtool_test test;
1821 const struct ethtool_ops *ops = dev->ethtool_ops;
1825 if (!ops->self_test || !ops->get_sset_count)
1828 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1831 WARN_ON(test_len == 0);
1833 if (copy_from_user(&test, useraddr, sizeof(test)))
1836 test.len = test_len;
1837 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1841 ops->self_test(dev, &test, data);
1844 if (copy_to_user(useraddr, &test, sizeof(test)))
1846 useraddr += sizeof(test);
1847 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1856 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1858 struct ethtool_gstrings gstrings;
1862 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1865 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1868 if (ret > S32_MAX / ETH_GSTRING_LEN)
1873 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1874 if (gstrings.len && !data)
1877 __ethtool_get_strings(dev, gstrings.string_set, data);
1880 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1882 useraddr += sizeof(gstrings);
1884 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1893 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1895 struct ethtool_value id;
1897 const struct ethtool_ops *ops = dev->ethtool_ops;
1900 if (!ops->set_phys_id)
1906 if (copy_from_user(&id, useraddr, sizeof(id)))
1909 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1913 /* Drop the RTNL lock while waiting, but prevent reentry or
1914 * removal of the device.
1921 /* Driver will handle this itself */
1922 schedule_timeout_interruptible(
1923 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1925 /* Driver expects to be called at twice the frequency in rc */
1926 int n = rc * 2, i, interval = HZ / n;
1928 /* Count down seconds */
1930 /* Count down iterations per second */
1934 rc = ops->set_phys_id(dev,
1935 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1939 schedule_timeout_interruptible(interval);
1940 } while (!signal_pending(current) && --i != 0);
1941 } while (!signal_pending(current) &&
1942 (id.data == 0 || --id.data != 0));
1949 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1953 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1955 struct ethtool_stats stats;
1956 const struct ethtool_ops *ops = dev->ethtool_ops;
1960 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1963 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1966 if (n_stats > S32_MAX / sizeof(u64))
1968 WARN_ON_ONCE(!n_stats);
1969 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1972 stats.n_stats = n_stats;
1973 data = vzalloc(n_stats * sizeof(u64));
1974 if (n_stats && !data)
1977 ops->get_ethtool_stats(dev, &stats, data);
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_phy_stats(struct net_device *dev, void __user *useraddr)
1994 struct ethtool_stats stats;
1995 struct phy_device *phydev = dev->phydev;
2002 n_stats = phy_get_sset_count(phydev);
2005 if (n_stats > S32_MAX / sizeof(u64))
2007 WARN_ON_ONCE(!n_stats);
2009 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2012 stats.n_stats = n_stats;
2013 data = vzalloc(n_stats * sizeof(u64));
2014 if (n_stats && !data)
2017 mutex_lock(&phydev->lock);
2018 phydev->drv->get_stats(phydev, &stats, data);
2019 mutex_unlock(&phydev->lock);
2022 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2024 useraddr += sizeof(stats);
2025 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
2034 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2036 struct ethtool_perm_addr epaddr;
2038 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2041 if (epaddr.size < dev->addr_len)
2043 epaddr.size = dev->addr_len;
2045 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2047 useraddr += sizeof(epaddr);
2048 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2053 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2054 u32 cmd, u32 (*actor)(struct net_device *))
2056 struct ethtool_value edata = { .cmd = cmd };
2061 edata.data = actor(dev);
2063 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2068 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2069 void (*actor)(struct net_device *, u32))
2071 struct ethtool_value edata;
2076 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2079 actor(dev, edata.data);
2083 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2084 int (*actor)(struct net_device *, u32))
2086 struct ethtool_value edata;
2091 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2094 return actor(dev, edata.data);
2097 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2098 char __user *useraddr)
2100 struct ethtool_flash efl;
2102 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2105 if (!dev->ethtool_ops->flash_device)
2108 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2110 return dev->ethtool_ops->flash_device(dev, &efl);
2113 static int ethtool_set_dump(struct net_device *dev,
2114 void __user *useraddr)
2116 struct ethtool_dump dump;
2118 if (!dev->ethtool_ops->set_dump)
2121 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2124 return dev->ethtool_ops->set_dump(dev, &dump);
2127 static int ethtool_get_dump_flag(struct net_device *dev,
2128 void __user *useraddr)
2131 struct ethtool_dump dump;
2132 const struct ethtool_ops *ops = dev->ethtool_ops;
2134 if (!ops->get_dump_flag)
2137 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2140 ret = ops->get_dump_flag(dev, &dump);
2144 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2149 static int ethtool_get_dump_data(struct net_device *dev,
2150 void __user *useraddr)
2154 struct ethtool_dump dump, tmp;
2155 const struct ethtool_ops *ops = dev->ethtool_ops;
2158 if (!ops->get_dump_data || !ops->get_dump_flag)
2161 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2164 memset(&tmp, 0, sizeof(tmp));
2165 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2166 ret = ops->get_dump_flag(dev, &tmp);
2170 len = min(tmp.len, dump.len);
2174 /* Don't ever let the driver think there's more space available
2175 * than it requested with .get_dump_flag().
2179 /* Always allocate enough space to hold the whole thing so that the
2180 * driver does not need to check the length and bother with partial
2183 data = vzalloc(tmp.len);
2186 ret = ops->get_dump_data(dev, &dump, data);
2190 /* There are two sane possibilities:
2191 * 1. The driver's .get_dump_data() does not touch dump.len.
2192 * 2. Or it may set dump.len to how much it really writes, which
2193 * should be tmp.len (or len if it can do a partial dump).
2194 * In any case respond to userspace with the actual length of data
2197 WARN_ON(dump.len != len && dump.len != tmp.len);
2200 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2204 useraddr += offsetof(struct ethtool_dump, data);
2205 if (copy_to_user(useraddr, data, len))
2212 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2215 struct ethtool_ts_info info;
2216 const struct ethtool_ops *ops = dev->ethtool_ops;
2217 struct phy_device *phydev = dev->phydev;
2219 memset(&info, 0, sizeof(info));
2220 info.cmd = ETHTOOL_GET_TS_INFO;
2222 if (phydev && phydev->drv && phydev->drv->ts_info) {
2223 err = phydev->drv->ts_info(phydev, &info);
2224 } else if (ops->get_ts_info) {
2225 err = ops->get_ts_info(dev, &info);
2227 info.so_timestamping =
2228 SOF_TIMESTAMPING_RX_SOFTWARE |
2229 SOF_TIMESTAMPING_SOFTWARE;
2230 info.phc_index = -1;
2236 if (copy_to_user(useraddr, &info, sizeof(info)))
2242 static int __ethtool_get_module_info(struct net_device *dev,
2243 struct ethtool_modinfo *modinfo)
2245 const struct ethtool_ops *ops = dev->ethtool_ops;
2246 struct phy_device *phydev = dev->phydev;
2248 if (phydev && phydev->drv && phydev->drv->module_info)
2249 return phydev->drv->module_info(phydev, modinfo);
2251 if (ops->get_module_info)
2252 return ops->get_module_info(dev, modinfo);
2257 static int ethtool_get_module_info(struct net_device *dev,
2258 void __user *useraddr)
2261 struct ethtool_modinfo modinfo;
2263 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2266 ret = __ethtool_get_module_info(dev, &modinfo);
2270 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2276 static int __ethtool_get_module_eeprom(struct net_device *dev,
2277 struct ethtool_eeprom *ee, u8 *data)
2279 const struct ethtool_ops *ops = dev->ethtool_ops;
2280 struct phy_device *phydev = dev->phydev;
2282 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2283 return phydev->drv->module_eeprom(phydev, ee, data);
2285 if (ops->get_module_eeprom)
2286 return ops->get_module_eeprom(dev, ee, data);
2291 static int ethtool_get_module_eeprom(struct net_device *dev,
2292 void __user *useraddr)
2295 struct ethtool_modinfo modinfo;
2297 ret = __ethtool_get_module_info(dev, &modinfo);
2301 return ethtool_get_any_eeprom(dev, useraddr,
2302 __ethtool_get_module_eeprom,
2303 modinfo.eeprom_len);
2306 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2309 case ETHTOOL_RX_COPYBREAK:
2310 case ETHTOOL_TX_COPYBREAK:
2311 if (tuna->len != sizeof(u32) ||
2312 tuna->type_id != ETHTOOL_TUNABLE_U32)
2315 case ETHTOOL_PFC_PREVENTION_TOUT:
2316 if (tuna->len != sizeof(u16) ||
2317 tuna->type_id != ETHTOOL_TUNABLE_U16)
2327 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2330 struct ethtool_tunable tuna;
2331 const struct ethtool_ops *ops = dev->ethtool_ops;
2334 if (!ops->get_tunable)
2336 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2338 ret = ethtool_tunable_valid(&tuna);
2341 data = kmalloc(tuna.len, GFP_USER);
2344 ret = ops->get_tunable(dev, &tuna, data);
2347 useraddr += sizeof(tuna);
2349 if (copy_to_user(useraddr, data, tuna.len))
2358 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2361 struct ethtool_tunable tuna;
2362 const struct ethtool_ops *ops = dev->ethtool_ops;
2365 if (!ops->set_tunable)
2367 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2369 ret = ethtool_tunable_valid(&tuna);
2372 useraddr += sizeof(tuna);
2373 data = memdup_user(useraddr, tuna.len);
2375 return PTR_ERR(data);
2376 ret = ops->set_tunable(dev, &tuna, data);
2382 static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2383 void __user *useraddr,
2384 struct ethtool_per_queue_op *per_queue_opt)
2388 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2390 if (!dev->ethtool_ops->get_per_queue_coalesce)
2393 useraddr += sizeof(*per_queue_opt);
2395 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2398 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2399 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2401 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2404 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2406 useraddr += sizeof(coalesce);
2412 static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2413 void __user *useraddr,
2414 struct ethtool_per_queue_op *per_queue_opt)
2419 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2420 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2422 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2423 (!dev->ethtool_ops->get_per_queue_coalesce))
2426 useraddr += sizeof(*per_queue_opt);
2428 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2429 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2430 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2434 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2435 struct ethtool_coalesce coalesce;
2437 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2443 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2448 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2452 useraddr += sizeof(coalesce);
2458 for_each_set_bit(i, queue_mask, bit) {
2459 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2468 static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2470 struct ethtool_per_queue_op per_queue_opt;
2472 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2475 switch (per_queue_opt.sub_command) {
2476 case ETHTOOL_GCOALESCE:
2477 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2478 case ETHTOOL_SCOALESCE:
2479 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2485 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2488 case ETHTOOL_PHY_DOWNSHIFT:
2489 if (tuna->len != sizeof(u8) ||
2490 tuna->type_id != ETHTOOL_TUNABLE_U8)
2500 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2503 struct ethtool_tunable tuna;
2504 struct phy_device *phydev = dev->phydev;
2507 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2510 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2512 ret = ethtool_phy_tunable_valid(&tuna);
2515 data = kmalloc(tuna.len, GFP_USER);
2518 mutex_lock(&phydev->lock);
2519 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2520 mutex_unlock(&phydev->lock);
2523 useraddr += sizeof(tuna);
2525 if (copy_to_user(useraddr, data, tuna.len))
2534 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2537 struct ethtool_tunable tuna;
2538 struct phy_device *phydev = dev->phydev;
2541 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2543 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2545 ret = ethtool_phy_tunable_valid(&tuna);
2548 useraddr += sizeof(tuna);
2549 data = memdup_user(useraddr, tuna.len);
2551 return PTR_ERR(data);
2552 mutex_lock(&phydev->lock);
2553 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2554 mutex_unlock(&phydev->lock);
2560 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2562 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
2565 if (!dev->ethtool_ops->get_fecparam)
2568 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2572 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2577 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2579 struct ethtool_fecparam fecparam;
2581 if (!dev->ethtool_ops->set_fecparam)
2584 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2587 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2590 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2592 int dev_ethtool(struct net *net, struct ifreq *ifr)
2594 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2595 void __user *useraddr = ifr->ifr_data;
2596 u32 ethcmd, sub_cmd;
2598 netdev_features_t old_features;
2600 if (!dev || !netif_device_present(dev))
2603 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2606 if (ethcmd == ETHTOOL_PERQUEUE) {
2607 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2612 /* Allow some commands to be done by anyone */
2615 case ETHTOOL_GDRVINFO:
2616 case ETHTOOL_GMSGLVL:
2618 case ETHTOOL_GCOALESCE:
2619 case ETHTOOL_GRINGPARAM:
2620 case ETHTOOL_GPAUSEPARAM:
2621 case ETHTOOL_GRXCSUM:
2622 case ETHTOOL_GTXCSUM:
2624 case ETHTOOL_GSSET_INFO:
2625 case ETHTOOL_GSTRINGS:
2626 case ETHTOOL_GSTATS:
2627 case ETHTOOL_GPHYSTATS:
2629 case ETHTOOL_GPERMADDR:
2632 case ETHTOOL_GFLAGS:
2633 case ETHTOOL_GPFLAGS:
2635 case ETHTOOL_GRXRINGS:
2636 case ETHTOOL_GRXCLSRLCNT:
2637 case ETHTOOL_GRXCLSRULE:
2638 case ETHTOOL_GRXCLSRLALL:
2639 case ETHTOOL_GRXFHINDIR:
2641 case ETHTOOL_GFEATURES:
2642 case ETHTOOL_GCHANNELS:
2643 case ETHTOOL_GET_TS_INFO:
2645 case ETHTOOL_GTUNABLE:
2646 case ETHTOOL_PHY_GTUNABLE:
2647 case ETHTOOL_GLINKSETTINGS:
2648 case ETHTOOL_GFECPARAM:
2651 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2655 if (dev->ethtool_ops->begin) {
2656 rc = dev->ethtool_ops->begin(dev);
2660 old_features = dev->features;
2664 rc = ethtool_get_settings(dev, useraddr);
2667 rc = ethtool_set_settings(dev, useraddr);
2669 case ETHTOOL_GDRVINFO:
2670 rc = ethtool_get_drvinfo(dev, useraddr);
2673 rc = ethtool_get_regs(dev, useraddr);
2676 rc = ethtool_get_wol(dev, useraddr);
2679 rc = ethtool_set_wol(dev, useraddr);
2681 case ETHTOOL_GMSGLVL:
2682 rc = ethtool_get_value(dev, useraddr, ethcmd,
2683 dev->ethtool_ops->get_msglevel);
2685 case ETHTOOL_SMSGLVL:
2686 rc = ethtool_set_value_void(dev, useraddr,
2687 dev->ethtool_ops->set_msglevel);
2690 rc = ethtool_get_eee(dev, useraddr);
2693 rc = ethtool_set_eee(dev, useraddr);
2695 case ETHTOOL_NWAY_RST:
2696 rc = ethtool_nway_reset(dev);
2699 rc = ethtool_get_link(dev, useraddr);
2701 case ETHTOOL_GEEPROM:
2702 rc = ethtool_get_eeprom(dev, useraddr);
2704 case ETHTOOL_SEEPROM:
2705 rc = ethtool_set_eeprom(dev, useraddr);
2707 case ETHTOOL_GCOALESCE:
2708 rc = ethtool_get_coalesce(dev, useraddr);
2710 case ETHTOOL_SCOALESCE:
2711 rc = ethtool_set_coalesce(dev, useraddr);
2713 case ETHTOOL_GRINGPARAM:
2714 rc = ethtool_get_ringparam(dev, useraddr);
2716 case ETHTOOL_SRINGPARAM:
2717 rc = ethtool_set_ringparam(dev, useraddr);
2719 case ETHTOOL_GPAUSEPARAM:
2720 rc = ethtool_get_pauseparam(dev, useraddr);
2722 case ETHTOOL_SPAUSEPARAM:
2723 rc = ethtool_set_pauseparam(dev, useraddr);
2726 rc = ethtool_self_test(dev, useraddr);
2728 case ETHTOOL_GSTRINGS:
2729 rc = ethtool_get_strings(dev, useraddr);
2731 case ETHTOOL_PHYS_ID:
2732 rc = ethtool_phys_id(dev, useraddr);
2734 case ETHTOOL_GSTATS:
2735 rc = ethtool_get_stats(dev, useraddr);
2737 case ETHTOOL_GPERMADDR:
2738 rc = ethtool_get_perm_addr(dev, useraddr);
2740 case ETHTOOL_GFLAGS:
2741 rc = ethtool_get_value(dev, useraddr, ethcmd,
2742 __ethtool_get_flags);
2744 case ETHTOOL_SFLAGS:
2745 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2747 case ETHTOOL_GPFLAGS:
2748 rc = ethtool_get_value(dev, useraddr, ethcmd,
2749 dev->ethtool_ops->get_priv_flags);
2751 case ETHTOOL_SPFLAGS:
2752 rc = ethtool_set_value(dev, useraddr,
2753 dev->ethtool_ops->set_priv_flags);
2756 case ETHTOOL_GRXRINGS:
2757 case ETHTOOL_GRXCLSRLCNT:
2758 case ETHTOOL_GRXCLSRULE:
2759 case ETHTOOL_GRXCLSRLALL:
2760 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2763 case ETHTOOL_SRXCLSRLDEL:
2764 case ETHTOOL_SRXCLSRLINS:
2765 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2767 case ETHTOOL_FLASHDEV:
2768 rc = ethtool_flash_device(dev, useraddr);
2771 rc = ethtool_reset(dev, useraddr);
2773 case ETHTOOL_GSSET_INFO:
2774 rc = ethtool_get_sset_info(dev, useraddr);
2776 case ETHTOOL_GRXFHINDIR:
2777 rc = ethtool_get_rxfh_indir(dev, useraddr);
2779 case ETHTOOL_SRXFHINDIR:
2780 rc = ethtool_set_rxfh_indir(dev, useraddr);
2783 rc = ethtool_get_rxfh(dev, useraddr);
2786 rc = ethtool_set_rxfh(dev, useraddr);
2788 case ETHTOOL_GFEATURES:
2789 rc = ethtool_get_features(dev, useraddr);
2791 case ETHTOOL_SFEATURES:
2792 rc = ethtool_set_features(dev, useraddr);
2794 case ETHTOOL_GTXCSUM:
2795 case ETHTOOL_GRXCSUM:
2800 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2802 case ETHTOOL_STXCSUM:
2803 case ETHTOOL_SRXCSUM:
2808 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2810 case ETHTOOL_GCHANNELS:
2811 rc = ethtool_get_channels(dev, useraddr);
2813 case ETHTOOL_SCHANNELS:
2814 rc = ethtool_set_channels(dev, useraddr);
2816 case ETHTOOL_SET_DUMP:
2817 rc = ethtool_set_dump(dev, useraddr);
2819 case ETHTOOL_GET_DUMP_FLAG:
2820 rc = ethtool_get_dump_flag(dev, useraddr);
2822 case ETHTOOL_GET_DUMP_DATA:
2823 rc = ethtool_get_dump_data(dev, useraddr);
2825 case ETHTOOL_GET_TS_INFO:
2826 rc = ethtool_get_ts_info(dev, useraddr);
2828 case ETHTOOL_GMODULEINFO:
2829 rc = ethtool_get_module_info(dev, useraddr);
2831 case ETHTOOL_GMODULEEEPROM:
2832 rc = ethtool_get_module_eeprom(dev, useraddr);
2834 case ETHTOOL_GTUNABLE:
2835 rc = ethtool_get_tunable(dev, useraddr);
2837 case ETHTOOL_STUNABLE:
2838 rc = ethtool_set_tunable(dev, useraddr);
2840 case ETHTOOL_GPHYSTATS:
2841 rc = ethtool_get_phy_stats(dev, useraddr);
2843 case ETHTOOL_PERQUEUE:
2844 rc = ethtool_set_per_queue(dev, useraddr);
2846 case ETHTOOL_GLINKSETTINGS:
2847 rc = ethtool_get_link_ksettings(dev, useraddr);
2849 case ETHTOOL_SLINKSETTINGS:
2850 rc = ethtool_set_link_ksettings(dev, useraddr);
2852 case ETHTOOL_PHY_GTUNABLE:
2853 rc = get_phy_tunable(dev, useraddr);
2855 case ETHTOOL_PHY_STUNABLE:
2856 rc = set_phy_tunable(dev, useraddr);
2858 case ETHTOOL_GFECPARAM:
2859 rc = ethtool_get_fecparam(dev, useraddr);
2861 case ETHTOOL_SFECPARAM:
2862 rc = ethtool_set_fecparam(dev, useraddr);
2868 if (dev->ethtool_ops->complete)
2869 dev->ethtool_ops->complete(dev);
2871 if (old_features != dev->features)
2872 netdev_features_change(dev);