2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <linux/net_tstamp.h>
21 #include <linux/phy.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/slab.h>
26 #include <linux/rtnetlink.h>
27 #include <linux/sched.h>
28 #include <linux/net.h>
31 * Some useful ethtool_ops methods that're device independent.
32 * If we find that all drivers want to do the same thing here,
33 * we can turn these into dev_() function calls.
36 u32
ethtool_op_get_link(struct net_device
*dev
)
38 return netif_carrier_ok(dev
) ? 1 : 0;
40 EXPORT_SYMBOL(ethtool_op_get_link
);
42 int ethtool_op_get_ts_info(struct net_device
*dev
, struct ethtool_ts_info
*info
)
44 info
->so_timestamping
=
45 SOF_TIMESTAMPING_TX_SOFTWARE
|
46 SOF_TIMESTAMPING_RX_SOFTWARE
|
47 SOF_TIMESTAMPING_SOFTWARE
;
51 EXPORT_SYMBOL(ethtool_op_get_ts_info
);
53 /* Handlers for each ethtool command */
55 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
57 static const char netdev_features_strings
[NETDEV_FEATURE_COUNT
][ETH_GSTRING_LEN
] = {
58 [NETIF_F_SG_BIT
] = "tx-scatter-gather",
59 [NETIF_F_IP_CSUM_BIT
] = "tx-checksum-ipv4",
60 [NETIF_F_HW_CSUM_BIT
] = "tx-checksum-ip-generic",
61 [NETIF_F_IPV6_CSUM_BIT
] = "tx-checksum-ipv6",
62 [NETIF_F_HIGHDMA_BIT
] = "highdma",
63 [NETIF_F_FRAGLIST_BIT
] = "tx-scatter-gather-fraglist",
64 [NETIF_F_HW_VLAN_CTAG_TX_BIT
] = "tx-vlan-hw-insert",
66 [NETIF_F_HW_VLAN_CTAG_RX_BIT
] = "rx-vlan-hw-parse",
67 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT
] = "rx-vlan-filter",
68 [NETIF_F_HW_VLAN_STAG_TX_BIT
] = "tx-vlan-stag-hw-insert",
69 [NETIF_F_HW_VLAN_STAG_RX_BIT
] = "rx-vlan-stag-hw-parse",
70 [NETIF_F_HW_VLAN_STAG_FILTER_BIT
] = "rx-vlan-stag-filter",
71 [NETIF_F_VLAN_CHALLENGED_BIT
] = "vlan-challenged",
72 [NETIF_F_GSO_BIT
] = "tx-generic-segmentation",
73 [NETIF_F_LLTX_BIT
] = "tx-lockless",
74 [NETIF_F_NETNS_LOCAL_BIT
] = "netns-local",
75 [NETIF_F_GRO_BIT
] = "rx-gro",
76 [NETIF_F_LRO_BIT
] = "rx-lro",
78 [NETIF_F_TSO_BIT
] = "tx-tcp-segmentation",
79 [NETIF_F_UFO_BIT
] = "tx-udp-fragmentation",
80 [NETIF_F_GSO_ROBUST_BIT
] = "tx-gso-robust",
81 [NETIF_F_TSO_ECN_BIT
] = "tx-tcp-ecn-segmentation",
82 [NETIF_F_TSO6_BIT
] = "tx-tcp6-segmentation",
83 [NETIF_F_FSO_BIT
] = "tx-fcoe-segmentation",
84 [NETIF_F_GSO_GRE_BIT
] = "tx-gre-segmentation",
85 [NETIF_F_GSO_IPIP_BIT
] = "tx-ipip-segmentation",
86 [NETIF_F_GSO_SIT_BIT
] = "tx-sit-segmentation",
87 [NETIF_F_GSO_UDP_TUNNEL_BIT
] = "tx-udp_tnl-segmentation",
89 [NETIF_F_FCOE_CRC_BIT
] = "tx-checksum-fcoe-crc",
90 [NETIF_F_SCTP_CRC_BIT
] = "tx-checksum-sctp",
91 [NETIF_F_FCOE_MTU_BIT
] = "fcoe-mtu",
92 [NETIF_F_NTUPLE_BIT
] = "rx-ntuple-filter",
93 [NETIF_F_RXHASH_BIT
] = "rx-hashing",
94 [NETIF_F_RXCSUM_BIT
] = "rx-checksum",
95 [NETIF_F_NOCACHE_COPY_BIT
] = "tx-nocache-copy",
96 [NETIF_F_LOOPBACK_BIT
] = "loopback",
97 [NETIF_F_RXFCS_BIT
] = "rx-fcs",
98 [NETIF_F_RXALL_BIT
] = "rx-all",
99 [NETIF_F_HW_L2FW_DOFFLOAD_BIT
] = "l2-fwd-offload",
100 [NETIF_F_BUSY_POLL_BIT
] = "busy-poll",
101 [NETIF_F_HW_TC_BIT
] = "hw-tc-offload",
105 rss_hash_func_strings
[ETH_RSS_HASH_FUNCS_COUNT
][ETH_GSTRING_LEN
] = {
106 [ETH_RSS_HASH_TOP_BIT
] = "toeplitz",
107 [ETH_RSS_HASH_XOR_BIT
] = "xor",
111 tunable_strings
[__ETHTOOL_TUNABLE_COUNT
][ETH_GSTRING_LEN
] = {
112 [ETHTOOL_ID_UNSPEC
] = "Unspec",
113 [ETHTOOL_RX_COPYBREAK
] = "rx-copybreak",
114 [ETHTOOL_TX_COPYBREAK
] = "tx-copybreak",
117 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
119 struct ethtool_gfeatures cmd
= {
120 .cmd
= ETHTOOL_GFEATURES
,
121 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
123 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
124 u32 __user
*sizeaddr
;
128 /* in case feature bits run out again */
129 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
131 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
132 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
133 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
134 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
135 features
[i
].never_changed
=
136 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
139 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
140 if (get_user(copy_size
, sizeaddr
))
143 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
144 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
146 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
148 useraddr
+= sizeof(cmd
);
149 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
155 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
157 struct ethtool_sfeatures cmd
;
158 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
159 netdev_features_t wanted
= 0, valid
= 0;
162 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
164 useraddr
+= sizeof(cmd
);
166 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
169 if (copy_from_user(features
, useraddr
, sizeof(features
)))
172 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
173 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
174 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
177 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
180 if (valid
& ~dev
->hw_features
) {
181 valid
&= dev
->hw_features
;
182 ret
|= ETHTOOL_F_UNSUPPORTED
;
185 dev
->wanted_features
&= ~valid
;
186 dev
->wanted_features
|= wanted
& valid
;
187 __netdev_update_features(dev
);
189 if ((dev
->wanted_features
^ dev
->features
) & valid
)
190 ret
|= ETHTOOL_F_WISH
;
195 static int phy_get_sset_count(struct phy_device
*phydev
)
199 if (phydev
->drv
->get_sset_count
&&
200 phydev
->drv
->get_strings
&&
201 phydev
->drv
->get_stats
) {
202 mutex_lock(&phydev
->lock
);
203 ret
= phydev
->drv
->get_sset_count(phydev
);
204 mutex_unlock(&phydev
->lock
);
212 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
214 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
216 if (sset
== ETH_SS_FEATURES
)
217 return ARRAY_SIZE(netdev_features_strings
);
219 if (sset
== ETH_SS_RSS_HASH_FUNCS
)
220 return ARRAY_SIZE(rss_hash_func_strings
);
222 if (sset
== ETH_SS_TUNABLES
)
223 return ARRAY_SIZE(tunable_strings
);
225 if (sset
== ETH_SS_PHY_STATS
) {
227 return phy_get_sset_count(dev
->phydev
);
232 if (ops
->get_sset_count
&& ops
->get_strings
)
233 return ops
->get_sset_count(dev
, sset
);
238 static void __ethtool_get_strings(struct net_device
*dev
,
239 u32 stringset
, u8
*data
)
241 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
243 if (stringset
== ETH_SS_FEATURES
)
244 memcpy(data
, netdev_features_strings
,
245 sizeof(netdev_features_strings
));
246 else if (stringset
== ETH_SS_RSS_HASH_FUNCS
)
247 memcpy(data
, rss_hash_func_strings
,
248 sizeof(rss_hash_func_strings
));
249 else if (stringset
== ETH_SS_TUNABLES
)
250 memcpy(data
, tunable_strings
, sizeof(tunable_strings
));
251 else if (stringset
== ETH_SS_PHY_STATS
) {
252 struct phy_device
*phydev
= dev
->phydev
;
255 mutex_lock(&phydev
->lock
);
256 phydev
->drv
->get_strings(phydev
, data
);
257 mutex_unlock(&phydev
->lock
);
262 /* ops->get_strings is valid because checked earlier */
263 ops
->get_strings(dev
, stringset
, data
);
266 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
268 /* feature masks of legacy discrete ethtool ops */
271 case ETHTOOL_GTXCSUM
:
272 case ETHTOOL_STXCSUM
:
273 return NETIF_F_CSUM_MASK
| NETIF_F_SCTP_CRC
;
274 case ETHTOOL_GRXCSUM
:
275 case ETHTOOL_SRXCSUM
:
276 return NETIF_F_RXCSUM
;
282 return NETIF_F_ALL_TSO
;
297 static int ethtool_get_one_feature(struct net_device
*dev
,
298 char __user
*useraddr
, u32 ethcmd
)
300 netdev_features_t mask
= ethtool_get_feature_mask(ethcmd
);
301 struct ethtool_value edata
= {
303 .data
= !!(dev
->features
& mask
),
306 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
311 static int ethtool_set_one_feature(struct net_device
*dev
,
312 void __user
*useraddr
, u32 ethcmd
)
314 struct ethtool_value edata
;
315 netdev_features_t mask
;
317 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
320 mask
= ethtool_get_feature_mask(ethcmd
);
321 mask
&= dev
->hw_features
;
326 dev
->wanted_features
|= mask
;
328 dev
->wanted_features
&= ~mask
;
330 __netdev_update_features(dev
);
335 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
336 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
337 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
338 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
341 static u32
__ethtool_get_flags(struct net_device
*dev
)
345 if (dev
->features
& NETIF_F_LRO
)
346 flags
|= ETH_FLAG_LRO
;
347 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_RX
)
348 flags
|= ETH_FLAG_RXVLAN
;
349 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_TX
)
350 flags
|= ETH_FLAG_TXVLAN
;
351 if (dev
->features
& NETIF_F_NTUPLE
)
352 flags
|= ETH_FLAG_NTUPLE
;
353 if (dev
->features
& NETIF_F_RXHASH
)
354 flags
|= ETH_FLAG_RXHASH
;
359 static int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
361 netdev_features_t features
= 0, changed
;
363 if (data
& ~ETH_ALL_FLAGS
)
366 if (data
& ETH_FLAG_LRO
)
367 features
|= NETIF_F_LRO
;
368 if (data
& ETH_FLAG_RXVLAN
)
369 features
|= NETIF_F_HW_VLAN_CTAG_RX
;
370 if (data
& ETH_FLAG_TXVLAN
)
371 features
|= NETIF_F_HW_VLAN_CTAG_TX
;
372 if (data
& ETH_FLAG_NTUPLE
)
373 features
|= NETIF_F_NTUPLE
;
374 if (data
& ETH_FLAG_RXHASH
)
375 features
|= NETIF_F_RXHASH
;
377 /* allow changing only bits set in hw_features */
378 changed
= (features
^ dev
->features
) & ETH_ALL_FEATURES
;
379 if (changed
& ~dev
->hw_features
)
380 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
382 dev
->wanted_features
=
383 (dev
->wanted_features
& ~changed
) | (features
& changed
);
385 __netdev_update_features(dev
);
390 int __ethtool_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
394 if (!dev
->ethtool_ops
->get_settings
)
397 memset(cmd
, 0, sizeof(struct ethtool_cmd
));
398 cmd
->cmd
= ETHTOOL_GSET
;
399 return dev
->ethtool_ops
->get_settings(dev
, cmd
);
401 EXPORT_SYMBOL(__ethtool_get_settings
);
403 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
406 struct ethtool_cmd cmd
;
408 err
= __ethtool_get_settings(dev
, &cmd
);
412 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
417 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
419 struct ethtool_cmd cmd
;
421 if (!dev
->ethtool_ops
->set_settings
)
424 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
427 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
430 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
431 void __user
*useraddr
)
433 struct ethtool_drvinfo info
;
434 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
436 memset(&info
, 0, sizeof(info
));
437 info
.cmd
= ETHTOOL_GDRVINFO
;
438 if (ops
->get_drvinfo
) {
439 ops
->get_drvinfo(dev
, &info
);
440 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
441 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
442 sizeof(info
.bus_info
));
443 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
444 sizeof(info
.driver
));
450 * this method of obtaining string set info is deprecated;
451 * Use ETHTOOL_GSSET_INFO instead.
453 if (ops
->get_sset_count
) {
456 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
458 info
.testinfo_len
= rc
;
459 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
462 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
464 info
.n_priv_flags
= rc
;
466 if (ops
->get_regs_len
)
467 info
.regdump_len
= ops
->get_regs_len(dev
);
468 if (ops
->get_eeprom_len
)
469 info
.eedump_len
= ops
->get_eeprom_len(dev
);
471 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
476 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
477 void __user
*useraddr
)
479 struct ethtool_sset_info info
;
481 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
482 u32
*info_buf
= NULL
;
484 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
487 /* store copy of mask, because we zero struct later on */
488 sset_mask
= info
.sset_mask
;
492 /* calculate size of return buffer */
493 n_bits
= hweight64(sset_mask
);
495 memset(&info
, 0, sizeof(info
));
496 info
.cmd
= ETHTOOL_GSSET_INFO
;
498 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
503 * fill return buffer based on input bitmask and successful
504 * get_sset_count return
506 for (i
= 0; i
< 64; i
++) {
507 if (!(sset_mask
& (1ULL << i
)))
510 rc
= __ethtool_get_sset_count(dev
, i
);
512 info
.sset_mask
|= (1ULL << i
);
513 info_buf
[idx
++] = rc
;
518 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
521 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
522 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
532 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
533 u32 cmd
, void __user
*useraddr
)
535 struct ethtool_rxnfc info
;
536 size_t info_size
= sizeof(info
);
539 if (!dev
->ethtool_ops
->set_rxnfc
)
542 /* struct ethtool_rxnfc was originally defined for
543 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
544 * members. User-space might still be using that
546 if (cmd
== ETHTOOL_SRXFH
)
547 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
550 if (copy_from_user(&info
, useraddr
, info_size
))
553 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
557 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
558 copy_to_user(useraddr
, &info
, info_size
))
564 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
565 u32 cmd
, void __user
*useraddr
)
567 struct ethtool_rxnfc info
;
568 size_t info_size
= sizeof(info
);
569 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
571 void *rule_buf
= NULL
;
576 /* struct ethtool_rxnfc was originally defined for
577 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
578 * members. User-space might still be using that
580 if (cmd
== ETHTOOL_GRXFH
)
581 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
584 if (copy_from_user(&info
, useraddr
, info_size
))
587 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
588 if (info
.rule_cnt
> 0) {
589 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
590 rule_buf
= kzalloc(info
.rule_cnt
* sizeof(u32
),
597 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
602 if (copy_to_user(useraddr
, &info
, info_size
))
606 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
607 if (copy_to_user(useraddr
, rule_buf
,
608 info
.rule_cnt
* sizeof(u32
)))
619 static int ethtool_copy_validate_indir(u32
*indir
, void __user
*useraddr
,
620 struct ethtool_rxnfc
*rx_rings
,
625 if (copy_from_user(indir
, useraddr
, size
* sizeof(indir
[0])))
628 /* Validate ring indices */
629 for (i
= 0; i
< size
; i
++)
630 if (indir
[i
] >= rx_rings
->data
)
636 u8 netdev_rss_key
[NETDEV_RSS_KEY_LEN
] __read_mostly
;
638 void netdev_rss_key_fill(void *buffer
, size_t len
)
640 BUG_ON(len
> sizeof(netdev_rss_key
));
641 net_get_random_once(netdev_rss_key
, sizeof(netdev_rss_key
));
642 memcpy(buffer
, netdev_rss_key
, len
);
644 EXPORT_SYMBOL(netdev_rss_key_fill
);
646 static int ethtool_get_max_rxfh_channel(struct net_device
*dev
, u32
*max
)
648 u32 dev_size
, current_max
= 0;
652 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
653 !dev
->ethtool_ops
->get_rxfh
)
655 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
659 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
663 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
668 current_max
= max(current_max
, indir
[dev_size
]);
677 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
678 void __user
*useraddr
)
680 u32 user_size
, dev_size
;
684 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
685 !dev
->ethtool_ops
->get_rxfh
)
687 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
691 if (copy_from_user(&user_size
,
692 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
696 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
697 &dev_size
, sizeof(dev_size
)))
700 /* If the user buffer size is 0, this is just a query for the
701 * device table size. Otherwise, if it's smaller than the
702 * device table size it's an error.
704 if (user_size
< dev_size
)
705 return user_size
== 0 ? 0 : -EINVAL
;
707 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
711 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
715 if (copy_to_user(useraddr
+
716 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
717 indir
, dev_size
* sizeof(indir
[0])))
725 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
726 void __user
*useraddr
)
728 struct ethtool_rxnfc rx_rings
;
729 u32 user_size
, dev_size
, i
;
731 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
733 u32 ringidx_offset
= offsetof(struct ethtool_rxfh_indir
, ring_index
[0]);
735 if (!ops
->get_rxfh_indir_size
|| !ops
->set_rxfh
||
739 dev_size
= ops
->get_rxfh_indir_size(dev
);
743 if (copy_from_user(&user_size
,
744 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
748 if (user_size
!= 0 && user_size
!= dev_size
)
751 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
755 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
756 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
760 if (user_size
== 0) {
761 for (i
= 0; i
< dev_size
; i
++)
762 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
764 ret
= ethtool_copy_validate_indir(indir
,
765 useraddr
+ ringidx_offset
,
772 ret
= ops
->set_rxfh(dev
, indir
, NULL
, ETH_RSS_HASH_NO_CHANGE
);
776 /* indicate whether rxfh was set to default */
778 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
780 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
787 static noinline_for_stack
int ethtool_get_rxfh(struct net_device
*dev
,
788 void __user
*useraddr
)
791 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
792 u32 user_indir_size
, user_key_size
;
793 u32 dev_indir_size
= 0, dev_key_size
= 0;
794 struct ethtool_rxfh rxfh
;
805 if (ops
->get_rxfh_indir_size
)
806 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
807 if (ops
->get_rxfh_key_size
)
808 dev_key_size
= ops
->get_rxfh_key_size(dev
);
810 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
812 user_indir_size
= rxfh
.indir_size
;
813 user_key_size
= rxfh
.key_size
;
815 /* Check that reserved fields are 0 for now */
816 if (rxfh
.rss_context
|| rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] ||
817 rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
820 rxfh
.indir_size
= dev_indir_size
;
821 rxfh
.key_size
= dev_key_size
;
822 if (copy_to_user(useraddr
, &rxfh
, sizeof(rxfh
)))
825 if ((user_indir_size
&& (user_indir_size
!= dev_indir_size
)) ||
826 (user_key_size
&& (user_key_size
!= dev_key_size
)))
829 indir_bytes
= user_indir_size
* sizeof(indir
[0]);
830 total_size
= indir_bytes
+ user_key_size
;
831 rss_config
= kzalloc(total_size
, GFP_USER
);
836 indir
= (u32
*)rss_config
;
839 hkey
= rss_config
+ indir_bytes
;
841 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, hkey
, &dev_hfunc
);
845 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, hfunc
),
846 &dev_hfunc
, sizeof(rxfh
.hfunc
))) {
848 } else if (copy_to_user(useraddr
+
849 offsetof(struct ethtool_rxfh
, rss_config
[0]),
850 rss_config
, total_size
)) {
859 static noinline_for_stack
int ethtool_set_rxfh(struct net_device
*dev
,
860 void __user
*useraddr
)
863 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
864 struct ethtool_rxnfc rx_rings
;
865 struct ethtool_rxfh rxfh
;
866 u32 dev_indir_size
= 0, dev_key_size
= 0, i
;
867 u32
*indir
= NULL
, indir_bytes
= 0;
870 u32 rss_cfg_offset
= offsetof(struct ethtool_rxfh
, rss_config
[0]);
872 if (!ops
->get_rxnfc
|| !ops
->set_rxfh
)
875 if (ops
->get_rxfh_indir_size
)
876 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
877 if (ops
->get_rxfh_key_size
)
878 dev_key_size
= ops
->get_rxfh_key_size(dev
);
880 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
883 /* Check that reserved fields are 0 for now */
884 if (rxfh
.rss_context
|| rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] ||
885 rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
888 /* If either indir, hash key or function is valid, proceed further.
889 * Must request at least one change: indir size, hash key or function.
891 if ((rxfh
.indir_size
&&
892 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
&&
893 rxfh
.indir_size
!= dev_indir_size
) ||
894 (rxfh
.key_size
&& (rxfh
.key_size
!= dev_key_size
)) ||
895 (rxfh
.indir_size
== ETH_RXFH_INDIR_NO_CHANGE
&&
896 rxfh
.key_size
== 0 && rxfh
.hfunc
== ETH_RSS_HASH_NO_CHANGE
))
899 if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
900 indir_bytes
= dev_indir_size
* sizeof(indir
[0]);
902 rss_config
= kzalloc(indir_bytes
+ rxfh
.key_size
, GFP_USER
);
906 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
907 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
911 /* rxfh.indir_size == 0 means reset the indir table to default.
912 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
914 if (rxfh
.indir_size
&&
915 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
) {
916 indir
= (u32
*)rss_config
;
917 ret
= ethtool_copy_validate_indir(indir
,
918 useraddr
+ rss_cfg_offset
,
923 } else if (rxfh
.indir_size
== 0) {
924 indir
= (u32
*)rss_config
;
925 for (i
= 0; i
< dev_indir_size
; i
++)
926 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
930 hkey
= rss_config
+ indir_bytes
;
931 if (copy_from_user(hkey
,
932 useraddr
+ rss_cfg_offset
+ indir_bytes
,
939 ret
= ops
->set_rxfh(dev
, indir
, hkey
, rxfh
.hfunc
);
943 /* indicate whether rxfh was set to default */
944 if (rxfh
.indir_size
== 0)
945 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
946 else if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
947 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
954 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
956 struct ethtool_regs regs
;
957 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
961 if (!ops
->get_regs
|| !ops
->get_regs_len
)
964 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
967 reglen
= ops
->get_regs_len(dev
);
968 if (regs
.len
> reglen
)
971 regbuf
= vzalloc(reglen
);
972 if (reglen
&& !regbuf
)
975 ops
->get_regs(dev
, ®s
, regbuf
);
978 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
980 useraddr
+= offsetof(struct ethtool_regs
, data
);
981 if (regbuf
&& copy_to_user(useraddr
, regbuf
, regs
.len
))
990 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
992 struct ethtool_value reset
;
995 if (!dev
->ethtool_ops
->reset
)
998 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
1001 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
1005 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
1010 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
1012 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
1014 if (!dev
->ethtool_ops
->get_wol
)
1017 dev
->ethtool_ops
->get_wol(dev
, &wol
);
1019 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
1024 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
1026 struct ethtool_wolinfo wol
;
1028 if (!dev
->ethtool_ops
->set_wol
)
1031 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
1034 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
1037 static int ethtool_get_eee(struct net_device
*dev
, char __user
*useraddr
)
1039 struct ethtool_eee edata
;
1042 if (!dev
->ethtool_ops
->get_eee
)
1045 memset(&edata
, 0, sizeof(struct ethtool_eee
));
1046 edata
.cmd
= ETHTOOL_GEEE
;
1047 rc
= dev
->ethtool_ops
->get_eee(dev
, &edata
);
1052 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1058 static int ethtool_set_eee(struct net_device
*dev
, char __user
*useraddr
)
1060 struct ethtool_eee edata
;
1062 if (!dev
->ethtool_ops
->set_eee
)
1065 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1068 return dev
->ethtool_ops
->set_eee(dev
, &edata
);
1071 static int ethtool_nway_reset(struct net_device
*dev
)
1073 if (!dev
->ethtool_ops
->nway_reset
)
1076 return dev
->ethtool_ops
->nway_reset(dev
);
1079 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
1081 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
1083 if (!dev
->ethtool_ops
->get_link
)
1086 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
1088 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1093 static int ethtool_get_any_eeprom(struct net_device
*dev
, void __user
*useraddr
,
1094 int (*getter
)(struct net_device
*,
1095 struct ethtool_eeprom
*, u8
*),
1098 struct ethtool_eeprom eeprom
;
1099 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1100 u32 bytes_remaining
;
1104 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1107 /* Check for wrap and zero */
1108 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1111 /* Check for exceeding total eeprom len */
1112 if (eeprom
.offset
+ eeprom
.len
> total_len
)
1115 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1119 bytes_remaining
= eeprom
.len
;
1120 while (bytes_remaining
> 0) {
1121 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1123 ret
= getter(dev
, &eeprom
, data
);
1126 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
1130 userbuf
+= eeprom
.len
;
1131 eeprom
.offset
+= eeprom
.len
;
1132 bytes_remaining
-= eeprom
.len
;
1135 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
1136 eeprom
.offset
-= eeprom
.len
;
1137 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
1144 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1146 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1148 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
||
1149 !ops
->get_eeprom_len(dev
))
1152 return ethtool_get_any_eeprom(dev
, useraddr
, ops
->get_eeprom
,
1153 ops
->get_eeprom_len(dev
));
1156 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1158 struct ethtool_eeprom eeprom
;
1159 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1160 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1161 u32 bytes_remaining
;
1165 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
||
1166 !ops
->get_eeprom_len(dev
))
1169 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1172 /* Check for wrap and zero */
1173 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1176 /* Check for exceeding total eeprom len */
1177 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
1180 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1184 bytes_remaining
= eeprom
.len
;
1185 while (bytes_remaining
> 0) {
1186 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1188 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
1192 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
1195 userbuf
+= eeprom
.len
;
1196 eeprom
.offset
+= eeprom
.len
;
1197 bytes_remaining
-= eeprom
.len
;
1204 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
1205 void __user
*useraddr
)
1207 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
1209 if (!dev
->ethtool_ops
->get_coalesce
)
1212 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
1214 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
1219 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
1220 void __user
*useraddr
)
1222 struct ethtool_coalesce coalesce
;
1224 if (!dev
->ethtool_ops
->set_coalesce
)
1227 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
1230 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
1233 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1235 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
1237 if (!dev
->ethtool_ops
->get_ringparam
)
1240 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
1242 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
1247 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1249 struct ethtool_ringparam ringparam
;
1251 if (!dev
->ethtool_ops
->set_ringparam
)
1254 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
1257 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
1260 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
1261 void __user
*useraddr
)
1263 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
1265 if (!dev
->ethtool_ops
->get_channels
)
1268 dev
->ethtool_ops
->get_channels(dev
, &channels
);
1270 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
1275 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
1276 void __user
*useraddr
)
1278 struct ethtool_channels channels
, max
;
1279 u32 max_rx_in_use
= 0;
1281 if (!dev
->ethtool_ops
->set_channels
|| !dev
->ethtool_ops
->get_channels
)
1284 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
1287 dev
->ethtool_ops
->get_channels(dev
, &max
);
1289 /* ensure new counts are within the maximums */
1290 if ((channels
.rx_count
> max
.max_rx
) ||
1291 (channels
.tx_count
> max
.max_tx
) ||
1292 (channels
.combined_count
> max
.max_combined
) ||
1293 (channels
.other_count
> max
.max_other
))
1296 /* ensure the new Rx count fits within the configured Rx flow
1297 * indirection table settings */
1298 if (netif_is_rxfh_configured(dev
) &&
1299 !ethtool_get_max_rxfh_channel(dev
, &max_rx_in_use
) &&
1300 (channels
.combined_count
+ channels
.rx_count
) <= max_rx_in_use
)
1303 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
1306 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1308 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
1310 if (!dev
->ethtool_ops
->get_pauseparam
)
1313 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
1315 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
1320 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1322 struct ethtool_pauseparam pauseparam
;
1324 if (!dev
->ethtool_ops
->set_pauseparam
)
1327 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
1330 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
1333 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1335 struct ethtool_test test
;
1336 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1340 if (!ops
->self_test
|| !ops
->get_sset_count
)
1343 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1346 WARN_ON(test_len
== 0);
1348 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1351 test
.len
= test_len
;
1352 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
1356 ops
->self_test(dev
, &test
, data
);
1359 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1361 useraddr
+= sizeof(test
);
1362 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1371 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1373 struct ethtool_gstrings gstrings
;
1377 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1380 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1386 data
= kcalloc(gstrings
.len
, ETH_GSTRING_LEN
, GFP_USER
);
1390 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1393 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1395 useraddr
+= sizeof(gstrings
);
1396 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1405 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1407 struct ethtool_value id
;
1409 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1412 if (!ops
->set_phys_id
)
1418 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1421 rc
= ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1425 /* Drop the RTNL lock while waiting, but prevent reentry or
1426 * removal of the device.
1433 /* Driver will handle this itself */
1434 schedule_timeout_interruptible(
1435 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1437 /* Driver expects to be called at twice the frequency in rc */
1438 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1440 /* Count down seconds */
1442 /* Count down iterations per second */
1446 rc
= ops
->set_phys_id(dev
,
1447 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1451 schedule_timeout_interruptible(interval
);
1452 } while (!signal_pending(current
) && --i
!= 0);
1453 } while (!signal_pending(current
) &&
1454 (id
.data
== 0 || --id
.data
!= 0));
1461 (void) ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1465 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1467 struct ethtool_stats stats
;
1468 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1472 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1475 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1478 WARN_ON(n_stats
== 0);
1480 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1483 stats
.n_stats
= n_stats
;
1484 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1488 ops
->get_ethtool_stats(dev
, &stats
, data
);
1491 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1493 useraddr
+= sizeof(stats
);
1494 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1503 static int ethtool_get_phy_stats(struct net_device
*dev
, void __user
*useraddr
)
1505 struct ethtool_stats stats
;
1506 struct phy_device
*phydev
= dev
->phydev
;
1513 n_stats
= phy_get_sset_count(phydev
);
1517 WARN_ON(n_stats
== 0);
1519 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1522 stats
.n_stats
= n_stats
;
1523 data
= kmalloc_array(n_stats
, sizeof(u64
), GFP_USER
);
1527 mutex_lock(&phydev
->lock
);
1528 phydev
->drv
->get_stats(phydev
, &stats
, data
);
1529 mutex_unlock(&phydev
->lock
);
1532 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1534 useraddr
+= sizeof(stats
);
1535 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1544 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1546 struct ethtool_perm_addr epaddr
;
1548 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1551 if (epaddr
.size
< dev
->addr_len
)
1553 epaddr
.size
= dev
->addr_len
;
1555 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1557 useraddr
+= sizeof(epaddr
);
1558 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1563 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1564 u32 cmd
, u32 (*actor
)(struct net_device
*))
1566 struct ethtool_value edata
= { .cmd
= cmd
};
1571 edata
.data
= actor(dev
);
1573 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1578 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1579 void (*actor
)(struct net_device
*, u32
))
1581 struct ethtool_value edata
;
1586 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1589 actor(dev
, edata
.data
);
1593 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1594 int (*actor
)(struct net_device
*, u32
))
1596 struct ethtool_value edata
;
1601 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1604 return actor(dev
, edata
.data
);
1607 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
1608 char __user
*useraddr
)
1610 struct ethtool_flash efl
;
1612 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1615 if (!dev
->ethtool_ops
->flash_device
)
1618 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
1620 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1623 static int ethtool_set_dump(struct net_device
*dev
,
1624 void __user
*useraddr
)
1626 struct ethtool_dump dump
;
1628 if (!dev
->ethtool_ops
->set_dump
)
1631 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1634 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
1637 static int ethtool_get_dump_flag(struct net_device
*dev
,
1638 void __user
*useraddr
)
1641 struct ethtool_dump dump
;
1642 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1644 if (!ops
->get_dump_flag
)
1647 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1650 ret
= ops
->get_dump_flag(dev
, &dump
);
1654 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
1659 static int ethtool_get_dump_data(struct net_device
*dev
,
1660 void __user
*useraddr
)
1664 struct ethtool_dump dump
, tmp
;
1665 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1668 if (!ops
->get_dump_data
|| !ops
->get_dump_flag
)
1671 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
1674 memset(&tmp
, 0, sizeof(tmp
));
1675 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
1676 ret
= ops
->get_dump_flag(dev
, &tmp
);
1680 len
= min(tmp
.len
, dump
.len
);
1684 /* Don't ever let the driver think there's more space available
1685 * than it requested with .get_dump_flag().
1689 /* Always allocate enough space to hold the whole thing so that the
1690 * driver does not need to check the length and bother with partial
1693 data
= vzalloc(tmp
.len
);
1696 ret
= ops
->get_dump_data(dev
, &dump
, data
);
1700 /* There are two sane possibilities:
1701 * 1. The driver's .get_dump_data() does not touch dump.len.
1702 * 2. Or it may set dump.len to how much it really writes, which
1703 * should be tmp.len (or len if it can do a partial dump).
1704 * In any case respond to userspace with the actual length of data
1707 WARN_ON(dump
.len
!= len
&& dump
.len
!= tmp
.len
);
1710 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
1714 useraddr
+= offsetof(struct ethtool_dump
, data
);
1715 if (copy_to_user(useraddr
, data
, len
))
1722 static int ethtool_get_ts_info(struct net_device
*dev
, void __user
*useraddr
)
1725 struct ethtool_ts_info info
;
1726 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1727 struct phy_device
*phydev
= dev
->phydev
;
1729 memset(&info
, 0, sizeof(info
));
1730 info
.cmd
= ETHTOOL_GET_TS_INFO
;
1732 if (phydev
&& phydev
->drv
&& phydev
->drv
->ts_info
) {
1733 err
= phydev
->drv
->ts_info(phydev
, &info
);
1734 } else if (ops
->get_ts_info
) {
1735 err
= ops
->get_ts_info(dev
, &info
);
1737 info
.so_timestamping
=
1738 SOF_TIMESTAMPING_RX_SOFTWARE
|
1739 SOF_TIMESTAMPING_SOFTWARE
;
1740 info
.phc_index
= -1;
1746 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
1752 static int __ethtool_get_module_info(struct net_device
*dev
,
1753 struct ethtool_modinfo
*modinfo
)
1755 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1756 struct phy_device
*phydev
= dev
->phydev
;
1758 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_info
)
1759 return phydev
->drv
->module_info(phydev
, modinfo
);
1761 if (ops
->get_module_info
)
1762 return ops
->get_module_info(dev
, modinfo
);
1767 static int ethtool_get_module_info(struct net_device
*dev
,
1768 void __user
*useraddr
)
1771 struct ethtool_modinfo modinfo
;
1773 if (copy_from_user(&modinfo
, useraddr
, sizeof(modinfo
)))
1776 ret
= __ethtool_get_module_info(dev
, &modinfo
);
1780 if (copy_to_user(useraddr
, &modinfo
, sizeof(modinfo
)))
1786 static int __ethtool_get_module_eeprom(struct net_device
*dev
,
1787 struct ethtool_eeprom
*ee
, u8
*data
)
1789 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1790 struct phy_device
*phydev
= dev
->phydev
;
1792 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_eeprom
)
1793 return phydev
->drv
->module_eeprom(phydev
, ee
, data
);
1795 if (ops
->get_module_eeprom
)
1796 return ops
->get_module_eeprom(dev
, ee
, data
);
1801 static int ethtool_get_module_eeprom(struct net_device
*dev
,
1802 void __user
*useraddr
)
1805 struct ethtool_modinfo modinfo
;
1807 ret
= __ethtool_get_module_info(dev
, &modinfo
);
1811 return ethtool_get_any_eeprom(dev
, useraddr
,
1812 __ethtool_get_module_eeprom
,
1813 modinfo
.eeprom_len
);
1816 static int ethtool_tunable_valid(const struct ethtool_tunable
*tuna
)
1819 case ETHTOOL_RX_COPYBREAK
:
1820 case ETHTOOL_TX_COPYBREAK
:
1821 if (tuna
->len
!= sizeof(u32
) ||
1822 tuna
->type_id
!= ETHTOOL_TUNABLE_U32
)
1832 static int ethtool_get_tunable(struct net_device
*dev
, void __user
*useraddr
)
1835 struct ethtool_tunable tuna
;
1836 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1839 if (!ops
->get_tunable
)
1841 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
1843 ret
= ethtool_tunable_valid(&tuna
);
1846 data
= kmalloc(tuna
.len
, GFP_USER
);
1849 ret
= ops
->get_tunable(dev
, &tuna
, data
);
1852 useraddr
+= sizeof(tuna
);
1854 if (copy_to_user(useraddr
, data
, tuna
.len
))
1863 static int ethtool_set_tunable(struct net_device
*dev
, void __user
*useraddr
)
1866 struct ethtool_tunable tuna
;
1867 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1870 if (!ops
->set_tunable
)
1872 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
1874 ret
= ethtool_tunable_valid(&tuna
);
1877 data
= kmalloc(tuna
.len
, GFP_USER
);
1880 useraddr
+= sizeof(tuna
);
1882 if (copy_from_user(data
, useraddr
, tuna
.len
))
1884 ret
= ops
->set_tunable(dev
, &tuna
, data
);
1891 static int ethtool_set_per_queue(struct net_device
*dev
, void __user
*useraddr
)
1893 struct ethtool_per_queue_op per_queue_opt
;
1895 if (copy_from_user(&per_queue_opt
, useraddr
, sizeof(per_queue_opt
)))
1898 switch (per_queue_opt
.sub_command
) {
1905 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
1907 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1909 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1910 void __user
*useraddr
= ifr
->ifr_data
;
1911 u32 ethcmd
, sub_cmd
;
1913 netdev_features_t old_features
;
1915 if (!dev
|| !netif_device_present(dev
))
1918 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
1921 if (ethcmd
== ETHTOOL_PERQUEUE
) {
1922 if (copy_from_user(&sub_cmd
, useraddr
+ sizeof(ethcmd
), sizeof(sub_cmd
)))
1927 /* Allow some commands to be done by anyone */
1930 case ETHTOOL_GDRVINFO
:
1931 case ETHTOOL_GMSGLVL
:
1933 case ETHTOOL_GCOALESCE
:
1934 case ETHTOOL_GRINGPARAM
:
1935 case ETHTOOL_GPAUSEPARAM
:
1936 case ETHTOOL_GRXCSUM
:
1937 case ETHTOOL_GTXCSUM
:
1939 case ETHTOOL_GSSET_INFO
:
1940 case ETHTOOL_GSTRINGS
:
1941 case ETHTOOL_GSTATS
:
1942 case ETHTOOL_GPHYSTATS
:
1944 case ETHTOOL_GPERMADDR
:
1948 case ETHTOOL_GFLAGS
:
1949 case ETHTOOL_GPFLAGS
:
1951 case ETHTOOL_GRXRINGS
:
1952 case ETHTOOL_GRXCLSRLCNT
:
1953 case ETHTOOL_GRXCLSRULE
:
1954 case ETHTOOL_GRXCLSRLALL
:
1955 case ETHTOOL_GRXFHINDIR
:
1957 case ETHTOOL_GFEATURES
:
1958 case ETHTOOL_GCHANNELS
:
1959 case ETHTOOL_GET_TS_INFO
:
1961 case ETHTOOL_GTUNABLE
:
1964 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1968 if (dev
->ethtool_ops
->begin
) {
1969 rc
= dev
->ethtool_ops
->begin(dev
);
1973 old_features
= dev
->features
;
1977 rc
= ethtool_get_settings(dev
, useraddr
);
1980 rc
= ethtool_set_settings(dev
, useraddr
);
1982 case ETHTOOL_GDRVINFO
:
1983 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1986 rc
= ethtool_get_regs(dev
, useraddr
);
1989 rc
= ethtool_get_wol(dev
, useraddr
);
1992 rc
= ethtool_set_wol(dev
, useraddr
);
1994 case ETHTOOL_GMSGLVL
:
1995 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1996 dev
->ethtool_ops
->get_msglevel
);
1998 case ETHTOOL_SMSGLVL
:
1999 rc
= ethtool_set_value_void(dev
, useraddr
,
2000 dev
->ethtool_ops
->set_msglevel
);
2003 rc
= ethtool_get_eee(dev
, useraddr
);
2006 rc
= ethtool_set_eee(dev
, useraddr
);
2008 case ETHTOOL_NWAY_RST
:
2009 rc
= ethtool_nway_reset(dev
);
2012 rc
= ethtool_get_link(dev
, useraddr
);
2014 case ETHTOOL_GEEPROM
:
2015 rc
= ethtool_get_eeprom(dev
, useraddr
);
2017 case ETHTOOL_SEEPROM
:
2018 rc
= ethtool_set_eeprom(dev
, useraddr
);
2020 case ETHTOOL_GCOALESCE
:
2021 rc
= ethtool_get_coalesce(dev
, useraddr
);
2023 case ETHTOOL_SCOALESCE
:
2024 rc
= ethtool_set_coalesce(dev
, useraddr
);
2026 case ETHTOOL_GRINGPARAM
:
2027 rc
= ethtool_get_ringparam(dev
, useraddr
);
2029 case ETHTOOL_SRINGPARAM
:
2030 rc
= ethtool_set_ringparam(dev
, useraddr
);
2032 case ETHTOOL_GPAUSEPARAM
:
2033 rc
= ethtool_get_pauseparam(dev
, useraddr
);
2035 case ETHTOOL_SPAUSEPARAM
:
2036 rc
= ethtool_set_pauseparam(dev
, useraddr
);
2039 rc
= ethtool_self_test(dev
, useraddr
);
2041 case ETHTOOL_GSTRINGS
:
2042 rc
= ethtool_get_strings(dev
, useraddr
);
2044 case ETHTOOL_PHYS_ID
:
2045 rc
= ethtool_phys_id(dev
, useraddr
);
2047 case ETHTOOL_GSTATS
:
2048 rc
= ethtool_get_stats(dev
, useraddr
);
2050 case ETHTOOL_GPERMADDR
:
2051 rc
= ethtool_get_perm_addr(dev
, useraddr
);
2053 case ETHTOOL_GFLAGS
:
2054 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2055 __ethtool_get_flags
);
2057 case ETHTOOL_SFLAGS
:
2058 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
2060 case ETHTOOL_GPFLAGS
:
2061 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2062 dev
->ethtool_ops
->get_priv_flags
);
2064 case ETHTOOL_SPFLAGS
:
2065 rc
= ethtool_set_value(dev
, useraddr
,
2066 dev
->ethtool_ops
->set_priv_flags
);
2069 case ETHTOOL_GRXRINGS
:
2070 case ETHTOOL_GRXCLSRLCNT
:
2071 case ETHTOOL_GRXCLSRULE
:
2072 case ETHTOOL_GRXCLSRLALL
:
2073 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
2076 case ETHTOOL_SRXCLSRLDEL
:
2077 case ETHTOOL_SRXCLSRLINS
:
2078 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
2080 case ETHTOOL_FLASHDEV
:
2081 rc
= ethtool_flash_device(dev
, useraddr
);
2084 rc
= ethtool_reset(dev
, useraddr
);
2086 case ETHTOOL_GSSET_INFO
:
2087 rc
= ethtool_get_sset_info(dev
, useraddr
);
2089 case ETHTOOL_GRXFHINDIR
:
2090 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
2092 case ETHTOOL_SRXFHINDIR
:
2093 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
2096 rc
= ethtool_get_rxfh(dev
, useraddr
);
2099 rc
= ethtool_set_rxfh(dev
, useraddr
);
2101 case ETHTOOL_GFEATURES
:
2102 rc
= ethtool_get_features(dev
, useraddr
);
2104 case ETHTOOL_SFEATURES
:
2105 rc
= ethtool_set_features(dev
, useraddr
);
2107 case ETHTOOL_GTXCSUM
:
2108 case ETHTOOL_GRXCSUM
:
2114 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
2116 case ETHTOOL_STXCSUM
:
2117 case ETHTOOL_SRXCSUM
:
2123 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
2125 case ETHTOOL_GCHANNELS
:
2126 rc
= ethtool_get_channels(dev
, useraddr
);
2128 case ETHTOOL_SCHANNELS
:
2129 rc
= ethtool_set_channels(dev
, useraddr
);
2131 case ETHTOOL_SET_DUMP
:
2132 rc
= ethtool_set_dump(dev
, useraddr
);
2134 case ETHTOOL_GET_DUMP_FLAG
:
2135 rc
= ethtool_get_dump_flag(dev
, useraddr
);
2137 case ETHTOOL_GET_DUMP_DATA
:
2138 rc
= ethtool_get_dump_data(dev
, useraddr
);
2140 case ETHTOOL_GET_TS_INFO
:
2141 rc
= ethtool_get_ts_info(dev
, useraddr
);
2143 case ETHTOOL_GMODULEINFO
:
2144 rc
= ethtool_get_module_info(dev
, useraddr
);
2146 case ETHTOOL_GMODULEEEPROM
:
2147 rc
= ethtool_get_module_eeprom(dev
, useraddr
);
2149 case ETHTOOL_GTUNABLE
:
2150 rc
= ethtool_get_tunable(dev
, useraddr
);
2152 case ETHTOOL_STUNABLE
:
2153 rc
= ethtool_set_tunable(dev
, useraddr
);
2155 case ETHTOOL_GPHYSTATS
:
2156 rc
= ethtool_get_phy_stats(dev
, useraddr
);
2158 case ETHTOOL_PERQUEUE
:
2159 rc
= ethtool_set_per_queue(dev
, useraddr
);
2165 if (dev
->ethtool_ops
->complete
)
2166 dev
->ethtool_ops
->complete(dev
);
2168 if (old_features
!= dev
->features
)
2169 netdev_features_change(dev
);