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. We fall back to calling do_ioctl()
7 * for drivers which haven't been converted to ethtool_ops yet.
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <linux/ethtool.h>
16 #include <linux/netdevice.h>
17 #include <asm/uaccess.h>
20 * Some useful ethtool_ops methods that're device independent.
21 * If we find that all drivers want to do the same thing here,
22 * we can turn these into dev_() function calls.
25 u32
ethtool_op_get_link(struct net_device
*dev
)
27 return netif_carrier_ok(dev
) ? 1 : 0;
30 u32
ethtool_op_get_tx_csum(struct net_device
*dev
)
32 return (dev
->features
& (NETIF_F_IP_CSUM
| NETIF_F_HW_CSUM
)) != 0;
35 int ethtool_op_set_tx_csum(struct net_device
*dev
, u32 data
)
38 dev
->features
|= NETIF_F_IP_CSUM
;
40 dev
->features
&= ~NETIF_F_IP_CSUM
;
45 int ethtool_op_set_tx_hw_csum(struct net_device
*dev
, u32 data
)
48 dev
->features
|= NETIF_F_HW_CSUM
;
50 dev
->features
&= ~NETIF_F_HW_CSUM
;
54 u32
ethtool_op_get_sg(struct net_device
*dev
)
56 return (dev
->features
& NETIF_F_SG
) != 0;
59 int ethtool_op_set_sg(struct net_device
*dev
, u32 data
)
62 dev
->features
|= NETIF_F_SG
;
64 dev
->features
&= ~NETIF_F_SG
;
69 u32
ethtool_op_get_tso(struct net_device
*dev
)
71 return (dev
->features
& NETIF_F_TSO
) != 0;
74 int ethtool_op_set_tso(struct net_device
*dev
, u32 data
)
77 dev
->features
|= NETIF_F_TSO
;
79 dev
->features
&= ~NETIF_F_TSO
;
84 int ethtool_op_get_perm_addr(struct net_device
*dev
, struct ethtool_perm_addr
*addr
, u8
*data
)
86 unsigned char len
= dev
->addr_len
;
87 if ( addr
->size
< len
)
91 memcpy(data
, dev
->perm_addr
, len
);
96 /* Handlers for each ethtool command */
98 static int ethtool_get_settings(struct net_device
*dev
, void __user
*useraddr
)
100 struct ethtool_cmd cmd
= { ETHTOOL_GSET
};
103 if (!dev
->ethtool_ops
->get_settings
)
106 err
= dev
->ethtool_ops
->get_settings(dev
, &cmd
);
110 if (copy_to_user(useraddr
, &cmd
, sizeof(cmd
)))
115 static int ethtool_set_settings(struct net_device
*dev
, void __user
*useraddr
)
117 struct ethtool_cmd cmd
;
119 if (!dev
->ethtool_ops
->set_settings
)
122 if (copy_from_user(&cmd
, useraddr
, sizeof(cmd
)))
125 return dev
->ethtool_ops
->set_settings(dev
, &cmd
);
128 static int ethtool_get_drvinfo(struct net_device
*dev
, void __user
*useraddr
)
130 struct ethtool_drvinfo info
;
131 struct ethtool_ops
*ops
= dev
->ethtool_ops
;
133 if (!ops
->get_drvinfo
)
136 memset(&info
, 0, sizeof(info
));
137 info
.cmd
= ETHTOOL_GDRVINFO
;
138 ops
->get_drvinfo(dev
, &info
);
140 if (ops
->self_test_count
)
141 info
.testinfo_len
= ops
->self_test_count(dev
);
142 if (ops
->get_stats_count
)
143 info
.n_stats
= ops
->get_stats_count(dev
);
144 if (ops
->get_regs_len
)
145 info
.regdump_len
= ops
->get_regs_len(dev
);
146 if (ops
->get_eeprom_len
)
147 info
.eedump_len
= ops
->get_eeprom_len(dev
);
149 if (copy_to_user(useraddr
, &info
, sizeof(info
)))
154 static int ethtool_get_regs(struct net_device
*dev
, char __user
*useraddr
)
156 struct ethtool_regs regs
;
157 struct ethtool_ops
*ops
= dev
->ethtool_ops
;
161 if (!ops
->get_regs
|| !ops
->get_regs_len
)
164 if (copy_from_user(®s
, useraddr
, sizeof(regs
)))
167 reglen
= ops
->get_regs_len(dev
);
168 if (regs
.len
> reglen
)
171 regbuf
= kmalloc(reglen
, GFP_USER
);
175 ops
->get_regs(dev
, ®s
, regbuf
);
178 if (copy_to_user(useraddr
, ®s
, sizeof(regs
)))
180 useraddr
+= offsetof(struct ethtool_regs
, data
);
181 if (copy_to_user(useraddr
, regbuf
, regs
.len
))
190 static int ethtool_get_wol(struct net_device
*dev
, char __user
*useraddr
)
192 struct ethtool_wolinfo wol
= { ETHTOOL_GWOL
};
194 if (!dev
->ethtool_ops
->get_wol
)
197 dev
->ethtool_ops
->get_wol(dev
, &wol
);
199 if (copy_to_user(useraddr
, &wol
, sizeof(wol
)))
204 static int ethtool_set_wol(struct net_device
*dev
, char __user
*useraddr
)
206 struct ethtool_wolinfo wol
;
208 if (!dev
->ethtool_ops
->set_wol
)
211 if (copy_from_user(&wol
, useraddr
, sizeof(wol
)))
214 return dev
->ethtool_ops
->set_wol(dev
, &wol
);
217 static int ethtool_get_msglevel(struct net_device
*dev
, char __user
*useraddr
)
219 struct ethtool_value edata
= { ETHTOOL_GMSGLVL
};
221 if (!dev
->ethtool_ops
->get_msglevel
)
224 edata
.data
= dev
->ethtool_ops
->get_msglevel(dev
);
226 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
231 static int ethtool_set_msglevel(struct net_device
*dev
, char __user
*useraddr
)
233 struct ethtool_value edata
;
235 if (!dev
->ethtool_ops
->set_msglevel
)
238 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
241 dev
->ethtool_ops
->set_msglevel(dev
, edata
.data
);
245 static int ethtool_nway_reset(struct net_device
*dev
)
247 if (!dev
->ethtool_ops
->nway_reset
)
250 return dev
->ethtool_ops
->nway_reset(dev
);
253 static int ethtool_get_link(struct net_device
*dev
, void __user
*useraddr
)
255 struct ethtool_value edata
= { ETHTOOL_GLINK
};
257 if (!dev
->ethtool_ops
->get_link
)
260 edata
.data
= dev
->ethtool_ops
->get_link(dev
);
262 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
267 static int ethtool_get_eeprom(struct net_device
*dev
, void __user
*useraddr
)
269 struct ethtool_eeprom eeprom
;
270 struct ethtool_ops
*ops
= dev
->ethtool_ops
;
274 if (!ops
->get_eeprom
|| !ops
->get_eeprom_len
)
277 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
280 /* Check for wrap and zero */
281 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
284 /* Check for exceeding total eeprom len */
285 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
288 data
= kmalloc(eeprom
.len
, GFP_USER
);
293 if (copy_from_user(data
, useraddr
+ sizeof(eeprom
), eeprom
.len
))
296 ret
= ops
->get_eeprom(dev
, &eeprom
, data
);
301 if (copy_to_user(useraddr
, &eeprom
, sizeof(eeprom
)))
303 if (copy_to_user(useraddr
+ sizeof(eeprom
), data
, eeprom
.len
))
312 static int ethtool_set_eeprom(struct net_device
*dev
, void __user
*useraddr
)
314 struct ethtool_eeprom eeprom
;
315 struct ethtool_ops
*ops
= dev
->ethtool_ops
;
319 if (!ops
->set_eeprom
|| !ops
->get_eeprom_len
)
322 if (copy_from_user(&eeprom
, useraddr
, sizeof(eeprom
)))
325 /* Check for wrap and zero */
326 if (eeprom
.offset
+ eeprom
.len
<= eeprom
.offset
)
329 /* Check for exceeding total eeprom len */
330 if (eeprom
.offset
+ eeprom
.len
> ops
->get_eeprom_len(dev
))
333 data
= kmalloc(eeprom
.len
, GFP_USER
);
338 if (copy_from_user(data
, useraddr
+ sizeof(eeprom
), eeprom
.len
))
341 ret
= ops
->set_eeprom(dev
, &eeprom
, data
);
345 if (copy_to_user(useraddr
+ sizeof(eeprom
), data
, eeprom
.len
))
353 static int ethtool_get_coalesce(struct net_device
*dev
, void __user
*useraddr
)
355 struct ethtool_coalesce coalesce
= { ETHTOOL_GCOALESCE
};
357 if (!dev
->ethtool_ops
->get_coalesce
)
360 dev
->ethtool_ops
->get_coalesce(dev
, &coalesce
);
362 if (copy_to_user(useraddr
, &coalesce
, sizeof(coalesce
)))
367 static int ethtool_set_coalesce(struct net_device
*dev
, void __user
*useraddr
)
369 struct ethtool_coalesce coalesce
;
371 if (!dev
->ethtool_ops
->set_coalesce
)
374 if (copy_from_user(&coalesce
, useraddr
, sizeof(coalesce
)))
377 return dev
->ethtool_ops
->set_coalesce(dev
, &coalesce
);
380 static int ethtool_get_ringparam(struct net_device
*dev
, void __user
*useraddr
)
382 struct ethtool_ringparam ringparam
= { ETHTOOL_GRINGPARAM
};
384 if (!dev
->ethtool_ops
->get_ringparam
)
387 dev
->ethtool_ops
->get_ringparam(dev
, &ringparam
);
389 if (copy_to_user(useraddr
, &ringparam
, sizeof(ringparam
)))
394 static int ethtool_set_ringparam(struct net_device
*dev
, void __user
*useraddr
)
396 struct ethtool_ringparam ringparam
;
398 if (!dev
->ethtool_ops
->set_ringparam
)
401 if (copy_from_user(&ringparam
, useraddr
, sizeof(ringparam
)))
404 return dev
->ethtool_ops
->set_ringparam(dev
, &ringparam
);
407 static int ethtool_get_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
409 struct ethtool_pauseparam pauseparam
= { ETHTOOL_GPAUSEPARAM
};
411 if (!dev
->ethtool_ops
->get_pauseparam
)
414 dev
->ethtool_ops
->get_pauseparam(dev
, &pauseparam
);
416 if (copy_to_user(useraddr
, &pauseparam
, sizeof(pauseparam
)))
421 static int ethtool_set_pauseparam(struct net_device
*dev
, void __user
*useraddr
)
423 struct ethtool_pauseparam pauseparam
;
425 if (!dev
->ethtool_ops
->get_pauseparam
)
428 if (copy_from_user(&pauseparam
, useraddr
, sizeof(pauseparam
)))
431 return dev
->ethtool_ops
->set_pauseparam(dev
, &pauseparam
);
434 static int ethtool_get_rx_csum(struct net_device
*dev
, char __user
*useraddr
)
436 struct ethtool_value edata
= { ETHTOOL_GRXCSUM
};
438 if (!dev
->ethtool_ops
->get_rx_csum
)
441 edata
.data
= dev
->ethtool_ops
->get_rx_csum(dev
);
443 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
448 static int ethtool_set_rx_csum(struct net_device
*dev
, char __user
*useraddr
)
450 struct ethtool_value edata
;
452 if (!dev
->ethtool_ops
->set_rx_csum
)
455 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
458 dev
->ethtool_ops
->set_rx_csum(dev
, edata
.data
);
462 static int ethtool_get_tx_csum(struct net_device
*dev
, char __user
*useraddr
)
464 struct ethtool_value edata
= { ETHTOOL_GTXCSUM
};
466 if (!dev
->ethtool_ops
->get_tx_csum
)
469 edata
.data
= dev
->ethtool_ops
->get_tx_csum(dev
);
471 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
476 static int __ethtool_set_sg(struct net_device
*dev
, u32 data
)
480 if (!data
&& dev
->ethtool_ops
->set_tso
) {
481 err
= dev
->ethtool_ops
->set_tso(dev
, 0);
486 return dev
->ethtool_ops
->set_sg(dev
, data
);
489 static int ethtool_set_tx_csum(struct net_device
*dev
, char __user
*useraddr
)
491 struct ethtool_value edata
;
494 if (!dev
->ethtool_ops
->set_tx_csum
)
497 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
500 if (!edata
.data
&& dev
->ethtool_ops
->set_sg
) {
501 err
= __ethtool_set_sg(dev
, 0);
506 return dev
->ethtool_ops
->set_tx_csum(dev
, edata
.data
);
509 static int ethtool_get_sg(struct net_device
*dev
, char __user
*useraddr
)
511 struct ethtool_value edata
= { ETHTOOL_GSG
};
513 if (!dev
->ethtool_ops
->get_sg
)
516 edata
.data
= dev
->ethtool_ops
->get_sg(dev
);
518 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
523 static int ethtool_set_sg(struct net_device
*dev
, char __user
*useraddr
)
525 struct ethtool_value edata
;
527 if (!dev
->ethtool_ops
->set_sg
)
530 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
534 !(dev
->features
& (NETIF_F_IP_CSUM
|
539 return __ethtool_set_sg(dev
, edata
.data
);
542 static int ethtool_get_tso(struct net_device
*dev
, char __user
*useraddr
)
544 struct ethtool_value edata
= { ETHTOOL_GTSO
};
546 if (!dev
->ethtool_ops
->get_tso
)
549 edata
.data
= dev
->ethtool_ops
->get_tso(dev
);
551 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
556 static int ethtool_set_tso(struct net_device
*dev
, char __user
*useraddr
)
558 struct ethtool_value edata
;
560 if (!dev
->ethtool_ops
->set_tso
)
563 if (copy_from_user(&edata
, useraddr
, sizeof(edata
)))
566 if (edata
.data
&& !(dev
->features
& NETIF_F_SG
))
569 return dev
->ethtool_ops
->set_tso(dev
, edata
.data
);
572 static int ethtool_self_test(struct net_device
*dev
, char __user
*useraddr
)
574 struct ethtool_test test
;
575 struct ethtool_ops
*ops
= dev
->ethtool_ops
;
579 if (!ops
->self_test
|| !ops
->self_test_count
)
582 if (copy_from_user(&test
, useraddr
, sizeof(test
)))
585 test
.len
= ops
->self_test_count(dev
);
586 data
= kmalloc(test
.len
* sizeof(u64
), GFP_USER
);
590 ops
->self_test(dev
, &test
, data
);
593 if (copy_to_user(useraddr
, &test
, sizeof(test
)))
595 useraddr
+= sizeof(test
);
596 if (copy_to_user(useraddr
, data
, test
.len
* sizeof(u64
)))
605 static int ethtool_get_strings(struct net_device
*dev
, void __user
*useraddr
)
607 struct ethtool_gstrings gstrings
;
608 struct ethtool_ops
*ops
= dev
->ethtool_ops
;
612 if (!ops
->get_strings
)
615 if (copy_from_user(&gstrings
, useraddr
, sizeof(gstrings
)))
618 switch (gstrings
.string_set
) {
620 if (!ops
->self_test_count
)
622 gstrings
.len
= ops
->self_test_count(dev
);
625 if (!ops
->get_stats_count
)
627 gstrings
.len
= ops
->get_stats_count(dev
);
633 data
= kmalloc(gstrings
.len
* ETH_GSTRING_LEN
, GFP_USER
);
637 ops
->get_strings(dev
, gstrings
.string_set
, data
);
640 if (copy_to_user(useraddr
, &gstrings
, sizeof(gstrings
)))
642 useraddr
+= sizeof(gstrings
);
643 if (copy_to_user(useraddr
, data
, gstrings
.len
* ETH_GSTRING_LEN
))
652 static int ethtool_phys_id(struct net_device
*dev
, void __user
*useraddr
)
654 struct ethtool_value id
;
656 if (!dev
->ethtool_ops
->phys_id
)
659 if (copy_from_user(&id
, useraddr
, sizeof(id
)))
662 return dev
->ethtool_ops
->phys_id(dev
, id
.data
);
665 static int ethtool_get_stats(struct net_device
*dev
, void __user
*useraddr
)
667 struct ethtool_stats stats
;
668 struct ethtool_ops
*ops
= dev
->ethtool_ops
;
672 if (!ops
->get_ethtool_stats
|| !ops
->get_stats_count
)
675 if (copy_from_user(&stats
, useraddr
, sizeof(stats
)))
678 stats
.n_stats
= ops
->get_stats_count(dev
);
679 data
= kmalloc(stats
.n_stats
* sizeof(u64
), GFP_USER
);
683 ops
->get_ethtool_stats(dev
, &stats
, data
);
686 if (copy_to_user(useraddr
, &stats
, sizeof(stats
)))
688 useraddr
+= sizeof(stats
);
689 if (copy_to_user(useraddr
, data
, stats
.n_stats
* sizeof(u64
)))
698 static int ethtool_get_perm_addr(struct net_device
*dev
, void __user
*useraddr
)
700 struct ethtool_perm_addr epaddr
;
704 if (!dev
->ethtool_ops
->get_perm_addr
)
707 if (copy_from_user(&epaddr
,useraddr
,sizeof(epaddr
)))
710 data
= kmalloc(epaddr
.size
, GFP_USER
);
714 ret
= dev
->ethtool_ops
->get_perm_addr(dev
,&epaddr
,data
);
719 if (copy_to_user(useraddr
, &epaddr
, sizeof(epaddr
)))
721 useraddr
+= sizeof(epaddr
);
722 if (copy_to_user(useraddr
, data
, epaddr
.size
))
731 /* The main entry point in this file. Called from net/core/dev.c */
733 int dev_ethtool(struct ifreq
*ifr
)
735 struct net_device
*dev
= __dev_get_by_name(ifr
->ifr_name
);
736 void __user
*useraddr
= ifr
->ifr_data
;
739 unsigned long old_features
;
742 * XXX: This can be pushed down into the ethtool_* handlers that
743 * need it. Keep existing behaviour for the moment.
745 if (!capable(CAP_NET_ADMIN
))
748 if (!dev
|| !netif_device_present(dev
))
751 if (!dev
->ethtool_ops
)
754 if (copy_from_user(ðcmd
, useraddr
, sizeof (ethcmd
)))
757 if(dev
->ethtool_ops
->begin
)
758 if ((rc
= dev
->ethtool_ops
->begin(dev
)) < 0)
761 old_features
= dev
->features
;
765 rc
= ethtool_get_settings(dev
, useraddr
);
768 rc
= ethtool_set_settings(dev
, useraddr
);
770 case ETHTOOL_GDRVINFO
:
771 rc
= ethtool_get_drvinfo(dev
, useraddr
);
774 rc
= ethtool_get_regs(dev
, useraddr
);
777 rc
= ethtool_get_wol(dev
, useraddr
);
780 rc
= ethtool_set_wol(dev
, useraddr
);
782 case ETHTOOL_GMSGLVL
:
783 rc
= ethtool_get_msglevel(dev
, useraddr
);
785 case ETHTOOL_SMSGLVL
:
786 rc
= ethtool_set_msglevel(dev
, useraddr
);
788 case ETHTOOL_NWAY_RST
:
789 rc
= ethtool_nway_reset(dev
);
792 rc
= ethtool_get_link(dev
, useraddr
);
794 case ETHTOOL_GEEPROM
:
795 rc
= ethtool_get_eeprom(dev
, useraddr
);
797 case ETHTOOL_SEEPROM
:
798 rc
= ethtool_set_eeprom(dev
, useraddr
);
800 case ETHTOOL_GCOALESCE
:
801 rc
= ethtool_get_coalesce(dev
, useraddr
);
803 case ETHTOOL_SCOALESCE
:
804 rc
= ethtool_set_coalesce(dev
, useraddr
);
806 case ETHTOOL_GRINGPARAM
:
807 rc
= ethtool_get_ringparam(dev
, useraddr
);
809 case ETHTOOL_SRINGPARAM
:
810 rc
= ethtool_set_ringparam(dev
, useraddr
);
812 case ETHTOOL_GPAUSEPARAM
:
813 rc
= ethtool_get_pauseparam(dev
, useraddr
);
815 case ETHTOOL_SPAUSEPARAM
:
816 rc
= ethtool_set_pauseparam(dev
, useraddr
);
818 case ETHTOOL_GRXCSUM
:
819 rc
= ethtool_get_rx_csum(dev
, useraddr
);
821 case ETHTOOL_SRXCSUM
:
822 rc
= ethtool_set_rx_csum(dev
, useraddr
);
824 case ETHTOOL_GTXCSUM
:
825 rc
= ethtool_get_tx_csum(dev
, useraddr
);
827 case ETHTOOL_STXCSUM
:
828 rc
= ethtool_set_tx_csum(dev
, useraddr
);
831 rc
= ethtool_get_sg(dev
, useraddr
);
834 rc
= ethtool_set_sg(dev
, useraddr
);
837 rc
= ethtool_get_tso(dev
, useraddr
);
840 rc
= ethtool_set_tso(dev
, useraddr
);
843 rc
= ethtool_self_test(dev
, useraddr
);
845 case ETHTOOL_GSTRINGS
:
846 rc
= ethtool_get_strings(dev
, useraddr
);
848 case ETHTOOL_PHYS_ID
:
849 rc
= ethtool_phys_id(dev
, useraddr
);
852 rc
= ethtool_get_stats(dev
, useraddr
);
854 case ETHTOOL_GPERMADDR
:
855 rc
= ethtool_get_perm_addr(dev
, useraddr
);
861 if(dev
->ethtool_ops
->complete
)
862 dev
->ethtool_ops
->complete(dev
);
864 if (old_features
!= dev
->features
)
865 netdev_features_change(dev
);
871 return dev
->do_ioctl(dev
, ifr
, SIOCETHTOOL
);
875 EXPORT_SYMBOL(dev_ethtool
);
876 EXPORT_SYMBOL(ethtool_op_get_link
);
877 EXPORT_SYMBOL_GPL(ethtool_op_get_perm_addr
);
878 EXPORT_SYMBOL(ethtool_op_get_sg
);
879 EXPORT_SYMBOL(ethtool_op_get_tso
);
880 EXPORT_SYMBOL(ethtool_op_get_tx_csum
);
881 EXPORT_SYMBOL(ethtool_op_set_sg
);
882 EXPORT_SYMBOL(ethtool_op_set_tso
);
883 EXPORT_SYMBOL(ethtool_op_set_tx_csum
);
884 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum
);