Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/core/ethtool.c - Ethtool ioctl handler |
| 3 | * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx> |
| 4 | * |
| 5 | * This file is where we call all the ethtool_ops commands to get |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 6 | * the information ethtool needs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 16 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/errno.h> |
| 18 | #include <linux/ethtool.h> |
| 19 | #include <linux/netdevice.h> |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 20 | #include <linux/net_tstamp.h> |
| 21 | #include <linux/phy.h> |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 22 | #include <linux/bitops.h> |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
David S. Miller | 73da16c | 2010-09-21 16:12:11 -0700 | [diff] [blame] | 24 | #include <linux/vmalloc.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 26 | #include <linux/rtnetlink.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 27 | #include <linux/sched/signal.h> |
Eric Dumazet | 960fb62 | 2014-11-16 06:23:05 -0800 | [diff] [blame] | 28 | #include <linux/net.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 30 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * Some useful ethtool_ops methods that're device independent. |
| 32 | * If we find that all drivers want to do the same thing here, |
| 33 | * we can turn these into dev_() function calls. |
| 34 | */ |
| 35 | |
| 36 | u32 ethtool_op_get_link(struct net_device *dev) |
| 37 | { |
| 38 | return netif_carrier_ok(dev) ? 1 : 0; |
| 39 | } |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 40 | EXPORT_SYMBOL(ethtool_op_get_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Richard Cochran | 02eacbd | 2012-04-03 22:59:22 +0000 | [diff] [blame] | 42 | int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info) |
| 43 | { |
| 44 | info->so_timestamping = |
| 45 | SOF_TIMESTAMPING_TX_SOFTWARE | |
| 46 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 47 | SOF_TIMESTAMPING_SOFTWARE; |
| 48 | info->phc_index = -1; |
| 49 | return 0; |
| 50 | } |
| 51 | EXPORT_SYMBOL(ethtool_op_get_ts_info); |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* Handlers for each ethtool command */ |
| 54 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 55 | #define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 56 | |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 57 | static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = { |
| 58 | [NETIF_F_SG_BIT] = "tx-scatter-gather", |
| 59 | [NETIF_F_IP_CSUM_BIT] = "tx-checksum-ipv4", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 60 | [NETIF_F_HW_CSUM_BIT] = "tx-checksum-ip-generic", |
| 61 | [NETIF_F_IPV6_CSUM_BIT] = "tx-checksum-ipv6", |
| 62 | [NETIF_F_HIGHDMA_BIT] = "highdma", |
| 63 | [NETIF_F_FRAGLIST_BIT] = "tx-scatter-gather-fraglist", |
Fernando Luis Vazquez Cao | 788dfca | 2013-06-17 11:28:03 +0900 | [diff] [blame] | 64 | [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 65 | |
Fernando Luis Vazquez Cao | 788dfca | 2013-06-17 11:28:03 +0900 | [diff] [blame] | 66 | [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse", |
| 67 | [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter", |
David S. Miller | 2d6577f | 2013-04-19 16:08:23 -0400 | [diff] [blame] | 68 | [NETIF_F_HW_VLAN_STAG_TX_BIT] = "tx-vlan-stag-hw-insert", |
| 69 | [NETIF_F_HW_VLAN_STAG_RX_BIT] = "rx-vlan-stag-hw-parse", |
| 70 | [NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 71 | [NETIF_F_VLAN_CHALLENGED_BIT] = "vlan-challenged", |
| 72 | [NETIF_F_GSO_BIT] = "tx-generic-segmentation", |
| 73 | [NETIF_F_LLTX_BIT] = "tx-lockless", |
| 74 | [NETIF_F_NETNS_LOCAL_BIT] = "netns-local", |
| 75 | [NETIF_F_GRO_BIT] = "rx-gro", |
| 76 | [NETIF_F_LRO_BIT] = "rx-lro", |
| 77 | |
| 78 | [NETIF_F_TSO_BIT] = "tx-tcp-segmentation", |
| 79 | [NETIF_F_UFO_BIT] = "tx-udp-fragmentation", |
| 80 | [NETIF_F_GSO_ROBUST_BIT] = "tx-gso-robust", |
| 81 | [NETIF_F_TSO_ECN_BIT] = "tx-tcp-ecn-segmentation", |
Alexander Duyck | cbc53e0 | 2016-04-10 21:44:51 -0400 | [diff] [blame] | 82 | [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 83 | [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation", |
| 84 | [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation", |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 85 | [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation", |
Alexander Duyck | 518f213 | 2016-04-10 21:44:44 -0400 | [diff] [blame] | 86 | [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation", |
Tom Herbert | 7e13318 | 2016-05-18 09:06:10 -0700 | [diff] [blame] | 87 | [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation", |
| 88 | [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation", |
Pravin B Shelar | 7313626 | 2013-03-07 13:21:51 +0000 | [diff] [blame] | 89 | [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation", |
Alexander Duyck | 518f213 | 2016-04-10 21:44:44 -0400 | [diff] [blame] | 90 | [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation", |
Alexander Duyck | 802ab55 | 2016-04-10 21:45:03 -0400 | [diff] [blame] | 91 | [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial", |
Marcelo Ricardo Leitner | 90017ac | 2016-06-02 15:05:43 -0300 | [diff] [blame] | 92 | [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation", |
Steffen Klassert | c7ef8f0 | 2017-04-14 10:05:36 +0200 | [diff] [blame] | 93 | [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 94 | |
| 95 | [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc", |
Tom Herbert | 53692b1 | 2015-12-14 11:19:41 -0800 | [diff] [blame] | 96 | [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 97 | [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu", |
| 98 | [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter", |
| 99 | [NETIF_F_RXHASH_BIT] = "rx-hashing", |
| 100 | [NETIF_F_RXCSUM_BIT] = "rx-checksum", |
| 101 | [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy", |
| 102 | [NETIF_F_LOOPBACK_BIT] = "loopback", |
Ben Greear | 36eabda3 | 2012-02-11 15:39:14 +0000 | [diff] [blame] | 103 | [NETIF_F_RXFCS_BIT] = "rx-fcs", |
Ben Greear | 5e0c03c | 2012-02-11 15:39:45 +0000 | [diff] [blame] | 104 | [NETIF_F_RXALL_BIT] = "rx-all", |
John Fastabend | a6cc0cf | 2013-11-06 09:54:46 -0800 | [diff] [blame] | 105 | [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload", |
John Fastabend | 1c78c64 | 2016-02-16 21:17:37 -0800 | [diff] [blame] | 106 | [NETIF_F_HW_TC_BIT] = "hw-tc-offload", |
Steffen Klassert | c7ef8f0 | 2017-04-14 10:05:36 +0200 | [diff] [blame] | 107 | [NETIF_F_HW_ESP_BIT] = "esp-hw-offload", |
| 108 | [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload", |
Michał Mirosław | 9d92154 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 111 | static const char |
| 112 | rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = { |
| 113 | [ETH_RSS_HASH_TOP_BIT] = "toeplitz", |
| 114 | [ETH_RSS_HASH_XOR_BIT] = "xor", |
Jakub Kicinski | abb521e | 2017-03-08 08:57:00 -0800 | [diff] [blame] | 115 | [ETH_RSS_HASH_CRC32_BIT] = "crc32", |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 116 | }; |
| 117 | |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 118 | static const char |
| 119 | tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = { |
| 120 | [ETHTOOL_ID_UNSPEC] = "Unspec", |
| 121 | [ETHTOOL_RX_COPYBREAK] = "rx-copybreak", |
| 122 | [ETHTOOL_TX_COPYBREAK] = "tx-copybreak", |
| 123 | }; |
| 124 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 125 | static const char |
| 126 | phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = { |
| 127 | [ETHTOOL_ID_UNSPEC] = "Unspec", |
Raju Lakkaraju | 65feddd | 2016-11-17 13:07:23 +0100 | [diff] [blame] | 128 | [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift", |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 129 | }; |
| 130 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 131 | static int ethtool_get_features(struct net_device *dev, void __user *useraddr) |
| 132 | { |
| 133 | struct ethtool_gfeatures cmd = { |
| 134 | .cmd = ETHTOOL_GFEATURES, |
| 135 | .size = ETHTOOL_DEV_FEATURE_WORDS, |
| 136 | }; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 137 | struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 138 | u32 __user *sizeaddr; |
| 139 | u32 copy_size; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 140 | int i; |
| 141 | |
| 142 | /* in case feature bits run out again */ |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 143 | BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t)); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 144 | |
| 145 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 146 | features[i].available = (u32)(dev->hw_features >> (32 * i)); |
| 147 | features[i].requested = (u32)(dev->wanted_features >> (32 * i)); |
| 148 | features[i].active = (u32)(dev->features >> (32 * i)); |
| 149 | features[i].never_changed = |
| 150 | (u32)(NETIF_F_NEVER_CHANGE >> (32 * i)); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 151 | } |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 152 | |
| 153 | sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size); |
| 154 | if (get_user(copy_size, sizeaddr)) |
| 155 | return -EFAULT; |
| 156 | |
| 157 | if (copy_size > ETHTOOL_DEV_FEATURE_WORDS) |
| 158 | copy_size = ETHTOOL_DEV_FEATURE_WORDS; |
| 159 | |
| 160 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) |
| 161 | return -EFAULT; |
| 162 | useraddr += sizeof(cmd); |
| 163 | if (copy_to_user(useraddr, features, copy_size * sizeof(*features))) |
| 164 | return -EFAULT; |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | static int ethtool_set_features(struct net_device *dev, void __user *useraddr) |
| 170 | { |
| 171 | struct ethtool_sfeatures cmd; |
| 172 | struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS]; |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 173 | netdev_features_t wanted = 0, valid = 0; |
| 174 | int i, ret = 0; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 175 | |
| 176 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 177 | return -EFAULT; |
| 178 | useraddr += sizeof(cmd); |
| 179 | |
| 180 | if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | if (copy_from_user(features, useraddr, sizeof(features))) |
| 184 | return -EFAULT; |
| 185 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 186 | for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) { |
Michał Mirosław | 09da71b | 2011-11-16 14:32:03 +0000 | [diff] [blame] | 187 | valid |= (netdev_features_t)features[i].valid << (32 * i); |
| 188 | wanted |= (netdev_features_t)features[i].requested << (32 * i); |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if (valid & ~NETIF_F_ETHTOOL_BITS) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 192 | return -EINVAL; |
| 193 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 194 | if (valid & ~dev->hw_features) { |
| 195 | valid &= dev->hw_features; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 196 | ret |= ETHTOOL_F_UNSUPPORTED; |
| 197 | } |
| 198 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 199 | dev->wanted_features &= ~valid; |
| 200 | dev->wanted_features |= wanted & valid; |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 201 | __netdev_update_features(dev); |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 202 | |
Michał Mirosław | 475414f | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 203 | if ((dev->wanted_features ^ dev->features) & valid) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 204 | ret |= ETHTOOL_F_WISH; |
| 205 | |
| 206 | return ret; |
| 207 | } |
| 208 | |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 209 | static int phy_get_sset_count(struct phy_device *phydev) |
| 210 | { |
| 211 | int ret; |
| 212 | |
| 213 | if (phydev->drv->get_sset_count && |
| 214 | phydev->drv->get_strings && |
| 215 | phydev->drv->get_stats) { |
| 216 | mutex_lock(&phydev->lock); |
| 217 | ret = phydev->drv->get_sset_count(phydev); |
| 218 | mutex_unlock(&phydev->lock); |
| 219 | |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | return -EOPNOTSUPP; |
| 224 | } |
| 225 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 226 | static int __ethtool_get_sset_count(struct net_device *dev, int sset) |
| 227 | { |
| 228 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 229 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 230 | if (sset == ETH_SS_FEATURES) |
| 231 | return ARRAY_SIZE(netdev_features_strings); |
| 232 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 233 | if (sset == ETH_SS_RSS_HASH_FUNCS) |
| 234 | return ARRAY_SIZE(rss_hash_func_strings); |
| 235 | |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 236 | if (sset == ETH_SS_TUNABLES) |
| 237 | return ARRAY_SIZE(tunable_strings); |
| 238 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 239 | if (sset == ETH_SS_PHY_TUNABLES) |
| 240 | return ARRAY_SIZE(phy_tunable_strings); |
| 241 | |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 242 | if (sset == ETH_SS_PHY_STATS) { |
| 243 | if (dev->phydev) |
| 244 | return phy_get_sset_count(dev->phydev); |
| 245 | else |
| 246 | return -EOPNOTSUPP; |
| 247 | } |
| 248 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 249 | if (ops->get_sset_count && ops->get_strings) |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 250 | return ops->get_sset_count(dev, sset); |
| 251 | else |
| 252 | return -EOPNOTSUPP; |
| 253 | } |
| 254 | |
| 255 | static void __ethtool_get_strings(struct net_device *dev, |
| 256 | u32 stringset, u8 *data) |
| 257 | { |
| 258 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 259 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 260 | if (stringset == ETH_SS_FEATURES) |
| 261 | memcpy(data, netdev_features_strings, |
| 262 | sizeof(netdev_features_strings)); |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 263 | else if (stringset == ETH_SS_RSS_HASH_FUNCS) |
| 264 | memcpy(data, rss_hash_func_strings, |
| 265 | sizeof(rss_hash_func_strings)); |
Hadar Hen Zion | a4244b0 | 2015-06-11 10:28:16 +0300 | [diff] [blame] | 266 | else if (stringset == ETH_SS_TUNABLES) |
| 267 | memcpy(data, tunable_strings, sizeof(tunable_strings)); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 268 | else if (stringset == ETH_SS_PHY_TUNABLES) |
| 269 | memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings)); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 270 | else if (stringset == ETH_SS_PHY_STATS) { |
| 271 | struct phy_device *phydev = dev->phydev; |
| 272 | |
| 273 | if (phydev) { |
| 274 | mutex_lock(&phydev->lock); |
| 275 | phydev->drv->get_strings(phydev, data); |
| 276 | mutex_unlock(&phydev->lock); |
| 277 | } else { |
| 278 | return; |
| 279 | } |
| 280 | } else |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 281 | /* ops->get_strings is valid because checked earlier */ |
| 282 | ops->get_strings(dev, stringset, data); |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 285 | static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd) |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 286 | { |
| 287 | /* feature masks of legacy discrete ethtool ops */ |
| 288 | |
| 289 | switch (eth_cmd) { |
| 290 | case ETHTOOL_GTXCSUM: |
| 291 | case ETHTOOL_STXCSUM: |
Tom Herbert | a188222 | 2015-12-14 11:19:43 -0800 | [diff] [blame] | 292 | return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC; |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 293 | case ETHTOOL_GRXCSUM: |
| 294 | case ETHTOOL_SRXCSUM: |
| 295 | return NETIF_F_RXCSUM; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 296 | case ETHTOOL_GSG: |
| 297 | case ETHTOOL_SSG: |
| 298 | return NETIF_F_SG; |
| 299 | case ETHTOOL_GTSO: |
| 300 | case ETHTOOL_STSO: |
| 301 | return NETIF_F_ALL_TSO; |
| 302 | case ETHTOOL_GUFO: |
| 303 | case ETHTOOL_SUFO: |
| 304 | return NETIF_F_UFO; |
| 305 | case ETHTOOL_GGSO: |
| 306 | case ETHTOOL_SGSO: |
| 307 | return NETIF_F_GSO; |
| 308 | case ETHTOOL_GGRO: |
| 309 | case ETHTOOL_SGRO: |
| 310 | return NETIF_F_GRO; |
| 311 | default: |
| 312 | BUG(); |
| 313 | } |
| 314 | } |
| 315 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 316 | static int ethtool_get_one_feature(struct net_device *dev, |
| 317 | char __user *useraddr, u32 ethcmd) |
| 318 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 319 | netdev_features_t mask = ethtool_get_feature_mask(ethcmd); |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 320 | struct ethtool_value edata = { |
| 321 | .cmd = ethcmd, |
Michał Mirosław | 8679488 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 322 | .data = !!(dev->features & mask), |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 323 | }; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 324 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 325 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 326 | return -EFAULT; |
| 327 | return 0; |
| 328 | } |
| 329 | |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 330 | static int ethtool_set_one_feature(struct net_device *dev, |
| 331 | void __user *useraddr, u32 ethcmd) |
| 332 | { |
| 333 | struct ethtool_value edata; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 334 | netdev_features_t mask; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 335 | |
| 336 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 337 | return -EFAULT; |
| 338 | |
Michał Mirosław | 8679488 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 339 | mask = ethtool_get_feature_mask(ethcmd); |
| 340 | mask &= dev->hw_features; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 341 | if (!mask) |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 342 | return -EOPNOTSUPP; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 343 | |
| 344 | if (edata.data) |
| 345 | dev->wanted_features |= mask; |
| 346 | else |
| 347 | dev->wanted_features &= ~mask; |
| 348 | |
| 349 | __netdev_update_features(dev); |
| 350 | |
| 351 | return 0; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 354 | #define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \ |
| 355 | ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH) |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 356 | #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \ |
| 357 | NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \ |
| 358 | NETIF_F_RXHASH) |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 359 | |
| 360 | static u32 __ethtool_get_flags(struct net_device *dev) |
| 361 | { |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 362 | u32 flags = 0; |
| 363 | |
Dragos Foianu | 8a73125 | 2013-07-13 14:43:00 +0100 | [diff] [blame] | 364 | if (dev->features & NETIF_F_LRO) |
| 365 | flags |= ETH_FLAG_LRO; |
| 366 | if (dev->features & NETIF_F_HW_VLAN_CTAG_RX) |
| 367 | flags |= ETH_FLAG_RXVLAN; |
| 368 | if (dev->features & NETIF_F_HW_VLAN_CTAG_TX) |
| 369 | flags |= ETH_FLAG_TXVLAN; |
| 370 | if (dev->features & NETIF_F_NTUPLE) |
| 371 | flags |= ETH_FLAG_NTUPLE; |
| 372 | if (dev->features & NETIF_F_RXHASH) |
| 373 | flags |= ETH_FLAG_RXHASH; |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 374 | |
| 375 | return flags; |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static int __ethtool_set_flags(struct net_device *dev, u32 data) |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 379 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 380 | netdev_features_t features = 0, changed; |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 381 | |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 382 | if (data & ~ETH_ALL_FLAGS) |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 383 | return -EINVAL; |
| 384 | |
Dragos Foianu | 8a73125 | 2013-07-13 14:43:00 +0100 | [diff] [blame] | 385 | if (data & ETH_FLAG_LRO) |
| 386 | features |= NETIF_F_LRO; |
| 387 | if (data & ETH_FLAG_RXVLAN) |
| 388 | features |= NETIF_F_HW_VLAN_CTAG_RX; |
| 389 | if (data & ETH_FLAG_TXVLAN) |
| 390 | features |= NETIF_F_HW_VLAN_CTAG_TX; |
| 391 | if (data & ETH_FLAG_NTUPLE) |
| 392 | features |= NETIF_F_NTUPLE; |
| 393 | if (data & ETH_FLAG_RXHASH) |
| 394 | features |= NETIF_F_RXHASH; |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 395 | |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 396 | /* allow changing only bits set in hw_features */ |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 397 | changed = (features ^ dev->features) & ETH_ALL_FEATURES; |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 398 | if (changed & ~dev->hw_features) |
| 399 | return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP; |
| 400 | |
| 401 | dev->wanted_features = |
Michał Mirosław | 02b3a55 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 402 | (dev->wanted_features & ~changed) | (features & changed); |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 403 | |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 404 | __netdev_update_features(dev); |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 409 | void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst, |
| 410 | u32 legacy_u32) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 411 | { |
| 412 | bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 413 | dst[0] = legacy_u32; |
| 414 | } |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 415 | EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 416 | |
| 417 | /* return false if src had higher bits set. lower bits always updated. */ |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 418 | bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32, |
| 419 | const unsigned long *src) |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 420 | { |
| 421 | bool retval = true; |
| 422 | |
| 423 | /* TODO: following test will soon always be true */ |
| 424 | if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) { |
| 425 | __ETHTOOL_DECLARE_LINK_MODE_MASK(ext); |
| 426 | |
| 427 | bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 428 | bitmap_fill(ext, 32); |
| 429 | bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 430 | if (bitmap_intersects(ext, src, |
| 431 | __ETHTOOL_LINK_MODE_MASK_NBITS)) { |
| 432 | /* src mask goes beyond bit 31 */ |
| 433 | retval = false; |
| 434 | } |
| 435 | } |
| 436 | *legacy_u32 = src[0]; |
| 437 | return retval; |
| 438 | } |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 439 | EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 440 | |
| 441 | /* return false if legacy contained non-0 deprecated fields |
| 442 | * transceiver/maxtxpkt/maxrxpkt. rest of ksettings always updated |
| 443 | */ |
| 444 | static bool |
| 445 | convert_legacy_settings_to_link_ksettings( |
| 446 | struct ethtool_link_ksettings *link_ksettings, |
| 447 | const struct ethtool_cmd *legacy_settings) |
| 448 | { |
| 449 | bool retval = true; |
| 450 | |
| 451 | memset(link_ksettings, 0, sizeof(*link_ksettings)); |
| 452 | |
| 453 | /* This is used to tell users that driver is still using these |
| 454 | * deprecated legacy fields, and they should not use |
| 455 | * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS |
| 456 | */ |
| 457 | if (legacy_settings->transceiver || |
| 458 | legacy_settings->maxtxpkt || |
| 459 | legacy_settings->maxrxpkt) |
| 460 | retval = false; |
| 461 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 462 | ethtool_convert_legacy_u32_to_link_mode( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 463 | link_ksettings->link_modes.supported, |
| 464 | legacy_settings->supported); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 465 | ethtool_convert_legacy_u32_to_link_mode( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 466 | link_ksettings->link_modes.advertising, |
| 467 | legacy_settings->advertising); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 468 | ethtool_convert_legacy_u32_to_link_mode( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 469 | link_ksettings->link_modes.lp_advertising, |
| 470 | legacy_settings->lp_advertising); |
| 471 | link_ksettings->base.speed |
| 472 | = ethtool_cmd_speed(legacy_settings); |
| 473 | link_ksettings->base.duplex |
| 474 | = legacy_settings->duplex; |
| 475 | link_ksettings->base.port |
| 476 | = legacy_settings->port; |
| 477 | link_ksettings->base.phy_address |
| 478 | = legacy_settings->phy_address; |
| 479 | link_ksettings->base.autoneg |
| 480 | = legacy_settings->autoneg; |
| 481 | link_ksettings->base.mdio_support |
| 482 | = legacy_settings->mdio_support; |
| 483 | link_ksettings->base.eth_tp_mdix |
| 484 | = legacy_settings->eth_tp_mdix; |
| 485 | link_ksettings->base.eth_tp_mdix_ctrl |
| 486 | = legacy_settings->eth_tp_mdix_ctrl; |
| 487 | return retval; |
| 488 | } |
| 489 | |
| 490 | /* return false if ksettings link modes had higher bits |
| 491 | * set. legacy_settings always updated (best effort) |
| 492 | */ |
| 493 | static bool |
| 494 | convert_link_ksettings_to_legacy_settings( |
| 495 | struct ethtool_cmd *legacy_settings, |
| 496 | const struct ethtool_link_ksettings *link_ksettings) |
| 497 | { |
| 498 | bool retval = true; |
| 499 | |
| 500 | memset(legacy_settings, 0, sizeof(*legacy_settings)); |
| 501 | /* this also clears the deprecated fields in legacy structure: |
| 502 | * __u8 transceiver; |
| 503 | * __u32 maxtxpkt; |
| 504 | * __u32 maxrxpkt; |
| 505 | */ |
| 506 | |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 507 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 508 | &legacy_settings->supported, |
| 509 | link_ksettings->link_modes.supported); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 510 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 511 | &legacy_settings->advertising, |
| 512 | link_ksettings->link_modes.advertising); |
Philippe Reynes | 6d62b4d | 2016-04-15 00:34:59 +0200 | [diff] [blame] | 513 | retval &= ethtool_convert_link_mode_to_legacy_u32( |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 514 | &legacy_settings->lp_advertising, |
| 515 | link_ksettings->link_modes.lp_advertising); |
| 516 | ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed); |
| 517 | legacy_settings->duplex |
| 518 | = link_ksettings->base.duplex; |
| 519 | legacy_settings->port |
| 520 | = link_ksettings->base.port; |
| 521 | legacy_settings->phy_address |
| 522 | = link_ksettings->base.phy_address; |
| 523 | legacy_settings->autoneg |
| 524 | = link_ksettings->base.autoneg; |
| 525 | legacy_settings->mdio_support |
| 526 | = link_ksettings->base.mdio_support; |
| 527 | legacy_settings->eth_tp_mdix |
| 528 | = link_ksettings->base.eth_tp_mdix; |
| 529 | legacy_settings->eth_tp_mdix_ctrl |
| 530 | = link_ksettings->base.eth_tp_mdix_ctrl; |
| 531 | return retval; |
| 532 | } |
| 533 | |
| 534 | /* number of 32-bit words to store the user's link mode bitmaps */ |
| 535 | #define __ETHTOOL_LINK_MODE_MASK_NU32 \ |
| 536 | DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32) |
| 537 | |
| 538 | /* layout of the struct passed from/to userland */ |
| 539 | struct ethtool_link_usettings { |
| 540 | struct ethtool_link_settings base; |
| 541 | struct { |
| 542 | __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 543 | __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 544 | __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32]; |
| 545 | } link_modes; |
| 546 | }; |
| 547 | |
| 548 | /* Internal kernel helper to query a device ethtool_link_settings. |
| 549 | * |
| 550 | * Backward compatibility note: for compatibility with legacy drivers |
| 551 | * that implement only the ethtool_cmd API, this has to work with both |
| 552 | * drivers implementing get_link_ksettings API and drivers |
| 553 | * implementing get_settings API. When drivers implement get_settings |
| 554 | * and report ethtool_cmd deprecated fields |
| 555 | * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored |
| 556 | * because the resulting struct ethtool_link_settings does not report them. |
| 557 | */ |
| 558 | int __ethtool_get_link_ksettings(struct net_device *dev, |
| 559 | struct ethtool_link_ksettings *link_ksettings) |
| 560 | { |
| 561 | int err; |
| 562 | struct ethtool_cmd cmd; |
| 563 | |
| 564 | ASSERT_RTNL(); |
| 565 | |
| 566 | if (dev->ethtool_ops->get_link_ksettings) { |
| 567 | memset(link_ksettings, 0, sizeof(*link_ksettings)); |
| 568 | return dev->ethtool_ops->get_link_ksettings(dev, |
| 569 | link_ksettings); |
| 570 | } |
| 571 | |
| 572 | /* driver doesn't support %ethtool_link_ksettings API. revert to |
| 573 | * legacy %ethtool_cmd API, unless it's not supported either. |
| 574 | * TODO: remove when ethtool_ops::get_settings disappears internally |
| 575 | */ |
David Decotigny | 3237fc6 | 2016-02-24 10:58:11 -0800 | [diff] [blame] | 576 | if (!dev->ethtool_ops->get_settings) |
| 577 | return -EOPNOTSUPP; |
| 578 | |
| 579 | memset(&cmd, 0, sizeof(cmd)); |
| 580 | cmd.cmd = ETHTOOL_GSET; |
| 581 | err = dev->ethtool_ops->get_settings(dev, &cmd); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 582 | if (err < 0) |
| 583 | return err; |
| 584 | |
| 585 | /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt |
| 586 | */ |
| 587 | convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd); |
| 588 | return err; |
| 589 | } |
| 590 | EXPORT_SYMBOL(__ethtool_get_link_ksettings); |
| 591 | |
| 592 | /* convert ethtool_link_usettings in user space to a kernel internal |
| 593 | * ethtool_link_ksettings. return 0 on success, errno on error. |
| 594 | */ |
| 595 | static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to, |
| 596 | const void __user *from) |
| 597 | { |
| 598 | struct ethtool_link_usettings link_usettings; |
| 599 | |
| 600 | if (copy_from_user(&link_usettings, from, sizeof(link_usettings))) |
| 601 | return -EFAULT; |
| 602 | |
| 603 | memcpy(&to->base, &link_usettings.base, sizeof(to->base)); |
| 604 | bitmap_from_u32array(to->link_modes.supported, |
| 605 | __ETHTOOL_LINK_MODE_MASK_NBITS, |
| 606 | link_usettings.link_modes.supported, |
| 607 | __ETHTOOL_LINK_MODE_MASK_NU32); |
| 608 | bitmap_from_u32array(to->link_modes.advertising, |
| 609 | __ETHTOOL_LINK_MODE_MASK_NBITS, |
| 610 | link_usettings.link_modes.advertising, |
| 611 | __ETHTOOL_LINK_MODE_MASK_NU32); |
| 612 | bitmap_from_u32array(to->link_modes.lp_advertising, |
| 613 | __ETHTOOL_LINK_MODE_MASK_NBITS, |
| 614 | link_usettings.link_modes.lp_advertising, |
| 615 | __ETHTOOL_LINK_MODE_MASK_NU32); |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | /* convert a kernel internal ethtool_link_ksettings to |
| 621 | * ethtool_link_usettings in user space. return 0 on success, errno on |
| 622 | * error. |
| 623 | */ |
| 624 | static int |
| 625 | store_link_ksettings_for_user(void __user *to, |
| 626 | const struct ethtool_link_ksettings *from) |
| 627 | { |
| 628 | struct ethtool_link_usettings link_usettings; |
| 629 | |
| 630 | memcpy(&link_usettings.base, &from->base, sizeof(link_usettings)); |
| 631 | bitmap_to_u32array(link_usettings.link_modes.supported, |
| 632 | __ETHTOOL_LINK_MODE_MASK_NU32, |
| 633 | from->link_modes.supported, |
| 634 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 635 | bitmap_to_u32array(link_usettings.link_modes.advertising, |
| 636 | __ETHTOOL_LINK_MODE_MASK_NU32, |
| 637 | from->link_modes.advertising, |
| 638 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 639 | bitmap_to_u32array(link_usettings.link_modes.lp_advertising, |
| 640 | __ETHTOOL_LINK_MODE_MASK_NU32, |
| 641 | from->link_modes.lp_advertising, |
| 642 | __ETHTOOL_LINK_MODE_MASK_NBITS); |
| 643 | |
| 644 | if (copy_to_user(to, &link_usettings, sizeof(link_usettings))) |
| 645 | return -EFAULT; |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* Query device for its ethtool_link_settings. |
| 651 | * |
| 652 | * Backward compatibility note: this function must fail when driver |
| 653 | * does not implement ethtool::get_link_ksettings, even if legacy |
| 654 | * ethtool_ops::get_settings is implemented. This tells new versions |
| 655 | * of ethtool that they should use the legacy API %ETHTOOL_GSET for |
| 656 | * this driver, so that they can correctly access the ethtool_cmd |
| 657 | * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver |
| 658 | * implements ethtool_ops::get_settings anymore. |
| 659 | */ |
| 660 | static int ethtool_get_link_ksettings(struct net_device *dev, |
| 661 | void __user *useraddr) |
| 662 | { |
| 663 | int err = 0; |
| 664 | struct ethtool_link_ksettings link_ksettings; |
| 665 | |
| 666 | ASSERT_RTNL(); |
| 667 | |
| 668 | if (!dev->ethtool_ops->get_link_ksettings) |
| 669 | return -EOPNOTSUPP; |
| 670 | |
| 671 | /* handle bitmap nbits handshake */ |
| 672 | if (copy_from_user(&link_ksettings.base, useraddr, |
| 673 | sizeof(link_ksettings.base))) |
| 674 | return -EFAULT; |
| 675 | |
| 676 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 677 | != link_ksettings.base.link_mode_masks_nwords) { |
| 678 | /* wrong link mode nbits requested */ |
| 679 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
Ben Hutchings | 793cf87 | 2016-03-14 01:05:38 +0000 | [diff] [blame] | 680 | link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 681 | /* send back number of words required as negative val */ |
| 682 | compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX, |
| 683 | "need too many bits for link modes!"); |
| 684 | link_ksettings.base.link_mode_masks_nwords |
| 685 | = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32); |
| 686 | |
| 687 | /* copy the base fields back to user, not the link |
| 688 | * mode bitmaps |
| 689 | */ |
| 690 | if (copy_to_user(useraddr, &link_ksettings.base, |
| 691 | sizeof(link_ksettings.base))) |
| 692 | return -EFAULT; |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | /* handshake successful: user/kernel agree on |
| 698 | * link_mode_masks_nwords |
| 699 | */ |
| 700 | |
| 701 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
| 702 | err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings); |
| 703 | if (err < 0) |
| 704 | return err; |
| 705 | |
| 706 | /* make sure we tell the right values to user */ |
| 707 | link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS; |
| 708 | link_ksettings.base.link_mode_masks_nwords |
| 709 | = __ETHTOOL_LINK_MODE_MASK_NU32; |
| 710 | |
| 711 | return store_link_ksettings_for_user(useraddr, &link_ksettings); |
| 712 | } |
| 713 | |
| 714 | /* Update device ethtool_link_settings. |
| 715 | * |
| 716 | * Backward compatibility note: this function must fail when driver |
| 717 | * does not implement ethtool::set_link_ksettings, even if legacy |
| 718 | * ethtool_ops::set_settings is implemented. This tells new versions |
| 719 | * of ethtool that they should use the legacy API %ETHTOOL_SSET for |
| 720 | * this driver, so that they can correctly update the ethtool_cmd |
| 721 | * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver |
| 722 | * implements ethtool_ops::get_settings anymore. |
| 723 | */ |
| 724 | static int ethtool_set_link_ksettings(struct net_device *dev, |
| 725 | void __user *useraddr) |
| 726 | { |
| 727 | int err; |
| 728 | struct ethtool_link_ksettings link_ksettings; |
| 729 | |
| 730 | ASSERT_RTNL(); |
| 731 | |
| 732 | if (!dev->ethtool_ops->set_link_ksettings) |
| 733 | return -EOPNOTSUPP; |
| 734 | |
| 735 | /* make sure nbits field has expected value */ |
| 736 | if (copy_from_user(&link_ksettings.base, useraddr, |
| 737 | sizeof(link_ksettings.base))) |
| 738 | return -EFAULT; |
| 739 | |
| 740 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 741 | != link_ksettings.base.link_mode_masks_nwords) |
| 742 | return -EINVAL; |
| 743 | |
| 744 | /* copy the whole structure, now that we know it has expected |
| 745 | * format |
| 746 | */ |
| 747 | err = load_link_ksettings_from_user(&link_ksettings, useraddr); |
| 748 | if (err) |
| 749 | return err; |
| 750 | |
| 751 | /* re-check nwords field, just in case */ |
| 752 | if (__ETHTOOL_LINK_MODE_MASK_NU32 |
| 753 | != link_ksettings.base.link_mode_masks_nwords) |
| 754 | return -EINVAL; |
| 755 | |
| 756 | return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings); |
| 757 | } |
| 758 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 759 | static void |
| 760 | warn_incomplete_ethtool_legacy_settings_conversion(const char *details) |
| 761 | { |
| 762 | char name[sizeof(current->comm)]; |
| 763 | |
| 764 | pr_info_once("warning: `%s' uses legacy ethtool link settings API, %s\n", |
| 765 | get_task_comm(name, current), details); |
| 766 | } |
| 767 | |
| 768 | /* Query device for its ethtool_cmd settings. |
| 769 | * |
| 770 | * Backward compatibility note: for compatibility with legacy ethtool, |
| 771 | * this has to work with both drivers implementing get_link_ksettings |
| 772 | * API and drivers implementing get_settings API. When drivers |
| 773 | * implement get_link_ksettings and report higher link mode bits, a |
| 774 | * kernel warning is logged once (with name of 1st driver/device) to |
| 775 | * recommend user to upgrade ethtool, but the command is successful |
| 776 | * (only the lower link mode bits reported back to user). |
| 777 | */ |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 778 | static int ethtool_get_settings(struct net_device *dev, void __user *useraddr) |
| 779 | { |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 780 | struct ethtool_cmd cmd; |
| 781 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 782 | ASSERT_RTNL(); |
| 783 | |
| 784 | if (dev->ethtool_ops->get_link_ksettings) { |
| 785 | /* First, use link_ksettings API if it is supported */ |
| 786 | int err; |
| 787 | struct ethtool_link_ksettings link_ksettings; |
| 788 | |
| 789 | memset(&link_ksettings, 0, sizeof(link_ksettings)); |
| 790 | err = dev->ethtool_ops->get_link_ksettings(dev, |
| 791 | &link_ksettings); |
| 792 | if (err < 0) |
| 793 | return err; |
| 794 | if (!convert_link_ksettings_to_legacy_settings(&cmd, |
| 795 | &link_ksettings)) |
| 796 | warn_incomplete_ethtool_legacy_settings_conversion( |
| 797 | "link modes are only partially reported"); |
| 798 | |
| 799 | /* send a sensible cmd tag back to user */ |
| 800 | cmd.cmd = ETHTOOL_GSET; |
| 801 | } else { |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 802 | /* driver doesn't support %ethtool_link_ksettings |
| 803 | * API. revert to legacy %ethtool_cmd API, unless it's |
| 804 | * not supported either. |
| 805 | */ |
David Decotigny | 3237fc6 | 2016-02-24 10:58:11 -0800 | [diff] [blame] | 806 | int err; |
| 807 | |
| 808 | if (!dev->ethtool_ops->get_settings) |
| 809 | return -EOPNOTSUPP; |
| 810 | |
| 811 | memset(&cmd, 0, sizeof(cmd)); |
| 812 | cmd.cmd = ETHTOOL_GSET; |
| 813 | err = dev->ethtool_ops->get_settings(dev, &cmd); |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 814 | if (err < 0) |
| 815 | return err; |
| 816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | if (copy_to_user(useraddr, &cmd, sizeof(cmd))) |
| 819 | return -EFAULT; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 820 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 824 | /* Update device link settings with given ethtool_cmd. |
| 825 | * |
| 826 | * Backward compatibility note: for compatibility with legacy ethtool, |
| 827 | * this has to work with both drivers implementing set_link_ksettings |
| 828 | * API and drivers implementing set_settings API. When drivers |
| 829 | * implement set_link_ksettings and user's request updates deprecated |
| 830 | * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel |
| 831 | * warning is logged once (with name of 1st driver/device) to |
| 832 | * recommend user to upgrade ethtool, and the request is rejected. |
| 833 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | static int ethtool_set_settings(struct net_device *dev, void __user *useraddr) |
| 835 | { |
| 836 | struct ethtool_cmd cmd; |
| 837 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 838 | ASSERT_RTNL(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
| 840 | if (copy_from_user(&cmd, useraddr, sizeof(cmd))) |
| 841 | return -EFAULT; |
| 842 | |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 843 | /* first, try new %ethtool_link_ksettings API. */ |
| 844 | if (dev->ethtool_ops->set_link_ksettings) { |
| 845 | struct ethtool_link_ksettings link_ksettings; |
| 846 | |
| 847 | if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, |
| 848 | &cmd)) |
| 849 | return -EINVAL; |
| 850 | |
| 851 | link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS; |
| 852 | link_ksettings.base.link_mode_masks_nwords |
| 853 | = __ETHTOOL_LINK_MODE_MASK_NU32; |
| 854 | return dev->ethtool_ops->set_link_ksettings(dev, |
| 855 | &link_ksettings); |
| 856 | } |
| 857 | |
| 858 | /* legacy %ethtool_cmd API */ |
| 859 | |
| 860 | /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings |
| 861 | * disappears internally |
| 862 | */ |
| 863 | |
| 864 | if (!dev->ethtool_ops->set_settings) |
| 865 | return -EOPNOTSUPP; |
| 866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | return dev->ethtool_ops->set_settings(dev, &cmd); |
| 868 | } |
| 869 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 870 | static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev, |
| 871 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
| 873 | struct ethtool_drvinfo info; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 874 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | memset(&info, 0, sizeof(info)); |
| 877 | info.cmd = ETHTOOL_GDRVINFO; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 878 | if (ops->get_drvinfo) { |
Ben Hutchings | 0141480 | 2010-08-17 02:31:15 -0700 | [diff] [blame] | 879 | ops->get_drvinfo(dev, &info); |
| 880 | } else if (dev->dev.parent && dev->dev.parent->driver) { |
| 881 | strlcpy(info.bus_info, dev_name(dev->dev.parent), |
| 882 | sizeof(info.bus_info)); |
| 883 | strlcpy(info.driver, dev->dev.parent->driver->name, |
| 884 | sizeof(info.driver)); |
| 885 | } else { |
| 886 | return -EOPNOTSUPP; |
| 887 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 889 | /* |
| 890 | * this method of obtaining string set info is deprecated; |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 891 | * Use ETHTOOL_GSSET_INFO instead. |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 892 | */ |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 893 | if (ops->get_sset_count) { |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 894 | int rc; |
| 895 | |
| 896 | rc = ops->get_sset_count(dev, ETH_SS_TEST); |
| 897 | if (rc >= 0) |
| 898 | info.testinfo_len = rc; |
| 899 | rc = ops->get_sset_count(dev, ETH_SS_STATS); |
| 900 | if (rc >= 0) |
| 901 | info.n_stats = rc; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 902 | rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS); |
| 903 | if (rc >= 0) |
| 904 | info.n_priv_flags = rc; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 905 | } |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 906 | if (ops->get_regs_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | info.regdump_len = ops->get_regs_len(dev); |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 908 | if (ops->get_eeprom_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | info.eedump_len = ops->get_eeprom_len(dev); |
| 910 | |
| 911 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 912 | return -EFAULT; |
| 913 | return 0; |
| 914 | } |
| 915 | |
Eric Dumazet | f5c445e | 2010-03-08 12:17:04 -0800 | [diff] [blame] | 916 | static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev, |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 917 | void __user *useraddr) |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 918 | { |
| 919 | struct ethtool_sset_info info; |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 920 | u64 sset_mask; |
| 921 | int i, idx = 0, n_bits = 0, ret, rc; |
| 922 | u32 *info_buf = NULL; |
| 923 | |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 924 | if (copy_from_user(&info, useraddr, sizeof(info))) |
| 925 | return -EFAULT; |
| 926 | |
| 927 | /* store copy of mask, because we zero struct later on */ |
| 928 | sset_mask = info.sset_mask; |
| 929 | if (!sset_mask) |
| 930 | return 0; |
| 931 | |
| 932 | /* calculate size of return buffer */ |
Jeff Garzik | d17792e | 2010-03-04 08:21:53 +0000 | [diff] [blame] | 933 | n_bits = hweight64(sset_mask); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 934 | |
| 935 | memset(&info, 0, sizeof(info)); |
| 936 | info.cmd = ETHTOOL_GSSET_INFO; |
| 937 | |
| 938 | info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER); |
| 939 | if (!info_buf) |
| 940 | return -ENOMEM; |
| 941 | |
| 942 | /* |
| 943 | * fill return buffer based on input bitmask and successful |
| 944 | * get_sset_count return |
| 945 | */ |
| 946 | for (i = 0; i < 64; i++) { |
| 947 | if (!(sset_mask & (1ULL << i))) |
| 948 | continue; |
| 949 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 950 | rc = __ethtool_get_sset_count(dev, i); |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 951 | if (rc >= 0) { |
| 952 | info.sset_mask |= (1ULL << i); |
| 953 | info_buf[idx++] = rc; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | ret = -EFAULT; |
| 958 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 959 | goto out; |
| 960 | |
| 961 | useraddr += offsetof(struct ethtool_sset_info, data); |
| 962 | if (copy_to_user(useraddr, info_buf, idx * sizeof(u32))) |
| 963 | goto out; |
| 964 | |
| 965 | ret = 0; |
| 966 | |
| 967 | out: |
| 968 | kfree(info_buf); |
| 969 | return ret; |
| 970 | } |
| 971 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 972 | static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev, |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 973 | u32 cmd, void __user *useraddr) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 974 | { |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 975 | struct ethtool_rxnfc info; |
| 976 | size_t info_size = sizeof(info); |
Ben Hutchings | 55664f3 | 2012-01-03 12:04:51 +0000 | [diff] [blame] | 977 | int rc; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 978 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 979 | if (!dev->ethtool_ops->set_rxnfc) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 980 | return -EOPNOTSUPP; |
| 981 | |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 982 | /* struct ethtool_rxnfc was originally defined for |
| 983 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data |
| 984 | * members. User-space might still be using that |
| 985 | * definition. */ |
| 986 | if (cmd == ETHTOOL_SRXFH) |
| 987 | info_size = (offsetof(struct ethtool_rxnfc, data) + |
| 988 | sizeof(info.data)); |
| 989 | |
| 990 | if (copy_from_user(&info, useraddr, info_size)) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 991 | return -EFAULT; |
| 992 | |
Ben Hutchings | 55664f3 | 2012-01-03 12:04:51 +0000 | [diff] [blame] | 993 | rc = dev->ethtool_ops->set_rxnfc(dev, &info); |
| 994 | if (rc) |
| 995 | return rc; |
| 996 | |
| 997 | if (cmd == ETHTOOL_SRXCLSRLINS && |
| 998 | copy_to_user(useraddr, &info, info_size)) |
| 999 | return -EFAULT; |
| 1000 | |
| 1001 | return 0; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 1004 | static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev, |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 1005 | u32 cmd, void __user *useraddr) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1006 | { |
| 1007 | struct ethtool_rxnfc info; |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 1008 | size_t info_size = sizeof(info); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1009 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 1010 | int ret; |
| 1011 | void *rule_buf = NULL; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1012 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1013 | if (!ops->get_rxnfc) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1014 | return -EOPNOTSUPP; |
| 1015 | |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 1016 | /* struct ethtool_rxnfc was originally defined for |
| 1017 | * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data |
| 1018 | * members. User-space might still be using that |
| 1019 | * definition. */ |
| 1020 | if (cmd == ETHTOOL_GRXFH) |
| 1021 | info_size = (offsetof(struct ethtool_rxnfc, data) + |
| 1022 | sizeof(info.data)); |
| 1023 | |
| 1024 | if (copy_from_user(&info, useraddr, info_size)) |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1025 | return -EFAULT; |
| 1026 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1027 | if (info.cmd == ETHTOOL_GRXCLSRLALL) { |
| 1028 | if (info.rule_cnt > 0) { |
Ben Hutchings | db048b6 | 2010-06-28 08:44:07 +0000 | [diff] [blame] | 1029 | if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32)) |
Kees Cook | ae6df5f | 2010-10-07 10:03:48 +0000 | [diff] [blame] | 1030 | rule_buf = kzalloc(info.rule_cnt * sizeof(u32), |
Ben Hutchings | db048b6 | 2010-06-28 08:44:07 +0000 | [diff] [blame] | 1031 | GFP_USER); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1032 | if (!rule_buf) |
| 1033 | return -ENOMEM; |
| 1034 | } |
| 1035 | } |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1036 | |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1037 | ret = ops->get_rxnfc(dev, &info, rule_buf); |
| 1038 | if (ret < 0) |
| 1039 | goto err_out; |
| 1040 | |
| 1041 | ret = -EFAULT; |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 1042 | if (copy_to_user(useraddr, &info, info_size)) |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1043 | goto err_out; |
| 1044 | |
| 1045 | if (rule_buf) { |
| 1046 | useraddr += offsetof(struct ethtool_rxnfc, rule_locs); |
| 1047 | if (copy_to_user(useraddr, rule_buf, |
| 1048 | info.rule_cnt * sizeof(u32))) |
| 1049 | goto err_out; |
| 1050 | } |
| 1051 | ret = 0; |
| 1052 | |
| 1053 | err_out: |
Wei Yongjun | c9cacec | 2009-03-31 15:06:26 -0700 | [diff] [blame] | 1054 | kfree(rule_buf); |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 1055 | |
| 1056 | return ret; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1059 | static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr, |
| 1060 | struct ethtool_rxnfc *rx_rings, |
| 1061 | u32 size) |
| 1062 | { |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 1063 | int i; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1064 | |
| 1065 | if (copy_from_user(indir, useraddr, size * sizeof(indir[0]))) |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 1066 | return -EFAULT; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1067 | |
| 1068 | /* Validate ring indices */ |
Ben Hutchings | fb95cd8 | 2014-05-15 00:46:45 +0100 | [diff] [blame] | 1069 | for (i = 0; i < size; i++) |
| 1070 | if (indir[i] >= rx_rings->data) |
| 1071 | return -EINVAL; |
| 1072 | |
| 1073 | return 0; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1074 | } |
| 1075 | |
Kim Jones | ba905f5 | 2016-02-02 03:51:16 +0000 | [diff] [blame] | 1076 | u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly; |
Eric Dumazet | 960fb62 | 2014-11-16 06:23:05 -0800 | [diff] [blame] | 1077 | |
| 1078 | void netdev_rss_key_fill(void *buffer, size_t len) |
| 1079 | { |
| 1080 | BUG_ON(len > sizeof(netdev_rss_key)); |
| 1081 | net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key)); |
| 1082 | memcpy(buffer, netdev_rss_key, len); |
| 1083 | } |
| 1084 | EXPORT_SYMBOL(netdev_rss_key_fill); |
| 1085 | |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1086 | static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max) |
| 1087 | { |
| 1088 | u32 dev_size, current_max = 0; |
| 1089 | u32 *indir; |
| 1090 | int ret; |
| 1091 | |
| 1092 | if (!dev->ethtool_ops->get_rxfh_indir_size || |
| 1093 | !dev->ethtool_ops->get_rxfh) |
| 1094 | return -EOPNOTSUPP; |
| 1095 | dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); |
| 1096 | if (dev_size == 0) |
| 1097 | return -EOPNOTSUPP; |
| 1098 | |
| 1099 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
| 1100 | if (!indir) |
| 1101 | return -ENOMEM; |
| 1102 | |
| 1103 | ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL); |
| 1104 | if (ret) |
| 1105 | goto out; |
| 1106 | |
| 1107 | while (dev_size--) |
| 1108 | current_max = max(current_max, indir[dev_size]); |
| 1109 | |
| 1110 | *max = current_max; |
| 1111 | |
| 1112 | out: |
| 1113 | kfree(indir); |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1117 | static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev, |
| 1118 | void __user *useraddr) |
| 1119 | { |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1120 | u32 user_size, dev_size; |
| 1121 | u32 *indir; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1122 | int ret; |
| 1123 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1124 | if (!dev->ethtool_ops->get_rxfh_indir_size || |
Ben Hutchings | fe62d00 | 2014-05-15 01:25:27 +0100 | [diff] [blame] | 1125 | !dev->ethtool_ops->get_rxfh) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1126 | return -EOPNOTSUPP; |
| 1127 | dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev); |
| 1128 | if (dev_size == 0) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1129 | return -EOPNOTSUPP; |
| 1130 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1131 | if (copy_from_user(&user_size, |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1132 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1133 | sizeof(user_size))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1134 | return -EFAULT; |
| 1135 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1136 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size), |
| 1137 | &dev_size, sizeof(dev_size))) |
| 1138 | return -EFAULT; |
| 1139 | |
| 1140 | /* If the user buffer size is 0, this is just a query for the |
| 1141 | * device table size. Otherwise, if it's smaller than the |
| 1142 | * device table size it's an error. |
| 1143 | */ |
| 1144 | if (user_size < dev_size) |
| 1145 | return user_size == 0 ? 0 : -EINVAL; |
| 1146 | |
| 1147 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1148 | if (!indir) |
| 1149 | return -ENOMEM; |
| 1150 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1151 | ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1152 | if (ret) |
| 1153 | goto out; |
| 1154 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1155 | if (copy_to_user(useraddr + |
| 1156 | offsetof(struct ethtool_rxfh_indir, ring_index[0]), |
| 1157 | indir, dev_size * sizeof(indir[0]))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1158 | ret = -EFAULT; |
| 1159 | |
| 1160 | out: |
| 1161 | kfree(indir); |
| 1162 | return ret; |
| 1163 | } |
| 1164 | |
| 1165 | static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev, |
| 1166 | void __user *useraddr) |
| 1167 | { |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1168 | struct ethtool_rxnfc rx_rings; |
| 1169 | u32 user_size, dev_size, i; |
| 1170 | u32 *indir; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1171 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1172 | int ret; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1173 | u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1174 | |
Ben Hutchings | fe62d00 | 2014-05-15 01:25:27 +0100 | [diff] [blame] | 1175 | if (!ops->get_rxfh_indir_size || !ops->set_rxfh || |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1176 | !ops->get_rxnfc) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1177 | return -EOPNOTSUPP; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1178 | |
| 1179 | dev_size = ops->get_rxfh_indir_size(dev); |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1180 | if (dev_size == 0) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1181 | return -EOPNOTSUPP; |
| 1182 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1183 | if (copy_from_user(&user_size, |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1184 | useraddr + offsetof(struct ethtool_rxfh_indir, size), |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1185 | sizeof(user_size))) |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1186 | return -EFAULT; |
| 1187 | |
Ben Hutchings | 278bc42 | 2011-12-15 13:56:49 +0000 | [diff] [blame] | 1188 | if (user_size != 0 && user_size != dev_size) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1189 | return -EINVAL; |
| 1190 | |
| 1191 | indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER); |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1192 | if (!indir) |
| 1193 | return -ENOMEM; |
| 1194 | |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1195 | rx_rings.cmd = ETHTOOL_GRXRINGS; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1196 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1197 | if (ret) |
| 1198 | goto out; |
Ben Hutchings | 278bc42 | 2011-12-15 13:56:49 +0000 | [diff] [blame] | 1199 | |
| 1200 | if (user_size == 0) { |
| 1201 | for (i = 0; i < dev_size; i++) |
| 1202 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); |
| 1203 | } else { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1204 | ret = ethtool_copy_validate_indir(indir, |
| 1205 | useraddr + ringidx_offset, |
| 1206 | &rx_rings, |
| 1207 | dev_size); |
| 1208 | if (ret) |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1209 | goto out; |
Ben Hutchings | 7850f63 | 2011-12-15 13:55:01 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1212 | ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE); |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1213 | if (ret) |
| 1214 | goto out; |
| 1215 | |
| 1216 | /* indicate whether rxfh was set to default */ |
| 1217 | if (user_size == 0) |
| 1218 | dev->priv_flags &= ~IFF_RXFH_CONFIGURED; |
| 1219 | else |
| 1220 | dev->priv_flags |= IFF_RXFH_CONFIGURED; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 1221 | |
| 1222 | out: |
| 1223 | kfree(indir); |
| 1224 | return ret; |
| 1225 | } |
| 1226 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1227 | static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev, |
| 1228 | void __user *useraddr) |
| 1229 | { |
| 1230 | int ret; |
| 1231 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1232 | u32 user_indir_size, user_key_size; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1233 | u32 dev_indir_size = 0, dev_key_size = 0; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1234 | struct ethtool_rxfh rxfh; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1235 | u32 total_size; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1236 | u32 indir_bytes; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1237 | u32 *indir = NULL; |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1238 | u8 dev_hfunc = 0; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1239 | u8 *hkey = NULL; |
| 1240 | u8 *rss_config; |
| 1241 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1242 | if (!ops->get_rxfh) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1243 | return -EOPNOTSUPP; |
| 1244 | |
| 1245 | if (ops->get_rxfh_indir_size) |
| 1246 | dev_indir_size = ops->get_rxfh_indir_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1247 | if (ops->get_rxfh_key_size) |
| 1248 | dev_key_size = ops->get_rxfh_key_size(dev); |
| 1249 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1250 | if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1251 | return -EFAULT; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1252 | user_indir_size = rxfh.indir_size; |
| 1253 | user_key_size = rxfh.key_size; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1254 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1255 | /* Check that reserved fields are 0 for now */ |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1256 | if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] || |
| 1257 | rxfh.rsvd8[2] || rxfh.rsvd32) |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1258 | return -EINVAL; |
| 1259 | |
| 1260 | rxfh.indir_size = dev_indir_size; |
| 1261 | rxfh.key_size = dev_key_size; |
| 1262 | if (copy_to_user(useraddr, &rxfh, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1263 | return -EFAULT; |
| 1264 | |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1265 | if ((user_indir_size && (user_indir_size != dev_indir_size)) || |
| 1266 | (user_key_size && (user_key_size != dev_key_size))) |
| 1267 | return -EINVAL; |
| 1268 | |
| 1269 | indir_bytes = user_indir_size * sizeof(indir[0]); |
| 1270 | total_size = indir_bytes + user_key_size; |
| 1271 | rss_config = kzalloc(total_size, GFP_USER); |
| 1272 | if (!rss_config) |
| 1273 | return -ENOMEM; |
| 1274 | |
| 1275 | if (user_indir_size) |
| 1276 | indir = (u32 *)rss_config; |
| 1277 | |
| 1278 | if (user_key_size) |
| 1279 | hkey = rss_config + indir_bytes; |
| 1280 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1281 | ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc); |
| 1282 | if (ret) |
| 1283 | goto out; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1284 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1285 | if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc), |
| 1286 | &dev_hfunc, sizeof(rxfh.hfunc))) { |
| 1287 | ret = -EFAULT; |
| 1288 | } else if (copy_to_user(useraddr + |
| 1289 | offsetof(struct ethtool_rxfh, rss_config[0]), |
| 1290 | rss_config, total_size)) { |
| 1291 | ret = -EFAULT; |
| 1292 | } |
| 1293 | out: |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1294 | kfree(rss_config); |
| 1295 | |
| 1296 | return ret; |
| 1297 | } |
| 1298 | |
| 1299 | static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev, |
| 1300 | void __user *useraddr) |
| 1301 | { |
| 1302 | int ret; |
| 1303 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 1304 | struct ethtool_rxnfc rx_rings; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1305 | struct ethtool_rxfh rxfh; |
| 1306 | u32 dev_indir_size = 0, dev_key_size = 0, i; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1307 | u32 *indir = NULL, indir_bytes = 0; |
| 1308 | u8 *hkey = NULL; |
| 1309 | u8 *rss_config; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1310 | u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]); |
| 1311 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1312 | if (!ops->get_rxnfc || !ops->set_rxfh) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1313 | return -EOPNOTSUPP; |
| 1314 | |
| 1315 | if (ops->get_rxfh_indir_size) |
| 1316 | dev_indir_size = ops->get_rxfh_indir_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1317 | if (ops->get_rxfh_key_size) |
Dan Carpenter | d340c86 | 2015-02-20 13:54:05 +0300 | [diff] [blame] | 1318 | dev_key_size = ops->get_rxfh_key_size(dev); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1319 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1320 | if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1321 | return -EFAULT; |
| 1322 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1323 | /* Check that reserved fields are 0 for now */ |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1324 | if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] || |
| 1325 | rxfh.rsvd8[2] || rxfh.rsvd32) |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1326 | return -EINVAL; |
| 1327 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1328 | /* If either indir, hash key or function is valid, proceed further. |
| 1329 | * Must request at least one change: indir size, hash key or function. |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1330 | */ |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1331 | if ((rxfh.indir_size && |
| 1332 | rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE && |
| 1333 | rxfh.indir_size != dev_indir_size) || |
| 1334 | (rxfh.key_size && (rxfh.key_size != dev_key_size)) || |
| 1335 | (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE && |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1336 | rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE)) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1337 | return -EINVAL; |
| 1338 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1339 | if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1340 | indir_bytes = dev_indir_size * sizeof(indir[0]); |
| 1341 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1342 | rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1343 | if (!rss_config) |
| 1344 | return -ENOMEM; |
| 1345 | |
| 1346 | rx_rings.cmd = ETHTOOL_GRXRINGS; |
| 1347 | ret = ops->get_rxnfc(dev, &rx_rings, NULL); |
| 1348 | if (ret) |
| 1349 | goto out; |
| 1350 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1351 | /* rxfh.indir_size == 0 means reset the indir table to default. |
| 1352 | * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged. |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1353 | */ |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1354 | if (rxfh.indir_size && |
| 1355 | rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1356 | indir = (u32 *)rss_config; |
| 1357 | ret = ethtool_copy_validate_indir(indir, |
| 1358 | useraddr + rss_cfg_offset, |
| 1359 | &rx_rings, |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1360 | rxfh.indir_size); |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1361 | if (ret) |
| 1362 | goto out; |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1363 | } else if (rxfh.indir_size == 0) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1364 | indir = (u32 *)rss_config; |
| 1365 | for (i = 0; i < dev_indir_size; i++) |
| 1366 | indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data); |
| 1367 | } |
| 1368 | |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1369 | if (rxfh.key_size) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1370 | hkey = rss_config + indir_bytes; |
| 1371 | if (copy_from_user(hkey, |
| 1372 | useraddr + rss_cfg_offset + indir_bytes, |
Ben Hutchings | f062a38 | 2014-05-15 16:28:07 +0100 | [diff] [blame] | 1373 | rxfh.key_size)) { |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1374 | ret = -EFAULT; |
| 1375 | goto out; |
| 1376 | } |
| 1377 | } |
| 1378 | |
Eyal Perry | 892311f | 2014-12-02 18:12:10 +0200 | [diff] [blame] | 1379 | ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc); |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1380 | if (ret) |
| 1381 | goto out; |
| 1382 | |
| 1383 | /* indicate whether rxfh was set to default */ |
| 1384 | if (rxfh.indir_size == 0) |
| 1385 | dev->priv_flags &= ~IFF_RXFH_CONFIGURED; |
| 1386 | else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) |
| 1387 | dev->priv_flags |= IFF_RXFH_CONFIGURED; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 1388 | |
| 1389 | out: |
| 1390 | kfree(rss_config); |
| 1391 | return ret; |
| 1392 | } |
| 1393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | static int ethtool_get_regs(struct net_device *dev, char __user *useraddr) |
| 1395 | { |
| 1396 | struct ethtool_regs regs; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1397 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | void *regbuf; |
| 1399 | int reglen, ret; |
| 1400 | |
| 1401 | if (!ops->get_regs || !ops->get_regs_len) |
| 1402 | return -EOPNOTSUPP; |
| 1403 | |
| 1404 | if (copy_from_user(®s, useraddr, sizeof(regs))) |
| 1405 | return -EFAULT; |
| 1406 | |
| 1407 | reglen = ops->get_regs_len(dev); |
| 1408 | if (regs.len > reglen) |
| 1409 | regs.len = reglen; |
| 1410 | |
Stanislaw Gruszka | 3808d34 | 2017-02-02 13:32:10 +0100 | [diff] [blame] | 1411 | regbuf = NULL; |
| 1412 | if (reglen) { |
| 1413 | regbuf = vzalloc(reglen); |
| 1414 | if (!regbuf) |
| 1415 | return -ENOMEM; |
| 1416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
| 1418 | ops->get_regs(dev, ®s, regbuf); |
| 1419 | |
| 1420 | ret = -EFAULT; |
| 1421 | if (copy_to_user(useraddr, ®s, sizeof(regs))) |
| 1422 | goto out; |
| 1423 | useraddr += offsetof(struct ethtool_regs, data); |
Ben Hutchings | 67ae7cf | 2011-07-21 15:25:30 -0700 | [diff] [blame] | 1424 | if (regbuf && copy_to_user(useraddr, regbuf, regs.len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | goto out; |
| 1426 | ret = 0; |
| 1427 | |
| 1428 | out: |
Ben Hutchings | a77f5db | 2010-09-20 08:42:17 +0000 | [diff] [blame] | 1429 | vfree(regbuf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | return ret; |
| 1431 | } |
| 1432 | |
Ben Hutchings | d73d3a8 | 2009-10-05 10:59:58 +0000 | [diff] [blame] | 1433 | static int ethtool_reset(struct net_device *dev, char __user *useraddr) |
| 1434 | { |
| 1435 | struct ethtool_value reset; |
| 1436 | int ret; |
| 1437 | |
| 1438 | if (!dev->ethtool_ops->reset) |
| 1439 | return -EOPNOTSUPP; |
| 1440 | |
| 1441 | if (copy_from_user(&reset, useraddr, sizeof(reset))) |
| 1442 | return -EFAULT; |
| 1443 | |
| 1444 | ret = dev->ethtool_ops->reset(dev, &reset.data); |
| 1445 | if (ret) |
| 1446 | return ret; |
| 1447 | |
| 1448 | if (copy_to_user(useraddr, &reset, sizeof(reset))) |
| 1449 | return -EFAULT; |
| 1450 | return 0; |
| 1451 | } |
| 1452 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1453 | static int ethtool_get_wol(struct net_device *dev, char __user *useraddr) |
| 1454 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1455 | struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | |
| 1457 | if (!dev->ethtool_ops->get_wol) |
| 1458 | return -EOPNOTSUPP; |
| 1459 | |
| 1460 | dev->ethtool_ops->get_wol(dev, &wol); |
| 1461 | |
| 1462 | if (copy_to_user(useraddr, &wol, sizeof(wol))) |
| 1463 | return -EFAULT; |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | static int ethtool_set_wol(struct net_device *dev, char __user *useraddr) |
| 1468 | { |
| 1469 | struct ethtool_wolinfo wol; |
| 1470 | |
| 1471 | if (!dev->ethtool_ops->set_wol) |
| 1472 | return -EOPNOTSUPP; |
| 1473 | |
| 1474 | if (copy_from_user(&wol, useraddr, sizeof(wol))) |
| 1475 | return -EFAULT; |
| 1476 | |
| 1477 | return dev->ethtool_ops->set_wol(dev, &wol); |
| 1478 | } |
| 1479 | |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 1480 | static int ethtool_get_eee(struct net_device *dev, char __user *useraddr) |
| 1481 | { |
| 1482 | struct ethtool_eee edata; |
| 1483 | int rc; |
| 1484 | |
| 1485 | if (!dev->ethtool_ops->get_eee) |
| 1486 | return -EOPNOTSUPP; |
| 1487 | |
| 1488 | memset(&edata, 0, sizeof(struct ethtool_eee)); |
| 1489 | edata.cmd = ETHTOOL_GEEE; |
| 1490 | rc = dev->ethtool_ops->get_eee(dev, &edata); |
| 1491 | |
| 1492 | if (rc) |
| 1493 | return rc; |
| 1494 | |
| 1495 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 1496 | return -EFAULT; |
| 1497 | |
| 1498 | return 0; |
| 1499 | } |
| 1500 | |
| 1501 | static int ethtool_set_eee(struct net_device *dev, char __user *useraddr) |
| 1502 | { |
| 1503 | struct ethtool_eee edata; |
| 1504 | |
| 1505 | if (!dev->ethtool_ops->set_eee) |
| 1506 | return -EOPNOTSUPP; |
| 1507 | |
| 1508 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 1509 | return -EFAULT; |
| 1510 | |
| 1511 | return dev->ethtool_ops->set_eee(dev, &edata); |
| 1512 | } |
| 1513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | static int ethtool_nway_reset(struct net_device *dev) |
| 1515 | { |
| 1516 | if (!dev->ethtool_ops->nway_reset) |
| 1517 | return -EOPNOTSUPP; |
| 1518 | |
| 1519 | return dev->ethtool_ops->nway_reset(dev); |
| 1520 | } |
| 1521 | |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 1522 | static int ethtool_get_link(struct net_device *dev, char __user *useraddr) |
| 1523 | { |
| 1524 | struct ethtool_value edata = { .cmd = ETHTOOL_GLINK }; |
| 1525 | |
| 1526 | if (!dev->ethtool_ops->get_link) |
| 1527 | return -EOPNOTSUPP; |
| 1528 | |
| 1529 | edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev); |
| 1530 | |
| 1531 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 1532 | return -EFAULT; |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1536 | static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr, |
| 1537 | int (*getter)(struct net_device *, |
| 1538 | struct ethtool_eeprom *, u8 *), |
| 1539 | u32 total_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | { |
| 1541 | struct ethtool_eeprom eeprom; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1542 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 1543 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1545 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 1548 | return -EFAULT; |
| 1549 | |
| 1550 | /* Check for wrap and zero */ |
| 1551 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 1552 | return -EINVAL; |
| 1553 | |
| 1554 | /* Check for exceeding total eeprom len */ |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1555 | if (eeprom.offset + eeprom.len > total_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | return -EINVAL; |
| 1557 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1558 | data = kmalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | if (!data) |
| 1560 | return -ENOMEM; |
| 1561 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1562 | bytes_remaining = eeprom.len; |
| 1563 | while (bytes_remaining > 0) { |
| 1564 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1566 | ret = getter(dev, &eeprom, data); |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1567 | if (ret) |
| 1568 | break; |
| 1569 | if (copy_to_user(userbuf, data, eeprom.len)) { |
| 1570 | ret = -EFAULT; |
| 1571 | break; |
| 1572 | } |
| 1573 | userbuf += eeprom.len; |
| 1574 | eeprom.offset += eeprom.len; |
| 1575 | bytes_remaining -= eeprom.len; |
| 1576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Mandeep Singh Baines | c5835df | 2008-04-24 20:55:56 -0700 | [diff] [blame] | 1578 | eeprom.len = userbuf - (useraddr + sizeof(eeprom)); |
| 1579 | eeprom.offset -= eeprom.len; |
| 1580 | if (copy_to_user(useraddr, &eeprom, sizeof(eeprom))) |
| 1581 | ret = -EFAULT; |
| 1582 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | kfree(data); |
| 1584 | return ret; |
| 1585 | } |
| 1586 | |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1587 | static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr) |
| 1588 | { |
| 1589 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 1590 | |
Guenter Roeck | e0fb6fb | 2014-10-30 20:50:15 -0700 | [diff] [blame] | 1591 | if (!ops->get_eeprom || !ops->get_eeprom_len || |
| 1592 | !ops->get_eeprom_len(dev)) |
Ben Hutchings | 081d094 | 2012-04-12 00:42:12 +0000 | [diff] [blame] | 1593 | return -EOPNOTSUPP; |
| 1594 | |
| 1595 | return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom, |
| 1596 | ops->get_eeprom_len(dev)); |
| 1597 | } |
| 1598 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr) |
| 1600 | { |
| 1601 | struct ethtool_eeprom eeprom; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1602 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1603 | void __user *userbuf = useraddr + sizeof(eeprom); |
| 1604 | u32 bytes_remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1605 | u8 *data; |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1606 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
Guenter Roeck | e0fb6fb | 2014-10-30 20:50:15 -0700 | [diff] [blame] | 1608 | if (!ops->set_eeprom || !ops->get_eeprom_len || |
| 1609 | !ops->get_eeprom_len(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | return -EOPNOTSUPP; |
| 1611 | |
| 1612 | if (copy_from_user(&eeprom, useraddr, sizeof(eeprom))) |
| 1613 | return -EFAULT; |
| 1614 | |
| 1615 | /* Check for wrap and zero */ |
| 1616 | if (eeprom.offset + eeprom.len <= eeprom.offset) |
| 1617 | return -EINVAL; |
| 1618 | |
| 1619 | /* Check for exceeding total eeprom len */ |
| 1620 | if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev)) |
| 1621 | return -EINVAL; |
| 1622 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1623 | data = kmalloc(PAGE_SIZE, GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | if (!data) |
| 1625 | return -ENOMEM; |
| 1626 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1627 | bytes_remaining = eeprom.len; |
| 1628 | while (bytes_remaining > 0) { |
| 1629 | eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Mandeep Singh Baines | b131dd5 | 2008-04-15 19:24:17 -0700 | [diff] [blame] | 1631 | if (copy_from_user(data, userbuf, eeprom.len)) { |
| 1632 | ret = -EFAULT; |
| 1633 | break; |
| 1634 | } |
| 1635 | ret = ops->set_eeprom(dev, &eeprom, data); |
| 1636 | if (ret) |
| 1637 | break; |
| 1638 | userbuf += eeprom.len; |
| 1639 | eeprom.offset += eeprom.len; |
| 1640 | bytes_remaining -= eeprom.len; |
| 1641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | kfree(data); |
| 1644 | return ret; |
| 1645 | } |
| 1646 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 1647 | static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev, |
| 1648 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1650 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | |
| 1652 | if (!dev->ethtool_ops->get_coalesce) |
| 1653 | return -EOPNOTSUPP; |
| 1654 | |
| 1655 | dev->ethtool_ops->get_coalesce(dev, &coalesce); |
| 1656 | |
| 1657 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) |
| 1658 | return -EFAULT; |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 1662 | static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev, |
| 1663 | void __user *useraddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | { |
| 1665 | struct ethtool_coalesce coalesce; |
| 1666 | |
David S. Miller | fa04ae5 | 2005-06-06 15:07:19 -0700 | [diff] [blame] | 1667 | if (!dev->ethtool_ops->set_coalesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | return -EOPNOTSUPP; |
| 1669 | |
| 1670 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) |
| 1671 | return -EFAULT; |
| 1672 | |
| 1673 | return dev->ethtool_ops->set_coalesce(dev, &coalesce); |
| 1674 | } |
| 1675 | |
| 1676 | static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr) |
| 1677 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 1678 | struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | |
| 1680 | if (!dev->ethtool_ops->get_ringparam) |
| 1681 | return -EOPNOTSUPP; |
| 1682 | |
| 1683 | dev->ethtool_ops->get_ringparam(dev, &ringparam); |
| 1684 | |
| 1685 | if (copy_to_user(useraddr, &ringparam, sizeof(ringparam))) |
| 1686 | return -EFAULT; |
| 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr) |
| 1691 | { |
| 1692 | struct ethtool_ringparam ringparam; |
| 1693 | |
| 1694 | if (!dev->ethtool_ops->set_ringparam) |
| 1695 | return -EOPNOTSUPP; |
| 1696 | |
| 1697 | if (copy_from_user(&ringparam, useraddr, sizeof(ringparam))) |
| 1698 | return -EFAULT; |
| 1699 | |
| 1700 | return dev->ethtool_ops->set_ringparam(dev, &ringparam); |
| 1701 | } |
| 1702 | |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1703 | static noinline_for_stack int ethtool_get_channels(struct net_device *dev, |
| 1704 | void __user *useraddr) |
| 1705 | { |
| 1706 | struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS }; |
| 1707 | |
| 1708 | if (!dev->ethtool_ops->get_channels) |
| 1709 | return -EOPNOTSUPP; |
| 1710 | |
| 1711 | dev->ethtool_ops->get_channels(dev, &channels); |
| 1712 | |
| 1713 | if (copy_to_user(useraddr, &channels, sizeof(channels))) |
| 1714 | return -EFAULT; |
| 1715 | return 0; |
| 1716 | } |
| 1717 | |
| 1718 | static noinline_for_stack int ethtool_set_channels(struct net_device *dev, |
| 1719 | void __user *useraddr) |
| 1720 | { |
Eran Ben Elisha | 31a86d1 | 2017-01-17 19:19:17 +0200 | [diff] [blame] | 1721 | struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS }; |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1722 | u32 max_rx_in_use = 0; |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1723 | |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1724 | if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels) |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1725 | return -EOPNOTSUPP; |
| 1726 | |
| 1727 | if (copy_from_user(&channels, useraddr, sizeof(channels))) |
| 1728 | return -EFAULT; |
| 1729 | |
Keller, Jacob E | 8bf3686 | 2016-02-08 16:05:04 -0800 | [diff] [blame] | 1730 | dev->ethtool_ops->get_channels(dev, &max); |
| 1731 | |
| 1732 | /* ensure new counts are within the maximums */ |
| 1733 | if ((channels.rx_count > max.max_rx) || |
| 1734 | (channels.tx_count > max.max_tx) || |
| 1735 | (channels.combined_count > max.max_combined) || |
| 1736 | (channels.other_count > max.max_other)) |
| 1737 | return -EINVAL; |
| 1738 | |
Keller, Jacob E | d4ab428 | 2016-02-08 16:05:03 -0800 | [diff] [blame] | 1739 | /* ensure the new Rx count fits within the configured Rx flow |
| 1740 | * indirection table settings */ |
| 1741 | if (netif_is_rxfh_configured(dev) && |
| 1742 | !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) && |
| 1743 | (channels.combined_count + channels.rx_count) <= max_rx_in_use) |
| 1744 | return -EINVAL; |
| 1745 | |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 1746 | return dev->ethtool_ops->set_channels(dev, &channels); |
| 1747 | } |
| 1748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr) |
| 1750 | { |
| 1751 | struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM }; |
| 1752 | |
| 1753 | if (!dev->ethtool_ops->get_pauseparam) |
| 1754 | return -EOPNOTSUPP; |
| 1755 | |
| 1756 | dev->ethtool_ops->get_pauseparam(dev, &pauseparam); |
| 1757 | |
| 1758 | if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam))) |
| 1759 | return -EFAULT; |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr) |
| 1764 | { |
| 1765 | struct ethtool_pauseparam pauseparam; |
| 1766 | |
Jeff Garzik | e1b90c4 | 2006-07-17 12:54:40 -0400 | [diff] [blame] | 1767 | if (!dev->ethtool_ops->set_pauseparam) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | return -EOPNOTSUPP; |
| 1769 | |
| 1770 | if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam))) |
| 1771 | return -EFAULT; |
| 1772 | |
| 1773 | return dev->ethtool_ops->set_pauseparam(dev, &pauseparam); |
| 1774 | } |
| 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | static int ethtool_self_test(struct net_device *dev, char __user *useraddr) |
| 1777 | { |
| 1778 | struct ethtool_test test; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1779 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1781 | int ret, test_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1783 | if (!ops->self_test || !ops->get_sset_count) |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1784 | return -EOPNOTSUPP; |
| 1785 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1786 | test_len = ops->get_sset_count(dev, ETH_SS_TEST); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1787 | if (test_len < 0) |
| 1788 | return test_len; |
| 1789 | WARN_ON(test_len == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | |
| 1791 | if (copy_from_user(&test, useraddr, sizeof(test))) |
| 1792 | return -EFAULT; |
| 1793 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1794 | test.len = test_len; |
| 1795 | data = kmalloc(test_len * sizeof(u64), GFP_USER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | if (!data) |
| 1797 | return -ENOMEM; |
| 1798 | |
| 1799 | ops->self_test(dev, &test, data); |
| 1800 | |
| 1801 | ret = -EFAULT; |
| 1802 | if (copy_to_user(useraddr, &test, sizeof(test))) |
| 1803 | goto out; |
| 1804 | useraddr += sizeof(test); |
| 1805 | if (copy_to_user(useraddr, data, test.len * sizeof(u64))) |
| 1806 | goto out; |
| 1807 | ret = 0; |
| 1808 | |
| 1809 | out: |
| 1810 | kfree(data); |
| 1811 | return ret; |
| 1812 | } |
| 1813 | |
| 1814 | static int ethtool_get_strings(struct net_device *dev, void __user *useraddr) |
| 1815 | { |
| 1816 | struct ethtool_gstrings gstrings; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | u8 *data; |
| 1818 | int ret; |
| 1819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | if (copy_from_user(&gstrings, useraddr, sizeof(gstrings))) |
| 1821 | return -EFAULT; |
| 1822 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1823 | ret = __ethtool_get_sset_count(dev, gstrings.string_set); |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1824 | if (ret < 0) |
| 1825 | return ret; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1826 | if (ret > S32_MAX / ETH_GSTRING_LEN) |
| 1827 | return -ENOMEM; |
| 1828 | WARN_ON_ONCE(!ret); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1829 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1830 | gstrings.len = ret; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1831 | data = vzalloc(gstrings.len * ETH_GSTRING_LEN); |
| 1832 | if (gstrings.len && !data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | return -ENOMEM; |
| 1834 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1835 | __ethtool_get_strings(dev, gstrings.string_set, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | |
| 1837 | ret = -EFAULT; |
| 1838 | if (copy_to_user(useraddr, &gstrings, sizeof(gstrings))) |
| 1839 | goto out; |
| 1840 | useraddr += sizeof(gstrings); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1841 | if (gstrings.len && |
| 1842 | copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | goto out; |
| 1844 | ret = 0; |
| 1845 | |
Michał Mirosław | 340ae16 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 1846 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1847 | vfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | return ret; |
| 1849 | } |
| 1850 | |
| 1851 | static int ethtool_phys_id(struct net_device *dev, void __user *useraddr) |
| 1852 | { |
| 1853 | struct ethtool_value id; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1854 | static bool busy; |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1855 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1856 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1858 | if (!ops->set_phys_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | return -EOPNOTSUPP; |
| 1860 | |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1861 | if (busy) |
| 1862 | return -EBUSY; |
| 1863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | if (copy_from_user(&id, useraddr, sizeof(id))) |
| 1865 | return -EFAULT; |
| 1866 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1867 | rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE); |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1868 | if (rc < 0) |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1869 | return rc; |
| 1870 | |
| 1871 | /* Drop the RTNL lock while waiting, but prevent reentry or |
| 1872 | * removal of the device. |
| 1873 | */ |
| 1874 | busy = true; |
| 1875 | dev_hold(dev); |
| 1876 | rtnl_unlock(); |
| 1877 | |
| 1878 | if (rc == 0) { |
| 1879 | /* Driver will handle this itself */ |
| 1880 | schedule_timeout_interruptible( |
Allan, Bruce W | 143780c | 2011-04-11 13:01:59 +0000 | [diff] [blame] | 1881 | id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1882 | } else { |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1883 | /* Driver expects to be called at twice the frequency in rc */ |
| 1884 | int n = rc * 2, i, interval = HZ / n; |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1885 | |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1886 | /* Count down seconds */ |
| 1887 | do { |
| 1888 | /* Count down iterations per second */ |
| 1889 | i = n; |
| 1890 | do { |
| 1891 | rtnl_lock(); |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1892 | rc = ops->set_phys_id(dev, |
Allan, Bruce W | fce5592 | 2011-04-13 13:09:10 +0000 | [diff] [blame] | 1893 | (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON); |
| 1894 | rtnl_unlock(); |
| 1895 | if (rc) |
| 1896 | break; |
| 1897 | schedule_timeout_interruptible(interval); |
| 1898 | } while (!signal_pending(current) && --i != 0); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1899 | } while (!signal_pending(current) && |
| 1900 | (id.data == 0 || --id.data != 0)); |
| 1901 | } |
| 1902 | |
| 1903 | rtnl_lock(); |
| 1904 | dev_put(dev); |
| 1905 | busy = false; |
| 1906 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 1907 | (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE); |
Ben Hutchings | 68f512f | 2011-04-02 00:35:15 +0100 | [diff] [blame] | 1908 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | static int ethtool_get_stats(struct net_device *dev, void __user *useraddr) |
| 1912 | { |
| 1913 | struct ethtool_stats stats; |
Stephen Hemminger | 76fd859 | 2006-09-08 11:16:13 -0700 | [diff] [blame] | 1914 | const struct ethtool_ops *ops = dev->ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | u64 *data; |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1916 | int ret, n_stats; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1918 | if (!ops->get_ethtool_stats || !ops->get_sset_count) |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1919 | return -EOPNOTSUPP; |
| 1920 | |
Ben Hutchings | a9828ec | 2009-10-01 11:33:03 +0000 | [diff] [blame] | 1921 | n_stats = ops->get_sset_count(dev, ETH_SS_STATS); |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1922 | if (n_stats < 0) |
| 1923 | return n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1924 | if (n_stats > S32_MAX / sizeof(u64)) |
| 1925 | return -ENOMEM; |
| 1926 | WARN_ON_ONCE(!n_stats); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
| 1928 | return -EFAULT; |
| 1929 | |
Jeff Garzik | ff03d49 | 2007-08-15 16:01:08 -0700 | [diff] [blame] | 1930 | stats.n_stats = n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1931 | data = vzalloc(n_stats * sizeof(u64)); |
| 1932 | if (n_stats && !data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | return -ENOMEM; |
| 1934 | |
| 1935 | ops->get_ethtool_stats(dev, &stats, data); |
| 1936 | |
| 1937 | ret = -EFAULT; |
| 1938 | if (copy_to_user(useraddr, &stats, sizeof(stats))) |
| 1939 | goto out; |
| 1940 | useraddr += sizeof(stats); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1941 | if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | goto out; |
| 1943 | ret = 0; |
| 1944 | |
| 1945 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1946 | vfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | return ret; |
| 1948 | } |
| 1949 | |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1950 | static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr) |
| 1951 | { |
| 1952 | struct ethtool_stats stats; |
| 1953 | struct phy_device *phydev = dev->phydev; |
| 1954 | u64 *data; |
| 1955 | int ret, n_stats; |
| 1956 | |
| 1957 | if (!phydev) |
| 1958 | return -EOPNOTSUPP; |
| 1959 | |
| 1960 | n_stats = phy_get_sset_count(phydev); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1961 | if (n_stats < 0) |
| 1962 | return n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1963 | if (n_stats > S32_MAX / sizeof(u64)) |
| 1964 | return -ENOMEM; |
| 1965 | WARN_ON_ONCE(!n_stats); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1966 | |
| 1967 | if (copy_from_user(&stats, useraddr, sizeof(stats))) |
| 1968 | return -EFAULT; |
| 1969 | |
| 1970 | stats.n_stats = n_stats; |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1971 | data = vzalloc(n_stats * sizeof(u64)); |
| 1972 | if (n_stats && !data) |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1973 | return -ENOMEM; |
| 1974 | |
| 1975 | mutex_lock(&phydev->lock); |
| 1976 | phydev->drv->get_stats(phydev, &stats, data); |
| 1977 | mutex_unlock(&phydev->lock); |
| 1978 | |
| 1979 | ret = -EFAULT; |
| 1980 | if (copy_to_user(useraddr, &stats, sizeof(stats))) |
| 1981 | goto out; |
| 1982 | useraddr += sizeof(stats); |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1983 | if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64))) |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1984 | goto out; |
| 1985 | ret = 0; |
| 1986 | |
| 1987 | out: |
Alexei Starovoitov | 4d1ceea | 2017-01-30 18:25:18 -0800 | [diff] [blame] | 1988 | vfree(data); |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 1989 | return ret; |
| 1990 | } |
| 1991 | |
viro@ftp.linux.org.uk | 0bf0519d | 2005-09-05 03:26:18 +0100 | [diff] [blame] | 1992 | static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1993 | { |
| 1994 | struct ethtool_perm_addr epaddr; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1995 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 1996 | if (copy_from_user(&epaddr, useraddr, sizeof(epaddr))) |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 1997 | return -EFAULT; |
| 1998 | |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 1999 | if (epaddr.size < dev->addr_len) |
| 2000 | return -ETOOSMALL; |
| 2001 | epaddr.size = dev->addr_len; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2002 | |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2003 | if (copy_to_user(useraddr, &epaddr, sizeof(epaddr))) |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 2004 | return -EFAULT; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2005 | useraddr += sizeof(epaddr); |
Matthew Wilcox | 313674a | 2007-07-31 14:00:29 -0700 | [diff] [blame] | 2006 | if (copy_to_user(useraddr, dev->perm_addr, epaddr.size)) |
| 2007 | return -EFAULT; |
| 2008 | return 0; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2009 | } |
| 2010 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2011 | static int ethtool_get_value(struct net_device *dev, char __user *useraddr, |
| 2012 | u32 cmd, u32 (*actor)(struct net_device *)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2013 | { |
Roland Dreier | 8e55742 | 2010-02-11 12:14:23 -0800 | [diff] [blame] | 2014 | struct ethtool_value edata = { .cmd = cmd }; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2015 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2016 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2017 | return -EOPNOTSUPP; |
| 2018 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2019 | edata.data = actor(dev); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2020 | |
| 2021 | if (copy_to_user(useraddr, &edata, sizeof(edata))) |
| 2022 | return -EFAULT; |
| 2023 | return 0; |
| 2024 | } |
| 2025 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2026 | static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr, |
| 2027 | void (*actor)(struct net_device *, u32)) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2028 | { |
| 2029 | struct ethtool_value edata; |
| 2030 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2031 | if (!actor) |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2032 | return -EOPNOTSUPP; |
| 2033 | |
| 2034 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 2035 | return -EFAULT; |
| 2036 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2037 | actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2038 | return 0; |
| 2039 | } |
| 2040 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2041 | static int ethtool_set_value(struct net_device *dev, char __user *useraddr, |
| 2042 | int (*actor)(struct net_device *, u32)) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2043 | { |
| 2044 | struct ethtool_value edata; |
| 2045 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2046 | if (!actor) |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2047 | return -EOPNOTSUPP; |
| 2048 | |
| 2049 | if (copy_from_user(&edata, useraddr, sizeof(edata))) |
| 2050 | return -EFAULT; |
| 2051 | |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2052 | return actor(dev, edata.data); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2053 | } |
| 2054 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2055 | static noinline_for_stack int ethtool_flash_device(struct net_device *dev, |
| 2056 | char __user *useraddr) |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2057 | { |
| 2058 | struct ethtool_flash efl; |
| 2059 | |
| 2060 | if (copy_from_user(&efl, useraddr, sizeof(efl))) |
| 2061 | return -EFAULT; |
| 2062 | |
| 2063 | if (!dev->ethtool_ops->flash_device) |
| 2064 | return -EOPNOTSUPP; |
| 2065 | |
Ben Hutchings | 786f528 | 2012-02-01 09:32:25 +0000 | [diff] [blame] | 2066 | efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0; |
| 2067 | |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2068 | return dev->ethtool_ops->flash_device(dev, &efl); |
| 2069 | } |
| 2070 | |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2071 | static int ethtool_set_dump(struct net_device *dev, |
| 2072 | void __user *useraddr) |
| 2073 | { |
| 2074 | struct ethtool_dump dump; |
| 2075 | |
| 2076 | if (!dev->ethtool_ops->set_dump) |
| 2077 | return -EOPNOTSUPP; |
| 2078 | |
| 2079 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2080 | return -EFAULT; |
| 2081 | |
| 2082 | return dev->ethtool_ops->set_dump(dev, &dump); |
| 2083 | } |
| 2084 | |
| 2085 | static int ethtool_get_dump_flag(struct net_device *dev, |
| 2086 | void __user *useraddr) |
| 2087 | { |
| 2088 | int ret; |
| 2089 | struct ethtool_dump dump; |
| 2090 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2091 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2092 | if (!ops->get_dump_flag) |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2093 | return -EOPNOTSUPP; |
| 2094 | |
| 2095 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2096 | return -EFAULT; |
| 2097 | |
| 2098 | ret = ops->get_dump_flag(dev, &dump); |
| 2099 | if (ret) |
| 2100 | return ret; |
| 2101 | |
| 2102 | if (copy_to_user(useraddr, &dump, sizeof(dump))) |
| 2103 | return -EFAULT; |
| 2104 | return 0; |
| 2105 | } |
| 2106 | |
| 2107 | static int ethtool_get_dump_data(struct net_device *dev, |
| 2108 | void __user *useraddr) |
| 2109 | { |
| 2110 | int ret; |
| 2111 | __u32 len; |
| 2112 | struct ethtool_dump dump, tmp; |
| 2113 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2114 | void *data = NULL; |
| 2115 | |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2116 | if (!ops->get_dump_data || !ops->get_dump_flag) |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2117 | return -EOPNOTSUPP; |
| 2118 | |
| 2119 | if (copy_from_user(&dump, useraddr, sizeof(dump))) |
| 2120 | return -EFAULT; |
| 2121 | |
| 2122 | memset(&tmp, 0, sizeof(tmp)); |
| 2123 | tmp.cmd = ETHTOOL_GET_DUMP_FLAG; |
| 2124 | ret = ops->get_dump_flag(dev, &tmp); |
| 2125 | if (ret) |
| 2126 | return ret; |
| 2127 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2128 | len = min(tmp.len, dump.len); |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2129 | if (!len) |
| 2130 | return -EFAULT; |
| 2131 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2132 | /* Don't ever let the driver think there's more space available |
| 2133 | * than it requested with .get_dump_flag(). |
| 2134 | */ |
| 2135 | dump.len = len; |
| 2136 | |
| 2137 | /* Always allocate enough space to hold the whole thing so that the |
| 2138 | * driver does not need to check the length and bother with partial |
| 2139 | * dumping. |
| 2140 | */ |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2141 | data = vzalloc(tmp.len); |
| 2142 | if (!data) |
| 2143 | return -ENOMEM; |
| 2144 | ret = ops->get_dump_data(dev, &dump, data); |
| 2145 | if (ret) |
| 2146 | goto out; |
| 2147 | |
Michal Schmidt | c590b5e | 2013-07-01 17:23:30 +0200 | [diff] [blame] | 2148 | /* There are two sane possibilities: |
| 2149 | * 1. The driver's .get_dump_data() does not touch dump.len. |
| 2150 | * 2. Or it may set dump.len to how much it really writes, which |
| 2151 | * should be tmp.len (or len if it can do a partial dump). |
| 2152 | * In any case respond to userspace with the actual length of data |
| 2153 | * it's receiving. |
| 2154 | */ |
| 2155 | WARN_ON(dump.len != len && dump.len != tmp.len); |
| 2156 | dump.len = len; |
| 2157 | |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2158 | if (copy_to_user(useraddr, &dump, sizeof(dump))) { |
| 2159 | ret = -EFAULT; |
| 2160 | goto out; |
| 2161 | } |
| 2162 | useraddr += offsetof(struct ethtool_dump, data); |
| 2163 | if (copy_to_user(useraddr, data, len)) |
| 2164 | ret = -EFAULT; |
| 2165 | out: |
| 2166 | vfree(data); |
| 2167 | return ret; |
| 2168 | } |
| 2169 | |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2170 | static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr) |
| 2171 | { |
| 2172 | int err = 0; |
| 2173 | struct ethtool_ts_info info; |
| 2174 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2175 | struct phy_device *phydev = dev->phydev; |
| 2176 | |
| 2177 | memset(&info, 0, sizeof(info)); |
| 2178 | info.cmd = ETHTOOL_GET_TS_INFO; |
| 2179 | |
| 2180 | if (phydev && phydev->drv && phydev->drv->ts_info) { |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2181 | err = phydev->drv->ts_info(phydev, &info); |
Jiri Pirko | c03a14e | 2013-01-07 09:02:08 +0000 | [diff] [blame] | 2182 | } else if (ops->get_ts_info) { |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2183 | err = ops->get_ts_info(dev, &info); |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2184 | } else { |
| 2185 | info.so_timestamping = |
| 2186 | SOF_TIMESTAMPING_RX_SOFTWARE | |
| 2187 | SOF_TIMESTAMPING_SOFTWARE; |
| 2188 | info.phc_index = -1; |
| 2189 | } |
| 2190 | |
| 2191 | if (err) |
| 2192 | return err; |
| 2193 | |
| 2194 | if (copy_to_user(useraddr, &info, sizeof(info))) |
| 2195 | err = -EFAULT; |
| 2196 | |
| 2197 | return err; |
| 2198 | } |
| 2199 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2200 | static int __ethtool_get_module_info(struct net_device *dev, |
| 2201 | struct ethtool_modinfo *modinfo) |
| 2202 | { |
| 2203 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2204 | struct phy_device *phydev = dev->phydev; |
| 2205 | |
| 2206 | if (phydev && phydev->drv && phydev->drv->module_info) |
| 2207 | return phydev->drv->module_info(phydev, modinfo); |
| 2208 | |
| 2209 | if (ops->get_module_info) |
| 2210 | return ops->get_module_info(dev, modinfo); |
| 2211 | |
| 2212 | return -EOPNOTSUPP; |
| 2213 | } |
| 2214 | |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2215 | static int ethtool_get_module_info(struct net_device *dev, |
| 2216 | void __user *useraddr) |
| 2217 | { |
| 2218 | int ret; |
| 2219 | struct ethtool_modinfo modinfo; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2220 | |
| 2221 | if (copy_from_user(&modinfo, useraddr, sizeof(modinfo))) |
| 2222 | return -EFAULT; |
| 2223 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2224 | ret = __ethtool_get_module_info(dev, &modinfo); |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2225 | if (ret) |
| 2226 | return ret; |
| 2227 | |
| 2228 | if (copy_to_user(useraddr, &modinfo, sizeof(modinfo))) |
| 2229 | return -EFAULT; |
| 2230 | |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2234 | static int __ethtool_get_module_eeprom(struct net_device *dev, |
| 2235 | struct ethtool_eeprom *ee, u8 *data) |
| 2236 | { |
| 2237 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2238 | struct phy_device *phydev = dev->phydev; |
| 2239 | |
| 2240 | if (phydev && phydev->drv && phydev->drv->module_eeprom) |
| 2241 | return phydev->drv->module_eeprom(phydev, ee, data); |
| 2242 | |
| 2243 | if (ops->get_module_eeprom) |
| 2244 | return ops->get_module_eeprom(dev, ee, data); |
| 2245 | |
| 2246 | return -EOPNOTSUPP; |
| 2247 | } |
| 2248 | |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2249 | static int ethtool_get_module_eeprom(struct net_device *dev, |
| 2250 | void __user *useraddr) |
| 2251 | { |
| 2252 | int ret; |
| 2253 | struct ethtool_modinfo modinfo; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2254 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2255 | ret = __ethtool_get_module_info(dev, &modinfo); |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2256 | if (ret) |
| 2257 | return ret; |
| 2258 | |
Ed Swierk | 2f43836 | 2015-01-02 17:27:56 -0800 | [diff] [blame] | 2259 | return ethtool_get_any_eeprom(dev, useraddr, |
| 2260 | __ethtool_get_module_eeprom, |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2261 | modinfo.eeprom_len); |
| 2262 | } |
| 2263 | |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2264 | static int ethtool_tunable_valid(const struct ethtool_tunable *tuna) |
| 2265 | { |
| 2266 | switch (tuna->id) { |
| 2267 | case ETHTOOL_RX_COPYBREAK: |
Eric Dumazet | 1255a50 | 2014-10-05 12:35:21 +0300 | [diff] [blame] | 2268 | case ETHTOOL_TX_COPYBREAK: |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2269 | if (tuna->len != sizeof(u32) || |
| 2270 | tuna->type_id != ETHTOOL_TUNABLE_U32) |
| 2271 | return -EINVAL; |
| 2272 | break; |
| 2273 | default: |
| 2274 | return -EINVAL; |
| 2275 | } |
| 2276 | |
| 2277 | return 0; |
| 2278 | } |
| 2279 | |
| 2280 | static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr) |
| 2281 | { |
| 2282 | int ret; |
| 2283 | struct ethtool_tunable tuna; |
| 2284 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2285 | void *data; |
| 2286 | |
| 2287 | if (!ops->get_tunable) |
| 2288 | return -EOPNOTSUPP; |
| 2289 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2290 | return -EFAULT; |
| 2291 | ret = ethtool_tunable_valid(&tuna); |
| 2292 | if (ret) |
| 2293 | return ret; |
| 2294 | data = kmalloc(tuna.len, GFP_USER); |
| 2295 | if (!data) |
| 2296 | return -ENOMEM; |
| 2297 | ret = ops->get_tunable(dev, &tuna, data); |
| 2298 | if (ret) |
| 2299 | goto out; |
| 2300 | useraddr += sizeof(tuna); |
| 2301 | ret = -EFAULT; |
| 2302 | if (copy_to_user(useraddr, data, tuna.len)) |
| 2303 | goto out; |
| 2304 | ret = 0; |
| 2305 | |
| 2306 | out: |
| 2307 | kfree(data); |
| 2308 | return ret; |
| 2309 | } |
| 2310 | |
| 2311 | static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr) |
| 2312 | { |
| 2313 | int ret; |
| 2314 | struct ethtool_tunable tuna; |
| 2315 | const struct ethtool_ops *ops = dev->ethtool_ops; |
| 2316 | void *data; |
| 2317 | |
| 2318 | if (!ops->set_tunable) |
| 2319 | return -EOPNOTSUPP; |
| 2320 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2321 | return -EFAULT; |
| 2322 | ret = ethtool_tunable_valid(&tuna); |
| 2323 | if (ret) |
| 2324 | return ret; |
| 2325 | data = kmalloc(tuna.len, GFP_USER); |
| 2326 | if (!data) |
| 2327 | return -ENOMEM; |
| 2328 | useraddr += sizeof(tuna); |
| 2329 | ret = -EFAULT; |
| 2330 | if (copy_from_user(data, useraddr, tuna.len)) |
| 2331 | goto out; |
| 2332 | ret = ops->set_tunable(dev, &tuna, data); |
| 2333 | |
| 2334 | out: |
| 2335 | kfree(data); |
| 2336 | return ret; |
| 2337 | } |
| 2338 | |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2339 | static int ethtool_get_per_queue_coalesce(struct net_device *dev, |
| 2340 | void __user *useraddr, |
| 2341 | struct ethtool_per_queue_op *per_queue_opt) |
| 2342 | { |
| 2343 | u32 bit; |
| 2344 | int ret; |
| 2345 | DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); |
| 2346 | |
| 2347 | if (!dev->ethtool_ops->get_per_queue_coalesce) |
| 2348 | return -EOPNOTSUPP; |
| 2349 | |
| 2350 | useraddr += sizeof(*per_queue_opt); |
| 2351 | |
| 2352 | bitmap_from_u32array(queue_mask, |
| 2353 | MAX_NUM_QUEUE, |
| 2354 | per_queue_opt->queue_mask, |
| 2355 | DIV_ROUND_UP(MAX_NUM_QUEUE, 32)); |
| 2356 | |
| 2357 | for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { |
| 2358 | struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE }; |
| 2359 | |
| 2360 | ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce); |
| 2361 | if (ret != 0) |
| 2362 | return ret; |
| 2363 | if (copy_to_user(useraddr, &coalesce, sizeof(coalesce))) |
| 2364 | return -EFAULT; |
| 2365 | useraddr += sizeof(coalesce); |
| 2366 | } |
| 2367 | |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2371 | static int ethtool_set_per_queue_coalesce(struct net_device *dev, |
| 2372 | void __user *useraddr, |
| 2373 | struct ethtool_per_queue_op *per_queue_opt) |
| 2374 | { |
| 2375 | u32 bit; |
| 2376 | int i, ret = 0; |
| 2377 | int n_queue; |
| 2378 | struct ethtool_coalesce *backup = NULL, *tmp = NULL; |
| 2379 | DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE); |
| 2380 | |
| 2381 | if ((!dev->ethtool_ops->set_per_queue_coalesce) || |
| 2382 | (!dev->ethtool_ops->get_per_queue_coalesce)) |
| 2383 | return -EOPNOTSUPP; |
| 2384 | |
| 2385 | useraddr += sizeof(*per_queue_opt); |
| 2386 | |
| 2387 | bitmap_from_u32array(queue_mask, |
| 2388 | MAX_NUM_QUEUE, |
| 2389 | per_queue_opt->queue_mask, |
| 2390 | DIV_ROUND_UP(MAX_NUM_QUEUE, 32)); |
| 2391 | n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE); |
| 2392 | tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL); |
| 2393 | if (!backup) |
| 2394 | return -ENOMEM; |
| 2395 | |
| 2396 | for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) { |
| 2397 | struct ethtool_coalesce coalesce; |
| 2398 | |
| 2399 | ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp); |
| 2400 | if (ret != 0) |
| 2401 | goto roll_back; |
| 2402 | |
| 2403 | tmp++; |
| 2404 | |
| 2405 | if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) { |
| 2406 | ret = -EFAULT; |
| 2407 | goto roll_back; |
| 2408 | } |
| 2409 | |
| 2410 | ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce); |
| 2411 | if (ret != 0) |
| 2412 | goto roll_back; |
| 2413 | |
| 2414 | useraddr += sizeof(coalesce); |
| 2415 | } |
| 2416 | |
| 2417 | roll_back: |
| 2418 | if (ret != 0) { |
| 2419 | tmp = backup; |
| 2420 | for_each_set_bit(i, queue_mask, bit) { |
| 2421 | dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp); |
| 2422 | tmp++; |
| 2423 | } |
| 2424 | } |
| 2425 | kfree(backup); |
| 2426 | |
| 2427 | return ret; |
| 2428 | } |
| 2429 | |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2430 | static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr) |
| 2431 | { |
| 2432 | struct ethtool_per_queue_op per_queue_opt; |
| 2433 | |
| 2434 | if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt))) |
| 2435 | return -EFAULT; |
| 2436 | |
| 2437 | switch (per_queue_opt.sub_command) { |
Kan Liang | 421797b | 2016-02-19 09:24:02 -0500 | [diff] [blame] | 2438 | case ETHTOOL_GCOALESCE: |
| 2439 | return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt); |
Kan Liang | f38d138 | 2016-02-19 09:24:03 -0500 | [diff] [blame] | 2440 | case ETHTOOL_SCOALESCE: |
| 2441 | return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt); |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2442 | default: |
| 2443 | return -EOPNOTSUPP; |
| 2444 | }; |
| 2445 | } |
| 2446 | |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2447 | static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna) |
| 2448 | { |
| 2449 | switch (tuna->id) { |
Raju Lakkaraju | 65feddd | 2016-11-17 13:07:23 +0100 | [diff] [blame] | 2450 | case ETHTOOL_PHY_DOWNSHIFT: |
| 2451 | if (tuna->len != sizeof(u8) || |
| 2452 | tuna->type_id != ETHTOOL_TUNABLE_U8) |
| 2453 | return -EINVAL; |
| 2454 | break; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2455 | default: |
| 2456 | return -EINVAL; |
| 2457 | } |
| 2458 | |
| 2459 | return 0; |
| 2460 | } |
| 2461 | |
| 2462 | static int get_phy_tunable(struct net_device *dev, void __user *useraddr) |
| 2463 | { |
| 2464 | int ret; |
| 2465 | struct ethtool_tunable tuna; |
| 2466 | struct phy_device *phydev = dev->phydev; |
| 2467 | void *data; |
| 2468 | |
| 2469 | if (!(phydev && phydev->drv && phydev->drv->get_tunable)) |
| 2470 | return -EOPNOTSUPP; |
| 2471 | |
| 2472 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2473 | return -EFAULT; |
| 2474 | ret = ethtool_phy_tunable_valid(&tuna); |
| 2475 | if (ret) |
| 2476 | return ret; |
| 2477 | data = kmalloc(tuna.len, GFP_USER); |
| 2478 | if (!data) |
| 2479 | return -ENOMEM; |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2480 | mutex_lock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2481 | ret = phydev->drv->get_tunable(phydev, &tuna, data); |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2482 | mutex_unlock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2483 | if (ret) |
| 2484 | goto out; |
| 2485 | useraddr += sizeof(tuna); |
| 2486 | ret = -EFAULT; |
| 2487 | if (copy_to_user(useraddr, data, tuna.len)) |
| 2488 | goto out; |
| 2489 | ret = 0; |
| 2490 | |
| 2491 | out: |
| 2492 | kfree(data); |
| 2493 | return ret; |
| 2494 | } |
| 2495 | |
| 2496 | static int set_phy_tunable(struct net_device *dev, void __user *useraddr) |
| 2497 | { |
| 2498 | int ret; |
| 2499 | struct ethtool_tunable tuna; |
| 2500 | struct phy_device *phydev = dev->phydev; |
| 2501 | void *data; |
| 2502 | |
| 2503 | if (!(phydev && phydev->drv && phydev->drv->set_tunable)) |
| 2504 | return -EOPNOTSUPP; |
| 2505 | if (copy_from_user(&tuna, useraddr, sizeof(tuna))) |
| 2506 | return -EFAULT; |
| 2507 | ret = ethtool_phy_tunable_valid(&tuna); |
| 2508 | if (ret) |
| 2509 | return ret; |
| 2510 | data = kmalloc(tuna.len, GFP_USER); |
| 2511 | if (!data) |
| 2512 | return -ENOMEM; |
| 2513 | useraddr += sizeof(tuna); |
| 2514 | ret = -EFAULT; |
| 2515 | if (copy_from_user(data, useraddr, tuna.len)) |
| 2516 | goto out; |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2517 | mutex_lock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2518 | ret = phydev->drv->set_tunable(phydev, &tuna, data); |
Florian Fainelli | 4b65246 | 2016-11-22 13:55:31 -0800 | [diff] [blame] | 2519 | mutex_unlock(&phydev->lock); |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2520 | |
| 2521 | out: |
| 2522 | kfree(data); |
| 2523 | return ret; |
| 2524 | } |
| 2525 | |
Yan Burman | 600fed5 | 2013-06-03 02:03:34 +0000 | [diff] [blame] | 2526 | /* The main entry point in this file. Called from net/core/dev_ioctl.c */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2528 | int dev_ethtool(struct net *net, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 2530 | struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | void __user *useraddr = ifr->ifr_data; |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2532 | u32 ethcmd, sub_cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2533 | int rc; |
Bjørn Mork | b29d314 | 2013-05-01 23:06:42 +0000 | [diff] [blame] | 2534 | netdev_features_t old_features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | if (!dev || !netif_device_present(dev)) |
| 2537 | return -ENODEV; |
| 2538 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2539 | if (copy_from_user(ðcmd, useraddr, sizeof(ethcmd))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | return -EFAULT; |
| 2541 | |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2542 | if (ethcmd == ETHTOOL_PERQUEUE) { |
| 2543 | if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd))) |
| 2544 | return -EFAULT; |
| 2545 | } else { |
| 2546 | sub_cmd = ethcmd; |
| 2547 | } |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2548 | /* Allow some commands to be done by anyone */ |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2549 | switch (sub_cmd) { |
stephen hemminger | 0fdc100 | 2010-08-23 10:24:18 +0000 | [diff] [blame] | 2550 | case ETHTOOL_GSET: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2551 | case ETHTOOL_GDRVINFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2552 | case ETHTOOL_GMSGLVL: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2553 | case ETHTOOL_GLINK: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2554 | case ETHTOOL_GCOALESCE: |
| 2555 | case ETHTOOL_GRINGPARAM: |
| 2556 | case ETHTOOL_GPAUSEPARAM: |
| 2557 | case ETHTOOL_GRXCSUM: |
| 2558 | case ETHTOOL_GTXCSUM: |
| 2559 | case ETHTOOL_GSG: |
Michał Mirosław | f80400a | 2012-01-22 00:20:40 +0000 | [diff] [blame] | 2560 | case ETHTOOL_GSSET_INFO: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2561 | case ETHTOOL_GSTRINGS: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2562 | case ETHTOOL_GSTATS: |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2563 | case ETHTOOL_GPHYSTATS: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2564 | case ETHTOOL_GTSO: |
| 2565 | case ETHTOOL_GPERMADDR: |
| 2566 | case ETHTOOL_GUFO: |
| 2567 | case ETHTOOL_GGSO: |
stephen hemminger | 1cab819 | 2010-02-11 13:48:29 +0000 | [diff] [blame] | 2568 | case ETHTOOL_GGRO: |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2569 | case ETHTOOL_GFLAGS: |
| 2570 | case ETHTOOL_GPFLAGS: |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2571 | case ETHTOOL_GRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2572 | case ETHTOOL_GRXRINGS: |
| 2573 | case ETHTOOL_GRXCLSRLCNT: |
| 2574 | case ETHTOOL_GRXCLSRULE: |
| 2575 | case ETHTOOL_GRXCLSRLALL: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2576 | case ETHTOOL_GRXFHINDIR: |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 2577 | case ETHTOOL_GRSSH: |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2578 | case ETHTOOL_GFEATURES: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2579 | case ETHTOOL_GCHANNELS: |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2580 | case ETHTOOL_GET_TS_INFO: |
Ben Hutchings | 2da45db | 2012-06-12 13:05:41 +0000 | [diff] [blame] | 2581 | case ETHTOOL_GEEE: |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2582 | case ETHTOOL_GTUNABLE: |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2583 | case ETHTOOL_PHY_GTUNABLE: |
Miroslav Lichvar | 8006f6b | 2016-11-24 10:55:06 +0100 | [diff] [blame] | 2584 | case ETHTOOL_GLINKSETTINGS: |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2585 | break; |
| 2586 | default: |
Eric W. Biederman | 5e1fccc | 2012-11-16 03:03:04 +0000 | [diff] [blame] | 2587 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Stephen Hemminger | 75f3123 | 2006-09-28 15:13:37 -0700 | [diff] [blame] | 2588 | return -EPERM; |
| 2589 | } |
| 2590 | |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2591 | if (dev->ethtool_ops->begin) { |
| 2592 | rc = dev->ethtool_ops->begin(dev); |
| 2593 | if (rc < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | return rc; |
chavey | 97f8aef | 2010-04-07 21:54:42 -0700 | [diff] [blame] | 2595 | } |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 2596 | old_features = dev->features; |
| 2597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | switch (ethcmd) { |
| 2599 | case ETHTOOL_GSET: |
| 2600 | rc = ethtool_get_settings(dev, useraddr); |
| 2601 | break; |
| 2602 | case ETHTOOL_SSET: |
| 2603 | rc = ethtool_set_settings(dev, useraddr); |
| 2604 | break; |
| 2605 | case ETHTOOL_GDRVINFO: |
| 2606 | rc = ethtool_get_drvinfo(dev, useraddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2607 | break; |
| 2608 | case ETHTOOL_GREGS: |
| 2609 | rc = ethtool_get_regs(dev, useraddr); |
| 2610 | break; |
| 2611 | case ETHTOOL_GWOL: |
| 2612 | rc = ethtool_get_wol(dev, useraddr); |
| 2613 | break; |
| 2614 | case ETHTOOL_SWOL: |
| 2615 | rc = ethtool_set_wol(dev, useraddr); |
| 2616 | break; |
| 2617 | case ETHTOOL_GMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2618 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 2619 | dev->ethtool_ops->get_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | break; |
| 2621 | case ETHTOOL_SMSGLVL: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2622 | rc = ethtool_set_value_void(dev, useraddr, |
| 2623 | dev->ethtool_ops->set_msglevel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | break; |
Yuval Mintz | 80f12ec | 2012-06-06 17:13:06 +0000 | [diff] [blame] | 2625 | case ETHTOOL_GEEE: |
| 2626 | rc = ethtool_get_eee(dev, useraddr); |
| 2627 | break; |
| 2628 | case ETHTOOL_SEEE: |
| 2629 | rc = ethtool_set_eee(dev, useraddr); |
| 2630 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | case ETHTOOL_NWAY_RST: |
| 2632 | rc = ethtool_nway_reset(dev); |
| 2633 | break; |
| 2634 | case ETHTOOL_GLINK: |
Ben Hutchings | e596e6e | 2010-12-09 12:08:35 +0000 | [diff] [blame] | 2635 | rc = ethtool_get_link(dev, useraddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | break; |
| 2637 | case ETHTOOL_GEEPROM: |
| 2638 | rc = ethtool_get_eeprom(dev, useraddr); |
| 2639 | break; |
| 2640 | case ETHTOOL_SEEPROM: |
| 2641 | rc = ethtool_set_eeprom(dev, useraddr); |
| 2642 | break; |
| 2643 | case ETHTOOL_GCOALESCE: |
| 2644 | rc = ethtool_get_coalesce(dev, useraddr); |
| 2645 | break; |
| 2646 | case ETHTOOL_SCOALESCE: |
| 2647 | rc = ethtool_set_coalesce(dev, useraddr); |
| 2648 | break; |
| 2649 | case ETHTOOL_GRINGPARAM: |
| 2650 | rc = ethtool_get_ringparam(dev, useraddr); |
| 2651 | break; |
| 2652 | case ETHTOOL_SRINGPARAM: |
| 2653 | rc = ethtool_set_ringparam(dev, useraddr); |
| 2654 | break; |
| 2655 | case ETHTOOL_GPAUSEPARAM: |
| 2656 | rc = ethtool_get_pauseparam(dev, useraddr); |
| 2657 | break; |
| 2658 | case ETHTOOL_SPAUSEPARAM: |
| 2659 | rc = ethtool_set_pauseparam(dev, useraddr); |
| 2660 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2661 | case ETHTOOL_TEST: |
| 2662 | rc = ethtool_self_test(dev, useraddr); |
| 2663 | break; |
| 2664 | case ETHTOOL_GSTRINGS: |
| 2665 | rc = ethtool_get_strings(dev, useraddr); |
| 2666 | break; |
| 2667 | case ETHTOOL_PHYS_ID: |
| 2668 | rc = ethtool_phys_id(dev, useraddr); |
| 2669 | break; |
| 2670 | case ETHTOOL_GSTATS: |
| 2671 | rc = ethtool_get_stats(dev, useraddr); |
| 2672 | break; |
Jon Wetzel | a6f9a70 | 2005-08-20 17:15:54 -0700 | [diff] [blame] | 2673 | case ETHTOOL_GPERMADDR: |
| 2674 | rc = ethtool_get_perm_addr(dev, useraddr); |
| 2675 | break; |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2676 | case ETHTOOL_GFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2677 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2678 | __ethtool_get_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2679 | break; |
| 2680 | case ETHTOOL_SFLAGS: |
Michał Mirosław | da8ac86c | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2681 | rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags); |
Jeff Garzik | 3ae7c0b | 2007-08-15 16:00:51 -0700 | [diff] [blame] | 2682 | break; |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2683 | case ETHTOOL_GPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2684 | rc = ethtool_get_value(dev, useraddr, ethcmd, |
| 2685 | dev->ethtool_ops->get_priv_flags); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2686 | break; |
| 2687 | case ETHTOOL_SPFLAGS: |
Jeff Garzik | 13c99b2 | 2007-08-15 16:01:56 -0700 | [diff] [blame] | 2688 | rc = ethtool_set_value(dev, useraddr, |
| 2689 | dev->ethtool_ops->set_priv_flags); |
Jeff Garzik | 339bf02 | 2007-08-15 16:01:32 -0700 | [diff] [blame] | 2690 | break; |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2691 | case ETHTOOL_GRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2692 | case ETHTOOL_GRXRINGS: |
| 2693 | case ETHTOOL_GRXCLSRLCNT: |
| 2694 | case ETHTOOL_GRXCLSRULE: |
| 2695 | case ETHTOOL_GRXCLSRLALL: |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 2696 | rc = ethtool_get_rxnfc(dev, ethcmd, useraddr); |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2697 | break; |
| 2698 | case ETHTOOL_SRXFH: |
Santwona Behera | 59089d8 | 2009-02-20 00:58:13 -0800 | [diff] [blame] | 2699 | case ETHTOOL_SRXCLSRLDEL: |
| 2700 | case ETHTOOL_SRXCLSRLINS: |
Ben Hutchings | bf98843 | 2010-06-28 08:45:58 +0000 | [diff] [blame] | 2701 | rc = ethtool_set_rxnfc(dev, ethcmd, useraddr); |
Santwona Behera | 0853ad6 | 2008-07-02 03:47:41 -0700 | [diff] [blame] | 2702 | break; |
Ajit Khaparde | 05c6a8d | 2009-09-02 17:02:55 +0000 | [diff] [blame] | 2703 | case ETHTOOL_FLASHDEV: |
| 2704 | rc = ethtool_flash_device(dev, useraddr); |
| 2705 | break; |
Ben Hutchings | d73d3a8 | 2009-10-05 10:59:58 +0000 | [diff] [blame] | 2706 | case ETHTOOL_RESET: |
| 2707 | rc = ethtool_reset(dev, useraddr); |
| 2708 | break; |
Jeff Garzik | 723b2f5 | 2010-03-03 22:51:50 +0000 | [diff] [blame] | 2709 | case ETHTOOL_GSSET_INFO: |
| 2710 | rc = ethtool_get_sset_info(dev, useraddr); |
| 2711 | break; |
Ben Hutchings | a5b6ee2 | 2010-06-30 05:05:23 +0000 | [diff] [blame] | 2712 | case ETHTOOL_GRXFHINDIR: |
| 2713 | rc = ethtool_get_rxfh_indir(dev, useraddr); |
| 2714 | break; |
| 2715 | case ETHTOOL_SRXFHINDIR: |
| 2716 | rc = ethtool_set_rxfh_indir(dev, useraddr); |
| 2717 | break; |
Venkata Duvvuru | 3de0b59 | 2014-04-21 15:37:59 +0530 | [diff] [blame] | 2718 | case ETHTOOL_GRSSH: |
| 2719 | rc = ethtool_get_rxfh(dev, useraddr); |
| 2720 | break; |
| 2721 | case ETHTOOL_SRSSH: |
| 2722 | rc = ethtool_set_rxfh(dev, useraddr); |
| 2723 | break; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2724 | case ETHTOOL_GFEATURES: |
| 2725 | rc = ethtool_get_features(dev, useraddr); |
| 2726 | break; |
| 2727 | case ETHTOOL_SFEATURES: |
| 2728 | rc = ethtool_set_features(dev, useraddr); |
| 2729 | break; |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2730 | case ETHTOOL_GTXCSUM: |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2731 | case ETHTOOL_GRXCSUM: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2732 | case ETHTOOL_GSG: |
| 2733 | case ETHTOOL_GTSO: |
| 2734 | case ETHTOOL_GUFO: |
| 2735 | case ETHTOOL_GGSO: |
| 2736 | case ETHTOOL_GGRO: |
| 2737 | rc = ethtool_get_one_feature(dev, useraddr, ethcmd); |
| 2738 | break; |
| 2739 | case ETHTOOL_STXCSUM: |
Michał Mirosław | e83d360 | 2011-02-15 16:59:18 +0000 | [diff] [blame] | 2740 | case ETHTOOL_SRXCSUM: |
Michał Mirosław | 0a41770 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 2741 | case ETHTOOL_SSG: |
| 2742 | case ETHTOOL_STSO: |
| 2743 | case ETHTOOL_SUFO: |
| 2744 | case ETHTOOL_SGSO: |
| 2745 | case ETHTOOL_SGRO: |
| 2746 | rc = ethtool_set_one_feature(dev, useraddr, ethcmd); |
| 2747 | break; |
amit salecha | 8b5933c | 2011-04-07 01:58:42 +0000 | [diff] [blame] | 2748 | case ETHTOOL_GCHANNELS: |
| 2749 | rc = ethtool_get_channels(dev, useraddr); |
| 2750 | break; |
| 2751 | case ETHTOOL_SCHANNELS: |
| 2752 | rc = ethtool_set_channels(dev, useraddr); |
| 2753 | break; |
Anirban Chakraborty | 29dd54b | 2011-05-12 12:48:32 +0000 | [diff] [blame] | 2754 | case ETHTOOL_SET_DUMP: |
| 2755 | rc = ethtool_set_dump(dev, useraddr); |
| 2756 | break; |
| 2757 | case ETHTOOL_GET_DUMP_FLAG: |
| 2758 | rc = ethtool_get_dump_flag(dev, useraddr); |
| 2759 | break; |
| 2760 | case ETHTOOL_GET_DUMP_DATA: |
| 2761 | rc = ethtool_get_dump_data(dev, useraddr); |
| 2762 | break; |
Richard Cochran | c8f3a8c | 2012-04-03 22:59:17 +0000 | [diff] [blame] | 2763 | case ETHTOOL_GET_TS_INFO: |
| 2764 | rc = ethtool_get_ts_info(dev, useraddr); |
| 2765 | break; |
Stuart Hodgson | 41c3cb6 | 2012-04-19 09:44:42 +0100 | [diff] [blame] | 2766 | case ETHTOOL_GMODULEINFO: |
| 2767 | rc = ethtool_get_module_info(dev, useraddr); |
| 2768 | break; |
| 2769 | case ETHTOOL_GMODULEEEPROM: |
| 2770 | rc = ethtool_get_module_eeprom(dev, useraddr); |
| 2771 | break; |
Govindarajulu Varadarajan | f0db9b0 | 2014-09-03 03:17:20 +0530 | [diff] [blame] | 2772 | case ETHTOOL_GTUNABLE: |
| 2773 | rc = ethtool_get_tunable(dev, useraddr); |
| 2774 | break; |
| 2775 | case ETHTOOL_STUNABLE: |
| 2776 | rc = ethtool_set_tunable(dev, useraddr); |
| 2777 | break; |
Andrew Lunn | f3a4094 | 2015-12-30 16:28:25 +0100 | [diff] [blame] | 2778 | case ETHTOOL_GPHYSTATS: |
| 2779 | rc = ethtool_get_phy_stats(dev, useraddr); |
| 2780 | break; |
Kan Liang | ac2c7ad | 2016-02-19 09:24:01 -0500 | [diff] [blame] | 2781 | case ETHTOOL_PERQUEUE: |
| 2782 | rc = ethtool_set_per_queue(dev, useraddr); |
| 2783 | break; |
David Decotigny | 3f1ac7a | 2016-02-24 10:57:59 -0800 | [diff] [blame] | 2784 | case ETHTOOL_GLINKSETTINGS: |
| 2785 | rc = ethtool_get_link_ksettings(dev, useraddr); |
| 2786 | break; |
| 2787 | case ETHTOOL_SLINKSETTINGS: |
| 2788 | rc = ethtool_set_link_ksettings(dev, useraddr); |
| 2789 | break; |
Raju Lakkaraju | 968ad9d | 2016-11-17 13:07:21 +0100 | [diff] [blame] | 2790 | case ETHTOOL_PHY_GTUNABLE: |
| 2791 | rc = get_phy_tunable(dev, useraddr); |
| 2792 | break; |
| 2793 | case ETHTOOL_PHY_STUNABLE: |
| 2794 | rc = set_phy_tunable(dev, useraddr); |
| 2795 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | default: |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 2797 | rc = -EOPNOTSUPP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2799 | |
Stephen Hemminger | e71a478 | 2007-04-10 20:10:33 -0700 | [diff] [blame] | 2800 | if (dev->ethtool_ops->complete) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | dev->ethtool_ops->complete(dev); |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 2802 | |
| 2803 | if (old_features != dev->features) |
| 2804 | netdev_features_change(dev); |
| 2805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | } |