2 * net/core/ethtool.c - Ethtool ioctl handler
3 * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
5 * This file is where we call all the ethtool_ops commands to get
6 * the information ethtool needs.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/capability.h>
17 #include <linux/errno.h>
18 #include <linux/ethtool.h>
19 #include <linux/netdevice.h>
20 #include <linux/net_tstamp.h>
21 #include <linux/phy.h>
22 #include <linux/bitops.h>
23 #include <linux/uaccess.h>
24 #include <linux/vmalloc.h>
25 #include <linux/sfp.h>
26 #include <linux/slab.h>
27 #include <linux/rtnetlink.h>
28 #include <linux/sched/signal.h>
29 #include <linux/net.h>
32 * Some useful ethtool_ops methods that're device independent.
33 * If we find that all drivers want to do the same thing here,
34 * we can turn these into dev_() function calls.
37 u32
ethtool_op_get_link(struct net_device
*dev
)
39 return netif_carrier_ok(dev
) ? 1 : 0;
41 EXPORT_SYMBOL(ethtool_op_get_link
);
43 int ethtool_op_get_ts_info(struct net_device
*dev
, struct ethtool_ts_info
*info
)
45 info
->so_timestamping
=
46 SOF_TIMESTAMPING_TX_SOFTWARE
|
47 SOF_TIMESTAMPING_RX_SOFTWARE
|
48 SOF_TIMESTAMPING_SOFTWARE
;
52 EXPORT_SYMBOL(ethtool_op_get_ts_info
);
54 /* Handlers for each ethtool command */
56 #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
58 static const char netdev_features_strings
[NETDEV_FEATURE_COUNT
][ETH_GSTRING_LEN
] = {
59 [NETIF_F_SG_BIT
] = "tx-scatter-gather",
60 [NETIF_F_IP_CSUM_BIT
] = "tx-checksum-ipv4",
61 [NETIF_F_HW_CSUM_BIT
] = "tx-checksum-ip-generic",
62 [NETIF_F_IPV6_CSUM_BIT
] = "tx-checksum-ipv6",
63 [NETIF_F_HIGHDMA_BIT
] = "highdma",
64 [NETIF_F_FRAGLIST_BIT
] = "tx-scatter-gather-fraglist",
65 [NETIF_F_HW_VLAN_CTAG_TX_BIT
] = "tx-vlan-hw-insert",
67 [NETIF_F_HW_VLAN_CTAG_RX_BIT
] = "rx-vlan-hw-parse",
68 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT
] = "rx-vlan-filter",
69 [NETIF_F_HW_VLAN_STAG_TX_BIT
] = "tx-vlan-stag-hw-insert",
70 [NETIF_F_HW_VLAN_STAG_RX_BIT
] = "rx-vlan-stag-hw-parse",
71 [NETIF_F_HW_VLAN_STAG_FILTER_BIT
] = "rx-vlan-stag-filter",
72 [NETIF_F_VLAN_CHALLENGED_BIT
] = "vlan-challenged",
73 [NETIF_F_GSO_BIT
] = "tx-generic-segmentation",
74 [NETIF_F_LLTX_BIT
] = "tx-lockless",
75 [NETIF_F_NETNS_LOCAL_BIT
] = "netns-local",
76 [NETIF_F_GRO_BIT
] = "rx-gro",
77 [NETIF_F_GRO_HW_BIT
] = "rx-gro-hw",
78 [NETIF_F_LRO_BIT
] = "rx-lro",
80 [NETIF_F_TSO_BIT
] = "tx-tcp-segmentation",
81 [NETIF_F_GSO_ROBUST_BIT
] = "tx-gso-robust",
82 [NETIF_F_TSO_ECN_BIT
] = "tx-tcp-ecn-segmentation",
83 [NETIF_F_TSO_MANGLEID_BIT
] = "tx-tcp-mangleid-segmentation",
84 [NETIF_F_TSO6_BIT
] = "tx-tcp6-segmentation",
85 [NETIF_F_FSO_BIT
] = "tx-fcoe-segmentation",
86 [NETIF_F_GSO_GRE_BIT
] = "tx-gre-segmentation",
87 [NETIF_F_GSO_GRE_CSUM_BIT
] = "tx-gre-csum-segmentation",
88 [NETIF_F_GSO_IPXIP4_BIT
] = "tx-ipxip4-segmentation",
89 [NETIF_F_GSO_IPXIP6_BIT
] = "tx-ipxip6-segmentation",
90 [NETIF_F_GSO_UDP_TUNNEL_BIT
] = "tx-udp_tnl-segmentation",
91 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT
] = "tx-udp_tnl-csum-segmentation",
92 [NETIF_F_GSO_PARTIAL_BIT
] = "tx-gso-partial",
93 [NETIF_F_GSO_SCTP_BIT
] = "tx-sctp-segmentation",
94 [NETIF_F_GSO_ESP_BIT
] = "tx-esp-segmentation",
95 [NETIF_F_GSO_UDP_L4_BIT
] = "tx-udp-segmentation",
97 [NETIF_F_FCOE_CRC_BIT
] = "tx-checksum-fcoe-crc",
98 [NETIF_F_SCTP_CRC_BIT
] = "tx-checksum-sctp",
99 [NETIF_F_FCOE_MTU_BIT
] = "fcoe-mtu",
100 [NETIF_F_NTUPLE_BIT
] = "rx-ntuple-filter",
101 [NETIF_F_RXHASH_BIT
] = "rx-hashing",
102 [NETIF_F_RXCSUM_BIT
] = "rx-checksum",
103 [NETIF_F_NOCACHE_COPY_BIT
] = "tx-nocache-copy",
104 [NETIF_F_LOOPBACK_BIT
] = "loopback",
105 [NETIF_F_RXFCS_BIT
] = "rx-fcs",
106 [NETIF_F_RXALL_BIT
] = "rx-all",
107 [NETIF_F_HW_L2FW_DOFFLOAD_BIT
] = "l2-fwd-offload",
108 [NETIF_F_HW_TC_BIT
] = "hw-tc-offload",
109 [NETIF_F_HW_ESP_BIT
] = "esp-hw-offload",
110 [NETIF_F_HW_ESP_TX_CSUM_BIT
] = "esp-tx-csum-hw-offload",
111 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT
] = "rx-udp_tunnel-port-offload",
112 [NETIF_F_HW_TLS_RECORD_BIT
] = "tls-hw-record",
113 [NETIF_F_HW_TLS_TX_BIT
] = "tls-hw-tx-offload",
114 [NETIF_F_HW_TLS_RX_BIT
] = "tls-hw-rx-offload",
118 rss_hash_func_strings
[ETH_RSS_HASH_FUNCS_COUNT
][ETH_GSTRING_LEN
] = {
119 [ETH_RSS_HASH_TOP_BIT
] = "toeplitz",
120 [ETH_RSS_HASH_XOR_BIT
] = "xor",
121 [ETH_RSS_HASH_CRC32_BIT
] = "crc32",
125 tunable_strings
[__ETHTOOL_TUNABLE_COUNT
][ETH_GSTRING_LEN
] = {
126 [ETHTOOL_ID_UNSPEC
] = "Unspec",
127 [ETHTOOL_RX_COPYBREAK
] = "rx-copybreak",
128 [ETHTOOL_TX_COPYBREAK
] = "tx-copybreak",
129 [ETHTOOL_PFC_PREVENTION_TOUT
] = "pfc-prevention-tout",
133 phy_tunable_strings
[__ETHTOOL_PHY_TUNABLE_COUNT
][ETH_GSTRING_LEN
] = {
134 [ETHTOOL_ID_UNSPEC
] = "Unspec",
135 [ETHTOOL_PHY_DOWNSHIFT
] = "phy-downshift",
138 static int ethtool_get_features(struct net_device
*dev
, void __user
*useraddr
)
140 struct ethtool_gfeatures cmd
= {
141 .cmd
= ETHTOOL_GFEATURES
,
142 .size
= ETHTOOL_DEV_FEATURE_WORDS
,
144 struct ethtool_get_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
145 u32 __user
*sizeaddr
;
149 /* in case feature bits run out again */
150 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS
* sizeof(u32
) > sizeof(netdev_features_t
));
152 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
153 features
[i
].available
= (u32
)(dev
->hw_features
>> (32 * i
));
154 features
[i
].requested
= (u32
)(dev
->wanted_features
>> (32 * i
));
155 features
[i
].active
= (u32
)(dev
->features
>> (32 * i
));
156 features
[i
].never_changed
=
157 (u32
)(NETIF_F_NEVER_CHANGE
>> (32 * i
));
160 sizeaddr
= useraddr
+ offsetof(struct ethtool_gfeatures
, size
);
161 if (get_user(copy_size
, sizeaddr
))
164 if (copy_size
> ETHTOOL_DEV_FEATURE_WORDS
)
165 copy_size
= ETHTOOL_DEV_FEATURE_WORDS
;
167 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
169 useraddr
+= sizeof(cmd
);
170 if (copy_to_user(useraddr
, features
, copy_size
* sizeof(*features
)))
176 static int ethtool_set_features(struct net_device
*dev
, void __user
*useraddr
)
178 struct ethtool_sfeatures cmd
;
179 struct ethtool_set_features_block features
[ETHTOOL_DEV_FEATURE_WORDS
];
180 netdev_features_t wanted
= 0, valid
= 0;
183 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
185 useraddr
+= sizeof(cmd
);
187 if (cmd
.size
!= ETHTOOL_DEV_FEATURE_WORDS
)
190 if (copy_from_user(features
, useraddr
, sizeof(features
)))
193 for (i
= 0; i
< ETHTOOL_DEV_FEATURE_WORDS
; ++i
) {
194 valid
|= (netdev_features_t
)features
[i
].valid
<< (32 * i
);
195 wanted
|= (netdev_features_t
)features
[i
].requested
<< (32 * i
);
198 if (valid
& ~NETIF_F_ETHTOOL_BITS
)
201 if (valid
& ~dev
->hw_features
) {
202 valid
&= dev
->hw_features
;
203 ret
|= ETHTOOL_F_UNSUPPORTED
;
206 dev
->wanted_features
&= ~valid
;
207 dev
->wanted_features
|= wanted
& valid
;
208 __netdev_update_features(dev
);
210 if ((dev
->wanted_features
^ dev
->features
) & valid
)
211 ret
|= ETHTOOL_F_WISH
;
216 static int __ethtool_get_sset_count(struct net_device
*dev
, int sset
)
218 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
220 if (sset
== ETH_SS_FEATURES
)
221 return ARRAY_SIZE(netdev_features_strings
);
223 if (sset
== ETH_SS_RSS_HASH_FUNCS
)
224 return ARRAY_SIZE(rss_hash_func_strings
);
226 if (sset
== ETH_SS_TUNABLES
)
227 return ARRAY_SIZE(tunable_strings
);
229 if (sset
== ETH_SS_PHY_TUNABLES
)
230 return ARRAY_SIZE(phy_tunable_strings
);
232 if (sset
== ETH_SS_PHY_STATS
&& dev
->phydev
&&
233 !ops
->get_ethtool_phy_stats
)
234 return phy_ethtool_get_sset_count(dev
->phydev
);
236 if (ops
->get_sset_count
&& ops
->get_strings
)
237 return ops
->get_sset_count(dev
, sset
);
242 static void __ethtool_get_strings(struct net_device
*dev
,
243 u32 stringset
, u8
*data
)
245 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
247 if (stringset
== ETH_SS_FEATURES
)
248 memcpy(data
, netdev_features_strings
,
249 sizeof(netdev_features_strings
));
250 else if (stringset
== ETH_SS_RSS_HASH_FUNCS
)
251 memcpy(data
, rss_hash_func_strings
,
252 sizeof(rss_hash_func_strings
));
253 else if (stringset
== ETH_SS_TUNABLES
)
254 memcpy(data
, tunable_strings
, sizeof(tunable_strings
));
255 else if (stringset
== ETH_SS_PHY_TUNABLES
)
256 memcpy(data
, phy_tunable_strings
, sizeof(phy_tunable_strings
));
257 else if (stringset
== ETH_SS_PHY_STATS
&& dev
->phydev
&&
258 !ops
->get_ethtool_phy_stats
)
259 phy_ethtool_get_strings(dev
->phydev
, data
);
261 /* ops->get_strings is valid because checked earlier */
262 ops
->get_strings(dev
, stringset
, data
);
265 static netdev_features_t
ethtool_get_feature_mask(u32 eth_cmd
)
267 /* feature masks of legacy discrete ethtool ops */
270 case ETHTOOL_GTXCSUM
:
271 case ETHTOOL_STXCSUM
:
272 return NETIF_F_CSUM_MASK
| NETIF_F_SCTP_CRC
;
273 case ETHTOOL_GRXCSUM
:
274 case ETHTOOL_SRXCSUM
:
275 return NETIF_F_RXCSUM
;
281 return NETIF_F_ALL_TSO
;
293 static int ethtool_get_one_feature(struct net_device
*dev
,
294 char __user
*useraddr
, u32 ethcmd
)
296 netdev_features_t mask
= ethtool_get_feature_mask(ethcmd
);
297 struct ethtool_value edata
= {
299 .data
= !!(dev
->features
& mask
),
302 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
307 static int ethtool_set_one_feature(struct net_device
*dev
,
308 void __user
*useraddr
, u32 ethcmd
)
310 struct ethtool_value edata
;
311 netdev_features_t mask
;
313 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
316 mask
= ethtool_get_feature_mask(ethcmd
);
317 mask
&= dev
->hw_features
;
322 dev
->wanted_features
|= mask
;
324 dev
->wanted_features
&= ~mask
;
326 __netdev_update_features(dev
);
331 #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
332 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
333 #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
334 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
337 static u32
__ethtool_get_flags(struct net_device
*dev
)
341 if (dev
->features
& NETIF_F_LRO
)
342 flags
|= ETH_FLAG_LRO
;
343 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_RX
)
344 flags
|= ETH_FLAG_RXVLAN
;
345 if (dev
->features
& NETIF_F_HW_VLAN_CTAG_TX
)
346 flags
|= ETH_FLAG_TXVLAN
;
347 if (dev
->features
& NETIF_F_NTUPLE
)
348 flags
|= ETH_FLAG_NTUPLE
;
349 if (dev
->features
& NETIF_F_RXHASH
)
350 flags
|= ETH_FLAG_RXHASH
;
355 static int __ethtool_set_flags(struct net_device
*dev
, u32 data
)
357 netdev_features_t features
= 0, changed
;
359 if (data
& ~ETH_ALL_FLAGS
)
362 if (data
& ETH_FLAG_LRO
)
363 features
|= NETIF_F_LRO
;
364 if (data
& ETH_FLAG_RXVLAN
)
365 features
|= NETIF_F_HW_VLAN_CTAG_RX
;
366 if (data
& ETH_FLAG_TXVLAN
)
367 features
|= NETIF_F_HW_VLAN_CTAG_TX
;
368 if (data
& ETH_FLAG_NTUPLE
)
369 features
|= NETIF_F_NTUPLE
;
370 if (data
& ETH_FLAG_RXHASH
)
371 features
|= NETIF_F_RXHASH
;
373 /* allow changing only bits set in hw_features */
374 changed
= (features
^ dev
->features
) & ETH_ALL_FEATURES
;
375 if (changed
& ~dev
->hw_features
)
376 return (changed
& dev
->hw_features
) ? -EINVAL
: -EOPNOTSUPP
;
378 dev
->wanted_features
=
379 (dev
->wanted_features
& ~changed
) | (features
& changed
);
381 __netdev_update_features(dev
);
386 /* Given two link masks, AND them together and save the result in dst. */
387 void ethtool_intersect_link_masks(struct ethtool_link_ksettings
*dst
,
388 struct ethtool_link_ksettings
*src
)
390 unsigned int size
= BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS
);
391 unsigned int idx
= 0;
393 for (; idx
< size
; idx
++) {
394 dst
->link_modes
.supported
[idx
] &=
395 src
->link_modes
.supported
[idx
];
396 dst
->link_modes
.advertising
[idx
] &=
397 src
->link_modes
.advertising
[idx
];
400 EXPORT_SYMBOL(ethtool_intersect_link_masks
);
402 void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst
,
405 bitmap_zero(dst
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
408 EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode
);
410 /* return false if src had higher bits set. lower bits always updated. */
411 bool ethtool_convert_link_mode_to_legacy_u32(u32
*legacy_u32
,
412 const unsigned long *src
)
416 /* TODO: following test will soon always be true */
417 if (__ETHTOOL_LINK_MODE_MASK_NBITS
> 32) {
418 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext
);
420 bitmap_zero(ext
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
421 bitmap_fill(ext
, 32);
422 bitmap_complement(ext
, ext
, __ETHTOOL_LINK_MODE_MASK_NBITS
);
423 if (bitmap_intersects(ext
, src
,
424 __ETHTOOL_LINK_MODE_MASK_NBITS
)) {
425 /* src mask goes beyond bit 31 */
429 *legacy_u32
= src
[0];
432 EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32
);
434 /* return false if legacy contained non-0 deprecated fields
435 * maxtxpkt/maxrxpkt. rest of ksettings always updated
438 convert_legacy_settings_to_link_ksettings(
439 struct ethtool_link_ksettings
*link_ksettings
,
440 const struct ethtool_cmd
*legacy_settings
)
444 memset(link_ksettings
, 0, sizeof(*link_ksettings
));
446 /* This is used to tell users that driver is still using these
447 * deprecated legacy fields, and they should not use
448 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
450 if (legacy_settings
->maxtxpkt
||
451 legacy_settings
->maxrxpkt
)
454 ethtool_convert_legacy_u32_to_link_mode(
455 link_ksettings
->link_modes
.supported
,
456 legacy_settings
->supported
);
457 ethtool_convert_legacy_u32_to_link_mode(
458 link_ksettings
->link_modes
.advertising
,
459 legacy_settings
->advertising
);
460 ethtool_convert_legacy_u32_to_link_mode(
461 link_ksettings
->link_modes
.lp_advertising
,
462 legacy_settings
->lp_advertising
);
463 link_ksettings
->base
.speed
464 = ethtool_cmd_speed(legacy_settings
);
465 link_ksettings
->base
.duplex
466 = legacy_settings
->duplex
;
467 link_ksettings
->base
.port
468 = legacy_settings
->port
;
469 link_ksettings
->base
.phy_address
470 = legacy_settings
->phy_address
;
471 link_ksettings
->base
.autoneg
472 = legacy_settings
->autoneg
;
473 link_ksettings
->base
.mdio_support
474 = legacy_settings
->mdio_support
;
475 link_ksettings
->base
.eth_tp_mdix
476 = legacy_settings
->eth_tp_mdix
;
477 link_ksettings
->base
.eth_tp_mdix_ctrl
478 = legacy_settings
->eth_tp_mdix_ctrl
;
482 /* return false if ksettings link modes had higher bits
483 * set. legacy_settings always updated (best effort)
486 convert_link_ksettings_to_legacy_settings(
487 struct ethtool_cmd
*legacy_settings
,
488 const struct ethtool_link_ksettings
*link_ksettings
)
492 memset(legacy_settings
, 0, sizeof(*legacy_settings
));
493 /* this also clears the deprecated fields in legacy structure:
499 retval
&= ethtool_convert_link_mode_to_legacy_u32(
500 &legacy_settings
->supported
,
501 link_ksettings
->link_modes
.supported
);
502 retval
&= ethtool_convert_link_mode_to_legacy_u32(
503 &legacy_settings
->advertising
,
504 link_ksettings
->link_modes
.advertising
);
505 retval
&= ethtool_convert_link_mode_to_legacy_u32(
506 &legacy_settings
->lp_advertising
,
507 link_ksettings
->link_modes
.lp_advertising
);
508 ethtool_cmd_speed_set(legacy_settings
, link_ksettings
->base
.speed
);
509 legacy_settings
->duplex
510 = link_ksettings
->base
.duplex
;
511 legacy_settings
->port
512 = link_ksettings
->base
.port
;
513 legacy_settings
->phy_address
514 = link_ksettings
->base
.phy_address
;
515 legacy_settings
->autoneg
516 = link_ksettings
->base
.autoneg
;
517 legacy_settings
->mdio_support
518 = link_ksettings
->base
.mdio_support
;
519 legacy_settings
->eth_tp_mdix
520 = link_ksettings
->base
.eth_tp_mdix
;
521 legacy_settings
->eth_tp_mdix_ctrl
522 = link_ksettings
->base
.eth_tp_mdix_ctrl
;
523 legacy_settings
->transceiver
524 = link_ksettings
->base
.transceiver
;
528 /* number of 32-bit words to store the user's link mode bitmaps */
529 #define __ETHTOOL_LINK_MODE_MASK_NU32 \
530 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
532 /* layout of the struct passed from/to userland */
533 struct ethtool_link_usettings
{
534 struct ethtool_link_settings base
;
536 __u32 supported
[__ETHTOOL_LINK_MODE_MASK_NU32
];
537 __u32 advertising
[__ETHTOOL_LINK_MODE_MASK_NU32
];
538 __u32 lp_advertising
[__ETHTOOL_LINK_MODE_MASK_NU32
];
542 /* Internal kernel helper to query a device ethtool_link_settings.
544 * Backward compatibility note: for compatibility with legacy drivers
545 * that implement only the ethtool_cmd API, this has to work with both
546 * drivers implementing get_link_ksettings API and drivers
547 * implementing get_settings API. When drivers implement get_settings
548 * and report ethtool_cmd deprecated fields
549 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
550 * because the resulting struct ethtool_link_settings does not report them.
552 int __ethtool_get_link_ksettings(struct net_device
*dev
,
553 struct ethtool_link_ksettings
*link_ksettings
)
556 struct ethtool_cmd cmd
;
560 if (dev
->ethtool_ops
->get_link_ksettings
) {
561 memset(link_ksettings
, 0, sizeof(*link_ksettings
));
562 return dev
->ethtool_ops
->get_link_ksettings(dev
,
566 /* driver doesn't support %ethtool_link_ksettings API. revert to
567 * legacy %ethtool_cmd API, unless it's not supported either.
568 * TODO: remove when ethtool_ops::get_settings disappears internally
570 if (!dev
->ethtool_ops
->get_settings
)
573 memset(&cmd
, 0, sizeof(cmd
));
574 cmd
.cmd
= ETHTOOL_GSET
;
575 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
579 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
581 convert_legacy_settings_to_link_ksettings(link_ksettings
, &cmd
);
584 EXPORT_SYMBOL(__ethtool_get_link_ksettings
);
586 /* convert ethtool_link_usettings in user space to a kernel internal
587 * ethtool_link_ksettings. return 0 on success, errno on error.
589 static int load_link_ksettings_from_user(struct ethtool_link_ksettings
*to
,
590 const void __user
*from
)
592 struct ethtool_link_usettings link_usettings
;
594 if (copy_from_user(&link_usettings
, from
, sizeof(link_usettings
)))
597 memcpy(&to
->base
, &link_usettings
.base
, sizeof(to
->base
));
598 bitmap_from_arr32(to
->link_modes
.supported
,
599 link_usettings
.link_modes
.supported
,
600 __ETHTOOL_LINK_MODE_MASK_NBITS
);
601 bitmap_from_arr32(to
->link_modes
.advertising
,
602 link_usettings
.link_modes
.advertising
,
603 __ETHTOOL_LINK_MODE_MASK_NBITS
);
604 bitmap_from_arr32(to
->link_modes
.lp_advertising
,
605 link_usettings
.link_modes
.lp_advertising
,
606 __ETHTOOL_LINK_MODE_MASK_NBITS
);
611 /* convert a kernel internal ethtool_link_ksettings to
612 * ethtool_link_usettings in user space. return 0 on success, errno on
616 store_link_ksettings_for_user(void __user
*to
,
617 const struct ethtool_link_ksettings
*from
)
619 struct ethtool_link_usettings link_usettings
;
621 memcpy(&link_usettings
.base
, &from
->base
, sizeof(link_usettings
));
622 bitmap_to_arr32(link_usettings
.link_modes
.supported
,
623 from
->link_modes
.supported
,
624 __ETHTOOL_LINK_MODE_MASK_NBITS
);
625 bitmap_to_arr32(link_usettings
.link_modes
.advertising
,
626 from
->link_modes
.advertising
,
627 __ETHTOOL_LINK_MODE_MASK_NBITS
);
628 bitmap_to_arr32(link_usettings
.link_modes
.lp_advertising
,
629 from
->link_modes
.lp_advertising
,
630 __ETHTOOL_LINK_MODE_MASK_NBITS
);
632 if (copy_to_user(to
, &link_usettings
, sizeof(link_usettings
)))
638 /* Query device for its ethtool_link_settings.
640 * Backward compatibility note: this function must fail when driver
641 * does not implement ethtool::get_link_ksettings, even if legacy
642 * ethtool_ops::get_settings is implemented. This tells new versions
643 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
644 * this driver, so that they can correctly access the ethtool_cmd
645 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
646 * implements ethtool_ops::get_settings anymore.
648 static int ethtool_get_link_ksettings(struct net_device
*dev
,
649 void __user
*useraddr
)
652 struct ethtool_link_ksettings link_ksettings
;
656 if (!dev
->ethtool_ops
->get_link_ksettings
)
659 /* handle bitmap nbits handshake */
660 if (copy_from_user(&link_ksettings
.base
, useraddr
,
661 sizeof(link_ksettings
.base
)))
664 if (__ETHTOOL_LINK_MODE_MASK_NU32
665 != link_ksettings
.base
.link_mode_masks_nwords
) {
666 /* wrong link mode nbits requested */
667 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
668 link_ksettings
.base
.cmd
= ETHTOOL_GLINKSETTINGS
;
669 /* send back number of words required as negative val */
670 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32
<= S8_MAX
,
671 "need too many bits for link modes!");
672 link_ksettings
.base
.link_mode_masks_nwords
673 = -((s8
)__ETHTOOL_LINK_MODE_MASK_NU32
);
675 /* copy the base fields back to user, not the link
678 if (copy_to_user(useraddr
, &link_ksettings
.base
,
679 sizeof(link_ksettings
.base
)))
685 /* handshake successful: user/kernel agree on
686 * link_mode_masks_nwords
689 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
690 err
= dev
->ethtool_ops
->get_link_ksettings(dev
, &link_ksettings
);
694 /* make sure we tell the right values to user */
695 link_ksettings
.base
.cmd
= ETHTOOL_GLINKSETTINGS
;
696 link_ksettings
.base
.link_mode_masks_nwords
697 = __ETHTOOL_LINK_MODE_MASK_NU32
;
699 return store_link_ksettings_for_user(useraddr
, &link_ksettings
);
702 /* Update device ethtool_link_settings.
704 * Backward compatibility note: this function must fail when driver
705 * does not implement ethtool::set_link_ksettings, even if legacy
706 * ethtool_ops::set_settings is implemented. This tells new versions
707 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
708 * this driver, so that they can correctly update the ethtool_cmd
709 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
710 * implements ethtool_ops::get_settings anymore.
712 static int ethtool_set_link_ksettings(struct net_device
*dev
,
713 void __user
*useraddr
)
716 struct ethtool_link_ksettings link_ksettings
;
720 if (!dev
->ethtool_ops
->set_link_ksettings
)
723 /* make sure nbits field has expected value */
724 if (copy_from_user(&link_ksettings
.base
, useraddr
,
725 sizeof(link_ksettings
.base
)))
728 if (__ETHTOOL_LINK_MODE_MASK_NU32
729 != link_ksettings
.base
.link_mode_masks_nwords
)
732 /* copy the whole structure, now that we know it has expected
735 err
= load_link_ksettings_from_user(&link_ksettings
, useraddr
);
739 /* re-check nwords field, just in case */
740 if (__ETHTOOL_LINK_MODE_MASK_NU32
741 != link_ksettings
.base
.link_mode_masks_nwords
)
744 return dev
->ethtool_ops
->set_link_ksettings(dev
, &link_ksettings
);
747 /* Query device for its ethtool_cmd settings.
749 * Backward compatibility note: for compatibility with legacy ethtool,
750 * this has to work with both drivers implementing get_link_ksettings
751 * API and drivers implementing get_settings API. When drivers
752 * implement get_link_ksettings and report higher link mode bits, a
753 * kernel warning is logged once (with name of 1st driver/device) to
754 * recommend user to upgrade ethtool, but the command is successful
755 * (only the lower link mode bits reported back to user).
757 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
759 struct ethtool_cmd cmd
;
763 if (dev
->ethtool_ops
->get_link_ksettings
) {
764 /* First, use link_ksettings API if it is supported */
766 struct ethtool_link_ksettings link_ksettings
;
768 memset(&link_ksettings
, 0, sizeof(link_ksettings
));
769 err
= dev
->ethtool_ops
->get_link_ksettings(dev
,
773 convert_link_ksettings_to_legacy_settings(&cmd
,
776 /* send a sensible cmd tag back to user */
777 cmd
.cmd
= ETHTOOL_GSET
;
779 /* driver doesn't support %ethtool_link_ksettings
780 * API. revert to legacy %ethtool_cmd API, unless it's
781 * not supported either.
785 if (!dev
->ethtool_ops
->get_settings
)
788 memset(&cmd
, 0, sizeof(cmd
));
789 cmd
.cmd
= ETHTOOL_GSET
;
790 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
795 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
801 /* Update device link settings with given ethtool_cmd.
803 * Backward compatibility note: for compatibility with legacy ethtool,
804 * this has to work with both drivers implementing set_link_ksettings
805 * API and drivers implementing set_settings API. When drivers
806 * implement set_link_ksettings and user's request updates deprecated
807 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
808 * warning is logged once (with name of 1st driver/device) to
809 * recommend user to upgrade ethtool, and the request is rejected.
811 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
813 struct ethtool_cmd cmd
;
817 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
820 /* first, try new %ethtool_link_ksettings API. */
821 if (dev
->ethtool_ops
->set_link_ksettings
) {
822 struct ethtool_link_ksettings link_ksettings
;
824 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings
,
828 link_ksettings
.base
.cmd
= ETHTOOL_SLINKSETTINGS
;
829 link_ksettings
.base
.link_mode_masks_nwords
830 = __ETHTOOL_LINK_MODE_MASK_NU32
;
831 return dev
->ethtool_ops
->set_link_ksettings(dev
,
835 /* legacy %ethtool_cmd API */
837 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
838 * disappears internally
841 if (!dev
->ethtool_ops
->set_settings
)
844 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
847 static noinline_for_stack
int ethtool_get_drvinfo(struct net_device
*dev
,
848 void __user
*useraddr
)
850 struct ethtool_drvinfo info
;
851 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
853 memset(&info
, 0, sizeof(info
));
854 info
.cmd
= ETHTOOL_GDRVINFO
;
855 if (ops
->get_drvinfo
) {
856 ops
->get_drvinfo(dev
, &info
);
857 } else if (dev
->dev
.parent
&& dev
->dev
.parent
->driver
) {
858 strlcpy(info
.bus_info
, dev_name(dev
->dev
.parent
),
859 sizeof(info
.bus_info
));
860 strlcpy(info
.driver
, dev
->dev
.parent
->driver
->name
,
861 sizeof(info
.driver
));
867 * this method of obtaining string set info is deprecated;
868 * Use ETHTOOL_GSSET_INFO instead.
870 if (ops
->get_sset_count
) {
873 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
875 info
.testinfo_len
= rc
;
876 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
879 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
881 info
.n_priv_flags
= rc
;
883 if (ops
->get_regs_len
)
884 info
.regdump_len
= ops
->get_regs_len(dev
);
885 if (ops
->get_eeprom_len
)
886 info
.eedump_len
= ops
->get_eeprom_len(dev
);
888 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
893 static noinline_for_stack
int ethtool_get_sset_info(struct net_device
*dev
,
894 void __user
*useraddr
)
896 struct ethtool_sset_info info
;
898 int i
, idx
= 0, n_bits
= 0, ret
, rc
;
899 u32
*info_buf
= NULL
;
901 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
904 /* store copy of mask, because we zero struct later on */
905 sset_mask
= info
.sset_mask
;
909 /* calculate size of return buffer */
910 n_bits
= hweight64(sset_mask
);
912 memset(&info
, 0, sizeof(info
));
913 info
.cmd
= ETHTOOL_GSSET_INFO
;
915 info_buf
= kcalloc(n_bits
, sizeof(u32
), GFP_USER
);
920 * fill return buffer based on input bitmask and successful
921 * get_sset_count return
923 for (i
= 0; i
< 64; i
++) {
924 if (!(sset_mask
& (1ULL << i
)))
927 rc
= __ethtool_get_sset_count(dev
, i
);
929 info
.sset_mask
|= (1ULL << i
);
930 info_buf
[idx
++] = rc
;
935 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
938 useraddr
+= offsetof(struct ethtool_sset_info
, data
);
939 if (copy_to_user(useraddr
, info_buf
, idx
* sizeof(u32
)))
949 static noinline_for_stack
int ethtool_set_rxnfc(struct net_device
*dev
,
950 u32 cmd
, void __user
*useraddr
)
952 struct ethtool_rxnfc info
;
953 size_t info_size
= sizeof(info
);
956 if (!dev
->ethtool_ops
->set_rxnfc
)
959 /* struct ethtool_rxnfc was originally defined for
960 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
961 * members. User-space might still be using that
963 if (cmd
== ETHTOOL_SRXFH
)
964 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
967 if (copy_from_user(&info
, useraddr
, info_size
))
970 rc
= dev
->ethtool_ops
->set_rxnfc(dev
, &info
);
974 if (cmd
== ETHTOOL_SRXCLSRLINS
&&
975 copy_to_user(useraddr
, &info
, info_size
))
981 static noinline_for_stack
int ethtool_get_rxnfc(struct net_device
*dev
,
982 u32 cmd
, void __user
*useraddr
)
984 struct ethtool_rxnfc info
;
985 size_t info_size
= sizeof(info
);
986 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
988 void *rule_buf
= NULL
;
993 /* struct ethtool_rxnfc was originally defined for
994 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
995 * members. User-space might still be using that
997 if (cmd
== ETHTOOL_GRXFH
)
998 info_size
= (offsetof(struct ethtool_rxnfc
, data
) +
1001 if (copy_from_user(&info
, useraddr
, info_size
))
1004 /* If FLOW_RSS was requested then user-space must be using the
1005 * new definition, as FLOW_RSS is newer.
1007 if (cmd
== ETHTOOL_GRXFH
&& info
.flow_type
& FLOW_RSS
) {
1008 info_size
= sizeof(info
);
1009 if (copy_from_user(&info
, useraddr
, info_size
))
1011 /* Since malicious users may modify the original data,
1012 * we need to check whether FLOW_RSS is still requested.
1014 if (!(info
.flow_type
& FLOW_RSS
))
1018 if (info
.cmd
!= cmd
)
1021 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
1022 if (info
.rule_cnt
> 0) {
1023 if (info
.rule_cnt
<= KMALLOC_MAX_SIZE
/ sizeof(u32
))
1024 rule_buf
= kcalloc(info
.rule_cnt
, sizeof(u32
),
1031 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
1036 if (copy_to_user(useraddr
, &info
, info_size
))
1040 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
1041 if (copy_to_user(useraddr
, rule_buf
,
1042 info
.rule_cnt
* sizeof(u32
)))
1053 static int ethtool_copy_validate_indir(u32
*indir
, void __user
*useraddr
,
1054 struct ethtool_rxnfc
*rx_rings
,
1059 if (copy_from_user(indir
, useraddr
, size
* sizeof(indir
[0])))
1062 /* Validate ring indices */
1063 for (i
= 0; i
< size
; i
++)
1064 if (indir
[i
] >= rx_rings
->data
)
1070 u8 netdev_rss_key
[NETDEV_RSS_KEY_LEN
] __read_mostly
;
1072 void netdev_rss_key_fill(void *buffer
, size_t len
)
1074 BUG_ON(len
> sizeof(netdev_rss_key
));
1075 net_get_random_once(netdev_rss_key
, sizeof(netdev_rss_key
));
1076 memcpy(buffer
, netdev_rss_key
, len
);
1078 EXPORT_SYMBOL(netdev_rss_key_fill
);
1080 static int ethtool_get_max_rxfh_channel(struct net_device
*dev
, u32
*max
)
1082 u32 dev_size
, current_max
= 0;
1086 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
1087 !dev
->ethtool_ops
->get_rxfh
)
1089 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
1093 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
1097 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
1102 current_max
= max(current_max
, indir
[dev_size
]);
1111 static noinline_for_stack
int ethtool_get_rxfh_indir(struct net_device
*dev
,
1112 void __user
*useraddr
)
1114 u32 user_size
, dev_size
;
1118 if (!dev
->ethtool_ops
->get_rxfh_indir_size
||
1119 !dev
->ethtool_ops
->get_rxfh
)
1121 dev_size
= dev
->ethtool_ops
->get_rxfh_indir_size(dev
);
1125 if (copy_from_user(&user_size
,
1126 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
1130 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
1131 &dev_size
, sizeof(dev_size
)))
1134 /* If the user buffer size is 0, this is just a query for the
1135 * device table size. Otherwise, if it's smaller than the
1136 * device table size it's an error.
1138 if (user_size
< dev_size
)
1139 return user_size
== 0 ? 0 : -EINVAL
;
1141 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
1145 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, NULL
, NULL
);
1149 if (copy_to_user(useraddr
+
1150 offsetof(struct ethtool_rxfh_indir
, ring_index
[0]),
1151 indir
, dev_size
* sizeof(indir
[0])))
1159 static noinline_for_stack
int ethtool_set_rxfh_indir(struct net_device
*dev
,
1160 void __user
*useraddr
)
1162 struct ethtool_rxnfc rx_rings
;
1163 u32 user_size
, dev_size
, i
;
1165 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1167 u32 ringidx_offset
= offsetof(struct ethtool_rxfh_indir
, ring_index
[0]);
1169 if (!ops
->get_rxfh_indir_size
|| !ops
->set_rxfh
||
1173 dev_size
= ops
->get_rxfh_indir_size(dev
);
1177 if (copy_from_user(&user_size
,
1178 useraddr
+ offsetof(struct ethtool_rxfh_indir
, size
),
1182 if (user_size
!= 0 && user_size
!= dev_size
)
1185 indir
= kcalloc(dev_size
, sizeof(indir
[0]), GFP_USER
);
1189 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
1190 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
1194 if (user_size
== 0) {
1195 for (i
= 0; i
< dev_size
; i
++)
1196 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
1198 ret
= ethtool_copy_validate_indir(indir
,
1199 useraddr
+ ringidx_offset
,
1206 ret
= ops
->set_rxfh(dev
, indir
, NULL
, ETH_RSS_HASH_NO_CHANGE
);
1210 /* indicate whether rxfh was set to default */
1212 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
1214 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
1221 static noinline_for_stack
int ethtool_get_rxfh(struct net_device
*dev
,
1222 void __user
*useraddr
)
1225 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1226 u32 user_indir_size
, user_key_size
;
1227 u32 dev_indir_size
= 0, dev_key_size
= 0;
1228 struct ethtool_rxfh rxfh
;
1239 if (ops
->get_rxfh_indir_size
)
1240 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
1241 if (ops
->get_rxfh_key_size
)
1242 dev_key_size
= ops
->get_rxfh_key_size(dev
);
1244 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
1246 user_indir_size
= rxfh
.indir_size
;
1247 user_key_size
= rxfh
.key_size
;
1249 /* Check that reserved fields are 0 for now */
1250 if (rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] || rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
1252 /* Most drivers don't handle rss_context, check it's 0 as well */
1253 if (rxfh
.rss_context
&& !ops
->get_rxfh_context
)
1256 rxfh
.indir_size
= dev_indir_size
;
1257 rxfh
.key_size
= dev_key_size
;
1258 if (copy_to_user(useraddr
, &rxfh
, sizeof(rxfh
)))
1261 if ((user_indir_size
&& (user_indir_size
!= dev_indir_size
)) ||
1262 (user_key_size
&& (user_key_size
!= dev_key_size
)))
1265 indir_bytes
= user_indir_size
* sizeof(indir
[0]);
1266 total_size
= indir_bytes
+ user_key_size
;
1267 rss_config
= kzalloc(total_size
, GFP_USER
);
1271 if (user_indir_size
)
1272 indir
= (u32
*)rss_config
;
1275 hkey
= rss_config
+ indir_bytes
;
1277 if (rxfh
.rss_context
)
1278 ret
= dev
->ethtool_ops
->get_rxfh_context(dev
, indir
, hkey
,
1282 ret
= dev
->ethtool_ops
->get_rxfh(dev
, indir
, hkey
, &dev_hfunc
);
1286 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, hfunc
),
1287 &dev_hfunc
, sizeof(rxfh
.hfunc
))) {
1289 } else if (copy_to_user(useraddr
+
1290 offsetof(struct ethtool_rxfh
, rss_config
[0]),
1291 rss_config
, total_size
)) {
1300 static noinline_for_stack
int ethtool_set_rxfh(struct net_device
*dev
,
1301 void __user
*useraddr
)
1304 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1305 struct ethtool_rxnfc rx_rings
;
1306 struct ethtool_rxfh rxfh
;
1307 u32 dev_indir_size
= 0, dev_key_size
= 0, i
;
1308 u32
*indir
= NULL
, indir_bytes
= 0;
1311 u32 rss_cfg_offset
= offsetof(struct ethtool_rxfh
, rss_config
[0]);
1312 bool delete = false;
1314 if (!ops
->get_rxnfc
|| !ops
->set_rxfh
)
1317 if (ops
->get_rxfh_indir_size
)
1318 dev_indir_size
= ops
->get_rxfh_indir_size(dev
);
1319 if (ops
->get_rxfh_key_size
)
1320 dev_key_size
= ops
->get_rxfh_key_size(dev
);
1322 if (copy_from_user(&rxfh
, useraddr
, sizeof(rxfh
)))
1325 /* Check that reserved fields are 0 for now */
1326 if (rxfh
.rsvd8
[0] || rxfh
.rsvd8
[1] || rxfh
.rsvd8
[2] || rxfh
.rsvd32
)
1328 /* Most drivers don't handle rss_context, check it's 0 as well */
1329 if (rxfh
.rss_context
&& !ops
->set_rxfh_context
)
1332 /* If either indir, hash key or function is valid, proceed further.
1333 * Must request at least one change: indir size, hash key or function.
1335 if ((rxfh
.indir_size
&&
1336 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
&&
1337 rxfh
.indir_size
!= dev_indir_size
) ||
1338 (rxfh
.key_size
&& (rxfh
.key_size
!= dev_key_size
)) ||
1339 (rxfh
.indir_size
== ETH_RXFH_INDIR_NO_CHANGE
&&
1340 rxfh
.key_size
== 0 && rxfh
.hfunc
== ETH_RSS_HASH_NO_CHANGE
))
1343 if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
1344 indir_bytes
= dev_indir_size
* sizeof(indir
[0]);
1346 rss_config
= kzalloc(indir_bytes
+ rxfh
.key_size
, GFP_USER
);
1350 rx_rings
.cmd
= ETHTOOL_GRXRINGS
;
1351 ret
= ops
->get_rxnfc(dev
, &rx_rings
, NULL
);
1355 /* rxfh.indir_size == 0 means reset the indir table to default (master
1356 * context) or delete the context (other RSS contexts).
1357 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
1359 if (rxfh
.indir_size
&&
1360 rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
) {
1361 indir
= (u32
*)rss_config
;
1362 ret
= ethtool_copy_validate_indir(indir
,
1363 useraddr
+ rss_cfg_offset
,
1368 } else if (rxfh
.indir_size
== 0) {
1369 if (rxfh
.rss_context
== 0) {
1370 indir
= (u32
*)rss_config
;
1371 for (i
= 0; i
< dev_indir_size
; i
++)
1372 indir
[i
] = ethtool_rxfh_indir_default(i
, rx_rings
.data
);
1378 if (rxfh
.key_size
) {
1379 hkey
= rss_config
+ indir_bytes
;
1380 if (copy_from_user(hkey
,
1381 useraddr
+ rss_cfg_offset
+ indir_bytes
,
1388 if (rxfh
.rss_context
)
1389 ret
= ops
->set_rxfh_context(dev
, indir
, hkey
, rxfh
.hfunc
,
1390 &rxfh
.rss_context
, delete);
1392 ret
= ops
->set_rxfh(dev
, indir
, hkey
, rxfh
.hfunc
);
1396 if (copy_to_user(useraddr
+ offsetof(struct ethtool_rxfh
, rss_context
),
1397 &rxfh
.rss_context
, sizeof(rxfh
.rss_context
)))
1400 if (!rxfh
.rss_context
) {
1401 /* indicate whether rxfh was set to default */
1402 if (rxfh
.indir_size
== 0)
1403 dev
->priv_flags
&= ~IFF_RXFH_CONFIGURED
;
1404 else if (rxfh
.indir_size
!= ETH_RXFH_INDIR_NO_CHANGE
)
1405 dev
->priv_flags
|= IFF_RXFH_CONFIGURED
;
1413 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
1415 struct ethtool_regs regs
;
1416 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1420 if (!ops
->get_regs
|| !ops
->get_regs_len
)
1423 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
1426 reglen
= ops
->get_regs_len(dev
);
1427 if (regs
.len
> reglen
)
1432 regbuf
= vzalloc(reglen
);
1437 ops
->get_regs(dev
, ®s
, regbuf
);
1440 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
1442 useraddr
+= offsetof(struct ethtool_regs
, data
);
1443 if (regbuf
&& copy_to_user(useraddr
, regbuf
, regs
.len
))
1452 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
1454 struct ethtool_value reset
;
1457 if (!dev
->ethtool_ops
->reset
)
1460 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
1463 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
1467 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
1472 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
1474 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
1476 if (!dev
->ethtool_ops
->get_wol
)
1479 dev
->ethtool_ops
->get_wol(dev
, &wol
);
1481 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
1486 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
1488 struct ethtool_wolinfo wol
;
1491 if (!dev
->ethtool_ops
->set_wol
)
1494 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
1497 ret
= dev
->ethtool_ops
->set_wol(dev
, &wol
);
1501 dev
->wol_enabled
= !!wol
.wolopts
;
1506 static int ethtool_get_eee(struct net_device
*dev
, char __user
*useraddr
)
1508 struct ethtool_eee edata
;
1511 if (!dev
->ethtool_ops
->get_eee
)
1514 memset(&edata
, 0, sizeof(struct ethtool_eee
));
1515 edata
.cmd
= ETHTOOL_GEEE
;
1516 rc
= dev
->ethtool_ops
->get_eee(dev
, &edata
);
1521 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1527 static int ethtool_set_eee(struct net_device
*dev
, char __user
*useraddr
)
1529 struct ethtool_eee edata
;
1531 if (!dev
->ethtool_ops
->set_eee
)
1534 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1537 return dev
->ethtool_ops
->set_eee(dev
, &edata
);
1540 static int ethtool_nway_reset(struct net_device
*dev
)
1542 if (!dev
->ethtool_ops
->nway_reset
)
1545 return dev
->ethtool_ops
->nway_reset(dev
);
1548 static int ethtool_get_link(struct net_device
*dev
, char __user
*useraddr
)
1550 struct ethtool_value edata
= { .cmd
= ETHTOOL_GLINK
};
1552 if (!dev
->ethtool_ops
->get_link
)
1555 edata
.data
= netif_running(dev
) && dev
->ethtool_ops
->get_link(dev
);
1557 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1562 static int ethtool_get_any_eeprom(struct net_device
*dev
, void __user
*useraddr
,
1563 int (*getter
)(struct net_device
*,
1564 struct ethtool_eeprom
*, u8
*),
1567 struct ethtool_eeprom eeprom
;
1568 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1569 u32 bytes_remaining
;
1573 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1576 /* Check for wrap and zero */
1577 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1580 /* Check for exceeding total eeprom len */
1581 if (eeprom
.offset
+ eeprom
.len
> total_len
)
1584 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1588 bytes_remaining
= eeprom
.len
;
1589 while (bytes_remaining
> 0) {
1590 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1592 ret
= getter(dev
, &eeprom
, data
);
1595 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
1599 userbuf
+= eeprom
.len
;
1600 eeprom
.offset
+= eeprom
.len
;
1601 bytes_remaining
-= eeprom
.len
;
1604 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
1605 eeprom
.offset
-= eeprom
.len
;
1606 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
1613 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1615 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1617 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
||
1618 !ops
->get_eeprom_len(dev
))
1621 return ethtool_get_any_eeprom(dev
, useraddr
, ops
->get_eeprom
,
1622 ops
->get_eeprom_len(dev
));
1625 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
1627 struct ethtool_eeprom eeprom
;
1628 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1629 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
1630 u32 bytes_remaining
;
1634 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
||
1635 !ops
->get_eeprom_len(dev
))
1638 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
1641 /* Check for wrap and zero */
1642 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
1645 /* Check for exceeding total eeprom len */
1646 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
1649 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
1653 bytes_remaining
= eeprom
.len
;
1654 while (bytes_remaining
> 0) {
1655 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
1657 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
1661 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
1664 userbuf
+= eeprom
.len
;
1665 eeprom
.offset
+= eeprom
.len
;
1666 bytes_remaining
-= eeprom
.len
;
1673 static noinline_for_stack
int ethtool_get_coalesce(struct net_device
*dev
,
1674 void __user
*useraddr
)
1676 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
1678 if (!dev
->ethtool_ops
->get_coalesce
)
1681 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
1683 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
1688 static noinline_for_stack
int ethtool_set_coalesce(struct net_device
*dev
,
1689 void __user
*useraddr
)
1691 struct ethtool_coalesce coalesce
;
1693 if (!dev
->ethtool_ops
->set_coalesce
)
1696 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
1699 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
1702 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1704 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
1706 if (!dev
->ethtool_ops
->get_ringparam
)
1709 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
1711 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
1716 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
1718 struct ethtool_ringparam ringparam
, max
= { .cmd
= ETHTOOL_GRINGPARAM
};
1720 if (!dev
->ethtool_ops
->set_ringparam
|| !dev
->ethtool_ops
->get_ringparam
)
1723 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
1726 dev
->ethtool_ops
->get_ringparam(dev
, &max
);
1728 /* ensure new ring parameters are within the maximums */
1729 if (ringparam
.rx_pending
> max
.rx_max_pending
||
1730 ringparam
.rx_mini_pending
> max
.rx_mini_max_pending
||
1731 ringparam
.rx_jumbo_pending
> max
.rx_jumbo_max_pending
||
1732 ringparam
.tx_pending
> max
.tx_max_pending
)
1735 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
1738 static noinline_for_stack
int ethtool_get_channels(struct net_device
*dev
,
1739 void __user
*useraddr
)
1741 struct ethtool_channels channels
= { .cmd
= ETHTOOL_GCHANNELS
};
1743 if (!dev
->ethtool_ops
->get_channels
)
1746 dev
->ethtool_ops
->get_channels(dev
, &channels
);
1748 if (copy_to_user(useraddr
, &channels
, sizeof(channels
)))
1753 static noinline_for_stack
int ethtool_set_channels(struct net_device
*dev
,
1754 void __user
*useraddr
)
1756 struct ethtool_channels channels
, max
= { .cmd
= ETHTOOL_GCHANNELS
};
1757 u32 max_rx_in_use
= 0;
1759 if (!dev
->ethtool_ops
->set_channels
|| !dev
->ethtool_ops
->get_channels
)
1762 if (copy_from_user(&channels
, useraddr
, sizeof(channels
)))
1765 dev
->ethtool_ops
->get_channels(dev
, &max
);
1767 /* ensure new counts are within the maximums */
1768 if ((channels
.rx_count
> max
.max_rx
) ||
1769 (channels
.tx_count
> max
.max_tx
) ||
1770 (channels
.combined_count
> max
.max_combined
) ||
1771 (channels
.other_count
> max
.max_other
))
1774 /* ensure the new Rx count fits within the configured Rx flow
1775 * indirection table settings */
1776 if (netif_is_rxfh_configured(dev
) &&
1777 !ethtool_get_max_rxfh_channel(dev
, &max_rx_in_use
) &&
1778 (channels
.combined_count
+ channels
.rx_count
) <= max_rx_in_use
)
1781 return dev
->ethtool_ops
->set_channels(dev
, &channels
);
1784 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1786 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
1788 if (!dev
->ethtool_ops
->get_pauseparam
)
1791 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
1793 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
1798 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
1800 struct ethtool_pauseparam pauseparam
;
1802 if (!dev
->ethtool_ops
->set_pauseparam
)
1805 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
1808 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
1811 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1813 struct ethtool_test test
;
1814 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1818 if (!ops
->self_test
|| !ops
->get_sset_count
)
1821 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1824 WARN_ON(test_len
== 0);
1826 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1829 test
.len
= test_len
;
1830 data
= kmalloc_array(test_len
, sizeof(u64
), GFP_USER
);
1834 ops
->self_test(dev
, &test
, data
);
1837 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1839 useraddr
+= sizeof(test
);
1840 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1849 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1851 struct ethtool_gstrings gstrings
;
1855 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1858 ret
= __ethtool_get_sset_count(dev
, gstrings
.string_set
);
1861 if (ret
> S32_MAX
/ ETH_GSTRING_LEN
)
1866 data
= vzalloc(array_size(gstrings
.len
, ETH_GSTRING_LEN
));
1867 if (gstrings
.len
&& !data
)
1870 __ethtool_get_strings(dev
, gstrings
.string_set
, data
);
1873 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1875 useraddr
+= sizeof(gstrings
);
1877 copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1886 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1888 struct ethtool_value id
;
1890 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1893 if (!ops
->set_phys_id
)
1899 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1902 rc
= ops
->set_phys_id(dev
, ETHTOOL_ID_ACTIVE
);
1906 /* Drop the RTNL lock while waiting, but prevent reentry or
1907 * removal of the device.
1914 /* Driver will handle this itself */
1915 schedule_timeout_interruptible(
1916 id
.data
? (id
.data
* HZ
) : MAX_SCHEDULE_TIMEOUT
);
1918 /* Driver expects to be called at twice the frequency in rc */
1919 int n
= rc
* 2, i
, interval
= HZ
/ n
;
1921 /* Count down seconds */
1923 /* Count down iterations per second */
1927 rc
= ops
->set_phys_id(dev
,
1928 (i
& 1) ? ETHTOOL_ID_OFF
: ETHTOOL_ID_ON
);
1932 schedule_timeout_interruptible(interval
);
1933 } while (!signal_pending(current
) && --i
!= 0);
1934 } while (!signal_pending(current
) &&
1935 (id
.data
== 0 || --id
.data
!= 0));
1942 (void) ops
->set_phys_id(dev
, ETHTOOL_ID_INACTIVE
);
1946 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1948 struct ethtool_stats stats
;
1949 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1953 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1956 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1959 if (n_stats
> S32_MAX
/ sizeof(u64
))
1961 WARN_ON_ONCE(!n_stats
);
1962 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1965 stats
.n_stats
= n_stats
;
1966 data
= vzalloc(array_size(n_stats
, sizeof(u64
)));
1967 if (n_stats
&& !data
)
1970 ops
->get_ethtool_stats(dev
, &stats
, data
);
1973 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1975 useraddr
+= sizeof(stats
);
1976 if (n_stats
&& copy_to_user(useraddr
, data
, n_stats
* sizeof(u64
)))
1985 static int ethtool_get_phy_stats(struct net_device
*dev
, void __user
*useraddr
)
1987 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1988 struct phy_device
*phydev
= dev
->phydev
;
1989 struct ethtool_stats stats
;
1993 if (!phydev
&& (!ops
->get_ethtool_phy_stats
|| !ops
->get_sset_count
))
1996 if (dev
->phydev
&& !ops
->get_ethtool_phy_stats
)
1997 n_stats
= phy_ethtool_get_sset_count(dev
->phydev
);
1999 n_stats
= ops
->get_sset_count(dev
, ETH_SS_PHY_STATS
);
2002 if (n_stats
> S32_MAX
/ sizeof(u64
))
2004 WARN_ON_ONCE(!n_stats
);
2006 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
2009 stats
.n_stats
= n_stats
;
2010 data
= vzalloc(array_size(n_stats
, sizeof(u64
)));
2011 if (n_stats
&& !data
)
2014 if (dev
->phydev
&& !ops
->get_ethtool_phy_stats
) {
2015 ret
= phy_ethtool_get_stats(dev
->phydev
, &stats
, data
);
2019 ops
->get_ethtool_phy_stats(dev
, &stats
, data
);
2023 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
2025 useraddr
+= sizeof(stats
);
2026 if (n_stats
&& copy_to_user(useraddr
, data
, n_stats
* sizeof(u64
)))
2035 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
2037 struct ethtool_perm_addr epaddr
;
2039 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
2042 if (epaddr
.size
< dev
->addr_len
)
2044 epaddr
.size
= dev
->addr_len
;
2046 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
2048 useraddr
+= sizeof(epaddr
);
2049 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
2054 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
2055 u32 cmd
, u32 (*actor
)(struct net_device
*))
2057 struct ethtool_value edata
= { .cmd
= cmd
};
2062 edata
.data
= actor(dev
);
2064 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
2069 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
2070 void (*actor
)(struct net_device
*, u32
))
2072 struct ethtool_value edata
;
2077 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2080 actor(dev
, edata
.data
);
2084 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
2085 int (*actor
)(struct net_device
*, u32
))
2087 struct ethtool_value edata
;
2092 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
2095 return actor(dev
, edata
.data
);
2098 static noinline_for_stack
int ethtool_flash_device(struct net_device
*dev
,
2099 char __user
*useraddr
)
2101 struct ethtool_flash efl
;
2103 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
2106 if (!dev
->ethtool_ops
->flash_device
)
2109 efl
.data
[ETHTOOL_FLASH_MAX_FILENAME
- 1] = 0;
2111 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
2114 static int ethtool_set_dump(struct net_device
*dev
,
2115 void __user
*useraddr
)
2117 struct ethtool_dump dump
;
2119 if (!dev
->ethtool_ops
->set_dump
)
2122 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2125 return dev
->ethtool_ops
->set_dump(dev
, &dump
);
2128 static int ethtool_get_dump_flag(struct net_device
*dev
,
2129 void __user
*useraddr
)
2132 struct ethtool_dump dump
;
2133 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2135 if (!ops
->get_dump_flag
)
2138 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2141 ret
= ops
->get_dump_flag(dev
, &dump
);
2145 if (copy_to_user(useraddr
, &dump
, sizeof(dump
)))
2150 static int ethtool_get_dump_data(struct net_device
*dev
,
2151 void __user
*useraddr
)
2155 struct ethtool_dump dump
, tmp
;
2156 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2159 if (!ops
->get_dump_data
|| !ops
->get_dump_flag
)
2162 if (copy_from_user(&dump
, useraddr
, sizeof(dump
)))
2165 memset(&tmp
, 0, sizeof(tmp
));
2166 tmp
.cmd
= ETHTOOL_GET_DUMP_FLAG
;
2167 ret
= ops
->get_dump_flag(dev
, &tmp
);
2171 len
= min(tmp
.len
, dump
.len
);
2175 /* Don't ever let the driver think there's more space available
2176 * than it requested with .get_dump_flag().
2180 /* Always allocate enough space to hold the whole thing so that the
2181 * driver does not need to check the length and bother with partial
2184 data
= vzalloc(tmp
.len
);
2187 ret
= ops
->get_dump_data(dev
, &dump
, data
);
2191 /* There are two sane possibilities:
2192 * 1. The driver's .get_dump_data() does not touch dump.len.
2193 * 2. Or it may set dump.len to how much it really writes, which
2194 * should be tmp.len (or len if it can do a partial dump).
2195 * In any case respond to userspace with the actual length of data
2198 WARN_ON(dump
.len
!= len
&& dump
.len
!= tmp
.len
);
2201 if (copy_to_user(useraddr
, &dump
, sizeof(dump
))) {
2205 useraddr
+= offsetof(struct ethtool_dump
, data
);
2206 if (copy_to_user(useraddr
, data
, len
))
2213 static int ethtool_get_ts_info(struct net_device
*dev
, void __user
*useraddr
)
2216 struct ethtool_ts_info info
;
2217 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2218 struct phy_device
*phydev
= dev
->phydev
;
2220 memset(&info
, 0, sizeof(info
));
2221 info
.cmd
= ETHTOOL_GET_TS_INFO
;
2223 if (phydev
&& phydev
->drv
&& phydev
->drv
->ts_info
) {
2224 err
= phydev
->drv
->ts_info(phydev
, &info
);
2225 } else if (ops
->get_ts_info
) {
2226 err
= ops
->get_ts_info(dev
, &info
);
2228 info
.so_timestamping
=
2229 SOF_TIMESTAMPING_RX_SOFTWARE
|
2230 SOF_TIMESTAMPING_SOFTWARE
;
2231 info
.phc_index
= -1;
2237 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
2243 static int __ethtool_get_module_info(struct net_device
*dev
,
2244 struct ethtool_modinfo
*modinfo
)
2246 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2247 struct phy_device
*phydev
= dev
->phydev
;
2250 return sfp_get_module_info(dev
->sfp_bus
, modinfo
);
2252 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_info
)
2253 return phydev
->drv
->module_info(phydev
, modinfo
);
2255 if (ops
->get_module_info
)
2256 return ops
->get_module_info(dev
, modinfo
);
2261 static int ethtool_get_module_info(struct net_device
*dev
,
2262 void __user
*useraddr
)
2265 struct ethtool_modinfo modinfo
;
2267 if (copy_from_user(&modinfo
, useraddr
, sizeof(modinfo
)))
2270 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2274 if (copy_to_user(useraddr
, &modinfo
, sizeof(modinfo
)))
2280 static int __ethtool_get_module_eeprom(struct net_device
*dev
,
2281 struct ethtool_eeprom
*ee
, u8
*data
)
2283 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2284 struct phy_device
*phydev
= dev
->phydev
;
2287 return sfp_get_module_eeprom(dev
->sfp_bus
, ee
, data
);
2289 if (phydev
&& phydev
->drv
&& phydev
->drv
->module_eeprom
)
2290 return phydev
->drv
->module_eeprom(phydev
, ee
, data
);
2292 if (ops
->get_module_eeprom
)
2293 return ops
->get_module_eeprom(dev
, ee
, data
);
2298 static int ethtool_get_module_eeprom(struct net_device
*dev
,
2299 void __user
*useraddr
)
2302 struct ethtool_modinfo modinfo
;
2304 ret
= __ethtool_get_module_info(dev
, &modinfo
);
2308 return ethtool_get_any_eeprom(dev
, useraddr
,
2309 __ethtool_get_module_eeprom
,
2310 modinfo
.eeprom_len
);
2313 static int ethtool_tunable_valid(const struct ethtool_tunable
*tuna
)
2316 case ETHTOOL_RX_COPYBREAK
:
2317 case ETHTOOL_TX_COPYBREAK
:
2318 if (tuna
->len
!= sizeof(u32
) ||
2319 tuna
->type_id
!= ETHTOOL_TUNABLE_U32
)
2322 case ETHTOOL_PFC_PREVENTION_TOUT
:
2323 if (tuna
->len
!= sizeof(u16
) ||
2324 tuna
->type_id
!= ETHTOOL_TUNABLE_U16
)
2334 static int ethtool_get_tunable(struct net_device
*dev
, void __user
*useraddr
)
2337 struct ethtool_tunable tuna
;
2338 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2341 if (!ops
->get_tunable
)
2343 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2345 ret
= ethtool_tunable_valid(&tuna
);
2348 data
= kmalloc(tuna
.len
, GFP_USER
);
2351 ret
= ops
->get_tunable(dev
, &tuna
, data
);
2354 useraddr
+= sizeof(tuna
);
2356 if (copy_to_user(useraddr
, data
, tuna
.len
))
2365 static int ethtool_set_tunable(struct net_device
*dev
, void __user
*useraddr
)
2368 struct ethtool_tunable tuna
;
2369 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
2372 if (!ops
->set_tunable
)
2374 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2376 ret
= ethtool_tunable_valid(&tuna
);
2379 useraddr
+= sizeof(tuna
);
2380 data
= memdup_user(useraddr
, tuna
.len
);
2382 return PTR_ERR(data
);
2383 ret
= ops
->set_tunable(dev
, &tuna
, data
);
2389 static int ethtool_get_per_queue_coalesce(struct net_device
*dev
,
2390 void __user
*useraddr
,
2391 struct ethtool_per_queue_op
*per_queue_opt
)
2395 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2397 if (!dev
->ethtool_ops
->get_per_queue_coalesce
)
2400 useraddr
+= sizeof(*per_queue_opt
);
2402 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
,
2405 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2406 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
2408 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, &coalesce
);
2411 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
2413 useraddr
+= sizeof(coalesce
);
2419 static int ethtool_set_per_queue_coalesce(struct net_device
*dev
,
2420 void __user
*useraddr
,
2421 struct ethtool_per_queue_op
*per_queue_opt
)
2426 struct ethtool_coalesce
*backup
= NULL
, *tmp
= NULL
;
2427 DECLARE_BITMAP(queue_mask
, MAX_NUM_QUEUE
);
2429 if ((!dev
->ethtool_ops
->set_per_queue_coalesce
) ||
2430 (!dev
->ethtool_ops
->get_per_queue_coalesce
))
2433 useraddr
+= sizeof(*per_queue_opt
);
2435 bitmap_from_arr32(queue_mask
, per_queue_opt
->queue_mask
, MAX_NUM_QUEUE
);
2436 n_queue
= bitmap_weight(queue_mask
, MAX_NUM_QUEUE
);
2437 tmp
= backup
= kmalloc_array(n_queue
, sizeof(*backup
), GFP_KERNEL
);
2441 for_each_set_bit(bit
, queue_mask
, MAX_NUM_QUEUE
) {
2442 struct ethtool_coalesce coalesce
;
2444 ret
= dev
->ethtool_ops
->get_per_queue_coalesce(dev
, bit
, tmp
);
2450 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
))) {
2455 ret
= dev
->ethtool_ops
->set_per_queue_coalesce(dev
, bit
, &coalesce
);
2459 useraddr
+= sizeof(coalesce
);
2465 for_each_set_bit(i
, queue_mask
, bit
) {
2466 dev
->ethtool_ops
->set_per_queue_coalesce(dev
, i
, tmp
);
2475 static int ethtool_set_per_queue(struct net_device
*dev
,
2476 void __user
*useraddr
, u32 sub_cmd
)
2478 struct ethtool_per_queue_op per_queue_opt
;
2480 if (copy_from_user(&per_queue_opt
, useraddr
, sizeof(per_queue_opt
)))
2483 if (per_queue_opt
.sub_command
!= sub_cmd
)
2486 switch (per_queue_opt
.sub_command
) {
2487 case ETHTOOL_GCOALESCE
:
2488 return ethtool_get_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2489 case ETHTOOL_SCOALESCE
:
2490 return ethtool_set_per_queue_coalesce(dev
, useraddr
, &per_queue_opt
);
2496 static int ethtool_phy_tunable_valid(const struct ethtool_tunable
*tuna
)
2499 case ETHTOOL_PHY_DOWNSHIFT
:
2500 if (tuna
->len
!= sizeof(u8
) ||
2501 tuna
->type_id
!= ETHTOOL_TUNABLE_U8
)
2511 static int get_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2514 struct ethtool_tunable tuna
;
2515 struct phy_device
*phydev
= dev
->phydev
;
2518 if (!(phydev
&& phydev
->drv
&& phydev
->drv
->get_tunable
))
2521 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2523 ret
= ethtool_phy_tunable_valid(&tuna
);
2526 data
= kmalloc(tuna
.len
, GFP_USER
);
2529 mutex_lock(&phydev
->lock
);
2530 ret
= phydev
->drv
->get_tunable(phydev
, &tuna
, data
);
2531 mutex_unlock(&phydev
->lock
);
2534 useraddr
+= sizeof(tuna
);
2536 if (copy_to_user(useraddr
, data
, tuna
.len
))
2545 static int set_phy_tunable(struct net_device
*dev
, void __user
*useraddr
)
2548 struct ethtool_tunable tuna
;
2549 struct phy_device
*phydev
= dev
->phydev
;
2552 if (!(phydev
&& phydev
->drv
&& phydev
->drv
->set_tunable
))
2554 if (copy_from_user(&tuna
, useraddr
, sizeof(tuna
)))
2556 ret
= ethtool_phy_tunable_valid(&tuna
);
2559 useraddr
+= sizeof(tuna
);
2560 data
= memdup_user(useraddr
, tuna
.len
);
2562 return PTR_ERR(data
);
2563 mutex_lock(&phydev
->lock
);
2564 ret
= phydev
->drv
->set_tunable(phydev
, &tuna
, data
);
2565 mutex_unlock(&phydev
->lock
);
2571 static int ethtool_get_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2573 struct ethtool_fecparam fecparam
= { ETHTOOL_GFECPARAM
};
2576 if (!dev
->ethtool_ops
->get_fecparam
)
2579 rc
= dev
->ethtool_ops
->get_fecparam(dev
, &fecparam
);
2583 if (copy_to_user(useraddr
, &fecparam
, sizeof(fecparam
)))
2588 static int ethtool_set_fecparam(struct net_device
*dev
, void __user
*useraddr
)
2590 struct ethtool_fecparam fecparam
;
2592 if (!dev
->ethtool_ops
->set_fecparam
)
2595 if (copy_from_user(&fecparam
, useraddr
, sizeof(fecparam
)))
2598 return dev
->ethtool_ops
->set_fecparam(dev
, &fecparam
);
2601 /* The main entry point in this file. Called from net/core/dev_ioctl.c */
2603 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
2605 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
2606 void __user
*useraddr
= ifr
->ifr_data
;
2607 u32 ethcmd
, sub_cmd
;
2609 netdev_features_t old_features
;
2611 if (!dev
|| !netif_device_present(dev
))
2614 if (copy_from_user(ðcmd
, useraddr
, sizeof(ethcmd
)))
2617 if (ethcmd
== ETHTOOL_PERQUEUE
) {
2618 if (copy_from_user(&sub_cmd
, useraddr
+ sizeof(ethcmd
), sizeof(sub_cmd
)))
2623 /* Allow some commands to be done by anyone */
2626 case ETHTOOL_GDRVINFO
:
2627 case ETHTOOL_GMSGLVL
:
2629 case ETHTOOL_GCOALESCE
:
2630 case ETHTOOL_GRINGPARAM
:
2631 case ETHTOOL_GPAUSEPARAM
:
2632 case ETHTOOL_GRXCSUM
:
2633 case ETHTOOL_GTXCSUM
:
2635 case ETHTOOL_GSSET_INFO
:
2636 case ETHTOOL_GSTRINGS
:
2637 case ETHTOOL_GSTATS
:
2638 case ETHTOOL_GPHYSTATS
:
2640 case ETHTOOL_GPERMADDR
:
2644 case ETHTOOL_GFLAGS
:
2645 case ETHTOOL_GPFLAGS
:
2647 case ETHTOOL_GRXRINGS
:
2648 case ETHTOOL_GRXCLSRLCNT
:
2649 case ETHTOOL_GRXCLSRULE
:
2650 case ETHTOOL_GRXCLSRLALL
:
2651 case ETHTOOL_GRXFHINDIR
:
2653 case ETHTOOL_GFEATURES
:
2654 case ETHTOOL_GCHANNELS
:
2655 case ETHTOOL_GET_TS_INFO
:
2657 case ETHTOOL_GTUNABLE
:
2658 case ETHTOOL_PHY_GTUNABLE
:
2659 case ETHTOOL_GLINKSETTINGS
:
2660 case ETHTOOL_GFECPARAM
:
2663 if (!ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
2667 if (dev
->ethtool_ops
->begin
) {
2668 rc
= dev
->ethtool_ops
->begin(dev
);
2672 old_features
= dev
->features
;
2676 rc
= ethtool_get_settings(dev
, useraddr
);
2679 rc
= ethtool_set_settings(dev
, useraddr
);
2681 case ETHTOOL_GDRVINFO
:
2682 rc
= ethtool_get_drvinfo(dev
, useraddr
);
2685 rc
= ethtool_get_regs(dev
, useraddr
);
2688 rc
= ethtool_get_wol(dev
, useraddr
);
2691 rc
= ethtool_set_wol(dev
, useraddr
);
2693 case ETHTOOL_GMSGLVL
:
2694 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2695 dev
->ethtool_ops
->get_msglevel
);
2697 case ETHTOOL_SMSGLVL
:
2698 rc
= ethtool_set_value_void(dev
, useraddr
,
2699 dev
->ethtool_ops
->set_msglevel
);
2702 rc
= ethtool_get_eee(dev
, useraddr
);
2705 rc
= ethtool_set_eee(dev
, useraddr
);
2707 case ETHTOOL_NWAY_RST
:
2708 rc
= ethtool_nway_reset(dev
);
2711 rc
= ethtool_get_link(dev
, useraddr
);
2713 case ETHTOOL_GEEPROM
:
2714 rc
= ethtool_get_eeprom(dev
, useraddr
);
2716 case ETHTOOL_SEEPROM
:
2717 rc
= ethtool_set_eeprom(dev
, useraddr
);
2719 case ETHTOOL_GCOALESCE
:
2720 rc
= ethtool_get_coalesce(dev
, useraddr
);
2722 case ETHTOOL_SCOALESCE
:
2723 rc
= ethtool_set_coalesce(dev
, useraddr
);
2725 case ETHTOOL_GRINGPARAM
:
2726 rc
= ethtool_get_ringparam(dev
, useraddr
);
2728 case ETHTOOL_SRINGPARAM
:
2729 rc
= ethtool_set_ringparam(dev
, useraddr
);
2731 case ETHTOOL_GPAUSEPARAM
:
2732 rc
= ethtool_get_pauseparam(dev
, useraddr
);
2734 case ETHTOOL_SPAUSEPARAM
:
2735 rc
= ethtool_set_pauseparam(dev
, useraddr
);
2738 rc
= ethtool_self_test(dev
, useraddr
);
2740 case ETHTOOL_GSTRINGS
:
2741 rc
= ethtool_get_strings(dev
, useraddr
);
2743 case ETHTOOL_PHYS_ID
:
2744 rc
= ethtool_phys_id(dev
, useraddr
);
2746 case ETHTOOL_GSTATS
:
2747 rc
= ethtool_get_stats(dev
, useraddr
);
2749 case ETHTOOL_GPERMADDR
:
2750 rc
= ethtool_get_perm_addr(dev
, useraddr
);
2752 case ETHTOOL_GFLAGS
:
2753 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2754 __ethtool_get_flags
);
2756 case ETHTOOL_SFLAGS
:
2757 rc
= ethtool_set_value(dev
, useraddr
, __ethtool_set_flags
);
2759 case ETHTOOL_GPFLAGS
:
2760 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
2761 dev
->ethtool_ops
->get_priv_flags
);
2763 case ETHTOOL_SPFLAGS
:
2764 rc
= ethtool_set_value(dev
, useraddr
,
2765 dev
->ethtool_ops
->set_priv_flags
);
2768 case ETHTOOL_GRXRINGS
:
2769 case ETHTOOL_GRXCLSRLCNT
:
2770 case ETHTOOL_GRXCLSRULE
:
2771 case ETHTOOL_GRXCLSRLALL
:
2772 rc
= ethtool_get_rxnfc(dev
, ethcmd
, useraddr
);
2775 case ETHTOOL_SRXCLSRLDEL
:
2776 case ETHTOOL_SRXCLSRLINS
:
2777 rc
= ethtool_set_rxnfc(dev
, ethcmd
, useraddr
);
2779 case ETHTOOL_FLASHDEV
:
2780 rc
= ethtool_flash_device(dev
, useraddr
);
2783 rc
= ethtool_reset(dev
, useraddr
);
2785 case ETHTOOL_GSSET_INFO
:
2786 rc
= ethtool_get_sset_info(dev
, useraddr
);
2788 case ETHTOOL_GRXFHINDIR
:
2789 rc
= ethtool_get_rxfh_indir(dev
, useraddr
);
2791 case ETHTOOL_SRXFHINDIR
:
2792 rc
= ethtool_set_rxfh_indir(dev
, useraddr
);
2795 rc
= ethtool_get_rxfh(dev
, useraddr
);
2798 rc
= ethtool_set_rxfh(dev
, useraddr
);
2800 case ETHTOOL_GFEATURES
:
2801 rc
= ethtool_get_features(dev
, useraddr
);
2803 case ETHTOOL_SFEATURES
:
2804 rc
= ethtool_set_features(dev
, useraddr
);
2806 case ETHTOOL_GTXCSUM
:
2807 case ETHTOOL_GRXCSUM
:
2812 rc
= ethtool_get_one_feature(dev
, useraddr
, ethcmd
);
2814 case ETHTOOL_STXCSUM
:
2815 case ETHTOOL_SRXCSUM
:
2820 rc
= ethtool_set_one_feature(dev
, useraddr
, ethcmd
);
2822 case ETHTOOL_GCHANNELS
:
2823 rc
= ethtool_get_channels(dev
, useraddr
);
2825 case ETHTOOL_SCHANNELS
:
2826 rc
= ethtool_set_channels(dev
, useraddr
);
2828 case ETHTOOL_SET_DUMP
:
2829 rc
= ethtool_set_dump(dev
, useraddr
);
2831 case ETHTOOL_GET_DUMP_FLAG
:
2832 rc
= ethtool_get_dump_flag(dev
, useraddr
);
2834 case ETHTOOL_GET_DUMP_DATA
:
2835 rc
= ethtool_get_dump_data(dev
, useraddr
);
2837 case ETHTOOL_GET_TS_INFO
:
2838 rc
= ethtool_get_ts_info(dev
, useraddr
);
2840 case ETHTOOL_GMODULEINFO
:
2841 rc
= ethtool_get_module_info(dev
, useraddr
);
2843 case ETHTOOL_GMODULEEEPROM
:
2844 rc
= ethtool_get_module_eeprom(dev
, useraddr
);
2846 case ETHTOOL_GTUNABLE
:
2847 rc
= ethtool_get_tunable(dev
, useraddr
);
2849 case ETHTOOL_STUNABLE
:
2850 rc
= ethtool_set_tunable(dev
, useraddr
);
2852 case ETHTOOL_GPHYSTATS
:
2853 rc
= ethtool_get_phy_stats(dev
, useraddr
);
2855 case ETHTOOL_PERQUEUE
:
2856 rc
= ethtool_set_per_queue(dev
, useraddr
, sub_cmd
);
2858 case ETHTOOL_GLINKSETTINGS
:
2859 rc
= ethtool_get_link_ksettings(dev
, useraddr
);
2861 case ETHTOOL_SLINKSETTINGS
:
2862 rc
= ethtool_set_link_ksettings(dev
, useraddr
);
2864 case ETHTOOL_PHY_GTUNABLE
:
2865 rc
= get_phy_tunable(dev
, useraddr
);
2867 case ETHTOOL_PHY_STUNABLE
:
2868 rc
= set_phy_tunable(dev
, useraddr
);
2870 case ETHTOOL_GFECPARAM
:
2871 rc
= ethtool_get_fecparam(dev
, useraddr
);
2873 case ETHTOOL_SFECPARAM
:
2874 rc
= ethtool_set_fecparam(dev
, useraddr
);
2880 if (dev
->ethtool_ops
->complete
)
2881 dev
->ethtool_ops
->complete(dev
);
2883 if (old_features
!= dev
->features
)
2884 netdev_features_change(dev
);