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 <asm/uaccess.h>
23 * Some useful ethtool_ops methods that're device independent.
24 * If we find that all drivers want to do the same thing here,
25 * we can turn these into dev_() function calls.
28 u32
ethtool_op_get_link(struct net_device
*dev
)
30 return netif_carrier_ok(dev
) ? 1 : 0;
33 u32
ethtool_op_get_rx_csum(struct net_device
*dev
)
35 return (dev
->features
& NETIF_F_ALL_CSUM
) != 0;
37 EXPORT_SYMBOL(ethtool_op_get_rx_csum
);
39 u32
ethtool_op_get_tx_csum(struct net_device
*dev
)
41 return (dev
->features
& NETIF_F_ALL_CSUM
) != 0;
43 EXPORT_SYMBOL(ethtool_op_get_tx_csum
);
45 int ethtool_op_set_tx_csum(struct net_device
*dev
, u32 data
)
48 dev
->features
|= NETIF_F_IP_CSUM
;
50 dev
->features
&= ~NETIF_F_IP_CSUM
;
55 int ethtool_op_set_tx_hw_csum(struct net_device
*dev
, u32 data
)
58 dev
->features
|= NETIF_F_HW_CSUM
;
60 dev
->features
&= ~NETIF_F_HW_CSUM
;
65 int ethtool_op_set_tx_ipv6_csum(struct net_device
*dev
, u32 data
)
68 dev
->features
|= NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
70 dev
->features
&= ~(NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
);
75 u32
ethtool_op_get_sg(struct net_device
*dev
)
77 return (dev
->features
& NETIF_F_SG
) != 0;
80 int ethtool_op_set_sg(struct net_device
*dev
, u32 data
)
83 dev
->features
|= NETIF_F_SG
;
85 dev
->features
&= ~NETIF_F_SG
;
90 u32
ethtool_op_get_tso(struct net_device
*dev
)
92 return (dev
->features
& NETIF_F_TSO
) != 0;
95 int ethtool_op_set_tso(struct net_device
*dev
, u32 data
)
98 dev
->features
|= NETIF_F_TSO
;
100 dev
->features
&= ~NETIF_F_TSO
;
105 u32
ethtool_op_get_ufo(struct net_device
*dev
)
107 return (dev
->features
& NETIF_F_UFO
) != 0;
110 int ethtool_op_set_ufo(struct net_device
*dev
, u32 data
)
113 dev
->features
|= NETIF_F_UFO
;
115 dev
->features
&= ~NETIF_F_UFO
;
119 /* the following list of flags are the same as their associated
120 * NETIF_F_xxx values in include/linux/netdevice.h
122 static const u32 flags_dup_features
=
123 (ETH_FLAG_LRO
| ETH_FLAG_NTUPLE
);
125 u32
ethtool_op_get_flags(struct net_device
*dev
)
127 /* in the future, this function will probably contain additional
128 * handling for flags which are not so easily handled
129 * by a simple masking operation
132 return dev
->features
& flags_dup_features
;
135 int ethtool_op_set_flags(struct net_device
*dev
, u32 data
)
137 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
139 if (data
& ETH_FLAG_LRO
)
140 dev
->features
|= NETIF_F_LRO
;
142 dev
->features
&= ~NETIF_F_LRO
;
144 if (data
& ETH_FLAG_NTUPLE
) {
145 if (!ops
->set_rx_ntuple
)
147 dev
->features
|= NETIF_F_NTUPLE
;
149 /* safe to clear regardless */
150 dev
->features
&= ~NETIF_F_NTUPLE
;
156 void ethtool_ntuple_flush(struct net_device
*dev
)
158 struct ethtool_rx_ntuple_flow_spec_container
*fsc
, *f
;
160 list_for_each_entry_safe(fsc
, f
, &dev
->ethtool_ntuple_list
.list
, list
) {
161 list_del(&fsc
->list
);
164 dev
->ethtool_ntuple_list
.count
= 0;
166 EXPORT_SYMBOL(ethtool_ntuple_flush
);
168 /* Handlers for each ethtool command */
170 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
172 struct ethtool_cmd cmd
= { .cmd
= ETHTOOL_GSET
};
175 if (!dev
->ethtool_ops
->get_settings
)
178 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
182 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
187 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
189 struct ethtool_cmd cmd
;
191 if (!dev
->ethtool_ops
->set_settings
)
194 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
197 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
201 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
203 static noinline
int ethtool_get_drvinfo(struct net_device
*dev
, void __user
*useraddr
)
205 struct ethtool_drvinfo info
;
206 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
208 if (!ops
->get_drvinfo
)
211 memset(&info
, 0, sizeof(info
));
212 info
.cmd
= ETHTOOL_GDRVINFO
;
213 ops
->get_drvinfo(dev
, &info
);
215 if (ops
->get_sset_count
) {
218 rc
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
220 info
.testinfo_len
= rc
;
221 rc
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
224 rc
= ops
->get_sset_count(dev
, ETH_SS_PRIV_FLAGS
);
226 info
.n_priv_flags
= rc
;
228 if (ops
->get_regs_len
)
229 info
.regdump_len
= ops
->get_regs_len(dev
);
230 if (ops
->get_eeprom_len
)
231 info
.eedump_len
= ops
->get_eeprom_len(dev
);
233 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
239 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
241 static noinline
int ethtool_set_rxnfc(struct net_device
*dev
, void __user
*useraddr
)
243 struct ethtool_rxnfc cmd
;
245 if (!dev
->ethtool_ops
->set_rxnfc
)
248 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
251 return dev
->ethtool_ops
->set_rxnfc(dev
, &cmd
);
255 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
257 static noinline
int ethtool_get_rxnfc(struct net_device
*dev
, void __user
*useraddr
)
259 struct ethtool_rxnfc info
;
260 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
262 void *rule_buf
= NULL
;
267 if (copy_from_user(&info
, useraddr
, sizeof(info
)))
270 if (info
.cmd
== ETHTOOL_GRXCLSRLALL
) {
271 if (info
.rule_cnt
> 0) {
272 rule_buf
= kmalloc(info
.rule_cnt
* sizeof(u32
),
279 ret
= ops
->get_rxnfc(dev
, &info
, rule_buf
);
284 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
288 useraddr
+= offsetof(struct ethtool_rxnfc
, rule_locs
);
289 if (copy_to_user(useraddr
, rule_buf
,
290 info
.rule_cnt
* sizeof(u32
)))
301 static void __rx_ntuple_filter_add(struct ethtool_rx_ntuple_list
*list
,
302 struct ethtool_rx_ntuple_flow_spec
*spec
,
303 struct ethtool_rx_ntuple_flow_spec_container
*fsc
)
306 /* don't add filters forever */
307 if (list
->count
>= ETHTOOL_MAX_NTUPLE_LIST_ENTRY
) {
308 /* free the container */
313 /* Copy the whole filter over */
314 fsc
->fs
.flow_type
= spec
->flow_type
;
315 memcpy(&fsc
->fs
.h_u
, &spec
->h_u
, sizeof(spec
->h_u
));
316 memcpy(&fsc
->fs
.m_u
, &spec
->m_u
, sizeof(spec
->m_u
));
318 fsc
->fs
.vlan_tag
= spec
->vlan_tag
;
319 fsc
->fs
.vlan_tag_mask
= spec
->vlan_tag_mask
;
320 fsc
->fs
.data
= spec
->data
;
321 fsc
->fs
.data_mask
= spec
->data_mask
;
322 fsc
->fs
.action
= spec
->action
;
324 /* add to the list */
325 list_add_tail_rcu(&fsc
->list
, &list
->list
);
330 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
332 static noinline
int ethtool_set_rx_ntuple(struct net_device
*dev
, void __user
*useraddr
)
334 struct ethtool_rx_ntuple cmd
;
335 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
336 struct ethtool_rx_ntuple_flow_spec_container
*fsc
= NULL
;
339 if (!(dev
->features
& NETIF_F_NTUPLE
))
342 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
346 * Cache filter in dev struct for GET operation only if
347 * the underlying driver doesn't have its own GET operation, and
348 * only if the filter was added successfully. First make sure we
349 * can allocate the filter, then continue if successful.
351 if (!ops
->get_rx_ntuple
) {
352 fsc
= kmalloc(sizeof(*fsc
), GFP_ATOMIC
);
357 ret
= ops
->set_rx_ntuple(dev
, &cmd
);
363 if (!ops
->get_rx_ntuple
)
364 __rx_ntuple_filter_add(&dev
->ethtool_ntuple_list
, &cmd
.fs
, fsc
);
369 static int ethtool_get_rx_ntuple(struct net_device
*dev
, void __user
*useraddr
)
371 struct ethtool_gstrings gstrings
;
372 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
373 struct ethtool_rx_ntuple_flow_spec_container
*fsc
;
376 int ret
, i
, num_strings
= 0;
378 if (!ops
->get_sset_count
)
381 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
384 ret
= ops
->get_sset_count(dev
, gstrings
.string_set
);
390 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
394 if (ops
->get_rx_ntuple
) {
395 /* driver-specific filter grab */
396 ret
= ops
->get_rx_ntuple(dev
, gstrings
.string_set
, data
);
400 /* default ethtool filter grab */
403 list_for_each_entry(fsc
, &dev
->ethtool_ntuple_list
.list
, list
) {
404 sprintf(p
, "Filter %d:\n", i
);
405 p
+= ETH_GSTRING_LEN
;
408 switch (fsc
->fs
.flow_type
) {
410 sprintf(p
, "\tFlow Type: TCP\n");
411 p
+= ETH_GSTRING_LEN
;
415 sprintf(p
, "\tFlow Type: UDP\n");
416 p
+= ETH_GSTRING_LEN
;
420 sprintf(p
, "\tFlow Type: SCTP\n");
421 p
+= ETH_GSTRING_LEN
;
425 sprintf(p
, "\tFlow Type: AH ESP\n");
426 p
+= ETH_GSTRING_LEN
;
430 sprintf(p
, "\tFlow Type: ESP\n");
431 p
+= ETH_GSTRING_LEN
;
435 sprintf(p
, "\tFlow Type: Raw IP\n");
436 p
+= ETH_GSTRING_LEN
;
440 sprintf(p
, "\tFlow Type: IPv4\n");
441 p
+= ETH_GSTRING_LEN
;
445 sprintf(p
, "\tFlow Type: Unknown\n");
446 p
+= ETH_GSTRING_LEN
;
451 /* now the rest of the filters */
452 switch (fsc
->fs
.flow_type
) {
456 sprintf(p
, "\tSrc IP addr: 0x%x\n",
457 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4src
);
458 p
+= ETH_GSTRING_LEN
;
460 sprintf(p
, "\tSrc IP mask: 0x%x\n",
461 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4src
);
462 p
+= ETH_GSTRING_LEN
;
464 sprintf(p
, "\tDest IP addr: 0x%x\n",
465 fsc
->fs
.h_u
.tcp_ip4_spec
.ip4dst
);
466 p
+= ETH_GSTRING_LEN
;
468 sprintf(p
, "\tDest IP mask: 0x%x\n",
469 fsc
->fs
.m_u
.tcp_ip4_spec
.ip4dst
);
470 p
+= ETH_GSTRING_LEN
;
472 sprintf(p
, "\tSrc Port: %d, mask: 0x%x\n",
473 fsc
->fs
.h_u
.tcp_ip4_spec
.psrc
,
474 fsc
->fs
.m_u
.tcp_ip4_spec
.psrc
);
475 p
+= ETH_GSTRING_LEN
;
477 sprintf(p
, "\tDest Port: %d, mask: 0x%x\n",
478 fsc
->fs
.h_u
.tcp_ip4_spec
.pdst
,
479 fsc
->fs
.m_u
.tcp_ip4_spec
.pdst
);
480 p
+= ETH_GSTRING_LEN
;
482 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
483 fsc
->fs
.h_u
.tcp_ip4_spec
.tos
,
484 fsc
->fs
.m_u
.tcp_ip4_spec
.tos
);
485 p
+= ETH_GSTRING_LEN
;
490 sprintf(p
, "\tSrc IP addr: 0x%x\n",
491 fsc
->fs
.h_u
.ah_ip4_spec
.ip4src
);
492 p
+= ETH_GSTRING_LEN
;
494 sprintf(p
, "\tSrc IP mask: 0x%x\n",
495 fsc
->fs
.m_u
.ah_ip4_spec
.ip4src
);
496 p
+= ETH_GSTRING_LEN
;
498 sprintf(p
, "\tDest IP addr: 0x%x\n",
499 fsc
->fs
.h_u
.ah_ip4_spec
.ip4dst
);
500 p
+= ETH_GSTRING_LEN
;
502 sprintf(p
, "\tDest IP mask: 0x%x\n",
503 fsc
->fs
.m_u
.ah_ip4_spec
.ip4dst
);
504 p
+= ETH_GSTRING_LEN
;
506 sprintf(p
, "\tSPI: %d, mask: 0x%x\n",
507 fsc
->fs
.h_u
.ah_ip4_spec
.spi
,
508 fsc
->fs
.m_u
.ah_ip4_spec
.spi
);
509 p
+= ETH_GSTRING_LEN
;
511 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
512 fsc
->fs
.h_u
.ah_ip4_spec
.tos
,
513 fsc
->fs
.m_u
.ah_ip4_spec
.tos
);
514 p
+= ETH_GSTRING_LEN
;
518 sprintf(p
, "\tSrc IP addr: 0x%x\n",
519 fsc
->fs
.h_u
.raw_ip4_spec
.ip4src
);
520 p
+= ETH_GSTRING_LEN
;
522 sprintf(p
, "\tSrc IP mask: 0x%x\n",
523 fsc
->fs
.m_u
.raw_ip4_spec
.ip4src
);
524 p
+= ETH_GSTRING_LEN
;
526 sprintf(p
, "\tDest IP addr: 0x%x\n",
527 fsc
->fs
.h_u
.raw_ip4_spec
.ip4dst
);
528 p
+= ETH_GSTRING_LEN
;
530 sprintf(p
, "\tDest IP mask: 0x%x\n",
531 fsc
->fs
.m_u
.raw_ip4_spec
.ip4dst
);
532 p
+= ETH_GSTRING_LEN
;
536 sprintf(p
, "\tSrc IP addr: 0x%x\n",
537 fsc
->fs
.h_u
.usr_ip4_spec
.ip4src
);
538 p
+= ETH_GSTRING_LEN
;
540 sprintf(p
, "\tSrc IP mask: 0x%x\n",
541 fsc
->fs
.m_u
.usr_ip4_spec
.ip4src
);
542 p
+= ETH_GSTRING_LEN
;
544 sprintf(p
, "\tDest IP addr: 0x%x\n",
545 fsc
->fs
.h_u
.usr_ip4_spec
.ip4dst
);
546 p
+= ETH_GSTRING_LEN
;
548 sprintf(p
, "\tDest IP mask: 0x%x\n",
549 fsc
->fs
.m_u
.usr_ip4_spec
.ip4dst
);
550 p
+= ETH_GSTRING_LEN
;
552 sprintf(p
, "\tL4 bytes: 0x%x, mask: 0x%x\n",
553 fsc
->fs
.h_u
.usr_ip4_spec
.l4_4_bytes
,
554 fsc
->fs
.m_u
.usr_ip4_spec
.l4_4_bytes
);
555 p
+= ETH_GSTRING_LEN
;
557 sprintf(p
, "\tTOS: %d, mask: 0x%x\n",
558 fsc
->fs
.h_u
.usr_ip4_spec
.tos
,
559 fsc
->fs
.m_u
.usr_ip4_spec
.tos
);
560 p
+= ETH_GSTRING_LEN
;
562 sprintf(p
, "\tIP Version: %d, mask: 0x%x\n",
563 fsc
->fs
.h_u
.usr_ip4_spec
.ip_ver
,
564 fsc
->fs
.m_u
.usr_ip4_spec
.ip_ver
);
565 p
+= ETH_GSTRING_LEN
;
567 sprintf(p
, "\tProtocol: %d, mask: 0x%x\n",
568 fsc
->fs
.h_u
.usr_ip4_spec
.proto
,
569 fsc
->fs
.m_u
.usr_ip4_spec
.proto
);
570 p
+= ETH_GSTRING_LEN
;
574 sprintf(p
, "\tVLAN: %d, mask: 0x%x\n",
575 fsc
->fs
.vlan_tag
, fsc
->fs
.vlan_tag_mask
);
576 p
+= ETH_GSTRING_LEN
;
578 sprintf(p
, "\tUser-defined: 0x%Lx\n", fsc
->fs
.data
);
579 p
+= ETH_GSTRING_LEN
;
581 sprintf(p
, "\tUser-defined mask: 0x%Lx\n", fsc
->fs
.data_mask
);
582 p
+= ETH_GSTRING_LEN
;
584 if (fsc
->fs
.action
== ETHTOOL_RXNTUPLE_ACTION_DROP
)
585 sprintf(p
, "\tAction: Drop\n");
587 sprintf(p
, "\tAction: Direct to queue %d\n",
589 p
+= ETH_GSTRING_LEN
;
595 /* indicate to userspace how many strings we actually have */
596 gstrings
.len
= num_strings
;
598 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
600 useraddr
+= sizeof(gstrings
);
601 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
610 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
612 struct ethtool_regs regs
;
613 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
617 if (!ops
->get_regs
|| !ops
->get_regs_len
)
620 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
623 reglen
= ops
->get_regs_len(dev
);
624 if (regs
.len
> reglen
)
627 regbuf
= kmalloc(reglen
, GFP_USER
);
631 ops
->get_regs(dev
, ®s
, regbuf
);
634 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
636 useraddr
+= offsetof(struct ethtool_regs
, data
);
637 if (copy_to_user(useraddr
, regbuf
, regs
.len
))
646 static int ethtool_reset(struct net_device
*dev
, char __user
*useraddr
)
648 struct ethtool_value reset
;
651 if (!dev
->ethtool_ops
->reset
)
654 if (copy_from_user(&reset
, useraddr
, sizeof(reset
)))
657 ret
= dev
->ethtool_ops
->reset(dev
, &reset
.data
);
661 if (copy_to_user(useraddr
, &reset
, sizeof(reset
)))
666 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
668 struct ethtool_wolinfo wol
= { .cmd
= ETHTOOL_GWOL
};
670 if (!dev
->ethtool_ops
->get_wol
)
673 dev
->ethtool_ops
->get_wol(dev
, &wol
);
675 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
680 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
682 struct ethtool_wolinfo wol
;
684 if (!dev
->ethtool_ops
->set_wol
)
687 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
690 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
693 static int ethtool_nway_reset(struct net_device
*dev
)
695 if (!dev
->ethtool_ops
->nway_reset
)
698 return dev
->ethtool_ops
->nway_reset(dev
);
701 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
703 struct ethtool_eeprom eeprom
;
704 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
705 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
710 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
713 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
716 /* Check for wrap and zero */
717 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
720 /* Check for exceeding total eeprom len */
721 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
724 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
728 bytes_remaining
= eeprom
.len
;
729 while (bytes_remaining
> 0) {
730 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
732 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
735 if (copy_to_user(userbuf
, data
, eeprom
.len
)) {
739 userbuf
+= eeprom
.len
;
740 eeprom
.offset
+= eeprom
.len
;
741 bytes_remaining
-= eeprom
.len
;
744 eeprom
.len
= userbuf
- (useraddr
+ sizeof(eeprom
));
745 eeprom
.offset
-= eeprom
.len
;
746 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
753 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
755 struct ethtool_eeprom eeprom
;
756 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
757 void __user
*userbuf
= useraddr
+ sizeof(eeprom
);
762 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
765 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
768 /* Check for wrap and zero */
769 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
772 /* Check for exceeding total eeprom len */
773 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
776 data
= kmalloc(PAGE_SIZE
, GFP_USER
);
780 bytes_remaining
= eeprom
.len
;
781 while (bytes_remaining
> 0) {
782 eeprom
.len
= min(bytes_remaining
, (u32
)PAGE_SIZE
);
784 if (copy_from_user(data
, userbuf
, eeprom
.len
)) {
788 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
791 userbuf
+= eeprom
.len
;
792 eeprom
.offset
+= eeprom
.len
;
793 bytes_remaining
-= eeprom
.len
;
801 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
803 static noinline
int ethtool_get_coalesce(struct net_device
*dev
, void __user
*useraddr
)
805 struct ethtool_coalesce coalesce
= { .cmd
= ETHTOOL_GCOALESCE
};
807 if (!dev
->ethtool_ops
->get_coalesce
)
810 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
812 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
818 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
820 static noinline
int ethtool_set_coalesce(struct net_device
*dev
, void __user
*useraddr
)
822 struct ethtool_coalesce coalesce
;
824 if (!dev
->ethtool_ops
->set_coalesce
)
827 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
830 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
833 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
835 struct ethtool_ringparam ringparam
= { .cmd
= ETHTOOL_GRINGPARAM
};
837 if (!dev
->ethtool_ops
->get_ringparam
)
840 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
842 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
847 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
849 struct ethtool_ringparam ringparam
;
851 if (!dev
->ethtool_ops
->set_ringparam
)
854 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
857 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
860 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
862 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
864 if (!dev
->ethtool_ops
->get_pauseparam
)
867 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
869 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
874 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
876 struct ethtool_pauseparam pauseparam
;
878 if (!dev
->ethtool_ops
->set_pauseparam
)
881 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
884 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
887 static int __ethtool_set_sg(struct net_device
*dev
, u32 data
)
891 if (!data
&& dev
->ethtool_ops
->set_tso
) {
892 err
= dev
->ethtool_ops
->set_tso(dev
, 0);
897 if (!data
&& dev
->ethtool_ops
->set_ufo
) {
898 err
= dev
->ethtool_ops
->set_ufo(dev
, 0);
902 return dev
->ethtool_ops
->set_sg(dev
, data
);
905 static int ethtool_set_tx_csum(struct net_device
*dev
, char __user
*useraddr
)
907 struct ethtool_value edata
;
910 if (!dev
->ethtool_ops
->set_tx_csum
)
913 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
916 if (!edata
.data
&& dev
->ethtool_ops
->set_sg
) {
917 err
= __ethtool_set_sg(dev
, 0);
922 return dev
->ethtool_ops
->set_tx_csum(dev
, edata
.data
);
925 static int ethtool_set_rx_csum(struct net_device
*dev
, char __user
*useraddr
)
927 struct ethtool_value edata
;
929 if (!dev
->ethtool_ops
->set_rx_csum
)
932 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
935 if (!edata
.data
&& dev
->ethtool_ops
->set_sg
)
936 dev
->features
&= ~NETIF_F_GRO
;
938 return dev
->ethtool_ops
->set_rx_csum(dev
, edata
.data
);
941 static int ethtool_set_sg(struct net_device
*dev
, char __user
*useraddr
)
943 struct ethtool_value edata
;
945 if (!dev
->ethtool_ops
->set_sg
)
948 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
952 !(dev
->features
& NETIF_F_ALL_CSUM
))
955 return __ethtool_set_sg(dev
, edata
.data
);
958 static int ethtool_set_tso(struct net_device
*dev
, char __user
*useraddr
)
960 struct ethtool_value edata
;
962 if (!dev
->ethtool_ops
->set_tso
)
965 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
968 if (edata
.data
&& !(dev
->features
& NETIF_F_SG
))
971 return dev
->ethtool_ops
->set_tso(dev
, edata
.data
);
974 static int ethtool_set_ufo(struct net_device
*dev
, char __user
*useraddr
)
976 struct ethtool_value edata
;
978 if (!dev
->ethtool_ops
->set_ufo
)
980 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
982 if (edata
.data
&& !(dev
->features
& NETIF_F_SG
))
984 if (edata
.data
&& !(dev
->features
& NETIF_F_HW_CSUM
))
986 return dev
->ethtool_ops
->set_ufo(dev
, edata
.data
);
989 static int ethtool_get_gso(struct net_device
*dev
, char __user
*useraddr
)
991 struct ethtool_value edata
= { ETHTOOL_GGSO
};
993 edata
.data
= dev
->features
& NETIF_F_GSO
;
994 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
999 static int ethtool_set_gso(struct net_device
*dev
, char __user
*useraddr
)
1001 struct ethtool_value edata
;
1003 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1006 dev
->features
|= NETIF_F_GSO
;
1008 dev
->features
&= ~NETIF_F_GSO
;
1012 static int ethtool_get_gro(struct net_device
*dev
, char __user
*useraddr
)
1014 struct ethtool_value edata
= { ETHTOOL_GGRO
};
1016 edata
.data
= dev
->features
& NETIF_F_GRO
;
1017 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1022 static int ethtool_set_gro(struct net_device
*dev
, char __user
*useraddr
)
1024 struct ethtool_value edata
;
1026 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1030 if (!dev
->ethtool_ops
->get_rx_csum
||
1031 !dev
->ethtool_ops
->get_rx_csum(dev
))
1033 dev
->features
|= NETIF_F_GRO
;
1035 dev
->features
&= ~NETIF_F_GRO
;
1040 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
1042 struct ethtool_test test
;
1043 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1047 if (!ops
->self_test
|| !ops
->get_sset_count
)
1050 test_len
= ops
->get_sset_count(dev
, ETH_SS_TEST
);
1053 WARN_ON(test_len
== 0);
1055 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
1058 test
.len
= test_len
;
1059 data
= kmalloc(test_len
* sizeof(u64
), GFP_USER
);
1063 ops
->self_test(dev
, &test
, data
);
1066 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
1068 useraddr
+= sizeof(test
);
1069 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
1078 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
1080 struct ethtool_gstrings gstrings
;
1081 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1085 if (!ops
->get_strings
|| !ops
->get_sset_count
)
1088 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
1091 ret
= ops
->get_sset_count(dev
, gstrings
.string_set
);
1097 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
1101 ops
->get_strings(dev
, gstrings
.string_set
, data
);
1104 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
1106 useraddr
+= sizeof(gstrings
);
1107 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
1116 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
1118 struct ethtool_value id
;
1120 if (!dev
->ethtool_ops
->phys_id
)
1123 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
1126 return dev
->ethtool_ops
->phys_id(dev
, id
.data
);
1129 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
1131 struct ethtool_stats stats
;
1132 const struct ethtool_ops
*ops
= dev
->ethtool_ops
;
1136 if (!ops
->get_ethtool_stats
|| !ops
->get_sset_count
)
1139 n_stats
= ops
->get_sset_count(dev
, ETH_SS_STATS
);
1142 WARN_ON(n_stats
== 0);
1144 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
1147 stats
.n_stats
= n_stats
;
1148 data
= kmalloc(n_stats
* sizeof(u64
), GFP_USER
);
1152 ops
->get_ethtool_stats(dev
, &stats
, data
);
1155 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
1157 useraddr
+= sizeof(stats
);
1158 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
1167 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
1169 struct ethtool_perm_addr epaddr
;
1171 if (copy_from_user(&epaddr
, useraddr
, sizeof(epaddr
)))
1174 if (epaddr
.size
< dev
->addr_len
)
1176 epaddr
.size
= dev
->addr_len
;
1178 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
1180 useraddr
+= sizeof(epaddr
);
1181 if (copy_to_user(useraddr
, dev
->perm_addr
, epaddr
.size
))
1186 static int ethtool_get_value(struct net_device
*dev
, char __user
*useraddr
,
1187 u32 cmd
, u32 (*actor
)(struct net_device
*))
1189 struct ethtool_value edata
= { .cmd
= cmd
};
1194 edata
.data
= actor(dev
);
1196 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1201 static int ethtool_set_value_void(struct net_device
*dev
, char __user
*useraddr
,
1202 void (*actor
)(struct net_device
*, u32
))
1204 struct ethtool_value edata
;
1209 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1212 actor(dev
, edata
.data
);
1216 static int ethtool_set_value(struct net_device
*dev
, char __user
*useraddr
,
1217 int (*actor
)(struct net_device
*, u32
))
1219 struct ethtool_value edata
;
1224 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
1227 return actor(dev
, edata
.data
);
1231 * noinline attribute so that gcc doesnt use too much stack in dev_ethtool()
1233 static noinline
int ethtool_flash_device(struct net_device
*dev
, char __user
*useraddr
)
1235 struct ethtool_flash efl
;
1237 if (copy_from_user(&efl
, useraddr
, sizeof(efl
)))
1240 if (!dev
->ethtool_ops
->flash_device
)
1243 return dev
->ethtool_ops
->flash_device(dev
, &efl
);
1246 /* The main entry point in this file. Called from net/core/dev.c */
1248 int dev_ethtool(struct net
*net
, struct ifreq
*ifr
)
1250 struct net_device
*dev
= __dev_get_by_name(net
, ifr
->ifr_name
);
1251 void __user
*useraddr
= ifr
->ifr_data
;
1254 unsigned long old_features
;
1256 if (!dev
|| !netif_device_present(dev
))
1259 if (!dev
->ethtool_ops
)
1262 if (copy_from_user(ðcmd
, useraddr
, sizeof (ethcmd
)))
1265 /* Allow some commands to be done by anyone */
1267 case ETHTOOL_GDRVINFO
:
1268 case ETHTOOL_GMSGLVL
:
1269 case ETHTOOL_GCOALESCE
:
1270 case ETHTOOL_GRINGPARAM
:
1271 case ETHTOOL_GPAUSEPARAM
:
1272 case ETHTOOL_GRXCSUM
:
1273 case ETHTOOL_GTXCSUM
:
1275 case ETHTOOL_GSTRINGS
:
1277 case ETHTOOL_GPERMADDR
:
1281 case ETHTOOL_GFLAGS
:
1282 case ETHTOOL_GPFLAGS
:
1284 case ETHTOOL_GRXRINGS
:
1285 case ETHTOOL_GRXCLSRLCNT
:
1286 case ETHTOOL_GRXCLSRULE
:
1287 case ETHTOOL_GRXCLSRLALL
:
1290 if (!capable(CAP_NET_ADMIN
))
1294 if (dev
->ethtool_ops
->begin
)
1295 if ((rc
= dev
->ethtool_ops
->begin(dev
)) < 0)
1298 old_features
= dev
->features
;
1302 rc
= ethtool_get_settings(dev
, useraddr
);
1305 rc
= ethtool_set_settings(dev
, useraddr
);
1307 case ETHTOOL_GDRVINFO
:
1308 rc
= ethtool_get_drvinfo(dev
, useraddr
);
1311 rc
= ethtool_get_regs(dev
, useraddr
);
1314 rc
= ethtool_get_wol(dev
, useraddr
);
1317 rc
= ethtool_set_wol(dev
, useraddr
);
1319 case ETHTOOL_GMSGLVL
:
1320 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1321 dev
->ethtool_ops
->get_msglevel
);
1323 case ETHTOOL_SMSGLVL
:
1324 rc
= ethtool_set_value_void(dev
, useraddr
,
1325 dev
->ethtool_ops
->set_msglevel
);
1327 case ETHTOOL_NWAY_RST
:
1328 rc
= ethtool_nway_reset(dev
);
1331 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1332 dev
->ethtool_ops
->get_link
);
1334 case ETHTOOL_GEEPROM
:
1335 rc
= ethtool_get_eeprom(dev
, useraddr
);
1337 case ETHTOOL_SEEPROM
:
1338 rc
= ethtool_set_eeprom(dev
, useraddr
);
1340 case ETHTOOL_GCOALESCE
:
1341 rc
= ethtool_get_coalesce(dev
, useraddr
);
1343 case ETHTOOL_SCOALESCE
:
1344 rc
= ethtool_set_coalesce(dev
, useraddr
);
1346 case ETHTOOL_GRINGPARAM
:
1347 rc
= ethtool_get_ringparam(dev
, useraddr
);
1349 case ETHTOOL_SRINGPARAM
:
1350 rc
= ethtool_set_ringparam(dev
, useraddr
);
1352 case ETHTOOL_GPAUSEPARAM
:
1353 rc
= ethtool_get_pauseparam(dev
, useraddr
);
1355 case ETHTOOL_SPAUSEPARAM
:
1356 rc
= ethtool_set_pauseparam(dev
, useraddr
);
1358 case ETHTOOL_GRXCSUM
:
1359 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1360 (dev
->ethtool_ops
->get_rx_csum
?
1361 dev
->ethtool_ops
->get_rx_csum
:
1362 ethtool_op_get_rx_csum
));
1364 case ETHTOOL_SRXCSUM
:
1365 rc
= ethtool_set_rx_csum(dev
, useraddr
);
1367 case ETHTOOL_GTXCSUM
:
1368 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1369 (dev
->ethtool_ops
->get_tx_csum
?
1370 dev
->ethtool_ops
->get_tx_csum
:
1371 ethtool_op_get_tx_csum
));
1373 case ETHTOOL_STXCSUM
:
1374 rc
= ethtool_set_tx_csum(dev
, useraddr
);
1377 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1378 (dev
->ethtool_ops
->get_sg
?
1379 dev
->ethtool_ops
->get_sg
:
1380 ethtool_op_get_sg
));
1383 rc
= ethtool_set_sg(dev
, useraddr
);
1386 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1387 (dev
->ethtool_ops
->get_tso
?
1388 dev
->ethtool_ops
->get_tso
:
1389 ethtool_op_get_tso
));
1392 rc
= ethtool_set_tso(dev
, useraddr
);
1395 rc
= ethtool_self_test(dev
, useraddr
);
1397 case ETHTOOL_GSTRINGS
:
1398 rc
= ethtool_get_strings(dev
, useraddr
);
1400 case ETHTOOL_PHYS_ID
:
1401 rc
= ethtool_phys_id(dev
, useraddr
);
1403 case ETHTOOL_GSTATS
:
1404 rc
= ethtool_get_stats(dev
, useraddr
);
1406 case ETHTOOL_GPERMADDR
:
1407 rc
= ethtool_get_perm_addr(dev
, useraddr
);
1410 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1411 (dev
->ethtool_ops
->get_ufo
?
1412 dev
->ethtool_ops
->get_ufo
:
1413 ethtool_op_get_ufo
));
1416 rc
= ethtool_set_ufo(dev
, useraddr
);
1419 rc
= ethtool_get_gso(dev
, useraddr
);
1422 rc
= ethtool_set_gso(dev
, useraddr
);
1424 case ETHTOOL_GFLAGS
:
1425 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1426 (dev
->ethtool_ops
->get_flags
?
1427 dev
->ethtool_ops
->get_flags
:
1428 ethtool_op_get_flags
));
1430 case ETHTOOL_SFLAGS
:
1431 rc
= ethtool_set_value(dev
, useraddr
,
1432 dev
->ethtool_ops
->set_flags
);
1434 case ETHTOOL_GPFLAGS
:
1435 rc
= ethtool_get_value(dev
, useraddr
, ethcmd
,
1436 dev
->ethtool_ops
->get_priv_flags
);
1438 case ETHTOOL_SPFLAGS
:
1439 rc
= ethtool_set_value(dev
, useraddr
,
1440 dev
->ethtool_ops
->set_priv_flags
);
1443 case ETHTOOL_GRXRINGS
:
1444 case ETHTOOL_GRXCLSRLCNT
:
1445 case ETHTOOL_GRXCLSRULE
:
1446 case ETHTOOL_GRXCLSRLALL
:
1447 rc
= ethtool_get_rxnfc(dev
, useraddr
);
1450 case ETHTOOL_SRXCLSRLDEL
:
1451 case ETHTOOL_SRXCLSRLINS
:
1452 rc
= ethtool_set_rxnfc(dev
, useraddr
);
1455 rc
= ethtool_get_gro(dev
, useraddr
);
1458 rc
= ethtool_set_gro(dev
, useraddr
);
1460 case ETHTOOL_FLASHDEV
:
1461 rc
= ethtool_flash_device(dev
, useraddr
);
1464 rc
= ethtool_reset(dev
, useraddr
);
1466 case ETHTOOL_SRXNTUPLE
:
1467 rc
= ethtool_set_rx_ntuple(dev
, useraddr
);
1469 case ETHTOOL_GRXNTUPLE
:
1470 rc
= ethtool_get_rx_ntuple(dev
, useraddr
);
1476 if (dev
->ethtool_ops
->complete
)
1477 dev
->ethtool_ops
->complete(dev
);
1479 if (old_features
!= dev
->features
)
1480 netdev_features_change(dev
);
1485 EXPORT_SYMBOL(ethtool_op_get_link
);
1486 EXPORT_SYMBOL(ethtool_op_get_sg
);
1487 EXPORT_SYMBOL(ethtool_op_get_tso
);
1488 EXPORT_SYMBOL(ethtool_op_set_sg
);
1489 EXPORT_SYMBOL(ethtool_op_set_tso
);
1490 EXPORT_SYMBOL(ethtool_op_set_tx_csum
);
1491 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum
);
1492 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum
);
1493 EXPORT_SYMBOL(ethtool_op_set_ufo
);
1494 EXPORT_SYMBOL(ethtool_op_get_ufo
);
1495 EXPORT_SYMBOL(ethtool_op_set_flags
);
1496 EXPORT_SYMBOL(ethtool_op_get_flags
);