blob: 6a7f99661c2fb3e323cd64244cb206fc139da359 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Wilcox61a44b92007-07-31 14:00:02 -07006 * the information ethtool needs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -07008 * 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 Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080016#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/errno.h>
18#include <linux/ethtool.h>
19#include <linux/netdevice.h>
Richard Cochranc8f3a8c2012-04-03 22:59:17 +000020#include <linux/net_tstamp.h>
21#include <linux/phy.h>
Jeff Garzikd17792e2010-03-04 08:21:53 +000022#include <linux/bitops.h>
chavey97f8aef2010-04-07 21:54:42 -070023#include <linux/uaccess.h>
David S. Miller73da16c2010-09-21 16:12:11 -070024#include <linux/vmalloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Ben Hutchings68f512f2011-04-02 00:35:15 +010026#include <linux/rtnetlink.h>
27#include <linux/sched.h>
Eric Dumazet960fb622014-11-16 06:23:05 -080028#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +090030/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * 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
36u32 ethtool_op_get_link(struct net_device *dev)
37{
38 return netif_carrier_ok(dev) ? 1 : 0;
39}
chavey97f8aef2010-04-07 21:54:42 -070040EXPORT_SYMBOL(ethtool_op_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Richard Cochran02eacbd2012-04-03 22:59:22 +000042int 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}
51EXPORT_SYMBOL(ethtool_op_get_ts_info);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Handlers for each ethtool command */
54
Michał Mirosław475414f2011-11-15 15:29:55 +000055#define ETHTOOL_DEV_FEATURE_WORDS ((NETDEV_FEATURE_COUNT + 31) / 32)
Michał Mirosław5455c692011-02-15 16:59:17 +000056
Michał Mirosław9d921542011-11-15 15:29:55 +000057static 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ław9d921542011-11-15 15:29:55 +000060 [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 Cao788dfca2013-06-17 11:28:03 +090064 [NETIF_F_HW_VLAN_CTAG_TX_BIT] = "tx-vlan-hw-insert",
Michał Mirosław9d921542011-11-15 15:29:55 +000065
Fernando Luis Vazquez Cao788dfca2013-06-17 11:28:03 +090066 [NETIF_F_HW_VLAN_CTAG_RX_BIT] = "rx-vlan-hw-parse",
67 [NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
David S. Miller2d6577f2013-04-19 16:08:23 -040068 [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ław9d921542011-11-15 15:29:55 +000071 [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",
82 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
83 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000084 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040085 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
Eric Dumazetcb32f512013-10-19 11:42:57 -070086 [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation",
Eric Dumazet61c1db72013-10-20 20:47:30 -070087 [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000088 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040089 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000090
91 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080092 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000093 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
94 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
95 [NETIF_F_RXHASH_BIT] = "rx-hashing",
96 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
97 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
98 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +000099 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000100 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800101 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
Jiri Pirkod0290212014-04-02 23:09:31 +0200102 [NETIF_F_BUSY_POLL_BIT] = "busy-poll",
John Fastabend1c78c642016-02-16 21:17:37 -0800103 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000104};
105
Eyal Perry892311f2014-12-02 18:12:10 +0200106static const char
107rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
108 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
109 [ETH_RSS_HASH_XOR_BIT] = "xor",
110};
111
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300112static const char
113tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
114 [ETHTOOL_ID_UNSPEC] = "Unspec",
115 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
116 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
117};
118
Michał Mirosław5455c692011-02-15 16:59:17 +0000119static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
120{
121 struct ethtool_gfeatures cmd = {
122 .cmd = ETHTOOL_GFEATURES,
123 .size = ETHTOOL_DEV_FEATURE_WORDS,
124 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000125 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000126 u32 __user *sizeaddr;
127 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000128 int i;
129
130 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000131 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000132
133 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000134 features[i].available = (u32)(dev->hw_features >> (32 * i));
135 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
136 features[i].active = (u32)(dev->features >> (32 * i));
137 features[i].never_changed =
138 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000139 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000140
141 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
142 if (get_user(copy_size, sizeaddr))
143 return -EFAULT;
144
145 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
146 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
147
148 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
149 return -EFAULT;
150 useraddr += sizeof(cmd);
151 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
152 return -EFAULT;
153
154 return 0;
155}
156
157static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
158{
159 struct ethtool_sfeatures cmd;
160 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000161 netdev_features_t wanted = 0, valid = 0;
162 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000163
164 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
165 return -EFAULT;
166 useraddr += sizeof(cmd);
167
168 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
169 return -EINVAL;
170
171 if (copy_from_user(features, useraddr, sizeof(features)))
172 return -EFAULT;
173
Michał Mirosław475414f2011-11-15 15:29:55 +0000174 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000175 valid |= (netdev_features_t)features[i].valid << (32 * i);
176 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000177 }
178
179 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000180 return -EINVAL;
181
Michał Mirosław475414f2011-11-15 15:29:55 +0000182 if (valid & ~dev->hw_features) {
183 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000184 ret |= ETHTOOL_F_UNSUPPORTED;
185 }
186
Michał Mirosław475414f2011-11-15 15:29:55 +0000187 dev->wanted_features &= ~valid;
188 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700189 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000190
Michał Mirosław475414f2011-11-15 15:29:55 +0000191 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000192 ret |= ETHTOOL_F_WISH;
193
194 return ret;
195}
196
Andrew Lunnf3a40942015-12-30 16:28:25 +0100197static int phy_get_sset_count(struct phy_device *phydev)
198{
199 int ret;
200
201 if (phydev->drv->get_sset_count &&
202 phydev->drv->get_strings &&
203 phydev->drv->get_stats) {
204 mutex_lock(&phydev->lock);
205 ret = phydev->drv->get_sset_count(phydev);
206 mutex_unlock(&phydev->lock);
207
208 return ret;
209 }
210
211 return -EOPNOTSUPP;
212}
213
Michał Mirosław340ae162011-02-15 16:59:16 +0000214static int __ethtool_get_sset_count(struct net_device *dev, int sset)
215{
216 const struct ethtool_ops *ops = dev->ethtool_ops;
217
Michał Mirosław5455c692011-02-15 16:59:17 +0000218 if (sset == ETH_SS_FEATURES)
219 return ARRAY_SIZE(netdev_features_strings);
220
Eyal Perry892311f2014-12-02 18:12:10 +0200221 if (sset == ETH_SS_RSS_HASH_FUNCS)
222 return ARRAY_SIZE(rss_hash_func_strings);
223
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300224 if (sset == ETH_SS_TUNABLES)
225 return ARRAY_SIZE(tunable_strings);
226
Andrew Lunnf3a40942015-12-30 16:28:25 +0100227 if (sset == ETH_SS_PHY_STATS) {
228 if (dev->phydev)
229 return phy_get_sset_count(dev->phydev);
230 else
231 return -EOPNOTSUPP;
232 }
233
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000234 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000235 return ops->get_sset_count(dev, sset);
236 else
237 return -EOPNOTSUPP;
238}
239
240static void __ethtool_get_strings(struct net_device *dev,
241 u32 stringset, u8 *data)
242{
243 const struct ethtool_ops *ops = dev->ethtool_ops;
244
Michał Mirosław5455c692011-02-15 16:59:17 +0000245 if (stringset == ETH_SS_FEATURES)
246 memcpy(data, netdev_features_strings,
247 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200248 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
249 memcpy(data, rss_hash_func_strings,
250 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300251 else if (stringset == ETH_SS_TUNABLES)
252 memcpy(data, tunable_strings, sizeof(tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100253 else if (stringset == ETH_SS_PHY_STATS) {
254 struct phy_device *phydev = dev->phydev;
255
256 if (phydev) {
257 mutex_lock(&phydev->lock);
258 phydev->drv->get_strings(phydev, data);
259 mutex_unlock(&phydev->lock);
260 } else {
261 return;
262 }
263 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000264 /* ops->get_strings is valid because checked earlier */
265 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000266}
267
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000268static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000269{
270 /* feature masks of legacy discrete ethtool ops */
271
272 switch (eth_cmd) {
273 case ETHTOOL_GTXCSUM:
274 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800275 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000276 case ETHTOOL_GRXCSUM:
277 case ETHTOOL_SRXCSUM:
278 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000279 case ETHTOOL_GSG:
280 case ETHTOOL_SSG:
281 return NETIF_F_SG;
282 case ETHTOOL_GTSO:
283 case ETHTOOL_STSO:
284 return NETIF_F_ALL_TSO;
285 case ETHTOOL_GUFO:
286 case ETHTOOL_SUFO:
287 return NETIF_F_UFO;
288 case ETHTOOL_GGSO:
289 case ETHTOOL_SGSO:
290 return NETIF_F_GSO;
291 case ETHTOOL_GGRO:
292 case ETHTOOL_SGRO:
293 return NETIF_F_GRO;
294 default:
295 BUG();
296 }
297}
298
Michał Mirosław0a417702011-02-15 16:59:17 +0000299static int ethtool_get_one_feature(struct net_device *dev,
300 char __user *useraddr, u32 ethcmd)
301{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000302 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000303 struct ethtool_value edata = {
304 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000305 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000306 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000307
Michał Mirosław0a417702011-02-15 16:59:17 +0000308 if (copy_to_user(useraddr, &edata, sizeof(edata)))
309 return -EFAULT;
310 return 0;
311}
312
Michał Mirosław0a417702011-02-15 16:59:17 +0000313static int ethtool_set_one_feature(struct net_device *dev,
314 void __user *useraddr, u32 ethcmd)
315{
316 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000317 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000318
319 if (copy_from_user(&edata, useraddr, sizeof(edata)))
320 return -EFAULT;
321
Michał Mirosław86794882011-02-15 16:59:17 +0000322 mask = ethtool_get_feature_mask(ethcmd);
323 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000324 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000325 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000326
327 if (edata.data)
328 dev->wanted_features |= mask;
329 else
330 dev->wanted_features &= ~mask;
331
332 __netdev_update_features(dev);
333
334 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000335}
336
Michał Mirosław02b3a552011-11-15 15:29:55 +0000337#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
338 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000339#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
340 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
341 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000342
343static u32 __ethtool_get_flags(struct net_device *dev)
344{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000345 u32 flags = 0;
346
Dragos Foianu8a731252013-07-13 14:43:00 +0100347 if (dev->features & NETIF_F_LRO)
348 flags |= ETH_FLAG_LRO;
349 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
350 flags |= ETH_FLAG_RXVLAN;
351 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
352 flags |= ETH_FLAG_TXVLAN;
353 if (dev->features & NETIF_F_NTUPLE)
354 flags |= ETH_FLAG_NTUPLE;
355 if (dev->features & NETIF_F_RXHASH)
356 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000357
358 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000359}
360
361static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000362{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000363 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000364
Michał Mirosław02b3a552011-11-15 15:29:55 +0000365 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000366 return -EINVAL;
367
Dragos Foianu8a731252013-07-13 14:43:00 +0100368 if (data & ETH_FLAG_LRO)
369 features |= NETIF_F_LRO;
370 if (data & ETH_FLAG_RXVLAN)
371 features |= NETIF_F_HW_VLAN_CTAG_RX;
372 if (data & ETH_FLAG_TXVLAN)
373 features |= NETIF_F_HW_VLAN_CTAG_TX;
374 if (data & ETH_FLAG_NTUPLE)
375 features |= NETIF_F_NTUPLE;
376 if (data & ETH_FLAG_RXHASH)
377 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000378
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000379 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000380 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000381 if (changed & ~dev->hw_features)
382 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
383
384 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000385 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000386
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700387 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000388
389 return 0;
390}
391
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800392static void convert_legacy_u32_to_link_mode(unsigned long *dst, u32 legacy_u32)
393{
394 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
395 dst[0] = legacy_u32;
396}
397
398/* return false if src had higher bits set. lower bits always updated. */
399static bool convert_link_mode_to_legacy_u32(u32 *legacy_u32,
400 const unsigned long *src)
401{
402 bool retval = true;
403
404 /* TODO: following test will soon always be true */
405 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
406 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
407
408 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
409 bitmap_fill(ext, 32);
410 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
411 if (bitmap_intersects(ext, src,
412 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
413 /* src mask goes beyond bit 31 */
414 retval = false;
415 }
416 }
417 *legacy_u32 = src[0];
418 return retval;
419}
420
421/* return false if legacy contained non-0 deprecated fields
422 * transceiver/maxtxpkt/maxrxpkt. rest of ksettings always updated
423 */
424static bool
425convert_legacy_settings_to_link_ksettings(
426 struct ethtool_link_ksettings *link_ksettings,
427 const struct ethtool_cmd *legacy_settings)
428{
429 bool retval = true;
430
431 memset(link_ksettings, 0, sizeof(*link_ksettings));
432
433 /* This is used to tell users that driver is still using these
434 * deprecated legacy fields, and they should not use
435 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
436 */
437 if (legacy_settings->transceiver ||
438 legacy_settings->maxtxpkt ||
439 legacy_settings->maxrxpkt)
440 retval = false;
441
442 convert_legacy_u32_to_link_mode(
443 link_ksettings->link_modes.supported,
444 legacy_settings->supported);
445 convert_legacy_u32_to_link_mode(
446 link_ksettings->link_modes.advertising,
447 legacy_settings->advertising);
448 convert_legacy_u32_to_link_mode(
449 link_ksettings->link_modes.lp_advertising,
450 legacy_settings->lp_advertising);
451 link_ksettings->base.speed
452 = ethtool_cmd_speed(legacy_settings);
453 link_ksettings->base.duplex
454 = legacy_settings->duplex;
455 link_ksettings->base.port
456 = legacy_settings->port;
457 link_ksettings->base.phy_address
458 = legacy_settings->phy_address;
459 link_ksettings->base.autoneg
460 = legacy_settings->autoneg;
461 link_ksettings->base.mdio_support
462 = legacy_settings->mdio_support;
463 link_ksettings->base.eth_tp_mdix
464 = legacy_settings->eth_tp_mdix;
465 link_ksettings->base.eth_tp_mdix_ctrl
466 = legacy_settings->eth_tp_mdix_ctrl;
467 return retval;
468}
469
470/* return false if ksettings link modes had higher bits
471 * set. legacy_settings always updated (best effort)
472 */
473static bool
474convert_link_ksettings_to_legacy_settings(
475 struct ethtool_cmd *legacy_settings,
476 const struct ethtool_link_ksettings *link_ksettings)
477{
478 bool retval = true;
479
480 memset(legacy_settings, 0, sizeof(*legacy_settings));
481 /* this also clears the deprecated fields in legacy structure:
482 * __u8 transceiver;
483 * __u32 maxtxpkt;
484 * __u32 maxrxpkt;
485 */
486
487 retval &= convert_link_mode_to_legacy_u32(
488 &legacy_settings->supported,
489 link_ksettings->link_modes.supported);
490 retval &= convert_link_mode_to_legacy_u32(
491 &legacy_settings->advertising,
492 link_ksettings->link_modes.advertising);
493 retval &= convert_link_mode_to_legacy_u32(
494 &legacy_settings->lp_advertising,
495 link_ksettings->link_modes.lp_advertising);
496 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
497 legacy_settings->duplex
498 = link_ksettings->base.duplex;
499 legacy_settings->port
500 = link_ksettings->base.port;
501 legacy_settings->phy_address
502 = link_ksettings->base.phy_address;
503 legacy_settings->autoneg
504 = link_ksettings->base.autoneg;
505 legacy_settings->mdio_support
506 = link_ksettings->base.mdio_support;
507 legacy_settings->eth_tp_mdix
508 = link_ksettings->base.eth_tp_mdix;
509 legacy_settings->eth_tp_mdix_ctrl
510 = link_ksettings->base.eth_tp_mdix_ctrl;
511 return retval;
512}
513
514/* number of 32-bit words to store the user's link mode bitmaps */
515#define __ETHTOOL_LINK_MODE_MASK_NU32 \
516 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
517
518/* layout of the struct passed from/to userland */
519struct ethtool_link_usettings {
520 struct ethtool_link_settings base;
521 struct {
522 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
523 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
524 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
525 } link_modes;
526};
527
528/* Internal kernel helper to query a device ethtool_link_settings.
529 *
530 * Backward compatibility note: for compatibility with legacy drivers
531 * that implement only the ethtool_cmd API, this has to work with both
532 * drivers implementing get_link_ksettings API and drivers
533 * implementing get_settings API. When drivers implement get_settings
534 * and report ethtool_cmd deprecated fields
535 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
536 * because the resulting struct ethtool_link_settings does not report them.
537 */
538int __ethtool_get_link_ksettings(struct net_device *dev,
539 struct ethtool_link_ksettings *link_ksettings)
540{
541 int err;
542 struct ethtool_cmd cmd;
543
544 ASSERT_RTNL();
545
546 if (dev->ethtool_ops->get_link_ksettings) {
547 memset(link_ksettings, 0, sizeof(*link_ksettings));
548 return dev->ethtool_ops->get_link_ksettings(dev,
549 link_ksettings);
550 }
551
552 /* driver doesn't support %ethtool_link_ksettings API. revert to
553 * legacy %ethtool_cmd API, unless it's not supported either.
554 * TODO: remove when ethtool_ops::get_settings disappears internally
555 */
David Decotigny3237fc62016-02-24 10:58:11 -0800556 if (!dev->ethtool_ops->get_settings)
557 return -EOPNOTSUPP;
558
559 memset(&cmd, 0, sizeof(cmd));
560 cmd.cmd = ETHTOOL_GSET;
561 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800562 if (err < 0)
563 return err;
564
565 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
566 */
567 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
568 return err;
569}
570EXPORT_SYMBOL(__ethtool_get_link_ksettings);
571
572/* convert ethtool_link_usettings in user space to a kernel internal
573 * ethtool_link_ksettings. return 0 on success, errno on error.
574 */
575static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
576 const void __user *from)
577{
578 struct ethtool_link_usettings link_usettings;
579
580 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
581 return -EFAULT;
582
583 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
584 bitmap_from_u32array(to->link_modes.supported,
585 __ETHTOOL_LINK_MODE_MASK_NBITS,
586 link_usettings.link_modes.supported,
587 __ETHTOOL_LINK_MODE_MASK_NU32);
588 bitmap_from_u32array(to->link_modes.advertising,
589 __ETHTOOL_LINK_MODE_MASK_NBITS,
590 link_usettings.link_modes.advertising,
591 __ETHTOOL_LINK_MODE_MASK_NU32);
592 bitmap_from_u32array(to->link_modes.lp_advertising,
593 __ETHTOOL_LINK_MODE_MASK_NBITS,
594 link_usettings.link_modes.lp_advertising,
595 __ETHTOOL_LINK_MODE_MASK_NU32);
596
597 return 0;
598}
599
600/* convert a kernel internal ethtool_link_ksettings to
601 * ethtool_link_usettings in user space. return 0 on success, errno on
602 * error.
603 */
604static int
605store_link_ksettings_for_user(void __user *to,
606 const struct ethtool_link_ksettings *from)
607{
608 struct ethtool_link_usettings link_usettings;
609
610 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
611 bitmap_to_u32array(link_usettings.link_modes.supported,
612 __ETHTOOL_LINK_MODE_MASK_NU32,
613 from->link_modes.supported,
614 __ETHTOOL_LINK_MODE_MASK_NBITS);
615 bitmap_to_u32array(link_usettings.link_modes.advertising,
616 __ETHTOOL_LINK_MODE_MASK_NU32,
617 from->link_modes.advertising,
618 __ETHTOOL_LINK_MODE_MASK_NBITS);
619 bitmap_to_u32array(link_usettings.link_modes.lp_advertising,
620 __ETHTOOL_LINK_MODE_MASK_NU32,
621 from->link_modes.lp_advertising,
622 __ETHTOOL_LINK_MODE_MASK_NBITS);
623
624 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
625 return -EFAULT;
626
627 return 0;
628}
629
630/* Query device for its ethtool_link_settings.
631 *
632 * Backward compatibility note: this function must fail when driver
633 * does not implement ethtool::get_link_ksettings, even if legacy
634 * ethtool_ops::get_settings is implemented. This tells new versions
635 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
636 * this driver, so that they can correctly access the ethtool_cmd
637 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
638 * implements ethtool_ops::get_settings anymore.
639 */
640static int ethtool_get_link_ksettings(struct net_device *dev,
641 void __user *useraddr)
642{
643 int err = 0;
644 struct ethtool_link_ksettings link_ksettings;
645
646 ASSERT_RTNL();
647
648 if (!dev->ethtool_ops->get_link_ksettings)
649 return -EOPNOTSUPP;
650
651 /* handle bitmap nbits handshake */
652 if (copy_from_user(&link_ksettings.base, useraddr,
653 sizeof(link_ksettings.base)))
654 return -EFAULT;
655
656 if (__ETHTOOL_LINK_MODE_MASK_NU32
657 != link_ksettings.base.link_mode_masks_nwords) {
658 /* wrong link mode nbits requested */
659 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000660 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800661 /* send back number of words required as negative val */
662 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
663 "need too many bits for link modes!");
664 link_ksettings.base.link_mode_masks_nwords
665 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
666
667 /* copy the base fields back to user, not the link
668 * mode bitmaps
669 */
670 if (copy_to_user(useraddr, &link_ksettings.base,
671 sizeof(link_ksettings.base)))
672 return -EFAULT;
673
674 return 0;
675 }
676
677 /* handshake successful: user/kernel agree on
678 * link_mode_masks_nwords
679 */
680
681 memset(&link_ksettings, 0, sizeof(link_ksettings));
682 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
683 if (err < 0)
684 return err;
685
686 /* make sure we tell the right values to user */
687 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
688 link_ksettings.base.link_mode_masks_nwords
689 = __ETHTOOL_LINK_MODE_MASK_NU32;
690
691 return store_link_ksettings_for_user(useraddr, &link_ksettings);
692}
693
694/* Update device ethtool_link_settings.
695 *
696 * Backward compatibility note: this function must fail when driver
697 * does not implement ethtool::set_link_ksettings, even if legacy
698 * ethtool_ops::set_settings is implemented. This tells new versions
699 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
700 * this driver, so that they can correctly update the ethtool_cmd
701 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
702 * implements ethtool_ops::get_settings anymore.
703 */
704static int ethtool_set_link_ksettings(struct net_device *dev,
705 void __user *useraddr)
706{
707 int err;
708 struct ethtool_link_ksettings link_ksettings;
709
710 ASSERT_RTNL();
711
712 if (!dev->ethtool_ops->set_link_ksettings)
713 return -EOPNOTSUPP;
714
715 /* make sure nbits field has expected value */
716 if (copy_from_user(&link_ksettings.base, useraddr,
717 sizeof(link_ksettings.base)))
718 return -EFAULT;
719
720 if (__ETHTOOL_LINK_MODE_MASK_NU32
721 != link_ksettings.base.link_mode_masks_nwords)
722 return -EINVAL;
723
724 /* copy the whole structure, now that we know it has expected
725 * format
726 */
727 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
728 if (err)
729 return err;
730
731 /* re-check nwords field, just in case */
732 if (__ETHTOOL_LINK_MODE_MASK_NU32
733 != link_ksettings.base.link_mode_masks_nwords)
734 return -EINVAL;
735
736 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
737}
738
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800739static void
740warn_incomplete_ethtool_legacy_settings_conversion(const char *details)
741{
742 char name[sizeof(current->comm)];
743
744 pr_info_once("warning: `%s' uses legacy ethtool link settings API, %s\n",
745 get_task_comm(name, current), details);
746}
747
748/* Query device for its ethtool_cmd settings.
749 *
750 * Backward compatibility note: for compatibility with legacy ethtool,
751 * this has to work with both drivers implementing get_link_ksettings
752 * API and drivers implementing get_settings API. When drivers
753 * implement get_link_ksettings and report higher link mode bits, a
754 * kernel warning is logged once (with name of 1st driver/device) to
755 * recommend user to upgrade ethtool, but the command is successful
756 * (only the lower link mode bits reported back to user).
757 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000758static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
759{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000760 struct ethtool_cmd cmd;
761
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800762 ASSERT_RTNL();
763
764 if (dev->ethtool_ops->get_link_ksettings) {
765 /* First, use link_ksettings API if it is supported */
766 int err;
767 struct ethtool_link_ksettings link_ksettings;
768
769 memset(&link_ksettings, 0, sizeof(link_ksettings));
770 err = dev->ethtool_ops->get_link_ksettings(dev,
771 &link_ksettings);
772 if (err < 0)
773 return err;
774 if (!convert_link_ksettings_to_legacy_settings(&cmd,
775 &link_ksettings))
776 warn_incomplete_ethtool_legacy_settings_conversion(
777 "link modes are only partially reported");
778
779 /* send a sensible cmd tag back to user */
780 cmd.cmd = ETHTOOL_GSET;
781 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800782 /* driver doesn't support %ethtool_link_ksettings
783 * API. revert to legacy %ethtool_cmd API, unless it's
784 * not supported either.
785 */
David Decotigny3237fc62016-02-24 10:58:11 -0800786 int err;
787
788 if (!dev->ethtool_ops->get_settings)
789 return -EOPNOTSUPP;
790
791 memset(&cmd, 0, sizeof(cmd));
792 cmd.cmd = ETHTOOL_GSET;
793 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800794 if (err < 0)
795 return err;
796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
799 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
802}
803
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800804/* Update device link settings with given ethtool_cmd.
805 *
806 * Backward compatibility note: for compatibility with legacy ethtool,
807 * this has to work with both drivers implementing set_link_ksettings
808 * API and drivers implementing set_settings API. When drivers
809 * implement set_link_ksettings and user's request updates deprecated
810 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
811 * warning is logged once (with name of 1st driver/device) to
812 * recommend user to upgrade ethtool, and the request is rejected.
813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
815{
816 struct ethtool_cmd cmd;
817
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800818 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
821 return -EFAULT;
822
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800823 /* first, try new %ethtool_link_ksettings API. */
824 if (dev->ethtool_ops->set_link_ksettings) {
825 struct ethtool_link_ksettings link_ksettings;
826
827 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
828 &cmd))
829 return -EINVAL;
830
831 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
832 link_ksettings.base.link_mode_masks_nwords
833 = __ETHTOOL_LINK_MODE_MASK_NU32;
834 return dev->ethtool_ops->set_link_ksettings(dev,
835 &link_ksettings);
836 }
837
838 /* legacy %ethtool_cmd API */
839
840 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
841 * disappears internally
842 */
843
844 if (!dev->ethtool_ops->set_settings)
845 return -EOPNOTSUPP;
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return dev->ethtool_ops->set_settings(dev, &cmd);
848}
849
chavey97f8aef2010-04-07 21:54:42 -0700850static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
851 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700854 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 memset(&info, 0, sizeof(info));
857 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000858 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700859 ops->get_drvinfo(dev, &info);
860 } else if (dev->dev.parent && dev->dev.parent->driver) {
861 strlcpy(info.bus_info, dev_name(dev->dev.parent),
862 sizeof(info.bus_info));
863 strlcpy(info.driver, dev->dev.parent->driver->name,
864 sizeof(info.driver));
865 } else {
866 return -EOPNOTSUPP;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Jeff Garzik723b2f52010-03-03 22:51:50 +0000869 /*
870 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000871 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000872 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000873 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700874 int rc;
875
876 rc = ops->get_sset_count(dev, ETH_SS_TEST);
877 if (rc >= 0)
878 info.testinfo_len = rc;
879 rc = ops->get_sset_count(dev, ETH_SS_STATS);
880 if (rc >= 0)
881 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700882 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
883 if (rc >= 0)
884 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700885 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000886 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000888 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 info.eedump_len = ops->get_eeprom_len(dev);
890
891 if (copy_to_user(useraddr, &info, sizeof(info)))
892 return -EFAULT;
893 return 0;
894}
895
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800896static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700897 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000898{
899 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000900 u64 sset_mask;
901 int i, idx = 0, n_bits = 0, ret, rc;
902 u32 *info_buf = NULL;
903
Jeff Garzik723b2f52010-03-03 22:51:50 +0000904 if (copy_from_user(&info, useraddr, sizeof(info)))
905 return -EFAULT;
906
907 /* store copy of mask, because we zero struct later on */
908 sset_mask = info.sset_mask;
909 if (!sset_mask)
910 return 0;
911
912 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000913 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000914
915 memset(&info, 0, sizeof(info));
916 info.cmd = ETHTOOL_GSSET_INFO;
917
918 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
919 if (!info_buf)
920 return -ENOMEM;
921
922 /*
923 * fill return buffer based on input bitmask and successful
924 * get_sset_count return
925 */
926 for (i = 0; i < 64; i++) {
927 if (!(sset_mask & (1ULL << i)))
928 continue;
929
Michał Mirosław340ae162011-02-15 16:59:16 +0000930 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000931 if (rc >= 0) {
932 info.sset_mask |= (1ULL << i);
933 info_buf[idx++] = rc;
934 }
935 }
936
937 ret = -EFAULT;
938 if (copy_to_user(useraddr, &info, sizeof(info)))
939 goto out;
940
941 useraddr += offsetof(struct ethtool_sset_info, data);
942 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
943 goto out;
944
945 ret = 0;
946
947out:
948 kfree(info_buf);
949 return ret;
950}
951
chavey97f8aef2010-04-07 21:54:42 -0700952static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000953 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700954{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000955 struct ethtool_rxnfc info;
956 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000957 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700958
Santwona Behera59089d82009-02-20 00:58:13 -0800959 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700960 return -EOPNOTSUPP;
961
Ben Hutchingsbf988432010-06-28 08:45:58 +0000962 /* struct ethtool_rxnfc was originally defined for
963 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
964 * members. User-space might still be using that
965 * definition. */
966 if (cmd == ETHTOOL_SRXFH)
967 info_size = (offsetof(struct ethtool_rxnfc, data) +
968 sizeof(info.data));
969
970 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700971 return -EFAULT;
972
Ben Hutchings55664f32012-01-03 12:04:51 +0000973 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
974 if (rc)
975 return rc;
976
977 if (cmd == ETHTOOL_SRXCLSRLINS &&
978 copy_to_user(useraddr, &info, info_size))
979 return -EFAULT;
980
981 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -0700982}
983
chavey97f8aef2010-04-07 21:54:42 -0700984static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000985 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700986{
987 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +0000988 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -0800989 const struct ethtool_ops *ops = dev->ethtool_ops;
990 int ret;
991 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -0700992
Santwona Behera59089d82009-02-20 00:58:13 -0800993 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700994 return -EOPNOTSUPP;
995
Ben Hutchingsbf988432010-06-28 08:45:58 +0000996 /* struct ethtool_rxnfc was originally defined for
997 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
998 * members. User-space might still be using that
999 * definition. */
1000 if (cmd == ETHTOOL_GRXFH)
1001 info_size = (offsetof(struct ethtool_rxnfc, data) +
1002 sizeof(info.data));
1003
1004 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001005 return -EFAULT;
1006
Santwona Behera59089d82009-02-20 00:58:13 -08001007 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1008 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001009 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001010 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001011 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001012 if (!rule_buf)
1013 return -ENOMEM;
1014 }
1015 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001016
Santwona Behera59089d82009-02-20 00:58:13 -08001017 ret = ops->get_rxnfc(dev, &info, rule_buf);
1018 if (ret < 0)
1019 goto err_out;
1020
1021 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001022 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001023 goto err_out;
1024
1025 if (rule_buf) {
1026 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1027 if (copy_to_user(useraddr, rule_buf,
1028 info.rule_cnt * sizeof(u32)))
1029 goto err_out;
1030 }
1031 ret = 0;
1032
1033err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001034 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001035
1036 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001037}
1038
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301039static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1040 struct ethtool_rxnfc *rx_rings,
1041 u32 size)
1042{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001043 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301044
1045 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001046 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301047
1048 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001049 for (i = 0; i < size; i++)
1050 if (indir[i] >= rx_rings->data)
1051 return -EINVAL;
1052
1053 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301054}
1055
Kim Jonesba905f52016-02-02 03:51:16 +00001056u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001057
1058void netdev_rss_key_fill(void *buffer, size_t len)
1059{
1060 BUG_ON(len > sizeof(netdev_rss_key));
1061 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1062 memcpy(buffer, netdev_rss_key, len);
1063}
1064EXPORT_SYMBOL(netdev_rss_key_fill);
1065
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001066static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1067{
1068 u32 dev_size, current_max = 0;
1069 u32 *indir;
1070 int ret;
1071
1072 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1073 !dev->ethtool_ops->get_rxfh)
1074 return -EOPNOTSUPP;
1075 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1076 if (dev_size == 0)
1077 return -EOPNOTSUPP;
1078
1079 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1080 if (!indir)
1081 return -ENOMEM;
1082
1083 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1084 if (ret)
1085 goto out;
1086
1087 while (dev_size--)
1088 current_max = max(current_max, indir[dev_size]);
1089
1090 *max = current_max;
1091
1092out:
1093 kfree(indir);
1094 return ret;
1095}
1096
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001097static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1098 void __user *useraddr)
1099{
Ben Hutchings7850f632011-12-15 13:55:01 +00001100 u32 user_size, dev_size;
1101 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001102 int ret;
1103
Ben Hutchings7850f632011-12-15 13:55:01 +00001104 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001105 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001106 return -EOPNOTSUPP;
1107 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1108 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001109 return -EOPNOTSUPP;
1110
Ben Hutchings7850f632011-12-15 13:55:01 +00001111 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001112 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001113 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001114 return -EFAULT;
1115
Ben Hutchings7850f632011-12-15 13:55:01 +00001116 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1117 &dev_size, sizeof(dev_size)))
1118 return -EFAULT;
1119
1120 /* If the user buffer size is 0, this is just a query for the
1121 * device table size. Otherwise, if it's smaller than the
1122 * device table size it's an error.
1123 */
1124 if (user_size < dev_size)
1125 return user_size == 0 ? 0 : -EINVAL;
1126
1127 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001128 if (!indir)
1129 return -ENOMEM;
1130
Eyal Perry892311f2014-12-02 18:12:10 +02001131 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001132 if (ret)
1133 goto out;
1134
Ben Hutchings7850f632011-12-15 13:55:01 +00001135 if (copy_to_user(useraddr +
1136 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1137 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001138 ret = -EFAULT;
1139
1140out:
1141 kfree(indir);
1142 return ret;
1143}
1144
1145static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1146 void __user *useraddr)
1147{
Ben Hutchings7850f632011-12-15 13:55:01 +00001148 struct ethtool_rxnfc rx_rings;
1149 u32 user_size, dev_size, i;
1150 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001151 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001152 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301153 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001154
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001155 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001156 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001157 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001158
1159 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001160 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001161 return -EOPNOTSUPP;
1162
Ben Hutchings7850f632011-12-15 13:55:01 +00001163 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001164 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001165 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001166 return -EFAULT;
1167
Ben Hutchings278bc422011-12-15 13:56:49 +00001168 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001169 return -EINVAL;
1170
1171 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001172 if (!indir)
1173 return -ENOMEM;
1174
Ben Hutchings7850f632011-12-15 13:55:01 +00001175 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001176 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001177 if (ret)
1178 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001179
1180 if (user_size == 0) {
1181 for (i = 0; i < dev_size; i++)
1182 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1183 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301184 ret = ethtool_copy_validate_indir(indir,
1185 useraddr + ringidx_offset,
1186 &rx_rings,
1187 dev_size);
1188 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001189 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001190 }
1191
Eyal Perry892311f2014-12-02 18:12:10 +02001192 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001193 if (ret)
1194 goto out;
1195
1196 /* indicate whether rxfh was set to default */
1197 if (user_size == 0)
1198 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1199 else
1200 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001201
1202out:
1203 kfree(indir);
1204 return ret;
1205}
1206
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301207static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1208 void __user *useraddr)
1209{
1210 int ret;
1211 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001212 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301213 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001214 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301215 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001216 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301217 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001218 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301219 u8 *hkey = NULL;
1220 u8 *rss_config;
1221
Eyal Perry892311f2014-12-02 18:12:10 +02001222 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301223 return -EOPNOTSUPP;
1224
1225 if (ops->get_rxfh_indir_size)
1226 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301227 if (ops->get_rxfh_key_size)
1228 dev_key_size = ops->get_rxfh_key_size(dev);
1229
Ben Hutchingsf062a382014-05-15 16:28:07 +01001230 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301231 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001232 user_indir_size = rxfh.indir_size;
1233 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301234
Ben Hutchingsf062a382014-05-15 16:28:07 +01001235 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +02001236 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1237 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001238 return -EINVAL;
1239
1240 rxfh.indir_size = dev_indir_size;
1241 rxfh.key_size = dev_key_size;
1242 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301243 return -EFAULT;
1244
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301245 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1246 (user_key_size && (user_key_size != dev_key_size)))
1247 return -EINVAL;
1248
1249 indir_bytes = user_indir_size * sizeof(indir[0]);
1250 total_size = indir_bytes + user_key_size;
1251 rss_config = kzalloc(total_size, GFP_USER);
1252 if (!rss_config)
1253 return -ENOMEM;
1254
1255 if (user_indir_size)
1256 indir = (u32 *)rss_config;
1257
1258 if (user_key_size)
1259 hkey = rss_config + indir_bytes;
1260
Eyal Perry892311f2014-12-02 18:12:10 +02001261 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1262 if (ret)
1263 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301264
Eyal Perry892311f2014-12-02 18:12:10 +02001265 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1266 &dev_hfunc, sizeof(rxfh.hfunc))) {
1267 ret = -EFAULT;
1268 } else if (copy_to_user(useraddr +
1269 offsetof(struct ethtool_rxfh, rss_config[0]),
1270 rss_config, total_size)) {
1271 ret = -EFAULT;
1272 }
1273out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301274 kfree(rss_config);
1275
1276 return ret;
1277}
1278
1279static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1280 void __user *useraddr)
1281{
1282 int ret;
1283 const struct ethtool_ops *ops = dev->ethtool_ops;
1284 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001285 struct ethtool_rxfh rxfh;
1286 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301287 u32 *indir = NULL, indir_bytes = 0;
1288 u8 *hkey = NULL;
1289 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301290 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1291
Eyal Perry892311f2014-12-02 18:12:10 +02001292 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301293 return -EOPNOTSUPP;
1294
1295 if (ops->get_rxfh_indir_size)
1296 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301297 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001298 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301299
Ben Hutchingsf062a382014-05-15 16:28:07 +01001300 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301301 return -EFAULT;
1302
Ben Hutchingsf062a382014-05-15 16:28:07 +01001303 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +02001304 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1305 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001306 return -EINVAL;
1307
Eyal Perry892311f2014-12-02 18:12:10 +02001308 /* If either indir, hash key or function is valid, proceed further.
1309 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301310 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001311 if ((rxfh.indir_size &&
1312 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1313 rxfh.indir_size != dev_indir_size) ||
1314 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1315 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001316 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301317 return -EINVAL;
1318
Ben Hutchingsf062a382014-05-15 16:28:07 +01001319 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301320 indir_bytes = dev_indir_size * sizeof(indir[0]);
1321
Ben Hutchingsf062a382014-05-15 16:28:07 +01001322 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301323 if (!rss_config)
1324 return -ENOMEM;
1325
1326 rx_rings.cmd = ETHTOOL_GRXRINGS;
1327 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1328 if (ret)
1329 goto out;
1330
Ben Hutchingsf062a382014-05-15 16:28:07 +01001331 /* rxfh.indir_size == 0 means reset the indir table to default.
1332 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301333 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001334 if (rxfh.indir_size &&
1335 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301336 indir = (u32 *)rss_config;
1337 ret = ethtool_copy_validate_indir(indir,
1338 useraddr + rss_cfg_offset,
1339 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001340 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301341 if (ret)
1342 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001343 } else if (rxfh.indir_size == 0) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301344 indir = (u32 *)rss_config;
1345 for (i = 0; i < dev_indir_size; i++)
1346 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1347 }
1348
Ben Hutchingsf062a382014-05-15 16:28:07 +01001349 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301350 hkey = rss_config + indir_bytes;
1351 if (copy_from_user(hkey,
1352 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001353 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301354 ret = -EFAULT;
1355 goto out;
1356 }
1357 }
1358
Eyal Perry892311f2014-12-02 18:12:10 +02001359 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001360 if (ret)
1361 goto out;
1362
1363 /* indicate whether rxfh was set to default */
1364 if (rxfh.indir_size == 0)
1365 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1366 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1367 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301368
1369out:
1370 kfree(rss_config);
1371 return ret;
1372}
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1375{
1376 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001377 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 void *regbuf;
1379 int reglen, ret;
1380
1381 if (!ops->get_regs || !ops->get_regs_len)
1382 return -EOPNOTSUPP;
1383
1384 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1385 return -EFAULT;
1386
1387 reglen = ops->get_regs_len(dev);
1388 if (regs.len > reglen)
1389 regs.len = reglen;
1390
Eugene Teob7c7d012011-01-24 21:05:17 -08001391 regbuf = vzalloc(reglen);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001392 if (reglen && !regbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 return -ENOMEM;
1394
1395 ops->get_regs(dev, &regs, regbuf);
1396
1397 ret = -EFAULT;
1398 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1399 goto out;
1400 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001401 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto out;
1403 ret = 0;
1404
1405 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001406 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return ret;
1408}
1409
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001410static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1411{
1412 struct ethtool_value reset;
1413 int ret;
1414
1415 if (!dev->ethtool_ops->reset)
1416 return -EOPNOTSUPP;
1417
1418 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1419 return -EFAULT;
1420
1421 ret = dev->ethtool_ops->reset(dev, &reset.data);
1422 if (ret)
1423 return ret;
1424
1425 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1426 return -EFAULT;
1427 return 0;
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1431{
Roland Dreier8e557422010-02-11 12:14:23 -08001432 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (!dev->ethtool_ops->get_wol)
1435 return -EOPNOTSUPP;
1436
1437 dev->ethtool_ops->get_wol(dev, &wol);
1438
1439 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1440 return -EFAULT;
1441 return 0;
1442}
1443
1444static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1445{
1446 struct ethtool_wolinfo wol;
1447
1448 if (!dev->ethtool_ops->set_wol)
1449 return -EOPNOTSUPP;
1450
1451 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1452 return -EFAULT;
1453
1454 return dev->ethtool_ops->set_wol(dev, &wol);
1455}
1456
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001457static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1458{
1459 struct ethtool_eee edata;
1460 int rc;
1461
1462 if (!dev->ethtool_ops->get_eee)
1463 return -EOPNOTSUPP;
1464
1465 memset(&edata, 0, sizeof(struct ethtool_eee));
1466 edata.cmd = ETHTOOL_GEEE;
1467 rc = dev->ethtool_ops->get_eee(dev, &edata);
1468
1469 if (rc)
1470 return rc;
1471
1472 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1473 return -EFAULT;
1474
1475 return 0;
1476}
1477
1478static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1479{
1480 struct ethtool_eee edata;
1481
1482 if (!dev->ethtool_ops->set_eee)
1483 return -EOPNOTSUPP;
1484
1485 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1486 return -EFAULT;
1487
1488 return dev->ethtool_ops->set_eee(dev, &edata);
1489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491static int ethtool_nway_reset(struct net_device *dev)
1492{
1493 if (!dev->ethtool_ops->nway_reset)
1494 return -EOPNOTSUPP;
1495
1496 return dev->ethtool_ops->nway_reset(dev);
1497}
1498
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001499static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1500{
1501 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1502
1503 if (!dev->ethtool_ops->get_link)
1504 return -EOPNOTSUPP;
1505
1506 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1507
1508 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1509 return -EFAULT;
1510 return 0;
1511}
1512
Ben Hutchings081d0942012-04-12 00:42:12 +00001513static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1514 int (*getter)(struct net_device *,
1515 struct ethtool_eeprom *, u8 *),
1516 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001519 void __user *userbuf = useraddr + sizeof(eeprom);
1520 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001522 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1525 return -EFAULT;
1526
1527 /* Check for wrap and zero */
1528 if (eeprom.offset + eeprom.len <= eeprom.offset)
1529 return -EINVAL;
1530
1531 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001532 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return -EINVAL;
1534
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001535 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 if (!data)
1537 return -ENOMEM;
1538
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001539 bytes_remaining = eeprom.len;
1540 while (bytes_remaining > 0) {
1541 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Ben Hutchings081d0942012-04-12 00:42:12 +00001543 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001544 if (ret)
1545 break;
1546 if (copy_to_user(userbuf, data, eeprom.len)) {
1547 ret = -EFAULT;
1548 break;
1549 }
1550 userbuf += eeprom.len;
1551 eeprom.offset += eeprom.len;
1552 bytes_remaining -= eeprom.len;
1553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001555 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1556 eeprom.offset -= eeprom.len;
1557 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1558 ret = -EFAULT;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 kfree(data);
1561 return ret;
1562}
1563
Ben Hutchings081d0942012-04-12 00:42:12 +00001564static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1565{
1566 const struct ethtool_ops *ops = dev->ethtool_ops;
1567
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001568 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1569 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001570 return -EOPNOTSUPP;
1571
1572 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1573 ops->get_eeprom_len(dev));
1574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1577{
1578 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001579 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001580 void __user *userbuf = useraddr + sizeof(eeprom);
1581 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001583 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001585 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1586 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 return -EOPNOTSUPP;
1588
1589 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1590 return -EFAULT;
1591
1592 /* Check for wrap and zero */
1593 if (eeprom.offset + eeprom.len <= eeprom.offset)
1594 return -EINVAL;
1595
1596 /* Check for exceeding total eeprom len */
1597 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1598 return -EINVAL;
1599
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001600 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!data)
1602 return -ENOMEM;
1603
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001604 bytes_remaining = eeprom.len;
1605 while (bytes_remaining > 0) {
1606 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001608 if (copy_from_user(data, userbuf, eeprom.len)) {
1609 ret = -EFAULT;
1610 break;
1611 }
1612 ret = ops->set_eeprom(dev, &eeprom, data);
1613 if (ret)
1614 break;
1615 userbuf += eeprom.len;
1616 eeprom.offset += eeprom.len;
1617 bytes_remaining -= eeprom.len;
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 kfree(data);
1621 return ret;
1622}
1623
chavey97f8aef2010-04-07 21:54:42 -07001624static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1625 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Roland Dreier8e557422010-02-11 12:14:23 -08001627 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 if (!dev->ethtool_ops->get_coalesce)
1630 return -EOPNOTSUPP;
1631
1632 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1633
1634 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1635 return -EFAULT;
1636 return 0;
1637}
1638
chavey97f8aef2010-04-07 21:54:42 -07001639static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1640 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
1642 struct ethtool_coalesce coalesce;
1643
David S. Millerfa04ae52005-06-06 15:07:19 -07001644 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return -EOPNOTSUPP;
1646
1647 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1648 return -EFAULT;
1649
1650 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1651}
1652
1653static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1654{
Roland Dreier8e557422010-02-11 12:14:23 -08001655 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 if (!dev->ethtool_ops->get_ringparam)
1658 return -EOPNOTSUPP;
1659
1660 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1661
1662 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1663 return -EFAULT;
1664 return 0;
1665}
1666
1667static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1668{
1669 struct ethtool_ringparam ringparam;
1670
1671 if (!dev->ethtool_ops->set_ringparam)
1672 return -EOPNOTSUPP;
1673
1674 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1675 return -EFAULT;
1676
1677 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1678}
1679
amit salecha8b5933c2011-04-07 01:58:42 +00001680static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1681 void __user *useraddr)
1682{
1683 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1684
1685 if (!dev->ethtool_ops->get_channels)
1686 return -EOPNOTSUPP;
1687
1688 dev->ethtool_ops->get_channels(dev, &channels);
1689
1690 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1691 return -EFAULT;
1692 return 0;
1693}
1694
1695static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1696 void __user *useraddr)
1697{
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001698 struct ethtool_channels channels, max;
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001699 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001700
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001701 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001702 return -EOPNOTSUPP;
1703
1704 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1705 return -EFAULT;
1706
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001707 dev->ethtool_ops->get_channels(dev, &max);
1708
1709 /* ensure new counts are within the maximums */
1710 if ((channels.rx_count > max.max_rx) ||
1711 (channels.tx_count > max.max_tx) ||
1712 (channels.combined_count > max.max_combined) ||
1713 (channels.other_count > max.max_other))
1714 return -EINVAL;
1715
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001716 /* ensure the new Rx count fits within the configured Rx flow
1717 * indirection table settings */
1718 if (netif_is_rxfh_configured(dev) &&
1719 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1720 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1721 return -EINVAL;
1722
amit salecha8b5933c2011-04-07 01:58:42 +00001723 return dev->ethtool_ops->set_channels(dev, &channels);
1724}
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1727{
1728 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1729
1730 if (!dev->ethtool_ops->get_pauseparam)
1731 return -EOPNOTSUPP;
1732
1733 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1734
1735 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1736 return -EFAULT;
1737 return 0;
1738}
1739
1740static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1741{
1742 struct ethtool_pauseparam pauseparam;
1743
Jeff Garzike1b90c42006-07-17 12:54:40 -04001744 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return -EOPNOTSUPP;
1746
1747 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1748 return -EFAULT;
1749
1750 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1751}
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1754{
1755 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001756 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001758 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001760 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001761 return -EOPNOTSUPP;
1762
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001763 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001764 if (test_len < 0)
1765 return test_len;
1766 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 if (copy_from_user(&test, useraddr, sizeof(test)))
1769 return -EFAULT;
1770
Jeff Garzikff03d492007-08-15 16:01:08 -07001771 test.len = test_len;
1772 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (!data)
1774 return -ENOMEM;
1775
1776 ops->self_test(dev, &test, data);
1777
1778 ret = -EFAULT;
1779 if (copy_to_user(useraddr, &test, sizeof(test)))
1780 goto out;
1781 useraddr += sizeof(test);
1782 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1783 goto out;
1784 ret = 0;
1785
1786 out:
1787 kfree(data);
1788 return ret;
1789}
1790
1791static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1792{
1793 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 u8 *data;
1795 int ret;
1796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1798 return -EFAULT;
1799
Michał Mirosław340ae162011-02-15 16:59:16 +00001800 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001801 if (ret < 0)
1802 return ret;
Jeff Garzikff03d492007-08-15 16:01:08 -07001803
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001804 gstrings.len = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Joe Perches077cb372015-10-14 01:09:40 -07001806 data = kcalloc(gstrings.len, ETH_GSTRING_LEN, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (!data)
1808 return -ENOMEM;
1809
Michał Mirosław340ae162011-02-15 16:59:16 +00001810 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 ret = -EFAULT;
1813 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1814 goto out;
1815 useraddr += sizeof(gstrings);
1816 if (copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
1817 goto out;
1818 ret = 0;
1819
Michał Mirosław340ae162011-02-15 16:59:16 +00001820out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 kfree(data);
1822 return ret;
1823}
1824
1825static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1826{
1827 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001828 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001829 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001830 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001832 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -EOPNOTSUPP;
1834
Ben Hutchings68f512f2011-04-02 00:35:15 +01001835 if (busy)
1836 return -EBUSY;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (copy_from_user(&id, useraddr, sizeof(id)))
1839 return -EFAULT;
1840
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001841 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001842 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001843 return rc;
1844
1845 /* Drop the RTNL lock while waiting, but prevent reentry or
1846 * removal of the device.
1847 */
1848 busy = true;
1849 dev_hold(dev);
1850 rtnl_unlock();
1851
1852 if (rc == 0) {
1853 /* Driver will handle this itself */
1854 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001855 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001856 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001857 /* Driver expects to be called at twice the frequency in rc */
1858 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001859
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001860 /* Count down seconds */
1861 do {
1862 /* Count down iterations per second */
1863 i = n;
1864 do {
1865 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001866 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001867 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1868 rtnl_unlock();
1869 if (rc)
1870 break;
1871 schedule_timeout_interruptible(interval);
1872 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001873 } while (!signal_pending(current) &&
1874 (id.data == 0 || --id.data != 0));
1875 }
1876
1877 rtnl_lock();
1878 dev_put(dev);
1879 busy = false;
1880
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001881 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001882 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
1885static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1886{
1887 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001888 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001890 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001892 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001893 return -EOPNOTSUPP;
1894
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001895 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001896 if (n_stats < 0)
1897 return n_stats;
1898 WARN_ON(n_stats == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1901 return -EFAULT;
1902
Jeff Garzikff03d492007-08-15 16:01:08 -07001903 stats.n_stats = n_stats;
1904 data = kmalloc(n_stats * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (!data)
1906 return -ENOMEM;
1907
1908 ops->get_ethtool_stats(dev, &stats, data);
1909
1910 ret = -EFAULT;
1911 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1912 goto out;
1913 useraddr += sizeof(stats);
1914 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1915 goto out;
1916 ret = 0;
1917
1918 out:
1919 kfree(data);
1920 return ret;
1921}
1922
Andrew Lunnf3a40942015-12-30 16:28:25 +01001923static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1924{
1925 struct ethtool_stats stats;
1926 struct phy_device *phydev = dev->phydev;
1927 u64 *data;
1928 int ret, n_stats;
1929
1930 if (!phydev)
1931 return -EOPNOTSUPP;
1932
1933 n_stats = phy_get_sset_count(phydev);
1934
1935 if (n_stats < 0)
1936 return n_stats;
1937 WARN_ON(n_stats == 0);
1938
1939 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1940 return -EFAULT;
1941
1942 stats.n_stats = n_stats;
1943 data = kmalloc_array(n_stats, sizeof(u64), GFP_USER);
1944 if (!data)
1945 return -ENOMEM;
1946
1947 mutex_lock(&phydev->lock);
1948 phydev->drv->get_stats(phydev, &stats, data);
1949 mutex_unlock(&phydev->lock);
1950
1951 ret = -EFAULT;
1952 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1953 goto out;
1954 useraddr += sizeof(stats);
1955 if (copy_to_user(useraddr, data, stats.n_stats * sizeof(u64)))
1956 goto out;
1957 ret = 0;
1958
1959 out:
1960 kfree(data);
1961 return ret;
1962}
1963
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001964static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001965{
1966 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001967
Matthew Wilcox313674a2007-07-31 14:00:29 -07001968 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001969 return -EFAULT;
1970
Matthew Wilcox313674a2007-07-31 14:00:29 -07001971 if (epaddr.size < dev->addr_len)
1972 return -ETOOSMALL;
1973 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001974
Jon Wetzela6f9a702005-08-20 17:15:54 -07001975 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07001976 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001977 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07001978 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
1979 return -EFAULT;
1980 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001981}
1982
Jeff Garzik13c99b22007-08-15 16:01:56 -07001983static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
1984 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001985{
Roland Dreier8e557422010-02-11 12:14:23 -08001986 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001987
Jeff Garzik13c99b22007-08-15 16:01:56 -07001988 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001989 return -EOPNOTSUPP;
1990
Jeff Garzik13c99b22007-08-15 16:01:56 -07001991 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07001992
1993 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1994 return -EFAULT;
1995 return 0;
1996}
1997
Jeff Garzik13c99b22007-08-15 16:01:56 -07001998static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
1999 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002000{
2001 struct ethtool_value edata;
2002
Jeff Garzik13c99b22007-08-15 16:01:56 -07002003 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002004 return -EOPNOTSUPP;
2005
2006 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2007 return -EFAULT;
2008
Jeff Garzik13c99b22007-08-15 16:01:56 -07002009 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002010 return 0;
2011}
2012
Jeff Garzik13c99b22007-08-15 16:01:56 -07002013static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2014 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002015{
2016 struct ethtool_value edata;
2017
Jeff Garzik13c99b22007-08-15 16:01:56 -07002018 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002019 return -EOPNOTSUPP;
2020
2021 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2022 return -EFAULT;
2023
Jeff Garzik13c99b22007-08-15 16:01:56 -07002024 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002025}
2026
chavey97f8aef2010-04-07 21:54:42 -07002027static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2028 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002029{
2030 struct ethtool_flash efl;
2031
2032 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2033 return -EFAULT;
2034
2035 if (!dev->ethtool_ops->flash_device)
2036 return -EOPNOTSUPP;
2037
Ben Hutchings786f5282012-02-01 09:32:25 +00002038 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2039
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002040 return dev->ethtool_ops->flash_device(dev, &efl);
2041}
2042
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002043static int ethtool_set_dump(struct net_device *dev,
2044 void __user *useraddr)
2045{
2046 struct ethtool_dump dump;
2047
2048 if (!dev->ethtool_ops->set_dump)
2049 return -EOPNOTSUPP;
2050
2051 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2052 return -EFAULT;
2053
2054 return dev->ethtool_ops->set_dump(dev, &dump);
2055}
2056
2057static int ethtool_get_dump_flag(struct net_device *dev,
2058 void __user *useraddr)
2059{
2060 int ret;
2061 struct ethtool_dump dump;
2062 const struct ethtool_ops *ops = dev->ethtool_ops;
2063
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002064 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002065 return -EOPNOTSUPP;
2066
2067 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2068 return -EFAULT;
2069
2070 ret = ops->get_dump_flag(dev, &dump);
2071 if (ret)
2072 return ret;
2073
2074 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2075 return -EFAULT;
2076 return 0;
2077}
2078
2079static int ethtool_get_dump_data(struct net_device *dev,
2080 void __user *useraddr)
2081{
2082 int ret;
2083 __u32 len;
2084 struct ethtool_dump dump, tmp;
2085 const struct ethtool_ops *ops = dev->ethtool_ops;
2086 void *data = NULL;
2087
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002088 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002089 return -EOPNOTSUPP;
2090
2091 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2092 return -EFAULT;
2093
2094 memset(&tmp, 0, sizeof(tmp));
2095 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2096 ret = ops->get_dump_flag(dev, &tmp);
2097 if (ret)
2098 return ret;
2099
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002100 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002101 if (!len)
2102 return -EFAULT;
2103
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002104 /* Don't ever let the driver think there's more space available
2105 * than it requested with .get_dump_flag().
2106 */
2107 dump.len = len;
2108
2109 /* Always allocate enough space to hold the whole thing so that the
2110 * driver does not need to check the length and bother with partial
2111 * dumping.
2112 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002113 data = vzalloc(tmp.len);
2114 if (!data)
2115 return -ENOMEM;
2116 ret = ops->get_dump_data(dev, &dump, data);
2117 if (ret)
2118 goto out;
2119
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002120 /* There are two sane possibilities:
2121 * 1. The driver's .get_dump_data() does not touch dump.len.
2122 * 2. Or it may set dump.len to how much it really writes, which
2123 * should be tmp.len (or len if it can do a partial dump).
2124 * In any case respond to userspace with the actual length of data
2125 * it's receiving.
2126 */
2127 WARN_ON(dump.len != len && dump.len != tmp.len);
2128 dump.len = len;
2129
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002130 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2131 ret = -EFAULT;
2132 goto out;
2133 }
2134 useraddr += offsetof(struct ethtool_dump, data);
2135 if (copy_to_user(useraddr, data, len))
2136 ret = -EFAULT;
2137out:
2138 vfree(data);
2139 return ret;
2140}
2141
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002142static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2143{
2144 int err = 0;
2145 struct ethtool_ts_info info;
2146 const struct ethtool_ops *ops = dev->ethtool_ops;
2147 struct phy_device *phydev = dev->phydev;
2148
2149 memset(&info, 0, sizeof(info));
2150 info.cmd = ETHTOOL_GET_TS_INFO;
2151
2152 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002153 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002154 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002155 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002156 } else {
2157 info.so_timestamping =
2158 SOF_TIMESTAMPING_RX_SOFTWARE |
2159 SOF_TIMESTAMPING_SOFTWARE;
2160 info.phc_index = -1;
2161 }
2162
2163 if (err)
2164 return err;
2165
2166 if (copy_to_user(useraddr, &info, sizeof(info)))
2167 err = -EFAULT;
2168
2169 return err;
2170}
2171
Ed Swierk2f438362015-01-02 17:27:56 -08002172static int __ethtool_get_module_info(struct net_device *dev,
2173 struct ethtool_modinfo *modinfo)
2174{
2175 const struct ethtool_ops *ops = dev->ethtool_ops;
2176 struct phy_device *phydev = dev->phydev;
2177
2178 if (phydev && phydev->drv && phydev->drv->module_info)
2179 return phydev->drv->module_info(phydev, modinfo);
2180
2181 if (ops->get_module_info)
2182 return ops->get_module_info(dev, modinfo);
2183
2184 return -EOPNOTSUPP;
2185}
2186
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002187static int ethtool_get_module_info(struct net_device *dev,
2188 void __user *useraddr)
2189{
2190 int ret;
2191 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002192
2193 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2194 return -EFAULT;
2195
Ed Swierk2f438362015-01-02 17:27:56 -08002196 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002197 if (ret)
2198 return ret;
2199
2200 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2201 return -EFAULT;
2202
2203 return 0;
2204}
2205
Ed Swierk2f438362015-01-02 17:27:56 -08002206static int __ethtool_get_module_eeprom(struct net_device *dev,
2207 struct ethtool_eeprom *ee, u8 *data)
2208{
2209 const struct ethtool_ops *ops = dev->ethtool_ops;
2210 struct phy_device *phydev = dev->phydev;
2211
2212 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2213 return phydev->drv->module_eeprom(phydev, ee, data);
2214
2215 if (ops->get_module_eeprom)
2216 return ops->get_module_eeprom(dev, ee, data);
2217
2218 return -EOPNOTSUPP;
2219}
2220
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002221static int ethtool_get_module_eeprom(struct net_device *dev,
2222 void __user *useraddr)
2223{
2224 int ret;
2225 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002226
Ed Swierk2f438362015-01-02 17:27:56 -08002227 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002228 if (ret)
2229 return ret;
2230
Ed Swierk2f438362015-01-02 17:27:56 -08002231 return ethtool_get_any_eeprom(dev, useraddr,
2232 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002233 modinfo.eeprom_len);
2234}
2235
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302236static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2237{
2238 switch (tuna->id) {
2239 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002240 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302241 if (tuna->len != sizeof(u32) ||
2242 tuna->type_id != ETHTOOL_TUNABLE_U32)
2243 return -EINVAL;
2244 break;
2245 default:
2246 return -EINVAL;
2247 }
2248
2249 return 0;
2250}
2251
2252static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2253{
2254 int ret;
2255 struct ethtool_tunable tuna;
2256 const struct ethtool_ops *ops = dev->ethtool_ops;
2257 void *data;
2258
2259 if (!ops->get_tunable)
2260 return -EOPNOTSUPP;
2261 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2262 return -EFAULT;
2263 ret = ethtool_tunable_valid(&tuna);
2264 if (ret)
2265 return ret;
2266 data = kmalloc(tuna.len, GFP_USER);
2267 if (!data)
2268 return -ENOMEM;
2269 ret = ops->get_tunable(dev, &tuna, data);
2270 if (ret)
2271 goto out;
2272 useraddr += sizeof(tuna);
2273 ret = -EFAULT;
2274 if (copy_to_user(useraddr, data, tuna.len))
2275 goto out;
2276 ret = 0;
2277
2278out:
2279 kfree(data);
2280 return ret;
2281}
2282
2283static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2284{
2285 int ret;
2286 struct ethtool_tunable tuna;
2287 const struct ethtool_ops *ops = dev->ethtool_ops;
2288 void *data;
2289
2290 if (!ops->set_tunable)
2291 return -EOPNOTSUPP;
2292 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2293 return -EFAULT;
2294 ret = ethtool_tunable_valid(&tuna);
2295 if (ret)
2296 return ret;
2297 data = kmalloc(tuna.len, GFP_USER);
2298 if (!data)
2299 return -ENOMEM;
2300 useraddr += sizeof(tuna);
2301 ret = -EFAULT;
2302 if (copy_from_user(data, useraddr, tuna.len))
2303 goto out;
2304 ret = ops->set_tunable(dev, &tuna, data);
2305
2306out:
2307 kfree(data);
2308 return ret;
2309}
2310
Kan Liang421797b2016-02-19 09:24:02 -05002311static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2312 void __user *useraddr,
2313 struct ethtool_per_queue_op *per_queue_opt)
2314{
2315 u32 bit;
2316 int ret;
2317 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2318
2319 if (!dev->ethtool_ops->get_per_queue_coalesce)
2320 return -EOPNOTSUPP;
2321
2322 useraddr += sizeof(*per_queue_opt);
2323
2324 bitmap_from_u32array(queue_mask,
2325 MAX_NUM_QUEUE,
2326 per_queue_opt->queue_mask,
2327 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2328
2329 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2330 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2331
2332 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2333 if (ret != 0)
2334 return ret;
2335 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2336 return -EFAULT;
2337 useraddr += sizeof(coalesce);
2338 }
2339
2340 return 0;
2341}
2342
Kan Liangf38d1382016-02-19 09:24:03 -05002343static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2344 void __user *useraddr,
2345 struct ethtool_per_queue_op *per_queue_opt)
2346{
2347 u32 bit;
2348 int i, ret = 0;
2349 int n_queue;
2350 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2351 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2352
2353 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2354 (!dev->ethtool_ops->get_per_queue_coalesce))
2355 return -EOPNOTSUPP;
2356
2357 useraddr += sizeof(*per_queue_opt);
2358
2359 bitmap_from_u32array(queue_mask,
2360 MAX_NUM_QUEUE,
2361 per_queue_opt->queue_mask,
2362 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2363 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2364 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2365 if (!backup)
2366 return -ENOMEM;
2367
2368 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2369 struct ethtool_coalesce coalesce;
2370
2371 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2372 if (ret != 0)
2373 goto roll_back;
2374
2375 tmp++;
2376
2377 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2378 ret = -EFAULT;
2379 goto roll_back;
2380 }
2381
2382 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2383 if (ret != 0)
2384 goto roll_back;
2385
2386 useraddr += sizeof(coalesce);
2387 }
2388
2389roll_back:
2390 if (ret != 0) {
2391 tmp = backup;
2392 for_each_set_bit(i, queue_mask, bit) {
2393 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2394 tmp++;
2395 }
2396 }
2397 kfree(backup);
2398
2399 return ret;
2400}
2401
Kan Liangac2c7ad2016-02-19 09:24:01 -05002402static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2403{
2404 struct ethtool_per_queue_op per_queue_opt;
2405
2406 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2407 return -EFAULT;
2408
2409 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002410 case ETHTOOL_GCOALESCE:
2411 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002412 case ETHTOOL_SCOALESCE:
2413 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002414 default:
2415 return -EOPNOTSUPP;
2416 };
2417}
2418
Yan Burman600fed52013-06-03 02:03:34 +00002419/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Eric W. Biederman881d9662007-09-17 11:56:21 -07002421int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002423 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002425 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002427 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if (!dev || !netif_device_present(dev))
2430 return -ENODEV;
2431
chavey97f8aef2010-04-07 21:54:42 -07002432 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return -EFAULT;
2434
Kan Liangac2c7ad2016-02-19 09:24:01 -05002435 if (ethcmd == ETHTOOL_PERQUEUE) {
2436 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2437 return -EFAULT;
2438 } else {
2439 sub_cmd = ethcmd;
2440 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002441 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002442 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002443 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002444 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002445 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002446 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002447 case ETHTOOL_GCOALESCE:
2448 case ETHTOOL_GRINGPARAM:
2449 case ETHTOOL_GPAUSEPARAM:
2450 case ETHTOOL_GRXCSUM:
2451 case ETHTOOL_GTXCSUM:
2452 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002453 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002454 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002455 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002456 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002457 case ETHTOOL_GTSO:
2458 case ETHTOOL_GPERMADDR:
2459 case ETHTOOL_GUFO:
2460 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002461 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002462 case ETHTOOL_GFLAGS:
2463 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002464 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002465 case ETHTOOL_GRXRINGS:
2466 case ETHTOOL_GRXCLSRLCNT:
2467 case ETHTOOL_GRXCLSRULE:
2468 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002469 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302470 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002471 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002472 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002473 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002474 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302475 case ETHTOOL_GTUNABLE:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002476 break;
2477 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002478 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002479 return -EPERM;
2480 }
2481
chavey97f8aef2010-04-07 21:54:42 -07002482 if (dev->ethtool_ops->begin) {
2483 rc = dev->ethtool_ops->begin(dev);
2484 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002486 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002487 old_features = dev->features;
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 switch (ethcmd) {
2490 case ETHTOOL_GSET:
2491 rc = ethtool_get_settings(dev, useraddr);
2492 break;
2493 case ETHTOOL_SSET:
2494 rc = ethtool_set_settings(dev, useraddr);
2495 break;
2496 case ETHTOOL_GDRVINFO:
2497 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 break;
2499 case ETHTOOL_GREGS:
2500 rc = ethtool_get_regs(dev, useraddr);
2501 break;
2502 case ETHTOOL_GWOL:
2503 rc = ethtool_get_wol(dev, useraddr);
2504 break;
2505 case ETHTOOL_SWOL:
2506 rc = ethtool_set_wol(dev, useraddr);
2507 break;
2508 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002509 rc = ethtool_get_value(dev, useraddr, ethcmd,
2510 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 break;
2512 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002513 rc = ethtool_set_value_void(dev, useraddr,
2514 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002516 case ETHTOOL_GEEE:
2517 rc = ethtool_get_eee(dev, useraddr);
2518 break;
2519 case ETHTOOL_SEEE:
2520 rc = ethtool_set_eee(dev, useraddr);
2521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 case ETHTOOL_NWAY_RST:
2523 rc = ethtool_nway_reset(dev);
2524 break;
2525 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002526 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 break;
2528 case ETHTOOL_GEEPROM:
2529 rc = ethtool_get_eeprom(dev, useraddr);
2530 break;
2531 case ETHTOOL_SEEPROM:
2532 rc = ethtool_set_eeprom(dev, useraddr);
2533 break;
2534 case ETHTOOL_GCOALESCE:
2535 rc = ethtool_get_coalesce(dev, useraddr);
2536 break;
2537 case ETHTOOL_SCOALESCE:
2538 rc = ethtool_set_coalesce(dev, useraddr);
2539 break;
2540 case ETHTOOL_GRINGPARAM:
2541 rc = ethtool_get_ringparam(dev, useraddr);
2542 break;
2543 case ETHTOOL_SRINGPARAM:
2544 rc = ethtool_set_ringparam(dev, useraddr);
2545 break;
2546 case ETHTOOL_GPAUSEPARAM:
2547 rc = ethtool_get_pauseparam(dev, useraddr);
2548 break;
2549 case ETHTOOL_SPAUSEPARAM:
2550 rc = ethtool_set_pauseparam(dev, useraddr);
2551 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 case ETHTOOL_TEST:
2553 rc = ethtool_self_test(dev, useraddr);
2554 break;
2555 case ETHTOOL_GSTRINGS:
2556 rc = ethtool_get_strings(dev, useraddr);
2557 break;
2558 case ETHTOOL_PHYS_ID:
2559 rc = ethtool_phys_id(dev, useraddr);
2560 break;
2561 case ETHTOOL_GSTATS:
2562 rc = ethtool_get_stats(dev, useraddr);
2563 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002564 case ETHTOOL_GPERMADDR:
2565 rc = ethtool_get_perm_addr(dev, useraddr);
2566 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002567 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002568 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002569 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002570 break;
2571 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002572 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002573 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002574 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002575 rc = ethtool_get_value(dev, useraddr, ethcmd,
2576 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002577 break;
2578 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002579 rc = ethtool_set_value(dev, useraddr,
2580 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002581 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002582 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002583 case ETHTOOL_GRXRINGS:
2584 case ETHTOOL_GRXCLSRLCNT:
2585 case ETHTOOL_GRXCLSRULE:
2586 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002587 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002588 break;
2589 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002590 case ETHTOOL_SRXCLSRLDEL:
2591 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002592 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002593 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002594 case ETHTOOL_FLASHDEV:
2595 rc = ethtool_flash_device(dev, useraddr);
2596 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002597 case ETHTOOL_RESET:
2598 rc = ethtool_reset(dev, useraddr);
2599 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002600 case ETHTOOL_GSSET_INFO:
2601 rc = ethtool_get_sset_info(dev, useraddr);
2602 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002603 case ETHTOOL_GRXFHINDIR:
2604 rc = ethtool_get_rxfh_indir(dev, useraddr);
2605 break;
2606 case ETHTOOL_SRXFHINDIR:
2607 rc = ethtool_set_rxfh_indir(dev, useraddr);
2608 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302609 case ETHTOOL_GRSSH:
2610 rc = ethtool_get_rxfh(dev, useraddr);
2611 break;
2612 case ETHTOOL_SRSSH:
2613 rc = ethtool_set_rxfh(dev, useraddr);
2614 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002615 case ETHTOOL_GFEATURES:
2616 rc = ethtool_get_features(dev, useraddr);
2617 break;
2618 case ETHTOOL_SFEATURES:
2619 rc = ethtool_set_features(dev, useraddr);
2620 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002621 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002622 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002623 case ETHTOOL_GSG:
2624 case ETHTOOL_GTSO:
2625 case ETHTOOL_GUFO:
2626 case ETHTOOL_GGSO:
2627 case ETHTOOL_GGRO:
2628 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2629 break;
2630 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002631 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002632 case ETHTOOL_SSG:
2633 case ETHTOOL_STSO:
2634 case ETHTOOL_SUFO:
2635 case ETHTOOL_SGSO:
2636 case ETHTOOL_SGRO:
2637 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2638 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002639 case ETHTOOL_GCHANNELS:
2640 rc = ethtool_get_channels(dev, useraddr);
2641 break;
2642 case ETHTOOL_SCHANNELS:
2643 rc = ethtool_set_channels(dev, useraddr);
2644 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002645 case ETHTOOL_SET_DUMP:
2646 rc = ethtool_set_dump(dev, useraddr);
2647 break;
2648 case ETHTOOL_GET_DUMP_FLAG:
2649 rc = ethtool_get_dump_flag(dev, useraddr);
2650 break;
2651 case ETHTOOL_GET_DUMP_DATA:
2652 rc = ethtool_get_dump_data(dev, useraddr);
2653 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002654 case ETHTOOL_GET_TS_INFO:
2655 rc = ethtool_get_ts_info(dev, useraddr);
2656 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002657 case ETHTOOL_GMODULEINFO:
2658 rc = ethtool_get_module_info(dev, useraddr);
2659 break;
2660 case ETHTOOL_GMODULEEEPROM:
2661 rc = ethtool_get_module_eeprom(dev, useraddr);
2662 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302663 case ETHTOOL_GTUNABLE:
2664 rc = ethtool_get_tunable(dev, useraddr);
2665 break;
2666 case ETHTOOL_STUNABLE:
2667 rc = ethtool_set_tunable(dev, useraddr);
2668 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002669 case ETHTOOL_GPHYSTATS:
2670 rc = ethtool_get_phy_stats(dev, useraddr);
2671 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002672 case ETHTOOL_PERQUEUE:
2673 rc = ethtool_set_per_queue(dev, useraddr);
2674 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002675 case ETHTOOL_GLINKSETTINGS:
2676 rc = ethtool_get_link_ksettings(dev, useraddr);
2677 break;
2678 case ETHTOOL_SLINKSETTINGS:
2679 rc = ethtool_set_link_ksettings(dev, useraddr);
2680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002682 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002684
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002685 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002687
2688 if (old_features != dev->features)
2689 netdev_features_change(dev);
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}