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",
96 [NETIF_F_FCOE_CRC_BIT
] = "tx-checksum-fcoe-crc",
97 [NETIF_F_SCTP_CRC_BIT
] = "tx-checksum-sctp",
98 [NETIF_F_FCOE_MTU_BIT
] = "fcoe-mtu",
99 [NETIF_F_NTUPLE_BIT
] = "rx-ntuple-filter",
100 [NETIF_F_RXHASH_BIT
] = "rx-hashing",
101 [NETIF_F_RXCSUM_BIT
] = "rx-checksum",
102 [NETIF_F_NOCACHE_COPY_BIT
] = "tx-nocache-copy",
103 [NETIF_F_LOOPBACK_BIT
] = "loopback",
104 [NETIF_F_RXFCS_BIT
] = "rx-fcs",
105 [NETIF_F_RXALL_BIT
] = "rx-all",
106 [NETIF_F_HW_L2FW_DOFFLOAD_BIT
] = "l2-fwd-offload",
107 [NETIF_F_HW_TC_BIT
] = "hw-tc-offload",
108 [NETIF_F_HW_ESP_BIT
] = "esp-hw-offload",
109 [NETIF_F_HW_ESP_TX_CSUM_BIT
] = "esp-tx-csum-hw-offload",
110 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT
] = "rx-udp_tunnel-port-offload",
111 [NETIF_F_HW_TLS_RECORD_BIT
] = "tls-hw-record",
115 rss_hash_func_strings
[ETH_RSS_HASH_FUNCS_COUNT
][ETH_GSTRING_LEN
] = {
116 [ETH_RSS_HASH_TOP_BIT
] = "toeplitz",
117 [ETH_RSS_HASH_XOR_BIT
] = "xor",
118 [ETH_RSS_HASH_CRC32_BIT
] = "crc32",
122 tunable_strings
[__ETHTOOL_TUNABLE_COUNT
][ETH_GSTRING_LEN
] = {
123 [ETHTOOL_ID_UNSPEC
] = "Unspec",
124 [ETHTOOL_RX_COPYBREAK
] = "rx-copybreak",
125 [ETHTOOL_TX_COPYBREAK
] = "tx-copybreak",
126 [ETHTOOL_PFC_PREVENTION_TOUT
] = "pfc-prevention-tout",
130 phy_tunable_strings
[__ETHTOOL_PHY_TUNABLE_COUNT
][ETH_GSTRING_LEN
] = {
131 [ETHTOOL_ID_UNSPEC
] = "Unspec",
132 [ETHTOOL_PHY_DOWNSHIFT
] = "phy-downshift",
135 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
137 struct ethtool_gfeatures cmd
= {
138 .cmd
= ETHTOOL_GFEATURES
,
139 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
141 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
142 u32 __user
*sizeaddr
;
146 /* in case feature bits run out again */
147 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
149 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
150 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
151 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
152 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
153 features
[i
].never_changed
=
154 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
157 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
158 if (get_user(copy_size
, sizeaddr
))
161 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
162 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
164 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
166 useraddr
+= sizeof(cmd
);
167 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
173 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
175 struct ethtool_sfeatures cmd
;
176 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
177 netdev_features_t wanted
= 0, valid
= 0;
180 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
182 useraddr
+= sizeof(cmd
);
184 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
187 if (copy_from_user(features
, useraddr
, sizeof(features
)))
190 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
191 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
192 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
195 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
198 if (valid
& ~dev
->hw_features
) {
199 valid
&= dev
->hw_features
;
200 ret
|= ETHTOOL_F_UNSUPPORTED
;
203 dev
->wanted_features
&= ~valid
;
204 dev
->wanted_features
|= wanted
& valid
;
205 __netdev_update_features(dev
);
207 if ((dev
->wanted_features
^ dev
->features
) & valid
)
208 ret
|= ETHTOOL_F_WISH
;
213 static int phy_get_sset_count(struct phy_device
*phydev
)
217 if (phydev
->drv
->get_sset_count
&&
218 phydev
->drv
->get_strings
&&
219 phydev
->drv
->get_stats
) {
220 mutex_lock(&phydev
->lock
);
221 ret
= phydev
->drv
->get_sset_count(phydev
);
222 mutex_unlock(&phydev
->lock
);
230 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
232 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
234 if (sset
== ETH_SS_FEATURES
)
235 return ARRAY_SIZE(netdev_features_strings
);
237 if (sset
== ETH_SS_RSS_HASH_FUNCS
)
238 return ARRAY_SIZE(rss_hash_func_strings
);
240 if (sset
== ETH_SS_TUNABLES
)
241 return ARRAY_SIZE(tunable_strings
);
243 if (sset
== ETH_SS_PHY_TUNABLES
)
244 return ARRAY_SIZE(phy_tunable_strings
);
246 if (sset
== ETH_SS_PHY_STATS
) {
248 return phy_get_sset_count(dev
->phydev
);
253 if (ops
->get_sset_count
&& ops
->get_strings
)
254 return ops
->get_sset_count(dev
, sset
);
259 static void __ethtool_get_strings(struct net_device
*dev
,
260 u32 stringset
, u8
*data
)
262 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
264 if (stringset
== ETH_SS_FEATURES
)
265 memcpy(data
, netdev_features_strings
,
266 sizeof(netdev_features_strings
));
267 else if (stringset
== ETH_SS_RSS_HASH_FUNCS
)
268 memcpy(data
, rss_hash_func_strings
,
269 sizeof(rss_hash_func_strings
));
270 else if (stringset
== ETH_SS_TUNABLES
)
271 memcpy(data
, tunable_strings
, sizeof(tunable_strings
));
272 else if (stringset
== ETH_SS_PHY_TUNABLES
)
273 memcpy(data
, phy_tunable_strings
, sizeof(phy_tunable_strings
));
274 else if (stringset
== ETH_SS_PHY_STATS
) {
275 struct phy_device
*phydev
= dev
->phydev
;
278 mutex_lock(&phydev
->lock
);
279 phydev
->drv
->get_strings(phydev
, data
);
280 mutex_unlock(&phydev
->lock
);
285 /* ops->get_strings is valid because checked earlier */
286 ops
->get_strings(dev
, stringset
, data
);
289 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
291 /* feature masks of legacy discrete ethtool ops */
294 case ETHTOOL_GTXCSUM
:
295 case ETHTOOL_STXCSUM
:
296 return NETIF_F_CSUM_MASK
| NETIF_F_SCTP_CRC
;
297 case ETHTOOL_GRXCSUM
:
298 case ETHTOOL_SRXCSUM
:
299 return NETIF_F_RXCSUM
;
305 return NETIF_F_ALL_TSO
;
317 static int ethtool_get_one_feature(struct net_device
*dev
,
318 char __user
*useraddr
, u32 ethcmd
)
320 netdev_features_t mask
= ethtool_get_feature_mask(ethcmd
);
321 struct ethtool_value edata
= {
323 .data
= !!(dev
->features
& mask
),
326 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
331 static int ethtool_set_one_feature(struct net_device
*dev
,
332 void __user
*useraddr
, u32 ethcmd
)
334 struct ethtool_value edata
;
335 netdev_features_t mask
;
337 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
340 mask
= ethtool_get_feature_mask(ethcmd
);
341 mask
&= dev
->hw_features
;
346 dev
->wanted_features
|= mask
;
348 dev
->wanted_features
&= ~mask
;
350 __netdev_update_features(dev
);
355 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
356 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
357 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
358 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
361 static u32
__ethtool_get_flags(struct net_device
*dev
)
365 if (dev
->features
& NETIF_F_LRO
)
366 flags
|= ETH_FLAG_LRO
;
367 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_RX
)
368 flags
|= ETH_FLAG_RXVLAN
;
369 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_TX
)
370 flags
|= ETH_FLAG_TXVLAN
;
371 if (dev
->features
& NETIF_F_NTUPLE
)
372 flags
|= ETH_FLAG_NTUPLE
;
373 if (dev
->features
& NETIF_F_RXHASH
)
374 flags
|= ETH_FLAG_RXHASH
;
379 static int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
381 netdev_features_t features
= 0, changed
;
383 if (data
& ~ETH_ALL_FLAGS
)
386 if (data
& ETH_FLAG_LRO
)
387 features
|= NETIF_F_LRO
;
388 if (data
& ETH_FLAG_RXVLAN
)
389 features
|= NETIF_F_HW_VLAN_CTAG_RX
;
390 if (data
& ETH_FLAG_TXVLAN
)
391 features
|= NETIF_F_HW_VLAN_CTAG_TX
;
392 if (data
& ETH_FLAG_NTUPLE
)
393 features
|= NETIF_F_NTUPLE
;
394 if (data
& ETH_FLAG_RXHASH
)
395 features
|= NETIF_F_RXHASH
;
397 /* allow changing only bits set in hw_features */
398 changed
= (features
^ dev
->features
) & ETH_ALL_FEATURES
;
399 if (changed
& ~dev
->hw_features
)
400 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
402 dev
->wanted_features
=
403 (dev
->wanted_features
& ~changed
) | (features
& changed
);
405 __netdev_update_features(dev
);
410 /* Given two link masks, AND them together and save the result in dst. */
411 void ethtool_intersect_link_masks(struct ethtool_link_ksettings
*dst
,
412 struct ethtool_link_ksettings
*src
)
414 unsigned int size
= BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS
);
415 unsigned int idx
= 0;
417 for (; idx
< size
; idx
++) {
418 dst
->link_modes
.supported
[idx
] &=
419 src
->link_modes
.supported
[idx
];
420 dst
->link_modes
.advertising
[idx
] &=
421 src
->link_modes
.advertising
[idx
];
424 EXPORT_SYMBOL(ethtool_intersect_link_masks
);
426 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst
,
429 bitmap_zero(dst
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
432 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode
);
434 /* return false if src had higher bits set. lower bits always updated. */
435 bool ethtool_convert_link_mode_to_legacy_u32(u32
*legacy_u32
,
436 const unsigned long *src
)
440 /* TODO: following test will soon always be true */
441 if (__ETHTOOL_LINK_MODE_MASK_NBITS
> 32) {
442 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext
);
444 bitmap_zero(ext
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
445 bitmap_fill(ext
, 32);
446 bitmap_complement(ext
, ext
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
447 if (bitmap_intersects(ext
, src
,
448 __ETHTOOL_LINK_MODE_MASK_NBITS
)) {
449 /* src mask goes beyond bit 31 */
453 *legacy_u32
= src
[0];
456 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32
);
458 /* return false if legacy contained non-0 deprecated fields
459 * maxtxpkt/maxrxpkt. rest of ksettings always updated
462 convert_legacy_settings_to_link_ksettings(
463 struct ethtool_link_ksettings
*link_ksettings
,
464 const struct ethtool_cmd
*legacy_settings
)
468 memset(link_ksettings
, 0, sizeof(*link_ksettings
));
470 /* This is used to tell users that driver is still using these
471 * deprecated legacy fields, and they should not use
472 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
474 if (legacy_settings
->maxtxpkt
||
475 legacy_settings
->maxrxpkt
)
478 ethtool_convert_legacy_u32_to_link_mode(
479 link_ksettings
->link_modes
.supported
,
480 legacy_settings
->supported
);
481 ethtool_convert_legacy_u32_to_link_mode(
482 link_ksettings
->link_modes
.advertising
,
483 legacy_settings
->advertising
);
484 ethtool_convert_legacy_u32_to_link_mode(
485 link_ksettings
->link_modes
.lp_advertising
,
486 legacy_settings
->lp_advertising
);
487 link_ksettings
->base
.speed
488 = ethtool_cmd_speed(legacy_settings
);
489 link_ksettings
->base
.duplex
490 = legacy_settings
->duplex
;
491 link_ksettings
->base
.port
492 = legacy_settings
->port
;
493 link_ksettings
->base
.phy_address
494 = legacy_settings
->phy_address
;
495 link_ksettings
->base
.autoneg
496 = legacy_settings
->autoneg
;
497 link_ksettings
->base
.mdio_support
498 = legacy_settings
->mdio_support
;
499 link_ksettings
->base
.eth_tp_mdix
500 = legacy_settings
->eth_tp_mdix
;
501 link_ksettings
->base
.eth_tp_mdix_ctrl
502 = legacy_settings
->eth_tp_mdix_ctrl
;
506 /* return false if ksettings link modes had higher bits
507 * set. legacy_settings always updated (best effort)
510 convert_link_ksettings_to_legacy_settings(
511 struct ethtool_cmd
*legacy_settings
,
512 const struct ethtool_link_ksettings
*link_ksettings
)
516 memset(legacy_settings
, 0, sizeof(*legacy_settings
));
517 /* this also clears the deprecated fields in legacy structure:
523 retval
&= ethtool_convert_link_mode_to_legacy_u32(
524 &legacy_settings
->supported
,
525 link_ksettings
->link_modes
.supported
);
526 retval
&= ethtool_convert_link_mode_to_legacy_u32(
527 &legacy_settings
->advertising
,
528 link_ksettings
->link_modes
.advertising
);
529 retval
&= ethtool_convert_link_mode_to_legacy_u32(
530 &legacy_settings
->lp_advertising
,
531 link_ksettings
->link_modes
.lp_advertising
);
532 ethtool_cmd_speed_set(legacy_settings
, link_ksettings
->base
.speed
);
533 legacy_settings
->duplex
534 = link_ksettings
->base
.duplex
;
535 legacy_settings
->port
536 = link_ksettings
->base
.port
;
537 legacy_settings
->phy_address
538 = link_ksettings
->base
.phy_address
;
539 legacy_settings
->autoneg
540 = link_ksettings
->base
.autoneg
;
541 legacy_settings
->mdio_support
542 = link_ksettings
->base
.mdio_support
;
543 legacy_settings
->eth_tp_mdix
544 = link_ksettings
->base
.eth_tp_mdix
;
545 legacy_settings
->eth_tp_mdix_ctrl
546 = link_ksettings
->base
.eth_tp_mdix_ctrl
;
547 legacy_settings
->transceiver
548 = link_ksettings
->base
.transceiver
;
552 /* number of 32-bit words to store the user's link mode bitmaps */
553 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
554 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
556 /* layout of the struct passed from/to userland */
557 struct ethtool_link_usettings
{
558 struct ethtool_link_settings base
;
560 __u32 supported
[__ETHTOOL_LINK_MODE_MASK_NU32
];
561 __u32 advertising
[__ETHTOOL_LINK_MODE_MASK_NU32
];
562 __u32 lp_advertising
[__ETHTOOL_LINK_MODE_MASK_NU32
];
566 /* Internal kernel helper to query a device ethtool_link_settings.
568 * Backward compatibility note: for compatibility with legacy drivers
569 * that implement only the ethtool_cmd API, this has to work with both
570 * drivers implementing get_link_ksettings API and drivers
571 * implementing get_settings API. When drivers implement get_settings
572 * and report ethtool_cmd deprecated fields
573 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
574 * because the resulting struct ethtool_link_settings does not report them.
576 int __ethtool_get_link_ksettings(struct net_device
*dev
,
577 struct ethtool_link_ksettings
*link_ksettings
)
580 struct ethtool_cmd cmd
;
584 if (dev
->ethtool_ops
->get_link_ksettings
) {
585 memset(link_ksettings
, 0, sizeof(*link_ksettings
));
586 return dev
->ethtool_ops
->get_link_ksettings(dev
,
590 /* driver doesn't support %ethtool_link_ksettings API. revert to
591 * legacy %ethtool_cmd API, unless it's not supported either.
592 * TODO: remove when ethtool_ops::get_settings disappears internally
594 if (!dev
->ethtool_ops
->get_settings
)
597 memset(&cmd
, 0, sizeof(cmd
));
598 cmd
.cmd
= ETHTOOL_GSET
;
599 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
603 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
605 convert_legacy_settings_to_link_ksettings(link_ksettings
, &cmd
);
608 EXPORT_SYMBOL(__ethtool_get_link_ksettings
);
610 /* convert ethtool_link_usettings in user space to a kernel internal
611 * ethtool_link_ksettings. return 0 on success, errno on error.
613 static int load_link_ksettings_from_user(struct ethtool_link_ksettings
*to
,
614 const void __user
*from
)
616 struct ethtool_link_usettings link_usettings
;
618 if (copy_from_user(&link_usettings
, from
, sizeof(link_usettings
)))
621 memcpy(&to
->base
, &link_usettings
.base
, sizeof(to
->base
));
622 bitmap_from_arr32(to
->link_modes
.supported
,
623 link_usettings
.link_modes
.supported
,
624 __ETHTOOL_LINK_MODE_MASK_NBITS
);
625 bitmap_from_arr32(to
->link_modes
.advertising
,
626 link_usettings
.link_modes
.advertising
,
627 __ETHTOOL_LINK_MODE_MASK_NBITS
);
628 bitmap_from_arr32(to
->link_modes
.lp_advertising
,
629 link_usettings
.link_modes
.lp_advertising
,
630 __ETHTOOL_LINK_MODE_MASK_NBITS
);
635 /* convert a kernel internal ethtool_link_ksettings to
636 * ethtool_link_usettings in user space. return 0 on success, errno on
640 store_link_ksettings_for_user(void __user
*to
,
641 const struct ethtool_link_ksettings
*from
)
643 struct ethtool_link_usettings link_usettings
;
645 memcpy(&link_usettings
.base
, &from
->base
, sizeof(link_usettings
));
646 bitmap_to_arr32(link_usettings
.link_modes
.supported
,
647 from
->link_modes
.supported
,
648 __ETHTOOL_LINK_MODE_MASK_NBITS
);
649 bitmap_to_arr32(link_usettings
.link_modes
.advertising
,
650 from
->link_modes
.advertising
,
651 __ETHTOOL_LINK_MODE_MASK_NBITS
);
652 bitmap_to_arr32(link_usettings
.link_modes
.lp_advertising
,
653 from
->link_modes
.lp_advertising
,
654 __ETHTOOL_LINK_MODE_MASK_NBITS
);
656 if (copy_to_user(to
, &link_usettings
, sizeof(link_usettings
)))
662 /* Query device for its ethtool_link_settings.
664 * Backward compatibility note: this function must fail when driver
665 * does not implement ethtool::get_link_ksettings, even if legacy
666 * ethtool_ops::get_settings is implemented. This tells new versions
667 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
668 * this driver, so that they can correctly access the ethtool_cmd
669 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
670 * implements ethtool_ops::get_settings anymore.
672 static int ethtool_get_link_ksettings(struct net_device
*dev
,
673 void __user
*useraddr
)
676 struct ethtool_link_ksettings link_ksettings
;
680 if (!dev
->ethtool_ops
->get_link_ksettings
)
683 /* handle bitmap nbits handshake */
684 if (copy_from_user(&link_ksettings
.base
, useraddr
,
685 sizeof(link_ksettings
.base
)))
688 if (__ETHTOOL_LINK_MODE_MASK_NU32
689 != link_ksettings
.base
.link_mode_masks_nwords
) {
690 /* wrong link mode nbits requested */
691 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
692 link_ksettings
.base
.cmd
= ETHTOOL_GLINKSETTINGS
;
693 /* send back number of words required as negative val */
694 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32
<= S8_MAX
,
695 "need too many bits for link modes!");
696 link_ksettings
.base
.link_mode_masks_nwords
697 = -((s8
)__ETHTOOL_LINK_MODE_MASK_NU32
);
699 /* copy the base fields back to user, not the link
702 if (copy_to_user(useraddr
, &link_ksettings
.base
,
703 sizeof(link_ksettings
.base
)))
709 /* handshake successful: user/kernel agree on
710 * link_mode_masks_nwords
713 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
714 err
= dev
->ethtool_ops
->get_link_ksettings(dev
, &link_ksettings
);
718 /* make sure we tell the right values to user */
719 link_ksettings
.base
.cmd
= ETHTOOL_GLINKSETTINGS
;
720 link_ksettings
.base
.link_mode_masks_nwords
721 = __ETHTOOL_LINK_MODE_MASK_NU32
;
723 return store_link_ksettings_for_user(useraddr
, &link_ksettings
);
726 /* Update device ethtool_link_settings.
728 * Backward compatibility note: this function must fail when driver
729 * does not implement ethtool::set_link_ksettings, even if legacy
730 * ethtool_ops::set_settings is implemented. This tells new versions
731 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
732 * this driver, so that they can correctly update the ethtool_cmd
733 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
734 * implements ethtool_ops::get_settings anymore.
736 static int ethtool_set_link_ksettings(struct net_device
*dev
,
737 void __user
*useraddr
)
740 struct ethtool_link_ksettings link_ksettings
;
744 if (!dev
->ethtool_ops
->set_link_ksettings
)
747 /* make sure nbits field has expected value */
748 if (copy_from_user(&link_ksettings
.base
, useraddr
,
749 sizeof(link_ksettings
.base
)))
752 if (__ETHTOOL_LINK_MODE_MASK_NU32
753 != link_ksettings
.base
.link_mode_masks_nwords
)
756 /* copy the whole structure, now that we know it has expected
759 err
= load_link_ksettings_from_user(&link_ksettings
, useraddr
);
763 /* re-check nwords field, just in case */
764 if (__ETHTOOL_LINK_MODE_MASK_NU32
765 != link_ksettings
.base
.link_mode_masks_nwords
)
768 return dev
->ethtool_ops
->set_link_ksettings(dev
, &link_ksettings
);
771 /* Query device for its ethtool_cmd settings.
773 * Backward compatibility note: for compatibility with legacy ethtool,
774 * this has to work with both drivers implementing get_link_ksettings
775 * API and drivers implementing get_settings API. When drivers
776 * implement get_link_ksettings and report higher link mode bits, a
777 * kernel warning is logged once (with name of 1st driver/device) to
778 * recommend user to upgrade ethtool, but the command is successful
779 * (only the lower link mode bits reported back to user).
781 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
783 struct ethtool_cmd cmd
;
787 if (dev
->ethtool_ops
->get_link_ksettings
) {
788 /* First, use link_ksettings API if it is supported */
790 struct ethtool_link_ksettings link_ksettings
;
792 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
793 err
= dev
->ethtool_ops
->get_link_ksettings(dev
,
797 convert_link_ksettings_to_legacy_settings(&cmd
,
800 /* send a sensible cmd tag back to user */
801 cmd
.cmd
= ETHTOOL_GSET
;
803 /* driver doesn't support %ethtool_link_ksettings
804 * API. revert to legacy %ethtool_cmd API, unless it's
805 * not supported either.
809 if (!dev
->ethtool_ops
->get_settings
)
812 memset(&cmd
, 0, sizeof(cmd
));
813 cmd
.cmd
= ETHTOOL_GSET
;
814 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
819 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
825 /* Update device link settings with given ethtool_cmd.
827 * Backward compatibility note: for compatibility with legacy ethtool,
828 * this has to work with both drivers implementing set_link_ksettings
829 * API and drivers implementing set_settings API. When drivers
830 * implement set_link_ksettings and user's request updates deprecated
831 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
832 * warning is logged once (with name of 1st driver/device) to
833 * recommend user to upgrade ethtool, and the request is rejected.
835 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
837 struct ethtool_cmd cmd
;
841 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
844 /* first, try new %ethtool_link_ksettings API. */
845 if (dev
->ethtool_ops
->set_link_ksettings
) {
846 struct ethtool_link_ksettings link_ksettings
;
848 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings
,
852 link_ksettings
.base
.cmd
= ETHTOOL_SLINKSETTINGS
;
853 link_ksettings
.base
.link_mode_masks_nwords
854 = __ETHTOOL_LINK_MODE_MASK_NU32
;
855 return dev
->ethtool_ops
->set_link_ksettings(dev
,
859 /* legacy %ethtool_cmd API */
861 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
862 * disappears internally
865 if (!dev
->ethtool_ops
->set_settings
)
868 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
871 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
872 void __user
*useraddr
)
874 struct ethtool_drvinfo info
;
875 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
877 memset(&info
, 0, sizeof(info
));
878 info
.cmd
= ETHTOOL_GDRVINFO
;
879 if (ops
->get_drvinfo
) {
880 ops
->get_drvinfo(dev
, &info
);
881 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
882 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
883 sizeof(info
.bus_info
));
884 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
885 sizeof(info
.driver
));
891 * this method of obtaining string set info is deprecated;
892 * Use ETHTOOL_GSSET_INFO instead.
894 if (ops
->get_sset_count
) {
897 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
899 info
.testinfo_len
= rc
;
900 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
903 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
905 info
.n_priv_flags
= rc
;
907 if (ops
->get_regs_len
)
908 info
.regdump_len
= ops
->get_regs_len(dev
);
909 if (ops
->get_eeprom_len
)
910 info
.eedump_len
= ops
->get_eeprom_len(dev
);
912 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
917 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
918 void __user
*useraddr
)
920 struct ethtool_sset_info info
;
922 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
923 u32
*info_buf
= NULL
;
925 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
928 /* store copy of mask, because we zero struct later on */
929 sset_mask
= info
.sset_mask
;
933 /* calculate size of return buffer */
934 n_bits
= hweight64(sset_mask
);
936 memset(&info
, 0, sizeof(info
));
937 info
.cmd
= ETHTOOL_GSSET_INFO
;
939 info_buf
= kzalloc(n_bits
* sizeof(u32
), GFP_USER
);
944 * fill return buffer based on input bitmask and successful
945 * get_sset_count return
947 for (i
= 0; i
< 64; i
++) {
948 if (!(sset_mask
& (1ULL << i
)))
951 rc
= __ethtool_get_sset_count(dev
, i
);
953 info
.sset_mask
|= (1ULL << i
);
954 info_buf
[idx
++] = rc
;
959 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
962 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
963 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
973 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
974 u32 cmd
, void __user
*useraddr
)
976 struct ethtool_rxnfc info
;
977 size_t info_size
= sizeof(info
);
980 if (!dev
->ethtool_ops
->set_rxnfc
)
983 /* struct ethtool_rxnfc was originally defined for
984 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
985 * members. User-space might still be using that
987 if (cmd
== ETHTOOL_SRXFH
)
988 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
991 if (copy_from_user(&info
, useraddr
, info_size
))
994 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
998 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
999 copy_to_user(useraddr
, &info
, info_size
))
1005 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
1006 u32 cmd
, void __user
*useraddr
)
1008 struct ethtool_rxnfc info
;
1009 size_t info_size
= sizeof(info
);
1010 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1012 void *rule_buf
= NULL
;
1014 if (!ops
->get_rxnfc
)
1017 /* struct ethtool_rxnfc was originally defined for
1018 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1019 * members. User-space might still be using that
1021 if (cmd
== ETHTOOL_GRXFH
)
1022 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
1025 if (copy_from_user(&info
, useraddr
, info_size
))
1028 /* If FLOW_RSS was requested then user-space must be using the
1029 * new definition, as FLOW_RSS is newer.
1031 if (cmd
== ETHTOOL_GRXFH
&& info
.flow_type
& FLOW_RSS
) {
1032 info_size
= sizeof(info
);
1033 if (copy_from_user(&info
, useraddr
, info_size
))
1035 /* Since malicious users may modify the original data,
1036 * we need to check whether FLOW_RSS is still requested.
1038 if (!(info
.flow_type
& FLOW_RSS
))
1042 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
1043 if (info
.rule_cnt
> 0) {
1044 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
1045 rule_buf
= kzalloc(info
.rule_cnt
* sizeof(u32
),
1052 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
1057 if (copy_to_user(useraddr
, &info
, info_size
))
1061 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
1062 if (copy_to_user(useraddr
, rule_buf
,
1063 info
.rule_cnt
* sizeof(u32
)))
1074 static int ethtool_copy_validate_indir(u32
*indir
, void __user
*useraddr
,
1075 struct ethtool_rxnfc
*rx_rings
,
1080 if (copy_from_user(indir
, useraddr
, size
* sizeof(indir
[0])))
1083 /* Validate ring indices */
1084 for (i
= 0; i
< size
; i
++)
1085 if (indir
[i
] >= rx_rings
->data
)
1091 u8 netdev_rss_key
[NETDEV_RSS_KEY_LEN
] __read_mostly
;
1093 void netdev_rss_key_fill(void *buffer
, size_t len
)
1095 BUG_ON(len
> sizeof(netdev_rss_key
));
1096 net_get_random_once(netdev_rss_key
, sizeof(netdev_rss_key
));
1097 memcpy(buffer
, netdev_rss_key
, len
);
1099 EXPORT_SYMBOL(netdev_rss_key_fill
);
1101 static int ethtool_get_max_rxfh_channel(struct net_device
*dev
, u32
*max
)
1103 u32 dev_size
, current_max
= 0;
1107 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
1108 !dev
->ethtool_ops
->get_rxfh
)
1110 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
1114 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
1118 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
1123 current_max
= max(current_max
, indir
[dev_size
]);
1132 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
1133 void __user
*useraddr
)
1135 u32 user_size
, dev_size
;
1139 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
1140 !dev
->ethtool_ops
->get_rxfh
)
1142 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
1146 if (copy_from_user(&user_size
,
1147 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
1151 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
1152 &dev_size
, sizeof(dev_size
)))
1155 /* If the user buffer size is 0, this is just a query for the
1156 * device table size. Otherwise, if it's smaller than the
1157 * device table size it's an error.
1159 if (user_size
< dev_size
)
1160 return user_size
== 0 ? 0 : -EINVAL
;
1162 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
1166 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
1170 if (copy_to_user(useraddr
+
1171 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
1172 indir
, dev_size
* sizeof(indir
[0])))
1180 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
1181 void __user
*useraddr
)
1183 struct ethtool_rxnfc rx_rings
;
1184 u32 user_size
, dev_size
, i
;
1186 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1188 u32 ringidx_offset
= offsetof(struct ethtool_rxfh_indir
, ring_index
[0]);
1190 if (!ops
->get_rxfh_indir_size
|| !ops
->set_rxfh
||
1194 dev_size
= ops
->get_rxfh_indir_size(dev
);
1198 if (copy_from_user(&user_size
,
1199 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
1203 if (user_size
!= 0 && user_size
!= dev_size
)
1206 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
1210 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
1211 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
1215 if (user_size
== 0) {
1216 for (i
= 0; i
< dev_size
; i
++)
1217 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
1219 ret
= ethtool_copy_validate_indir(indir
,
1220 useraddr
+ ringidx_offset
,
1227 ret
= ops
->set_rxfh(dev
, indir
, NULL
, ETH_RSS_HASH_NO_CHANGE
);
1231 /* indicate whether rxfh was set to default */
1233 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
1235 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
1242 static noinline_for_stack
int ethtool_get_rxfh(struct net_device
*dev
,
1243 void __user
*useraddr
)
1246 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1247 u32 user_indir_size
, user_key_size
;
1248 u32 dev_indir_size
= 0, dev_key_size
= 0;
1249 struct ethtool_rxfh rxfh
;
1260 if (ops
->get_rxfh_indir_size
)
1261 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
1262 if (ops
->get_rxfh_key_size
)
1263 dev_key_size
= ops
->get_rxfh_key_size(dev
);
1265 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
1267 user_indir_size
= rxfh
.indir_size
;
1268 user_key_size
= rxfh
.key_size
;
1270 /* Check that reserved fields are 0 for now */
1271 if (rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] || rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
1273 /* Most drivers don't handle rss_context, check it's 0 as well */
1274 if (rxfh
.rss_context
&& !ops
->get_rxfh_context
)
1277 rxfh
.indir_size
= dev_indir_size
;
1278 rxfh
.key_size
= dev_key_size
;
1279 if (copy_to_user(useraddr
, &rxfh
, sizeof(rxfh
)))
1282 if ((user_indir_size
&& (user_indir_size
!= dev_indir_size
)) ||
1283 (user_key_size
&& (user_key_size
!= dev_key_size
)))
1286 indir_bytes
= user_indir_size
* sizeof(indir
[0]);
1287 total_size
= indir_bytes
+ user_key_size
;
1288 rss_config
= kzalloc(total_size
, GFP_USER
);
1292 if (user_indir_size
)
1293 indir
= (u32
*)rss_config
;
1296 hkey
= rss_config
+ indir_bytes
;
1298 if (rxfh
.rss_context
)
1299 ret
= dev
->ethtool_ops
->get_rxfh_context(dev
, indir
, hkey
,
1303 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, hkey
, &dev_hfunc
);
1307 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, hfunc
),
1308 &dev_hfunc
, sizeof(rxfh
.hfunc
))) {
1310 } else if (copy_to_user(useraddr
+
1311 offsetof(struct ethtool_rxfh
, rss_config
[0]),
1312 rss_config
, total_size
)) {
1321 static noinline_for_stack
int ethtool_set_rxfh(struct net_device
*dev
,
1322 void __user
*useraddr
)
1325 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1326 struct ethtool_rxnfc rx_rings
;
1327 struct ethtool_rxfh rxfh
;
1328 u32 dev_indir_size
= 0, dev_key_size
= 0, i
;
1329 u32
*indir
= NULL
, indir_bytes
= 0;
1332 u32 rss_cfg_offset
= offsetof(struct ethtool_rxfh
, rss_config
[0]);
1333 bool delete = false;
1335 if (!ops
->get_rxnfc
|| !ops
->set_rxfh
)
1338 if (ops
->get_rxfh_indir_size
)
1339 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
1340 if (ops
->get_rxfh_key_size
)
1341 dev_key_size
= ops
->get_rxfh_key_size(dev
);
1343 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
1346 /* Check that reserved fields are 0 for now */
1347 if (rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] || rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
1349 /* Most drivers don't handle rss_context, check it's 0 as well */
1350 if (rxfh
.rss_context
&& !ops
->set_rxfh_context
)
1353 /* If either indir, hash key or function is valid, proceed further.
1354 * Must request at least one change: indir size, hash key or function.
1356 if ((rxfh
.indir_size
&&
1357 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
&&
1358 rxfh
.indir_size
!= dev_indir_size
) ||
1359 (rxfh
.key_size
&& (rxfh
.key_size
!= dev_key_size
)) ||
1360 (rxfh
.indir_size
== ETH_RXFH_INDIR_NO_CHANGE
&&
1361 rxfh
.key_size
== 0 && rxfh
.hfunc
== ETH_RSS_HASH_NO_CHANGE
))
1364 if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
1365 indir_bytes
= dev_indir_size
* sizeof(indir
[0]);
1367 rss_config
= kzalloc(indir_bytes
+ rxfh
.key_size
, GFP_USER
);
1371 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
1372 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
1376 /* rxfh.indir_size == 0 means reset the indir table to default (master
1377 * context) or delete the context (other RSS contexts).
1378 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1380 if (rxfh
.indir_size
&&
1381 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
) {
1382 indir
= (u32
*)rss_config
;
1383 ret
= ethtool_copy_validate_indir(indir
,
1384 useraddr
+ rss_cfg_offset
,
1389 } else if (rxfh
.indir_size
== 0) {
1390 if (rxfh
.rss_context
== 0) {
1391 indir
= (u32
*)rss_config
;
1392 for (i
= 0; i
< dev_indir_size
; i
++)
1393 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
1399 if (rxfh
.key_size
) {
1400 hkey
= rss_config
+ indir_bytes
;
1401 if (copy_from_user(hkey
,
1402 useraddr
+ rss_cfg_offset
+ indir_bytes
,
1409 if (rxfh
.rss_context
)
1410 ret
= ops
->set_rxfh_context(dev
, indir
, hkey
, rxfh
.hfunc
,
1411 &rxfh
.rss_context
, delete);
1413 ret
= ops
->set_rxfh(dev
, indir
, hkey
, rxfh
.hfunc
);
1417 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, rss_context
),
1418 &rxfh
.rss_context
, sizeof(rxfh
.rss_context
)))
1421 if (!rxfh
.rss_context
) {
1422 /* indicate whether rxfh was set to default */
1423 if (rxfh
.indir_size
== 0)
1424 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
1425 else if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
1426 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
1434 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
1436 struct ethtool_regs regs
;
1437 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1441 if (!ops
->get_regs
|| !ops
->get_regs_len
)
1444 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
1447 reglen
= ops
->get_regs_len(dev
);
1448 if (regs
.len
> reglen
)
1453 regbuf
= vzalloc(reglen
);
1458 ops
->get_regs(dev
, ®s
, regbuf
);
1461 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
1463 useraddr
+= offsetof(struct ethtool_regs
, data
);
1464 if (regbuf
&& copy_to_user(useraddr
, regbuf
, regs
.len
))
1473 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
1475 struct ethtool_value reset
;
1478 if (!dev
->ethtool_ops
->reset
)
1481 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
1484 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
1488 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
1493 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
1495 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
1497 if (!dev
->ethtool_ops
->get_wol
)
1500 dev
->ethtool_ops
->get_wol(dev
, &wol
);
1502 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
1507 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
1509 struct ethtool_wolinfo wol
;
1511 if (!dev
->ethtool_ops
->set_wol
)
1514 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
1517 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
1520 static int ethtool_get_eee(struct net_device
*dev
, char __user
*useraddr
)
1522 struct ethtool_eee edata
;
1525 if (!dev
->ethtool_ops
->get_eee
)
1528 memset(&edata
, 0, sizeof(struct ethtool_eee
));
1529 edata
.cmd
= ETHTOOL_GEEE
;
1530 rc
= dev
->ethtool_ops
->get_eee(dev
, &edata
);
1535 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1541 static int ethtool_set_eee(struct net_device
*dev
, char __user
*useraddr
)
1543 struct ethtool_eee edata
;
1545 if (!dev
->ethtool_ops
->set_eee
)
1548 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1551 return dev
->ethtool_ops
->set_eee(dev
, &edata
);
1554 static int ethtool_nway_reset(struct net_device
*dev
)
1556 if (!dev
->ethtool_ops
->nway_reset
)
1559 return dev
->ethtool_ops
->nway_reset(dev
);
1562 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
1564 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
1566 if (!dev
->ethtool_ops
->get_link
)
1569 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
1571 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1576 static int ethtool_get_any_eeprom(struct net_device
*dev
, void __user
*useraddr
,
1577 int (*getter
)(struct net_device
*,
1578 struct ethtool_eeprom
*, u8
*),
1581 struct ethtool_eeprom eeprom
;
1582 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1583 u32 bytes_remaining
;
1587 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1590 /* Check for wrap and zero */
1591 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1594 /* Check for exceeding total eeprom len */
1595 if (eeprom
.offset
+ eeprom
.len
> total_len
)
1598 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1602 bytes_remaining
= eeprom
.len
;
1603 while (bytes_remaining
> 0) {
1604 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1606 ret
= getter(dev
, &eeprom
, data
);
1609 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
1613 userbuf
+= eeprom
.len
;
1614 eeprom
.offset
+= eeprom
.len
;
1615 bytes_remaining
-= eeprom
.len
;
1618 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
1619 eeprom
.offset
-= eeprom
.len
;
1620 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
1627 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1629 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1631 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
||
1632 !ops
->get_eeprom_len(dev
))
1635 return ethtool_get_any_eeprom(dev
, useraddr
, ops
->get_eeprom
,
1636 ops
->get_eeprom_len(dev
));
1639 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1641 struct ethtool_eeprom eeprom
;
1642 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1643 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1644 u32 bytes_remaining
;
1648 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
||
1649 !ops
->get_eeprom_len(dev
))
1652 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1655 /* Check for wrap and zero */
1656 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1659 /* Check for exceeding total eeprom len */
1660 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
1663 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1667 bytes_remaining
= eeprom
.len
;
1668 while (bytes_remaining
> 0) {
1669 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1671 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
1675 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
1678 userbuf
+= eeprom
.len
;
1679 eeprom
.offset
+= eeprom
.len
;
1680 bytes_remaining
-= eeprom
.len
;
1687 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
1688 void __user
*useraddr
)
1690 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
1692 if (!dev
->ethtool_ops
->get_coalesce
)
1695 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
1697 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
1702 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
1703 void __user
*useraddr
)
1705 struct ethtool_coalesce coalesce
;
1707 if (!dev
->ethtool_ops
->set_coalesce
)
1710 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
1713 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
1716 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1718 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
1720 if (!dev
->ethtool_ops
->get_ringparam
)
1723 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
1725 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
1730 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1732 struct ethtool_ringparam ringparam
, max
= { .cmd
= ETHTOOL_GRINGPARAM
};
1734 if (!dev
->ethtool_ops
->set_ringparam
|| !dev
->ethtool_ops
->get_ringparam
)
1737 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
1740 dev
->ethtool_ops
->get_ringparam(dev
, &max
);
1742 /* ensure new ring parameters are within the maximums */
1743 if (ringparam
.rx_pending
> max
.rx_max_pending
||
1744 ringparam
.rx_mini_pending
> max
.rx_mini_max_pending
||
1745 ringparam
.rx_jumbo_pending
> max
.rx_jumbo_max_pending
||
1746 ringparam
.tx_pending
> max
.tx_max_pending
)
1749 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
1752 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
1753 void __user
*useraddr
)
1755 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
1757 if (!dev
->ethtool_ops
->get_channels
)
1760 dev
->ethtool_ops
->get_channels(dev
, &channels
);
1762 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
1767 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
1768 void __user
*useraddr
)
1770 struct ethtool_channels channels
, max
= { .cmd
= ETHTOOL_GCHANNELS
};
1771 u32 max_rx_in_use
= 0;
1773 if (!dev
->ethtool_ops
->set_channels
|| !dev
->ethtool_ops
->get_channels
)
1776 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
1779 dev
->ethtool_ops
->get_channels(dev
, &max
);
1781 /* ensure new counts are within the maximums */
1782 if ((channels
.rx_count
> max
.max_rx
) ||
1783 (channels
.tx_count
> max
.max_tx
) ||
1784 (channels
.combined_count
> max
.max_combined
) ||
1785 (channels
.other_count
> max
.max_other
))
1788 /* ensure the new Rx count fits within the configured Rx flow
1789 * indirection table settings */
1790 if (netif_is_rxfh_configured(dev
) &&
1791 !ethtool_get_max_rxfh_channel(dev
, &max_rx_in_use
) &&
1792 (channels
.combined_count
+ channels
.rx_count
) <= max_rx_in_use
)
1795 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
1798 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1800 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
1802 if (!dev
->ethtool_ops
->get_pauseparam
)
1805 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
1807 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
1812 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1814 struct ethtool_pauseparam pauseparam
;
1816 if (!dev
->ethtool_ops
->set_pauseparam
)
1819 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
1822 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
1825 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1827 struct ethtool_test test
;
1828 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1832 if (!ops
->self_test
|| !ops
->get_sset_count
)
1835 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1838 WARN_ON(test_len
== 0);
1840 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1843 test
.len
= test_len
;
1844 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
1848 ops
->self_test(dev
, &test
, data
);
1851 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1853 useraddr
+= sizeof(test
);
1854 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1863 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1865 struct ethtool_gstrings gstrings
;
1869 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1872 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1875 if (ret
> S32_MAX
/ ETH_GSTRING_LEN
)
1880 data
= vzalloc(gstrings
.len
* ETH_GSTRING_LEN
);
1881 if (gstrings
.len
&& !data
)
1884 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1887 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1889 useraddr
+= sizeof(gstrings
);
1891 copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1900 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1902 struct ethtool_value id
;
1904 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1907 if (!ops
->set_phys_id
)
1913 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1916 rc
= ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1920 /* Drop the RTNL lock while waiting, but prevent reentry or
1921 * removal of the device.
1928 /* Driver will handle this itself */
1929 schedule_timeout_interruptible(
1930 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1932 /* Driver expects to be called at twice the frequency in rc */
1933 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1935 /* Count down seconds */
1937 /* Count down iterations per second */
1941 rc
= ops
->set_phys_id(dev
,
1942 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1946 schedule_timeout_interruptible(interval
);
1947 } while (!signal_pending(current
) && --i
!= 0);
1948 } while (!signal_pending(current
) &&
1949 (id
.data
== 0 || --id
.data
!= 0));
1956 (void) ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1960 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1962 struct ethtool_stats stats
;
1963 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1967 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1970 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1973 if (n_stats
> S32_MAX
/ sizeof(u64
))
1975 WARN_ON_ONCE(!n_stats
);
1976 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1979 stats
.n_stats
= n_stats
;
1980 data
= vzalloc(n_stats
* sizeof(u64
));
1981 if (n_stats
&& !data
)
1984 ops
->get_ethtool_stats(dev
, &stats
, data
);
1987 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1989 useraddr
+= sizeof(stats
);
1990 if (n_stats
&& copy_to_user(useraddr
, data
, n_stats
* sizeof(u64
)))
1999 static int ethtool_get_phy_stats(struct net_device
*dev
, void __user
*useraddr
)
2001 struct ethtool_stats stats
;
2002 struct phy_device
*phydev
= dev
->phydev
;
2009 n_stats
= phy_get_sset_count(phydev
);
2012 if (n_stats
> S32_MAX
/ sizeof(u64
))
2014 WARN_ON_ONCE(!n_stats
);
2016 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
2019 stats
.n_stats
= n_stats
;
2020 data
= vzalloc(n_stats
* sizeof(u64
));
2021 if (n_stats
&& !data
)
2024 mutex_lock(&phydev
->lock
);
2025 phydev
->drv
->get_stats(phydev
, &stats
, data
);
2026 mutex_unlock(&phydev
->lock
);
2029 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
2031 useraddr
+= sizeof(stats
);
2032 if (n_stats
&& copy_to_user(useraddr
, data
, n_stats
* sizeof(u64
)))
2041 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
2043 struct ethtool_perm_addr epaddr
;
2045 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
2048 if (epaddr
.size
< dev
->addr_len
)
2050 epaddr
.size
= dev
->addr_len
;
2052 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
2054 useraddr
+= sizeof(epaddr
);
2055 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
2060 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
2061 u32 cmd
, u32 (*actor
)(struct net_device
*))
2063 struct ethtool_value edata
= { .cmd
= cmd
};
2068 edata
.data
= actor(dev
);
2070 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
2075 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
2076 void (*actor
)(struct net_device
*, u32
))
2078 struct ethtool_value edata
;
2083 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2086 actor(dev
, edata
.data
);
2090 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
2091 int (*actor
)(struct net_device
*, u32
))
2093 struct ethtool_value edata
;
2098 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2101 return actor(dev
, edata
.data
);
2104 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
2105 char __user
*useraddr
)
2107 struct ethtool_flash efl
;
2109 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
2112 if (!dev
->ethtool_ops
->flash_device
)
2115 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
2117 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
2120 static int ethtool_set_dump(struct net_device
*dev
,
2121 void __user
*useraddr
)
2123 struct ethtool_dump dump
;
2125 if (!dev
->ethtool_ops
->set_dump
)
2128 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2131 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
2134 static int ethtool_get_dump_flag(struct net_device
*dev
,
2135 void __user
*useraddr
)
2138 struct ethtool_dump dump
;
2139 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2141 if (!ops
->get_dump_flag
)
2144 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2147 ret
= ops
->get_dump_flag(dev
, &dump
);
2151 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
2156 static int ethtool_get_dump_data(struct net_device
*dev
,
2157 void __user
*useraddr
)
2161 struct ethtool_dump dump
, tmp
;
2162 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2165 if (!ops
->get_dump_data
|| !ops
->get_dump_flag
)
2168 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2171 memset(&tmp
, 0, sizeof(tmp
));
2172 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
2173 ret
= ops
->get_dump_flag(dev
, &tmp
);
2177 len
= min(tmp
.len
, dump
.len
);
2181 /* Don't ever let the driver think there's more space available
2182 * than it requested with .get_dump_flag().
2186 /* Always allocate enough space to hold the whole thing so that the
2187 * driver does not need to check the length and bother with partial
2190 data
= vzalloc(tmp
.len
);
2193 ret
= ops
->get_dump_data(dev
, &dump
, data
);
2197 /* There are two sane possibilities:
2198 * 1. The driver's .get_dump_data() does not touch dump.len.
2199 * 2. Or it may set dump.len to how much it really writes, which
2200 * should be tmp.len (or len if it can do a partial dump).
2201 * In any case respond to userspace with the actual length of data
2204 WARN_ON(dump
.len
!= len
&& dump
.len
!= tmp
.len
);
2207 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
2211 useraddr
+= offsetof(struct ethtool_dump
, data
);
2212 if (copy_to_user(useraddr
, data
, len
))
2219 static int ethtool_get_ts_info(struct net_device
*dev
, void __user
*useraddr
)
2222 struct ethtool_ts_info info
;
2223 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2224 struct phy_device
*phydev
= dev
->phydev
;
2226 memset(&info
, 0, sizeof(info
));
2227 info
.cmd
= ETHTOOL_GET_TS_INFO
;
2229 if (phydev
&& phydev
->drv
&& phydev
->drv
->ts_info
) {
2230 err
= phydev
->drv
->ts_info(phydev
, &info
);
2231 } else if (ops
->get_ts_info
) {
2232 err
= ops
->get_ts_info(dev
, &info
);
2234 info
.so_timestamping
=
2235 SOF_TIMESTAMPING_RX_SOFTWARE
|
2236 SOF_TIMESTAMPING_SOFTWARE
;
2237 info
.phc_index
= -1;
2243 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
2249 static int __ethtool_get_module_info(struct net_device
*dev
,
2250 struct ethtool_modinfo
*modinfo
)
2252 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2253 struct phy_device
*phydev
= dev
->phydev
;
2256 return sfp_get_module_info(dev
->sfp_bus
, modinfo
);
2258 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_info
)
2259 return phydev
->drv
->module_info(phydev
, modinfo
);
2261 if (ops
->get_module_info
)
2262 return ops
->get_module_info(dev
, modinfo
);
2267 static int ethtool_get_module_info(struct net_device
*dev
,
2268 void __user
*useraddr
)
2271 struct ethtool_modinfo modinfo
;
2273 if (copy_from_user(&modinfo
, useraddr
, sizeof(modinfo
)))
2276 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2280 if (copy_to_user(useraddr
, &modinfo
, sizeof(modinfo
)))
2286 static int __ethtool_get_module_eeprom(struct net_device
*dev
,
2287 struct ethtool_eeprom
*ee
, u8
*data
)
2289 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2290 struct phy_device
*phydev
= dev
->phydev
;
2293 return sfp_get_module_eeprom(dev
->sfp_bus
, ee
, data
);
2295 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_eeprom
)
2296 return phydev
->drv
->module_eeprom(phydev
, ee
, data
);
2298 if (ops
->get_module_eeprom
)
2299 return ops
->get_module_eeprom(dev
, ee
, data
);
2304 static int ethtool_get_module_eeprom(struct net_device
*dev
,
2305 void __user
*useraddr
)
2308 struct ethtool_modinfo modinfo
;
2310 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2314 return ethtool_get_any_eeprom(dev
, useraddr
,
2315 __ethtool_get_module_eeprom
,
2316 modinfo
.eeprom_len
);
2319 static int ethtool_tunable_valid(const struct ethtool_tunable
*tuna
)
2322 case ETHTOOL_RX_COPYBREAK
:
2323 case ETHTOOL_TX_COPYBREAK
:
2324 if (tuna
->len
!= sizeof(u32
) ||
2325 tuna
->type_id
!= ETHTOOL_TUNABLE_U32
)
2328 case ETHTOOL_PFC_PREVENTION_TOUT
:
2329 if (tuna
->len
!= sizeof(u16
) ||
2330 tuna
->type_id
!= ETHTOOL_TUNABLE_U16
)
2340 static int ethtool_get_tunable(struct net_device
*dev
, void __user
*useraddr
)
2343 struct ethtool_tunable tuna
;
2344 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2347 if (!ops
->get_tunable
)
2349 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2351 ret
= ethtool_tunable_valid(&tuna
);
2354 data
= kmalloc(tuna
.len
, GFP_USER
);
2357 ret
= ops
->get_tunable(dev
, &tuna
, data
);
2360 useraddr
+= sizeof(tuna
);
2362 if (copy_to_user(useraddr
, data
, tuna
.len
))
2371 static int ethtool_set_tunable(struct net_device
*dev
, void __user
*useraddr
)
2374 struct ethtool_tunable tuna
;
2375 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2378 if (!ops
->set_tunable
)
2380 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2382 ret
= ethtool_tunable_valid(&tuna
);
2385 useraddr
+= sizeof(tuna
);
2386 data
= memdup_user(useraddr
, tuna
.len
);
2388 return PTR_ERR(data
);
2389 ret
= ops
->set_tunable(dev
, &tuna
, data
);
2395 static int ethtool_get_per_queue_coalesce(struct net_device
*dev
,
2396 void __user
*useraddr
,
2397 struct ethtool_per_queue_op
*per_queue_opt
)
2401 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2403 if (!dev
->ethtool_ops
->get_per_queue_coalesce
)
2406 useraddr
+= sizeof(*per_queue_opt
);
2408 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
,
2411 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2412 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
2414 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, &coalesce
);
2417 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
2419 useraddr
+= sizeof(coalesce
);
2425 static int ethtool_set_per_queue_coalesce(struct net_device
*dev
,
2426 void __user
*useraddr
,
2427 struct ethtool_per_queue_op
*per_queue_opt
)
2432 struct ethtool_coalesce
*backup
= NULL
, *tmp
= NULL
;
2433 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2435 if ((!dev
->ethtool_ops
->set_per_queue_coalesce
) ||
2436 (!dev
->ethtool_ops
->get_per_queue_coalesce
))
2439 useraddr
+= sizeof(*per_queue_opt
);
2441 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
, MAX_NUM_QUEUE
);
2442 n_queue
= bitmap_weight(queue_mask
, MAX_NUM_QUEUE
);
2443 tmp
= backup
= kmalloc_array(n_queue
, sizeof(*backup
), GFP_KERNEL
);
2447 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2448 struct ethtool_coalesce coalesce
;
2450 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, tmp
);
2456 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
))) {
2461 ret
= dev
->ethtool_ops
->set_per_queue_coalesce(dev
, bit
, &coalesce
);
2465 useraddr
+= sizeof(coalesce
);
2471 for_each_set_bit(i
, queue_mask
, bit
) {
2472 dev
->ethtool_ops
->set_per_queue_coalesce(dev
, i
, tmp
);
2481 static int ethtool_set_per_queue(struct net_device
*dev
, void __user
*useraddr
)
2483 struct ethtool_per_queue_op per_queue_opt
;
2485 if (copy_from_user(&per_queue_opt
, useraddr
, sizeof(per_queue_opt
)))
2488 switch (per_queue_opt
.sub_command
) {
2489 case ETHTOOL_GCOALESCE
:
2490 return ethtool_get_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2491 case ETHTOOL_SCOALESCE
:
2492 return ethtool_set_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2498 static int ethtool_phy_tunable_valid(const struct ethtool_tunable
*tuna
)
2501 case ETHTOOL_PHY_DOWNSHIFT
:
2502 if (tuna
->len
!= sizeof(u8
) ||
2503 tuna
->type_id
!= ETHTOOL_TUNABLE_U8
)
2513 static int get_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2516 struct ethtool_tunable tuna
;
2517 struct phy_device
*phydev
= dev
->phydev
;
2520 if (!(phydev
&& phydev
->drv
&& phydev
->drv
->get_tunable
))
2523 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2525 ret
= ethtool_phy_tunable_valid(&tuna
);
2528 data
= kmalloc(tuna
.len
, GFP_USER
);
2531 mutex_lock(&phydev
->lock
);
2532 ret
= phydev
->drv
->get_tunable(phydev
, &tuna
, data
);
2533 mutex_unlock(&phydev
->lock
);
2536 useraddr
+= sizeof(tuna
);
2538 if (copy_to_user(useraddr
, data
, tuna
.len
))
2547 static int set_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2550 struct ethtool_tunable tuna
;
2551 struct phy_device
*phydev
= dev
->phydev
;
2554 if (!(phydev
&& phydev
->drv
&& phydev
->drv
->set_tunable
))
2556 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2558 ret
= ethtool_phy_tunable_valid(&tuna
);
2561 useraddr
+= sizeof(tuna
);
2562 data
= memdup_user(useraddr
, tuna
.len
);
2564 return PTR_ERR(data
);
2565 mutex_lock(&phydev
->lock
);
2566 ret
= phydev
->drv
->set_tunable(phydev
, &tuna
, data
);
2567 mutex_unlock(&phydev
->lock
);
2573 static int ethtool_get_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2575 struct ethtool_fecparam fecparam
= { ETHTOOL_GFECPARAM
};
2578 if (!dev
->ethtool_ops
->get_fecparam
)
2581 rc
= dev
->ethtool_ops
->get_fecparam(dev
, &fecparam
);
2585 if (copy_to_user(useraddr
, &fecparam
, sizeof(fecparam
)))
2590 static int ethtool_set_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2592 struct ethtool_fecparam fecparam
;
2594 if (!dev
->ethtool_ops
->set_fecparam
)
2597 if (copy_from_user(&fecparam
, useraddr
, sizeof(fecparam
)))
2600 return dev
->ethtool_ops
->set_fecparam(dev
, &fecparam
);
2603 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2605 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
2607 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
2608 void __user
*useraddr
= ifr
->ifr_data
;
2609 u32 ethcmd
, sub_cmd
;
2611 netdev_features_t old_features
;
2613 if (!dev
|| !netif_device_present(dev
))
2616 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
2619 if (ethcmd
== ETHTOOL_PERQUEUE
) {
2620 if (copy_from_user(&sub_cmd
, useraddr
+ sizeof(ethcmd
), sizeof(sub_cmd
)))
2625 /* Allow some commands to be done by anyone */
2628 case ETHTOOL_GDRVINFO
:
2629 case ETHTOOL_GMSGLVL
:
2631 case ETHTOOL_GCOALESCE
:
2632 case ETHTOOL_GRINGPARAM
:
2633 case ETHTOOL_GPAUSEPARAM
:
2634 case ETHTOOL_GRXCSUM
:
2635 case ETHTOOL_GTXCSUM
:
2637 case ETHTOOL_GSSET_INFO
:
2638 case ETHTOOL_GSTRINGS
:
2639 case ETHTOOL_GSTATS
:
2640 case ETHTOOL_GPHYSTATS
:
2642 case ETHTOOL_GPERMADDR
:
2645 case ETHTOOL_GFLAGS
:
2646 case ETHTOOL_GPFLAGS
:
2648 case ETHTOOL_GRXRINGS
:
2649 case ETHTOOL_GRXCLSRLCNT
:
2650 case ETHTOOL_GRXCLSRULE
:
2651 case ETHTOOL_GRXCLSRLALL
:
2652 case ETHTOOL_GRXFHINDIR
:
2654 case ETHTOOL_GFEATURES
:
2655 case ETHTOOL_GCHANNELS
:
2656 case ETHTOOL_GET_TS_INFO
:
2658 case ETHTOOL_GTUNABLE
:
2659 case ETHTOOL_PHY_GTUNABLE
:
2660 case ETHTOOL_GLINKSETTINGS
:
2661 case ETHTOOL_GFECPARAM
:
2664 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2668 if (dev
->ethtool_ops
->begin
) {
2669 rc
= dev
->ethtool_ops
->begin(dev
);
2673 old_features
= dev
->features
;
2677 rc
= ethtool_get_settings(dev
, useraddr
);
2680 rc
= ethtool_set_settings(dev
, useraddr
);
2682 case ETHTOOL_GDRVINFO
:
2683 rc
= ethtool_get_drvinfo(dev
, useraddr
);
2686 rc
= ethtool_get_regs(dev
, useraddr
);
2689 rc
= ethtool_get_wol(dev
, useraddr
);
2692 rc
= ethtool_set_wol(dev
, useraddr
);
2694 case ETHTOOL_GMSGLVL
:
2695 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2696 dev
->ethtool_ops
->get_msglevel
);
2698 case ETHTOOL_SMSGLVL
:
2699 rc
= ethtool_set_value_void(dev
, useraddr
,
2700 dev
->ethtool_ops
->set_msglevel
);
2703 rc
= ethtool_get_eee(dev
, useraddr
);
2706 rc
= ethtool_set_eee(dev
, useraddr
);
2708 case ETHTOOL_NWAY_RST
:
2709 rc
= ethtool_nway_reset(dev
);
2712 rc
= ethtool_get_link(dev
, useraddr
);
2714 case ETHTOOL_GEEPROM
:
2715 rc
= ethtool_get_eeprom(dev
, useraddr
);
2717 case ETHTOOL_SEEPROM
:
2718 rc
= ethtool_set_eeprom(dev
, useraddr
);
2720 case ETHTOOL_GCOALESCE
:
2721 rc
= ethtool_get_coalesce(dev
, useraddr
);
2723 case ETHTOOL_SCOALESCE
:
2724 rc
= ethtool_set_coalesce(dev
, useraddr
);
2726 case ETHTOOL_GRINGPARAM
:
2727 rc
= ethtool_get_ringparam(dev
, useraddr
);
2729 case ETHTOOL_SRINGPARAM
:
2730 rc
= ethtool_set_ringparam(dev
, useraddr
);
2732 case ETHTOOL_GPAUSEPARAM
:
2733 rc
= ethtool_get_pauseparam(dev
, useraddr
);
2735 case ETHTOOL_SPAUSEPARAM
:
2736 rc
= ethtool_set_pauseparam(dev
, useraddr
);
2739 rc
= ethtool_self_test(dev
, useraddr
);
2741 case ETHTOOL_GSTRINGS
:
2742 rc
= ethtool_get_strings(dev
, useraddr
);
2744 case ETHTOOL_PHYS_ID
:
2745 rc
= ethtool_phys_id(dev
, useraddr
);
2747 case ETHTOOL_GSTATS
:
2748 rc
= ethtool_get_stats(dev
, useraddr
);
2750 case ETHTOOL_GPERMADDR
:
2751 rc
= ethtool_get_perm_addr(dev
, useraddr
);
2753 case ETHTOOL_GFLAGS
:
2754 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2755 __ethtool_get_flags
);
2757 case ETHTOOL_SFLAGS
:
2758 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
2760 case ETHTOOL_GPFLAGS
:
2761 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2762 dev
->ethtool_ops
->get_priv_flags
);
2764 case ETHTOOL_SPFLAGS
:
2765 rc
= ethtool_set_value(dev
, useraddr
,
2766 dev
->ethtool_ops
->set_priv_flags
);
2769 case ETHTOOL_GRXRINGS
:
2770 case ETHTOOL_GRXCLSRLCNT
:
2771 case ETHTOOL_GRXCLSRULE
:
2772 case ETHTOOL_GRXCLSRLALL
:
2773 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
2776 case ETHTOOL_SRXCLSRLDEL
:
2777 case ETHTOOL_SRXCLSRLINS
:
2778 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
2780 case ETHTOOL_FLASHDEV
:
2781 rc
= ethtool_flash_device(dev
, useraddr
);
2784 rc
= ethtool_reset(dev
, useraddr
);
2786 case ETHTOOL_GSSET_INFO
:
2787 rc
= ethtool_get_sset_info(dev
, useraddr
);
2789 case ETHTOOL_GRXFHINDIR
:
2790 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
2792 case ETHTOOL_SRXFHINDIR
:
2793 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
2796 rc
= ethtool_get_rxfh(dev
, useraddr
);
2799 rc
= ethtool_set_rxfh(dev
, useraddr
);
2801 case ETHTOOL_GFEATURES
:
2802 rc
= ethtool_get_features(dev
, useraddr
);
2804 case ETHTOOL_SFEATURES
:
2805 rc
= ethtool_set_features(dev
, useraddr
);
2807 case ETHTOOL_GTXCSUM
:
2808 case ETHTOOL_GRXCSUM
:
2813 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
2815 case ETHTOOL_STXCSUM
:
2816 case ETHTOOL_SRXCSUM
:
2821 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
2823 case ETHTOOL_GCHANNELS
:
2824 rc
= ethtool_get_channels(dev
, useraddr
);
2826 case ETHTOOL_SCHANNELS
:
2827 rc
= ethtool_set_channels(dev
, useraddr
);
2829 case ETHTOOL_SET_DUMP
:
2830 rc
= ethtool_set_dump(dev
, useraddr
);
2832 case ETHTOOL_GET_DUMP_FLAG
:
2833 rc
= ethtool_get_dump_flag(dev
, useraddr
);
2835 case ETHTOOL_GET_DUMP_DATA
:
2836 rc
= ethtool_get_dump_data(dev
, useraddr
);
2838 case ETHTOOL_GET_TS_INFO
:
2839 rc
= ethtool_get_ts_info(dev
, useraddr
);
2841 case ETHTOOL_GMODULEINFO
:
2842 rc
= ethtool_get_module_info(dev
, useraddr
);
2844 case ETHTOOL_GMODULEEEPROM
:
2845 rc
= ethtool_get_module_eeprom(dev
, useraddr
);
2847 case ETHTOOL_GTUNABLE
:
2848 rc
= ethtool_get_tunable(dev
, useraddr
);
2850 case ETHTOOL_STUNABLE
:
2851 rc
= ethtool_set_tunable(dev
, useraddr
);
2853 case ETHTOOL_GPHYSTATS
:
2854 rc
= ethtool_get_phy_stats(dev
, useraddr
);
2856 case ETHTOOL_PERQUEUE
:
2857 rc
= ethtool_set_per_queue(dev
, useraddr
);
2859 case ETHTOOL_GLINKSETTINGS
:
2860 rc
= ethtool_get_link_ksettings(dev
, useraddr
);
2862 case ETHTOOL_SLINKSETTINGS
:
2863 rc
= ethtool_set_link_ksettings(dev
, useraddr
);
2865 case ETHTOOL_PHY_GTUNABLE
:
2866 rc
= get_phy_tunable(dev
, useraddr
);
2868 case ETHTOOL_PHY_STUNABLE
:
2869 rc
= set_phy_tunable(dev
, useraddr
);
2871 case ETHTOOL_GFECPARAM
:
2872 rc
= ethtool_get_fecparam(dev
, useraddr
);
2874 case ETHTOOL_SFECPARAM
:
2875 rc
= ethtool_set_fecparam(dev
, useraddr
);
2881 if (dev
->ethtool_ops
->complete
)
2882 dev
->ethtool_ops
->complete(dev
);
2884 if (old_features
!= dev
->features
)
2885 netdev_features_change(dev
);