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 /* Handlers for each ethtool command */
86 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
88 struct ethtool_cmd cmd = { ETHTOOL_GSET };
91 if (!dev->ethtool_ops->get_settings)
94 err = dev->ethtool_ops->get_settings(dev, &cmd);
98 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
103 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
105 struct ethtool_cmd cmd;
107 if (!dev->ethtool_ops->set_settings)
110 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
113 return dev->ethtool_ops->set_settings(dev, &cmd);
116 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
118 struct ethtool_drvinfo info;
119 struct ethtool_ops *ops = dev->ethtool_ops;
121 if (!ops->get_drvinfo)
124 memset(&info, 0, sizeof(info));
125 info.cmd = ETHTOOL_GDRVINFO;
126 ops->get_drvinfo(dev, &info);
128 if (ops->self_test_count)
129 info.testinfo_len = ops->self_test_count(dev);
130 if (ops->get_stats_count)
131 info.n_stats = ops->get_stats_count(dev);
132 if (ops->get_regs_len)
133 info.regdump_len = ops->get_regs_len(dev);
134 if (ops->get_eeprom_len)
135 info.eedump_len = ops->get_eeprom_len(dev);
137 if (copy_to_user(useraddr, &info, sizeof(info)))
142 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
144 struct ethtool_regs regs;
145 struct ethtool_ops *ops = dev->ethtool_ops;
149 if (!ops->get_regs || !ops->get_regs_len)
152 if (copy_from_user(®s, useraddr, sizeof(regs)))
155 reglen = ops->get_regs_len(dev);
156 if (regs.len > reglen)
159 regbuf = kmalloc(reglen, GFP_USER);
163 ops->get_regs(dev, ®s, regbuf);
166 if (copy_to_user(useraddr, ®s, sizeof(regs)))
168 useraddr += offsetof(struct ethtool_regs, data);
169 if (copy_to_user(useraddr, regbuf, regs.len))
178 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
180 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
182 if (!dev->ethtool_ops->get_wol)
185 dev->ethtool_ops->get_wol(dev, &wol);
187 if (copy_to_user(useraddr, &wol, sizeof(wol)))
192 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
194 struct ethtool_wolinfo wol;
196 if (!dev->ethtool_ops->set_wol)
199 if (copy_from_user(&wol, useraddr, sizeof(wol)))
202 return dev->ethtool_ops->set_wol(dev, &wol);
205 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
207 struct ethtool_value edata = { ETHTOOL_GMSGLVL };
209 if (!dev->ethtool_ops->get_msglevel)
212 edata.data = dev->ethtool_ops->get_msglevel(dev);
214 if (copy_to_user(useraddr, &edata, sizeof(edata)))
219 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
221 struct ethtool_value edata;
223 if (!dev->ethtool_ops->set_msglevel)
226 if (copy_from_user(&edata, useraddr, sizeof(edata)))
229 dev->ethtool_ops->set_msglevel(dev, edata.data);
233 static int ethtool_nway_reset(struct net_device *dev)
235 if (!dev->ethtool_ops->nway_reset)
238 return dev->ethtool_ops->nway_reset(dev);
241 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
243 struct ethtool_value edata = { ETHTOOL_GLINK };
245 if (!dev->ethtool_ops->get_link)
248 edata.data = dev->ethtool_ops->get_link(dev);
250 if (copy_to_user(useraddr, &edata, sizeof(edata)))
255 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
257 struct ethtool_eeprom eeprom;
258 struct ethtool_ops *ops = dev->ethtool_ops;
262 if (!ops->get_eeprom || !ops->get_eeprom_len)
265 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
268 /* Check for wrap and zero */
269 if (eeprom.offset + eeprom.len <= eeprom.offset)
272 /* Check for exceeding total eeprom len */
273 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
276 data = kmalloc(eeprom.len, GFP_USER);
281 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
284 ret = ops->get_eeprom(dev, &eeprom, data);
289 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
291 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
300 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
302 struct ethtool_eeprom eeprom;
303 struct ethtool_ops *ops = dev->ethtool_ops;
307 if (!ops->set_eeprom || !ops->get_eeprom_len)
310 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
313 /* Check for wrap and zero */
314 if (eeprom.offset + eeprom.len <= eeprom.offset)
317 /* Check for exceeding total eeprom len */
318 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
321 data = kmalloc(eeprom.len, GFP_USER);
326 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
329 ret = ops->set_eeprom(dev, &eeprom, data);
333 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
341 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
343 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
345 if (!dev->ethtool_ops->get_coalesce)
348 dev->ethtool_ops->get_coalesce(dev, &coalesce);
350 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
355 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
357 struct ethtool_coalesce coalesce;
359 if (!dev->ethtool_ops->set_coalesce)
362 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
365 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
368 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
370 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
372 if (!dev->ethtool_ops->get_ringparam)
375 dev->ethtool_ops->get_ringparam(dev, &ringparam);
377 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
382 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
384 struct ethtool_ringparam ringparam;
386 if (!dev->ethtool_ops->set_ringparam)
389 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
392 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
395 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
397 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
399 if (!dev->ethtool_ops->get_pauseparam)
402 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
404 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
409 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
411 struct ethtool_pauseparam pauseparam;
413 if (!dev->ethtool_ops->get_pauseparam)
416 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
419 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
422 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
424 struct ethtool_value edata = { ETHTOOL_GRXCSUM };
426 if (!dev->ethtool_ops->get_rx_csum)
429 edata.data = dev->ethtool_ops->get_rx_csum(dev);
431 if (copy_to_user(useraddr, &edata, sizeof(edata)))
436 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
438 struct ethtool_value edata;
440 if (!dev->ethtool_ops->set_rx_csum)
443 if (copy_from_user(&edata, useraddr, sizeof(edata)))
446 dev->ethtool_ops->set_rx_csum(dev, edata.data);
450 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
452 struct ethtool_value edata = { ETHTOOL_GTXCSUM };
454 if (!dev->ethtool_ops->get_tx_csum)
457 edata.data = dev->ethtool_ops->get_tx_csum(dev);
459 if (copy_to_user(useraddr, &edata, sizeof(edata)))
464 static int __ethtool_set_sg(struct net_device *dev, u32 data)
468 if (!data && dev->ethtool_ops->set_tso) {
469 err = dev->ethtool_ops->set_tso(dev, 0);
474 return dev->ethtool_ops->set_sg(dev, data);
477 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
479 struct ethtool_value edata;
482 if (!dev->ethtool_ops->set_tx_csum)
485 if (copy_from_user(&edata, useraddr, sizeof(edata)))
488 if (!edata.data && dev->ethtool_ops->set_sg) {
489 err = __ethtool_set_sg(dev, 0);
494 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
497 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
499 struct ethtool_value edata = { ETHTOOL_GSG };
501 if (!dev->ethtool_ops->get_sg)
504 edata.data = dev->ethtool_ops->get_sg(dev);
506 if (copy_to_user(useraddr, &edata, sizeof(edata)))
511 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
513 struct ethtool_value edata;
515 if (!dev->ethtool_ops->set_sg)
518 if (copy_from_user(&edata, useraddr, sizeof(edata)))
522 !(dev->features & (NETIF_F_IP_CSUM |
527 return __ethtool_set_sg(dev, edata.data);
530 static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
532 struct ethtool_value edata = { ETHTOOL_GTSO };
534 if (!dev->ethtool_ops->get_tso)
537 edata.data = dev->ethtool_ops->get_tso(dev);
539 if (copy_to_user(useraddr, &edata, sizeof(edata)))
544 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
546 struct ethtool_value edata;
548 if (!dev->ethtool_ops->set_tso)
551 if (copy_from_user(&edata, useraddr, sizeof(edata)))
554 if (edata.data && !(dev->features & NETIF_F_SG))
557 return dev->ethtool_ops->set_tso(dev, edata.data);
560 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
562 struct ethtool_test test;
563 struct ethtool_ops *ops = dev->ethtool_ops;
567 if (!ops->self_test || !ops->self_test_count)
570 if (copy_from_user(&test, useraddr, sizeof(test)))
573 test.len = ops->self_test_count(dev);
574 data = kmalloc(test.len * sizeof(u64), GFP_USER);
578 ops->self_test(dev, &test, data);
581 if (copy_to_user(useraddr, &test, sizeof(test)))
583 useraddr += sizeof(test);
584 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
593 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
595 struct ethtool_gstrings gstrings;
596 struct ethtool_ops *ops = dev->ethtool_ops;
600 if (!ops->get_strings)
603 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
606 switch (gstrings.string_set) {
608 if (!ops->self_test_count)
610 gstrings.len = ops->self_test_count(dev);
613 if (!ops->get_stats_count)
615 gstrings.len = ops->get_stats_count(dev);
621 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
625 ops->get_strings(dev, gstrings.string_set, data);
628 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
630 useraddr += sizeof(gstrings);
631 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
640 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
642 struct ethtool_value id;
644 if (!dev->ethtool_ops->phys_id)
647 if (copy_from_user(&id, useraddr, sizeof(id)))
650 return dev->ethtool_ops->phys_id(dev, id.data);
653 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
655 struct ethtool_stats stats;
656 struct ethtool_ops *ops = dev->ethtool_ops;
660 if (!ops->get_ethtool_stats || !ops->get_stats_count)
663 if (copy_from_user(&stats, useraddr, sizeof(stats)))
666 stats.n_stats = ops->get_stats_count(dev);
667 data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
671 ops->get_ethtool_stats(dev, &stats, data);
674 if (copy_to_user(useraddr, &stats, sizeof(stats)))
676 useraddr += sizeof(stats);
677 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
686 /* The main entry point in this file. Called from net/core/dev.c */
688 int dev_ethtool(struct ifreq *ifr)
690 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
691 void __user *useraddr = ifr->ifr_data;
694 unsigned long old_features;
697 * XXX: This can be pushed down into the ethtool_* handlers that
698 * need it. Keep existing behaviour for the moment.
700 if (!capable(CAP_NET_ADMIN))
703 if (!dev || !netif_device_present(dev))
706 if (!dev->ethtool_ops)
709 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
712 if(dev->ethtool_ops->begin)
713 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
716 old_features = dev->features;
720 rc = ethtool_get_settings(dev, useraddr);
723 rc = ethtool_set_settings(dev, useraddr);
725 case ETHTOOL_GDRVINFO:
726 rc = ethtool_get_drvinfo(dev, useraddr);
729 rc = ethtool_get_regs(dev, useraddr);
732 rc = ethtool_get_wol(dev, useraddr);
735 rc = ethtool_set_wol(dev, useraddr);
737 case ETHTOOL_GMSGLVL:
738 rc = ethtool_get_msglevel(dev, useraddr);
740 case ETHTOOL_SMSGLVL:
741 rc = ethtool_set_msglevel(dev, useraddr);
743 case ETHTOOL_NWAY_RST:
744 rc = ethtool_nway_reset(dev);
747 rc = ethtool_get_link(dev, useraddr);
749 case ETHTOOL_GEEPROM:
750 rc = ethtool_get_eeprom(dev, useraddr);
752 case ETHTOOL_SEEPROM:
753 rc = ethtool_set_eeprom(dev, useraddr);
755 case ETHTOOL_GCOALESCE:
756 rc = ethtool_get_coalesce(dev, useraddr);
758 case ETHTOOL_SCOALESCE:
759 rc = ethtool_set_coalesce(dev, useraddr);
761 case ETHTOOL_GRINGPARAM:
762 rc = ethtool_get_ringparam(dev, useraddr);
764 case ETHTOOL_SRINGPARAM:
765 rc = ethtool_set_ringparam(dev, useraddr);
767 case ETHTOOL_GPAUSEPARAM:
768 rc = ethtool_get_pauseparam(dev, useraddr);
770 case ETHTOOL_SPAUSEPARAM:
771 rc = ethtool_set_pauseparam(dev, useraddr);
773 case ETHTOOL_GRXCSUM:
774 rc = ethtool_get_rx_csum(dev, useraddr);
776 case ETHTOOL_SRXCSUM:
777 rc = ethtool_set_rx_csum(dev, useraddr);
779 case ETHTOOL_GTXCSUM:
780 rc = ethtool_get_tx_csum(dev, useraddr);
782 case ETHTOOL_STXCSUM:
783 rc = ethtool_set_tx_csum(dev, useraddr);
786 rc = ethtool_get_sg(dev, useraddr);
789 rc = ethtool_set_sg(dev, useraddr);
792 rc = ethtool_get_tso(dev, useraddr);
795 rc = ethtool_set_tso(dev, useraddr);
798 rc = ethtool_self_test(dev, useraddr);
800 case ETHTOOL_GSTRINGS:
801 rc = ethtool_get_strings(dev, useraddr);
803 case ETHTOOL_PHYS_ID:
804 rc = ethtool_phys_id(dev, useraddr);
807 rc = ethtool_get_stats(dev, useraddr);
813 if(dev->ethtool_ops->complete)
814 dev->ethtool_ops->complete(dev);
816 if (old_features != dev->features)
817 netdev_features_change(dev);
823 return dev->do_ioctl(dev, ifr, SIOCETHTOOL);
827 EXPORT_SYMBOL(dev_ethtool);
828 EXPORT_SYMBOL(ethtool_op_get_link);
829 EXPORT_SYMBOL(ethtool_op_get_sg);
830 EXPORT_SYMBOL(ethtool_op_get_tso);
831 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
832 EXPORT_SYMBOL(ethtool_op_set_sg);
833 EXPORT_SYMBOL(ethtool_op_set_tso);
834 EXPORT_SYMBOL(ethtool_op_set_tx_csum);
835 EXPORT_SYMBOL(ethtool_op_set_tx_hw_csum);