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 u32 ethtool_op_get_ufo(struct net_device *dev)
98 return (dev->features & NETIF_F_UFO) != 0;
101 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
104 dev->features |= NETIF_F_UFO;
106 dev->features &= ~NETIF_F_UFO;
110 /* Handlers for each ethtool command */
112 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
114 struct ethtool_cmd cmd = { ETHTOOL_GSET };
117 if (!dev->ethtool_ops->get_settings)
120 err = dev->ethtool_ops->get_settings(dev, &cmd);
124 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
129 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
131 struct ethtool_cmd cmd;
133 if (!dev->ethtool_ops->set_settings)
136 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
139 return dev->ethtool_ops->set_settings(dev, &cmd);
142 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
144 struct ethtool_drvinfo info;
145 struct ethtool_ops *ops = dev->ethtool_ops;
147 if (!ops->get_drvinfo)
150 memset(&info, 0, sizeof(info));
151 info.cmd = ETHTOOL_GDRVINFO;
152 ops->get_drvinfo(dev, &info);
154 if (ops->self_test_count)
155 info.testinfo_len = ops->self_test_count(dev);
156 if (ops->get_stats_count)
157 info.n_stats = ops->get_stats_count(dev);
158 if (ops->get_regs_len)
159 info.regdump_len = ops->get_regs_len(dev);
160 if (ops->get_eeprom_len)
161 info.eedump_len = ops->get_eeprom_len(dev);
163 if (copy_to_user(useraddr, &info, sizeof(info)))
168 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
170 struct ethtool_regs regs;
171 struct ethtool_ops *ops = dev->ethtool_ops;
175 if (!ops->get_regs || !ops->get_regs_len)
178 if (copy_from_user(®s, useraddr, sizeof(regs)))
181 reglen = ops->get_regs_len(dev);
182 if (regs.len > reglen)
185 regbuf = kmalloc(reglen, GFP_USER);
189 ops->get_regs(dev, ®s, regbuf);
192 if (copy_to_user(useraddr, ®s, sizeof(regs)))
194 useraddr += offsetof(struct ethtool_regs, data);
195 if (copy_to_user(useraddr, regbuf, regs.len))
204 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
206 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
208 if (!dev->ethtool_ops->get_wol)
211 dev->ethtool_ops->get_wol(dev, &wol);
213 if (copy_to_user(useraddr, &wol, sizeof(wol)))
218 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
220 struct ethtool_wolinfo wol;
222 if (!dev->ethtool_ops->set_wol)
225 if (copy_from_user(&wol, useraddr, sizeof(wol)))
228 return dev->ethtool_ops->set_wol(dev, &wol);
231 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
233 struct ethtool_value edata = { ETHTOOL_GMSGLVL };
235 if (!dev->ethtool_ops->get_msglevel)
238 edata.data = dev->ethtool_ops->get_msglevel(dev);
240 if (copy_to_user(useraddr, &edata, sizeof(edata)))
245 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
247 struct ethtool_value edata;
249 if (!dev->ethtool_ops->set_msglevel)
252 if (copy_from_user(&edata, useraddr, sizeof(edata)))
255 dev->ethtool_ops->set_msglevel(dev, edata.data);
259 static int ethtool_nway_reset(struct net_device *dev)
261 if (!dev->ethtool_ops->nway_reset)
264 return dev->ethtool_ops->nway_reset(dev);
267 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
269 struct ethtool_value edata = { ETHTOOL_GLINK };
271 if (!dev->ethtool_ops->get_link)
274 edata.data = dev->ethtool_ops->get_link(dev);
276 if (copy_to_user(useraddr, &edata, sizeof(edata)))
281 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
283 struct ethtool_eeprom eeprom;
284 struct ethtool_ops *ops = dev->ethtool_ops;
288 if (!ops->get_eeprom || !ops->get_eeprom_len)
291 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
294 /* Check for wrap and zero */
295 if (eeprom.offset + eeprom.len <= eeprom.offset)
298 /* Check for exceeding total eeprom len */
299 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
302 data = kmalloc(eeprom.len, GFP_USER);
307 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
310 ret = ops->get_eeprom(dev, &eeprom, data);
315 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
317 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
326 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
328 struct ethtool_eeprom eeprom;
329 struct ethtool_ops *ops = dev->ethtool_ops;
333 if (!ops->set_eeprom || !ops->get_eeprom_len)
336 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
339 /* Check for wrap and zero */
340 if (eeprom.offset + eeprom.len <= eeprom.offset)
343 /* Check for exceeding total eeprom len */
344 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
347 data = kmalloc(eeprom.len, GFP_USER);
352 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
355 ret = ops->set_eeprom(dev, &eeprom, data);
359 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
367 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
369 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
371 if (!dev->ethtool_ops->get_coalesce)
374 dev->ethtool_ops->get_coalesce(dev, &coalesce);
376 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
381 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
383 struct ethtool_coalesce coalesce;
385 if (!dev->ethtool_ops->set_coalesce)
388 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
391 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
394 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
396 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
398 if (!dev->ethtool_ops->get_ringparam)
401 dev->ethtool_ops->get_ringparam(dev, &ringparam);
403 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
408 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
410 struct ethtool_ringparam ringparam;
412 if (!dev->ethtool_ops->set_ringparam)
415 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
418 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
421 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
423 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
425 if (!dev->ethtool_ops->get_pauseparam)
428 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
430 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
435 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
437 struct ethtool_pauseparam pauseparam;
439 if (!dev->ethtool_ops->get_pauseparam)
442 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
445 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
448 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
450 struct ethtool_value edata = { ETHTOOL_GRXCSUM };
452 if (!dev->ethtool_ops->get_rx_csum)
455 edata.data = dev->ethtool_ops->get_rx_csum(dev);
457 if (copy_to_user(useraddr, &edata, sizeof(edata)))
462 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
464 struct ethtool_value edata;
466 if (!dev->ethtool_ops->set_rx_csum)
469 if (copy_from_user(&edata, useraddr, sizeof(edata)))
472 dev->ethtool_ops->set_rx_csum(dev, edata.data);
476 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
478 struct ethtool_value edata = { ETHTOOL_GTXCSUM };
480 if (!dev->ethtool_ops->get_tx_csum)
483 edata.data = dev->ethtool_ops->get_tx_csum(dev);
485 if (copy_to_user(useraddr, &edata, sizeof(edata)))
490 static int __ethtool_set_sg(struct net_device *dev, u32 data)
494 if (!data && dev->ethtool_ops->set_tso) {
495 err = dev->ethtool_ops->set_tso(dev, 0);
500 if (!data && dev->ethtool_ops->set_ufo) {
501 err = dev->ethtool_ops->set_ufo(dev, 0);
505 return dev->ethtool_ops->set_sg(dev, data);
508 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
510 struct ethtool_value edata;
513 if (!dev->ethtool_ops->set_tx_csum)
516 if (copy_from_user(&edata, useraddr, sizeof(edata)))
519 if (!edata.data && dev->ethtool_ops->set_sg) {
520 err = __ethtool_set_sg(dev, 0);
525 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
528 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
530 struct ethtool_value edata = { ETHTOOL_GSG };
532 if (!dev->ethtool_ops->get_sg)
535 edata.data = dev->ethtool_ops->get_sg(dev);
537 if (copy_to_user(useraddr, &edata, sizeof(edata)))
542 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
544 struct ethtool_value edata;
546 if (!dev->ethtool_ops->set_sg)
549 if (copy_from_user(&edata, useraddr, sizeof(edata)))
553 !(dev->features & (NETIF_F_IP_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_GTSO };
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)))
602 static int ethtool_set_ufo(struct net_device *dev, char __user *useraddr)
604 struct ethtool_value edata;
606 if (!dev->ethtool_ops->set_ufo)
608 if (copy_from_user(&edata, useraddr, sizeof(edata)))
610 if (edata.data && !(dev->features & NETIF_F_SG))
612 if (edata.data && !(dev->features & NETIF_F_HW_CSUM))
614 return dev->ethtool_ops->set_ufo(dev, edata.data);
617 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
619 struct ethtool_test test;
620 struct ethtool_ops *ops = dev->ethtool_ops;
624 if (!ops->self_test || !ops->self_test_count)
627 if (copy_from_user(&test, useraddr, sizeof(test)))
630 test.len = ops->self_test_count(dev);
631 data = kmalloc(test.len * sizeof(u64), GFP_USER);
635 ops->self_test(dev, &test, data);
638 if (copy_to_user(useraddr, &test, sizeof(test)))
640 useraddr += sizeof(test);
641 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
650 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
652 struct ethtool_gstrings gstrings;
653 struct ethtool_ops *ops = dev->ethtool_ops;
657 if (!ops->get_strings)
660 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
663 switch (gstrings.string_set) {
665 if (!ops->self_test_count)
667 gstrings.len = ops->self_test_count(dev);
670 if (!ops->get_stats_count)
672 gstrings.len = ops->get_stats_count(dev);
678 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
682 ops->get_strings(dev, gstrings.string_set, data);
685 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
687 useraddr += sizeof(gstrings);
688 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
697 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
699 struct ethtool_value id;
701 if (!dev->ethtool_ops->phys_id)
704 if (copy_from_user(&id, useraddr, sizeof(id)))
707 return dev->ethtool_ops->phys_id(dev, id.data);
710 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
712 struct ethtool_stats stats;
713 struct ethtool_ops *ops = dev->ethtool_ops;
717 if (!ops->get_ethtool_stats || !ops->get_stats_count)
720 if (copy_from_user(&stats, useraddr, sizeof(stats)))
723 stats.n_stats = ops->get_stats_count(dev);
724 data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
728 ops->get_ethtool_stats(dev, &stats, data);
731 if (copy_to_user(useraddr, &stats, sizeof(stats)))
733 useraddr += sizeof(stats);
734 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
743 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
745 struct ethtool_perm_addr epaddr;
749 if (!dev->ethtool_ops->get_perm_addr)
752 if (copy_from_user(&epaddr,useraddr,sizeof(epaddr)))
755 data = kmalloc(epaddr.size, GFP_USER);
759 ret = dev->ethtool_ops->get_perm_addr(dev,&epaddr,data);
764 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
766 useraddr += sizeof(epaddr);
767 if (copy_to_user(useraddr, data, epaddr.size))
776 /* The main entry point in this file. Called from net/core/dev.c */
778 int dev_ethtool(struct ifreq *ifr)
780 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
781 void __user *useraddr = ifr->ifr_data;
784 unsigned long old_features;
787 * XXX: This can be pushed down into the ethtool_* handlers that
788 * need it. Keep existing behaviour for the moment.
790 if (!capable(CAP_NET_ADMIN))
793 if (!dev || !netif_device_present(dev))
796 if (!dev->ethtool_ops)
799 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
802 if(dev->ethtool_ops->begin)
803 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
806 old_features = dev->features;
810 rc = ethtool_get_settings(dev, useraddr);
813 rc = ethtool_set_settings(dev, useraddr);
815 case ETHTOOL_GDRVINFO:
816 rc = ethtool_get_drvinfo(dev, useraddr);
819 rc = ethtool_get_regs(dev, useraddr);
822 rc = ethtool_get_wol(dev, useraddr);
825 rc = ethtool_set_wol(dev, useraddr);
827 case ETHTOOL_GMSGLVL:
828 rc = ethtool_get_msglevel(dev, useraddr);
830 case ETHTOOL_SMSGLVL:
831 rc = ethtool_set_msglevel(dev, useraddr);
833 case ETHTOOL_NWAY_RST:
834 rc = ethtool_nway_reset(dev);
837 rc = ethtool_get_link(dev, useraddr);
839 case ETHTOOL_GEEPROM:
840 rc = ethtool_get_eeprom(dev, useraddr);
842 case ETHTOOL_SEEPROM:
843 rc = ethtool_set_eeprom(dev, useraddr);
845 case ETHTOOL_GCOALESCE:
846 rc = ethtool_get_coalesce(dev, useraddr);
848 case ETHTOOL_SCOALESCE:
849 rc = ethtool_set_coalesce(dev, useraddr);
851 case ETHTOOL_GRINGPARAM:
852 rc = ethtool_get_ringparam(dev, useraddr);
854 case ETHTOOL_SRINGPARAM:
855 rc = ethtool_set_ringparam(dev, useraddr);
857 case ETHTOOL_GPAUSEPARAM:
858 rc = ethtool_get_pauseparam(dev, useraddr);
860 case ETHTOOL_SPAUSEPARAM:
861 rc = ethtool_set_pauseparam(dev, useraddr);
863 case ETHTOOL_GRXCSUM:
864 rc = ethtool_get_rx_csum(dev, useraddr);
866 case ETHTOOL_SRXCSUM:
867 rc = ethtool_set_rx_csum(dev, useraddr);
869 case ETHTOOL_GTXCSUM:
870 rc = ethtool_get_tx_csum(dev, useraddr);
872 case ETHTOOL_STXCSUM:
873 rc = ethtool_set_tx_csum(dev, useraddr);
876 rc = ethtool_get_sg(dev, useraddr);
879 rc = ethtool_set_sg(dev, useraddr);
882 rc = ethtool_get_tso(dev, useraddr);
885 rc = ethtool_set_tso(dev, useraddr);
888 rc = ethtool_self_test(dev, useraddr);
890 case ETHTOOL_GSTRINGS:
891 rc = ethtool_get_strings(dev, useraddr);
893 case ETHTOOL_PHYS_ID:
894 rc = ethtool_phys_id(dev, useraddr);
897 rc = ethtool_get_stats(dev, useraddr);
899 case ETHTOOL_GPERMADDR:
900 rc = ethtool_get_perm_addr(dev, useraddr);
903 rc = ethtool_get_ufo(dev, useraddr);
906 rc = ethtool_set_ufo(dev, useraddr);
912 if(dev->ethtool_ops->complete)
913 dev->ethtool_ops->complete(dev);
915 if (old_features != dev->features)
916 netdev_features_change(dev);
922 return dev->do_ioctl(dev, ifr, SIOCETHTOOL);
926 EXPORT_SYMBOL(dev_ethtool);
927 EXPORT_SYMBOL(ethtool_op_get_link);
928 EXPORT_SYMBOL_GPL(ethtool_op_get_perm_addr);
929 EXPORT_SYMBOL(ethtool_op_get_sg);
930 EXPORT_SYMBOL(ethtool_op_get_tso);
931 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
932 EXPORT_SYMBOL(ethtool_op_set_sg);
933 EXPORT_SYMBOL(ethtool_op_set_tso);
934 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
935 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
936 EXPORT_SYMBOL(ethtool_op_set_ufo);
937 EXPORT_SYMBOL(ethtool_op_get_ufo);