blob: 03111a2d6653b8f06427540eb5864b255af4ead3 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010027#include <linux/sched/signal.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",
Alexander Duyckcbc53e02016-04-10 21:44:51 -040082 [NETIF_F_TSO_MANGLEID_BIT] = "tx-tcp-mangleid-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000083 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
84 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
Pravin B Shelar68c33162013-02-14 14:02:41 +000085 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040086 [NETIF_F_GSO_GRE_CSUM_BIT] = "tx-gre-csum-segmentation",
Tom Herbert7e133182016-05-18 09:06:10 -070087 [NETIF_F_GSO_IPXIP4_BIT] = "tx-ipxip4-segmentation",
88 [NETIF_F_GSO_IPXIP6_BIT] = "tx-ipxip6-segmentation",
Pravin B Shelar73136262013-03-07 13:21:51 +000089 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
Alexander Duyck518f2132016-04-10 21:44:44 -040090 [NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
Alexander Duyck802ab552016-04-10 21:45:03 -040091 [NETIF_F_GSO_PARTIAL_BIT] = "tx-gso-partial",
Marcelo Ricardo Leitner90017ac2016-06-02 15:05:43 -030092 [NETIF_F_GSO_SCTP_BIT] = "tx-sctp-segmentation",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +020093 [NETIF_F_GSO_ESP_BIT] = "tx-esp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000094
95 [NETIF_F_FCOE_CRC_BIT] = "tx-checksum-fcoe-crc",
Tom Herbert53692b12015-12-14 11:19:41 -080096 [NETIF_F_SCTP_CRC_BIT] = "tx-checksum-sctp",
Michał Mirosław9d921542011-11-15 15:29:55 +000097 [NETIF_F_FCOE_MTU_BIT] = "fcoe-mtu",
98 [NETIF_F_NTUPLE_BIT] = "rx-ntuple-filter",
99 [NETIF_F_RXHASH_BIT] = "rx-hashing",
100 [NETIF_F_RXCSUM_BIT] = "rx-checksum",
101 [NETIF_F_NOCACHE_COPY_BIT] = "tx-nocache-copy",
102 [NETIF_F_LOOPBACK_BIT] = "loopback",
Ben Greear36eabda32012-02-11 15:39:14 +0000103 [NETIF_F_RXFCS_BIT] = "rx-fcs",
Ben Greear5e0c03c2012-02-11 15:39:45 +0000104 [NETIF_F_RXALL_BIT] = "rx-all",
John Fastabenda6cc0cf2013-11-06 09:54:46 -0800105 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
John Fastabend1c78c642016-02-16 21:17:37 -0800106 [NETIF_F_HW_TC_BIT] = "hw-tc-offload",
Steffen Klassertc7ef8f02017-04-14 10:05:36 +0200107 [NETIF_F_HW_ESP_BIT] = "esp-hw-offload",
108 [NETIF_F_HW_ESP_TX_CSUM_BIT] = "esp-tx-csum-hw-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000109};
110
Eyal Perry892311f2014-12-02 18:12:10 +0200111static const char
112rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
113 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
114 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800115 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200116};
117
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300118static const char
119tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
120 [ETHTOOL_ID_UNSPEC] = "Unspec",
121 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
122 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
123};
124
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100125static const char
126phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
127 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100128 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100129};
130
Michał Mirosław5455c692011-02-15 16:59:17 +0000131static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
132{
133 struct ethtool_gfeatures cmd = {
134 .cmd = ETHTOOL_GFEATURES,
135 .size = ETHTOOL_DEV_FEATURE_WORDS,
136 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000137 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000138 u32 __user *sizeaddr;
139 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000140 int i;
141
142 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000143 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000144
145 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000146 features[i].available = (u32)(dev->hw_features >> (32 * i));
147 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
148 features[i].active = (u32)(dev->features >> (32 * i));
149 features[i].never_changed =
150 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000151 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000152
153 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
154 if (get_user(copy_size, sizeaddr))
155 return -EFAULT;
156
157 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
158 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
159
160 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
161 return -EFAULT;
162 useraddr += sizeof(cmd);
163 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
164 return -EFAULT;
165
166 return 0;
167}
168
169static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
170{
171 struct ethtool_sfeatures cmd;
172 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000173 netdev_features_t wanted = 0, valid = 0;
174 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000175
176 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
177 return -EFAULT;
178 useraddr += sizeof(cmd);
179
180 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
181 return -EINVAL;
182
183 if (copy_from_user(features, useraddr, sizeof(features)))
184 return -EFAULT;
185
Michał Mirosław475414f2011-11-15 15:29:55 +0000186 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000187 valid |= (netdev_features_t)features[i].valid << (32 * i);
188 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000189 }
190
191 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000192 return -EINVAL;
193
Michał Mirosław475414f2011-11-15 15:29:55 +0000194 if (valid & ~dev->hw_features) {
195 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000196 ret |= ETHTOOL_F_UNSUPPORTED;
197 }
198
Michał Mirosław475414f2011-11-15 15:29:55 +0000199 dev->wanted_features &= ~valid;
200 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700201 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000202
Michał Mirosław475414f2011-11-15 15:29:55 +0000203 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000204 ret |= ETHTOOL_F_WISH;
205
206 return ret;
207}
208
Andrew Lunnf3a40942015-12-30 16:28:25 +0100209static int phy_get_sset_count(struct phy_device *phydev)
210{
211 int ret;
212
213 if (phydev->drv->get_sset_count &&
214 phydev->drv->get_strings &&
215 phydev->drv->get_stats) {
216 mutex_lock(&phydev->lock);
217 ret = phydev->drv->get_sset_count(phydev);
218 mutex_unlock(&phydev->lock);
219
220 return ret;
221 }
222
223 return -EOPNOTSUPP;
224}
225
Michał Mirosław340ae162011-02-15 16:59:16 +0000226static int __ethtool_get_sset_count(struct net_device *dev, int sset)
227{
228 const struct ethtool_ops *ops = dev->ethtool_ops;
229
Michał Mirosław5455c692011-02-15 16:59:17 +0000230 if (sset == ETH_SS_FEATURES)
231 return ARRAY_SIZE(netdev_features_strings);
232
Eyal Perry892311f2014-12-02 18:12:10 +0200233 if (sset == ETH_SS_RSS_HASH_FUNCS)
234 return ARRAY_SIZE(rss_hash_func_strings);
235
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300236 if (sset == ETH_SS_TUNABLES)
237 return ARRAY_SIZE(tunable_strings);
238
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100239 if (sset == ETH_SS_PHY_TUNABLES)
240 return ARRAY_SIZE(phy_tunable_strings);
241
Andrew Lunnf3a40942015-12-30 16:28:25 +0100242 if (sset == ETH_SS_PHY_STATS) {
243 if (dev->phydev)
244 return phy_get_sset_count(dev->phydev);
245 else
246 return -EOPNOTSUPP;
247 }
248
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000249 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000250 return ops->get_sset_count(dev, sset);
251 else
252 return -EOPNOTSUPP;
253}
254
255static void __ethtool_get_strings(struct net_device *dev,
256 u32 stringset, u8 *data)
257{
258 const struct ethtool_ops *ops = dev->ethtool_ops;
259
Michał Mirosław5455c692011-02-15 16:59:17 +0000260 if (stringset == ETH_SS_FEATURES)
261 memcpy(data, netdev_features_strings,
262 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200263 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
264 memcpy(data, rss_hash_func_strings,
265 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300266 else if (stringset == ETH_SS_TUNABLES)
267 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100268 else if (stringset == ETH_SS_PHY_TUNABLES)
269 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100270 else if (stringset == ETH_SS_PHY_STATS) {
271 struct phy_device *phydev = dev->phydev;
272
273 if (phydev) {
274 mutex_lock(&phydev->lock);
275 phydev->drv->get_strings(phydev, data);
276 mutex_unlock(&phydev->lock);
277 } else {
278 return;
279 }
280 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000281 /* ops->get_strings is valid because checked earlier */
282 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000283}
284
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000285static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000286{
287 /* feature masks of legacy discrete ethtool ops */
288
289 switch (eth_cmd) {
290 case ETHTOOL_GTXCSUM:
291 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800292 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000293 case ETHTOOL_GRXCSUM:
294 case ETHTOOL_SRXCSUM:
295 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000296 case ETHTOOL_GSG:
297 case ETHTOOL_SSG:
298 return NETIF_F_SG;
299 case ETHTOOL_GTSO:
300 case ETHTOOL_STSO:
301 return NETIF_F_ALL_TSO;
302 case ETHTOOL_GUFO:
303 case ETHTOOL_SUFO:
304 return NETIF_F_UFO;
305 case ETHTOOL_GGSO:
306 case ETHTOOL_SGSO:
307 return NETIF_F_GSO;
308 case ETHTOOL_GGRO:
309 case ETHTOOL_SGRO:
310 return NETIF_F_GRO;
311 default:
312 BUG();
313 }
314}
315
Michał Mirosław0a417702011-02-15 16:59:17 +0000316static int ethtool_get_one_feature(struct net_device *dev,
317 char __user *useraddr, u32 ethcmd)
318{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000319 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000320 struct ethtool_value edata = {
321 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000322 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000323 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000324
Michał Mirosław0a417702011-02-15 16:59:17 +0000325 if (copy_to_user(useraddr, &edata, sizeof(edata)))
326 return -EFAULT;
327 return 0;
328}
329
Michał Mirosław0a417702011-02-15 16:59:17 +0000330static int ethtool_set_one_feature(struct net_device *dev,
331 void __user *useraddr, u32 ethcmd)
332{
333 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000334 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000335
336 if (copy_from_user(&edata, useraddr, sizeof(edata)))
337 return -EFAULT;
338
Michał Mirosław86794882011-02-15 16:59:17 +0000339 mask = ethtool_get_feature_mask(ethcmd);
340 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000341 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000342 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000343
344 if (edata.data)
345 dev->wanted_features |= mask;
346 else
347 dev->wanted_features &= ~mask;
348
349 __netdev_update_features(dev);
350
351 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000352}
353
Michał Mirosław02b3a552011-11-15 15:29:55 +0000354#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
355 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000356#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
357 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
358 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000359
360static u32 __ethtool_get_flags(struct net_device *dev)
361{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000362 u32 flags = 0;
363
Dragos Foianu8a731252013-07-13 14:43:00 +0100364 if (dev->features & NETIF_F_LRO)
365 flags |= ETH_FLAG_LRO;
366 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
367 flags |= ETH_FLAG_RXVLAN;
368 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
369 flags |= ETH_FLAG_TXVLAN;
370 if (dev->features & NETIF_F_NTUPLE)
371 flags |= ETH_FLAG_NTUPLE;
372 if (dev->features & NETIF_F_RXHASH)
373 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000374
375 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000376}
377
378static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000379{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000380 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000381
Michał Mirosław02b3a552011-11-15 15:29:55 +0000382 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000383 return -EINVAL;
384
Dragos Foianu8a731252013-07-13 14:43:00 +0100385 if (data & ETH_FLAG_LRO)
386 features |= NETIF_F_LRO;
387 if (data & ETH_FLAG_RXVLAN)
388 features |= NETIF_F_HW_VLAN_CTAG_RX;
389 if (data & ETH_FLAG_TXVLAN)
390 features |= NETIF_F_HW_VLAN_CTAG_TX;
391 if (data & ETH_FLAG_NTUPLE)
392 features |= NETIF_F_NTUPLE;
393 if (data & ETH_FLAG_RXHASH)
394 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000395
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000396 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000397 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000398 if (changed & ~dev->hw_features)
399 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
400
401 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000402 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000403
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700404 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000405
406 return 0;
407}
408
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200409void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
410 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800411{
412 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
413 dst[0] = legacy_u32;
414}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200415EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800416
417/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200418bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
419 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800420{
421 bool retval = true;
422
423 /* TODO: following test will soon always be true */
424 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
425 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
426
427 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
428 bitmap_fill(ext, 32);
429 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
430 if (bitmap_intersects(ext, src,
431 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
432 /* src mask goes beyond bit 31 */
433 retval = false;
434 }
435 }
436 *legacy_u32 = src[0];
437 return retval;
438}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200439EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800440
441/* return false if legacy contained non-0 deprecated fields
442 * transceiver/maxtxpkt/maxrxpkt. rest of ksettings always updated
443 */
444static bool
445convert_legacy_settings_to_link_ksettings(
446 struct ethtool_link_ksettings *link_ksettings,
447 const struct ethtool_cmd *legacy_settings)
448{
449 bool retval = true;
450
451 memset(link_ksettings, 0, sizeof(*link_ksettings));
452
453 /* This is used to tell users that driver is still using these
454 * deprecated legacy fields, and they should not use
455 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
456 */
457 if (legacy_settings->transceiver ||
458 legacy_settings->maxtxpkt ||
459 legacy_settings->maxrxpkt)
460 retval = false;
461
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200462 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800463 link_ksettings->link_modes.supported,
464 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200465 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800466 link_ksettings->link_modes.advertising,
467 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200468 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800469 link_ksettings->link_modes.lp_advertising,
470 legacy_settings->lp_advertising);
471 link_ksettings->base.speed
472 = ethtool_cmd_speed(legacy_settings);
473 link_ksettings->base.duplex
474 = legacy_settings->duplex;
475 link_ksettings->base.port
476 = legacy_settings->port;
477 link_ksettings->base.phy_address
478 = legacy_settings->phy_address;
479 link_ksettings->base.autoneg
480 = legacy_settings->autoneg;
481 link_ksettings->base.mdio_support
482 = legacy_settings->mdio_support;
483 link_ksettings->base.eth_tp_mdix
484 = legacy_settings->eth_tp_mdix;
485 link_ksettings->base.eth_tp_mdix_ctrl
486 = legacy_settings->eth_tp_mdix_ctrl;
487 return retval;
488}
489
490/* return false if ksettings link modes had higher bits
491 * set. legacy_settings always updated (best effort)
492 */
493static bool
494convert_link_ksettings_to_legacy_settings(
495 struct ethtool_cmd *legacy_settings,
496 const struct ethtool_link_ksettings *link_ksettings)
497{
498 bool retval = true;
499
500 memset(legacy_settings, 0, sizeof(*legacy_settings));
501 /* this also clears the deprecated fields in legacy structure:
502 * __u8 transceiver;
503 * __u32 maxtxpkt;
504 * __u32 maxrxpkt;
505 */
506
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200507 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800508 &legacy_settings->supported,
509 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200510 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800511 &legacy_settings->advertising,
512 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200513 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800514 &legacy_settings->lp_advertising,
515 link_ksettings->link_modes.lp_advertising);
516 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
517 legacy_settings->duplex
518 = link_ksettings->base.duplex;
519 legacy_settings->port
520 = link_ksettings->base.port;
521 legacy_settings->phy_address
522 = link_ksettings->base.phy_address;
523 legacy_settings->autoneg
524 = link_ksettings->base.autoneg;
525 legacy_settings->mdio_support
526 = link_ksettings->base.mdio_support;
527 legacy_settings->eth_tp_mdix
528 = link_ksettings->base.eth_tp_mdix;
529 legacy_settings->eth_tp_mdix_ctrl
530 = link_ksettings->base.eth_tp_mdix_ctrl;
531 return retval;
532}
533
534/* number of 32-bit words to store the user's link mode bitmaps */
535#define __ETHTOOL_LINK_MODE_MASK_NU32 \
536 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
537
538/* layout of the struct passed from/to userland */
539struct ethtool_link_usettings {
540 struct ethtool_link_settings base;
541 struct {
542 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
543 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
544 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
545 } link_modes;
546};
547
548/* Internal kernel helper to query a device ethtool_link_settings.
549 *
550 * Backward compatibility note: for compatibility with legacy drivers
551 * that implement only the ethtool_cmd API, this has to work with both
552 * drivers implementing get_link_ksettings API and drivers
553 * implementing get_settings API. When drivers implement get_settings
554 * and report ethtool_cmd deprecated fields
555 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
556 * because the resulting struct ethtool_link_settings does not report them.
557 */
558int __ethtool_get_link_ksettings(struct net_device *dev,
559 struct ethtool_link_ksettings *link_ksettings)
560{
561 int err;
562 struct ethtool_cmd cmd;
563
564 ASSERT_RTNL();
565
566 if (dev->ethtool_ops->get_link_ksettings) {
567 memset(link_ksettings, 0, sizeof(*link_ksettings));
568 return dev->ethtool_ops->get_link_ksettings(dev,
569 link_ksettings);
570 }
571
572 /* driver doesn't support %ethtool_link_ksettings API. revert to
573 * legacy %ethtool_cmd API, unless it's not supported either.
574 * TODO: remove when ethtool_ops::get_settings disappears internally
575 */
David Decotigny3237fc62016-02-24 10:58:11 -0800576 if (!dev->ethtool_ops->get_settings)
577 return -EOPNOTSUPP;
578
579 memset(&cmd, 0, sizeof(cmd));
580 cmd.cmd = ETHTOOL_GSET;
581 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800582 if (err < 0)
583 return err;
584
585 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
586 */
587 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
588 return err;
589}
590EXPORT_SYMBOL(__ethtool_get_link_ksettings);
591
592/* convert ethtool_link_usettings in user space to a kernel internal
593 * ethtool_link_ksettings. return 0 on success, errno on error.
594 */
595static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
596 const void __user *from)
597{
598 struct ethtool_link_usettings link_usettings;
599
600 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
601 return -EFAULT;
602
603 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
604 bitmap_from_u32array(to->link_modes.supported,
605 __ETHTOOL_LINK_MODE_MASK_NBITS,
606 link_usettings.link_modes.supported,
607 __ETHTOOL_LINK_MODE_MASK_NU32);
608 bitmap_from_u32array(to->link_modes.advertising,
609 __ETHTOOL_LINK_MODE_MASK_NBITS,
610 link_usettings.link_modes.advertising,
611 __ETHTOOL_LINK_MODE_MASK_NU32);
612 bitmap_from_u32array(to->link_modes.lp_advertising,
613 __ETHTOOL_LINK_MODE_MASK_NBITS,
614 link_usettings.link_modes.lp_advertising,
615 __ETHTOOL_LINK_MODE_MASK_NU32);
616
617 return 0;
618}
619
620/* convert a kernel internal ethtool_link_ksettings to
621 * ethtool_link_usettings in user space. return 0 on success, errno on
622 * error.
623 */
624static int
625store_link_ksettings_for_user(void __user *to,
626 const struct ethtool_link_ksettings *from)
627{
628 struct ethtool_link_usettings link_usettings;
629
630 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
631 bitmap_to_u32array(link_usettings.link_modes.supported,
632 __ETHTOOL_LINK_MODE_MASK_NU32,
633 from->link_modes.supported,
634 __ETHTOOL_LINK_MODE_MASK_NBITS);
635 bitmap_to_u32array(link_usettings.link_modes.advertising,
636 __ETHTOOL_LINK_MODE_MASK_NU32,
637 from->link_modes.advertising,
638 __ETHTOOL_LINK_MODE_MASK_NBITS);
639 bitmap_to_u32array(link_usettings.link_modes.lp_advertising,
640 __ETHTOOL_LINK_MODE_MASK_NU32,
641 from->link_modes.lp_advertising,
642 __ETHTOOL_LINK_MODE_MASK_NBITS);
643
644 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
645 return -EFAULT;
646
647 return 0;
648}
649
650/* Query device for its ethtool_link_settings.
651 *
652 * Backward compatibility note: this function must fail when driver
653 * does not implement ethtool::get_link_ksettings, even if legacy
654 * ethtool_ops::get_settings is implemented. This tells new versions
655 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
656 * this driver, so that they can correctly access the ethtool_cmd
657 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
658 * implements ethtool_ops::get_settings anymore.
659 */
660static int ethtool_get_link_ksettings(struct net_device *dev,
661 void __user *useraddr)
662{
663 int err = 0;
664 struct ethtool_link_ksettings link_ksettings;
665
666 ASSERT_RTNL();
667
668 if (!dev->ethtool_ops->get_link_ksettings)
669 return -EOPNOTSUPP;
670
671 /* handle bitmap nbits handshake */
672 if (copy_from_user(&link_ksettings.base, useraddr,
673 sizeof(link_ksettings.base)))
674 return -EFAULT;
675
676 if (__ETHTOOL_LINK_MODE_MASK_NU32
677 != link_ksettings.base.link_mode_masks_nwords) {
678 /* wrong link mode nbits requested */
679 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000680 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800681 /* send back number of words required as negative val */
682 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
683 "need too many bits for link modes!");
684 link_ksettings.base.link_mode_masks_nwords
685 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
686
687 /* copy the base fields back to user, not the link
688 * mode bitmaps
689 */
690 if (copy_to_user(useraddr, &link_ksettings.base,
691 sizeof(link_ksettings.base)))
692 return -EFAULT;
693
694 return 0;
695 }
696
697 /* handshake successful: user/kernel agree on
698 * link_mode_masks_nwords
699 */
700
701 memset(&link_ksettings, 0, sizeof(link_ksettings));
702 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
703 if (err < 0)
704 return err;
705
706 /* make sure we tell the right values to user */
707 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
708 link_ksettings.base.link_mode_masks_nwords
709 = __ETHTOOL_LINK_MODE_MASK_NU32;
710
711 return store_link_ksettings_for_user(useraddr, &link_ksettings);
712}
713
714/* Update device ethtool_link_settings.
715 *
716 * Backward compatibility note: this function must fail when driver
717 * does not implement ethtool::set_link_ksettings, even if legacy
718 * ethtool_ops::set_settings is implemented. This tells new versions
719 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
720 * this driver, so that they can correctly update the ethtool_cmd
721 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
722 * implements ethtool_ops::get_settings anymore.
723 */
724static int ethtool_set_link_ksettings(struct net_device *dev,
725 void __user *useraddr)
726{
727 int err;
728 struct ethtool_link_ksettings link_ksettings;
729
730 ASSERT_RTNL();
731
732 if (!dev->ethtool_ops->set_link_ksettings)
733 return -EOPNOTSUPP;
734
735 /* make sure nbits field has expected value */
736 if (copy_from_user(&link_ksettings.base, useraddr,
737 sizeof(link_ksettings.base)))
738 return -EFAULT;
739
740 if (__ETHTOOL_LINK_MODE_MASK_NU32
741 != link_ksettings.base.link_mode_masks_nwords)
742 return -EINVAL;
743
744 /* copy the whole structure, now that we know it has expected
745 * format
746 */
747 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
748 if (err)
749 return err;
750
751 /* re-check nwords field, just in case */
752 if (__ETHTOOL_LINK_MODE_MASK_NU32
753 != link_ksettings.base.link_mode_masks_nwords)
754 return -EINVAL;
755
756 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
757}
758
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800759static void
760warn_incomplete_ethtool_legacy_settings_conversion(const char *details)
761{
762 char name[sizeof(current->comm)];
763
764 pr_info_once("warning: `%s' uses legacy ethtool link settings API, %s\n",
765 get_task_comm(name, current), details);
766}
767
768/* Query device for its ethtool_cmd settings.
769 *
770 * Backward compatibility note: for compatibility with legacy ethtool,
771 * this has to work with both drivers implementing get_link_ksettings
772 * API and drivers implementing get_settings API. When drivers
773 * implement get_link_ksettings and report higher link mode bits, a
774 * kernel warning is logged once (with name of 1st driver/device) to
775 * recommend user to upgrade ethtool, but the command is successful
776 * (only the lower link mode bits reported back to user).
777 */
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000778static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
779{
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000780 struct ethtool_cmd cmd;
781
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800782 ASSERT_RTNL();
783
784 if (dev->ethtool_ops->get_link_ksettings) {
785 /* First, use link_ksettings API if it is supported */
786 int err;
787 struct ethtool_link_ksettings link_ksettings;
788
789 memset(&link_ksettings, 0, sizeof(link_ksettings));
790 err = dev->ethtool_ops->get_link_ksettings(dev,
791 &link_ksettings);
792 if (err < 0)
793 return err;
794 if (!convert_link_ksettings_to_legacy_settings(&cmd,
795 &link_ksettings))
796 warn_incomplete_ethtool_legacy_settings_conversion(
797 "link modes are only partially reported");
798
799 /* send a sensible cmd tag back to user */
800 cmd.cmd = ETHTOOL_GSET;
801 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800802 /* driver doesn't support %ethtool_link_ksettings
803 * API. revert to legacy %ethtool_cmd API, unless it's
804 * not supported either.
805 */
David Decotigny3237fc62016-02-24 10:58:11 -0800806 int err;
807
808 if (!dev->ethtool_ops->get_settings)
809 return -EOPNOTSUPP;
810
811 memset(&cmd, 0, sizeof(cmd));
812 cmd.cmd = ETHTOOL_GSET;
813 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800814 if (err < 0)
815 return err;
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
819 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
822}
823
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800824/* Update device link settings with given ethtool_cmd.
825 *
826 * Backward compatibility note: for compatibility with legacy ethtool,
827 * this has to work with both drivers implementing set_link_ksettings
828 * API and drivers implementing set_settings API. When drivers
829 * implement set_link_ksettings and user's request updates deprecated
830 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
831 * warning is logged once (with name of 1st driver/device) to
832 * recommend user to upgrade ethtool, and the request is rejected.
833 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
835{
836 struct ethtool_cmd cmd;
837
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800838 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
841 return -EFAULT;
842
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800843 /* first, try new %ethtool_link_ksettings API. */
844 if (dev->ethtool_ops->set_link_ksettings) {
845 struct ethtool_link_ksettings link_ksettings;
846
847 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
848 &cmd))
849 return -EINVAL;
850
851 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
852 link_ksettings.base.link_mode_masks_nwords
853 = __ETHTOOL_LINK_MODE_MASK_NU32;
854 return dev->ethtool_ops->set_link_ksettings(dev,
855 &link_ksettings);
856 }
857
858 /* legacy %ethtool_cmd API */
859
860 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
861 * disappears internally
862 */
863
864 if (!dev->ethtool_ops->set_settings)
865 return -EOPNOTSUPP;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return dev->ethtool_ops->set_settings(dev, &cmd);
868}
869
chavey97f8aef2010-04-07 21:54:42 -0700870static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
871 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700874 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 memset(&info, 0, sizeof(info));
877 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000878 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700879 ops->get_drvinfo(dev, &info);
880 } else if (dev->dev.parent && dev->dev.parent->driver) {
881 strlcpy(info.bus_info, dev_name(dev->dev.parent),
882 sizeof(info.bus_info));
883 strlcpy(info.driver, dev->dev.parent->driver->name,
884 sizeof(info.driver));
885 } else {
886 return -EOPNOTSUPP;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Jeff Garzik723b2f52010-03-03 22:51:50 +0000889 /*
890 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000891 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000892 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000893 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700894 int rc;
895
896 rc = ops->get_sset_count(dev, ETH_SS_TEST);
897 if (rc >= 0)
898 info.testinfo_len = rc;
899 rc = ops->get_sset_count(dev, ETH_SS_STATS);
900 if (rc >= 0)
901 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700902 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
903 if (rc >= 0)
904 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700905 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000906 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000908 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 info.eedump_len = ops->get_eeprom_len(dev);
910
911 if (copy_to_user(useraddr, &info, sizeof(info)))
912 return -EFAULT;
913 return 0;
914}
915
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800916static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700917 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000918{
919 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000920 u64 sset_mask;
921 int i, idx = 0, n_bits = 0, ret, rc;
922 u32 *info_buf = NULL;
923
Jeff Garzik723b2f52010-03-03 22:51:50 +0000924 if (copy_from_user(&info, useraddr, sizeof(info)))
925 return -EFAULT;
926
927 /* store copy of mask, because we zero struct later on */
928 sset_mask = info.sset_mask;
929 if (!sset_mask)
930 return 0;
931
932 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000933 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000934
935 memset(&info, 0, sizeof(info));
936 info.cmd = ETHTOOL_GSSET_INFO;
937
938 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
939 if (!info_buf)
940 return -ENOMEM;
941
942 /*
943 * fill return buffer based on input bitmask and successful
944 * get_sset_count return
945 */
946 for (i = 0; i < 64; i++) {
947 if (!(sset_mask & (1ULL << i)))
948 continue;
949
Michał Mirosław340ae162011-02-15 16:59:16 +0000950 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000951 if (rc >= 0) {
952 info.sset_mask |= (1ULL << i);
953 info_buf[idx++] = rc;
954 }
955 }
956
957 ret = -EFAULT;
958 if (copy_to_user(useraddr, &info, sizeof(info)))
959 goto out;
960
961 useraddr += offsetof(struct ethtool_sset_info, data);
962 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
963 goto out;
964
965 ret = 0;
966
967out:
968 kfree(info_buf);
969 return ret;
970}
971
chavey97f8aef2010-04-07 21:54:42 -0700972static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000973 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700974{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000975 struct ethtool_rxnfc info;
976 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000977 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700978
Santwona Behera59089d82009-02-20 00:58:13 -0800979 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700980 return -EOPNOTSUPP;
981
Ben Hutchingsbf988432010-06-28 08:45:58 +0000982 /* struct ethtool_rxnfc was originally defined for
983 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
984 * members. User-space might still be using that
985 * definition. */
986 if (cmd == ETHTOOL_SRXFH)
987 info_size = (offsetof(struct ethtool_rxnfc, data) +
988 sizeof(info.data));
989
990 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700991 return -EFAULT;
992
Ben Hutchings55664f32012-01-03 12:04:51 +0000993 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
994 if (rc)
995 return rc;
996
997 if (cmd == ETHTOOL_SRXCLSRLINS &&
998 copy_to_user(useraddr, &info, info_size))
999 return -EFAULT;
1000
1001 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -07001002}
1003
chavey97f8aef2010-04-07 21:54:42 -07001004static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +00001005 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -07001006{
1007 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001008 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -08001009 const struct ethtool_ops *ops = dev->ethtool_ops;
1010 int ret;
1011 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -07001012
Santwona Behera59089d82009-02-20 00:58:13 -08001013 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -07001014 return -EOPNOTSUPP;
1015
Ben Hutchingsbf988432010-06-28 08:45:58 +00001016 /* struct ethtool_rxnfc was originally defined for
1017 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1018 * members. User-space might still be using that
1019 * definition. */
1020 if (cmd == ETHTOOL_GRXFH)
1021 info_size = (offsetof(struct ethtool_rxnfc, data) +
1022 sizeof(info.data));
1023
1024 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001025 return -EFAULT;
1026
Santwona Behera59089d82009-02-20 00:58:13 -08001027 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1028 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001029 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001030 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001031 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001032 if (!rule_buf)
1033 return -ENOMEM;
1034 }
1035 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001036
Santwona Behera59089d82009-02-20 00:58:13 -08001037 ret = ops->get_rxnfc(dev, &info, rule_buf);
1038 if (ret < 0)
1039 goto err_out;
1040
1041 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001042 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001043 goto err_out;
1044
1045 if (rule_buf) {
1046 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1047 if (copy_to_user(useraddr, rule_buf,
1048 info.rule_cnt * sizeof(u32)))
1049 goto err_out;
1050 }
1051 ret = 0;
1052
1053err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001054 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001055
1056 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001057}
1058
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301059static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1060 struct ethtool_rxnfc *rx_rings,
1061 u32 size)
1062{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001063 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301064
1065 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001066 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301067
1068 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001069 for (i = 0; i < size; i++)
1070 if (indir[i] >= rx_rings->data)
1071 return -EINVAL;
1072
1073 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301074}
1075
Kim Jonesba905f52016-02-02 03:51:16 +00001076u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001077
1078void netdev_rss_key_fill(void *buffer, size_t len)
1079{
1080 BUG_ON(len > sizeof(netdev_rss_key));
1081 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1082 memcpy(buffer, netdev_rss_key, len);
1083}
1084EXPORT_SYMBOL(netdev_rss_key_fill);
1085
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001086static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1087{
1088 u32 dev_size, current_max = 0;
1089 u32 *indir;
1090 int ret;
1091
1092 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1093 !dev->ethtool_ops->get_rxfh)
1094 return -EOPNOTSUPP;
1095 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1096 if (dev_size == 0)
1097 return -EOPNOTSUPP;
1098
1099 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1100 if (!indir)
1101 return -ENOMEM;
1102
1103 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1104 if (ret)
1105 goto out;
1106
1107 while (dev_size--)
1108 current_max = max(current_max, indir[dev_size]);
1109
1110 *max = current_max;
1111
1112out:
1113 kfree(indir);
1114 return ret;
1115}
1116
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001117static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1118 void __user *useraddr)
1119{
Ben Hutchings7850f632011-12-15 13:55:01 +00001120 u32 user_size, dev_size;
1121 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001122 int ret;
1123
Ben Hutchings7850f632011-12-15 13:55:01 +00001124 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001125 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001126 return -EOPNOTSUPP;
1127 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1128 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001129 return -EOPNOTSUPP;
1130
Ben Hutchings7850f632011-12-15 13:55:01 +00001131 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001132 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001133 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001134 return -EFAULT;
1135
Ben Hutchings7850f632011-12-15 13:55:01 +00001136 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1137 &dev_size, sizeof(dev_size)))
1138 return -EFAULT;
1139
1140 /* If the user buffer size is 0, this is just a query for the
1141 * device table size. Otherwise, if it's smaller than the
1142 * device table size it's an error.
1143 */
1144 if (user_size < dev_size)
1145 return user_size == 0 ? 0 : -EINVAL;
1146
1147 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001148 if (!indir)
1149 return -ENOMEM;
1150
Eyal Perry892311f2014-12-02 18:12:10 +02001151 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001152 if (ret)
1153 goto out;
1154
Ben Hutchings7850f632011-12-15 13:55:01 +00001155 if (copy_to_user(useraddr +
1156 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1157 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001158 ret = -EFAULT;
1159
1160out:
1161 kfree(indir);
1162 return ret;
1163}
1164
1165static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1166 void __user *useraddr)
1167{
Ben Hutchings7850f632011-12-15 13:55:01 +00001168 struct ethtool_rxnfc rx_rings;
1169 u32 user_size, dev_size, i;
1170 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001171 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001172 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301173 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001174
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001175 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001176 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001177 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001178
1179 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001180 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001181 return -EOPNOTSUPP;
1182
Ben Hutchings7850f632011-12-15 13:55:01 +00001183 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001184 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001185 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001186 return -EFAULT;
1187
Ben Hutchings278bc422011-12-15 13:56:49 +00001188 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001189 return -EINVAL;
1190
1191 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001192 if (!indir)
1193 return -ENOMEM;
1194
Ben Hutchings7850f632011-12-15 13:55:01 +00001195 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001196 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001197 if (ret)
1198 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001199
1200 if (user_size == 0) {
1201 for (i = 0; i < dev_size; i++)
1202 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1203 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301204 ret = ethtool_copy_validate_indir(indir,
1205 useraddr + ringidx_offset,
1206 &rx_rings,
1207 dev_size);
1208 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001209 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001210 }
1211
Eyal Perry892311f2014-12-02 18:12:10 +02001212 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001213 if (ret)
1214 goto out;
1215
1216 /* indicate whether rxfh was set to default */
1217 if (user_size == 0)
1218 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1219 else
1220 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001221
1222out:
1223 kfree(indir);
1224 return ret;
1225}
1226
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301227static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1228 void __user *useraddr)
1229{
1230 int ret;
1231 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001232 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301233 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001234 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301235 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001236 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301237 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001238 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301239 u8 *hkey = NULL;
1240 u8 *rss_config;
1241
Eyal Perry892311f2014-12-02 18:12:10 +02001242 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301243 return -EOPNOTSUPP;
1244
1245 if (ops->get_rxfh_indir_size)
1246 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301247 if (ops->get_rxfh_key_size)
1248 dev_key_size = ops->get_rxfh_key_size(dev);
1249
Ben Hutchingsf062a382014-05-15 16:28:07 +01001250 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301251 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001252 user_indir_size = rxfh.indir_size;
1253 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301254
Ben Hutchingsf062a382014-05-15 16:28:07 +01001255 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +02001256 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1257 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001258 return -EINVAL;
1259
1260 rxfh.indir_size = dev_indir_size;
1261 rxfh.key_size = dev_key_size;
1262 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301263 return -EFAULT;
1264
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301265 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1266 (user_key_size && (user_key_size != dev_key_size)))
1267 return -EINVAL;
1268
1269 indir_bytes = user_indir_size * sizeof(indir[0]);
1270 total_size = indir_bytes + user_key_size;
1271 rss_config = kzalloc(total_size, GFP_USER);
1272 if (!rss_config)
1273 return -ENOMEM;
1274
1275 if (user_indir_size)
1276 indir = (u32 *)rss_config;
1277
1278 if (user_key_size)
1279 hkey = rss_config + indir_bytes;
1280
Eyal Perry892311f2014-12-02 18:12:10 +02001281 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
1282 if (ret)
1283 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301284
Eyal Perry892311f2014-12-02 18:12:10 +02001285 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1286 &dev_hfunc, sizeof(rxfh.hfunc))) {
1287 ret = -EFAULT;
1288 } else if (copy_to_user(useraddr +
1289 offsetof(struct ethtool_rxfh, rss_config[0]),
1290 rss_config, total_size)) {
1291 ret = -EFAULT;
1292 }
1293out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301294 kfree(rss_config);
1295
1296 return ret;
1297}
1298
1299static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1300 void __user *useraddr)
1301{
1302 int ret;
1303 const struct ethtool_ops *ops = dev->ethtool_ops;
1304 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001305 struct ethtool_rxfh rxfh;
1306 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301307 u32 *indir = NULL, indir_bytes = 0;
1308 u8 *hkey = NULL;
1309 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301310 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
1311
Eyal Perry892311f2014-12-02 18:12:10 +02001312 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301313 return -EOPNOTSUPP;
1314
1315 if (ops->get_rxfh_indir_size)
1316 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301317 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001318 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301319
Ben Hutchingsf062a382014-05-15 16:28:07 +01001320 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301321 return -EFAULT;
1322
Ben Hutchingsf062a382014-05-15 16:28:07 +01001323 /* Check that reserved fields are 0 for now */
Eyal Perry892311f2014-12-02 18:12:10 +02001324 if (rxfh.rss_context || rxfh.rsvd8[0] || rxfh.rsvd8[1] ||
1325 rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001326 return -EINVAL;
1327
Eyal Perry892311f2014-12-02 18:12:10 +02001328 /* If either indir, hash key or function is valid, proceed further.
1329 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301330 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001331 if ((rxfh.indir_size &&
1332 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1333 rxfh.indir_size != dev_indir_size) ||
1334 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1335 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001336 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301337 return -EINVAL;
1338
Ben Hutchingsf062a382014-05-15 16:28:07 +01001339 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301340 indir_bytes = dev_indir_size * sizeof(indir[0]);
1341
Ben Hutchingsf062a382014-05-15 16:28:07 +01001342 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301343 if (!rss_config)
1344 return -ENOMEM;
1345
1346 rx_rings.cmd = ETHTOOL_GRXRINGS;
1347 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1348 if (ret)
1349 goto out;
1350
Ben Hutchingsf062a382014-05-15 16:28:07 +01001351 /* rxfh.indir_size == 0 means reset the indir table to default.
1352 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301353 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001354 if (rxfh.indir_size &&
1355 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301356 indir = (u32 *)rss_config;
1357 ret = ethtool_copy_validate_indir(indir,
1358 useraddr + rss_cfg_offset,
1359 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001360 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301361 if (ret)
1362 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001363 } else if (rxfh.indir_size == 0) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301364 indir = (u32 *)rss_config;
1365 for (i = 0; i < dev_indir_size; i++)
1366 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1367 }
1368
Ben Hutchingsf062a382014-05-15 16:28:07 +01001369 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301370 hkey = rss_config + indir_bytes;
1371 if (copy_from_user(hkey,
1372 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001373 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301374 ret = -EFAULT;
1375 goto out;
1376 }
1377 }
1378
Eyal Perry892311f2014-12-02 18:12:10 +02001379 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001380 if (ret)
1381 goto out;
1382
1383 /* indicate whether rxfh was set to default */
1384 if (rxfh.indir_size == 0)
1385 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1386 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1387 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301388
1389out:
1390 kfree(rss_config);
1391 return ret;
1392}
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1395{
1396 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001397 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 void *regbuf;
1399 int reglen, ret;
1400
1401 if (!ops->get_regs || !ops->get_regs_len)
1402 return -EOPNOTSUPP;
1403
1404 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1405 return -EFAULT;
1406
1407 reglen = ops->get_regs_len(dev);
1408 if (regs.len > reglen)
1409 regs.len = reglen;
1410
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001411 regbuf = NULL;
1412 if (reglen) {
1413 regbuf = vzalloc(reglen);
1414 if (!regbuf)
1415 return -ENOMEM;
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 ops->get_regs(dev, &regs, regbuf);
1419
1420 ret = -EFAULT;
1421 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1422 goto out;
1423 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001424 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 goto out;
1426 ret = 0;
1427
1428 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001429 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 return ret;
1431}
1432
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001433static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1434{
1435 struct ethtool_value reset;
1436 int ret;
1437
1438 if (!dev->ethtool_ops->reset)
1439 return -EOPNOTSUPP;
1440
1441 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1442 return -EFAULT;
1443
1444 ret = dev->ethtool_ops->reset(dev, &reset.data);
1445 if (ret)
1446 return ret;
1447
1448 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1449 return -EFAULT;
1450 return 0;
1451}
1452
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1454{
Roland Dreier8e557422010-02-11 12:14:23 -08001455 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 if (!dev->ethtool_ops->get_wol)
1458 return -EOPNOTSUPP;
1459
1460 dev->ethtool_ops->get_wol(dev, &wol);
1461
1462 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1463 return -EFAULT;
1464 return 0;
1465}
1466
1467static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1468{
1469 struct ethtool_wolinfo wol;
1470
1471 if (!dev->ethtool_ops->set_wol)
1472 return -EOPNOTSUPP;
1473
1474 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1475 return -EFAULT;
1476
1477 return dev->ethtool_ops->set_wol(dev, &wol);
1478}
1479
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001480static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1481{
1482 struct ethtool_eee edata;
1483 int rc;
1484
1485 if (!dev->ethtool_ops->get_eee)
1486 return -EOPNOTSUPP;
1487
1488 memset(&edata, 0, sizeof(struct ethtool_eee));
1489 edata.cmd = ETHTOOL_GEEE;
1490 rc = dev->ethtool_ops->get_eee(dev, &edata);
1491
1492 if (rc)
1493 return rc;
1494
1495 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1496 return -EFAULT;
1497
1498 return 0;
1499}
1500
1501static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1502{
1503 struct ethtool_eee edata;
1504
1505 if (!dev->ethtool_ops->set_eee)
1506 return -EOPNOTSUPP;
1507
1508 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1509 return -EFAULT;
1510
1511 return dev->ethtool_ops->set_eee(dev, &edata);
1512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static int ethtool_nway_reset(struct net_device *dev)
1515{
1516 if (!dev->ethtool_ops->nway_reset)
1517 return -EOPNOTSUPP;
1518
1519 return dev->ethtool_ops->nway_reset(dev);
1520}
1521
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001522static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1523{
1524 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1525
1526 if (!dev->ethtool_ops->get_link)
1527 return -EOPNOTSUPP;
1528
1529 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1530
1531 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1532 return -EFAULT;
1533 return 0;
1534}
1535
Ben Hutchings081d0942012-04-12 00:42:12 +00001536static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1537 int (*getter)(struct net_device *,
1538 struct ethtool_eeprom *, u8 *),
1539 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001542 void __user *userbuf = useraddr + sizeof(eeprom);
1543 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001545 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1548 return -EFAULT;
1549
1550 /* Check for wrap and zero */
1551 if (eeprom.offset + eeprom.len <= eeprom.offset)
1552 return -EINVAL;
1553
1554 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001555 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return -EINVAL;
1557
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001558 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 if (!data)
1560 return -ENOMEM;
1561
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001562 bytes_remaining = eeprom.len;
1563 while (bytes_remaining > 0) {
1564 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Ben Hutchings081d0942012-04-12 00:42:12 +00001566 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001567 if (ret)
1568 break;
1569 if (copy_to_user(userbuf, data, eeprom.len)) {
1570 ret = -EFAULT;
1571 break;
1572 }
1573 userbuf += eeprom.len;
1574 eeprom.offset += eeprom.len;
1575 bytes_remaining -= eeprom.len;
1576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001578 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1579 eeprom.offset -= eeprom.len;
1580 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1581 ret = -EFAULT;
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 kfree(data);
1584 return ret;
1585}
1586
Ben Hutchings081d0942012-04-12 00:42:12 +00001587static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1588{
1589 const struct ethtool_ops *ops = dev->ethtool_ops;
1590
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001591 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1592 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001593 return -EOPNOTSUPP;
1594
1595 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1596 ops->get_eeprom_len(dev));
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1600{
1601 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001602 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001603 void __user *userbuf = useraddr + sizeof(eeprom);
1604 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001606 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001608 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1609 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 return -EOPNOTSUPP;
1611
1612 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1613 return -EFAULT;
1614
1615 /* Check for wrap and zero */
1616 if (eeprom.offset + eeprom.len <= eeprom.offset)
1617 return -EINVAL;
1618
1619 /* Check for exceeding total eeprom len */
1620 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1621 return -EINVAL;
1622
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001623 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (!data)
1625 return -ENOMEM;
1626
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001627 bytes_remaining = eeprom.len;
1628 while (bytes_remaining > 0) {
1629 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001631 if (copy_from_user(data, userbuf, eeprom.len)) {
1632 ret = -EFAULT;
1633 break;
1634 }
1635 ret = ops->set_eeprom(dev, &eeprom, data);
1636 if (ret)
1637 break;
1638 userbuf += eeprom.len;
1639 eeprom.offset += eeprom.len;
1640 bytes_remaining -= eeprom.len;
1641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 kfree(data);
1644 return ret;
1645}
1646
chavey97f8aef2010-04-07 21:54:42 -07001647static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1648 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Roland Dreier8e557422010-02-11 12:14:23 -08001650 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 if (!dev->ethtool_ops->get_coalesce)
1653 return -EOPNOTSUPP;
1654
1655 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1656
1657 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1658 return -EFAULT;
1659 return 0;
1660}
1661
chavey97f8aef2010-04-07 21:54:42 -07001662static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1663 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 struct ethtool_coalesce coalesce;
1666
David S. Millerfa04ae52005-06-06 15:07:19 -07001667 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return -EOPNOTSUPP;
1669
1670 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1671 return -EFAULT;
1672
1673 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1674}
1675
1676static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1677{
Roland Dreier8e557422010-02-11 12:14:23 -08001678 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (!dev->ethtool_ops->get_ringparam)
1681 return -EOPNOTSUPP;
1682
1683 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1684
1685 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1686 return -EFAULT;
1687 return 0;
1688}
1689
1690static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1691{
1692 struct ethtool_ringparam ringparam;
1693
1694 if (!dev->ethtool_ops->set_ringparam)
1695 return -EOPNOTSUPP;
1696
1697 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1698 return -EFAULT;
1699
1700 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1701}
1702
amit salecha8b5933c2011-04-07 01:58:42 +00001703static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1704 void __user *useraddr)
1705{
1706 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1707
1708 if (!dev->ethtool_ops->get_channels)
1709 return -EOPNOTSUPP;
1710
1711 dev->ethtool_ops->get_channels(dev, &channels);
1712
1713 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1714 return -EFAULT;
1715 return 0;
1716}
1717
1718static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1719 void __user *useraddr)
1720{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001721 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001722 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001723
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001724 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001725 return -EOPNOTSUPP;
1726
1727 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1728 return -EFAULT;
1729
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001730 dev->ethtool_ops->get_channels(dev, &max);
1731
1732 /* ensure new counts are within the maximums */
1733 if ((channels.rx_count > max.max_rx) ||
1734 (channels.tx_count > max.max_tx) ||
1735 (channels.combined_count > max.max_combined) ||
1736 (channels.other_count > max.max_other))
1737 return -EINVAL;
1738
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001739 /* ensure the new Rx count fits within the configured Rx flow
1740 * indirection table settings */
1741 if (netif_is_rxfh_configured(dev) &&
1742 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1743 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1744 return -EINVAL;
1745
amit salecha8b5933c2011-04-07 01:58:42 +00001746 return dev->ethtool_ops->set_channels(dev, &channels);
1747}
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1750{
1751 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1752
1753 if (!dev->ethtool_ops->get_pauseparam)
1754 return -EOPNOTSUPP;
1755
1756 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1757
1758 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1759 return -EFAULT;
1760 return 0;
1761}
1762
1763static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1764{
1765 struct ethtool_pauseparam pauseparam;
1766
Jeff Garzike1b90c42006-07-17 12:54:40 -04001767 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return -EOPNOTSUPP;
1769
1770 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1771 return -EFAULT;
1772
1773 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1774}
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1777{
1778 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001779 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001781 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001783 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001784 return -EOPNOTSUPP;
1785
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001786 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001787 if (test_len < 0)
1788 return test_len;
1789 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 if (copy_from_user(&test, useraddr, sizeof(test)))
1792 return -EFAULT;
1793
Jeff Garzikff03d492007-08-15 16:01:08 -07001794 test.len = test_len;
1795 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (!data)
1797 return -ENOMEM;
1798
1799 ops->self_test(dev, &test, data);
1800
1801 ret = -EFAULT;
1802 if (copy_to_user(useraddr, &test, sizeof(test)))
1803 goto out;
1804 useraddr += sizeof(test);
1805 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1806 goto out;
1807 ret = 0;
1808
1809 out:
1810 kfree(data);
1811 return ret;
1812}
1813
1814static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1815{
1816 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 u8 *data;
1818 int ret;
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1821 return -EFAULT;
1822
Michał Mirosław340ae162011-02-15 16:59:16 +00001823 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001824 if (ret < 0)
1825 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001826 if (ret > S32_MAX / ETH_GSTRING_LEN)
1827 return -ENOMEM;
1828 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001829
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001830 gstrings.len = ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001831 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1832 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return -ENOMEM;
1834
Michał Mirosław340ae162011-02-15 16:59:16 +00001835 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 ret = -EFAULT;
1838 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1839 goto out;
1840 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001841 if (gstrings.len &&
1842 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 goto out;
1844 ret = 0;
1845
Michał Mirosław340ae162011-02-15 16:59:16 +00001846out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001847 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return ret;
1849}
1850
1851static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1852{
1853 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001854 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001855 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001856 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001858 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 return -EOPNOTSUPP;
1860
Ben Hutchings68f512f2011-04-02 00:35:15 +01001861 if (busy)
1862 return -EBUSY;
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (copy_from_user(&id, useraddr, sizeof(id)))
1865 return -EFAULT;
1866
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001867 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001868 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001869 return rc;
1870
1871 /* Drop the RTNL lock while waiting, but prevent reentry or
1872 * removal of the device.
1873 */
1874 busy = true;
1875 dev_hold(dev);
1876 rtnl_unlock();
1877
1878 if (rc == 0) {
1879 /* Driver will handle this itself */
1880 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001881 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001882 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001883 /* Driver expects to be called at twice the frequency in rc */
1884 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001885
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001886 /* Count down seconds */
1887 do {
1888 /* Count down iterations per second */
1889 i = n;
1890 do {
1891 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001892 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001893 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1894 rtnl_unlock();
1895 if (rc)
1896 break;
1897 schedule_timeout_interruptible(interval);
1898 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001899 } while (!signal_pending(current) &&
1900 (id.data == 0 || --id.data != 0));
1901 }
1902
1903 rtnl_lock();
1904 dev_put(dev);
1905 busy = false;
1906
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001907 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001908 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
1911static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1912{
1913 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001914 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001916 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001918 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001919 return -EOPNOTSUPP;
1920
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001921 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001922 if (n_stats < 0)
1923 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001924 if (n_stats > S32_MAX / sizeof(u64))
1925 return -ENOMEM;
1926 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1928 return -EFAULT;
1929
Jeff Garzikff03d492007-08-15 16:01:08 -07001930 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001931 data = vzalloc(n_stats * sizeof(u64));
1932 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return -ENOMEM;
1934
1935 ops->get_ethtool_stats(dev, &stats, data);
1936
1937 ret = -EFAULT;
1938 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1939 goto out;
1940 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001941 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto out;
1943 ret = 0;
1944
1945 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001946 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return ret;
1948}
1949
Andrew Lunnf3a40942015-12-30 16:28:25 +01001950static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1951{
1952 struct ethtool_stats stats;
1953 struct phy_device *phydev = dev->phydev;
1954 u64 *data;
1955 int ret, n_stats;
1956
1957 if (!phydev)
1958 return -EOPNOTSUPP;
1959
1960 n_stats = phy_get_sset_count(phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001961 if (n_stats < 0)
1962 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001963 if (n_stats > S32_MAX / sizeof(u64))
1964 return -ENOMEM;
1965 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001966
1967 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1968 return -EFAULT;
1969
1970 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001971 data = vzalloc(n_stats * sizeof(u64));
1972 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01001973 return -ENOMEM;
1974
1975 mutex_lock(&phydev->lock);
1976 phydev->drv->get_stats(phydev, &stats, data);
1977 mutex_unlock(&phydev->lock);
1978
1979 ret = -EFAULT;
1980 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1981 goto out;
1982 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001983 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01001984 goto out;
1985 ret = 0;
1986
1987 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001988 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01001989 return ret;
1990}
1991
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01001992static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07001993{
1994 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07001995
Matthew Wilcox313674a2007-07-31 14:00:29 -07001996 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07001997 return -EFAULT;
1998
Matthew Wilcox313674a2007-07-31 14:00:29 -07001999 if (epaddr.size < dev->addr_len)
2000 return -ETOOSMALL;
2001 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002002
Jon Wetzela6f9a702005-08-20 17:15:54 -07002003 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002004 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002005 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002006 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2007 return -EFAULT;
2008 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002009}
2010
Jeff Garzik13c99b22007-08-15 16:01:56 -07002011static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2012 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002013{
Roland Dreier8e557422010-02-11 12:14:23 -08002014 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002015
Jeff Garzik13c99b22007-08-15 16:01:56 -07002016 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002017 return -EOPNOTSUPP;
2018
Jeff Garzik13c99b22007-08-15 16:01:56 -07002019 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002020
2021 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2022 return -EFAULT;
2023 return 0;
2024}
2025
Jeff Garzik13c99b22007-08-15 16:01:56 -07002026static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2027 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002028{
2029 struct ethtool_value edata;
2030
Jeff Garzik13c99b22007-08-15 16:01:56 -07002031 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002032 return -EOPNOTSUPP;
2033
2034 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2035 return -EFAULT;
2036
Jeff Garzik13c99b22007-08-15 16:01:56 -07002037 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002038 return 0;
2039}
2040
Jeff Garzik13c99b22007-08-15 16:01:56 -07002041static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2042 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002043{
2044 struct ethtool_value edata;
2045
Jeff Garzik13c99b22007-08-15 16:01:56 -07002046 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002047 return -EOPNOTSUPP;
2048
2049 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2050 return -EFAULT;
2051
Jeff Garzik13c99b22007-08-15 16:01:56 -07002052 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002053}
2054
chavey97f8aef2010-04-07 21:54:42 -07002055static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2056 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002057{
2058 struct ethtool_flash efl;
2059
2060 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2061 return -EFAULT;
2062
2063 if (!dev->ethtool_ops->flash_device)
2064 return -EOPNOTSUPP;
2065
Ben Hutchings786f5282012-02-01 09:32:25 +00002066 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2067
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002068 return dev->ethtool_ops->flash_device(dev, &efl);
2069}
2070
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002071static int ethtool_set_dump(struct net_device *dev,
2072 void __user *useraddr)
2073{
2074 struct ethtool_dump dump;
2075
2076 if (!dev->ethtool_ops->set_dump)
2077 return -EOPNOTSUPP;
2078
2079 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2080 return -EFAULT;
2081
2082 return dev->ethtool_ops->set_dump(dev, &dump);
2083}
2084
2085static int ethtool_get_dump_flag(struct net_device *dev,
2086 void __user *useraddr)
2087{
2088 int ret;
2089 struct ethtool_dump dump;
2090 const struct ethtool_ops *ops = dev->ethtool_ops;
2091
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002092 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002093 return -EOPNOTSUPP;
2094
2095 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2096 return -EFAULT;
2097
2098 ret = ops->get_dump_flag(dev, &dump);
2099 if (ret)
2100 return ret;
2101
2102 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2103 return -EFAULT;
2104 return 0;
2105}
2106
2107static int ethtool_get_dump_data(struct net_device *dev,
2108 void __user *useraddr)
2109{
2110 int ret;
2111 __u32 len;
2112 struct ethtool_dump dump, tmp;
2113 const struct ethtool_ops *ops = dev->ethtool_ops;
2114 void *data = NULL;
2115
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002116 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002117 return -EOPNOTSUPP;
2118
2119 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2120 return -EFAULT;
2121
2122 memset(&tmp, 0, sizeof(tmp));
2123 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2124 ret = ops->get_dump_flag(dev, &tmp);
2125 if (ret)
2126 return ret;
2127
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002128 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002129 if (!len)
2130 return -EFAULT;
2131
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002132 /* Don't ever let the driver think there's more space available
2133 * than it requested with .get_dump_flag().
2134 */
2135 dump.len = len;
2136
2137 /* Always allocate enough space to hold the whole thing so that the
2138 * driver does not need to check the length and bother with partial
2139 * dumping.
2140 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002141 data = vzalloc(tmp.len);
2142 if (!data)
2143 return -ENOMEM;
2144 ret = ops->get_dump_data(dev, &dump, data);
2145 if (ret)
2146 goto out;
2147
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002148 /* There are two sane possibilities:
2149 * 1. The driver's .get_dump_data() does not touch dump.len.
2150 * 2. Or it may set dump.len to how much it really writes, which
2151 * should be tmp.len (or len if it can do a partial dump).
2152 * In any case respond to userspace with the actual length of data
2153 * it's receiving.
2154 */
2155 WARN_ON(dump.len != len && dump.len != tmp.len);
2156 dump.len = len;
2157
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002158 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2159 ret = -EFAULT;
2160 goto out;
2161 }
2162 useraddr += offsetof(struct ethtool_dump, data);
2163 if (copy_to_user(useraddr, data, len))
2164 ret = -EFAULT;
2165out:
2166 vfree(data);
2167 return ret;
2168}
2169
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002170static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2171{
2172 int err = 0;
2173 struct ethtool_ts_info info;
2174 const struct ethtool_ops *ops = dev->ethtool_ops;
2175 struct phy_device *phydev = dev->phydev;
2176
2177 memset(&info, 0, sizeof(info));
2178 info.cmd = ETHTOOL_GET_TS_INFO;
2179
2180 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002181 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002182 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002183 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002184 } else {
2185 info.so_timestamping =
2186 SOF_TIMESTAMPING_RX_SOFTWARE |
2187 SOF_TIMESTAMPING_SOFTWARE;
2188 info.phc_index = -1;
2189 }
2190
2191 if (err)
2192 return err;
2193
2194 if (copy_to_user(useraddr, &info, sizeof(info)))
2195 err = -EFAULT;
2196
2197 return err;
2198}
2199
Ed Swierk2f438362015-01-02 17:27:56 -08002200static int __ethtool_get_module_info(struct net_device *dev,
2201 struct ethtool_modinfo *modinfo)
2202{
2203 const struct ethtool_ops *ops = dev->ethtool_ops;
2204 struct phy_device *phydev = dev->phydev;
2205
2206 if (phydev && phydev->drv && phydev->drv->module_info)
2207 return phydev->drv->module_info(phydev, modinfo);
2208
2209 if (ops->get_module_info)
2210 return ops->get_module_info(dev, modinfo);
2211
2212 return -EOPNOTSUPP;
2213}
2214
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002215static int ethtool_get_module_info(struct net_device *dev,
2216 void __user *useraddr)
2217{
2218 int ret;
2219 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002220
2221 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2222 return -EFAULT;
2223
Ed Swierk2f438362015-01-02 17:27:56 -08002224 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002225 if (ret)
2226 return ret;
2227
2228 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2229 return -EFAULT;
2230
2231 return 0;
2232}
2233
Ed Swierk2f438362015-01-02 17:27:56 -08002234static int __ethtool_get_module_eeprom(struct net_device *dev,
2235 struct ethtool_eeprom *ee, u8 *data)
2236{
2237 const struct ethtool_ops *ops = dev->ethtool_ops;
2238 struct phy_device *phydev = dev->phydev;
2239
2240 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2241 return phydev->drv->module_eeprom(phydev, ee, data);
2242
2243 if (ops->get_module_eeprom)
2244 return ops->get_module_eeprom(dev, ee, data);
2245
2246 return -EOPNOTSUPP;
2247}
2248
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002249static int ethtool_get_module_eeprom(struct net_device *dev,
2250 void __user *useraddr)
2251{
2252 int ret;
2253 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002254
Ed Swierk2f438362015-01-02 17:27:56 -08002255 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002256 if (ret)
2257 return ret;
2258
Ed Swierk2f438362015-01-02 17:27:56 -08002259 return ethtool_get_any_eeprom(dev, useraddr,
2260 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002261 modinfo.eeprom_len);
2262}
2263
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302264static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2265{
2266 switch (tuna->id) {
2267 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002268 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302269 if (tuna->len != sizeof(u32) ||
2270 tuna->type_id != ETHTOOL_TUNABLE_U32)
2271 return -EINVAL;
2272 break;
2273 default:
2274 return -EINVAL;
2275 }
2276
2277 return 0;
2278}
2279
2280static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2281{
2282 int ret;
2283 struct ethtool_tunable tuna;
2284 const struct ethtool_ops *ops = dev->ethtool_ops;
2285 void *data;
2286
2287 if (!ops->get_tunable)
2288 return -EOPNOTSUPP;
2289 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2290 return -EFAULT;
2291 ret = ethtool_tunable_valid(&tuna);
2292 if (ret)
2293 return ret;
2294 data = kmalloc(tuna.len, GFP_USER);
2295 if (!data)
2296 return -ENOMEM;
2297 ret = ops->get_tunable(dev, &tuna, data);
2298 if (ret)
2299 goto out;
2300 useraddr += sizeof(tuna);
2301 ret = -EFAULT;
2302 if (copy_to_user(useraddr, data, tuna.len))
2303 goto out;
2304 ret = 0;
2305
2306out:
2307 kfree(data);
2308 return ret;
2309}
2310
2311static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2312{
2313 int ret;
2314 struct ethtool_tunable tuna;
2315 const struct ethtool_ops *ops = dev->ethtool_ops;
2316 void *data;
2317
2318 if (!ops->set_tunable)
2319 return -EOPNOTSUPP;
2320 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2321 return -EFAULT;
2322 ret = ethtool_tunable_valid(&tuna);
2323 if (ret)
2324 return ret;
2325 data = kmalloc(tuna.len, GFP_USER);
2326 if (!data)
2327 return -ENOMEM;
2328 useraddr += sizeof(tuna);
2329 ret = -EFAULT;
2330 if (copy_from_user(data, useraddr, tuna.len))
2331 goto out;
2332 ret = ops->set_tunable(dev, &tuna, data);
2333
2334out:
2335 kfree(data);
2336 return ret;
2337}
2338
Kan Liang421797b2016-02-19 09:24:02 -05002339static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2340 void __user *useraddr,
2341 struct ethtool_per_queue_op *per_queue_opt)
2342{
2343 u32 bit;
2344 int ret;
2345 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2346
2347 if (!dev->ethtool_ops->get_per_queue_coalesce)
2348 return -EOPNOTSUPP;
2349
2350 useraddr += sizeof(*per_queue_opt);
2351
2352 bitmap_from_u32array(queue_mask,
2353 MAX_NUM_QUEUE,
2354 per_queue_opt->queue_mask,
2355 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2356
2357 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2358 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2359
2360 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2361 if (ret != 0)
2362 return ret;
2363 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2364 return -EFAULT;
2365 useraddr += sizeof(coalesce);
2366 }
2367
2368 return 0;
2369}
2370
Kan Liangf38d1382016-02-19 09:24:03 -05002371static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2372 void __user *useraddr,
2373 struct ethtool_per_queue_op *per_queue_opt)
2374{
2375 u32 bit;
2376 int i, ret = 0;
2377 int n_queue;
2378 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2379 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2380
2381 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2382 (!dev->ethtool_ops->get_per_queue_coalesce))
2383 return -EOPNOTSUPP;
2384
2385 useraddr += sizeof(*per_queue_opt);
2386
2387 bitmap_from_u32array(queue_mask,
2388 MAX_NUM_QUEUE,
2389 per_queue_opt->queue_mask,
2390 DIV_ROUND_UP(MAX_NUM_QUEUE, 32));
2391 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2392 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2393 if (!backup)
2394 return -ENOMEM;
2395
2396 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2397 struct ethtool_coalesce coalesce;
2398
2399 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2400 if (ret != 0)
2401 goto roll_back;
2402
2403 tmp++;
2404
2405 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2406 ret = -EFAULT;
2407 goto roll_back;
2408 }
2409
2410 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2411 if (ret != 0)
2412 goto roll_back;
2413
2414 useraddr += sizeof(coalesce);
2415 }
2416
2417roll_back:
2418 if (ret != 0) {
2419 tmp = backup;
2420 for_each_set_bit(i, queue_mask, bit) {
2421 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2422 tmp++;
2423 }
2424 }
2425 kfree(backup);
2426
2427 return ret;
2428}
2429
Kan Liangac2c7ad2016-02-19 09:24:01 -05002430static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2431{
2432 struct ethtool_per_queue_op per_queue_opt;
2433
2434 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2435 return -EFAULT;
2436
2437 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002438 case ETHTOOL_GCOALESCE:
2439 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002440 case ETHTOOL_SCOALESCE:
2441 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002442 default:
2443 return -EOPNOTSUPP;
2444 };
2445}
2446
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002447static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2448{
2449 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002450 case ETHTOOL_PHY_DOWNSHIFT:
2451 if (tuna->len != sizeof(u8) ||
2452 tuna->type_id != ETHTOOL_TUNABLE_U8)
2453 return -EINVAL;
2454 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002455 default:
2456 return -EINVAL;
2457 }
2458
2459 return 0;
2460}
2461
2462static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2463{
2464 int ret;
2465 struct ethtool_tunable tuna;
2466 struct phy_device *phydev = dev->phydev;
2467 void *data;
2468
2469 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2470 return -EOPNOTSUPP;
2471
2472 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2473 return -EFAULT;
2474 ret = ethtool_phy_tunable_valid(&tuna);
2475 if (ret)
2476 return ret;
2477 data = kmalloc(tuna.len, GFP_USER);
2478 if (!data)
2479 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002480 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002481 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002482 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002483 if (ret)
2484 goto out;
2485 useraddr += sizeof(tuna);
2486 ret = -EFAULT;
2487 if (copy_to_user(useraddr, data, tuna.len))
2488 goto out;
2489 ret = 0;
2490
2491out:
2492 kfree(data);
2493 return ret;
2494}
2495
2496static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2497{
2498 int ret;
2499 struct ethtool_tunable tuna;
2500 struct phy_device *phydev = dev->phydev;
2501 void *data;
2502
2503 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2504 return -EOPNOTSUPP;
2505 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2506 return -EFAULT;
2507 ret = ethtool_phy_tunable_valid(&tuna);
2508 if (ret)
2509 return ret;
2510 data = kmalloc(tuna.len, GFP_USER);
2511 if (!data)
2512 return -ENOMEM;
2513 useraddr += sizeof(tuna);
2514 ret = -EFAULT;
2515 if (copy_from_user(data, useraddr, tuna.len))
2516 goto out;
Florian Fainelli4b652462016-11-22 13:55:31 -08002517 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002518 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002519 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002520
2521out:
2522 kfree(data);
2523 return ret;
2524}
2525
Yan Burman600fed52013-06-03 02:03:34 +00002526/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Eric W. Biederman881d9662007-09-17 11:56:21 -07002528int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002530 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002532 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002534 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (!dev || !netif_device_present(dev))
2537 return -ENODEV;
2538
chavey97f8aef2010-04-07 21:54:42 -07002539 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return -EFAULT;
2541
Kan Liangac2c7ad2016-02-19 09:24:01 -05002542 if (ethcmd == ETHTOOL_PERQUEUE) {
2543 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2544 return -EFAULT;
2545 } else {
2546 sub_cmd = ethcmd;
2547 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002548 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002549 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002550 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002551 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002552 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002553 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002554 case ETHTOOL_GCOALESCE:
2555 case ETHTOOL_GRINGPARAM:
2556 case ETHTOOL_GPAUSEPARAM:
2557 case ETHTOOL_GRXCSUM:
2558 case ETHTOOL_GTXCSUM:
2559 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002560 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002561 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002562 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002563 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002564 case ETHTOOL_GTSO:
2565 case ETHTOOL_GPERMADDR:
2566 case ETHTOOL_GUFO:
2567 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002568 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002569 case ETHTOOL_GFLAGS:
2570 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002571 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002572 case ETHTOOL_GRXRINGS:
2573 case ETHTOOL_GRXCLSRLCNT:
2574 case ETHTOOL_GRXCLSRULE:
2575 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002576 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302577 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002578 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002579 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002580 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002581 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302582 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002583 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002584 case ETHTOOL_GLINKSETTINGS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002585 break;
2586 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002587 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002588 return -EPERM;
2589 }
2590
chavey97f8aef2010-04-07 21:54:42 -07002591 if (dev->ethtool_ops->begin) {
2592 rc = dev->ethtool_ops->begin(dev);
2593 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002595 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002596 old_features = dev->features;
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 switch (ethcmd) {
2599 case ETHTOOL_GSET:
2600 rc = ethtool_get_settings(dev, useraddr);
2601 break;
2602 case ETHTOOL_SSET:
2603 rc = ethtool_set_settings(dev, useraddr);
2604 break;
2605 case ETHTOOL_GDRVINFO:
2606 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 break;
2608 case ETHTOOL_GREGS:
2609 rc = ethtool_get_regs(dev, useraddr);
2610 break;
2611 case ETHTOOL_GWOL:
2612 rc = ethtool_get_wol(dev, useraddr);
2613 break;
2614 case ETHTOOL_SWOL:
2615 rc = ethtool_set_wol(dev, useraddr);
2616 break;
2617 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002618 rc = ethtool_get_value(dev, useraddr, ethcmd,
2619 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 break;
2621 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002622 rc = ethtool_set_value_void(dev, useraddr,
2623 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002625 case ETHTOOL_GEEE:
2626 rc = ethtool_get_eee(dev, useraddr);
2627 break;
2628 case ETHTOOL_SEEE:
2629 rc = ethtool_set_eee(dev, useraddr);
2630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 case ETHTOOL_NWAY_RST:
2632 rc = ethtool_nway_reset(dev);
2633 break;
2634 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002635 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 break;
2637 case ETHTOOL_GEEPROM:
2638 rc = ethtool_get_eeprom(dev, useraddr);
2639 break;
2640 case ETHTOOL_SEEPROM:
2641 rc = ethtool_set_eeprom(dev, useraddr);
2642 break;
2643 case ETHTOOL_GCOALESCE:
2644 rc = ethtool_get_coalesce(dev, useraddr);
2645 break;
2646 case ETHTOOL_SCOALESCE:
2647 rc = ethtool_set_coalesce(dev, useraddr);
2648 break;
2649 case ETHTOOL_GRINGPARAM:
2650 rc = ethtool_get_ringparam(dev, useraddr);
2651 break;
2652 case ETHTOOL_SRINGPARAM:
2653 rc = ethtool_set_ringparam(dev, useraddr);
2654 break;
2655 case ETHTOOL_GPAUSEPARAM:
2656 rc = ethtool_get_pauseparam(dev, useraddr);
2657 break;
2658 case ETHTOOL_SPAUSEPARAM:
2659 rc = ethtool_set_pauseparam(dev, useraddr);
2660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 case ETHTOOL_TEST:
2662 rc = ethtool_self_test(dev, useraddr);
2663 break;
2664 case ETHTOOL_GSTRINGS:
2665 rc = ethtool_get_strings(dev, useraddr);
2666 break;
2667 case ETHTOOL_PHYS_ID:
2668 rc = ethtool_phys_id(dev, useraddr);
2669 break;
2670 case ETHTOOL_GSTATS:
2671 rc = ethtool_get_stats(dev, useraddr);
2672 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002673 case ETHTOOL_GPERMADDR:
2674 rc = ethtool_get_perm_addr(dev, useraddr);
2675 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002676 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002677 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002678 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002679 break;
2680 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002681 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002682 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002683 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002684 rc = ethtool_get_value(dev, useraddr, ethcmd,
2685 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002686 break;
2687 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002688 rc = ethtool_set_value(dev, useraddr,
2689 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002690 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002691 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002692 case ETHTOOL_GRXRINGS:
2693 case ETHTOOL_GRXCLSRLCNT:
2694 case ETHTOOL_GRXCLSRULE:
2695 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002696 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002697 break;
2698 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002699 case ETHTOOL_SRXCLSRLDEL:
2700 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002701 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002702 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002703 case ETHTOOL_FLASHDEV:
2704 rc = ethtool_flash_device(dev, useraddr);
2705 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002706 case ETHTOOL_RESET:
2707 rc = ethtool_reset(dev, useraddr);
2708 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002709 case ETHTOOL_GSSET_INFO:
2710 rc = ethtool_get_sset_info(dev, useraddr);
2711 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002712 case ETHTOOL_GRXFHINDIR:
2713 rc = ethtool_get_rxfh_indir(dev, useraddr);
2714 break;
2715 case ETHTOOL_SRXFHINDIR:
2716 rc = ethtool_set_rxfh_indir(dev, useraddr);
2717 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302718 case ETHTOOL_GRSSH:
2719 rc = ethtool_get_rxfh(dev, useraddr);
2720 break;
2721 case ETHTOOL_SRSSH:
2722 rc = ethtool_set_rxfh(dev, useraddr);
2723 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002724 case ETHTOOL_GFEATURES:
2725 rc = ethtool_get_features(dev, useraddr);
2726 break;
2727 case ETHTOOL_SFEATURES:
2728 rc = ethtool_set_features(dev, useraddr);
2729 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002730 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002731 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002732 case ETHTOOL_GSG:
2733 case ETHTOOL_GTSO:
2734 case ETHTOOL_GUFO:
2735 case ETHTOOL_GGSO:
2736 case ETHTOOL_GGRO:
2737 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2738 break;
2739 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002740 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002741 case ETHTOOL_SSG:
2742 case ETHTOOL_STSO:
2743 case ETHTOOL_SUFO:
2744 case ETHTOOL_SGSO:
2745 case ETHTOOL_SGRO:
2746 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2747 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002748 case ETHTOOL_GCHANNELS:
2749 rc = ethtool_get_channels(dev, useraddr);
2750 break;
2751 case ETHTOOL_SCHANNELS:
2752 rc = ethtool_set_channels(dev, useraddr);
2753 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002754 case ETHTOOL_SET_DUMP:
2755 rc = ethtool_set_dump(dev, useraddr);
2756 break;
2757 case ETHTOOL_GET_DUMP_FLAG:
2758 rc = ethtool_get_dump_flag(dev, useraddr);
2759 break;
2760 case ETHTOOL_GET_DUMP_DATA:
2761 rc = ethtool_get_dump_data(dev, useraddr);
2762 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002763 case ETHTOOL_GET_TS_INFO:
2764 rc = ethtool_get_ts_info(dev, useraddr);
2765 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002766 case ETHTOOL_GMODULEINFO:
2767 rc = ethtool_get_module_info(dev, useraddr);
2768 break;
2769 case ETHTOOL_GMODULEEEPROM:
2770 rc = ethtool_get_module_eeprom(dev, useraddr);
2771 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302772 case ETHTOOL_GTUNABLE:
2773 rc = ethtool_get_tunable(dev, useraddr);
2774 break;
2775 case ETHTOOL_STUNABLE:
2776 rc = ethtool_set_tunable(dev, useraddr);
2777 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002778 case ETHTOOL_GPHYSTATS:
2779 rc = ethtool_get_phy_stats(dev, useraddr);
2780 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002781 case ETHTOOL_PERQUEUE:
2782 rc = ethtool_set_per_queue(dev, useraddr);
2783 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002784 case ETHTOOL_GLINKSETTINGS:
2785 rc = ethtool_get_link_ksettings(dev, useraddr);
2786 break;
2787 case ETHTOOL_SLINKSETTINGS:
2788 rc = ethtool_set_link_ksettings(dev, useraddr);
2789 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002790 case ETHTOOL_PHY_GTUNABLE:
2791 rc = get_phy_tunable(dev, useraddr);
2792 break;
2793 case ETHTOOL_PHY_STUNABLE:
2794 rc = set_phy_tunable(dev, useraddr);
2795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002797 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002799
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002800 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002802
2803 if (old_features != dev->features)
2804 netdev_features_change(dev);
2805
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807}