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 u32 ethtool_op_get_ufo(struct net_device *dev)
100 return (dev->features & NETIF_F_UFO) != 0;
103 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
106 dev->features |= NETIF_F_UFO;
108 dev->features &= ~NETIF_F_UFO;
112 /* Handlers for each ethtool command */
114 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
116 struct ethtool_cmd cmd = { ETHTOOL_GSET };
119 if (!dev->ethtool_ops->get_settings)
122 err = dev->ethtool_ops->get_settings(dev, &cmd);
126 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
131 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
133 struct ethtool_cmd cmd;
135 if (!dev->ethtool_ops->set_settings)
138 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
141 return dev->ethtool_ops->set_settings(dev, &cmd);
144 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
146 struct ethtool_drvinfo info;
147 const struct ethtool_ops *ops = dev->ethtool_ops;
149 if (!ops->get_drvinfo)
152 memset(&info, 0, sizeof(info));
153 info.cmd = ETHTOOL_GDRVINFO;
154 ops->get_drvinfo(dev, &info);
156 if (ops->self_test_count)
157 info.testinfo_len = ops->self_test_count(dev);
158 if (ops->get_stats_count)
159 info.n_stats = ops->get_stats_count(dev);
160 if (ops->get_regs_len)
161 info.regdump_len = ops->get_regs_len(dev);
162 if (ops->get_eeprom_len)
163 info.eedump_len = ops->get_eeprom_len(dev);
165 if (copy_to_user(useraddr, &info, sizeof(info)))
170 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
172 struct ethtool_regs regs;
173 const struct ethtool_ops *ops = dev->ethtool_ops;
177 if (!ops->get_regs || !ops->get_regs_len)
180 if (copy_from_user(®s, useraddr, sizeof(regs)))
183 reglen = ops->get_regs_len(dev);
184 if (regs.len > reglen)
187 regbuf = kmalloc(reglen, GFP_USER);
191 ops->get_regs(dev, ®s, regbuf);
194 if (copy_to_user(useraddr, ®s, sizeof(regs)))
196 useraddr += offsetof(struct ethtool_regs, data);
197 if (copy_to_user(useraddr, regbuf, regs.len))
206 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
208 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
210 if (!dev->ethtool_ops->get_wol)
213 dev->ethtool_ops->get_wol(dev, &wol);
215 if (copy_to_user(useraddr, &wol, sizeof(wol)))
220 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
222 struct ethtool_wolinfo wol;
224 if (!dev->ethtool_ops->set_wol)
227 if (copy_from_user(&wol, useraddr, sizeof(wol)))
230 return dev->ethtool_ops->set_wol(dev, &wol);
233 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
235 struct ethtool_value edata = { ETHTOOL_GMSGLVL };
237 if (!dev->ethtool_ops->get_msglevel)
240 edata.data = dev->ethtool_ops->get_msglevel(dev);
242 if (copy_to_user(useraddr, &edata, sizeof(edata)))
247 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
249 struct ethtool_value edata;
251 if (!dev->ethtool_ops->set_msglevel)
254 if (copy_from_user(&edata, useraddr, sizeof(edata)))
257 dev->ethtool_ops->set_msglevel(dev, edata.data);
261 static int ethtool_nway_reset(struct net_device *dev)
263 if (!dev->ethtool_ops->nway_reset)
266 return dev->ethtool_ops->nway_reset(dev);
269 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
271 struct ethtool_value edata = { ETHTOOL_GLINK };
273 if (!dev->ethtool_ops->get_link)
276 edata.data = dev->ethtool_ops->get_link(dev);
278 if (copy_to_user(useraddr, &edata, sizeof(edata)))
283 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
285 struct ethtool_eeprom eeprom;
286 const struct ethtool_ops *ops = dev->ethtool_ops;
290 if (!ops->get_eeprom || !ops->get_eeprom_len)
293 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
296 /* Check for wrap and zero */
297 if (eeprom.offset + eeprom.len <= eeprom.offset)
300 /* Check for exceeding total eeprom len */
301 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
304 data = kmalloc(eeprom.len, GFP_USER);
309 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
312 ret = ops->get_eeprom(dev, &eeprom, data);
317 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
319 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
328 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
330 struct ethtool_eeprom eeprom;
331 const struct ethtool_ops *ops = dev->ethtool_ops;
335 if (!ops->set_eeprom || !ops->get_eeprom_len)
338 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
341 /* Check for wrap and zero */
342 if (eeprom.offset + eeprom.len <= eeprom.offset)
345 /* Check for exceeding total eeprom len */
346 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
349 data = kmalloc(eeprom.len, GFP_USER);
354 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
357 ret = ops->set_eeprom(dev, &eeprom, data);
361 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
369 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
371 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
373 if (!dev->ethtool_ops->get_coalesce)
376 dev->ethtool_ops->get_coalesce(dev, &coalesce);
378 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
383 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
385 struct ethtool_coalesce coalesce;
387 if (!dev->ethtool_ops->set_coalesce)
390 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
393 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
396 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
398 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
400 if (!dev->ethtool_ops->get_ringparam)
403 dev->ethtool_ops->get_ringparam(dev, &ringparam);
405 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
410 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
412 struct ethtool_ringparam ringparam;
414 if (!dev->ethtool_ops->set_ringparam)
417 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
420 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
423 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
425 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
427 if (!dev->ethtool_ops->get_pauseparam)
430 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
432 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
437 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
439 struct ethtool_pauseparam pauseparam;
441 if (!dev->ethtool_ops->set_pauseparam)
444 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
447 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
450 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
452 struct ethtool_value edata = { ETHTOOL_GRXCSUM };
454 if (!dev->ethtool_ops->get_rx_csum)
457 edata.data = dev->ethtool_ops->get_rx_csum(dev);
459 if (copy_to_user(useraddr, &edata, sizeof(edata)))
464 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
466 struct ethtool_value edata;
468 if (!dev->ethtool_ops->set_rx_csum)
471 if (copy_from_user(&edata, useraddr, sizeof(edata)))
474 dev->ethtool_ops->set_rx_csum(dev, edata.data);
478 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
480 struct ethtool_value edata = { ETHTOOL_GTXCSUM };
482 if (!dev->ethtool_ops->get_tx_csum)
485 edata.data = dev->ethtool_ops->get_tx_csum(dev);
487 if (copy_to_user(useraddr, &edata, sizeof(edata)))
492 static int __ethtool_set_sg(struct net_device *dev, u32 data)
496 if (!data && dev->ethtool_ops->set_tso) {
497 err = dev->ethtool_ops->set_tso(dev, 0);
502 if (!data && dev->ethtool_ops->set_ufo) {
503 err = dev->ethtool_ops->set_ufo(dev, 0);
507 return dev->ethtool_ops->set_sg(dev, data);
510 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
512 struct ethtool_value edata;
515 if (!dev->ethtool_ops->set_tx_csum)
518 if (copy_from_user(&edata, useraddr, sizeof(edata)))
521 if (!edata.data && dev->ethtool_ops->set_sg) {
522 err = __ethtool_set_sg(dev, 0);
527 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
530 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
532 struct ethtool_value edata = { ETHTOOL_GSG };
534 if (!dev->ethtool_ops->get_sg)
537 edata.data = dev->ethtool_ops->get_sg(dev);
539 if (copy_to_user(useraddr, &edata, sizeof(edata)))
544 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
546 struct ethtool_value edata;
548 if (!dev->ethtool_ops->set_sg)
551 if (copy_from_user(&edata, useraddr, sizeof(edata)))
555 !(dev->features & NETIF_F_ALL_CSUM))
558 return __ethtool_set_sg(dev, edata.data);
561 static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
563 struct ethtool_value edata = { ETHTOOL_GTSO };
565 if (!dev->ethtool_ops->get_tso)
568 edata.data = dev->ethtool_ops->get_tso(dev);
570 if (copy_to_user(useraddr, &edata, sizeof(edata)))
575 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
577 struct ethtool_value edata;
579 if (!dev->ethtool_ops->set_tso)
582 if (copy_from_user(&edata, useraddr, sizeof(edata)))
585 if (edata.data && !(dev->features & NETIF_F_SG))
588 return dev->ethtool_ops->set_tso(dev, edata.data);
591 static int ethtool_get_ufo(struct net_device *dev, char __user *useraddr)
593 struct ethtool_value edata = { ETHTOOL_GUFO };
595 if (!dev->ethtool_ops->get_ufo)
597 edata.data = dev->ethtool_ops->get_ufo(dev);
598 if (copy_to_user(useraddr, &edata, sizeof(edata)))
603 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
605 struct ethtool_value edata;
607 if (!dev->ethtool_ops->set_ufo)
609 if (copy_from_user(&edata, useraddr, sizeof(edata)))
611 if (edata.data && !(dev->features & NETIF_F_SG))
613 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
615 return dev->ethtool_ops->set_ufo(dev, edata.data);
618 static int ethtool_get_gso(struct net_device *dev, char __user *useraddr)
620 struct ethtool_value edata = { ETHTOOL_GGSO };
622 edata.data = dev->features & NETIF_F_GSO;
623 if (copy_to_user(useraddr, &edata, sizeof(edata)))
628 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
630 struct ethtool_value edata;
632 if (copy_from_user(&edata, useraddr, sizeof(edata)))
635 dev->features |= NETIF_F_GSO;
637 dev->features &= ~NETIF_F_GSO;
641 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
643 struct ethtool_test test;
644 const struct ethtool_ops *ops = dev->ethtool_ops;
648 if (!ops->self_test || !ops->self_test_count)
651 if (copy_from_user(&test, useraddr, sizeof(test)))
654 test.len = ops->self_test_count(dev);
655 data = kmalloc(test.len * sizeof(u64), GFP_USER);
659 ops->self_test(dev, &test, data);
662 if (copy_to_user(useraddr, &test, sizeof(test)))
664 useraddr += sizeof(test);
665 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
674 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
676 struct ethtool_gstrings gstrings;
677 const struct ethtool_ops *ops = dev->ethtool_ops;
681 if (!ops->get_strings)
684 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
687 switch (gstrings.string_set) {
689 if (!ops->self_test_count)
691 gstrings.len = ops->self_test_count(dev);
694 if (!ops->get_stats_count)
696 gstrings.len = ops->get_stats_count(dev);
702 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
706 ops->get_strings(dev, gstrings.string_set, data);
709 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
711 useraddr += sizeof(gstrings);
712 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
721 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
723 struct ethtool_value id;
725 if (!dev->ethtool_ops->phys_id)
728 if (copy_from_user(&id, useraddr, sizeof(id)))
731 return dev->ethtool_ops->phys_id(dev, id.data);
734 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
736 struct ethtool_stats stats;
737 const struct ethtool_ops *ops = dev->ethtool_ops;
741 if (!ops->get_ethtool_stats || !ops->get_stats_count)
744 if (copy_from_user(&stats, useraddr, sizeof(stats)))
747 stats.n_stats = ops->get_stats_count(dev);
748 data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
752 ops->get_ethtool_stats(dev, &stats, data);
755 if (copy_to_user(useraddr, &stats, sizeof(stats)))
757 useraddr += sizeof(stats);
758 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
767 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
769 struct ethtool_perm_addr epaddr;
771 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
774 if (epaddr.size < dev->addr_len)
776 epaddr.size = dev->addr_len;
778 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
780 useraddr += sizeof(epaddr);
781 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
786 /* The main entry point in this file. Called from net/core/dev.c */
788 int dev_ethtool(struct ifreq *ifr)
790 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
791 void __user *useraddr = ifr->ifr_data;
794 unsigned long old_features;
796 if (!dev || !netif_device_present(dev))
799 if (!dev->ethtool_ops)
802 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
805 /* Allow some commands to be done by anyone */
807 case ETHTOOL_GDRVINFO:
808 case ETHTOOL_GMSGLVL:
809 case ETHTOOL_GCOALESCE:
810 case ETHTOOL_GRINGPARAM:
811 case ETHTOOL_GPAUSEPARAM:
812 case ETHTOOL_GRXCSUM:
813 case ETHTOOL_GTXCSUM:
815 case ETHTOOL_GSTRINGS:
817 case ETHTOOL_GPERMADDR:
822 if (!capable(CAP_NET_ADMIN))
826 if (dev->ethtool_ops->begin)
827 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
830 old_features = dev->features;
834 rc = ethtool_get_settings(dev, useraddr);
837 rc = ethtool_set_settings(dev, useraddr);
839 case ETHTOOL_GDRVINFO:
840 rc = ethtool_get_drvinfo(dev, useraddr);
843 rc = ethtool_get_regs(dev, useraddr);
846 rc = ethtool_get_wol(dev, useraddr);
849 rc = ethtool_set_wol(dev, useraddr);
851 case ETHTOOL_GMSGLVL:
852 rc = ethtool_get_msglevel(dev, useraddr);
854 case ETHTOOL_SMSGLVL:
855 rc = ethtool_set_msglevel(dev, useraddr);
857 case ETHTOOL_NWAY_RST:
858 rc = ethtool_nway_reset(dev);
861 rc = ethtool_get_link(dev, useraddr);
863 case ETHTOOL_GEEPROM:
864 rc = ethtool_get_eeprom(dev, useraddr);
866 case ETHTOOL_SEEPROM:
867 rc = ethtool_set_eeprom(dev, useraddr);
869 case ETHTOOL_GCOALESCE:
870 rc = ethtool_get_coalesce(dev, useraddr);
872 case ETHTOOL_SCOALESCE:
873 rc = ethtool_set_coalesce(dev, useraddr);
875 case ETHTOOL_GRINGPARAM:
876 rc = ethtool_get_ringparam(dev, useraddr);
878 case ETHTOOL_SRINGPARAM:
879 rc = ethtool_set_ringparam(dev, useraddr);
881 case ETHTOOL_GPAUSEPARAM:
882 rc = ethtool_get_pauseparam(dev, useraddr);
884 case ETHTOOL_SPAUSEPARAM:
885 rc = ethtool_set_pauseparam(dev, useraddr);
887 case ETHTOOL_GRXCSUM:
888 rc = ethtool_get_rx_csum(dev, useraddr);
890 case ETHTOOL_SRXCSUM:
891 rc = ethtool_set_rx_csum(dev, useraddr);
893 case ETHTOOL_GTXCSUM:
894 rc = ethtool_get_tx_csum(dev, useraddr);
896 case ETHTOOL_STXCSUM:
897 rc = ethtool_set_tx_csum(dev, useraddr);
900 rc = ethtool_get_sg(dev, useraddr);
903 rc = ethtool_set_sg(dev, useraddr);
906 rc = ethtool_get_tso(dev, useraddr);
909 rc = ethtool_set_tso(dev, useraddr);
912 rc = ethtool_self_test(dev, useraddr);
914 case ETHTOOL_GSTRINGS:
915 rc = ethtool_get_strings(dev, useraddr);
917 case ETHTOOL_PHYS_ID:
918 rc = ethtool_phys_id(dev, useraddr);
921 rc = ethtool_get_stats(dev, useraddr);
923 case ETHTOOL_GPERMADDR:
924 rc = ethtool_get_perm_addr(dev, useraddr);
927 rc = ethtool_get_ufo(dev, useraddr);
930 rc = ethtool_set_ufo(dev, useraddr);
933 rc = ethtool_get_gso(dev, useraddr);
936 rc = ethtool_set_gso(dev, useraddr);
942 if (dev->ethtool_ops->complete)
943 dev->ethtool_ops->complete(dev);
945 if (old_features != dev->features)
946 netdev_features_change(dev);
951 EXPORT_SYMBOL(ethtool_op_get_link);
952 EXPORT_SYMBOL(ethtool_op_get_sg);
953 EXPORT_SYMBOL(ethtool_op_get_tso);
954 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
955 EXPORT_SYMBOL(ethtool_op_set_sg);
956 EXPORT_SYMBOL(ethtool_op_set_tso);
957 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
958 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
959 EXPORT_SYMBOL(ethtool_op_set_tx_ipv6_csum);
960 EXPORT_SYMBOL(ethtool_op_set_ufo);
961 EXPORT_SYMBOL(ethtool_op_get_ufo);