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_tx_csum(struct net_device *dev)
35 return (dev->features & NETIF_F_ALL_CSUM) != 0;
38 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
41 dev->features |= NETIF_F_IP_CSUM;
43 dev->features &= ~NETIF_F_IP_CSUM;
48 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
51 dev->features |= NETIF_F_HW_CSUM;
53 dev->features &= ~NETIF_F_HW_CSUM;
58 int ethtool_op_set_tx_ipv6_csum(struct net_device *dev, u32 data)
61 dev->features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
63 dev->features &= ~(NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM);
68 u32 ethtool_op_get_sg(struct net_device *dev)
70 return (dev->features & NETIF_F_SG) != 0;
73 int ethtool_op_set_sg(struct net_device *dev, u32 data)
76 dev->features |= NETIF_F_SG;
78 dev->features &= ~NETIF_F_SG;
83 u32 ethtool_op_get_tso(struct net_device *dev)
85 return (dev->features & NETIF_F_TSO) != 0;
88 int ethtool_op_set_tso(struct net_device *dev, u32 data)
91 dev->features |= NETIF_F_TSO;
93 dev->features &= ~NETIF_F_TSO;
98 int ethtool_op_get_perm_addr(struct net_device *dev, struct ethtool_perm_addr *addr, u8 *data)
100 unsigned char len = dev->addr_len;
101 if ( addr->size < len )
105 memcpy(data, dev->perm_addr, len);
110 u32 ethtool_op_get_ufo(struct net_device *dev)
112 return (dev->features & NETIF_F_UFO) != 0;
115 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
118 dev->features |= NETIF_F_UFO;
120 dev->features &= ~NETIF_F_UFO;
124 /* Handlers for each ethtool command */
126 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
128 struct ethtool_cmd cmd = { ETHTOOL_GSET };
131 if (!dev->ethtool_ops->get_settings)
134 err = dev->ethtool_ops->get_settings(dev, &cmd);
138 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
143 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
145 struct ethtool_cmd cmd;
147 if (!dev->ethtool_ops->set_settings)
150 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
153 return dev->ethtool_ops->set_settings(dev, &cmd);
156 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
158 struct ethtool_drvinfo info;
159 const struct ethtool_ops *ops = dev->ethtool_ops;
161 if (!ops->get_drvinfo)
164 memset(&info, 0, sizeof(info));
165 info.cmd = ETHTOOL_GDRVINFO;
166 ops->get_drvinfo(dev, &info);
168 if (ops->self_test_count)
169 info.testinfo_len = ops->self_test_count(dev);
170 if (ops->get_stats_count)
171 info.n_stats = ops->get_stats_count(dev);
172 if (ops->get_regs_len)
173 info.regdump_len = ops->get_regs_len(dev);
174 if (ops->get_eeprom_len)
175 info.eedump_len = ops->get_eeprom_len(dev);
177 if (copy_to_user(useraddr, &info, sizeof(info)))
182 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
184 struct ethtool_regs regs;
185 const struct ethtool_ops *ops = dev->ethtool_ops;
189 if (!ops->get_regs || !ops->get_regs_len)
192 if (copy_from_user(®s, useraddr, sizeof(regs)))
195 reglen = ops->get_regs_len(dev);
196 if (regs.len > reglen)
199 regbuf = kmalloc(reglen, GFP_USER);
203 ops->get_regs(dev, ®s, regbuf);
206 if (copy_to_user(useraddr, ®s, sizeof(regs)))
208 useraddr += offsetof(struct ethtool_regs, data);
209 if (copy_to_user(useraddr, regbuf, regs.len))
218 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
220 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
222 if (!dev->ethtool_ops->get_wol)
225 dev->ethtool_ops->get_wol(dev, &wol);
227 if (copy_to_user(useraddr, &wol, sizeof(wol)))
232 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
234 struct ethtool_wolinfo wol;
236 if (!dev->ethtool_ops->set_wol)
239 if (copy_from_user(&wol, useraddr, sizeof(wol)))
242 return dev->ethtool_ops->set_wol(dev, &wol);
245 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
247 struct ethtool_value edata = { ETHTOOL_GMSGLVL };
249 if (!dev->ethtool_ops->get_msglevel)
252 edata.data = dev->ethtool_ops->get_msglevel(dev);
254 if (copy_to_user(useraddr, &edata, sizeof(edata)))
259 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
261 struct ethtool_value edata;
263 if (!dev->ethtool_ops->set_msglevel)
266 if (copy_from_user(&edata, useraddr, sizeof(edata)))
269 dev->ethtool_ops->set_msglevel(dev, edata.data);
273 static int ethtool_nway_reset(struct net_device *dev)
275 if (!dev->ethtool_ops->nway_reset)
278 return dev->ethtool_ops->nway_reset(dev);
281 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
283 struct ethtool_value edata = { ETHTOOL_GLINK };
285 if (!dev->ethtool_ops->get_link)
288 edata.data = dev->ethtool_ops->get_link(dev);
290 if (copy_to_user(useraddr, &edata, sizeof(edata)))
295 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
297 struct ethtool_eeprom eeprom;
298 const struct ethtool_ops *ops = dev->ethtool_ops;
302 if (!ops->get_eeprom || !ops->get_eeprom_len)
305 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
308 /* Check for wrap and zero */
309 if (eeprom.offset + eeprom.len <= eeprom.offset)
312 /* Check for exceeding total eeprom len */
313 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
316 data = kmalloc(eeprom.len, GFP_USER);
321 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
324 ret = ops->get_eeprom(dev, &eeprom, data);
329 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
331 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
340 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
342 struct ethtool_eeprom eeprom;
343 const struct ethtool_ops *ops = dev->ethtool_ops;
347 if (!ops->set_eeprom || !ops->get_eeprom_len)
350 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
353 /* Check for wrap and zero */
354 if (eeprom.offset + eeprom.len <= eeprom.offset)
357 /* Check for exceeding total eeprom len */
358 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
361 data = kmalloc(eeprom.len, GFP_USER);
366 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
369 ret = ops->set_eeprom(dev, &eeprom, data);
373 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
381 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
383 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
385 if (!dev->ethtool_ops->get_coalesce)
388 dev->ethtool_ops->get_coalesce(dev, &coalesce);
390 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
395 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
397 struct ethtool_coalesce coalesce;
399 if (!dev->ethtool_ops->set_coalesce)
402 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
405 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
408 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
410 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
412 if (!dev->ethtool_ops->get_ringparam)
415 dev->ethtool_ops->get_ringparam(dev, &ringparam);
417 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
422 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
424 struct ethtool_ringparam ringparam;
426 if (!dev->ethtool_ops->set_ringparam)
429 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
432 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
435 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
437 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
439 if (!dev->ethtool_ops->get_pauseparam)
442 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
444 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
449 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
451 struct ethtool_pauseparam pauseparam;
453 if (!dev->ethtool_ops->set_pauseparam)
456 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
459 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
462 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
464 struct ethtool_value edata = { ETHTOOL_GRXCSUM };
466 if (!dev->ethtool_ops->get_rx_csum)
469 edata.data = dev->ethtool_ops->get_rx_csum(dev);
471 if (copy_to_user(useraddr, &edata, sizeof(edata)))
476 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
478 struct ethtool_value edata;
480 if (!dev->ethtool_ops->set_rx_csum)
483 if (copy_from_user(&edata, useraddr, sizeof(edata)))
486 dev->ethtool_ops->set_rx_csum(dev, edata.data);
490 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
492 struct ethtool_value edata = { ETHTOOL_GTXCSUM };
494 if (!dev->ethtool_ops->get_tx_csum)
497 edata.data = dev->ethtool_ops->get_tx_csum(dev);
499 if (copy_to_user(useraddr, &edata, sizeof(edata)))
504 static int __ethtool_set_sg(struct net_device *dev, u32 data)
508 if (!data && dev->ethtool_ops->set_tso) {
509 err = dev->ethtool_ops->set_tso(dev, 0);
514 if (!data && dev->ethtool_ops->set_ufo) {
515 err = dev->ethtool_ops->set_ufo(dev, 0);
519 return dev->ethtool_ops->set_sg(dev, data);
522 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
524 struct ethtool_value edata;
527 if (!dev->ethtool_ops->set_tx_csum)
530 if (copy_from_user(&edata, useraddr, sizeof(edata)))
533 if (!edata.data && dev->ethtool_ops->set_sg) {
534 err = __ethtool_set_sg(dev, 0);
539 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
542 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
544 struct ethtool_value edata = { ETHTOOL_GSG };
546 if (!dev->ethtool_ops->get_sg)
549 edata.data = dev->ethtool_ops->get_sg(dev);
551 if (copy_to_user(useraddr, &edata, sizeof(edata)))
556 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
558 struct ethtool_value edata;
560 if (!dev->ethtool_ops->set_sg)
563 if (copy_from_user(&edata, useraddr, sizeof(edata)))
567 !(dev->features & NETIF_F_ALL_CSUM))
570 return __ethtool_set_sg(dev, edata.data);
573 static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
575 struct ethtool_value edata = { ETHTOOL_GTSO };
577 if (!dev->ethtool_ops->get_tso)
580 edata.data = dev->ethtool_ops->get_tso(dev);
582 if (copy_to_user(useraddr, &edata, sizeof(edata)))
587 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
589 struct ethtool_value edata;
591 if (!dev->ethtool_ops->set_tso)
594 if (copy_from_user(&edata, useraddr, sizeof(edata)))
597 if (edata.data && !(dev->features & NETIF_F_SG))
600 return dev->ethtool_ops->set_tso(dev, edata.data);
603 static int ethtool_get_ufo(struct net_device *dev, char __user *useraddr)
605 struct ethtool_value edata = { ETHTOOL_GUFO };
607 if (!dev->ethtool_ops->get_ufo)
609 edata.data = dev->ethtool_ops->get_ufo(dev);
610 if (copy_to_user(useraddr, &edata, sizeof(edata)))
615 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
617 struct ethtool_value edata;
619 if (!dev->ethtool_ops->set_ufo)
621 if (copy_from_user(&edata, useraddr, sizeof(edata)))
623 if (edata.data && !(dev->features & NETIF_F_SG))
625 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
627 return dev->ethtool_ops->set_ufo(dev, edata.data);
630 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
632 struct ethtool_value edata = { ETHTOOL_GGSO };
634 edata.data = dev->features & NETIF_F_GSO;
635 if (copy_to_user(useraddr, &edata, sizeof(edata)))
640 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
642 struct ethtool_value edata;
644 if (copy_from_user(&edata, useraddr, sizeof(edata)))
647 dev->features |= NETIF_F_GSO;
649 dev->features &= ~NETIF_F_GSO;
653 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
655 struct ethtool_test test;
656 const struct ethtool_ops *ops = dev->ethtool_ops;
660 if (!ops->self_test || !ops->self_test_count)
663 if (copy_from_user(&test, useraddr, sizeof(test)))
666 test.len = ops->self_test_count(dev);
667 data = kmalloc(test.len * sizeof(u64), GFP_USER);
671 ops->self_test(dev, &test, data);
674 if (copy_to_user(useraddr, &test, sizeof(test)))
676 useraddr += sizeof(test);
677 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
686 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
688 struct ethtool_gstrings gstrings;
689 const struct ethtool_ops *ops = dev->ethtool_ops;
693 if (!ops->get_strings)
696 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
699 switch (gstrings.string_set) {
701 if (!ops->self_test_count)
703 gstrings.len = ops->self_test_count(dev);
706 if (!ops->get_stats_count)
708 gstrings.len = ops->get_stats_count(dev);
714 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
718 ops->get_strings(dev, gstrings.string_set, data);
721 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
723 useraddr += sizeof(gstrings);
724 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
733 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
735 struct ethtool_value id;
737 if (!dev->ethtool_ops->phys_id)
740 if (copy_from_user(&id, useraddr, sizeof(id)))
743 return dev->ethtool_ops->phys_id(dev, id.data);
746 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
748 struct ethtool_stats stats;
749 const struct ethtool_ops *ops = dev->ethtool_ops;
753 if (!ops->get_ethtool_stats || !ops->get_stats_count)
756 if (copy_from_user(&stats, useraddr, sizeof(stats)))
759 stats.n_stats = ops->get_stats_count(dev);
760 data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
764 ops->get_ethtool_stats(dev, &stats, data);
767 if (copy_to_user(useraddr, &stats, sizeof(stats)))
769 useraddr += sizeof(stats);
770 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
779 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
781 struct ethtool_perm_addr epaddr;
785 if (!dev->ethtool_ops->get_perm_addr)
788 if (copy_from_user(&epaddr,useraddr,sizeof(epaddr)))
791 data = kmalloc(epaddr.size, GFP_USER);
795 ret = dev->ethtool_ops->get_perm_addr(dev,&epaddr,data);
800 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
802 useraddr += sizeof(epaddr);
803 if (copy_to_user(useraddr, data, epaddr.size))
812 /* The main entry point in this file. Called from net/core/dev.c */
814 int dev_ethtool(struct ifreq *ifr)
816 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
817 void __user *useraddr = ifr->ifr_data;
820 unsigned long old_features;
822 if (!dev || !netif_device_present(dev))
825 if (!dev->ethtool_ops)
828 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
831 /* Allow some commands to be done by anyone */
833 case ETHTOOL_GDRVINFO:
834 case ETHTOOL_GMSGLVL:
835 case ETHTOOL_GCOALESCE:
836 case ETHTOOL_GRINGPARAM:
837 case ETHTOOL_GPAUSEPARAM:
838 case ETHTOOL_GRXCSUM:
839 case ETHTOOL_GTXCSUM:
841 case ETHTOOL_GSTRINGS:
843 case ETHTOOL_GPERMADDR:
848 if (!capable(CAP_NET_ADMIN))
852 if (dev->ethtool_ops->begin)
853 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
856 old_features = dev->features;
860 rc = ethtool_get_settings(dev, useraddr);
863 rc = ethtool_set_settings(dev, useraddr);
865 case ETHTOOL_GDRVINFO:
866 rc = ethtool_get_drvinfo(dev, useraddr);
869 rc = ethtool_get_regs(dev, useraddr);
872 rc = ethtool_get_wol(dev, useraddr);
875 rc = ethtool_set_wol(dev, useraddr);
877 case ETHTOOL_GMSGLVL:
878 rc = ethtool_get_msglevel(dev, useraddr);
880 case ETHTOOL_SMSGLVL:
881 rc = ethtool_set_msglevel(dev, useraddr);
883 case ETHTOOL_NWAY_RST:
884 rc = ethtool_nway_reset(dev);
887 rc = ethtool_get_link(dev, useraddr);
889 case ETHTOOL_GEEPROM:
890 rc = ethtool_get_eeprom(dev, useraddr);
892 case ETHTOOL_SEEPROM:
893 rc = ethtool_set_eeprom(dev, useraddr);
895 case ETHTOOL_GCOALESCE:
896 rc = ethtool_get_coalesce(dev, useraddr);
898 case ETHTOOL_SCOALESCE:
899 rc = ethtool_set_coalesce(dev, useraddr);
901 case ETHTOOL_GRINGPARAM:
902 rc = ethtool_get_ringparam(dev, useraddr);
904 case ETHTOOL_SRINGPARAM:
905 rc = ethtool_set_ringparam(dev, useraddr);
907 case ETHTOOL_GPAUSEPARAM:
908 rc = ethtool_get_pauseparam(dev, useraddr);
910 case ETHTOOL_SPAUSEPARAM:
911 rc = ethtool_set_pauseparam(dev, useraddr);
913 case ETHTOOL_GRXCSUM:
914 rc = ethtool_get_rx_csum(dev, useraddr);
916 case ETHTOOL_SRXCSUM:
917 rc = ethtool_set_rx_csum(dev, useraddr);
919 case ETHTOOL_GTXCSUM:
920 rc = ethtool_get_tx_csum(dev, useraddr);
922 case ETHTOOL_STXCSUM:
923 rc = ethtool_set_tx_csum(dev, useraddr);
926 rc = ethtool_get_sg(dev, useraddr);
929 rc = ethtool_set_sg(dev, useraddr);
932 rc = ethtool_get_tso(dev, useraddr);
935 rc = ethtool_set_tso(dev, useraddr);
938 rc = ethtool_self_test(dev, useraddr);
940 case ETHTOOL_GSTRINGS:
941 rc = ethtool_get_strings(dev, useraddr);
943 case ETHTOOL_PHYS_ID:
944 rc = ethtool_phys_id(dev, useraddr);
947 rc = ethtool_get_stats(dev, useraddr);
949 case ETHTOOL_GPERMADDR:
950 rc = ethtool_get_perm_addr(dev, useraddr);
953 rc = ethtool_get_ufo(dev, useraddr);
956 rc = ethtool_set_ufo(dev, useraddr);
959 rc = ethtool_get_gso(dev, useraddr);
962 rc = ethtool_set_gso(dev, useraddr);
968 if (dev->ethtool_ops->complete)
969 dev->ethtool_ops->complete(dev);
971 if (old_features != dev->features)
972 netdev_features_change(dev);
977 EXPORT_SYMBOL(dev_ethtool);
978 EXPORT_SYMBOL(ethtool_op_get_link);
979 EXPORT_SYMBOL_GPL(ethtool_op_get_perm_addr);
980 EXPORT_SYMBOL(ethtool_op_get_sg);
981 EXPORT_SYMBOL(ethtool_op_get_tso);
982 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
983 EXPORT_SYMBOL(ethtool_op_set_sg);
984 EXPORT_SYMBOL(ethtool_op_set_tso);
985 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
986 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
987 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
988 EXPORT_SYMBOL(ethtool_op_set_ufo);
989 EXPORT_SYMBOL(ethtool_op_get_ufo);