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",
95 [NETIF_F_GSO_UDP_L4_BIT] = "tx-udp-segmentation",
97 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
98 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
99 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
100 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
101 [NETIF_F_RXHASH_BIT] = "rx-hashing",
102 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
103 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
104 [NETIF_F_LOOPBACK_BIT] = "loopback",
105 [NETIF_F_RXFCS_BIT] = "rx-fcs",
106 [NETIF_F_RXALL_BIT] = "rx-all",
107 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
108 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
109 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
110 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
111 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
112 [NETIF_F_HW_TLS_RECORD_BIT] = "tls-hw-record",
113 [NETIF_F_HW_TLS_TX_BIT] = "tls-hw-tx-offload",
114 [NETIF_F_HW_TLS_RX_BIT] = "tls-hw-rx-offload",
118 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
119 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
120 [ETH_RSS_HASH_XOR_BIT] = "xor",
121 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
125 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
126 [ETHTOOL_ID_UNSPEC] = "Unspec",
127 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
128 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
129 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
133 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
134 [ETHTOOL_ID_UNSPEC] = "Unspec",
135 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
138 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
140 struct ethtool_gfeatures cmd = {
141 .cmd = ETHTOOL_GFEATURES,
142 .size = ETHTOOL_DEV_FEATURE_WORDS,
144 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
145 u32 __user *sizeaddr;
149 /* in case feature bits run out again */
150 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
152 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
153 features[i].available = (u32)(dev->hw_features >> (32 * i));
154 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
155 features[i].active = (u32)(dev->features >> (32 * i));
156 features[i].never_changed =
157 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
160 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
161 if (get_user(copy_size, sizeaddr))
164 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
165 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
167 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
169 useraddr += sizeof(cmd);
170 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
176 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
178 struct ethtool_sfeatures cmd;
179 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
180 netdev_features_t wanted = 0, valid = 0;
183 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
185 useraddr += sizeof(cmd);
187 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
190 if (copy_from_user(features, useraddr, sizeof(features)))
193 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
194 valid |= (netdev_features_t)features[i].valid << (32 * i);
195 wanted |= (netdev_features_t)features[i].requested << (32 * i);
198 if (valid & ~NETIF_F_ETHTOOL_BITS)
201 if (valid & ~dev->hw_features) {
202 valid &= dev->hw_features;
203 ret |= ETHTOOL_F_UNSUPPORTED;
206 dev->wanted_features &= ~valid;
207 dev->wanted_features |= wanted & valid;
208 __netdev_update_features(dev);
210 if ((dev->wanted_features ^ dev->features) & valid)
211 ret |= ETHTOOL_F_WISH;
216 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
218 const struct ethtool_ops *ops = dev->ethtool_ops;
220 if (sset == ETH_SS_FEATURES)
221 return ARRAY_SIZE(netdev_features_strings);
223 if (sset == ETH_SS_RSS_HASH_FUNCS)
224 return ARRAY_SIZE(rss_hash_func_strings);
226 if (sset == ETH_SS_TUNABLES)
227 return ARRAY_SIZE(tunable_strings);
229 if (sset == ETH_SS_PHY_TUNABLES)
230 return ARRAY_SIZE(phy_tunable_strings);
232 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
233 !ops->get_ethtool_phy_stats)
234 return phy_ethtool_get_sset_count(dev->phydev);
236 if (ops->get_sset_count && ops->get_strings)
237 return ops->get_sset_count(dev, sset);
242 static void __ethtool_get_strings(struct net_device *dev,
243 u32 stringset, u8 *data)
245 const struct ethtool_ops *ops = dev->ethtool_ops;
247 if (stringset == ETH_SS_FEATURES)
248 memcpy(data, netdev_features_strings,
249 sizeof(netdev_features_strings));
250 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
251 memcpy(data, rss_hash_func_strings,
252 sizeof(rss_hash_func_strings));
253 else if (stringset == ETH_SS_TUNABLES)
254 memcpy(data, tunable_strings, sizeof(tunable_strings));
255 else if (stringset == ETH_SS_PHY_TUNABLES)
256 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
257 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
258 !ops->get_ethtool_phy_stats)
259 phy_ethtool_get_strings(dev->phydev, data);
261 /* ops->get_strings is valid because checked earlier */
262 ops->get_strings(dev, stringset, data);
265 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
267 /* feature masks of legacy discrete ethtool ops */
270 case ETHTOOL_GTXCSUM:
271 case ETHTOOL_STXCSUM:
272 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
273 case ETHTOOL_GRXCSUM:
274 case ETHTOOL_SRXCSUM:
275 return NETIF_F_RXCSUM;
281 return NETIF_F_ALL_TSO;
293 static int ethtool_get_one_feature(struct net_device *dev,
294 char __user *useraddr, u32 ethcmd)
296 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
297 struct ethtool_value edata = {
299 .data = !!(dev->features & mask),
302 if (copy_to_user(useraddr, &edata, sizeof(edata)))
307 static int ethtool_set_one_feature(struct net_device *dev,
308 void __user *useraddr, u32 ethcmd)
310 struct ethtool_value edata;
311 netdev_features_t mask;
313 if (copy_from_user(&edata, useraddr, sizeof(edata)))
316 mask = ethtool_get_feature_mask(ethcmd);
317 mask &= dev->hw_features;
322 dev->wanted_features |= mask;
324 dev->wanted_features &= ~mask;
326 __netdev_update_features(dev);
331 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
332 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
333 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
334 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
337 static u32 __ethtool_get_flags(struct net_device *dev)
341 if (dev->features & NETIF_F_LRO)
342 flags |= ETH_FLAG_LRO;
343 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
344 flags |= ETH_FLAG_RXVLAN;
345 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
346 flags |= ETH_FLAG_TXVLAN;
347 if (dev->features & NETIF_F_NTUPLE)
348 flags |= ETH_FLAG_NTUPLE;
349 if (dev->features & NETIF_F_RXHASH)
350 flags |= ETH_FLAG_RXHASH;
355 static int __ethtool_set_flags(struct net_device *dev, u32 data)
357 netdev_features_t features = 0, changed;
359 if (data & ~ETH_ALL_FLAGS)
362 if (data & ETH_FLAG_LRO)
363 features |= NETIF_F_LRO;
364 if (data & ETH_FLAG_RXVLAN)
365 features |= NETIF_F_HW_VLAN_CTAG_RX;
366 if (data & ETH_FLAG_TXVLAN)
367 features |= NETIF_F_HW_VLAN_CTAG_TX;
368 if (data & ETH_FLAG_NTUPLE)
369 features |= NETIF_F_NTUPLE;
370 if (data & ETH_FLAG_RXHASH)
371 features |= NETIF_F_RXHASH;
373 /* allow changing only bits set in hw_features */
374 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
375 if (changed & ~dev->hw_features)
376 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
378 dev->wanted_features =
379 (dev->wanted_features & ~changed) | (features & changed);
381 __netdev_update_features(dev);
386 /* Given two link masks, AND them together and save the result in dst. */
387 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
388 struct ethtool_link_ksettings *src)
390 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
391 unsigned int idx = 0;
393 for (; idx < size; idx++) {
394 dst->link_modes.supported[idx] &=
395 src->link_modes.supported[idx];
396 dst->link_modes.advertising[idx] &=
397 src->link_modes.advertising[idx];
400 EXPORT_SYMBOL(ethtool_intersect_link_masks);
402 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
405 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
408 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
410 /* return false if src had higher bits set. lower bits always updated. */
411 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
412 const unsigned long *src)
416 /* TODO: following test will soon always be true */
417 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
418 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
420 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
421 bitmap_fill(ext, 32);
422 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
423 if (bitmap_intersects(ext, src,
424 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
425 /* src mask goes beyond bit 31 */
429 *legacy_u32 = src[0];
432 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
434 /* return false if legacy contained non-0 deprecated fields
435 * maxtxpkt/maxrxpkt. rest of ksettings always updated
438 convert_legacy_settings_to_link_ksettings(
439 struct ethtool_link_ksettings *link_ksettings,
440 const struct ethtool_cmd *legacy_settings)
444 memset(link_ksettings, 0, sizeof(*link_ksettings));
446 /* This is used to tell users that driver is still using these
447 * deprecated legacy fields, and they should not use
448 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
450 if (legacy_settings->maxtxpkt ||
451 legacy_settings->maxrxpkt)
454 ethtool_convert_legacy_u32_to_link_mode(
455 link_ksettings->link_modes.supported,
456 legacy_settings->supported);
457 ethtool_convert_legacy_u32_to_link_mode(
458 link_ksettings->link_modes.advertising,
459 legacy_settings->advertising);
460 ethtool_convert_legacy_u32_to_link_mode(
461 link_ksettings->link_modes.lp_advertising,
462 legacy_settings->lp_advertising);
463 link_ksettings->base.speed
464 = ethtool_cmd_speed(legacy_settings);
465 link_ksettings->base.duplex
466 = legacy_settings->duplex;
467 link_ksettings->base.port
468 = legacy_settings->port;
469 link_ksettings->base.phy_address
470 = legacy_settings->phy_address;
471 link_ksettings->base.autoneg
472 = legacy_settings->autoneg;
473 link_ksettings->base.mdio_support
474 = legacy_settings->mdio_support;
475 link_ksettings->base.eth_tp_mdix
476 = legacy_settings->eth_tp_mdix;
477 link_ksettings->base.eth_tp_mdix_ctrl
478 = legacy_settings->eth_tp_mdix_ctrl;
482 /* return false if ksettings link modes had higher bits
483 * set. legacy_settings always updated (best effort)
486 convert_link_ksettings_to_legacy_settings(
487 struct ethtool_cmd *legacy_settings,
488 const struct ethtool_link_ksettings *link_ksettings)
492 memset(legacy_settings, 0, sizeof(*legacy_settings));
493 /* this also clears the deprecated fields in legacy structure:
499 retval &= ethtool_convert_link_mode_to_legacy_u32(
500 &legacy_settings->supported,
501 link_ksettings->link_modes.supported);
502 retval &= ethtool_convert_link_mode_to_legacy_u32(
503 &legacy_settings->advertising,
504 link_ksettings->link_modes.advertising);
505 retval &= ethtool_convert_link_mode_to_legacy_u32(
506 &legacy_settings->lp_advertising,
507 link_ksettings->link_modes.lp_advertising);
508 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
509 legacy_settings->duplex
510 = link_ksettings->base.duplex;
511 legacy_settings->port
512 = link_ksettings->base.port;
513 legacy_settings->phy_address
514 = link_ksettings->base.phy_address;
515 legacy_settings->autoneg
516 = link_ksettings->base.autoneg;
517 legacy_settings->mdio_support
518 = link_ksettings->base.mdio_support;
519 legacy_settings->eth_tp_mdix
520 = link_ksettings->base.eth_tp_mdix;
521 legacy_settings->eth_tp_mdix_ctrl
522 = link_ksettings->base.eth_tp_mdix_ctrl;
523 legacy_settings->transceiver
524 = link_ksettings->base.transceiver;
528 /* number of 32-bit words to store the user's link mode bitmaps */
529 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
530 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
532 /* layout of the struct passed from/to userland */
533 struct ethtool_link_usettings {
534 struct ethtool_link_settings base;
536 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
537 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
538 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
542 /* Internal kernel helper to query a device ethtool_link_settings.
544 * Backward compatibility note: for compatibility with legacy drivers
545 * that implement only the ethtool_cmd API, this has to work with both
546 * drivers implementing get_link_ksettings API and drivers
547 * implementing get_settings API. When drivers implement get_settings
548 * and report ethtool_cmd deprecated fields
549 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
550 * because the resulting struct ethtool_link_settings does not report them.
552 int __ethtool_get_link_ksettings(struct net_device *dev,
553 struct ethtool_link_ksettings *link_ksettings)
556 struct ethtool_cmd cmd;
560 if (dev->ethtool_ops->get_link_ksettings) {
561 memset(link_ksettings, 0, sizeof(*link_ksettings));
562 return dev->ethtool_ops->get_link_ksettings(dev,
566 /* driver doesn't support %ethtool_link_ksettings API. revert to
567 * legacy %ethtool_cmd API, unless it's not supported either.
568 * TODO: remove when ethtool_ops::get_settings disappears internally
570 if (!dev->ethtool_ops->get_settings)
573 memset(&cmd, 0, sizeof(cmd));
574 cmd.cmd = ETHTOOL_GSET;
575 err = dev->ethtool_ops->get_settings(dev, &cmd);
579 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
581 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
584 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
586 /* convert ethtool_link_usettings in user space to a kernel internal
587 * ethtool_link_ksettings. return 0 on success, errno on error.
589 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
590 const void __user *from)
592 struct ethtool_link_usettings link_usettings;
594 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
597 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
598 bitmap_from_arr32(to->link_modes.supported,
599 link_usettings.link_modes.supported,
600 __ETHTOOL_LINK_MODE_MASK_NBITS);
601 bitmap_from_arr32(to->link_modes.advertising,
602 link_usettings.link_modes.advertising,
603 __ETHTOOL_LINK_MODE_MASK_NBITS);
604 bitmap_from_arr32(to->link_modes.lp_advertising,
605 link_usettings.link_modes.lp_advertising,
606 __ETHTOOL_LINK_MODE_MASK_NBITS);
611 /* convert a kernel internal ethtool_link_ksettings to
612 * ethtool_link_usettings in user space. return 0 on success, errno on
616 store_link_ksettings_for_user(void __user *to,
617 const struct ethtool_link_ksettings *from)
619 struct ethtool_link_usettings link_usettings;
621 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
622 bitmap_to_arr32(link_usettings.link_modes.supported,
623 from->link_modes.supported,
624 __ETHTOOL_LINK_MODE_MASK_NBITS);
625 bitmap_to_arr32(link_usettings.link_modes.advertising,
626 from->link_modes.advertising,
627 __ETHTOOL_LINK_MODE_MASK_NBITS);
628 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
629 from->link_modes.lp_advertising,
630 __ETHTOOL_LINK_MODE_MASK_NBITS);
632 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
638 /* Query device for its ethtool_link_settings.
640 * Backward compatibility note: this function must fail when driver
641 * does not implement ethtool::get_link_ksettings, even if legacy
642 * ethtool_ops::get_settings is implemented. This tells new versions
643 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
644 * this driver, so that they can correctly access the ethtool_cmd
645 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
646 * implements ethtool_ops::get_settings anymore.
648 static int ethtool_get_link_ksettings(struct net_device *dev,
649 void __user *useraddr)
652 struct ethtool_link_ksettings link_ksettings;
656 if (!dev->ethtool_ops->get_link_ksettings)
659 /* handle bitmap nbits handshake */
660 if (copy_from_user(&link_ksettings.base, useraddr,
661 sizeof(link_ksettings.base)))
664 if (__ETHTOOL_LINK_MODE_MASK_NU32
665 != link_ksettings.base.link_mode_masks_nwords) {
666 /* wrong link mode nbits requested */
667 memset(&link_ksettings, 0, sizeof(link_ksettings));
668 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
669 /* send back number of words required as negative val */
670 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
671 "need too many bits for link modes!");
672 link_ksettings.base.link_mode_masks_nwords
673 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
675 /* copy the base fields back to user, not the link
678 if (copy_to_user(useraddr, &link_ksettings.base,
679 sizeof(link_ksettings.base)))
685 /* handshake successful: user/kernel agree on
686 * link_mode_masks_nwords
689 memset(&link_ksettings, 0, sizeof(link_ksettings));
690 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
694 /* make sure we tell the right values to user */
695 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
696 link_ksettings.base.link_mode_masks_nwords
697 = __ETHTOOL_LINK_MODE_MASK_NU32;
699 return store_link_ksettings_for_user(useraddr, &link_ksettings);
702 /* Update device ethtool_link_settings.
704 * Backward compatibility note: this function must fail when driver
705 * does not implement ethtool::set_link_ksettings, even if legacy
706 * ethtool_ops::set_settings is implemented. This tells new versions
707 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
708 * this driver, so that they can correctly update the ethtool_cmd
709 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
710 * implements ethtool_ops::get_settings anymore.
712 static int ethtool_set_link_ksettings(struct net_device *dev,
713 void __user *useraddr)
716 struct ethtool_link_ksettings link_ksettings;
720 if (!dev->ethtool_ops->set_link_ksettings)
723 /* make sure nbits field has expected value */
724 if (copy_from_user(&link_ksettings.base, useraddr,
725 sizeof(link_ksettings.base)))
728 if (__ETHTOOL_LINK_MODE_MASK_NU32
729 != link_ksettings.base.link_mode_masks_nwords)
732 /* copy the whole structure, now that we know it has expected
735 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
739 /* re-check nwords field, just in case */
740 if (__ETHTOOL_LINK_MODE_MASK_NU32
741 != link_ksettings.base.link_mode_masks_nwords)
744 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
747 /* Query device for its ethtool_cmd settings.
749 * Backward compatibility note: for compatibility with legacy ethtool,
750 * this has to work with both drivers implementing get_link_ksettings
751 * API and drivers implementing get_settings API. When drivers
752 * implement get_link_ksettings and report higher link mode bits, a
753 * kernel warning is logged once (with name of 1st driver/device) to
754 * recommend user to upgrade ethtool, but the command is successful
755 * (only the lower link mode bits reported back to user).
757 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
759 struct ethtool_cmd cmd;
763 if (dev->ethtool_ops->get_link_ksettings) {
764 /* First, use link_ksettings API if it is supported */
766 struct ethtool_link_ksettings link_ksettings;
768 memset(&link_ksettings, 0, sizeof(link_ksettings));
769 err = dev->ethtool_ops->get_link_ksettings(dev,
773 convert_link_ksettings_to_legacy_settings(&cmd,
776 /* send a sensible cmd tag back to user */
777 cmd.cmd = ETHTOOL_GSET;
779 /* driver doesn't support %ethtool_link_ksettings
780 * API. revert to legacy %ethtool_cmd API, unless it's
781 * not supported either.
785 if (!dev->ethtool_ops->get_settings)
788 memset(&cmd, 0, sizeof(cmd));
789 cmd.cmd = ETHTOOL_GSET;
790 err = dev->ethtool_ops->get_settings(dev, &cmd);
795 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
801 /* Update device link settings with given ethtool_cmd.
803 * Backward compatibility note: for compatibility with legacy ethtool,
804 * this has to work with both drivers implementing set_link_ksettings
805 * API and drivers implementing set_settings API. When drivers
806 * implement set_link_ksettings and user's request updates deprecated
807 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
808 * warning is logged once (with name of 1st driver/device) to
809 * recommend user to upgrade ethtool, and the request is rejected.
811 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
813 struct ethtool_cmd cmd;
817 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
820 /* first, try new %ethtool_link_ksettings API. */
821 if (dev->ethtool_ops->set_link_ksettings) {
822 struct ethtool_link_ksettings link_ksettings;
824 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
828 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
829 link_ksettings.base.link_mode_masks_nwords
830 = __ETHTOOL_LINK_MODE_MASK_NU32;
831 return dev->ethtool_ops->set_link_ksettings(dev,
835 /* legacy %ethtool_cmd API */
837 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
838 * disappears internally
841 if (!dev->ethtool_ops->set_settings)
844 return dev->ethtool_ops->set_settings(dev, &cmd);
847 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
848 void __user *useraddr)
850 struct ethtool_drvinfo info;
851 const struct ethtool_ops *ops = dev->ethtool_ops;
853 memset(&info, 0, sizeof(info));
854 info.cmd = ETHTOOL_GDRVINFO;
855 if (ops->get_drvinfo) {
856 ops->get_drvinfo(dev, &info);
857 } else if (dev->dev.parent && dev->dev.parent->driver) {
858 strlcpy(info.bus_info, dev_name(dev->dev.parent),
859 sizeof(info.bus_info));
860 strlcpy(info.driver, dev->dev.parent->driver->name,
861 sizeof(info.driver));
867 * this method of obtaining string set info is deprecated;
868 * Use ETHTOOL_GSSET_INFO instead.
870 if (ops->get_sset_count) {
873 rc = ops->get_sset_count(dev, ETH_SS_TEST);
875 info.testinfo_len = rc;
876 rc = ops->get_sset_count(dev, ETH_SS_STATS);
879 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
881 info.n_priv_flags = rc;
883 if (ops->get_regs_len)
884 info.regdump_len = ops->get_regs_len(dev);
885 if (ops->get_eeprom_len)
886 info.eedump_len = ops->get_eeprom_len(dev);
888 if (copy_to_user(useraddr, &info, sizeof(info)))
893 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
894 void __user *useraddr)
896 struct ethtool_sset_info info;
898 int i, idx = 0, n_bits = 0, ret, rc;
899 u32 *info_buf = NULL;
901 if (copy_from_user(&info, useraddr, sizeof(info)))
904 /* store copy of mask, because we zero struct later on */
905 sset_mask = info.sset_mask;
909 /* calculate size of return buffer */
910 n_bits = hweight64(sset_mask);
912 memset(&info, 0, sizeof(info));
913 info.cmd = ETHTOOL_GSSET_INFO;
915 info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
920 * fill return buffer based on input bitmask and successful
921 * get_sset_count return
923 for (i = 0; i < 64; i++) {
924 if (!(sset_mask & (1ULL << i)))
927 rc = __ethtool_get_sset_count(dev, i);
929 info.sset_mask |= (1ULL << i);
930 info_buf[idx++] = rc;
935 if (copy_to_user(useraddr, &info, sizeof(info)))
938 useraddr += offsetof(struct ethtool_sset_info, data);
939 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
949 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
950 u32 cmd, void __user *useraddr)
952 struct ethtool_rxnfc info;
953 size_t info_size = sizeof(info);
956 if (!dev->ethtool_ops->set_rxnfc)
959 /* struct ethtool_rxnfc was originally defined for
960 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
961 * members. User-space might still be using that
963 if (cmd == ETHTOOL_SRXFH)
964 info_size = (offsetof(struct ethtool_rxnfc, data) +
967 if (copy_from_user(&info, useraddr, info_size))
970 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
974 if (cmd == ETHTOOL_SRXCLSRLINS &&
975 copy_to_user(useraddr, &info, info_size))
981 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
982 u32 cmd, void __user *useraddr)
984 struct ethtool_rxnfc info;
985 size_t info_size = sizeof(info);
986 const struct ethtool_ops *ops = dev->ethtool_ops;
988 void *rule_buf = NULL;
993 /* struct ethtool_rxnfc was originally defined for
994 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
995 * members. User-space might still be using that
997 if (cmd == ETHTOOL_GRXFH)
998 info_size = (offsetof(struct ethtool_rxnfc, data) +
1001 if (copy_from_user(&info, useraddr, info_size))
1004 /* If FLOW_RSS was requested then user-space must be using the
1005 * new definition, as FLOW_RSS is newer.
1007 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1008 info_size = sizeof(info);
1009 if (copy_from_user(&info, useraddr, info_size))
1011 /* Since malicious users may modify the original data,
1012 * we need to check whether FLOW_RSS is still requested.
1014 if (!(info.flow_type & FLOW_RSS))
1018 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1019 if (info.rule_cnt > 0) {
1020 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1021 rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
1028 ret = ops->get_rxnfc(dev, &info, rule_buf);
1033 if (copy_to_user(useraddr, &info, info_size))
1037 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1038 if (copy_to_user(useraddr, rule_buf,
1039 info.rule_cnt * sizeof(u32)))
1050 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1051 struct ethtool_rxnfc *rx_rings,
1056 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1059 /* Validate ring indices */
1060 for (i = 0; i < size; i++)
1061 if (indir[i] >= rx_rings->data)
1067 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1069 void netdev_rss_key_fill(void *buffer, size_t len)
1071 BUG_ON(len > sizeof(netdev_rss_key));
1072 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1073 memcpy(buffer, netdev_rss_key, len);
1075 EXPORT_SYMBOL(netdev_rss_key_fill);
1077 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1079 u32 dev_size, current_max = 0;
1083 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1084 !dev->ethtool_ops->get_rxfh)
1086 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1090 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1094 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1099 current_max = max(current_max, indir[dev_size]);
1108 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1109 void __user *useraddr)
1111 u32 user_size, dev_size;
1115 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1116 !dev->ethtool_ops->get_rxfh)
1118 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1122 if (copy_from_user(&user_size,
1123 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1127 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1128 &dev_size, sizeof(dev_size)))
1131 /* If the user buffer size is 0, this is just a query for the
1132 * device table size. Otherwise, if it's smaller than the
1133 * device table size it's an error.
1135 if (user_size < dev_size)
1136 return user_size == 0 ? 0 : -EINVAL;
1138 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1142 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1146 if (copy_to_user(useraddr +
1147 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1148 indir, dev_size * sizeof(indir[0])))
1156 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1157 void __user *useraddr)
1159 struct ethtool_rxnfc rx_rings;
1160 u32 user_size, dev_size, i;
1162 const struct ethtool_ops *ops = dev->ethtool_ops;
1164 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1166 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1170 dev_size = ops->get_rxfh_indir_size(dev);
1174 if (copy_from_user(&user_size,
1175 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1179 if (user_size != 0 && user_size != dev_size)
1182 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1186 rx_rings.cmd = ETHTOOL_GRXRINGS;
1187 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1191 if (user_size == 0) {
1192 for (i = 0; i < dev_size; i++)
1193 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1195 ret = ethtool_copy_validate_indir(indir,
1196 useraddr + ringidx_offset,
1203 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1207 /* indicate whether rxfh was set to default */
1209 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1211 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1218 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1219 void __user *useraddr)
1222 const struct ethtool_ops *ops = dev->ethtool_ops;
1223 u32 user_indir_size, user_key_size;
1224 u32 dev_indir_size = 0, dev_key_size = 0;
1225 struct ethtool_rxfh rxfh;
1236 if (ops->get_rxfh_indir_size)
1237 dev_indir_size = ops->get_rxfh_indir_size(dev);
1238 if (ops->get_rxfh_key_size)
1239 dev_key_size = ops->get_rxfh_key_size(dev);
1241 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1243 user_indir_size = rxfh.indir_size;
1244 user_key_size = rxfh.key_size;
1246 /* Check that reserved fields are 0 for now */
1247 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1249 /* Most drivers don't handle rss_context, check it's 0 as well */
1250 if (rxfh.rss_context && !ops->get_rxfh_context)
1253 rxfh.indir_size = dev_indir_size;
1254 rxfh.key_size = dev_key_size;
1255 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1258 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1259 (user_key_size && (user_key_size != dev_key_size)))
1262 indir_bytes = user_indir_size * sizeof(indir[0]);
1263 total_size = indir_bytes + user_key_size;
1264 rss_config = kzalloc(total_size, GFP_USER);
1268 if (user_indir_size)
1269 indir = (u32 *)rss_config;
1272 hkey = rss_config + indir_bytes;
1274 if (rxfh.rss_context)
1275 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1279 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1283 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1284 &dev_hfunc, sizeof(rxfh.hfunc))) {
1286 } else if (copy_to_user(useraddr +
1287 offsetof(struct ethtool_rxfh, rss_config[0]),
1288 rss_config, total_size)) {
1297 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1298 void __user *useraddr)
1301 const struct ethtool_ops *ops = dev->ethtool_ops;
1302 struct ethtool_rxnfc rx_rings;
1303 struct ethtool_rxfh rxfh;
1304 u32 dev_indir_size = 0, dev_key_size = 0, i;
1305 u32 *indir = NULL, indir_bytes = 0;
1308 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1309 bool delete = false;
1311 if (!ops->get_rxnfc || !ops->set_rxfh)
1314 if (ops->get_rxfh_indir_size)
1315 dev_indir_size = ops->get_rxfh_indir_size(dev);
1316 if (ops->get_rxfh_key_size)
1317 dev_key_size = ops->get_rxfh_key_size(dev);
1319 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1322 /* Check that reserved fields are 0 for now */
1323 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1325 /* Most drivers don't handle rss_context, check it's 0 as well */
1326 if (rxfh.rss_context && !ops->set_rxfh_context)
1329 /* If either indir, hash key or function is valid, proceed further.
1330 * Must request at least one change: indir size, hash key or function.
1332 if ((rxfh.indir_size &&
1333 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1334 rxfh.indir_size != dev_indir_size) ||
1335 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1336 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1337 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1340 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1341 indir_bytes = dev_indir_size * sizeof(indir[0]);
1343 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1347 rx_rings.cmd = ETHTOOL_GRXRINGS;
1348 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1352 /* rxfh.indir_size == 0 means reset the indir table to default (master
1353 * context) or delete the context (other RSS contexts).
1354 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1356 if (rxfh.indir_size &&
1357 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1358 indir = (u32 *)rss_config;
1359 ret = ethtool_copy_validate_indir(indir,
1360 useraddr + rss_cfg_offset,
1365 } else if (rxfh.indir_size == 0) {
1366 if (rxfh.rss_context == 0) {
1367 indir = (u32 *)rss_config;
1368 for (i = 0; i < dev_indir_size; i++)
1369 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1375 if (rxfh.key_size) {
1376 hkey = rss_config + indir_bytes;
1377 if (copy_from_user(hkey,
1378 useraddr + rss_cfg_offset + indir_bytes,
1385 if (rxfh.rss_context)
1386 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1387 &rxfh.rss_context, delete);
1389 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1393 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1394 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1397 if (!rxfh.rss_context) {
1398 /* indicate whether rxfh was set to default */
1399 if (rxfh.indir_size == 0)
1400 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1401 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1402 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1410 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1412 struct ethtool_regs regs;
1413 const struct ethtool_ops *ops = dev->ethtool_ops;
1417 if (!ops->get_regs || !ops->get_regs_len)
1420 if (copy_from_user(®s, useraddr, sizeof(regs)))
1423 reglen = ops->get_regs_len(dev);
1424 if (regs.len > reglen)
1429 regbuf = vzalloc(reglen);
1434 ops->get_regs(dev, ®s, regbuf);
1437 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1439 useraddr += offsetof(struct ethtool_regs, data);
1440 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1449 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1451 struct ethtool_value reset;
1454 if (!dev->ethtool_ops->reset)
1457 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1460 ret = dev->ethtool_ops->reset(dev, &reset.data);
1464 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1469 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1471 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1473 if (!dev->ethtool_ops->get_wol)
1476 dev->ethtool_ops->get_wol(dev, &wol);
1478 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1483 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1485 struct ethtool_wolinfo wol;
1487 if (!dev->ethtool_ops->set_wol)
1490 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1493 return dev->ethtool_ops->set_wol(dev, &wol);
1496 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1498 struct ethtool_eee edata;
1501 if (!dev->ethtool_ops->get_eee)
1504 memset(&edata, 0, sizeof(struct ethtool_eee));
1505 edata.cmd = ETHTOOL_GEEE;
1506 rc = dev->ethtool_ops->get_eee(dev, &edata);
1511 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1517 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1519 struct ethtool_eee edata;
1521 if (!dev->ethtool_ops->set_eee)
1524 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1527 return dev->ethtool_ops->set_eee(dev, &edata);
1530 static int ethtool_nway_reset(struct net_device *dev)
1532 if (!dev->ethtool_ops->nway_reset)
1535 return dev->ethtool_ops->nway_reset(dev);
1538 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1540 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1542 if (!dev->ethtool_ops->get_link)
1545 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1547 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1552 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1553 int (*getter)(struct net_device *,
1554 struct ethtool_eeprom *, u8 *),
1557 struct ethtool_eeprom eeprom;
1558 void __user *userbuf = useraddr + sizeof(eeprom);
1559 u32 bytes_remaining;
1563 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1566 /* Check for wrap and zero */
1567 if (eeprom.offset + eeprom.len <= eeprom.offset)
1570 /* Check for exceeding total eeprom len */
1571 if (eeprom.offset + eeprom.len > total_len)
1574 data = kmalloc(PAGE_SIZE, GFP_USER);
1578 bytes_remaining = eeprom.len;
1579 while (bytes_remaining > 0) {
1580 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1582 ret = getter(dev, &eeprom, data);
1585 if (copy_to_user(userbuf, data, eeprom.len)) {
1589 userbuf += eeprom.len;
1590 eeprom.offset += eeprom.len;
1591 bytes_remaining -= eeprom.len;
1594 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1595 eeprom.offset -= eeprom.len;
1596 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1603 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1605 const struct ethtool_ops *ops = dev->ethtool_ops;
1607 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1608 !ops->get_eeprom_len(dev))
1611 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1612 ops->get_eeprom_len(dev));
1615 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1617 struct ethtool_eeprom eeprom;
1618 const struct ethtool_ops *ops = dev->ethtool_ops;
1619 void __user *userbuf = useraddr + sizeof(eeprom);
1620 u32 bytes_remaining;
1624 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1625 !ops->get_eeprom_len(dev))
1628 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1631 /* Check for wrap and zero */
1632 if (eeprom.offset + eeprom.len <= eeprom.offset)
1635 /* Check for exceeding total eeprom len */
1636 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1639 data = kmalloc(PAGE_SIZE, GFP_USER);
1643 bytes_remaining = eeprom.len;
1644 while (bytes_remaining > 0) {
1645 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1647 if (copy_from_user(data, userbuf, eeprom.len)) {
1651 ret = ops->set_eeprom(dev, &eeprom, data);
1654 userbuf += eeprom.len;
1655 eeprom.offset += eeprom.len;
1656 bytes_remaining -= eeprom.len;
1663 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1664 void __user *useraddr)
1666 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1668 if (!dev->ethtool_ops->get_coalesce)
1671 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1673 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1678 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1679 void __user *useraddr)
1681 struct ethtool_coalesce coalesce;
1683 if (!dev->ethtool_ops->set_coalesce)
1686 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1689 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1692 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1694 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1696 if (!dev->ethtool_ops->get_ringparam)
1699 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1701 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1706 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1708 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1710 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1713 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1716 dev->ethtool_ops->get_ringparam(dev, &max);
1718 /* ensure new ring parameters are within the maximums */
1719 if (ringparam.rx_pending > max.rx_max_pending ||
1720 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1721 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1722 ringparam.tx_pending > max.tx_max_pending)
1725 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1728 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1729 void __user *useraddr)
1731 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1733 if (!dev->ethtool_ops->get_channels)
1736 dev->ethtool_ops->get_channels(dev, &channels);
1738 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1743 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1744 void __user *useraddr)
1746 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
1747 u32 max_rx_in_use = 0;
1749 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1752 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1755 dev->ethtool_ops->get_channels(dev, &max);
1757 /* ensure new counts are within the maximums */
1758 if ((channels.rx_count > max.max_rx) ||
1759 (channels.tx_count > max.max_tx) ||
1760 (channels.combined_count > max.max_combined) ||
1761 (channels.other_count > max.max_other))
1764 /* ensure the new Rx count fits within the configured Rx flow
1765 * indirection table settings */
1766 if (netif_is_rxfh_configured(dev) &&
1767 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1768 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1771 return dev->ethtool_ops->set_channels(dev, &channels);
1774 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1776 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1778 if (!dev->ethtool_ops->get_pauseparam)
1781 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1783 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1788 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1790 struct ethtool_pauseparam pauseparam;
1792 if (!dev->ethtool_ops->set_pauseparam)
1795 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1798 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1801 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1803 struct ethtool_test test;
1804 const struct ethtool_ops *ops = dev->ethtool_ops;
1808 if (!ops->self_test || !ops->get_sset_count)
1811 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1814 WARN_ON(test_len == 0);
1816 if (copy_from_user(&test, useraddr, sizeof(test)))
1819 test.len = test_len;
1820 data = kmalloc_array(test_len, sizeof(u64), GFP_USER);
1824 ops->self_test(dev, &test, data);
1827 if (copy_to_user(useraddr, &test, sizeof(test)))
1829 useraddr += sizeof(test);
1830 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1839 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1841 struct ethtool_gstrings gstrings;
1845 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1848 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1851 if (ret > S32_MAX / ETH_GSTRING_LEN)
1856 data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
1857 if (gstrings.len && !data)
1860 __ethtool_get_strings(dev, gstrings.string_set, data);
1863 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1865 useraddr += sizeof(gstrings);
1867 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1876 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1878 struct ethtool_value id;
1880 const struct ethtool_ops *ops = dev->ethtool_ops;
1883 if (!ops->set_phys_id)
1889 if (copy_from_user(&id, useraddr, sizeof(id)))
1892 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1896 /* Drop the RTNL lock while waiting, but prevent reentry or
1897 * removal of the device.
1904 /* Driver will handle this itself */
1905 schedule_timeout_interruptible(
1906 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1908 /* Driver expects to be called at twice the frequency in rc */
1909 int n = rc * 2, i, interval = HZ / n;
1911 /* Count down seconds */
1913 /* Count down iterations per second */
1917 rc = ops->set_phys_id(dev,
1918 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1922 schedule_timeout_interruptible(interval);
1923 } while (!signal_pending(current) && --i != 0);
1924 } while (!signal_pending(current) &&
1925 (id.data == 0 || --id.data != 0));
1932 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1936 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1938 struct ethtool_stats stats;
1939 const struct ethtool_ops *ops = dev->ethtool_ops;
1943 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1946 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1949 if (n_stats > S32_MAX / sizeof(u64))
1951 WARN_ON_ONCE(!n_stats);
1952 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1955 stats.n_stats = n_stats;
1956 data = vzalloc(array_size(n_stats, sizeof(u64)));
1957 if (n_stats && !data)
1960 ops->get_ethtool_stats(dev, &stats, data);
1963 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1965 useraddr += sizeof(stats);
1966 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1975 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1977 const struct ethtool_ops *ops = dev->ethtool_ops;
1978 struct phy_device *phydev = dev->phydev;
1979 struct ethtool_stats stats;
1983 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1986 if (dev->phydev && !ops->get_ethtool_phy_stats)
1987 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1989 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1992 if (n_stats > S32_MAX / sizeof(u64))
1994 WARN_ON_ONCE(!n_stats);
1996 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1999 stats.n_stats = n_stats;
2000 data = vzalloc(array_size(n_stats, sizeof(u64)));
2001 if (n_stats && !data)
2004 if (dev->phydev && !ops->get_ethtool_phy_stats) {
2005 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
2009 ops->get_ethtool_phy_stats(dev, &stats, data);
2013 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2015 useraddr += sizeof(stats);
2016 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
2025 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2027 struct ethtool_perm_addr epaddr;
2029 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2032 if (epaddr.size < dev->addr_len)
2034 epaddr.size = dev->addr_len;
2036 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2038 useraddr += sizeof(epaddr);
2039 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2044 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2045 u32 cmd, u32 (*actor)(struct net_device *))
2047 struct ethtool_value edata = { .cmd = cmd };
2052 edata.data = actor(dev);
2054 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2059 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2060 void (*actor)(struct net_device *, u32))
2062 struct ethtool_value edata;
2067 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2070 actor(dev, edata.data);
2074 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2075 int (*actor)(struct net_device *, u32))
2077 struct ethtool_value edata;
2082 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2085 return actor(dev, edata.data);
2088 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2089 char __user *useraddr)
2091 struct ethtool_flash efl;
2093 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2096 if (!dev->ethtool_ops->flash_device)
2099 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2101 return dev->ethtool_ops->flash_device(dev, &efl);
2104 static int ethtool_set_dump(struct net_device *dev,
2105 void __user *useraddr)
2107 struct ethtool_dump dump;
2109 if (!dev->ethtool_ops->set_dump)
2112 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2115 return dev->ethtool_ops->set_dump(dev, &dump);
2118 static int ethtool_get_dump_flag(struct net_device *dev,
2119 void __user *useraddr)
2122 struct ethtool_dump dump;
2123 const struct ethtool_ops *ops = dev->ethtool_ops;
2125 if (!ops->get_dump_flag)
2128 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2131 ret = ops->get_dump_flag(dev, &dump);
2135 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2140 static int ethtool_get_dump_data(struct net_device *dev,
2141 void __user *useraddr)
2145 struct ethtool_dump dump, tmp;
2146 const struct ethtool_ops *ops = dev->ethtool_ops;
2149 if (!ops->get_dump_data || !ops->get_dump_flag)
2152 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2155 memset(&tmp, 0, sizeof(tmp));
2156 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2157 ret = ops->get_dump_flag(dev, &tmp);
2161 len = min(tmp.len, dump.len);
2165 /* Don't ever let the driver think there's more space available
2166 * than it requested with .get_dump_flag().
2170 /* Always allocate enough space to hold the whole thing so that the
2171 * driver does not need to check the length and bother with partial
2174 data = vzalloc(tmp.len);
2177 ret = ops->get_dump_data(dev, &dump, data);
2181 /* There are two sane possibilities:
2182 * 1. The driver's .get_dump_data() does not touch dump.len.
2183 * 2. Or it may set dump.len to how much it really writes, which
2184 * should be tmp.len (or len if it can do a partial dump).
2185 * In any case respond to userspace with the actual length of data
2188 WARN_ON(dump.len != len && dump.len != tmp.len);
2191 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2195 useraddr += offsetof(struct ethtool_dump, data);
2196 if (copy_to_user(useraddr, data, len))
2203 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2206 struct ethtool_ts_info info;
2207 const struct ethtool_ops *ops = dev->ethtool_ops;
2208 struct phy_device *phydev = dev->phydev;
2210 memset(&info, 0, sizeof(info));
2211 info.cmd = ETHTOOL_GET_TS_INFO;
2213 if (phydev && phydev->drv && phydev->drv->ts_info) {
2214 err = phydev->drv->ts_info(phydev, &info);
2215 } else if (ops->get_ts_info) {
2216 err = ops->get_ts_info(dev, &info);
2218 info.so_timestamping =
2219 SOF_TIMESTAMPING_RX_SOFTWARE |
2220 SOF_TIMESTAMPING_SOFTWARE;
2221 info.phc_index = -1;
2227 if (copy_to_user(useraddr, &info, sizeof(info)))
2233 static int __ethtool_get_module_info(struct net_device *dev,
2234 struct ethtool_modinfo *modinfo)
2236 const struct ethtool_ops *ops = dev->ethtool_ops;
2237 struct phy_device *phydev = dev->phydev;
2240 return sfp_get_module_info(dev->sfp_bus, modinfo);
2242 if (phydev && phydev->drv && phydev->drv->module_info)
2243 return phydev->drv->module_info(phydev, modinfo);
2245 if (ops->get_module_info)
2246 return ops->get_module_info(dev, modinfo);
2251 static int ethtool_get_module_info(struct net_device *dev,
2252 void __user *useraddr)
2255 struct ethtool_modinfo modinfo;
2257 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2260 ret = __ethtool_get_module_info(dev, &modinfo);
2264 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2270 static int __ethtool_get_module_eeprom(struct net_device *dev,
2271 struct ethtool_eeprom *ee, u8 *data)
2273 const struct ethtool_ops *ops = dev->ethtool_ops;
2274 struct phy_device *phydev = dev->phydev;
2277 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2279 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2280 return phydev->drv->module_eeprom(phydev, ee, data);
2282 if (ops->get_module_eeprom)
2283 return ops->get_module_eeprom(dev, ee, data);
2288 static int ethtool_get_module_eeprom(struct net_device *dev,
2289 void __user *useraddr)
2292 struct ethtool_modinfo modinfo;
2294 ret = __ethtool_get_module_info(dev, &modinfo);
2298 return ethtool_get_any_eeprom(dev, useraddr,
2299 __ethtool_get_module_eeprom,
2300 modinfo.eeprom_len);
2303 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2306 case ETHTOOL_RX_COPYBREAK:
2307 case ETHTOOL_TX_COPYBREAK:
2308 if (tuna->len != sizeof(u32) ||
2309 tuna->type_id != ETHTOOL_TUNABLE_U32)
2312 case ETHTOOL_PFC_PREVENTION_TOUT:
2313 if (tuna->len != sizeof(u16) ||
2314 tuna->type_id != ETHTOOL_TUNABLE_U16)
2324 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2327 struct ethtool_tunable tuna;
2328 const struct ethtool_ops *ops = dev->ethtool_ops;
2331 if (!ops->get_tunable)
2333 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2335 ret = ethtool_tunable_valid(&tuna);
2338 data = kmalloc(tuna.len, GFP_USER);
2341 ret = ops->get_tunable(dev, &tuna, data);
2344 useraddr += sizeof(tuna);
2346 if (copy_to_user(useraddr, data, tuna.len))
2355 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2358 struct ethtool_tunable tuna;
2359 const struct ethtool_ops *ops = dev->ethtool_ops;
2362 if (!ops->set_tunable)
2364 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2366 ret = ethtool_tunable_valid(&tuna);
2369 useraddr += sizeof(tuna);
2370 data = memdup_user(useraddr, tuna.len);
2372 return PTR_ERR(data);
2373 ret = ops->set_tunable(dev, &tuna, data);
2379 static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2380 void __user *useraddr,
2381 struct ethtool_per_queue_op *per_queue_opt)
2385 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2387 if (!dev->ethtool_ops->get_per_queue_coalesce)
2390 useraddr += sizeof(*per_queue_opt);
2392 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2395 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2396 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2398 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2401 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2403 useraddr += sizeof(coalesce);
2409 static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2410 void __user *useraddr,
2411 struct ethtool_per_queue_op *per_queue_opt)
2416 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2417 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2419 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2420 (!dev->ethtool_ops->get_per_queue_coalesce))
2423 useraddr += sizeof(*per_queue_opt);
2425 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2426 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2427 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2431 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2432 struct ethtool_coalesce coalesce;
2434 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2440 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2445 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2449 useraddr += sizeof(coalesce);
2455 for_each_set_bit(i, queue_mask, bit) {
2456 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2465 static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2467 struct ethtool_per_queue_op per_queue_opt;
2469 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2472 switch (per_queue_opt.sub_command) {
2473 case ETHTOOL_GCOALESCE:
2474 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2475 case ETHTOOL_SCOALESCE:
2476 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2482 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2485 case ETHTOOL_PHY_DOWNSHIFT:
2486 if (tuna->len != sizeof(u8) ||
2487 tuna->type_id != ETHTOOL_TUNABLE_U8)
2497 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2500 struct ethtool_tunable tuna;
2501 struct phy_device *phydev = dev->phydev;
2504 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2507 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2509 ret = ethtool_phy_tunable_valid(&tuna);
2512 data = kmalloc(tuna.len, GFP_USER);
2515 mutex_lock(&phydev->lock);
2516 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2517 mutex_unlock(&phydev->lock);
2520 useraddr += sizeof(tuna);
2522 if (copy_to_user(useraddr, data, tuna.len))
2531 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2534 struct ethtool_tunable tuna;
2535 struct phy_device *phydev = dev->phydev;
2538 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2540 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2542 ret = ethtool_phy_tunable_valid(&tuna);
2545 useraddr += sizeof(tuna);
2546 data = memdup_user(useraddr, tuna.len);
2548 return PTR_ERR(data);
2549 mutex_lock(&phydev->lock);
2550 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2551 mutex_unlock(&phydev->lock);
2557 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2559 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
2562 if (!dev->ethtool_ops->get_fecparam)
2565 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2569 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2574 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2576 struct ethtool_fecparam fecparam;
2578 if (!dev->ethtool_ops->set_fecparam)
2581 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2584 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2587 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2589 int dev_ethtool(struct net *net, struct ifreq *ifr)
2591 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2592 void __user *useraddr = ifr->ifr_data;
2593 u32 ethcmd, sub_cmd;
2595 netdev_features_t old_features;
2597 if (!dev || !netif_device_present(dev))
2600 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2603 if (ethcmd == ETHTOOL_PERQUEUE) {
2604 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2609 /* Allow some commands to be done by anyone */
2612 case ETHTOOL_GDRVINFO:
2613 case ETHTOOL_GMSGLVL:
2615 case ETHTOOL_GCOALESCE:
2616 case ETHTOOL_GRINGPARAM:
2617 case ETHTOOL_GPAUSEPARAM:
2618 case ETHTOOL_GRXCSUM:
2619 case ETHTOOL_GTXCSUM:
2621 case ETHTOOL_GSSET_INFO:
2622 case ETHTOOL_GSTRINGS:
2623 case ETHTOOL_GSTATS:
2624 case ETHTOOL_GPHYSTATS:
2626 case ETHTOOL_GPERMADDR:
2629 case ETHTOOL_GFLAGS:
2630 case ETHTOOL_GPFLAGS:
2632 case ETHTOOL_GRXRINGS:
2633 case ETHTOOL_GRXCLSRLCNT:
2634 case ETHTOOL_GRXCLSRULE:
2635 case ETHTOOL_GRXCLSRLALL:
2636 case ETHTOOL_GRXFHINDIR:
2638 case ETHTOOL_GFEATURES:
2639 case ETHTOOL_GCHANNELS:
2640 case ETHTOOL_GET_TS_INFO:
2642 case ETHTOOL_GTUNABLE:
2643 case ETHTOOL_PHY_GTUNABLE:
2644 case ETHTOOL_GLINKSETTINGS:
2645 case ETHTOOL_GFECPARAM:
2648 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2652 if (dev->ethtool_ops->begin) {
2653 rc = dev->ethtool_ops->begin(dev);
2657 old_features = dev->features;
2661 rc = ethtool_get_settings(dev, useraddr);
2664 rc = ethtool_set_settings(dev, useraddr);
2666 case ETHTOOL_GDRVINFO:
2667 rc = ethtool_get_drvinfo(dev, useraddr);
2670 rc = ethtool_get_regs(dev, useraddr);
2673 rc = ethtool_get_wol(dev, useraddr);
2676 rc = ethtool_set_wol(dev, useraddr);
2678 case ETHTOOL_GMSGLVL:
2679 rc = ethtool_get_value(dev, useraddr, ethcmd,
2680 dev->ethtool_ops->get_msglevel);
2682 case ETHTOOL_SMSGLVL:
2683 rc = ethtool_set_value_void(dev, useraddr,
2684 dev->ethtool_ops->set_msglevel);
2687 rc = ethtool_get_eee(dev, useraddr);
2690 rc = ethtool_set_eee(dev, useraddr);
2692 case ETHTOOL_NWAY_RST:
2693 rc = ethtool_nway_reset(dev);
2696 rc = ethtool_get_link(dev, useraddr);
2698 case ETHTOOL_GEEPROM:
2699 rc = ethtool_get_eeprom(dev, useraddr);
2701 case ETHTOOL_SEEPROM:
2702 rc = ethtool_set_eeprom(dev, useraddr);
2704 case ETHTOOL_GCOALESCE:
2705 rc = ethtool_get_coalesce(dev, useraddr);
2707 case ETHTOOL_SCOALESCE:
2708 rc = ethtool_set_coalesce(dev, useraddr);
2710 case ETHTOOL_GRINGPARAM:
2711 rc = ethtool_get_ringparam(dev, useraddr);
2713 case ETHTOOL_SRINGPARAM:
2714 rc = ethtool_set_ringparam(dev, useraddr);
2716 case ETHTOOL_GPAUSEPARAM:
2717 rc = ethtool_get_pauseparam(dev, useraddr);
2719 case ETHTOOL_SPAUSEPARAM:
2720 rc = ethtool_set_pauseparam(dev, useraddr);
2723 rc = ethtool_self_test(dev, useraddr);
2725 case ETHTOOL_GSTRINGS:
2726 rc = ethtool_get_strings(dev, useraddr);
2728 case ETHTOOL_PHYS_ID:
2729 rc = ethtool_phys_id(dev, useraddr);
2731 case ETHTOOL_GSTATS:
2732 rc = ethtool_get_stats(dev, useraddr);
2734 case ETHTOOL_GPERMADDR:
2735 rc = ethtool_get_perm_addr(dev, useraddr);
2737 case ETHTOOL_GFLAGS:
2738 rc = ethtool_get_value(dev, useraddr, ethcmd,
2739 __ethtool_get_flags);
2741 case ETHTOOL_SFLAGS:
2742 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2744 case ETHTOOL_GPFLAGS:
2745 rc = ethtool_get_value(dev, useraddr, ethcmd,
2746 dev->ethtool_ops->get_priv_flags);
2748 case ETHTOOL_SPFLAGS:
2749 rc = ethtool_set_value(dev, useraddr,
2750 dev->ethtool_ops->set_priv_flags);
2753 case ETHTOOL_GRXRINGS:
2754 case ETHTOOL_GRXCLSRLCNT:
2755 case ETHTOOL_GRXCLSRULE:
2756 case ETHTOOL_GRXCLSRLALL:
2757 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2760 case ETHTOOL_SRXCLSRLDEL:
2761 case ETHTOOL_SRXCLSRLINS:
2762 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2764 case ETHTOOL_FLASHDEV:
2765 rc = ethtool_flash_device(dev, useraddr);
2768 rc = ethtool_reset(dev, useraddr);
2770 case ETHTOOL_GSSET_INFO:
2771 rc = ethtool_get_sset_info(dev, useraddr);
2773 case ETHTOOL_GRXFHINDIR:
2774 rc = ethtool_get_rxfh_indir(dev, useraddr);
2776 case ETHTOOL_SRXFHINDIR:
2777 rc = ethtool_set_rxfh_indir(dev, useraddr);
2780 rc = ethtool_get_rxfh(dev, useraddr);
2783 rc = ethtool_set_rxfh(dev, useraddr);
2785 case ETHTOOL_GFEATURES:
2786 rc = ethtool_get_features(dev, useraddr);
2788 case ETHTOOL_SFEATURES:
2789 rc = ethtool_set_features(dev, useraddr);
2791 case ETHTOOL_GTXCSUM:
2792 case ETHTOOL_GRXCSUM:
2797 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2799 case ETHTOOL_STXCSUM:
2800 case ETHTOOL_SRXCSUM:
2805 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2807 case ETHTOOL_GCHANNELS:
2808 rc = ethtool_get_channels(dev, useraddr);
2810 case ETHTOOL_SCHANNELS:
2811 rc = ethtool_set_channels(dev, useraddr);
2813 case ETHTOOL_SET_DUMP:
2814 rc = ethtool_set_dump(dev, useraddr);
2816 case ETHTOOL_GET_DUMP_FLAG:
2817 rc = ethtool_get_dump_flag(dev, useraddr);
2819 case ETHTOOL_GET_DUMP_DATA:
2820 rc = ethtool_get_dump_data(dev, useraddr);
2822 case ETHTOOL_GET_TS_INFO:
2823 rc = ethtool_get_ts_info(dev, useraddr);
2825 case ETHTOOL_GMODULEINFO:
2826 rc = ethtool_get_module_info(dev, useraddr);
2828 case ETHTOOL_GMODULEEEPROM:
2829 rc = ethtool_get_module_eeprom(dev, useraddr);
2831 case ETHTOOL_GTUNABLE:
2832 rc = ethtool_get_tunable(dev, useraddr);
2834 case ETHTOOL_STUNABLE:
2835 rc = ethtool_set_tunable(dev, useraddr);
2837 case ETHTOOL_GPHYSTATS:
2838 rc = ethtool_get_phy_stats(dev, useraddr);
2840 case ETHTOOL_PERQUEUE:
2841 rc = ethtool_set_per_queue(dev, useraddr);
2843 case ETHTOOL_GLINKSETTINGS:
2844 rc = ethtool_get_link_ksettings(dev, useraddr);
2846 case ETHTOOL_SLINKSETTINGS:
2847 rc = ethtool_set_link_ksettings(dev, useraddr);
2849 case ETHTOOL_PHY_GTUNABLE:
2850 rc = get_phy_tunable(dev, useraddr);
2852 case ETHTOOL_PHY_STUNABLE:
2853 rc = set_phy_tunable(dev, useraddr);
2855 case ETHTOOL_GFECPARAM:
2856 rc = ethtool_get_fecparam(dev, useraddr);
2858 case ETHTOOL_SFECPARAM:
2859 rc = ethtool_set_fecparam(dev, useraddr);
2865 if (dev->ethtool_ops->complete)
2866 dev->ethtool_ops->complete(dev);
2868 if (old_features != dev->features)
2869 netdev_features_change(dev);