2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <linux/net_tstamp.h>
21 #include <linux/phy.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/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",
116 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
117 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
118 [ETH_RSS_HASH_XOR_BIT] = "xor",
119 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
123 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
124 [ETHTOOL_ID_UNSPEC] = "Unspec",
125 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
126 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
127 [ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
131 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
132 [ETHTOOL_ID_UNSPEC] = "Unspec",
133 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
136 static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
138 struct ethtool_gfeatures cmd = {
139 .cmd = ETHTOOL_GFEATURES,
140 .size = ETHTOOL_DEV_FEATURE_WORDS,
142 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
143 u32 __user *sizeaddr;
147 /* in case feature bits run out again */
148 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
150 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
151 features[i].available = (u32)(dev->hw_features >> (32 * i));
152 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
153 features[i].active = (u32)(dev->features >> (32 * i));
154 features[i].never_changed =
155 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
158 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
159 if (get_user(copy_size, sizeaddr))
162 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
163 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
165 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
167 useraddr += sizeof(cmd);
168 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
174 static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
176 struct ethtool_sfeatures cmd;
177 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
178 netdev_features_t wanted = 0, valid = 0;
181 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
183 useraddr += sizeof(cmd);
185 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
188 if (copy_from_user(features, useraddr, sizeof(features)))
191 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
192 valid |= (netdev_features_t)features[i].valid << (32 * i);
193 wanted |= (netdev_features_t)features[i].requested << (32 * i);
196 if (valid & ~NETIF_F_ETHTOOL_BITS)
199 if (valid & ~dev->hw_features) {
200 valid &= dev->hw_features;
201 ret |= ETHTOOL_F_UNSUPPORTED;
204 dev->wanted_features &= ~valid;
205 dev->wanted_features |= wanted & valid;
206 __netdev_update_features(dev);
208 if ((dev->wanted_features ^ dev->features) & valid)
209 ret |= ETHTOOL_F_WISH;
214 static int __ethtool_get_sset_count(struct net_device *dev, int sset)
216 const struct ethtool_ops *ops = dev->ethtool_ops;
218 if (sset == ETH_SS_FEATURES)
219 return ARRAY_SIZE(netdev_features_strings);
221 if (sset == ETH_SS_RSS_HASH_FUNCS)
222 return ARRAY_SIZE(rss_hash_func_strings);
224 if (sset == ETH_SS_TUNABLES)
225 return ARRAY_SIZE(tunable_strings);
227 if (sset == ETH_SS_PHY_TUNABLES)
228 return ARRAY_SIZE(phy_tunable_strings);
230 if (sset == ETH_SS_PHY_STATS && dev->phydev &&
231 !ops->get_ethtool_phy_stats)
232 return phy_ethtool_get_sset_count(dev->phydev);
234 if (ops->get_sset_count && ops->get_strings)
235 return ops->get_sset_count(dev, sset);
240 static void __ethtool_get_strings(struct net_device *dev,
241 u32 stringset, u8 *data)
243 const struct ethtool_ops *ops = dev->ethtool_ops;
245 if (stringset == ETH_SS_FEATURES)
246 memcpy(data, netdev_features_strings,
247 sizeof(netdev_features_strings));
248 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
249 memcpy(data, rss_hash_func_strings,
250 sizeof(rss_hash_func_strings));
251 else if (stringset == ETH_SS_TUNABLES)
252 memcpy(data, tunable_strings, sizeof(tunable_strings));
253 else if (stringset == ETH_SS_PHY_TUNABLES)
254 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
255 else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
256 !ops->get_ethtool_phy_stats)
257 phy_ethtool_get_strings(dev->phydev, data);
259 /* ops->get_strings is valid because checked earlier */
260 ops->get_strings(dev, stringset, data);
263 static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
265 /* feature masks of legacy discrete ethtool ops */
268 case ETHTOOL_GTXCSUM:
269 case ETHTOOL_STXCSUM:
270 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
271 case ETHTOOL_GRXCSUM:
272 case ETHTOOL_SRXCSUM:
273 return NETIF_F_RXCSUM;
279 return NETIF_F_ALL_TSO;
291 static int ethtool_get_one_feature(struct net_device *dev,
292 char __user *useraddr, u32 ethcmd)
294 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
295 struct ethtool_value edata = {
297 .data = !!(dev->features & mask),
300 if (copy_to_user(useraddr, &edata, sizeof(edata)))
305 static int ethtool_set_one_feature(struct net_device *dev,
306 void __user *useraddr, u32 ethcmd)
308 struct ethtool_value edata;
309 netdev_features_t mask;
311 if (copy_from_user(&edata, useraddr, sizeof(edata)))
314 mask = ethtool_get_feature_mask(ethcmd);
315 mask &= dev->hw_features;
320 dev->wanted_features |= mask;
322 dev->wanted_features &= ~mask;
324 __netdev_update_features(dev);
329 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
330 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
331 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
332 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
335 static u32 __ethtool_get_flags(struct net_device *dev)
339 if (dev->features & NETIF_F_LRO)
340 flags |= ETH_FLAG_LRO;
341 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
342 flags |= ETH_FLAG_RXVLAN;
343 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
344 flags |= ETH_FLAG_TXVLAN;
345 if (dev->features & NETIF_F_NTUPLE)
346 flags |= ETH_FLAG_NTUPLE;
347 if (dev->features & NETIF_F_RXHASH)
348 flags |= ETH_FLAG_RXHASH;
353 static int __ethtool_set_flags(struct net_device *dev, u32 data)
355 netdev_features_t features = 0, changed;
357 if (data & ~ETH_ALL_FLAGS)
360 if (data & ETH_FLAG_LRO)
361 features |= NETIF_F_LRO;
362 if (data & ETH_FLAG_RXVLAN)
363 features |= NETIF_F_HW_VLAN_CTAG_RX;
364 if (data & ETH_FLAG_TXVLAN)
365 features |= NETIF_F_HW_VLAN_CTAG_TX;
366 if (data & ETH_FLAG_NTUPLE)
367 features |= NETIF_F_NTUPLE;
368 if (data & ETH_FLAG_RXHASH)
369 features |= NETIF_F_RXHASH;
371 /* allow changing only bits set in hw_features */
372 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
373 if (changed & ~dev->hw_features)
374 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
376 dev->wanted_features =
377 (dev->wanted_features & ~changed) | (features & changed);
379 __netdev_update_features(dev);
384 /* Given two link masks, AND them together and save the result in dst. */
385 void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
386 struct ethtool_link_ksettings *src)
388 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
389 unsigned int idx = 0;
391 for (; idx < size; idx++) {
392 dst->link_modes.supported[idx] &=
393 src->link_modes.supported[idx];
394 dst->link_modes.advertising[idx] &=
395 src->link_modes.advertising[idx];
398 EXPORT_SYMBOL(ethtool_intersect_link_masks);
400 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
403 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
406 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
408 /* return false if src had higher bits set. lower bits always updated. */
409 bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
410 const unsigned long *src)
414 /* TODO: following test will soon always be true */
415 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
416 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
418 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
419 bitmap_fill(ext, 32);
420 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
421 if (bitmap_intersects(ext, src,
422 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
423 /* src mask goes beyond bit 31 */
427 *legacy_u32 = src[0];
430 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
432 /* return false if legacy contained non-0 deprecated fields
433 * maxtxpkt/maxrxpkt. rest of ksettings always updated
436 convert_legacy_settings_to_link_ksettings(
437 struct ethtool_link_ksettings *link_ksettings,
438 const struct ethtool_cmd *legacy_settings)
442 memset(link_ksettings, 0, sizeof(*link_ksettings));
444 /* This is used to tell users that driver is still using these
445 * deprecated legacy fields, and they should not use
446 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
448 if (legacy_settings->maxtxpkt ||
449 legacy_settings->maxrxpkt)
452 ethtool_convert_legacy_u32_to_link_mode(
453 link_ksettings->link_modes.supported,
454 legacy_settings->supported);
455 ethtool_convert_legacy_u32_to_link_mode(
456 link_ksettings->link_modes.advertising,
457 legacy_settings->advertising);
458 ethtool_convert_legacy_u32_to_link_mode(
459 link_ksettings->link_modes.lp_advertising,
460 legacy_settings->lp_advertising);
461 link_ksettings->base.speed
462 = ethtool_cmd_speed(legacy_settings);
463 link_ksettings->base.duplex
464 = legacy_settings->duplex;
465 link_ksettings->base.port
466 = legacy_settings->port;
467 link_ksettings->base.phy_address
468 = legacy_settings->phy_address;
469 link_ksettings->base.autoneg
470 = legacy_settings->autoneg;
471 link_ksettings->base.mdio_support
472 = legacy_settings->mdio_support;
473 link_ksettings->base.eth_tp_mdix
474 = legacy_settings->eth_tp_mdix;
475 link_ksettings->base.eth_tp_mdix_ctrl
476 = legacy_settings->eth_tp_mdix_ctrl;
480 /* return false if ksettings link modes had higher bits
481 * set. legacy_settings always updated (best effort)
484 convert_link_ksettings_to_legacy_settings(
485 struct ethtool_cmd *legacy_settings,
486 const struct ethtool_link_ksettings *link_ksettings)
490 memset(legacy_settings, 0, sizeof(*legacy_settings));
491 /* this also clears the deprecated fields in legacy structure:
497 retval &= ethtool_convert_link_mode_to_legacy_u32(
498 &legacy_settings->supported,
499 link_ksettings->link_modes.supported);
500 retval &= ethtool_convert_link_mode_to_legacy_u32(
501 &legacy_settings->advertising,
502 link_ksettings->link_modes.advertising);
503 retval &= ethtool_convert_link_mode_to_legacy_u32(
504 &legacy_settings->lp_advertising,
505 link_ksettings->link_modes.lp_advertising);
506 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
507 legacy_settings->duplex
508 = link_ksettings->base.duplex;
509 legacy_settings->port
510 = link_ksettings->base.port;
511 legacy_settings->phy_address
512 = link_ksettings->base.phy_address;
513 legacy_settings->autoneg
514 = link_ksettings->base.autoneg;
515 legacy_settings->mdio_support
516 = link_ksettings->base.mdio_support;
517 legacy_settings->eth_tp_mdix
518 = link_ksettings->base.eth_tp_mdix;
519 legacy_settings->eth_tp_mdix_ctrl
520 = link_ksettings->base.eth_tp_mdix_ctrl;
521 legacy_settings->transceiver
522 = link_ksettings->base.transceiver;
526 /* number of 32-bit words to store the user's link mode bitmaps */
527 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
528 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
530 /* layout of the struct passed from/to userland */
531 struct ethtool_link_usettings {
532 struct ethtool_link_settings base;
534 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
535 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
536 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
540 /* Internal kernel helper to query a device ethtool_link_settings.
542 * Backward compatibility note: for compatibility with legacy drivers
543 * that implement only the ethtool_cmd API, this has to work with both
544 * drivers implementing get_link_ksettings API and drivers
545 * implementing get_settings API. When drivers implement get_settings
546 * and report ethtool_cmd deprecated fields
547 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
548 * because the resulting struct ethtool_link_settings does not report them.
550 int __ethtool_get_link_ksettings(struct net_device *dev,
551 struct ethtool_link_ksettings *link_ksettings)
554 struct ethtool_cmd cmd;
558 if (dev->ethtool_ops->get_link_ksettings) {
559 memset(link_ksettings, 0, sizeof(*link_ksettings));
560 return dev->ethtool_ops->get_link_ksettings(dev,
564 /* driver doesn't support %ethtool_link_ksettings API. revert to
565 * legacy %ethtool_cmd API, unless it's not supported either.
566 * TODO: remove when ethtool_ops::get_settings disappears internally
568 if (!dev->ethtool_ops->get_settings)
571 memset(&cmd, 0, sizeof(cmd));
572 cmd.cmd = ETHTOOL_GSET;
573 err = dev->ethtool_ops->get_settings(dev, &cmd);
577 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
579 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
582 EXPORT_SYMBOL(__ethtool_get_link_ksettings);
584 /* convert ethtool_link_usettings in user space to a kernel internal
585 * ethtool_link_ksettings. return 0 on success, errno on error.
587 static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
588 const void __user *from)
590 struct ethtool_link_usettings link_usettings;
592 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
595 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
596 bitmap_from_arr32(to->link_modes.supported,
597 link_usettings.link_modes.supported,
598 __ETHTOOL_LINK_MODE_MASK_NBITS);
599 bitmap_from_arr32(to->link_modes.advertising,
600 link_usettings.link_modes.advertising,
601 __ETHTOOL_LINK_MODE_MASK_NBITS);
602 bitmap_from_arr32(to->link_modes.lp_advertising,
603 link_usettings.link_modes.lp_advertising,
604 __ETHTOOL_LINK_MODE_MASK_NBITS);
609 /* convert a kernel internal ethtool_link_ksettings to
610 * ethtool_link_usettings in user space. return 0 on success, errno on
614 store_link_ksettings_for_user(void __user *to,
615 const struct ethtool_link_ksettings *from)
617 struct ethtool_link_usettings link_usettings;
619 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
620 bitmap_to_arr32(link_usettings.link_modes.supported,
621 from->link_modes.supported,
622 __ETHTOOL_LINK_MODE_MASK_NBITS);
623 bitmap_to_arr32(link_usettings.link_modes.advertising,
624 from->link_modes.advertising,
625 __ETHTOOL_LINK_MODE_MASK_NBITS);
626 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
627 from->link_modes.lp_advertising,
628 __ETHTOOL_LINK_MODE_MASK_NBITS);
630 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
636 /* Query device for its ethtool_link_settings.
638 * Backward compatibility note: this function must fail when driver
639 * does not implement ethtool::get_link_ksettings, even if legacy
640 * ethtool_ops::get_settings is implemented. This tells new versions
641 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
642 * this driver, so that they can correctly access the ethtool_cmd
643 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
644 * implements ethtool_ops::get_settings anymore.
646 static int ethtool_get_link_ksettings(struct net_device *dev,
647 void __user *useraddr)
650 struct ethtool_link_ksettings link_ksettings;
654 if (!dev->ethtool_ops->get_link_ksettings)
657 /* handle bitmap nbits handshake */
658 if (copy_from_user(&link_ksettings.base, useraddr,
659 sizeof(link_ksettings.base)))
662 if (__ETHTOOL_LINK_MODE_MASK_NU32
663 != link_ksettings.base.link_mode_masks_nwords) {
664 /* wrong link mode nbits requested */
665 memset(&link_ksettings, 0, sizeof(link_ksettings));
666 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
667 /* send back number of words required as negative val */
668 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
669 "need too many bits for link modes!");
670 link_ksettings.base.link_mode_masks_nwords
671 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
673 /* copy the base fields back to user, not the link
676 if (copy_to_user(useraddr, &link_ksettings.base,
677 sizeof(link_ksettings.base)))
683 /* handshake successful: user/kernel agree on
684 * link_mode_masks_nwords
687 memset(&link_ksettings, 0, sizeof(link_ksettings));
688 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
692 /* make sure we tell the right values to user */
693 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
694 link_ksettings.base.link_mode_masks_nwords
695 = __ETHTOOL_LINK_MODE_MASK_NU32;
697 return store_link_ksettings_for_user(useraddr, &link_ksettings);
700 /* Update device ethtool_link_settings.
702 * Backward compatibility note: this function must fail when driver
703 * does not implement ethtool::set_link_ksettings, even if legacy
704 * ethtool_ops::set_settings is implemented. This tells new versions
705 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
706 * this driver, so that they can correctly update the ethtool_cmd
707 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
708 * implements ethtool_ops::get_settings anymore.
710 static int ethtool_set_link_ksettings(struct net_device *dev,
711 void __user *useraddr)
714 struct ethtool_link_ksettings link_ksettings;
718 if (!dev->ethtool_ops->set_link_ksettings)
721 /* make sure nbits field has expected value */
722 if (copy_from_user(&link_ksettings.base, useraddr,
723 sizeof(link_ksettings.base)))
726 if (__ETHTOOL_LINK_MODE_MASK_NU32
727 != link_ksettings.base.link_mode_masks_nwords)
730 /* copy the whole structure, now that we know it has expected
733 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
737 /* re-check nwords field, just in case */
738 if (__ETHTOOL_LINK_MODE_MASK_NU32
739 != link_ksettings.base.link_mode_masks_nwords)
742 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
745 /* Query device for its ethtool_cmd settings.
747 * Backward compatibility note: for compatibility with legacy ethtool,
748 * this has to work with both drivers implementing get_link_ksettings
749 * API and drivers implementing get_settings API. When drivers
750 * implement get_link_ksettings and report higher link mode bits, a
751 * kernel warning is logged once (with name of 1st driver/device) to
752 * recommend user to upgrade ethtool, but the command is successful
753 * (only the lower link mode bits reported back to user).
755 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
757 struct ethtool_cmd cmd;
761 if (dev->ethtool_ops->get_link_ksettings) {
762 /* First, use link_ksettings API if it is supported */
764 struct ethtool_link_ksettings link_ksettings;
766 memset(&link_ksettings, 0, sizeof(link_ksettings));
767 err = dev->ethtool_ops->get_link_ksettings(dev,
771 convert_link_ksettings_to_legacy_settings(&cmd,
774 /* send a sensible cmd tag back to user */
775 cmd.cmd = ETHTOOL_GSET;
777 /* driver doesn't support %ethtool_link_ksettings
778 * API. revert to legacy %ethtool_cmd API, unless it's
779 * not supported either.
783 if (!dev->ethtool_ops->get_settings)
786 memset(&cmd, 0, sizeof(cmd));
787 cmd.cmd = ETHTOOL_GSET;
788 err = dev->ethtool_ops->get_settings(dev, &cmd);
793 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
799 /* Update device link settings with given ethtool_cmd.
801 * Backward compatibility note: for compatibility with legacy ethtool,
802 * this has to work with both drivers implementing set_link_ksettings
803 * API and drivers implementing set_settings API. When drivers
804 * implement set_link_ksettings and user's request updates deprecated
805 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
806 * warning is logged once (with name of 1st driver/device) to
807 * recommend user to upgrade ethtool, and the request is rejected.
809 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
811 struct ethtool_cmd cmd;
815 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
818 /* first, try new %ethtool_link_ksettings API. */
819 if (dev->ethtool_ops->set_link_ksettings) {
820 struct ethtool_link_ksettings link_ksettings;
822 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
826 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
827 link_ksettings.base.link_mode_masks_nwords
828 = __ETHTOOL_LINK_MODE_MASK_NU32;
829 return dev->ethtool_ops->set_link_ksettings(dev,
833 /* legacy %ethtool_cmd API */
835 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
836 * disappears internally
839 if (!dev->ethtool_ops->set_settings)
842 return dev->ethtool_ops->set_settings(dev, &cmd);
845 static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
846 void __user *useraddr)
848 struct ethtool_drvinfo info;
849 const struct ethtool_ops *ops = dev->ethtool_ops;
851 memset(&info, 0, sizeof(info));
852 info.cmd = ETHTOOL_GDRVINFO;
853 if (ops->get_drvinfo) {
854 ops->get_drvinfo(dev, &info);
855 } else if (dev->dev.parent && dev->dev.parent->driver) {
856 strlcpy(info.bus_info, dev_name(dev->dev.parent),
857 sizeof(info.bus_info));
858 strlcpy(info.driver, dev->dev.parent->driver->name,
859 sizeof(info.driver));
865 * this method of obtaining string set info is deprecated;
866 * Use ETHTOOL_GSSET_INFO instead.
868 if (ops->get_sset_count) {
871 rc = ops->get_sset_count(dev, ETH_SS_TEST);
873 info.testinfo_len = rc;
874 rc = ops->get_sset_count(dev, ETH_SS_STATS);
877 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
879 info.n_priv_flags = rc;
881 if (ops->get_regs_len)
882 info.regdump_len = ops->get_regs_len(dev);
883 if (ops->get_eeprom_len)
884 info.eedump_len = ops->get_eeprom_len(dev);
886 if (copy_to_user(useraddr, &info, sizeof(info)))
891 static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
892 void __user *useraddr)
894 struct ethtool_sset_info info;
896 int i, idx = 0, n_bits = 0, ret, rc;
897 u32 *info_buf = NULL;
899 if (copy_from_user(&info, useraddr, sizeof(info)))
902 /* store copy of mask, because we zero struct later on */
903 sset_mask = info.sset_mask;
907 /* calculate size of return buffer */
908 n_bits = hweight64(sset_mask);
910 memset(&info, 0, sizeof(info));
911 info.cmd = ETHTOOL_GSSET_INFO;
913 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
918 * fill return buffer based on input bitmask and successful
919 * get_sset_count return
921 for (i = 0; i < 64; i++) {
922 if (!(sset_mask & (1ULL << i)))
925 rc = __ethtool_get_sset_count(dev, i);
927 info.sset_mask |= (1ULL << i);
928 info_buf[idx++] = rc;
933 if (copy_to_user(useraddr, &info, sizeof(info)))
936 useraddr += offsetof(struct ethtool_sset_info, data);
937 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
947 static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
948 u32 cmd, void __user *useraddr)
950 struct ethtool_rxnfc info;
951 size_t info_size = sizeof(info);
954 if (!dev->ethtool_ops->set_rxnfc)
957 /* struct ethtool_rxnfc was originally defined for
958 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
959 * members. User-space might still be using that
961 if (cmd == ETHTOOL_SRXFH)
962 info_size = (offsetof(struct ethtool_rxnfc, data) +
965 if (copy_from_user(&info, useraddr, info_size))
968 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
972 if (cmd == ETHTOOL_SRXCLSRLINS &&
973 copy_to_user(useraddr, &info, info_size))
979 static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
980 u32 cmd, void __user *useraddr)
982 struct ethtool_rxnfc info;
983 size_t info_size = sizeof(info);
984 const struct ethtool_ops *ops = dev->ethtool_ops;
986 void *rule_buf = NULL;
991 /* struct ethtool_rxnfc was originally defined for
992 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
993 * members. User-space might still be using that
995 if (cmd == ETHTOOL_GRXFH)
996 info_size = (offsetof(struct ethtool_rxnfc, data) +
999 if (copy_from_user(&info, useraddr, info_size))
1002 /* If FLOW_RSS was requested then user-space must be using the
1003 * new definition, as FLOW_RSS is newer.
1005 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1006 info_size = sizeof(info);
1007 if (copy_from_user(&info, useraddr, info_size))
1011 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1012 if (info.rule_cnt > 0) {
1013 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
1014 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
1021 ret = ops->get_rxnfc(dev, &info, rule_buf);
1026 if (copy_to_user(useraddr, &info, info_size))
1030 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1031 if (copy_to_user(useraddr, rule_buf,
1032 info.rule_cnt * sizeof(u32)))
1043 static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1044 struct ethtool_rxnfc *rx_rings,
1049 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
1052 /* Validate ring indices */
1053 for (i = 0; i < size; i++)
1054 if (indir[i] >= rx_rings->data)
1060 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1062 void netdev_rss_key_fill(void *buffer, size_t len)
1064 BUG_ON(len > sizeof(netdev_rss_key));
1065 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1066 memcpy(buffer, netdev_rss_key, len);
1068 EXPORT_SYMBOL(netdev_rss_key_fill);
1070 static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1072 u32 dev_size, current_max = 0;
1076 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1077 !dev->ethtool_ops->get_rxfh)
1079 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1083 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1087 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1092 current_max = max(current_max, indir[dev_size]);
1101 static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1102 void __user *useraddr)
1104 u32 user_size, dev_size;
1108 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1109 !dev->ethtool_ops->get_rxfh)
1111 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1115 if (copy_from_user(&user_size,
1116 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1120 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1121 &dev_size, sizeof(dev_size)))
1124 /* If the user buffer size is 0, this is just a query for the
1125 * device table size. Otherwise, if it's smaller than the
1126 * device table size it's an error.
1128 if (user_size < dev_size)
1129 return user_size == 0 ? 0 : -EINVAL;
1131 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1135 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1139 if (copy_to_user(useraddr +
1140 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1141 indir, dev_size * sizeof(indir[0])))
1149 static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1150 void __user *useraddr)
1152 struct ethtool_rxnfc rx_rings;
1153 u32 user_size, dev_size, i;
1155 const struct ethtool_ops *ops = dev->ethtool_ops;
1157 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
1159 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
1163 dev_size = ops->get_rxfh_indir_size(dev);
1167 if (copy_from_user(&user_size,
1168 useraddr + offsetof(struct ethtool_rxfh_indir, size),
1172 if (user_size != 0 && user_size != dev_size)
1175 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1179 rx_rings.cmd = ETHTOOL_GRXRINGS;
1180 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1184 if (user_size == 0) {
1185 for (i = 0; i < dev_size; i++)
1186 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1188 ret = ethtool_copy_validate_indir(indir,
1189 useraddr + ringidx_offset,
1196 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
1200 /* indicate whether rxfh was set to default */
1202 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1204 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1211 static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1212 void __user *useraddr)
1215 const struct ethtool_ops *ops = dev->ethtool_ops;
1216 u32 user_indir_size, user_key_size;
1217 u32 dev_indir_size = 0, dev_key_size = 0;
1218 struct ethtool_rxfh rxfh;
1229 if (ops->get_rxfh_indir_size)
1230 dev_indir_size = ops->get_rxfh_indir_size(dev);
1231 if (ops->get_rxfh_key_size)
1232 dev_key_size = ops->get_rxfh_key_size(dev);
1234 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1236 user_indir_size = rxfh.indir_size;
1237 user_key_size = rxfh.key_size;
1239 /* Check that reserved fields are 0 for now */
1240 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1242 /* Most drivers don't handle rss_context, check it's 0 as well */
1243 if (rxfh.rss_context && !ops->get_rxfh_context)
1246 rxfh.indir_size = dev_indir_size;
1247 rxfh.key_size = dev_key_size;
1248 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
1251 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1252 (user_key_size && (user_key_size != dev_key_size)))
1255 indir_bytes = user_indir_size * sizeof(indir[0]);
1256 total_size = indir_bytes + user_key_size;
1257 rss_config = kzalloc(total_size, GFP_USER);
1261 if (user_indir_size)
1262 indir = (u32 *)rss_config;
1265 hkey = rss_config + indir_bytes;
1267 if (rxfh.rss_context)
1268 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1272 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1276 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1277 &dev_hfunc, sizeof(rxfh.hfunc))) {
1279 } else if (copy_to_user(useraddr +
1280 offsetof(struct ethtool_rxfh, rss_config[0]),
1281 rss_config, total_size)) {
1290 static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1291 void __user *useraddr)
1294 const struct ethtool_ops *ops = dev->ethtool_ops;
1295 struct ethtool_rxnfc rx_rings;
1296 struct ethtool_rxfh rxfh;
1297 u32 dev_indir_size = 0, dev_key_size = 0, i;
1298 u32 *indir = NULL, indir_bytes = 0;
1301 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1302 bool delete = false;
1304 if (!ops->get_rxnfc || !ops->set_rxfh)
1307 if (ops->get_rxfh_indir_size)
1308 dev_indir_size = ops->get_rxfh_indir_size(dev);
1309 if (ops->get_rxfh_key_size)
1310 dev_key_size = ops->get_rxfh_key_size(dev);
1312 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
1315 /* Check that reserved fields are 0 for now */
1316 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
1318 /* Most drivers don't handle rss_context, check it's 0 as well */
1319 if (rxfh.rss_context && !ops->set_rxfh_context)
1322 /* If either indir, hash key or function is valid, proceed further.
1323 * Must request at least one change: indir size, hash key or function.
1325 if ((rxfh.indir_size &&
1326 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1327 rxfh.indir_size != dev_indir_size) ||
1328 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1329 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
1330 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
1333 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1334 indir_bytes = dev_indir_size * sizeof(indir[0]);
1336 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
1340 rx_rings.cmd = ETHTOOL_GRXRINGS;
1341 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1345 /* rxfh.indir_size == 0 means reset the indir table to default (master
1346 * context) or delete the context (other RSS contexts).
1347 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1349 if (rxfh.indir_size &&
1350 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
1351 indir = (u32 *)rss_config;
1352 ret = ethtool_copy_validate_indir(indir,
1353 useraddr + rss_cfg_offset,
1358 } else if (rxfh.indir_size == 0) {
1359 if (rxfh.rss_context == 0) {
1360 indir = (u32 *)rss_config;
1361 for (i = 0; i < dev_indir_size; i++)
1362 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1368 if (rxfh.key_size) {
1369 hkey = rss_config + indir_bytes;
1370 if (copy_from_user(hkey,
1371 useraddr + rss_cfg_offset + indir_bytes,
1378 if (rxfh.rss_context)
1379 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1380 &rxfh.rss_context, delete);
1382 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
1386 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1387 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1390 if (!rxfh.rss_context) {
1391 /* indicate whether rxfh was set to default */
1392 if (rxfh.indir_size == 0)
1393 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1394 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1395 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1403 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1405 struct ethtool_regs regs;
1406 const struct ethtool_ops *ops = dev->ethtool_ops;
1410 if (!ops->get_regs || !ops->get_regs_len)
1413 if (copy_from_user(®s, useraddr, sizeof(regs)))
1416 reglen = ops->get_regs_len(dev);
1417 if (regs.len > reglen)
1422 regbuf = vzalloc(reglen);
1427 ops->get_regs(dev, ®s, regbuf);
1430 if (copy_to_user(useraddr, ®s, sizeof(regs)))
1432 useraddr += offsetof(struct ethtool_regs, data);
1433 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
1442 static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1444 struct ethtool_value reset;
1447 if (!dev->ethtool_ops->reset)
1450 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1453 ret = dev->ethtool_ops->reset(dev, &reset.data);
1457 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1462 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1464 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
1466 if (!dev->ethtool_ops->get_wol)
1469 dev->ethtool_ops->get_wol(dev, &wol);
1471 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1476 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1478 struct ethtool_wolinfo wol;
1480 if (!dev->ethtool_ops->set_wol)
1483 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1486 return dev->ethtool_ops->set_wol(dev, &wol);
1489 static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1491 struct ethtool_eee edata;
1494 if (!dev->ethtool_ops->get_eee)
1497 memset(&edata, 0, sizeof(struct ethtool_eee));
1498 edata.cmd = ETHTOOL_GEEE;
1499 rc = dev->ethtool_ops->get_eee(dev, &edata);
1504 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1510 static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1512 struct ethtool_eee edata;
1514 if (!dev->ethtool_ops->set_eee)
1517 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1520 return dev->ethtool_ops->set_eee(dev, &edata);
1523 static int ethtool_nway_reset(struct net_device *dev)
1525 if (!dev->ethtool_ops->nway_reset)
1528 return dev->ethtool_ops->nway_reset(dev);
1531 static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1533 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1535 if (!dev->ethtool_ops->get_link)
1538 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1540 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1545 static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1546 int (*getter)(struct net_device *,
1547 struct ethtool_eeprom *, u8 *),
1550 struct ethtool_eeprom eeprom;
1551 void __user *userbuf = useraddr + sizeof(eeprom);
1552 u32 bytes_remaining;
1556 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1559 /* Check for wrap and zero */
1560 if (eeprom.offset + eeprom.len <= eeprom.offset)
1563 /* Check for exceeding total eeprom len */
1564 if (eeprom.offset + eeprom.len > total_len)
1567 data = kmalloc(PAGE_SIZE, GFP_USER);
1571 bytes_remaining = eeprom.len;
1572 while (bytes_remaining > 0) {
1573 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1575 ret = getter(dev, &eeprom, data);
1578 if (copy_to_user(userbuf, data, eeprom.len)) {
1582 userbuf += eeprom.len;
1583 eeprom.offset += eeprom.len;
1584 bytes_remaining -= eeprom.len;
1587 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1588 eeprom.offset -= eeprom.len;
1589 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1596 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1598 const struct ethtool_ops *ops = dev->ethtool_ops;
1600 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1601 !ops->get_eeprom_len(dev))
1604 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1605 ops->get_eeprom_len(dev));
1608 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1610 struct ethtool_eeprom eeprom;
1611 const struct ethtool_ops *ops = dev->ethtool_ops;
1612 void __user *userbuf = useraddr + sizeof(eeprom);
1613 u32 bytes_remaining;
1617 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1618 !ops->get_eeprom_len(dev))
1621 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1624 /* Check for wrap and zero */
1625 if (eeprom.offset + eeprom.len <= eeprom.offset)
1628 /* Check for exceeding total eeprom len */
1629 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1632 data = kmalloc(PAGE_SIZE, GFP_USER);
1636 bytes_remaining = eeprom.len;
1637 while (bytes_remaining > 0) {
1638 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
1640 if (copy_from_user(data, userbuf, eeprom.len)) {
1644 ret = ops->set_eeprom(dev, &eeprom, data);
1647 userbuf += eeprom.len;
1648 eeprom.offset += eeprom.len;
1649 bytes_remaining -= eeprom.len;
1656 static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1657 void __user *useraddr)
1659 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1661 if (!dev->ethtool_ops->get_coalesce)
1664 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1666 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1671 static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1672 void __user *useraddr)
1674 struct ethtool_coalesce coalesce;
1676 if (!dev->ethtool_ops->set_coalesce)
1679 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1682 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1685 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1687 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
1689 if (!dev->ethtool_ops->get_ringparam)
1692 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1694 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1699 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1701 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1703 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
1706 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1709 dev->ethtool_ops->get_ringparam(dev, &max);
1711 /* ensure new ring parameters are within the maximums */
1712 if (ringparam.rx_pending > max.rx_max_pending ||
1713 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1714 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1715 ringparam.tx_pending > max.tx_max_pending)
1718 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1721 static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1722 void __user *useraddr)
1724 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1726 if (!dev->ethtool_ops->get_channels)
1729 dev->ethtool_ops->get_channels(dev, &channels);
1731 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1736 static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1737 void __user *useraddr)
1739 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
1740 u32 max_rx_in_use = 0;
1742 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
1745 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1748 dev->ethtool_ops->get_channels(dev, &max);
1750 /* ensure new counts are within the maximums */
1751 if ((channels.rx_count > max.max_rx) ||
1752 (channels.tx_count > max.max_tx) ||
1753 (channels.combined_count > max.max_combined) ||
1754 (channels.other_count > max.max_other))
1757 /* ensure the new Rx count fits within the configured Rx flow
1758 * indirection table settings */
1759 if (netif_is_rxfh_configured(dev) &&
1760 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1761 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1764 return dev->ethtool_ops->set_channels(dev, &channels);
1767 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1769 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1771 if (!dev->ethtool_ops->get_pauseparam)
1774 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1776 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1781 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1783 struct ethtool_pauseparam pauseparam;
1785 if (!dev->ethtool_ops->set_pauseparam)
1788 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1791 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1794 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1796 struct ethtool_test test;
1797 const struct ethtool_ops *ops = dev->ethtool_ops;
1801 if (!ops->self_test || !ops->get_sset_count)
1804 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
1807 WARN_ON(test_len == 0);
1809 if (copy_from_user(&test, useraddr, sizeof(test)))
1812 test.len = test_len;
1813 data = kmalloc(test_len * sizeof(u64), GFP_USER);
1817 ops->self_test(dev, &test, data);
1820 if (copy_to_user(useraddr, &test, sizeof(test)))
1822 useraddr += sizeof(test);
1823 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1832 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1834 struct ethtool_gstrings gstrings;
1838 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1841 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
1844 if (ret > S32_MAX / ETH_GSTRING_LEN)
1849 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1850 if (gstrings.len && !data)
1853 __ethtool_get_strings(dev, gstrings.string_set, data);
1856 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1858 useraddr += sizeof(gstrings);
1860 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1869 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1871 struct ethtool_value id;
1873 const struct ethtool_ops *ops = dev->ethtool_ops;
1876 if (!ops->set_phys_id)
1882 if (copy_from_user(&id, useraddr, sizeof(id)))
1885 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
1889 /* Drop the RTNL lock while waiting, but prevent reentry or
1890 * removal of the device.
1897 /* Driver will handle this itself */
1898 schedule_timeout_interruptible(
1899 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
1901 /* Driver expects to be called at twice the frequency in rc */
1902 int n = rc * 2, i, interval = HZ / n;
1904 /* Count down seconds */
1906 /* Count down iterations per second */
1910 rc = ops->set_phys_id(dev,
1911 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1915 schedule_timeout_interruptible(interval);
1916 } while (!signal_pending(current) && --i != 0);
1917 } while (!signal_pending(current) &&
1918 (id.data == 0 || --id.data != 0));
1925 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
1929 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1931 struct ethtool_stats stats;
1932 const struct ethtool_ops *ops = dev->ethtool_ops;
1936 if (!ops->get_ethtool_stats || !ops->get_sset_count)
1939 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
1942 if (n_stats > S32_MAX / sizeof(u64))
1944 WARN_ON_ONCE(!n_stats);
1945 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1948 stats.n_stats = n_stats;
1949 data = vzalloc(n_stats * sizeof(u64));
1950 if (n_stats && !data)
1953 ops->get_ethtool_stats(dev, &stats, data);
1956 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1958 useraddr += sizeof(stats);
1959 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
1968 static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1970 const struct ethtool_ops *ops = dev->ethtool_ops;
1971 struct phy_device *phydev = dev->phydev;
1972 struct ethtool_stats stats;
1976 if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
1979 if (dev->phydev && !ops->get_ethtool_phy_stats)
1980 n_stats = phy_ethtool_get_sset_count(dev->phydev);
1982 n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
1985 if (n_stats > S32_MAX / sizeof(u64))
1987 WARN_ON_ONCE(!n_stats);
1989 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1992 stats.n_stats = n_stats;
1993 data = vzalloc(n_stats * sizeof(u64));
1994 if (n_stats && !data)
1997 if (dev->phydev && !ops->get_ethtool_phy_stats) {
1998 ret = phy_ethtool_get_stats(dev->phydev, &stats, data);
2002 ops->get_ethtool_phy_stats(dev, &stats, data);
2006 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2008 useraddr += sizeof(stats);
2009 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
2018 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
2020 struct ethtool_perm_addr epaddr;
2022 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
2025 if (epaddr.size < dev->addr_len)
2027 epaddr.size = dev->addr_len;
2029 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
2031 useraddr += sizeof(epaddr);
2032 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2037 static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2038 u32 cmd, u32 (*actor)(struct net_device *))
2040 struct ethtool_value edata = { .cmd = cmd };
2045 edata.data = actor(dev);
2047 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2052 static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2053 void (*actor)(struct net_device *, u32))
2055 struct ethtool_value edata;
2060 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2063 actor(dev, edata.data);
2067 static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2068 int (*actor)(struct net_device *, u32))
2070 struct ethtool_value edata;
2075 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2078 return actor(dev, edata.data);
2081 static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2082 char __user *useraddr)
2084 struct ethtool_flash efl;
2086 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2089 if (!dev->ethtool_ops->flash_device)
2092 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2094 return dev->ethtool_ops->flash_device(dev, &efl);
2097 static int ethtool_set_dump(struct net_device *dev,
2098 void __user *useraddr)
2100 struct ethtool_dump dump;
2102 if (!dev->ethtool_ops->set_dump)
2105 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2108 return dev->ethtool_ops->set_dump(dev, &dump);
2111 static int ethtool_get_dump_flag(struct net_device *dev,
2112 void __user *useraddr)
2115 struct ethtool_dump dump;
2116 const struct ethtool_ops *ops = dev->ethtool_ops;
2118 if (!ops->get_dump_flag)
2121 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2124 ret = ops->get_dump_flag(dev, &dump);
2128 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2133 static int ethtool_get_dump_data(struct net_device *dev,
2134 void __user *useraddr)
2138 struct ethtool_dump dump, tmp;
2139 const struct ethtool_ops *ops = dev->ethtool_ops;
2142 if (!ops->get_dump_data || !ops->get_dump_flag)
2145 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2148 memset(&tmp, 0, sizeof(tmp));
2149 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2150 ret = ops->get_dump_flag(dev, &tmp);
2154 len = min(tmp.len, dump.len);
2158 /* Don't ever let the driver think there's more space available
2159 * than it requested with .get_dump_flag().
2163 /* Always allocate enough space to hold the whole thing so that the
2164 * driver does not need to check the length and bother with partial
2167 data = vzalloc(tmp.len);
2170 ret = ops->get_dump_data(dev, &dump, data);
2174 /* There are two sane possibilities:
2175 * 1. The driver's .get_dump_data() does not touch dump.len.
2176 * 2. Or it may set dump.len to how much it really writes, which
2177 * should be tmp.len (or len if it can do a partial dump).
2178 * In any case respond to userspace with the actual length of data
2181 WARN_ON(dump.len != len && dump.len != tmp.len);
2184 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2188 useraddr += offsetof(struct ethtool_dump, data);
2189 if (copy_to_user(useraddr, data, len))
2196 static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2199 struct ethtool_ts_info info;
2200 const struct ethtool_ops *ops = dev->ethtool_ops;
2201 struct phy_device *phydev = dev->phydev;
2203 memset(&info, 0, sizeof(info));
2204 info.cmd = ETHTOOL_GET_TS_INFO;
2206 if (phydev && phydev->drv && phydev->drv->ts_info) {
2207 err = phydev->drv->ts_info(phydev, &info);
2208 } else if (ops->get_ts_info) {
2209 err = ops->get_ts_info(dev, &info);
2211 info.so_timestamping =
2212 SOF_TIMESTAMPING_RX_SOFTWARE |
2213 SOF_TIMESTAMPING_SOFTWARE;
2214 info.phc_index = -1;
2220 if (copy_to_user(useraddr, &info, sizeof(info)))
2226 static int __ethtool_get_module_info(struct net_device *dev,
2227 struct ethtool_modinfo *modinfo)
2229 const struct ethtool_ops *ops = dev->ethtool_ops;
2230 struct phy_device *phydev = dev->phydev;
2233 return sfp_get_module_info(dev->sfp_bus, modinfo);
2235 if (phydev && phydev->drv && phydev->drv->module_info)
2236 return phydev->drv->module_info(phydev, modinfo);
2238 if (ops->get_module_info)
2239 return ops->get_module_info(dev, modinfo);
2244 static int ethtool_get_module_info(struct net_device *dev,
2245 void __user *useraddr)
2248 struct ethtool_modinfo modinfo;
2250 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2253 ret = __ethtool_get_module_info(dev, &modinfo);
2257 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2263 static int __ethtool_get_module_eeprom(struct net_device *dev,
2264 struct ethtool_eeprom *ee, u8 *data)
2266 const struct ethtool_ops *ops = dev->ethtool_ops;
2267 struct phy_device *phydev = dev->phydev;
2270 return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
2272 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2273 return phydev->drv->module_eeprom(phydev, ee, data);
2275 if (ops->get_module_eeprom)
2276 return ops->get_module_eeprom(dev, ee, data);
2281 static int ethtool_get_module_eeprom(struct net_device *dev,
2282 void __user *useraddr)
2285 struct ethtool_modinfo modinfo;
2287 ret = __ethtool_get_module_info(dev, &modinfo);
2291 return ethtool_get_any_eeprom(dev, useraddr,
2292 __ethtool_get_module_eeprom,
2293 modinfo.eeprom_len);
2296 static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2299 case ETHTOOL_RX_COPYBREAK:
2300 case ETHTOOL_TX_COPYBREAK:
2301 if (tuna->len != sizeof(u32) ||
2302 tuna->type_id != ETHTOOL_TUNABLE_U32)
2305 case ETHTOOL_PFC_PREVENTION_TOUT:
2306 if (tuna->len != sizeof(u16) ||
2307 tuna->type_id != ETHTOOL_TUNABLE_U16)
2317 static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2320 struct ethtool_tunable tuna;
2321 const struct ethtool_ops *ops = dev->ethtool_ops;
2324 if (!ops->get_tunable)
2326 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2328 ret = ethtool_tunable_valid(&tuna);
2331 data = kmalloc(tuna.len, GFP_USER);
2334 ret = ops->get_tunable(dev, &tuna, data);
2337 useraddr += sizeof(tuna);
2339 if (copy_to_user(useraddr, data, tuna.len))
2348 static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2351 struct ethtool_tunable tuna;
2352 const struct ethtool_ops *ops = dev->ethtool_ops;
2355 if (!ops->set_tunable)
2357 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2359 ret = ethtool_tunable_valid(&tuna);
2362 useraddr += sizeof(tuna);
2363 data = memdup_user(useraddr, tuna.len);
2365 return PTR_ERR(data);
2366 ret = ops->set_tunable(dev, &tuna, data);
2372 static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2373 void __user *useraddr,
2374 struct ethtool_per_queue_op *per_queue_opt)
2378 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2380 if (!dev->ethtool_ops->get_per_queue_coalesce)
2383 useraddr += sizeof(*per_queue_opt);
2385 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2388 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2389 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2391 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2394 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2396 useraddr += sizeof(coalesce);
2402 static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2403 void __user *useraddr,
2404 struct ethtool_per_queue_op *per_queue_opt)
2409 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2410 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2412 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2413 (!dev->ethtool_ops->get_per_queue_coalesce))
2416 useraddr += sizeof(*per_queue_opt);
2418 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
2419 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2420 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2424 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2425 struct ethtool_coalesce coalesce;
2427 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2433 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2438 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2442 useraddr += sizeof(coalesce);
2448 for_each_set_bit(i, queue_mask, bit) {
2449 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2458 static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2460 struct ethtool_per_queue_op per_queue_opt;
2462 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2465 switch (per_queue_opt.sub_command) {
2466 case ETHTOOL_GCOALESCE:
2467 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2468 case ETHTOOL_SCOALESCE:
2469 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
2475 static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2478 case ETHTOOL_PHY_DOWNSHIFT:
2479 if (tuna->len != sizeof(u8) ||
2480 tuna->type_id != ETHTOOL_TUNABLE_U8)
2490 static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2493 struct ethtool_tunable tuna;
2494 struct phy_device *phydev = dev->phydev;
2497 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2500 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2502 ret = ethtool_phy_tunable_valid(&tuna);
2505 data = kmalloc(tuna.len, GFP_USER);
2508 mutex_lock(&phydev->lock);
2509 ret = phydev->drv->get_tunable(phydev, &tuna, data);
2510 mutex_unlock(&phydev->lock);
2513 useraddr += sizeof(tuna);
2515 if (copy_to_user(useraddr, data, tuna.len))
2524 static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2527 struct ethtool_tunable tuna;
2528 struct phy_device *phydev = dev->phydev;
2531 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2533 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2535 ret = ethtool_phy_tunable_valid(&tuna);
2538 useraddr += sizeof(tuna);
2539 data = memdup_user(useraddr, tuna.len);
2541 return PTR_ERR(data);
2542 mutex_lock(&phydev->lock);
2543 ret = phydev->drv->set_tunable(phydev, &tuna, data);
2544 mutex_unlock(&phydev->lock);
2550 static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2552 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
2555 if (!dev->ethtool_ops->get_fecparam)
2558 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2562 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2567 static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2569 struct ethtool_fecparam fecparam;
2571 if (!dev->ethtool_ops->set_fecparam)
2574 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2577 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2580 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2582 int dev_ethtool(struct net *net, struct ifreq *ifr)
2584 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
2585 void __user *useraddr = ifr->ifr_data;
2586 u32 ethcmd, sub_cmd;
2588 netdev_features_t old_features;
2590 if (!dev || !netif_device_present(dev))
2593 if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd)))
2596 if (ethcmd == ETHTOOL_PERQUEUE) {
2597 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2602 /* Allow some commands to be done by anyone */
2605 case ETHTOOL_GDRVINFO:
2606 case ETHTOOL_GMSGLVL:
2608 case ETHTOOL_GCOALESCE:
2609 case ETHTOOL_GRINGPARAM:
2610 case ETHTOOL_GPAUSEPARAM:
2611 case ETHTOOL_GRXCSUM:
2612 case ETHTOOL_GTXCSUM:
2614 case ETHTOOL_GSSET_INFO:
2615 case ETHTOOL_GSTRINGS:
2616 case ETHTOOL_GSTATS:
2617 case ETHTOOL_GPHYSTATS:
2619 case ETHTOOL_GPERMADDR:
2622 case ETHTOOL_GFLAGS:
2623 case ETHTOOL_GPFLAGS:
2625 case ETHTOOL_GRXRINGS:
2626 case ETHTOOL_GRXCLSRLCNT:
2627 case ETHTOOL_GRXCLSRULE:
2628 case ETHTOOL_GRXCLSRLALL:
2629 case ETHTOOL_GRXFHINDIR:
2631 case ETHTOOL_GFEATURES:
2632 case ETHTOOL_GCHANNELS:
2633 case ETHTOOL_GET_TS_INFO:
2635 case ETHTOOL_GTUNABLE:
2636 case ETHTOOL_PHY_GTUNABLE:
2637 case ETHTOOL_GLINKSETTINGS:
2638 case ETHTOOL_GFECPARAM:
2641 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
2645 if (dev->ethtool_ops->begin) {
2646 rc = dev->ethtool_ops->begin(dev);
2650 old_features = dev->features;
2654 rc = ethtool_get_settings(dev, useraddr);
2657 rc = ethtool_set_settings(dev, useraddr);
2659 case ETHTOOL_GDRVINFO:
2660 rc = ethtool_get_drvinfo(dev, useraddr);
2663 rc = ethtool_get_regs(dev, useraddr);
2666 rc = ethtool_get_wol(dev, useraddr);
2669 rc = ethtool_set_wol(dev, useraddr);
2671 case ETHTOOL_GMSGLVL:
2672 rc = ethtool_get_value(dev, useraddr, ethcmd,
2673 dev->ethtool_ops->get_msglevel);
2675 case ETHTOOL_SMSGLVL:
2676 rc = ethtool_set_value_void(dev, useraddr,
2677 dev->ethtool_ops->set_msglevel);
2680 rc = ethtool_get_eee(dev, useraddr);
2683 rc = ethtool_set_eee(dev, useraddr);
2685 case ETHTOOL_NWAY_RST:
2686 rc = ethtool_nway_reset(dev);
2689 rc = ethtool_get_link(dev, useraddr);
2691 case ETHTOOL_GEEPROM:
2692 rc = ethtool_get_eeprom(dev, useraddr);
2694 case ETHTOOL_SEEPROM:
2695 rc = ethtool_set_eeprom(dev, useraddr);
2697 case ETHTOOL_GCOALESCE:
2698 rc = ethtool_get_coalesce(dev, useraddr);
2700 case ETHTOOL_SCOALESCE:
2701 rc = ethtool_set_coalesce(dev, useraddr);
2703 case ETHTOOL_GRINGPARAM:
2704 rc = ethtool_get_ringparam(dev, useraddr);
2706 case ETHTOOL_SRINGPARAM:
2707 rc = ethtool_set_ringparam(dev, useraddr);
2709 case ETHTOOL_GPAUSEPARAM:
2710 rc = ethtool_get_pauseparam(dev, useraddr);
2712 case ETHTOOL_SPAUSEPARAM:
2713 rc = ethtool_set_pauseparam(dev, useraddr);
2716 rc = ethtool_self_test(dev, useraddr);
2718 case ETHTOOL_GSTRINGS:
2719 rc = ethtool_get_strings(dev, useraddr);
2721 case ETHTOOL_PHYS_ID:
2722 rc = ethtool_phys_id(dev, useraddr);
2724 case ETHTOOL_GSTATS:
2725 rc = ethtool_get_stats(dev, useraddr);
2727 case ETHTOOL_GPERMADDR:
2728 rc = ethtool_get_perm_addr(dev, useraddr);
2730 case ETHTOOL_GFLAGS:
2731 rc = ethtool_get_value(dev, useraddr, ethcmd,
2732 __ethtool_get_flags);
2734 case ETHTOOL_SFLAGS:
2735 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
2737 case ETHTOOL_GPFLAGS:
2738 rc = ethtool_get_value(dev, useraddr, ethcmd,
2739 dev->ethtool_ops->get_priv_flags);
2741 case ETHTOOL_SPFLAGS:
2742 rc = ethtool_set_value(dev, useraddr,
2743 dev->ethtool_ops->set_priv_flags);
2746 case ETHTOOL_GRXRINGS:
2747 case ETHTOOL_GRXCLSRLCNT:
2748 case ETHTOOL_GRXCLSRULE:
2749 case ETHTOOL_GRXCLSRLALL:
2750 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
2753 case ETHTOOL_SRXCLSRLDEL:
2754 case ETHTOOL_SRXCLSRLINS:
2755 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
2757 case ETHTOOL_FLASHDEV:
2758 rc = ethtool_flash_device(dev, useraddr);
2761 rc = ethtool_reset(dev, useraddr);
2763 case ETHTOOL_GSSET_INFO:
2764 rc = ethtool_get_sset_info(dev, useraddr);
2766 case ETHTOOL_GRXFHINDIR:
2767 rc = ethtool_get_rxfh_indir(dev, useraddr);
2769 case ETHTOOL_SRXFHINDIR:
2770 rc = ethtool_set_rxfh_indir(dev, useraddr);
2773 rc = ethtool_get_rxfh(dev, useraddr);
2776 rc = ethtool_set_rxfh(dev, useraddr);
2778 case ETHTOOL_GFEATURES:
2779 rc = ethtool_get_features(dev, useraddr);
2781 case ETHTOOL_SFEATURES:
2782 rc = ethtool_set_features(dev, useraddr);
2784 case ETHTOOL_GTXCSUM:
2785 case ETHTOOL_GRXCSUM:
2790 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2792 case ETHTOOL_STXCSUM:
2793 case ETHTOOL_SRXCSUM:
2798 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2800 case ETHTOOL_GCHANNELS:
2801 rc = ethtool_get_channels(dev, useraddr);
2803 case ETHTOOL_SCHANNELS:
2804 rc = ethtool_set_channels(dev, useraddr);
2806 case ETHTOOL_SET_DUMP:
2807 rc = ethtool_set_dump(dev, useraddr);
2809 case ETHTOOL_GET_DUMP_FLAG:
2810 rc = ethtool_get_dump_flag(dev, useraddr);
2812 case ETHTOOL_GET_DUMP_DATA:
2813 rc = ethtool_get_dump_data(dev, useraddr);
2815 case ETHTOOL_GET_TS_INFO:
2816 rc = ethtool_get_ts_info(dev, useraddr);
2818 case ETHTOOL_GMODULEINFO:
2819 rc = ethtool_get_module_info(dev, useraddr);
2821 case ETHTOOL_GMODULEEEPROM:
2822 rc = ethtool_get_module_eeprom(dev, useraddr);
2824 case ETHTOOL_GTUNABLE:
2825 rc = ethtool_get_tunable(dev, useraddr);
2827 case ETHTOOL_STUNABLE:
2828 rc = ethtool_set_tunable(dev, useraddr);
2830 case ETHTOOL_GPHYSTATS:
2831 rc = ethtool_get_phy_stats(dev, useraddr);
2833 case ETHTOOL_PERQUEUE:
2834 rc = ethtool_set_per_queue(dev, useraddr);
2836 case ETHTOOL_GLINKSETTINGS:
2837 rc = ethtool_get_link_ksettings(dev, useraddr);
2839 case ETHTOOL_SLINKSETTINGS:
2840 rc = ethtool_set_link_ksettings(dev, useraddr);
2842 case ETHTOOL_PHY_GTUNABLE:
2843 rc = get_phy_tunable(dev, useraddr);
2845 case ETHTOOL_PHY_STUNABLE:
2846 rc = set_phy_tunable(dev, useraddr);
2848 case ETHTOOL_GFECPARAM:
2849 rc = ethtool_get_fecparam(dev, useraddr);
2851 case ETHTOOL_SFECPARAM:
2852 rc = ethtool_set_fecparam(dev, useraddr);
2858 if (dev->ethtool_ops->complete)
2859 dev->ethtool_ops->complete(dev);
2861 if (old_features != dev->features)
2862 netdev_features_change(dev);