2 * net/core/ethtool.c - Ethtool ioctl handler
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/sfp.h>
26 #include <linux/slab.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sched/signal.h>
29 #include <linux/net.h>
32 * Some useful ethtool_ops methods that're device independent.
33 * If we find that all drivers want to do the same thing here,
34 * we can turn these into dev_() function calls.
37 u32 ethtool_op_get_link(struct net_device *dev)
39 return netif_carrier_ok(dev) ? 1 : 0;
41 EXPORT_SYMBOL(ethtool_op_get_link);
43 int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
45 info->so_timestamping =
46 SOF_TIMESTAMPING_TX_SOFTWARE |
47 SOF_TIMESTAMPING_RX_SOFTWARE |
48 SOF_TIMESTAMPING_SOFTWARE;
52 EXPORT_SYMBOL(ethtool_op_get_ts_info);
54 /* Handlers for each ethtool command */
56 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
58 static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
59 [NETIF_F_SG_BIT] = "tx-scatter-gather",
60 [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4",
61 [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic",
62 [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6",
63 [NETIF_F_HIGHDMA_BIT] = "highdma",
64 [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist",
65 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
67 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
69 [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert",
70 [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse",
71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
72 [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged",
73 [NETIF_F_GSO_BIT] = "tx-generic-segmentation",
74 [NETIF_F_LLTX_BIT] = "tx-lockless",
75 [NETIF_F_NETNS_LOCAL_BIT] = "netns-local",
76 [NETIF_F_GRO_BIT] = "rx-gro",
77 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
78 [NETIF_F_LRO_BIT] = "rx-lro",
80 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
81 [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust",
82 [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation",
83 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
84 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
85 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
86 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
87 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
88 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
89 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
90 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
91 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
92 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
93 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
94 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
96 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
97 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
98 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
99 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
100 [NETIF_F_RXHASH_BIT] = "rx-hashing",
101 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
102 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
103 [NETIF_F_LOOPBACK_BIT] = "loopback",
104 [NETIF_F_RXFCS_BIT] = "rx-fcs",
105 [NETIF_F_RXALL_BIT] = "rx-all",
106 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
107 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
108 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
109 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
110 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
111 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
115 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
116 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
117 [ETH_RSS_HASH_XOR_BIT] = "xor",
118 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
122 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
123 [ETHTOOL_ID_UNSPEC] = "Unspec",
124 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
125 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
126 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
130 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
131 [ETHTOOL_ID_UNSPEC] = "Unspec",
132 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
135 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
137 struct ethtool_gfeatures cmd = {
138 .cmd = ETHTOOL_GFEATURES,
139 .size = ETHTOOL_DEV_FEATURE_WORDS,
141 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
142 u32 __user *sizeaddr;
146 /* in case feature bits run out again */
147 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
149 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
150 features[i].available = (u32)(dev->hw_features >> (32 * i));
151 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
152 features[i].active = (u32)(dev->features >> (32 * i));
153 features[i].never_changed =
154 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
157 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
158 if (get_user(copy_size, sizeaddr))
161 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
162 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
164 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
166 useraddr += sizeof(cmd);
167 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
173 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
175 struct ethtool_sfeatures cmd;
176 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
177 netdev_features_t wanted = 0, valid = 0;
180 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
182 useraddr += sizeof(cmd);
184 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
187 if (copy_from_user(features, useraddr, sizeof(features)))
190 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
191 valid |= (netdev_features_t)features[i].valid << (32 * i);
192 wanted |= (netdev_features_t)features[i].requested << (32 * i);
195 if (valid & ~NETIF_F_ETHTOOL_BITS)
198 if (valid & ~dev->hw_features) {
199 valid &= dev->hw_features;
200 ret |= ETHTOOL_F_UNSUPPORTED;
203 dev->wanted_features &= ~valid;
204 dev->wanted_features |= wanted & valid;
205 __netdev_update_features(dev);
207 if ((dev->wanted_features ^ dev->features) & valid)
208 ret |= ETHTOOL_F_WISH;
213 static int phy_get_sset_count(struct phy_device *phydev)
217 if (phydev->drv->get_sset_count &&
218 phydev->drv->get_strings &&
219 phydev->drv->get_stats) {
220 mutex_lock(&phydev->lock);
221 ret = phydev->drv->get_sset_count(phydev);
222 mutex_unlock(&phydev->lock);
230 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
232 const struct ethtool_ops *ops = dev->ethtool_ops;
234 if (sset == ETH_SS_FEATURES)
235 return ARRAY_SIZE(netdev_features_strings);
237 if (sset == ETH_SS_RSS_HASH_FUNCS)
238 return ARRAY_SIZE(rss_hash_func_strings);
240 if (sset == ETH_SS_TUNABLES)
241 return ARRAY_SIZE(tunable_strings);
243 if (sset == ETH_SS_PHY_TUNABLES)
244 return ARRAY_SIZE(phy_tunable_strings);
246 if (sset == ETH_SS_PHY_STATS) {
248 return phy_get_sset_count(dev->phydev);
253 if (ops->get_sset_count && ops->get_strings)
254 return ops->get_sset_count(dev, sset);
259 static void __ethtool_get_strings(struct net_device *dev,
260 u32 stringset, u8 *data)
262 const struct ethtool_ops *ops = dev->ethtool_ops;
264 if (stringset == ETH_SS_FEATURES)
265 memcpy(data, netdev_features_strings,
266 sizeof(netdev_features_strings));
267 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
268 memcpy(data, rss_hash_func_strings,
269 sizeof(rss_hash_func_strings));
270 else if (stringset == ETH_SS_TUNABLES)
271 memcpy(data, tunable_strings, sizeof(tunable_strings));
272 else if (stringset == ETH_SS_PHY_TUNABLES)
273 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
274 else if (stringset == ETH_SS_PHY_STATS) {
275 struct phy_device *phydev = dev->phydev;
278 mutex_lock(&phydev->lock);
279 phydev->drv->get_strings(phydev, data);
280 mutex_unlock(&phydev->lock);
285 /* ops->get_strings is valid because checked earlier */
286 ops->get_strings(dev, stringset, data);
289 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
291 /* feature masks of legacy discrete ethtool ops */
294 case ETHTOOL_GTXCSUM:
295 case ETHTOOL_STXCSUM:
296 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
297 case ETHTOOL_GRXCSUM:
298 case ETHTOOL_SRXCSUM:
299 return NETIF_F_RXCSUM;
305 return NETIF_F_ALL_TSO;
317 static int ethtool_get_one_feature(struct net_device *dev,
318 char __user *useraddr, u32 ethcmd)
320 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
321 struct ethtool_value edata = {
323 .data = !!(dev->features & mask),
326 if (copy_to_user(useraddr, &edata, sizeof(edata)))
331 static int ethtool_set_one_feature(struct net_device *dev,
332 void __user *useraddr, u32 ethcmd)
334 struct ethtool_value edata;
335 netdev_features_t mask;
337 if (copy_from_user(&edata, useraddr, sizeof(edata)))
340 mask = ethtool_get_feature_mask(ethcmd);
341 mask &= dev->hw_features;
346 dev->wanted_features |= mask;
348 dev->wanted_features &= ~mask;
350 __netdev_update_features(dev);
355 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
356 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
357 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
358 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
361 static u32 __ethtool_get_flags(struct net_device *dev)
365 if (dev->features & NETIF_F_LRO)
366 flags |= ETH_FLAG_LRO;
367 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
368 flags |= ETH_FLAG_RXVLAN;
369 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
370 flags |= ETH_FLAG_TXVLAN;
371 if (dev->features & NETIF_F_NTUPLE)
372 flags |= ETH_FLAG_NTUPLE;
373 if (dev->features & NETIF_F_RXHASH)
374 flags |= ETH_FLAG_RXHASH;
379 static int __ethtool_set_flags(struct net_device *dev, u32 data)
381 netdev_features_t features = 0, changed;
383 if (data & ~ETH_ALL_FLAGS)
386 if (data & ETH_FLAG_LRO)
387 features |= NETIF_F_LRO;
388 if (data & ETH_FLAG_RXVLAN)
389 features |= NETIF_F_HW_VLAN_CTAG_RX;
390 if (data & ETH_FLAG_TXVLAN)
391 features |= NETIF_F_HW_VLAN_CTAG_TX;
392 if (data & ETH_FLAG_NTUPLE)
393 features |= NETIF_F_NTUPLE;
394 if (data & ETH_FLAG_RXHASH)
395 features |= NETIF_F_RXHASH;
397 /* allow changing only bits set in hw_features */
398 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
399 if (changed & ~dev->hw_features)
400 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
402 dev->wanted_features =
403 (dev->wanted_features & ~changed) | (features & changed);
405 __netdev_update_features(dev);
410 /* Given two link masks, AND them together and save the result in dst. */
411 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
412 struct ethtool_link_ksettings *src)
414 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
415 unsigned int idx = 0;
417 for (; idx < size; idx++) {
418 dst->link_modes.supported[idx] &=
419 src->link_modes.supported[idx];
420 dst->link_modes.advertising[idx] &=
421 src->link_modes.advertising[idx];
424 EXPORT_SYMBOL(ethtool_intersect_link_masks);
426 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
429 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
432 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
434 /* return false if src had higher bits set. lower bits always updated. */
435 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
436 const unsigned long *src)
440 /* TODO: following test will soon always be true */
441 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
442 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
444 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
445 bitmap_fill(ext, 32);
446 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
447 if (bitmap_intersects(ext, src,
448 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
449 /* src mask goes beyond bit 31 */
453 *legacy_u32 = src[0];
456 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
458 /* return false if legacy contained non-0 deprecated fields
459 * maxtxpkt/maxrxpkt. rest of ksettings always updated
462 convert_legacy_settings_to_link_ksettings(
463 struct ethtool_link_ksettings *link_ksettings,
464 const struct ethtool_cmd *legacy_settings)
468 memset(link_ksettings, 0, sizeof(*link_ksettings));
470 /* This is used to tell users that driver is still using these
471 * deprecated legacy fields, and they should not use
472 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
474 if (legacy_settings->maxtxpkt ||
475 legacy_settings->maxrxpkt)
478 ethtool_convert_legacy_u32_to_link_mode(
479 link_ksettings->link_modes.supported,
480 legacy_settings->supported);
481 ethtool_convert_legacy_u32_to_link_mode(
482 link_ksettings->link_modes.advertising,
483 legacy_settings->advertising);
484 ethtool_convert_legacy_u32_to_link_mode(
485 link_ksettings->link_modes.lp_advertising,
486 legacy_settings->lp_advertising);
487 link_ksettings->base.speed
488 = ethtool_cmd_speed(legacy_settings);
489 link_ksettings->base.duplex
490 = legacy_settings->duplex;
491 link_ksettings->base.port
492 = legacy_settings->port;
493 link_ksettings->base.phy_address
494 = legacy_settings->phy_address;
495 link_ksettings->base.autoneg
496 = legacy_settings->autoneg;
497 link_ksettings->base.mdio_support
498 = legacy_settings->mdio_support;
499 link_ksettings->base.eth_tp_mdix
500 = legacy_settings->eth_tp_mdix;
501 link_ksettings->base.eth_tp_mdix_ctrl
502 = legacy_settings->eth_tp_mdix_ctrl;
506 /* return false if ksettings link modes had higher bits
507 * set. legacy_settings always updated (best effort)
510 convert_link_ksettings_to_legacy_settings(
511 struct ethtool_cmd *legacy_settings,
512 const struct ethtool_link_ksettings *link_ksettings)
516 memset(legacy_settings, 0, sizeof(*legacy_settings));
517 /* this also clears the deprecated fields in legacy structure:
523 retval &= ethtool_convert_link_mode_to_legacy_u32(
524 &legacy_settings->supported,
525 link_ksettings->link_modes.supported);
526 retval &= ethtool_convert_link_mode_to_legacy_u32(
527 &legacy_settings->advertising,
528 link_ksettings->link_modes.advertising);
529 retval &= ethtool_convert_link_mode_to_legacy_u32(
530 &legacy_settings->lp_advertising,
531 link_ksettings->link_modes.lp_advertising);
532 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
533 legacy_settings->duplex
534 = link_ksettings->base.duplex;
535 legacy_settings->port
536 = link_ksettings->base.port;
537 legacy_settings->phy_address
538 = link_ksettings->base.phy_address;
539 legacy_settings->autoneg
540 = link_ksettings->base.autoneg;
541 legacy_settings->mdio_support
542 = link_ksettings->base.mdio_support;
543 legacy_settings->eth_tp_mdix
544 = link_ksettings->base.eth_tp_mdix;
545 legacy_settings->eth_tp_mdix_ctrl
546 = link_ksettings->base.eth_tp_mdix_ctrl;
547 legacy_settings->transceiver
548 = link_ksettings->base.transceiver;
552 /* number of 32-bit words to store the user's link mode bitmaps */
553 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
554 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
556 /* layout of the struct passed from/to userland */
557 struct ethtool_link_usettings {
558 struct ethtool_link_settings base;
560 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
561 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
562 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
566 /* Internal kernel helper to query a device ethtool_link_settings.
568 * Backward compatibility note: for compatibility with legacy drivers
569 * that implement only the ethtool_cmd API, this has to work with both
570 * drivers implementing get_link_ksettings API and drivers
571 * implementing get_settings API. When drivers implement get_settings
572 * and report ethtool_cmd deprecated fields
573 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
574 * because the resulting struct ethtool_link_settings does not report them.
576 int __ethtool_get_link_ksettings(struct net_device *dev,
577 struct ethtool_link_ksettings *link_ksettings)
580 struct ethtool_cmd cmd;
584 if (dev->ethtool_ops->get_link_ksettings) {
585 memset(link_ksettings, 0, sizeof(*link_ksettings));
586 return dev->ethtool_ops->get_link_ksettings(dev,
590 /* driver doesn't support %ethtool_link_ksettings API. revert to
591 * legacy %ethtool_cmd API, unless it's not supported either.
592 * TODO: remove when ethtool_ops::get_settings disappears internally
594 if (!dev->ethtool_ops->get_settings)
597 memset(&cmd, 0, sizeof(cmd));
598 cmd.cmd = ETHTOOL_GSET;
599 err = dev->ethtool_ops->get_settings(dev, &cmd);
603 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
605 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
608 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
610 /* convert ethtool_link_usettings in user space to a kernel internal
611 * ethtool_link_ksettings. return 0 on success, errno on error.
613 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
614 const void __user *from)
616 struct ethtool_link_usettings link_usettings;
618 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
621 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
622 bitmap_from_arr32(to->link_modes.supported,
623 link_usettings.link_modes.supported,
624 __ETHTOOL_LINK_MODE_MASK_NBITS);
625 bitmap_from_arr32(to->link_modes.advertising,
626 link_usettings.link_modes.advertising,
627 __ETHTOOL_LINK_MODE_MASK_NBITS);
628 bitmap_from_arr32(to->link_modes.lp_advertising,
629 link_usettings.link_modes.lp_advertising,
630 __ETHTOOL_LINK_MODE_MASK_NBITS);
635 /* convert a kernel internal ethtool_link_ksettings to
636 * ethtool_link_usettings in user space. return 0 on success, errno on
640 store_link_ksettings_for_user(void __user *to,
641 const struct ethtool_link_ksettings *from)
643 struct ethtool_link_usettings link_usettings;
645 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
646 bitmap_to_arr32(link_usettings.link_modes.supported,
647 from->link_modes.supported,
648 __ETHTOOL_LINK_MODE_MASK_NBITS);
649 bitmap_to_arr32(link_usettings.link_modes.advertising,
650 from->link_modes.advertising,
651 __ETHTOOL_LINK_MODE_MASK_NBITS);
652 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
653 from->link_modes.lp_advertising,
654 __ETHTOOL_LINK_MODE_MASK_NBITS);
656 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
662 /* Query device for its ethtool_link_settings.
664 * Backward compatibility note: this function must fail when driver
665 * does not implement ethtool::get_link_ksettings, even if legacy
666 * ethtool_ops::get_settings is implemented. This tells new versions
667 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
668 * this driver, so that they can correctly access the ethtool_cmd
669 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
670 * implements ethtool_ops::get_settings anymore.
672 static int ethtool_get_link_ksettings(struct net_device *dev,
673 void __user *useraddr)
676 struct ethtool_link_ksettings link_ksettings;
680 if (!dev->ethtool_ops->get_link_ksettings)
683 /* handle bitmap nbits handshake */
684 if (copy_from_user(&link_ksettings.base, useraddr,
685 sizeof(link_ksettings.base)))
688 if (__ETHTOOL_LINK_MODE_MASK_NU32
689 != link_ksettings.base.link_mode_masks_nwords) {
690 /* wrong link mode nbits requested */
691 memset(&link_ksettings, 0, sizeof(link_ksettings));
692 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
693 /* send back number of words required as negative val */
694 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
695 "need too many bits for link modes!");
696 link_ksettings.base.link_mode_masks_nwords
697 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
699 /* copy the base fields back to user, not the link
702 if (copy_to_user(useraddr, &link_ksettings.base,
703 sizeof(link_ksettings.base)))
709 /* handshake successful: user/kernel agree on
710 * link_mode_masks_nwords
713 memset(&link_ksettings, 0, sizeof(link_ksettings));
714 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
718 /* make sure we tell the right values to user */
719 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
720 link_ksettings.base.link_mode_masks_nwords
721 = __ETHTOOL_LINK_MODE_MASK_NU32;
723 return store_link_ksettings_for_user(useraddr, &link_ksettings);
726 /* Update device ethtool_link_settings.
728 * Backward compatibility note: this function must fail when driver
729 * does not implement ethtool::set_link_ksettings, even if legacy
730 * ethtool_ops::set_settings is implemented. This tells new versions
731 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
732 * this driver, so that they can correctly update the ethtool_cmd
733 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
734 * implements ethtool_ops::get_settings anymore.
736 static int ethtool_set_link_ksettings(struct net_device *dev,
737 void __user *useraddr)
740 struct ethtool_link_ksettings link_ksettings;
744 if (!dev->ethtool_ops->set_link_ksettings)
747 /* make sure nbits field has expected value */
748 if (copy_from_user(&link_ksettings.base, useraddr,
749 sizeof(link_ksettings.base)))
752 if (__ETHTOOL_LINK_MODE_MASK_NU32
753 != link_ksettings.base.link_mode_masks_nwords)
756 /* copy the whole structure, now that we know it has expected
759 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
763 /* re-check nwords field, just in case */
764 if (__ETHTOOL_LINK_MODE_MASK_NU32
765 != link_ksettings.base.link_mode_masks_nwords)
768 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
771 /* Query device for its ethtool_cmd settings.
773 * Backward compatibility note: for compatibility with legacy ethtool,
774 * this has to work with both drivers implementing get_link_ksettings
775 * API and drivers implementing get_settings API. When drivers
776 * implement get_link_ksettings and report higher link mode bits, a
777 * kernel warning is logged once (with name of 1st driver/device) to
778 * recommend user to upgrade ethtool, but the command is successful
779 * (only the lower link mode bits reported back to user).
781 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
783 struct ethtool_cmd cmd;
787 if (dev->ethtool_ops->get_link_ksettings) {
788 /* First, use link_ksettings API if it is supported */
790 struct ethtool_link_ksettings link_ksettings;
792 memset(&link_ksettings, 0, sizeof(link_ksettings));
793 err = dev->ethtool_ops->get_link_ksettings(dev,
797 convert_link_ksettings_to_legacy_settings(&cmd,
800 /* send a sensible cmd tag back to user */
801 cmd.cmd = ETHTOOL_GSET;
803 /* driver doesn't support %ethtool_link_ksettings
804 * API. revert to legacy %ethtool_cmd API, unless it's
805 * not supported either.
809 if (!dev->ethtool_ops->get_settings)
812 memset(&cmd, 0, sizeof(cmd));
813 cmd.cmd = ETHTOOL_GSET;
814 err = dev->ethtool_ops->get_settings(dev, &cmd);
819 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
825 /* Update device link settings with given ethtool_cmd.
827 * Backward compatibility note: for compatibility with legacy ethtool,
828 * this has to work with both drivers implementing set_link_ksettings
829 * API and drivers implementing set_settings API. When drivers
830 * implement set_link_ksettings and user's request updates deprecated
831 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
832 * warning is logged once (with name of 1st driver/device) to
833 * recommend user to upgrade ethtool, and the request is rejected.
835 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
837 struct ethtool_cmd cmd;
841 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
844 /* first, try new %ethtool_link_ksettings API. */
845 if (dev->ethtool_ops->set_link_ksettings) {
846 struct ethtool_link_ksettings link_ksettings;
848 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
852 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
853 link_ksettings.base.link_mode_masks_nwords
854 = __ETHTOOL_LINK_MODE_MASK_NU32;
855 return dev->ethtool_ops->set_link_ksettings(dev,
859 /* legacy %ethtool_cmd API */
861 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
862 * disappears internally
865 if (!dev->ethtool_ops->set_settings)
868 return dev->ethtool_ops->set_settings(dev, &cmd);
871 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
872 void __user *useraddr)
874 struct ethtool_drvinfo info;
875 const struct ethtool_ops *ops = dev->ethtool_ops;
877 memset(&info, 0, sizeof(info));
878 info.cmd = ETHTOOL_GDRVINFO;
879 if (ops->get_drvinfo) {
880 ops->get_drvinfo(dev, &info);
881 } else if (dev->dev.parent && dev->dev.parent->driver) {
882 strlcpy(info.bus_info, dev_name(dev->dev.parent),
883 sizeof(info.bus_info));
884 strlcpy(info.driver, dev->dev.parent->driver->name,
885 sizeof(info.driver));
891 * this method of obtaining string set info is deprecated;
892 * Use ETHTOOL_GSSET_INFO instead.
894 if (ops->get_sset_count) {
897 rc = ops->get_sset_count(dev, ETH_SS_TEST);
899 info.testinfo_len = rc;
900 rc = ops->get_sset_count(dev, ETH_SS_STATS);
903 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
905 info.n_priv_flags = rc;
907 if (ops->get_regs_len)
908 info.regdump_len = ops->get_regs_len(dev);
909 if (ops->get_eeprom_len)
910 info.eedump_len = ops->get_eeprom_len(dev);
912 if (copy_to_user(useraddr, &info, sizeof(info)))
917 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
918 void __user *useraddr)
920 struct ethtool_sset_info info;
922 int i, idx = 0, n_bits = 0, ret, rc;
923 u32 *info_buf = NULL;
925 if (copy_from_user(&info, useraddr, sizeof(info)))
928 /* store copy of mask, because we zero struct later on */
929 sset_mask = info.sset_mask;
933 /* calculate size of return buffer */
934 n_bits = hweight64(sset_mask);
936 memset(&info, 0, sizeof(info));
937 info.cmd = ETHTOOL_GSSET_INFO;
939 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
944 * fill return buffer based on input bitmask and successful
945 * get_sset_count return
947 for (i = 0; i < 64; i++) {
948 if (!(sset_mask & (1ULL << i)))
951 rc = __ethtool_get_sset_count(dev, i);
953 info.sset_mask |= (1ULL << i);
954 info_buf[idx++] = rc;
959 if (copy_to_user(useraddr, &info, sizeof(info)))
962 useraddr += offsetof(struct ethtool_sset_info, data);
963 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
973 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
974 u32 cmd, void __user *useraddr)
976 struct ethtool_rxnfc info;
977 size_t info_size = sizeof(info);
980 if (!dev->ethtool_ops->set_rxnfc)
983 /* struct ethtool_rxnfc was originally defined for
984 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
985 * members. User-space might still be using that
987 if (cmd == ETHTOOL_SRXFH)
988 info_size = (offsetof(struct ethtool_rxnfc, data) +
991 if (copy_from_user(&info, useraddr, info_size))
994 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
998 if (cmd == ETHTOOL_SRXCLSRLINS &&
999 copy_to_user(useraddr, &info, info_size))
1005 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
1006 u32 cmd, void __user *useraddr)
1008 struct ethtool_rxnfc info;
1009 size_t info_size = sizeof(info);
1010 const struct ethtool_ops *ops = dev->ethtool_ops;
1012 void *rule_buf = NULL;
1014 if (!ops->get_rxnfc)
1017 /* struct ethtool_rxnfc was originally defined for
1018 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1019 * members. User-space might still be using that
1021 if (cmd == ETHTOOL_GRXFH)
1022 info_size = (offsetof(struct ethtool_rxnfc, data) +
1025 if (copy_from_user(&info, useraddr, info_size))
1028 /* If FLOW_RSS was requested then user-space must be using the
1029 * new definition, as FLOW_RSS is newer.
1031 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1032 info_size = sizeof(info);
1033 if (copy_from_user(&info, useraddr, info_size))
1037 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1038 if (info.rule_cnt > 0) {
1039 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1040 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
1047 ret = ops->get_rxnfc(dev, &info, rule_buf);
1052 if (copy_to_user(useraddr, &info, info_size))
1056 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1057 if (copy_to_user(useraddr, rule_buf,
1058 info.rule_cnt * sizeof(u32)))
1069 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1070 struct ethtool_rxnfc *rx_rings,
1075 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1078 /* Validate ring indices */
1079 for (i = 0; i < size; i++)
1080 if (indir[i] >= rx_rings->data)
1086 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1088 void netdev_rss_key_fill(void *buffer, size_t len)
1090 BUG_ON(len > sizeof(netdev_rss_key));
1091 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1092 memcpy(buffer, netdev_rss_key, len);
1094 EXPORT_SYMBOL(netdev_rss_key_fill);
1096 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1098 u32 dev_size, current_max = 0;
1102 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1103 !dev->ethtool_ops->get_rxfh)
1105 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1109 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1113 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1118 current_max = max(current_max, indir[dev_size]);
1127 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1128 void __user *useraddr)
1130 u32 user_size, dev_size;
1134 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1135 !dev->ethtool_ops->get_rxfh)
1137 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1141 if (copy_from_user(&user_size,
1142 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1146 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1147 &dev_size, sizeof(dev_size)))
1150 /* If the user buffer size is 0, this is just a query for the
1151 * device table size. Otherwise, if it's smaller than the
1152 * device table size it's an error.
1154 if (user_size < dev_size)
1155 return user_size == 0 ? 0 : -EINVAL;
1157 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1161 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1165 if (copy_to_user(useraddr +
1166 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1167 indir, dev_size * sizeof(indir[0])))
1175 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1176 void __user *useraddr)
1178 struct ethtool_rxnfc rx_rings;
1179 u32 user_size, dev_size, i;
1181 const struct ethtool_ops *ops = dev->ethtool_ops;
1183 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1185 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1189 dev_size = ops->get_rxfh_indir_size(dev);
1193 if (copy_from_user(&user_size,
1194 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1198 if (user_size != 0 && user_size != dev_size)
1201 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1205 rx_rings.cmd = ETHTOOL_GRXRINGS;
1206 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1210 if (user_size == 0) {
1211 for (i = 0; i < dev_size; i++)
1212 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1214 ret = ethtool_copy_validate_indir(indir,
1215 useraddr + ringidx_offset,
1222 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1226 /* indicate whether rxfh was set to default */
1228 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1230 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1237 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1238 void __user *useraddr)
1241 const struct ethtool_ops *ops = dev->ethtool_ops;
1242 u32 user_indir_size, user_key_size;
1243 u32 dev_indir_size = 0, dev_key_size = 0;
1244 struct ethtool_rxfh rxfh;
1255 if (ops->get_rxfh_indir_size)
1256 dev_indir_size = ops->get_rxfh_indir_size(dev);
1257 if (ops->get_rxfh_key_size)
1258 dev_key_size = ops->get_rxfh_key_size(dev);
1260 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1262 user_indir_size = rxfh.indir_size;
1263 user_key_size = rxfh.key_size;
1265 /* Check that reserved fields are 0 for now */
1266 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1268 /* Most drivers don't handle rss_context, check it's 0 as well */
1269 if (rxfh.rss_context && !ops->get_rxfh_context)
1272 rxfh.indir_size = dev_indir_size;
1273 rxfh.key_size = dev_key_size;
1274 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1277 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1278 (user_key_size && (user_key_size != dev_key_size)))
1281 indir_bytes = user_indir_size * sizeof(indir[0]);
1282 total_size = indir_bytes + user_key_size;
1283 rss_config = kzalloc(total_size, GFP_USER);
1287 if (user_indir_size)
1288 indir = (u32 *)rss_config;
1291 hkey = rss_config + indir_bytes;
1293 if (rxfh.rss_context)
1294 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1298 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1302 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1303 &dev_hfunc, sizeof(rxfh.hfunc))) {
1305 } else if (copy_to_user(useraddr +
1306 offsetof(struct ethtool_rxfh, rss_config[0]),
1307 rss_config, total_size)) {
1316 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1317 void __user *useraddr)
1320 const struct ethtool_ops *ops = dev->ethtool_ops;
1321 struct ethtool_rxnfc rx_rings;
1322 struct ethtool_rxfh rxfh;
1323 u32 dev_indir_size = 0, dev_key_size = 0, i;
1324 u32 *indir = NULL, indir_bytes = 0;
1327 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1328 bool delete = false;
1330 if (!ops->get_rxnfc || !ops->set_rxfh)
1333 if (ops->get_rxfh_indir_size)
1334 dev_indir_size = ops->get_rxfh_indir_size(dev);
1335 if (ops->get_rxfh_key_size)
1336 dev_key_size = ops->get_rxfh_key_size(dev);
1338 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1341 /* Check that reserved fields are 0 for now */
1342 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1344 /* Most drivers don't handle rss_context, check it's 0 as well */
1345 if (rxfh.rss_context && !ops->set_rxfh_context)
1348 /* If either indir, hash key or function is valid, proceed further.
1349 * Must request at least one change: indir size, hash key or function.
1351 if ((rxfh.indir_size &&
1352 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1353 rxfh.indir_size != dev_indir_size) ||
1354 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1355 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1356 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1359 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1360 indir_bytes = dev_indir_size * sizeof(indir[0]);
1362 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1366 rx_rings.cmd = ETHTOOL_GRXRINGS;
1367 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1371 /* rxfh.indir_size == 0 means reset the indir table to default (master
1372 * context) or delete the context (other RSS contexts).
1373 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1375 if (rxfh.indir_size &&
1376 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1377 indir = (u32 *)rss_config;
1378 ret = ethtool_copy_validate_indir(indir,
1379 useraddr + rss_cfg_offset,
1384 } else if (rxfh.indir_size == 0) {
1385 if (rxfh.rss_context == 0) {
1386 indir = (u32 *)rss_config;
1387 for (i = 0; i < dev_indir_size; i++)
1388 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1394 if (rxfh.key_size) {
1395 hkey = rss_config + indir_bytes;
1396 if (copy_from_user(hkey,
1397 useraddr + rss_cfg_offset + indir_bytes,
1404 if (rxfh.rss_context)
1405 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1406 &rxfh.rss_context, delete);
1408 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1412 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1413 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1416 if (!rxfh.rss_context) {
1417 /* indicate whether rxfh was set to default */
1418 if (rxfh.indir_size == 0)
1419 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1420 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1421 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1429 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1431 struct ethtool_regs regs;
1432 const struct ethtool_ops *ops = dev->ethtool_ops;
1436 if (!ops->get_regs || !ops->get_regs_len)
1439 if (copy_from_user(®s, useraddr, sizeof(regs)))
1442 reglen = ops->get_regs_len(dev);
1443 if (regs.len > reglen)
1448 regbuf = vzalloc(reglen);
1453 ops->get_regs(dev, ®s, regbuf);
1456 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1458 useraddr += offsetof(struct ethtool_regs, data);
1459 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1468 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1470 struct ethtool_value reset;
1473 if (!dev->ethtool_ops->reset)
1476 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1479 ret = dev->ethtool_ops->reset(dev, &reset.data);
1483 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1488 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1490 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1492 if (!dev->ethtool_ops->get_wol)
1495 dev->ethtool_ops->get_wol(dev, &wol);
1497 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1502 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1504 struct ethtool_wolinfo wol;
1506 if (!dev->ethtool_ops->set_wol)
1509 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1512 return dev->ethtool_ops->set_wol(dev, &wol);
1515 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1517 struct ethtool_eee edata;
1520 if (!dev->ethtool_ops->get_eee)
1523 memset(&edata, 0, sizeof(struct ethtool_eee));
1524 edata.cmd = ETHTOOL_GEEE;
1525 rc = dev->ethtool_ops->get_eee(dev, &edata);
1530 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1536 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1538 struct ethtool_eee edata;
1540 if (!dev->ethtool_ops->set_eee)
1543 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1546 return dev->ethtool_ops->set_eee(dev, &edata);
1549 static int ethtool_nway_reset(struct net_device *dev)
1551 if (!dev->ethtool_ops->nway_reset)
1554 return dev->ethtool_ops->nway_reset(dev);
1557 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1559 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1561 if (!dev->ethtool_ops->get_link)
1564 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1566 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1571 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1572 int (*getter)(struct net_device *,
1573 struct ethtool_eeprom *, u8 *),
1576 struct ethtool_eeprom eeprom;
1577 void __user *userbuf = useraddr + sizeof(eeprom);
1578 u32 bytes_remaining;
1582 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1585 /* Check for wrap and zero */
1586 if (eeprom.offset + eeprom.len <= eeprom.offset)
1589 /* Check for exceeding total eeprom len */
1590 if (eeprom.offset + eeprom.len > total_len)
1593 data = kmalloc(PAGE_SIZE, GFP_USER);
1597 bytes_remaining = eeprom.len;
1598 while (bytes_remaining > 0) {
1599 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1601 ret = getter(dev, &eeprom, data);
1604 if (copy_to_user(userbuf, data, eeprom.len)) {
1608 userbuf += eeprom.len;
1609 eeprom.offset += eeprom.len;
1610 bytes_remaining -= eeprom.len;
1613 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1614 eeprom.offset -= eeprom.len;
1615 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1622 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1624 const struct ethtool_ops *ops = dev->ethtool_ops;
1626 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1627 !ops->get_eeprom_len(dev))
1630 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1631 ops->get_eeprom_len(dev));
1634 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1636 struct ethtool_eeprom eeprom;
1637 const struct ethtool_ops *ops = dev->ethtool_ops;
1638 void __user *userbuf = useraddr + sizeof(eeprom);
1639 u32 bytes_remaining;
1643 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1644 !ops->get_eeprom_len(dev))
1647 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1650 /* Check for wrap and zero */
1651 if (eeprom.offset + eeprom.len <= eeprom.offset)
1654 /* Check for exceeding total eeprom len */
1655 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1658 data = kmalloc(PAGE_SIZE, GFP_USER);
1662 bytes_remaining = eeprom.len;
1663 while (bytes_remaining > 0) {
1664 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1666 if (copy_from_user(data, userbuf, eeprom.len)) {
1670 ret = ops->set_eeprom(dev, &eeprom, data);
1673 userbuf += eeprom.len;
1674 eeprom.offset += eeprom.len;
1675 bytes_remaining -= eeprom.len;
1682 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1683 void __user *useraddr)
1685 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1687 if (!dev->ethtool_ops->get_coalesce)
1690 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1692 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1697 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1698 void __user *useraddr)
1700 struct ethtool_coalesce coalesce;
1702 if (!dev->ethtool_ops->set_coalesce)
1705 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1708 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1711 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1713 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1715 if (!dev->ethtool_ops->get_ringparam)
1718 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1720 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1725 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1727 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1729 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1732 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1735 dev->ethtool_ops->get_ringparam(dev, &max);
1737 /* ensure new ring parameters are within the maximums */
1738 if (ringparam.rx_pending > max.rx_max_pending ||
1739 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1740 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1741 ringparam.tx_pending > max.tx_max_pending)
1744 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1747 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1748 void __user *useraddr)
1750 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1752 if (!dev->ethtool_ops->get_channels)
1755 dev->ethtool_ops->get_channels(dev, &channels);
1757 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1762 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1763 void __user *useraddr)
1765 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
1766 u32 max_rx_in_use = 0;
1768 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1771 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1774 dev->ethtool_ops->get_channels(dev, &max);
1776 /* ensure new counts are within the maximums */
1777 if ((channels.rx_count > max.max_rx) ||
1778 (channels.tx_count > max.max_tx) ||
1779 (channels.combined_count > max.max_combined) ||
1780 (channels.other_count > max.max_other))
1783 /* ensure the new Rx count fits within the configured Rx flow
1784 * indirection table settings */
1785 if (netif_is_rxfh_configured(dev) &&
1786 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1787 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1790 return dev->ethtool_ops->set_channels(dev, &channels);
1793 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1795 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1797 if (!dev->ethtool_ops->get_pauseparam)
1800 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1802 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1807 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1809 struct ethtool_pauseparam pauseparam;
1811 if (!dev->ethtool_ops->set_pauseparam)
1814 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1817 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1820 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1822 struct ethtool_test test;
1823 const struct ethtool_ops *ops = dev->ethtool_ops;
1827 if (!ops->self_test || !ops->get_sset_count)
1830 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1833 WARN_ON(test_len == 0);
1835 if (copy_from_user(&test, useraddr, sizeof(test)))
1838 test.len = test_len;
1839 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1843 ops->self_test(dev, &test, data);
1846 if (copy_to_user(useraddr, &test, sizeof(test)))
1848 useraddr += sizeof(test);
1849 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1858 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1860 struct ethtool_gstrings gstrings;
1864 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1867 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1870 if (ret > S32_MAX / ETH_GSTRING_LEN)
1875 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1876 if (gstrings.len && !data)
1879 __ethtool_get_strings(dev, gstrings.string_set, data);
1882 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1884 useraddr += sizeof(gstrings);
1886 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1895 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1897 struct ethtool_value id;
1899 const struct ethtool_ops *ops = dev->ethtool_ops;
1902 if (!ops->set_phys_id)
1908 if (copy_from_user(&id, useraddr, sizeof(id)))
1911 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1915 /* Drop the RTNL lock while waiting, but prevent reentry or
1916 * removal of the device.
1923 /* Driver will handle this itself */
1924 schedule_timeout_interruptible(
1925 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1927 /* Driver expects to be called at twice the frequency in rc */
1928 int n = rc * 2, i, interval = HZ / n;
1930 /* Count down seconds */
1932 /* Count down iterations per second */
1936 rc = ops->set_phys_id(dev,
1937 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1941 schedule_timeout_interruptible(interval);
1942 } while (!signal_pending(current) && --i != 0);
1943 } while (!signal_pending(current) &&
1944 (id.data == 0 || --id.data != 0));
1951 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1955 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1957 struct ethtool_stats stats;
1958 const struct ethtool_ops *ops = dev->ethtool_ops;
1962 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1965 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1968 if (n_stats > S32_MAX / sizeof(u64))
1970 WARN_ON_ONCE(!n_stats);
1971 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1974 stats.n_stats = n_stats;
1975 data = vzalloc(n_stats * sizeof(u64));
1976 if (n_stats && !data)
1979 ops->get_ethtool_stats(dev, &stats, data);
1982 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1984 useraddr += sizeof(stats);
1985 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1994 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1996 struct ethtool_stats stats;
1997 struct phy_device *phydev = dev->phydev;
2004 n_stats = phy_get_sset_count(phydev);
2007 if (n_stats > S32_MAX / sizeof(u64))
2009 WARN_ON_ONCE(!n_stats);
2011 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2014 stats.n_stats = n_stats;
2015 data = vzalloc(n_stats * sizeof(u64));
2016 if (n_stats && !data)
2019 mutex_lock(&phydev->lock);
2020 phydev->drv->get_stats(phydev, &stats, data);
2021 mutex_unlock(&phydev->lock);
2024 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2026 useraddr += sizeof(stats);
2027 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
2036 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2038 struct ethtool_perm_addr epaddr;
2040 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2043 if (epaddr.size < dev->addr_len)
2045 epaddr.size = dev->addr_len;
2047 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2049 useraddr += sizeof(epaddr);
2050 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2055 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2056 u32 cmd, u32 (*actor)(struct net_device *))
2058 struct ethtool_value edata = { .cmd = cmd };
2063 edata.data = actor(dev);
2065 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2070 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2071 void (*actor)(struct net_device *, u32))
2073 struct ethtool_value edata;
2078 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2081 actor(dev, edata.data);
2085 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2086 int (*actor)(struct net_device *, u32))
2088 struct ethtool_value edata;
2093 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2096 return actor(dev, edata.data);
2099 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2100 char __user *useraddr)
2102 struct ethtool_flash efl;
2104 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2107 if (!dev->ethtool_ops->flash_device)
2110 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2112 return dev->ethtool_ops->flash_device(dev, &efl);
2115 static int ethtool_set_dump(struct net_device *dev,
2116 void __user *useraddr)
2118 struct ethtool_dump dump;
2120 if (!dev->ethtool_ops->set_dump)
2123 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2126 return dev->ethtool_ops->set_dump(dev, &dump);
2129 static int ethtool_get_dump_flag(struct net_device *dev,
2130 void __user *useraddr)
2133 struct ethtool_dump dump;
2134 const struct ethtool_ops *ops = dev->ethtool_ops;
2136 if (!ops->get_dump_flag)
2139 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2142 ret = ops->get_dump_flag(dev, &dump);
2146 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2151 static int ethtool_get_dump_data(struct net_device *dev,
2152 void __user *useraddr)
2156 struct ethtool_dump dump, tmp;
2157 const struct ethtool_ops *ops = dev->ethtool_ops;
2160 if (!ops->get_dump_data || !ops->get_dump_flag)
2163 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2166 memset(&tmp, 0, sizeof(tmp));
2167 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2168 ret = ops->get_dump_flag(dev, &tmp);
2172 len = min(tmp.len, dump.len);
2176 /* Don't ever let the driver think there's more space available
2177 * than it requested with .get_dump_flag().
2181 /* Always allocate enough space to hold the whole thing so that the
2182 * driver does not need to check the length and bother with partial
2185 data = vzalloc(tmp.len);
2188 ret = ops->get_dump_data(dev, &dump, data);
2192 /* There are two sane possibilities:
2193 * 1. The driver's .get_dump_data() does not touch dump.len.
2194 * 2. Or it may set dump.len to how much it really writes, which
2195 * should be tmp.len (or len if it can do a partial dump).
2196 * In any case respond to userspace with the actual length of data
2199 WARN_ON(dump.len != len && dump.len != tmp.len);
2202 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2206 useraddr += offsetof(struct ethtool_dump, data);
2207 if (copy_to_user(useraddr, data, len))
2214 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2217 struct ethtool_ts_info info;
2218 const struct ethtool_ops *ops = dev->ethtool_ops;
2219 struct phy_device *phydev = dev->phydev;
2221 memset(&info, 0, sizeof(info));
2222 info.cmd = ETHTOOL_GET_TS_INFO;
2224 if (phydev && phydev->drv && phydev->drv->ts_info) {
2225 err = phydev->drv->ts_info(phydev, &info);
2226 } else if (ops->get_ts_info) {
2227 err = ops->get_ts_info(dev, &info);
2229 info.so_timestamping =
2230 SOF_TIMESTAMPING_RX_SOFTWARE |
2231 SOF_TIMESTAMPING_SOFTWARE;
2232 info.phc_index = -1;
2238 if (copy_to_user(useraddr, &info, sizeof(info)))
2244 static int __ethtool_get_module_info(struct net_device *dev,
2245 struct ethtool_modinfo *modinfo)
2247 const struct ethtool_ops *ops = dev->ethtool_ops;
2248 struct phy_device *phydev = dev->phydev;
2251 return sfp_get_module_info(dev->sfp_bus, modinfo);
2253 if (phydev && phydev->drv && phydev->drv->module_info)
2254 return phydev->drv->module_info(phydev, modinfo);
2256 if (ops->get_module_info)
2257 return ops->get_module_info(dev, modinfo);
2262 static int ethtool_get_module_info(struct net_device *dev,
2263 void __user *useraddr)
2266 struct ethtool_modinfo modinfo;
2268 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2271 ret = __ethtool_get_module_info(dev, &modinfo);
2275 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2281 static int __ethtool_get_module_eeprom(struct net_device *dev,
2282 struct ethtool_eeprom *ee, u8 *data)
2284 const struct ethtool_ops *ops = dev->ethtool_ops;
2285 struct phy_device *phydev = dev->phydev;
2288 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2290 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2291 return phydev->drv->module_eeprom(phydev, ee, data);
2293 if (ops->get_module_eeprom)
2294 return ops->get_module_eeprom(dev, ee, data);
2299 static int ethtool_get_module_eeprom(struct net_device *dev,
2300 void __user *useraddr)
2303 struct ethtool_modinfo modinfo;
2305 ret = __ethtool_get_module_info(dev, &modinfo);
2309 return ethtool_get_any_eeprom(dev, useraddr,
2310 __ethtool_get_module_eeprom,
2311 modinfo.eeprom_len);
2314 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2317 case ETHTOOL_RX_COPYBREAK:
2318 case ETHTOOL_TX_COPYBREAK:
2319 if (tuna->len != sizeof(u32) ||
2320 tuna->type_id != ETHTOOL_TUNABLE_U32)
2323 case ETHTOOL_PFC_PREVENTION_TOUT:
2324 if (tuna->len != sizeof(u16) ||
2325 tuna->type_id != ETHTOOL_TUNABLE_U16)
2335 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2338 struct ethtool_tunable tuna;
2339 const struct ethtool_ops *ops = dev->ethtool_ops;
2342 if (!ops->get_tunable)
2344 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2346 ret = ethtool_tunable_valid(&tuna);
2349 data = kmalloc(tuna.len, GFP_USER);
2352 ret = ops->get_tunable(dev, &tuna, data);
2355 useraddr += sizeof(tuna);
2357 if (copy_to_user(useraddr, data, tuna.len))
2366 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2369 struct ethtool_tunable tuna;
2370 const struct ethtool_ops *ops = dev->ethtool_ops;
2373 if (!ops->set_tunable)
2375 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2377 ret = ethtool_tunable_valid(&tuna);
2380 useraddr += sizeof(tuna);
2381 data = memdup_user(useraddr, tuna.len);
2383 return PTR_ERR(data);
2384 ret = ops->set_tunable(dev, &tuna, data);
2390 static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2391 void __user *useraddr,
2392 struct ethtool_per_queue_op *per_queue_opt)
2396 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2398 if (!dev->ethtool_ops->get_per_queue_coalesce)
2401 useraddr += sizeof(*per_queue_opt);
2403 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2406 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2407 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2409 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2412 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2414 useraddr += sizeof(coalesce);
2420 static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2421 void __user *useraddr,
2422 struct ethtool_per_queue_op *per_queue_opt)
2427 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2428 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2430 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2431 (!dev->ethtool_ops->get_per_queue_coalesce))
2434 useraddr += sizeof(*per_queue_opt);
2436 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2437 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2438 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2442 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2443 struct ethtool_coalesce coalesce;
2445 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2451 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2456 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2460 useraddr += sizeof(coalesce);
2466 for_each_set_bit(i, queue_mask, bit) {
2467 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2476 static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2478 struct ethtool_per_queue_op per_queue_opt;
2480 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2483 switch (per_queue_opt.sub_command) {
2484 case ETHTOOL_GCOALESCE:
2485 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2486 case ETHTOOL_SCOALESCE:
2487 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2493 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2496 case ETHTOOL_PHY_DOWNSHIFT:
2497 if (tuna->len != sizeof(u8) ||
2498 tuna->type_id != ETHTOOL_TUNABLE_U8)
2508 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2511 struct ethtool_tunable tuna;
2512 struct phy_device *phydev = dev->phydev;
2515 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2518 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2520 ret = ethtool_phy_tunable_valid(&tuna);
2523 data = kmalloc(tuna.len, GFP_USER);
2526 mutex_lock(&phydev->lock);
2527 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2528 mutex_unlock(&phydev->lock);
2531 useraddr += sizeof(tuna);
2533 if (copy_to_user(useraddr, data, tuna.len))
2542 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2545 struct ethtool_tunable tuna;
2546 struct phy_device *phydev = dev->phydev;
2549 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2551 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2553 ret = ethtool_phy_tunable_valid(&tuna);
2556 useraddr += sizeof(tuna);
2557 data = memdup_user(useraddr, tuna.len);
2559 return PTR_ERR(data);
2560 mutex_lock(&phydev->lock);
2561 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2562 mutex_unlock(&phydev->lock);
2568 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2570 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
2573 if (!dev->ethtool_ops->get_fecparam)
2576 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2580 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2585 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2587 struct ethtool_fecparam fecparam;
2589 if (!dev->ethtool_ops->set_fecparam)
2592 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2595 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2598 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2600 int dev_ethtool(struct net *net, struct ifreq *ifr)
2602 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2603 void __user *useraddr = ifr->ifr_data;
2604 u32 ethcmd, sub_cmd;
2606 netdev_features_t old_features;
2608 if (!dev || !netif_device_present(dev))
2611 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2614 if (ethcmd == ETHTOOL_PERQUEUE) {
2615 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2620 /* Allow some commands to be done by anyone */
2623 case ETHTOOL_GDRVINFO:
2624 case ETHTOOL_GMSGLVL:
2626 case ETHTOOL_GCOALESCE:
2627 case ETHTOOL_GRINGPARAM:
2628 case ETHTOOL_GPAUSEPARAM:
2629 case ETHTOOL_GRXCSUM:
2630 case ETHTOOL_GTXCSUM:
2632 case ETHTOOL_GSSET_INFO:
2633 case ETHTOOL_GSTRINGS:
2634 case ETHTOOL_GSTATS:
2635 case ETHTOOL_GPHYSTATS:
2637 case ETHTOOL_GPERMADDR:
2640 case ETHTOOL_GFLAGS:
2641 case ETHTOOL_GPFLAGS:
2643 case ETHTOOL_GRXRINGS:
2644 case ETHTOOL_GRXCLSRLCNT:
2645 case ETHTOOL_GRXCLSRULE:
2646 case ETHTOOL_GRXCLSRLALL:
2647 case ETHTOOL_GRXFHINDIR:
2649 case ETHTOOL_GFEATURES:
2650 case ETHTOOL_GCHANNELS:
2651 case ETHTOOL_GET_TS_INFO:
2653 case ETHTOOL_GTUNABLE:
2654 case ETHTOOL_PHY_GTUNABLE:
2655 case ETHTOOL_GLINKSETTINGS:
2656 case ETHTOOL_GFECPARAM:
2659 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2663 if (dev->ethtool_ops->begin) {
2664 rc = dev->ethtool_ops->begin(dev);
2668 old_features = dev->features;
2672 rc = ethtool_get_settings(dev, useraddr);
2675 rc = ethtool_set_settings(dev, useraddr);
2677 case ETHTOOL_GDRVINFO:
2678 rc = ethtool_get_drvinfo(dev, useraddr);
2681 rc = ethtool_get_regs(dev, useraddr);
2684 rc = ethtool_get_wol(dev, useraddr);
2687 rc = ethtool_set_wol(dev, useraddr);
2689 case ETHTOOL_GMSGLVL:
2690 rc = ethtool_get_value(dev, useraddr, ethcmd,
2691 dev->ethtool_ops->get_msglevel);
2693 case ETHTOOL_SMSGLVL:
2694 rc = ethtool_set_value_void(dev, useraddr,
2695 dev->ethtool_ops->set_msglevel);
2698 rc = ethtool_get_eee(dev, useraddr);
2701 rc = ethtool_set_eee(dev, useraddr);
2703 case ETHTOOL_NWAY_RST:
2704 rc = ethtool_nway_reset(dev);
2707 rc = ethtool_get_link(dev, useraddr);
2709 case ETHTOOL_GEEPROM:
2710 rc = ethtool_get_eeprom(dev, useraddr);
2712 case ETHTOOL_SEEPROM:
2713 rc = ethtool_set_eeprom(dev, useraddr);
2715 case ETHTOOL_GCOALESCE:
2716 rc = ethtool_get_coalesce(dev, useraddr);
2718 case ETHTOOL_SCOALESCE:
2719 rc = ethtool_set_coalesce(dev, useraddr);
2721 case ETHTOOL_GRINGPARAM:
2722 rc = ethtool_get_ringparam(dev, useraddr);
2724 case ETHTOOL_SRINGPARAM:
2725 rc = ethtool_set_ringparam(dev, useraddr);
2727 case ETHTOOL_GPAUSEPARAM:
2728 rc = ethtool_get_pauseparam(dev, useraddr);
2730 case ETHTOOL_SPAUSEPARAM:
2731 rc = ethtool_set_pauseparam(dev, useraddr);
2734 rc = ethtool_self_test(dev, useraddr);
2736 case ETHTOOL_GSTRINGS:
2737 rc = ethtool_get_strings(dev, useraddr);
2739 case ETHTOOL_PHYS_ID:
2740 rc = ethtool_phys_id(dev, useraddr);
2742 case ETHTOOL_GSTATS:
2743 rc = ethtool_get_stats(dev, useraddr);
2745 case ETHTOOL_GPERMADDR:
2746 rc = ethtool_get_perm_addr(dev, useraddr);
2748 case ETHTOOL_GFLAGS:
2749 rc = ethtool_get_value(dev, useraddr, ethcmd,
2750 __ethtool_get_flags);
2752 case ETHTOOL_SFLAGS:
2753 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2755 case ETHTOOL_GPFLAGS:
2756 rc = ethtool_get_value(dev, useraddr, ethcmd,
2757 dev->ethtool_ops->get_priv_flags);
2759 case ETHTOOL_SPFLAGS:
2760 rc = ethtool_set_value(dev, useraddr,
2761 dev->ethtool_ops->set_priv_flags);
2764 case ETHTOOL_GRXRINGS:
2765 case ETHTOOL_GRXCLSRLCNT:
2766 case ETHTOOL_GRXCLSRULE:
2767 case ETHTOOL_GRXCLSRLALL:
2768 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2771 case ETHTOOL_SRXCLSRLDEL:
2772 case ETHTOOL_SRXCLSRLINS:
2773 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2775 case ETHTOOL_FLASHDEV:
2776 rc = ethtool_flash_device(dev, useraddr);
2779 rc = ethtool_reset(dev, useraddr);
2781 case ETHTOOL_GSSET_INFO:
2782 rc = ethtool_get_sset_info(dev, useraddr);
2784 case ETHTOOL_GRXFHINDIR:
2785 rc = ethtool_get_rxfh_indir(dev, useraddr);
2787 case ETHTOOL_SRXFHINDIR:
2788 rc = ethtool_set_rxfh_indir(dev, useraddr);
2791 rc = ethtool_get_rxfh(dev, useraddr);
2794 rc = ethtool_set_rxfh(dev, useraddr);
2796 case ETHTOOL_GFEATURES:
2797 rc = ethtool_get_features(dev, useraddr);
2799 case ETHTOOL_SFEATURES:
2800 rc = ethtool_set_features(dev, useraddr);
2802 case ETHTOOL_GTXCSUM:
2803 case ETHTOOL_GRXCSUM:
2808 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2810 case ETHTOOL_STXCSUM:
2811 case ETHTOOL_SRXCSUM:
2816 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2818 case ETHTOOL_GCHANNELS:
2819 rc = ethtool_get_channels(dev, useraddr);
2821 case ETHTOOL_SCHANNELS:
2822 rc = ethtool_set_channels(dev, useraddr);
2824 case ETHTOOL_SET_DUMP:
2825 rc = ethtool_set_dump(dev, useraddr);
2827 case ETHTOOL_GET_DUMP_FLAG:
2828 rc = ethtool_get_dump_flag(dev, useraddr);
2830 case ETHTOOL_GET_DUMP_DATA:
2831 rc = ethtool_get_dump_data(dev, useraddr);
2833 case ETHTOOL_GET_TS_INFO:
2834 rc = ethtool_get_ts_info(dev, useraddr);
2836 case ETHTOOL_GMODULEINFO:
2837 rc = ethtool_get_module_info(dev, useraddr);
2839 case ETHTOOL_GMODULEEEPROM:
2840 rc = ethtool_get_module_eeprom(dev, useraddr);
2842 case ETHTOOL_GTUNABLE:
2843 rc = ethtool_get_tunable(dev, useraddr);
2845 case ETHTOOL_STUNABLE:
2846 rc = ethtool_set_tunable(dev, useraddr);
2848 case ETHTOOL_GPHYSTATS:
2849 rc = ethtool_get_phy_stats(dev, useraddr);
2851 case ETHTOOL_PERQUEUE:
2852 rc = ethtool_set_per_queue(dev, useraddr);
2854 case ETHTOOL_GLINKSETTINGS:
2855 rc = ethtool_get_link_ksettings(dev, useraddr);
2857 case ETHTOOL_SLINKSETTINGS:
2858 rc = ethtool_set_link_ksettings(dev, useraddr);
2860 case ETHTOOL_PHY_GTUNABLE:
2861 rc = get_phy_tunable(dev, useraddr);
2863 case ETHTOOL_PHY_STUNABLE:
2864 rc = set_phy_tunable(dev, useraddr);
2866 case ETHTOOL_GFECPARAM:
2867 rc = ethtool_get_fecparam(dev, useraddr);
2869 case ETHTOOL_SFECPARAM:
2870 rc = ethtool_set_fecparam(dev, useraddr);
2876 if (dev->ethtool_ops->complete)
2877 dev->ethtool_ops->complete(dev);
2879 if (old_features != dev->features)
2880 netdev_features_change(dev);