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/capability.h>
15 #include <linux/errno.h>
16 #include <linux/ethtool.h>
17 #include <linux/netdevice.h>
18 #include <asm/uaccess.h>
21 * Some useful ethtool_ops methods that're device independent.
22 * If we find that all drivers want to do the same thing here,
23 * we can turn these into dev_() function calls.
26 u32 ethtool_op_get_link(struct net_device *dev)
28 return netif_carrier_ok(dev) ? 1 : 0;
31 u32 ethtool_op_get_tx_csum(struct net_device *dev)
33 return (dev->features & NETIF_F_ALL_CSUM) != 0;
36 int ethtool_op_set_tx_csum(struct net_device *dev, u32 data)
39 dev->features |= NETIF_F_IP_CSUM;
41 dev->features &= ~NETIF_F_IP_CSUM;
46 int ethtool_op_set_tx_hw_csum(struct net_device *dev, u32 data)
49 dev->features |= NETIF_F_HW_CSUM;
51 dev->features &= ~NETIF_F_HW_CSUM;
55 u32 ethtool_op_get_sg(struct net_device *dev)
57 return (dev->features & NETIF_F_SG) != 0;
60 int ethtool_op_set_sg(struct net_device *dev, u32 data)
63 dev->features |= NETIF_F_SG;
65 dev->features &= ~NETIF_F_SG;
70 u32 ethtool_op_get_tso(struct net_device *dev)
72 return (dev->features & NETIF_F_TSO) != 0;
75 int ethtool_op_set_tso(struct net_device *dev, u32 data)
78 dev->features |= NETIF_F_TSO;
80 dev->features &= ~NETIF_F_TSO;
85 int ethtool_op_get_perm_addr(struct net_device *dev, struct ethtool_perm_addr *addr, u8 *data)
87 unsigned char len = dev->addr_len;
88 if ( addr->size < len )
92 memcpy(data, dev->perm_addr, len);
97 u32 ethtool_op_get_ufo(struct net_device *dev)
99 return (dev->features & NETIF_F_UFO) != 0;
102 int ethtool_op_set_ufo(struct net_device *dev, u32 data)
105 dev->features |= NETIF_F_UFO;
107 dev->features &= ~NETIF_F_UFO;
111 /* Handlers for each ethtool command */
113 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
115 struct ethtool_cmd cmd = { ETHTOOL_GSET };
118 if (!dev->ethtool_ops->get_settings)
121 err = dev->ethtool_ops->get_settings(dev, &cmd);
125 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
130 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
132 struct ethtool_cmd cmd;
134 if (!dev->ethtool_ops->set_settings)
137 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
140 return dev->ethtool_ops->set_settings(dev, &cmd);
143 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
145 struct ethtool_drvinfo info;
146 struct ethtool_ops *ops = dev->ethtool_ops;
148 if (!ops->get_drvinfo)
151 memset(&info, 0, sizeof(info));
152 info.cmd = ETHTOOL_GDRVINFO;
153 ops->get_drvinfo(dev, &info);
155 if (ops->self_test_count)
156 info.testinfo_len = ops->self_test_count(dev);
157 if (ops->get_stats_count)
158 info.n_stats = ops->get_stats_count(dev);
159 if (ops->get_regs_len)
160 info.regdump_len = ops->get_regs_len(dev);
161 if (ops->get_eeprom_len)
162 info.eedump_len = ops->get_eeprom_len(dev);
164 if (copy_to_user(useraddr, &info, sizeof(info)))
169 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
171 struct ethtool_regs regs;
172 struct ethtool_ops *ops = dev->ethtool_ops;
176 if (!ops->get_regs || !ops->get_regs_len)
179 if (copy_from_user(®s, useraddr, sizeof(regs)))
182 reglen = ops->get_regs_len(dev);
183 if (regs.len > reglen)
186 regbuf = kmalloc(reglen, GFP_USER);
190 ops->get_regs(dev, ®s, regbuf);
193 if (copy_to_user(useraddr, ®s, sizeof(regs)))
195 useraddr += offsetof(struct ethtool_regs, data);
196 if (copy_to_user(useraddr, regbuf, regs.len))
205 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
207 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
209 if (!dev->ethtool_ops->get_wol)
212 dev->ethtool_ops->get_wol(dev, &wol);
214 if (copy_to_user(useraddr, &wol, sizeof(wol)))
219 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
221 struct ethtool_wolinfo wol;
223 if (!dev->ethtool_ops->set_wol)
226 if (copy_from_user(&wol, useraddr, sizeof(wol)))
229 return dev->ethtool_ops->set_wol(dev, &wol);
232 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
234 struct ethtool_value edata = { ETHTOOL_GMSGLVL };
236 if (!dev->ethtool_ops->get_msglevel)
239 edata.data = dev->ethtool_ops->get_msglevel(dev);
241 if (copy_to_user(useraddr, &edata, sizeof(edata)))
246 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
248 struct ethtool_value edata;
250 if (!dev->ethtool_ops->set_msglevel)
253 if (copy_from_user(&edata, useraddr, sizeof(edata)))
256 dev->ethtool_ops->set_msglevel(dev, edata.data);
260 static int ethtool_nway_reset(struct net_device *dev)
262 if (!dev->ethtool_ops->nway_reset)
265 return dev->ethtool_ops->nway_reset(dev);
268 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
270 struct ethtool_value edata = { ETHTOOL_GLINK };
272 if (!dev->ethtool_ops->get_link)
275 edata.data = dev->ethtool_ops->get_link(dev);
277 if (copy_to_user(useraddr, &edata, sizeof(edata)))
282 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
284 struct ethtool_eeprom eeprom;
285 struct ethtool_ops *ops = dev->ethtool_ops;
289 if (!ops->get_eeprom || !ops->get_eeprom_len)
292 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
295 /* Check for wrap and zero */
296 if (eeprom.offset + eeprom.len <= eeprom.offset)
299 /* Check for exceeding total eeprom len */
300 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
303 data = kmalloc(eeprom.len, GFP_USER);
308 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
311 ret = ops->get_eeprom(dev, &eeprom, data);
316 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
318 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
327 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
329 struct ethtool_eeprom eeprom;
330 struct ethtool_ops *ops = dev->ethtool_ops;
334 if (!ops->set_eeprom || !ops->get_eeprom_len)
337 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
340 /* Check for wrap and zero */
341 if (eeprom.offset + eeprom.len <= eeprom.offset)
344 /* Check for exceeding total eeprom len */
345 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
348 data = kmalloc(eeprom.len, GFP_USER);
353 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
356 ret = ops->set_eeprom(dev, &eeprom, data);
360 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
368 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
370 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
372 if (!dev->ethtool_ops->get_coalesce)
375 dev->ethtool_ops->get_coalesce(dev, &coalesce);
377 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
382 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
384 struct ethtool_coalesce coalesce;
386 if (!dev->ethtool_ops->set_coalesce)
389 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
392 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
395 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
397 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
399 if (!dev->ethtool_ops->get_ringparam)
402 dev->ethtool_ops->get_ringparam(dev, &ringparam);
404 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
409 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
411 struct ethtool_ringparam ringparam;
413 if (!dev->ethtool_ops->set_ringparam)
416 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
419 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
422 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
424 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
426 if (!dev->ethtool_ops->get_pauseparam)
429 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
431 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
436 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
438 struct ethtool_pauseparam pauseparam;
440 if (!dev->ethtool_ops->get_pauseparam)
443 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
446 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
449 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
451 struct ethtool_value edata = { ETHTOOL_GRXCSUM };
453 if (!dev->ethtool_ops->get_rx_csum)
456 edata.data = dev->ethtool_ops->get_rx_csum(dev);
458 if (copy_to_user(useraddr, &edata, sizeof(edata)))
463 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
465 struct ethtool_value edata;
467 if (!dev->ethtool_ops->set_rx_csum)
470 if (copy_from_user(&edata, useraddr, sizeof(edata)))
473 dev->ethtool_ops->set_rx_csum(dev, edata.data);
477 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
479 struct ethtool_value edata = { ETHTOOL_GTXCSUM };
481 if (!dev->ethtool_ops->get_tx_csum)
484 edata.data = dev->ethtool_ops->get_tx_csum(dev);
486 if (copy_to_user(useraddr, &edata, sizeof(edata)))
491 static int __ethtool_set_sg(struct net_device *dev, u32 data)
495 if (!data && dev->ethtool_ops->set_tso) {
496 err = dev->ethtool_ops->set_tso(dev, 0);
501 if (!data && dev->ethtool_ops->set_ufo) {
502 err = dev->ethtool_ops->set_ufo(dev, 0);
506 return dev->ethtool_ops->set_sg(dev, data);
509 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
511 struct ethtool_value edata;
514 if (!dev->ethtool_ops->set_tx_csum)
517 if (copy_from_user(&edata, useraddr, sizeof(edata)))
520 if (!edata.data && dev->ethtool_ops->set_sg) {
521 err = __ethtool_set_sg(dev, 0);
526 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
529 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
531 struct ethtool_value edata = { ETHTOOL_GSG };
533 if (!dev->ethtool_ops->get_sg)
536 edata.data = dev->ethtool_ops->get_sg(dev);
538 if (copy_to_user(useraddr, &edata, sizeof(edata)))
543 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
545 struct ethtool_value edata;
547 if (!dev->ethtool_ops->set_sg)
550 if (copy_from_user(&edata, useraddr, sizeof(edata)))
554 !(dev->features & NETIF_F_ALL_CSUM))
557 return __ethtool_set_sg(dev, edata.data);
560 static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
562 struct ethtool_value edata = { ETHTOOL_GTSO };
564 if (!dev->ethtool_ops->get_tso)
567 edata.data = dev->ethtool_ops->get_tso(dev);
569 if (copy_to_user(useraddr, &edata, sizeof(edata)))
574 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
576 struct ethtool_value edata;
578 if (!dev->ethtool_ops->set_tso)
581 if (copy_from_user(&edata, useraddr, sizeof(edata)))
584 if (edata.data && !(dev->features & NETIF_F_SG))
587 return dev->ethtool_ops->set_tso(dev, edata.data);
590 static int ethtool_get_ufo(struct net_device *dev, char __user *useraddr)
592 struct ethtool_value edata = { ETHTOOL_GUFO };
594 if (!dev->ethtool_ops->get_ufo)
596 edata.data = dev->ethtool_ops->get_ufo(dev);
597 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_get_gso(struct net_device *dev, char __user *useraddr)
619 struct ethtool_value edata = { ETHTOOL_GGSO };
621 edata.data = dev->features & NETIF_F_GSO;
622 if (copy_to_user(useraddr, &edata, sizeof(edata)))
627 static int ethtool_set_gso(struct net_device *dev, char __user *useraddr)
629 struct ethtool_value edata;
631 if (copy_from_user(&edata, useraddr, sizeof(edata)))
634 dev->features |= NETIF_F_GSO;
636 dev->features &= ~NETIF_F_GSO;
640 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
642 struct ethtool_test test;
643 struct ethtool_ops *ops = dev->ethtool_ops;
647 if (!ops->self_test || !ops->self_test_count)
650 if (copy_from_user(&test, useraddr, sizeof(test)))
653 test.len = ops->self_test_count(dev);
654 data = kmalloc(test.len * sizeof(u64), GFP_USER);
658 ops->self_test(dev, &test, data);
661 if (copy_to_user(useraddr, &test, sizeof(test)))
663 useraddr += sizeof(test);
664 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
673 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
675 struct ethtool_gstrings gstrings;
676 struct ethtool_ops *ops = dev->ethtool_ops;
680 if (!ops->get_strings)
683 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
686 switch (gstrings.string_set) {
688 if (!ops->self_test_count)
690 gstrings.len = ops->self_test_count(dev);
693 if (!ops->get_stats_count)
695 gstrings.len = ops->get_stats_count(dev);
701 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
705 ops->get_strings(dev, gstrings.string_set, data);
708 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
710 useraddr += sizeof(gstrings);
711 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
720 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
722 struct ethtool_value id;
724 if (!dev->ethtool_ops->phys_id)
727 if (copy_from_user(&id, useraddr, sizeof(id)))
730 return dev->ethtool_ops->phys_id(dev, id.data);
733 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
735 struct ethtool_stats stats;
736 struct ethtool_ops *ops = dev->ethtool_ops;
740 if (!ops->get_ethtool_stats || !ops->get_stats_count)
743 if (copy_from_user(&stats, useraddr, sizeof(stats)))
746 stats.n_stats = ops->get_stats_count(dev);
747 data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
751 ops->get_ethtool_stats(dev, &stats, data);
754 if (copy_to_user(useraddr, &stats, sizeof(stats)))
756 useraddr += sizeof(stats);
757 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
766 static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
768 struct ethtool_perm_addr epaddr;
772 if (!dev->ethtool_ops->get_perm_addr)
775 if (copy_from_user(&epaddr,useraddr,sizeof(epaddr)))
778 data = kmalloc(epaddr.size, GFP_USER);
782 ret = dev->ethtool_ops->get_perm_addr(dev,&epaddr,data);
787 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
789 useraddr += sizeof(epaddr);
790 if (copy_to_user(useraddr, data, epaddr.size))
799 /* The main entry point in this file. Called from net/core/dev.c */
801 int dev_ethtool(struct ifreq *ifr)
803 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
804 void __user *useraddr = ifr->ifr_data;
807 unsigned long old_features;
810 * XXX: This can be pushed down into the ethtool_* handlers that
811 * need it. Keep existing behaviour for the moment.
813 if (!capable(CAP_NET_ADMIN))
816 if (!dev || !netif_device_present(dev))
819 if (!dev->ethtool_ops)
822 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
825 if(dev->ethtool_ops->begin)
826 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
829 old_features = dev->features;
833 rc = ethtool_get_settings(dev, useraddr);
836 rc = ethtool_set_settings(dev, useraddr);
838 case ETHTOOL_GDRVINFO:
839 rc = ethtool_get_drvinfo(dev, useraddr);
842 rc = ethtool_get_regs(dev, useraddr);
845 rc = ethtool_get_wol(dev, useraddr);
848 rc = ethtool_set_wol(dev, useraddr);
850 case ETHTOOL_GMSGLVL:
851 rc = ethtool_get_msglevel(dev, useraddr);
853 case ETHTOOL_SMSGLVL:
854 rc = ethtool_set_msglevel(dev, useraddr);
856 case ETHTOOL_NWAY_RST:
857 rc = ethtool_nway_reset(dev);
860 rc = ethtool_get_link(dev, useraddr);
862 case ETHTOOL_GEEPROM:
863 rc = ethtool_get_eeprom(dev, useraddr);
865 case ETHTOOL_SEEPROM:
866 rc = ethtool_set_eeprom(dev, useraddr);
868 case ETHTOOL_GCOALESCE:
869 rc = ethtool_get_coalesce(dev, useraddr);
871 case ETHTOOL_SCOALESCE:
872 rc = ethtool_set_coalesce(dev, useraddr);
874 case ETHTOOL_GRINGPARAM:
875 rc = ethtool_get_ringparam(dev, useraddr);
877 case ETHTOOL_SRINGPARAM:
878 rc = ethtool_set_ringparam(dev, useraddr);
880 case ETHTOOL_GPAUSEPARAM:
881 rc = ethtool_get_pauseparam(dev, useraddr);
883 case ETHTOOL_SPAUSEPARAM:
884 rc = ethtool_set_pauseparam(dev, useraddr);
886 case ETHTOOL_GRXCSUM:
887 rc = ethtool_get_rx_csum(dev, useraddr);
889 case ETHTOOL_SRXCSUM:
890 rc = ethtool_set_rx_csum(dev, useraddr);
892 case ETHTOOL_GTXCSUM:
893 rc = ethtool_get_tx_csum(dev, useraddr);
895 case ETHTOOL_STXCSUM:
896 rc = ethtool_set_tx_csum(dev, useraddr);
899 rc = ethtool_get_sg(dev, useraddr);
902 rc = ethtool_set_sg(dev, useraddr);
905 rc = ethtool_get_tso(dev, useraddr);
908 rc = ethtool_set_tso(dev, useraddr);
911 rc = ethtool_self_test(dev, useraddr);
913 case ETHTOOL_GSTRINGS:
914 rc = ethtool_get_strings(dev, useraddr);
916 case ETHTOOL_PHYS_ID:
917 rc = ethtool_phys_id(dev, useraddr);
920 rc = ethtool_get_stats(dev, useraddr);
922 case ETHTOOL_GPERMADDR:
923 rc = ethtool_get_perm_addr(dev, useraddr);
926 rc = ethtool_get_ufo(dev, useraddr);
929 rc = ethtool_set_ufo(dev, useraddr);
932 rc = ethtool_get_gso(dev, useraddr);
935 rc = ethtool_set_gso(dev, useraddr);
941 if(dev->ethtool_ops->complete)
942 dev->ethtool_ops->complete(dev);
944 if (old_features != dev->features)
945 netdev_features_change(dev);
951 return dev->do_ioctl(dev, ifr, SIOCETHTOOL);
955 EXPORT_SYMBOL(dev_ethtool);
956 EXPORT_SYMBOL(ethtool_op_get_link);
957 EXPORT_SYMBOL_GPL(ethtool_op_get_perm_addr);
958 EXPORT_SYMBOL(ethtool_op_get_sg);
959 EXPORT_SYMBOL(ethtool_op_get_tso);
960 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
961 EXPORT_SYMBOL(ethtool_op_set_sg);
962 EXPORT_SYMBOL(ethtool_op_set_tso);
963 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
964 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);
965 EXPORT_SYMBOL(ethtool_op_set_ufo);
966 EXPORT_SYMBOL(ethtool_op_get_ufo);