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) != 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 u32 ethtool_op_get_sg(struct net_device *dev)
47 return (dev->features & NETIF_F_SG) != 0;
50 int ethtool_op_set_sg(struct net_device *dev, u32 data)
53 dev->features |= NETIF_F_SG;
55 dev->features &= ~NETIF_F_SG;
61 u32 ethtool_op_get_tso(struct net_device *dev)
63 return (dev->features & NETIF_F_TSO) != 0;
66 int ethtool_op_set_tso(struct net_device *dev, u32 data)
69 dev->features |= NETIF_F_TSO;
71 dev->features &= ~NETIF_F_TSO;
75 #endif /* NETIF_F_TSO */
77 /* Handlers for each ethtool command */
79 static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
81 struct ethtool_cmd cmd = { ETHTOOL_GSET };
84 if (!dev->ethtool_ops->get_settings)
87 err = dev->ethtool_ops->get_settings(dev, &cmd);
91 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
96 static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
98 struct ethtool_cmd cmd;
100 if (!dev->ethtool_ops->set_settings)
103 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
106 return dev->ethtool_ops->set_settings(dev, &cmd);
109 static int ethtool_get_drvinfo(struct net_device *dev, void __user *useraddr)
111 struct ethtool_drvinfo info;
112 struct ethtool_ops *ops = dev->ethtool_ops;
114 if (!ops->get_drvinfo)
117 memset(&info, 0, sizeof(info));
118 info.cmd = ETHTOOL_GDRVINFO;
119 ops->get_drvinfo(dev, &info);
121 if (ops->self_test_count)
122 info.testinfo_len = ops->self_test_count(dev);
123 if (ops->get_stats_count)
124 info.n_stats = ops->get_stats_count(dev);
125 if (ops->get_regs_len)
126 info.regdump_len = ops->get_regs_len(dev);
127 if (ops->get_eeprom_len)
128 info.eedump_len = ops->get_eeprom_len(dev);
130 if (copy_to_user(useraddr, &info, sizeof(info)))
135 static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
137 struct ethtool_regs regs;
138 struct ethtool_ops *ops = dev->ethtool_ops;
142 if (!ops->get_regs || !ops->get_regs_len)
145 if (copy_from_user(®s, useraddr, sizeof(regs)))
148 reglen = ops->get_regs_len(dev);
149 if (regs.len > reglen)
152 regbuf = kmalloc(reglen, GFP_USER);
156 ops->get_regs(dev, ®s, regbuf);
159 if (copy_to_user(useraddr, ®s, sizeof(regs)))
161 useraddr += offsetof(struct ethtool_regs, data);
162 if (copy_to_user(useraddr, regbuf, regs.len))
171 static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
173 struct ethtool_wolinfo wol = { ETHTOOL_GWOL };
175 if (!dev->ethtool_ops->get_wol)
178 dev->ethtool_ops->get_wol(dev, &wol);
180 if (copy_to_user(useraddr, &wol, sizeof(wol)))
185 static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
187 struct ethtool_wolinfo wol;
189 if (!dev->ethtool_ops->set_wol)
192 if (copy_from_user(&wol, useraddr, sizeof(wol)))
195 return dev->ethtool_ops->set_wol(dev, &wol);
198 static int ethtool_get_msglevel(struct net_device *dev, char __user *useraddr)
200 struct ethtool_value edata = { ETHTOOL_GMSGLVL };
202 if (!dev->ethtool_ops->get_msglevel)
205 edata.data = dev->ethtool_ops->get_msglevel(dev);
207 if (copy_to_user(useraddr, &edata, sizeof(edata)))
212 static int ethtool_set_msglevel(struct net_device *dev, char __user *useraddr)
214 struct ethtool_value edata;
216 if (!dev->ethtool_ops->set_msglevel)
219 if (copy_from_user(&edata, useraddr, sizeof(edata)))
222 dev->ethtool_ops->set_msglevel(dev, edata.data);
226 static int ethtool_nway_reset(struct net_device *dev)
228 if (!dev->ethtool_ops->nway_reset)
231 return dev->ethtool_ops->nway_reset(dev);
234 static int ethtool_get_link(struct net_device *dev, void __user *useraddr)
236 struct ethtool_value edata = { ETHTOOL_GLINK };
238 if (!dev->ethtool_ops->get_link)
241 edata.data = dev->ethtool_ops->get_link(dev);
243 if (copy_to_user(useraddr, &edata, sizeof(edata)))
248 static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
250 struct ethtool_eeprom eeprom;
251 struct ethtool_ops *ops = dev->ethtool_ops;
255 if (!ops->get_eeprom || !ops->get_eeprom_len)
258 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
261 /* Check for wrap and zero */
262 if (eeprom.offset + eeprom.len <= eeprom.offset)
265 /* Check for exceeding total eeprom len */
266 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
269 data = kmalloc(eeprom.len, GFP_USER);
274 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
277 ret = ops->get_eeprom(dev, &eeprom, data);
282 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
284 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
293 static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
295 struct ethtool_eeprom eeprom;
296 struct ethtool_ops *ops = dev->ethtool_ops;
300 if (!ops->set_eeprom || !ops->get_eeprom_len)
303 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
306 /* Check for wrap and zero */
307 if (eeprom.offset + eeprom.len <= eeprom.offset)
310 /* Check for exceeding total eeprom len */
311 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
314 data = kmalloc(eeprom.len, GFP_USER);
319 if (copy_from_user(data, useraddr + sizeof(eeprom), eeprom.len))
322 ret = ops->set_eeprom(dev, &eeprom, data);
326 if (copy_to_user(useraddr + sizeof(eeprom), data, eeprom.len))
334 static int ethtool_get_coalesce(struct net_device *dev, void __user *useraddr)
336 struct ethtool_coalesce coalesce = { ETHTOOL_GCOALESCE };
338 if (!dev->ethtool_ops->get_coalesce)
341 dev->ethtool_ops->get_coalesce(dev, &coalesce);
343 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
348 static int ethtool_set_coalesce(struct net_device *dev, void __user *useraddr)
350 struct ethtool_coalesce coalesce;
352 if (!dev->ethtool_ops->set_coalesce)
355 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
358 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
361 static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
363 struct ethtool_ringparam ringparam = { ETHTOOL_GRINGPARAM };
365 if (!dev->ethtool_ops->get_ringparam)
368 dev->ethtool_ops->get_ringparam(dev, &ringparam);
370 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
375 static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
377 struct ethtool_ringparam ringparam;
379 if (!dev->ethtool_ops->set_ringparam)
382 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
385 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
388 static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
390 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
392 if (!dev->ethtool_ops->get_pauseparam)
395 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
397 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
402 static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
404 struct ethtool_pauseparam pauseparam;
406 if (!dev->ethtool_ops->set_pauseparam)
409 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
412 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
415 static int ethtool_get_rx_csum(struct net_device *dev, char __user *useraddr)
417 struct ethtool_value edata = { ETHTOOL_GRXCSUM };
419 if (!dev->ethtool_ops->get_rx_csum)
422 edata.data = dev->ethtool_ops->get_rx_csum(dev);
424 if (copy_to_user(useraddr, &edata, sizeof(edata)))
429 static int ethtool_set_rx_csum(struct net_device *dev, char __user *useraddr)
431 struct ethtool_value edata;
433 if (!dev->ethtool_ops->set_rx_csum)
436 if (copy_from_user(&edata, useraddr, sizeof(edata)))
439 dev->ethtool_ops->set_rx_csum(dev, edata.data);
443 static int ethtool_get_tx_csum(struct net_device *dev, char __user *useraddr)
445 struct ethtool_value edata = { ETHTOOL_GTXCSUM };
447 if (!dev->ethtool_ops->get_tx_csum)
450 edata.data = dev->ethtool_ops->get_tx_csum(dev);
452 if (copy_to_user(useraddr, &edata, sizeof(edata)))
457 static int ethtool_set_tx_csum(struct net_device *dev, char __user *useraddr)
459 struct ethtool_value edata;
461 if (!dev->ethtool_ops->set_tx_csum)
464 if (copy_from_user(&edata, useraddr, sizeof(edata)))
467 return dev->ethtool_ops->set_tx_csum(dev, edata.data);
470 static int ethtool_get_sg(struct net_device *dev, char __user *useraddr)
472 struct ethtool_value edata = { ETHTOOL_GSG };
474 if (!dev->ethtool_ops->get_sg)
477 edata.data = dev->ethtool_ops->get_sg(dev);
479 if (copy_to_user(useraddr, &edata, sizeof(edata)))
484 static int ethtool_set_sg(struct net_device *dev, char __user *useraddr)
486 struct ethtool_value edata;
488 if (!dev->ethtool_ops->set_sg)
491 if (copy_from_user(&edata, useraddr, sizeof(edata)))
494 return dev->ethtool_ops->set_sg(dev, edata.data);
498 static int ethtool_get_tso(struct net_device *dev, char __user *useraddr)
500 struct ethtool_value edata = { ETHTOOL_GTSO };
502 if (!dev->ethtool_ops->get_tso)
505 edata.data = dev->ethtool_ops->get_tso(dev);
507 if (copy_to_user(useraddr, &edata, sizeof(edata)))
512 static int ethtool_set_tso(struct net_device *dev, char __user *useraddr)
514 struct ethtool_value edata;
516 if (!dev->ethtool_ops->set_tso)
519 if (copy_from_user(&edata, useraddr, sizeof(edata)))
522 return dev->ethtool_ops->set_tso(dev, edata.data);
525 EXPORT_SYMBOL(ethtool_op_get_tso);
526 EXPORT_SYMBOL(ethtool_op_set_tso);
527 #endif /* NETIF_F_TSO */
529 static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
531 struct ethtool_test test;
532 struct ethtool_ops *ops = dev->ethtool_ops;
536 if (!ops->self_test || !ops->self_test_count)
539 if (copy_from_user(&test, useraddr, sizeof(test)))
542 test.len = ops->self_test_count(dev);
543 data = kmalloc(test.len * sizeof(u64), GFP_USER);
547 ops->self_test(dev, &test, data);
550 if (copy_to_user(useraddr, &test, sizeof(test)))
552 useraddr += sizeof(test);
553 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
562 static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
564 struct ethtool_gstrings gstrings;
565 struct ethtool_ops *ops = dev->ethtool_ops;
569 if (!ops->get_strings)
572 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
575 switch (gstrings.string_set) {
577 if (!ops->self_test_count)
579 gstrings.len = ops->self_test_count(dev);
582 if (!ops->get_stats_count)
584 gstrings.len = ops->get_stats_count(dev);
590 data = kmalloc(gstrings.len * ETH_GSTRING_LEN, GFP_USER);
594 ops->get_strings(dev, gstrings.string_set, data);
597 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
599 useraddr += sizeof(gstrings);
600 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
609 static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
611 struct ethtool_value id;
613 if (!dev->ethtool_ops->phys_id)
616 if (copy_from_user(&id, useraddr, sizeof(id)))
619 return dev->ethtool_ops->phys_id(dev, id.data);
622 static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
624 struct ethtool_stats stats;
625 struct ethtool_ops *ops = dev->ethtool_ops;
629 if (!ops->get_ethtool_stats || !ops->get_stats_count)
632 if (copy_from_user(&stats, useraddr, sizeof(stats)))
635 stats.n_stats = ops->get_stats_count(dev);
636 data = kmalloc(stats.n_stats * sizeof(u64), GFP_USER);
640 ops->get_ethtool_stats(dev, &stats, data);
643 if (copy_to_user(useraddr, &stats, sizeof(stats)))
645 useraddr += sizeof(stats);
646 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
655 /* The main entry point in this file. Called from net/core/dev.c */
657 int dev_ethtool(struct ifreq *ifr)
659 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
660 void __user *useraddr = ifr->ifr_data;
665 * XXX: This can be pushed down into the ethtool_* handlers that
666 * need it. Keep existing behaviour for the moment.
668 if (!capable(CAP_NET_ADMIN))
671 if (!dev || !netif_device_present(dev))
674 if (!dev->ethtool_ops)
677 if (copy_from_user(ðcmd, useraddr, sizeof (ethcmd)))
680 if(dev->ethtool_ops->begin)
681 if ((rc = dev->ethtool_ops->begin(dev)) < 0)
686 rc = ethtool_get_settings(dev, useraddr);
689 rc = ethtool_set_settings(dev, useraddr);
691 case ETHTOOL_GDRVINFO:
692 rc = ethtool_get_drvinfo(dev, useraddr);
696 rc = ethtool_get_regs(dev, useraddr);
699 rc = ethtool_get_wol(dev, useraddr);
702 rc = ethtool_set_wol(dev, useraddr);
704 case ETHTOOL_GMSGLVL:
705 rc = ethtool_get_msglevel(dev, useraddr);
707 case ETHTOOL_SMSGLVL:
708 rc = ethtool_set_msglevel(dev, useraddr);
710 case ETHTOOL_NWAY_RST:
711 rc = ethtool_nway_reset(dev);
714 rc = ethtool_get_link(dev, useraddr);
716 case ETHTOOL_GEEPROM:
717 rc = ethtool_get_eeprom(dev, useraddr);
719 case ETHTOOL_SEEPROM:
720 rc = ethtool_set_eeprom(dev, useraddr);
722 case ETHTOOL_GCOALESCE:
723 rc = ethtool_get_coalesce(dev, useraddr);
725 case ETHTOOL_SCOALESCE:
726 rc = ethtool_set_coalesce(dev, useraddr);
728 case ETHTOOL_GRINGPARAM:
729 rc = ethtool_get_ringparam(dev, useraddr);
731 case ETHTOOL_SRINGPARAM:
732 rc = ethtool_set_ringparam(dev, useraddr);
734 case ETHTOOL_GPAUSEPARAM:
735 rc = ethtool_get_pauseparam(dev, useraddr);
737 case ETHTOOL_SPAUSEPARAM:
738 rc = ethtool_set_pauseparam(dev, useraddr);
740 case ETHTOOL_GRXCSUM:
741 rc = ethtool_get_rx_csum(dev, useraddr);
743 case ETHTOOL_SRXCSUM:
744 rc = ethtool_set_rx_csum(dev, useraddr);
746 case ETHTOOL_GTXCSUM:
747 rc = ethtool_get_tx_csum(dev, useraddr);
749 case ETHTOOL_STXCSUM:
750 rc = ethtool_set_tx_csum(dev, useraddr);
753 rc = ethtool_get_sg(dev, useraddr);
756 rc = ethtool_set_sg(dev, useraddr);
760 rc = ethtool_get_tso(dev, useraddr);
763 rc = ethtool_set_tso(dev, useraddr);
765 #endif /* NETIF_F_TSO */
767 rc = ethtool_self_test(dev, useraddr);
769 case ETHTOOL_GSTRINGS:
770 rc = ethtool_get_strings(dev, useraddr);
772 case ETHTOOL_PHYS_ID:
773 rc = ethtool_phys_id(dev, useraddr);
776 rc = ethtool_get_stats(dev, useraddr);
782 if(dev->ethtool_ops->complete)
783 dev->ethtool_ops->complete(dev);
788 return dev->do_ioctl(dev, ifr, SIOCETHTOOL);
792 EXPORT_SYMBOL(dev_ethtool);
793 EXPORT_SYMBOL(ethtool_op_get_link);
794 EXPORT_SYMBOL(ethtool_op_get_sg);
795 EXPORT_SYMBOL(ethtool_op_get_tx_csum);
796 EXPORT_SYMBOL(ethtool_op_set_sg);
797 EXPORT_SYMBOL(ethtool_op_set_tx_csum);