blob: 157cd9efa4bedf868bd498676eb662cbfb786dab [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",
Michael Chanfb1f5f72017-12-16 03:09:40 -050076 [NETIF_F_GRO_HW_BIT] = "rx-gro-hw",
Michał Mirosław9d921542011-11-15 15:29:55 +000077 [NETIF_F_LRO_BIT] = "rx-lro",
78
79 [NETIF_F_TSO_BIT] = "tx-tcp-segmentation",
Michał Mirosław9d921542011-11-15 15:29:55 +000080 [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",
Sabrina Dubrocad764a122017-07-21 12:49:28 +0200109 [NETIF_F_RX_UDP_TUNNEL_PORT_BIT] = "rx-udp_tunnel-port-offload",
Michał Mirosław9d921542011-11-15 15:29:55 +0000110};
111
Eyal Perry892311f2014-12-02 18:12:10 +0200112static const char
113rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
114 [ETH_RSS_HASH_TOP_BIT] = "toeplitz",
115 [ETH_RSS_HASH_XOR_BIT] = "xor",
Jakub Kicinskiabb521e2017-03-08 08:57:00 -0800116 [ETH_RSS_HASH_CRC32_BIT] = "crc32",
Eyal Perry892311f2014-12-02 18:12:10 +0200117};
118
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300119static const char
120tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
121 [ETHTOOL_ID_UNSPEC] = "Unspec",
122 [ETHTOOL_RX_COPYBREAK] = "rx-copybreak",
123 [ETHTOOL_TX_COPYBREAK] = "tx-copybreak",
124};
125
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100126static const char
127phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
128 [ETHTOOL_ID_UNSPEC] = "Unspec",
Raju Lakkaraju65feddd2016-11-17 13:07:23 +0100129 [ETHTOOL_PHY_DOWNSHIFT] = "phy-downshift",
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100130};
131
Michał Mirosław5455c692011-02-15 16:59:17 +0000132static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
133{
134 struct ethtool_gfeatures cmd = {
135 .cmd = ETHTOOL_GFEATURES,
136 .size = ETHTOOL_DEV_FEATURE_WORDS,
137 };
Michał Mirosław475414f2011-11-15 15:29:55 +0000138 struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław5455c692011-02-15 16:59:17 +0000139 u32 __user *sizeaddr;
140 u32 copy_size;
Michał Mirosław475414f2011-11-15 15:29:55 +0000141 int i;
142
143 /* in case feature bits run out again */
Michał Mirosław09da71b2011-11-16 14:32:03 +0000144 BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
Michał Mirosław475414f2011-11-15 15:29:55 +0000145
146 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000147 features[i].available = (u32)(dev->hw_features >> (32 * i));
148 features[i].requested = (u32)(dev->wanted_features >> (32 * i));
149 features[i].active = (u32)(dev->features >> (32 * i));
150 features[i].never_changed =
151 (u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
Michał Mirosław475414f2011-11-15 15:29:55 +0000152 }
Michał Mirosław5455c692011-02-15 16:59:17 +0000153
154 sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
155 if (get_user(copy_size, sizeaddr))
156 return -EFAULT;
157
158 if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
159 copy_size = ETHTOOL_DEV_FEATURE_WORDS;
160
161 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
162 return -EFAULT;
163 useraddr += sizeof(cmd);
164 if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
165 return -EFAULT;
166
167 return 0;
168}
169
170static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
171{
172 struct ethtool_sfeatures cmd;
173 struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
Michał Mirosław475414f2011-11-15 15:29:55 +0000174 netdev_features_t wanted = 0, valid = 0;
175 int i, ret = 0;
Michał Mirosław5455c692011-02-15 16:59:17 +0000176
177 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
178 return -EFAULT;
179 useraddr += sizeof(cmd);
180
181 if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
182 return -EINVAL;
183
184 if (copy_from_user(features, useraddr, sizeof(features)))
185 return -EFAULT;
186
Michał Mirosław475414f2011-11-15 15:29:55 +0000187 for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
Michał Mirosław09da71b2011-11-16 14:32:03 +0000188 valid |= (netdev_features_t)features[i].valid << (32 * i);
189 wanted |= (netdev_features_t)features[i].requested << (32 * i);
Michał Mirosław475414f2011-11-15 15:29:55 +0000190 }
191
192 if (valid & ~NETIF_F_ETHTOOL_BITS)
Michał Mirosław5455c692011-02-15 16:59:17 +0000193 return -EINVAL;
194
Michał Mirosław475414f2011-11-15 15:29:55 +0000195 if (valid & ~dev->hw_features) {
196 valid &= dev->hw_features;
Michał Mirosław5455c692011-02-15 16:59:17 +0000197 ret |= ETHTOOL_F_UNSUPPORTED;
198 }
199
Michał Mirosław475414f2011-11-15 15:29:55 +0000200 dev->wanted_features &= ~valid;
201 dev->wanted_features |= wanted & valid;
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700202 __netdev_update_features(dev);
Michał Mirosław5455c692011-02-15 16:59:17 +0000203
Michał Mirosław475414f2011-11-15 15:29:55 +0000204 if ((dev->wanted_features ^ dev->features) & valid)
Michał Mirosław5455c692011-02-15 16:59:17 +0000205 ret |= ETHTOOL_F_WISH;
206
207 return ret;
208}
209
Andrew Lunnf3a40942015-12-30 16:28:25 +0100210static int phy_get_sset_count(struct phy_device *phydev)
211{
212 int ret;
213
214 if (phydev->drv->get_sset_count &&
215 phydev->drv->get_strings &&
216 phydev->drv->get_stats) {
217 mutex_lock(&phydev->lock);
218 ret = phydev->drv->get_sset_count(phydev);
219 mutex_unlock(&phydev->lock);
220
221 return ret;
222 }
223
224 return -EOPNOTSUPP;
225}
226
Michał Mirosław340ae162011-02-15 16:59:16 +0000227static int __ethtool_get_sset_count(struct net_device *dev, int sset)
228{
229 const struct ethtool_ops *ops = dev->ethtool_ops;
230
Michał Mirosław5455c692011-02-15 16:59:17 +0000231 if (sset == ETH_SS_FEATURES)
232 return ARRAY_SIZE(netdev_features_strings);
233
Eyal Perry892311f2014-12-02 18:12:10 +0200234 if (sset == ETH_SS_RSS_HASH_FUNCS)
235 return ARRAY_SIZE(rss_hash_func_strings);
236
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300237 if (sset == ETH_SS_TUNABLES)
238 return ARRAY_SIZE(tunable_strings);
239
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100240 if (sset == ETH_SS_PHY_TUNABLES)
241 return ARRAY_SIZE(phy_tunable_strings);
242
Andrew Lunnf3a40942015-12-30 16:28:25 +0100243 if (sset == ETH_SS_PHY_STATS) {
244 if (dev->phydev)
245 return phy_get_sset_count(dev->phydev);
246 else
247 return -EOPNOTSUPP;
248 }
249
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000250 if (ops->get_sset_count && ops->get_strings)
Michał Mirosław340ae162011-02-15 16:59:16 +0000251 return ops->get_sset_count(dev, sset);
252 else
253 return -EOPNOTSUPP;
254}
255
256static void __ethtool_get_strings(struct net_device *dev,
257 u32 stringset, u8 *data)
258{
259 const struct ethtool_ops *ops = dev->ethtool_ops;
260
Michał Mirosław5455c692011-02-15 16:59:17 +0000261 if (stringset == ETH_SS_FEATURES)
262 memcpy(data, netdev_features_strings,
263 sizeof(netdev_features_strings));
Eyal Perry892311f2014-12-02 18:12:10 +0200264 else if (stringset == ETH_SS_RSS_HASH_FUNCS)
265 memcpy(data, rss_hash_func_strings,
266 sizeof(rss_hash_func_strings));
Hadar Hen Ziona4244b02015-06-11 10:28:16 +0300267 else if (stringset == ETH_SS_TUNABLES)
268 memcpy(data, tunable_strings, sizeof(tunable_strings));
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +0100269 else if (stringset == ETH_SS_PHY_TUNABLES)
270 memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
Andrew Lunnf3a40942015-12-30 16:28:25 +0100271 else if (stringset == ETH_SS_PHY_STATS) {
272 struct phy_device *phydev = dev->phydev;
273
274 if (phydev) {
275 mutex_lock(&phydev->lock);
276 phydev->drv->get_strings(phydev, data);
277 mutex_unlock(&phydev->lock);
278 } else {
279 return;
280 }
281 } else
Michał Mirosław5455c692011-02-15 16:59:17 +0000282 /* ops->get_strings is valid because checked earlier */
283 ops->get_strings(dev, stringset, data);
Michał Mirosław340ae162011-02-15 16:59:16 +0000284}
285
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000286static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
Michał Mirosław0a417702011-02-15 16:59:17 +0000287{
288 /* feature masks of legacy discrete ethtool ops */
289
290 switch (eth_cmd) {
291 case ETHTOOL_GTXCSUM:
292 case ETHTOOL_STXCSUM:
Tom Herberta1882222015-12-14 11:19:43 -0800293 return NETIF_F_CSUM_MASK | NETIF_F_SCTP_CRC;
Michał Mirosławe83d3602011-02-15 16:59:18 +0000294 case ETHTOOL_GRXCSUM:
295 case ETHTOOL_SRXCSUM:
296 return NETIF_F_RXCSUM;
Michał Mirosław0a417702011-02-15 16:59:17 +0000297 case ETHTOOL_GSG:
298 case ETHTOOL_SSG:
299 return NETIF_F_SG;
300 case ETHTOOL_GTSO:
301 case ETHTOOL_STSO:
302 return NETIF_F_ALL_TSO;
Michał Mirosław0a417702011-02-15 16:59:17 +0000303 case ETHTOOL_GGSO:
304 case ETHTOOL_SGSO:
305 return NETIF_F_GSO;
306 case ETHTOOL_GGRO:
307 case ETHTOOL_SGRO:
308 return NETIF_F_GRO;
309 default:
310 BUG();
311 }
312}
313
Michał Mirosław0a417702011-02-15 16:59:17 +0000314static int ethtool_get_one_feature(struct net_device *dev,
315 char __user *useraddr, u32 ethcmd)
316{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000317 netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
Michał Mirosław0a417702011-02-15 16:59:17 +0000318 struct ethtool_value edata = {
319 .cmd = ethcmd,
Michał Mirosław86794882011-02-15 16:59:17 +0000320 .data = !!(dev->features & mask),
Michał Mirosław0a417702011-02-15 16:59:17 +0000321 };
Michał Mirosław0a417702011-02-15 16:59:17 +0000322
Michał Mirosław0a417702011-02-15 16:59:17 +0000323 if (copy_to_user(useraddr, &edata, sizeof(edata)))
324 return -EFAULT;
325 return 0;
326}
327
Michał Mirosław0a417702011-02-15 16:59:17 +0000328static int ethtool_set_one_feature(struct net_device *dev,
329 void __user *useraddr, u32 ethcmd)
330{
331 struct ethtool_value edata;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000332 netdev_features_t mask;
Michał Mirosław0a417702011-02-15 16:59:17 +0000333
334 if (copy_from_user(&edata, useraddr, sizeof(edata)))
335 return -EFAULT;
336
Michał Mirosław86794882011-02-15 16:59:17 +0000337 mask = ethtool_get_feature_mask(ethcmd);
338 mask &= dev->hw_features;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000339 if (!mask)
Michał Mirosław0a417702011-02-15 16:59:17 +0000340 return -EOPNOTSUPP;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000341
342 if (edata.data)
343 dev->wanted_features |= mask;
344 else
345 dev->wanted_features &= ~mask;
346
347 __netdev_update_features(dev);
348
349 return 0;
Michał Mirosław0a417702011-02-15 16:59:17 +0000350}
351
Michał Mirosław02b3a552011-11-15 15:29:55 +0000352#define ETH_ALL_FLAGS (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
353 ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
Patrick McHardyf6469682013-04-19 02:04:27 +0000354#define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
355 NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
356 NETIF_F_RXHASH)
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000357
358static u32 __ethtool_get_flags(struct net_device *dev)
359{
Michał Mirosław02b3a552011-11-15 15:29:55 +0000360 u32 flags = 0;
361
Dragos Foianu8a731252013-07-13 14:43:00 +0100362 if (dev->features & NETIF_F_LRO)
363 flags |= ETH_FLAG_LRO;
364 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
365 flags |= ETH_FLAG_RXVLAN;
366 if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
367 flags |= ETH_FLAG_TXVLAN;
368 if (dev->features & NETIF_F_NTUPLE)
369 flags |= ETH_FLAG_NTUPLE;
370 if (dev->features & NETIF_F_RXHASH)
371 flags |= ETH_FLAG_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000372
373 return flags;
Michał Mirosławbc5787c62011-11-15 15:29:55 +0000374}
375
376static int __ethtool_set_flags(struct net_device *dev, u32 data)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000377{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000378 netdev_features_t features = 0, changed;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000379
Michał Mirosław02b3a552011-11-15 15:29:55 +0000380 if (data & ~ETH_ALL_FLAGS)
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000381 return -EINVAL;
382
Dragos Foianu8a731252013-07-13 14:43:00 +0100383 if (data & ETH_FLAG_LRO)
384 features |= NETIF_F_LRO;
385 if (data & ETH_FLAG_RXVLAN)
386 features |= NETIF_F_HW_VLAN_CTAG_RX;
387 if (data & ETH_FLAG_TXVLAN)
388 features |= NETIF_F_HW_VLAN_CTAG_TX;
389 if (data & ETH_FLAG_NTUPLE)
390 features |= NETIF_F_NTUPLE;
391 if (data & ETH_FLAG_RXHASH)
392 features |= NETIF_F_RXHASH;
Michał Mirosław02b3a552011-11-15 15:29:55 +0000393
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000394 /* allow changing only bits set in hw_features */
Michał Mirosław02b3a552011-11-15 15:29:55 +0000395 changed = (features ^ dev->features) & ETH_ALL_FEATURES;
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000396 if (changed & ~dev->hw_features)
397 return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
398
399 dev->wanted_features =
Michał Mirosław02b3a552011-11-15 15:29:55 +0000400 (dev->wanted_features & ~changed) | (features & changed);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000401
Michał Mirosław6cb6a272011-04-02 22:48:47 -0700402 __netdev_update_features(dev);
Michał Mirosławda8ac86c2011-02-15 16:59:18 +0000403
404 return 0;
405}
406
Alan Brady5a6cd6d2017-10-05 14:53:40 -0700407/* Given two link masks, AND them together and save the result in dst. */
408void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
409 struct ethtool_link_ksettings *src)
410{
411 unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
412 unsigned int idx = 0;
413
414 for (; idx < size; idx++) {
415 dst->link_modes.supported[idx] &=
416 src->link_modes.supported[idx];
417 dst->link_modes.advertising[idx] &=
418 src->link_modes.advertising[idx];
419 }
420}
421EXPORT_SYMBOL(ethtool_intersect_link_masks);
422
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200423void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
424 u32 legacy_u32)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800425{
426 bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
427 dst[0] = legacy_u32;
428}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200429EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800430
431/* return false if src had higher bits set. lower bits always updated. */
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200432bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
433 const unsigned long *src)
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800434{
435 bool retval = true;
436
437 /* TODO: following test will soon always be true */
438 if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
439 __ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
440
441 bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
442 bitmap_fill(ext, 32);
443 bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
444 if (bitmap_intersects(ext, src,
445 __ETHTOOL_LINK_MODE_MASK_NBITS)) {
446 /* src mask goes beyond bit 31 */
447 retval = false;
448 }
449 }
450 *legacy_u32 = src[0];
451 return retval;
452}
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200453EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800454
455/* return false if legacy contained non-0 deprecated fields
Niklas Söderlund95491e32017-10-20 01:32:08 +0200456 * maxtxpkt/maxrxpkt. rest of ksettings always updated
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800457 */
458static bool
459convert_legacy_settings_to_link_ksettings(
460 struct ethtool_link_ksettings *link_ksettings,
461 const struct ethtool_cmd *legacy_settings)
462{
463 bool retval = true;
464
465 memset(link_ksettings, 0, sizeof(*link_ksettings));
466
467 /* This is used to tell users that driver is still using these
468 * deprecated legacy fields, and they should not use
469 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
470 */
Niklas Söderlund95491e32017-10-20 01:32:08 +0200471 if (legacy_settings->maxtxpkt ||
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800472 legacy_settings->maxrxpkt)
473 retval = false;
474
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200475 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800476 link_ksettings->link_modes.supported,
477 legacy_settings->supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200478 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800479 link_ksettings->link_modes.advertising,
480 legacy_settings->advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200481 ethtool_convert_legacy_u32_to_link_mode(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800482 link_ksettings->link_modes.lp_advertising,
483 legacy_settings->lp_advertising);
484 link_ksettings->base.speed
485 = ethtool_cmd_speed(legacy_settings);
486 link_ksettings->base.duplex
487 = legacy_settings->duplex;
488 link_ksettings->base.port
489 = legacy_settings->port;
490 link_ksettings->base.phy_address
491 = legacy_settings->phy_address;
492 link_ksettings->base.autoneg
493 = legacy_settings->autoneg;
494 link_ksettings->base.mdio_support
495 = legacy_settings->mdio_support;
496 link_ksettings->base.eth_tp_mdix
497 = legacy_settings->eth_tp_mdix;
498 link_ksettings->base.eth_tp_mdix_ctrl
499 = legacy_settings->eth_tp_mdix_ctrl;
500 return retval;
501}
502
503/* return false if ksettings link modes had higher bits
504 * set. legacy_settings always updated (best effort)
505 */
506static bool
507convert_link_ksettings_to_legacy_settings(
508 struct ethtool_cmd *legacy_settings,
509 const struct ethtool_link_ksettings *link_ksettings)
510{
511 bool retval = true;
512
513 memset(legacy_settings, 0, sizeof(*legacy_settings));
514 /* this also clears the deprecated fields in legacy structure:
515 * __u8 transceiver;
516 * __u32 maxtxpkt;
517 * __u32 maxrxpkt;
518 */
519
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200520 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800521 &legacy_settings->supported,
522 link_ksettings->link_modes.supported);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200523 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800524 &legacy_settings->advertising,
525 link_ksettings->link_modes.advertising);
Philippe Reynes6d62b4d2016-04-15 00:34:59 +0200526 retval &= ethtool_convert_link_mode_to_legacy_u32(
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800527 &legacy_settings->lp_advertising,
528 link_ksettings->link_modes.lp_advertising);
529 ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
530 legacy_settings->duplex
531 = link_ksettings->base.duplex;
532 legacy_settings->port
533 = link_ksettings->base.port;
534 legacy_settings->phy_address
535 = link_ksettings->base.phy_address;
536 legacy_settings->autoneg
537 = link_ksettings->base.autoneg;
538 legacy_settings->mdio_support
539 = link_ksettings->base.mdio_support;
540 legacy_settings->eth_tp_mdix
541 = link_ksettings->base.eth_tp_mdix;
542 legacy_settings->eth_tp_mdix_ctrl
543 = link_ksettings->base.eth_tp_mdix_ctrl;
Florian Fainelli19cab882017-09-20 15:52:13 -0700544 legacy_settings->transceiver
545 = link_ksettings->base.transceiver;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800546 return retval;
547}
548
549/* number of 32-bit words to store the user's link mode bitmaps */
550#define __ETHTOOL_LINK_MODE_MASK_NU32 \
551 DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
552
553/* layout of the struct passed from/to userland */
554struct ethtool_link_usettings {
555 struct ethtool_link_settings base;
556 struct {
557 __u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
558 __u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
559 __u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
560 } link_modes;
561};
562
563/* Internal kernel helper to query a device ethtool_link_settings.
564 *
565 * Backward compatibility note: for compatibility with legacy drivers
566 * that implement only the ethtool_cmd API, this has to work with both
567 * drivers implementing get_link_ksettings API and drivers
568 * implementing get_settings API. When drivers implement get_settings
569 * and report ethtool_cmd deprecated fields
570 * (transceiver/maxrxpkt/maxtxpkt), these fields are silently ignored
571 * because the resulting struct ethtool_link_settings does not report them.
572 */
573int __ethtool_get_link_ksettings(struct net_device *dev,
574 struct ethtool_link_ksettings *link_ksettings)
575{
576 int err;
577 struct ethtool_cmd cmd;
578
579 ASSERT_RTNL();
580
581 if (dev->ethtool_ops->get_link_ksettings) {
582 memset(link_ksettings, 0, sizeof(*link_ksettings));
583 return dev->ethtool_ops->get_link_ksettings(dev,
584 link_ksettings);
585 }
586
587 /* driver doesn't support %ethtool_link_ksettings API. revert to
588 * legacy %ethtool_cmd API, unless it's not supported either.
589 * TODO: remove when ethtool_ops::get_settings disappears internally
590 */
David Decotigny3237fc62016-02-24 10:58:11 -0800591 if (!dev->ethtool_ops->get_settings)
592 return -EOPNOTSUPP;
593
594 memset(&cmd, 0, sizeof(cmd));
595 cmd.cmd = ETHTOOL_GSET;
596 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800597 if (err < 0)
598 return err;
599
600 /* we ignore deprecated fields transceiver/maxrxpkt/maxtxpkt
601 */
602 convert_legacy_settings_to_link_ksettings(link_ksettings, &cmd);
603 return err;
604}
605EXPORT_SYMBOL(__ethtool_get_link_ksettings);
606
607/* convert ethtool_link_usettings in user space to a kernel internal
608 * ethtool_link_ksettings. return 0 on success, errno on error.
609 */
610static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
611 const void __user *from)
612{
613 struct ethtool_link_usettings link_usettings;
614
615 if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
616 return -EFAULT;
617
618 memcpy(&to->base, &link_usettings.base, sizeof(to->base));
Yury Norov3aa56882018-02-06 15:38:06 -0800619 bitmap_from_arr32(to->link_modes.supported,
620 link_usettings.link_modes.supported,
621 __ETHTOOL_LINK_MODE_MASK_NBITS);
622 bitmap_from_arr32(to->link_modes.advertising,
623 link_usettings.link_modes.advertising,
624 __ETHTOOL_LINK_MODE_MASK_NBITS);
625 bitmap_from_arr32(to->link_modes.lp_advertising,
626 link_usettings.link_modes.lp_advertising,
627 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800628
629 return 0;
630}
631
632/* convert a kernel internal ethtool_link_ksettings to
633 * ethtool_link_usettings in user space. return 0 on success, errno on
634 * error.
635 */
636static int
637store_link_ksettings_for_user(void __user *to,
638 const struct ethtool_link_ksettings *from)
639{
640 struct ethtool_link_usettings link_usettings;
641
642 memcpy(&link_usettings.base, &from->base, sizeof(link_usettings));
Yury Norov3aa56882018-02-06 15:38:06 -0800643 bitmap_to_arr32(link_usettings.link_modes.supported,
644 from->link_modes.supported,
645 __ETHTOOL_LINK_MODE_MASK_NBITS);
646 bitmap_to_arr32(link_usettings.link_modes.advertising,
647 from->link_modes.advertising,
648 __ETHTOOL_LINK_MODE_MASK_NBITS);
649 bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
650 from->link_modes.lp_advertising,
651 __ETHTOOL_LINK_MODE_MASK_NBITS);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800652
653 if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
654 return -EFAULT;
655
656 return 0;
657}
658
659/* Query device for its ethtool_link_settings.
660 *
661 * Backward compatibility note: this function must fail when driver
662 * does not implement ethtool::get_link_ksettings, even if legacy
663 * ethtool_ops::get_settings is implemented. This tells new versions
664 * of ethtool that they should use the legacy API %ETHTOOL_GSET for
665 * this driver, so that they can correctly access the ethtool_cmd
666 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
667 * implements ethtool_ops::get_settings anymore.
668 */
669static int ethtool_get_link_ksettings(struct net_device *dev,
670 void __user *useraddr)
671{
672 int err = 0;
673 struct ethtool_link_ksettings link_ksettings;
674
675 ASSERT_RTNL();
676
677 if (!dev->ethtool_ops->get_link_ksettings)
678 return -EOPNOTSUPP;
679
680 /* handle bitmap nbits handshake */
681 if (copy_from_user(&link_ksettings.base, useraddr,
682 sizeof(link_ksettings.base)))
683 return -EFAULT;
684
685 if (__ETHTOOL_LINK_MODE_MASK_NU32
686 != link_ksettings.base.link_mode_masks_nwords) {
687 /* wrong link mode nbits requested */
688 memset(&link_ksettings, 0, sizeof(link_ksettings));
Ben Hutchings793cf872016-03-14 01:05:38 +0000689 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800690 /* send back number of words required as negative val */
691 compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
692 "need too many bits for link modes!");
693 link_ksettings.base.link_mode_masks_nwords
694 = -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
695
696 /* copy the base fields back to user, not the link
697 * mode bitmaps
698 */
699 if (copy_to_user(useraddr, &link_ksettings.base,
700 sizeof(link_ksettings.base)))
701 return -EFAULT;
702
703 return 0;
704 }
705
706 /* handshake successful: user/kernel agree on
707 * link_mode_masks_nwords
708 */
709
710 memset(&link_ksettings, 0, sizeof(link_ksettings));
711 err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
712 if (err < 0)
713 return err;
714
715 /* make sure we tell the right values to user */
716 link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
717 link_ksettings.base.link_mode_masks_nwords
718 = __ETHTOOL_LINK_MODE_MASK_NU32;
719
720 return store_link_ksettings_for_user(useraddr, &link_ksettings);
721}
722
723/* Update device ethtool_link_settings.
724 *
725 * Backward compatibility note: this function must fail when driver
726 * does not implement ethtool::set_link_ksettings, even if legacy
727 * ethtool_ops::set_settings is implemented. This tells new versions
728 * of ethtool that they should use the legacy API %ETHTOOL_SSET for
729 * this driver, so that they can correctly update the ethtool_cmd
730 * deprecated fields (transceiver/maxrxpkt/maxtxpkt), until no driver
731 * implements ethtool_ops::get_settings anymore.
732 */
733static int ethtool_set_link_ksettings(struct net_device *dev,
734 void __user *useraddr)
735{
736 int err;
737 struct ethtool_link_ksettings link_ksettings;
738
739 ASSERT_RTNL();
740
741 if (!dev->ethtool_ops->set_link_ksettings)
742 return -EOPNOTSUPP;
743
744 /* make sure nbits field has expected value */
745 if (copy_from_user(&link_ksettings.base, useraddr,
746 sizeof(link_ksettings.base)))
747 return -EFAULT;
748
749 if (__ETHTOOL_LINK_MODE_MASK_NU32
750 != link_ksettings.base.link_mode_masks_nwords)
751 return -EINVAL;
752
753 /* copy the whole structure, now that we know it has expected
754 * format
755 */
756 err = load_link_ksettings_from_user(&link_ksettings, useraddr);
757 if (err)
758 return err;
759
760 /* re-check nwords field, just in case */
761 if (__ETHTOOL_LINK_MODE_MASK_NU32
762 != link_ksettings.base.link_mode_masks_nwords)
763 return -EINVAL;
764
765 return dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
766}
767
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800768/* 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;
Stephen Hemminger71891e22017-12-29 10:02:52 -0800794 convert_link_ksettings_to_legacy_settings(&cmd,
795 &link_ksettings);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800796
797 /* send a sensible cmd tag back to user */
798 cmd.cmd = ETHTOOL_GSET;
799 } else {
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800800 /* driver doesn't support %ethtool_link_ksettings
801 * API. revert to legacy %ethtool_cmd API, unless it's
802 * not supported either.
803 */
David Decotigny3237fc62016-02-24 10:58:11 -0800804 int err;
805
806 if (!dev->ethtool_ops->get_settings)
807 return -EOPNOTSUPP;
808
809 memset(&cmd, 0, sizeof(cmd));
810 cmd.cmd = ETHTOOL_GSET;
811 err = dev->ethtool_ops->get_settings(dev, &cmd);
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800812 if (err < 0)
813 return err;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
817 return -EFAULT;
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820}
821
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800822/* Update device link settings with given ethtool_cmd.
823 *
824 * Backward compatibility note: for compatibility with legacy ethtool,
825 * this has to work with both drivers implementing set_link_ksettings
826 * API and drivers implementing set_settings API. When drivers
827 * implement set_link_ksettings and user's request updates deprecated
828 * ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
829 * warning is logged once (with name of 1st driver/device) to
830 * recommend user to upgrade ethtool, and the request is rejected.
831 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
833{
834 struct ethtool_cmd cmd;
835
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800836 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
839 return -EFAULT;
840
David Decotigny3f1ac7a2016-02-24 10:57:59 -0800841 /* first, try new %ethtool_link_ksettings API. */
842 if (dev->ethtool_ops->set_link_ksettings) {
843 struct ethtool_link_ksettings link_ksettings;
844
845 if (!convert_legacy_settings_to_link_ksettings(&link_ksettings,
846 &cmd))
847 return -EINVAL;
848
849 link_ksettings.base.cmd = ETHTOOL_SLINKSETTINGS;
850 link_ksettings.base.link_mode_masks_nwords
851 = __ETHTOOL_LINK_MODE_MASK_NU32;
852 return dev->ethtool_ops->set_link_ksettings(dev,
853 &link_ksettings);
854 }
855
856 /* legacy %ethtool_cmd API */
857
858 /* TODO: return -EOPNOTSUPP when ethtool_ops::get_settings
859 * disappears internally
860 */
861
862 if (!dev->ethtool_ops->set_settings)
863 return -EOPNOTSUPP;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 return dev->ethtool_ops->set_settings(dev, &cmd);
866}
867
chavey97f8aef2010-04-07 21:54:42 -0700868static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
869 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 struct ethtool_drvinfo info;
Stephen Hemminger76fd8592006-09-08 11:16:13 -0700872 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 memset(&info, 0, sizeof(info));
875 info.cmd = ETHTOOL_GDRVINFO;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000876 if (ops->get_drvinfo) {
Ben Hutchings01414802010-08-17 02:31:15 -0700877 ops->get_drvinfo(dev, &info);
878 } else if (dev->dev.parent && dev->dev.parent->driver) {
879 strlcpy(info.bus_info, dev_name(dev->dev.parent),
880 sizeof(info.bus_info));
881 strlcpy(info.driver, dev->dev.parent->driver->name,
882 sizeof(info.driver));
883 } else {
884 return -EOPNOTSUPP;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Jeff Garzik723b2f52010-03-03 22:51:50 +0000887 /*
888 * this method of obtaining string set info is deprecated;
Jeff Garzikd17792e2010-03-04 08:21:53 +0000889 * Use ETHTOOL_GSSET_INFO instead.
Jeff Garzik723b2f52010-03-03 22:51:50 +0000890 */
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000891 if (ops->get_sset_count) {
Jeff Garzikff03d492007-08-15 16:01:08 -0700892 int rc;
893
894 rc = ops->get_sset_count(dev, ETH_SS_TEST);
895 if (rc >= 0)
896 info.testinfo_len = rc;
897 rc = ops->get_sset_count(dev, ETH_SS_STATS);
898 if (rc >= 0)
899 info.n_stats = rc;
Jeff Garzik339bf022007-08-15 16:01:32 -0700900 rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
901 if (rc >= 0)
902 info.n_priv_flags = rc;
Jeff Garzikff03d492007-08-15 16:01:08 -0700903 }
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000904 if (ops->get_regs_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 info.regdump_len = ops->get_regs_len(dev);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +0000906 if (ops->get_eeprom_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 info.eedump_len = ops->get_eeprom_len(dev);
908
909 if (copy_to_user(useraddr, &info, sizeof(info)))
910 return -EFAULT;
911 return 0;
912}
913
Eric Dumazetf5c445e2010-03-08 12:17:04 -0800914static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
chavey97f8aef2010-04-07 21:54:42 -0700915 void __user *useraddr)
Jeff Garzik723b2f52010-03-03 22:51:50 +0000916{
917 struct ethtool_sset_info info;
Jeff Garzik723b2f52010-03-03 22:51:50 +0000918 u64 sset_mask;
919 int i, idx = 0, n_bits = 0, ret, rc;
920 u32 *info_buf = NULL;
921
Jeff Garzik723b2f52010-03-03 22:51:50 +0000922 if (copy_from_user(&info, useraddr, sizeof(info)))
923 return -EFAULT;
924
925 /* store copy of mask, because we zero struct later on */
926 sset_mask = info.sset_mask;
927 if (!sset_mask)
928 return 0;
929
930 /* calculate size of return buffer */
Jeff Garzikd17792e2010-03-04 08:21:53 +0000931 n_bits = hweight64(sset_mask);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000932
933 memset(&info, 0, sizeof(info));
934 info.cmd = ETHTOOL_GSSET_INFO;
935
936 info_buf = kzalloc(n_bits * sizeof(u32), GFP_USER);
937 if (!info_buf)
938 return -ENOMEM;
939
940 /*
941 * fill return buffer based on input bitmask and successful
942 * get_sset_count return
943 */
944 for (i = 0; i < 64; i++) {
945 if (!(sset_mask & (1ULL << i)))
946 continue;
947
Michał Mirosław340ae162011-02-15 16:59:16 +0000948 rc = __ethtool_get_sset_count(dev, i);
Jeff Garzik723b2f52010-03-03 22:51:50 +0000949 if (rc >= 0) {
950 info.sset_mask |= (1ULL << i);
951 info_buf[idx++] = rc;
952 }
953 }
954
955 ret = -EFAULT;
956 if (copy_to_user(useraddr, &info, sizeof(info)))
957 goto out;
958
959 useraddr += offsetof(struct ethtool_sset_info, data);
960 if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
961 goto out;
962
963 ret = 0;
964
965out:
966 kfree(info_buf);
967 return ret;
968}
969
chavey97f8aef2010-04-07 21:54:42 -0700970static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +0000971 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -0700972{
Ben Hutchingsbf988432010-06-28 08:45:58 +0000973 struct ethtool_rxnfc info;
974 size_t info_size = sizeof(info);
Ben Hutchings55664f32012-01-03 12:04:51 +0000975 int rc;
Santwona Behera0853ad62008-07-02 03:47:41 -0700976
Santwona Behera59089d82009-02-20 00:58:13 -0800977 if (!dev->ethtool_ops->set_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -0700978 return -EOPNOTSUPP;
979
Ben Hutchingsbf988432010-06-28 08:45:58 +0000980 /* struct ethtool_rxnfc was originally defined for
981 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
982 * members. User-space might still be using that
983 * definition. */
984 if (cmd == ETHTOOL_SRXFH)
985 info_size = (offsetof(struct ethtool_rxnfc, data) +
986 sizeof(info.data));
987
988 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -0700989 return -EFAULT;
990
Ben Hutchings55664f32012-01-03 12:04:51 +0000991 rc = dev->ethtool_ops->set_rxnfc(dev, &info);
992 if (rc)
993 return rc;
994
995 if (cmd == ETHTOOL_SRXCLSRLINS &&
996 copy_to_user(useraddr, &info, info_size))
997 return -EFAULT;
998
999 return 0;
Santwona Behera0853ad62008-07-02 03:47:41 -07001000}
1001
chavey97f8aef2010-04-07 21:54:42 -07001002static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
Ben Hutchingsbf988432010-06-28 08:45:58 +00001003 u32 cmd, void __user *useraddr)
Santwona Behera0853ad62008-07-02 03:47:41 -07001004{
1005 struct ethtool_rxnfc info;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001006 size_t info_size = sizeof(info);
Santwona Behera59089d82009-02-20 00:58:13 -08001007 const struct ethtool_ops *ops = dev->ethtool_ops;
1008 int ret;
1009 void *rule_buf = NULL;
Santwona Behera0853ad62008-07-02 03:47:41 -07001010
Santwona Behera59089d82009-02-20 00:58:13 -08001011 if (!ops->get_rxnfc)
Santwona Behera0853ad62008-07-02 03:47:41 -07001012 return -EOPNOTSUPP;
1013
Ben Hutchingsbf988432010-06-28 08:45:58 +00001014 /* struct ethtool_rxnfc was originally defined for
1015 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
1016 * members. User-space might still be using that
1017 * definition. */
1018 if (cmd == ETHTOOL_GRXFH)
1019 info_size = (offsetof(struct ethtool_rxnfc, data) +
1020 sizeof(info.data));
1021
1022 if (copy_from_user(&info, useraddr, info_size))
Santwona Behera0853ad62008-07-02 03:47:41 -07001023 return -EFAULT;
1024
Edward Cree84a1d9c42018-03-08 15:45:03 +00001025 /* If FLOW_RSS was requested then user-space must be using the
1026 * new definition, as FLOW_RSS is newer.
1027 */
1028 if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
1029 info_size = sizeof(info);
1030 if (copy_from_user(&info, useraddr, info_size))
1031 return -EFAULT;
1032 }
1033
Santwona Behera59089d82009-02-20 00:58:13 -08001034 if (info.cmd == ETHTOOL_GRXCLSRLALL) {
1035 if (info.rule_cnt > 0) {
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001036 if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
Kees Cookae6df5f2010-10-07 10:03:48 +00001037 rule_buf = kzalloc(info.rule_cnt * sizeof(u32),
Ben Hutchingsdb048b62010-06-28 08:44:07 +00001038 GFP_USER);
Santwona Behera59089d82009-02-20 00:58:13 -08001039 if (!rule_buf)
1040 return -ENOMEM;
1041 }
1042 }
Santwona Behera0853ad62008-07-02 03:47:41 -07001043
Santwona Behera59089d82009-02-20 00:58:13 -08001044 ret = ops->get_rxnfc(dev, &info, rule_buf);
1045 if (ret < 0)
1046 goto err_out;
1047
1048 ret = -EFAULT;
Ben Hutchingsbf988432010-06-28 08:45:58 +00001049 if (copy_to_user(useraddr, &info, info_size))
Santwona Behera59089d82009-02-20 00:58:13 -08001050 goto err_out;
1051
1052 if (rule_buf) {
1053 useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
1054 if (copy_to_user(useraddr, rule_buf,
1055 info.rule_cnt * sizeof(u32)))
1056 goto err_out;
1057 }
1058 ret = 0;
1059
1060err_out:
Wei Yongjunc9cacec2009-03-31 15:06:26 -07001061 kfree(rule_buf);
Santwona Behera59089d82009-02-20 00:58:13 -08001062
1063 return ret;
Santwona Behera0853ad62008-07-02 03:47:41 -07001064}
1065
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301066static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
1067 struct ethtool_rxnfc *rx_rings,
1068 u32 size)
1069{
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001070 int i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301071
1072 if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001073 return -EFAULT;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301074
1075 /* Validate ring indices */
Ben Hutchingsfb95cd82014-05-15 00:46:45 +01001076 for (i = 0; i < size; i++)
1077 if (indir[i] >= rx_rings->data)
1078 return -EINVAL;
1079
1080 return 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301081}
1082
Kim Jonesba905f52016-02-02 03:51:16 +00001083u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
Eric Dumazet960fb622014-11-16 06:23:05 -08001084
1085void netdev_rss_key_fill(void *buffer, size_t len)
1086{
1087 BUG_ON(len > sizeof(netdev_rss_key));
1088 net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
1089 memcpy(buffer, netdev_rss_key, len);
1090}
1091EXPORT_SYMBOL(netdev_rss_key_fill);
1092
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001093static int ethtool_get_max_rxfh_channel(struct net_device *dev, u32 *max)
1094{
1095 u32 dev_size, current_max = 0;
1096 u32 *indir;
1097 int ret;
1098
1099 if (!dev->ethtool_ops->get_rxfh_indir_size ||
1100 !dev->ethtool_ops->get_rxfh)
1101 return -EOPNOTSUPP;
1102 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1103 if (dev_size == 0)
1104 return -EOPNOTSUPP;
1105
1106 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
1107 if (!indir)
1108 return -ENOMEM;
1109
1110 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
1111 if (ret)
1112 goto out;
1113
1114 while (dev_size--)
1115 current_max = max(current_max, indir[dev_size]);
1116
1117 *max = current_max;
1118
1119out:
1120 kfree(indir);
1121 return ret;
1122}
1123
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001124static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
1125 void __user *useraddr)
1126{
Ben Hutchings7850f632011-12-15 13:55:01 +00001127 u32 user_size, dev_size;
1128 u32 *indir;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001129 int ret;
1130
Ben Hutchings7850f632011-12-15 13:55:01 +00001131 if (!dev->ethtool_ops->get_rxfh_indir_size ||
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001132 !dev->ethtool_ops->get_rxfh)
Ben Hutchings7850f632011-12-15 13:55:01 +00001133 return -EOPNOTSUPP;
1134 dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
1135 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001136 return -EOPNOTSUPP;
1137
Ben Hutchings7850f632011-12-15 13:55:01 +00001138 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001139 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001140 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001141 return -EFAULT;
1142
Ben Hutchings7850f632011-12-15 13:55:01 +00001143 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
1144 &dev_size, sizeof(dev_size)))
1145 return -EFAULT;
1146
1147 /* If the user buffer size is 0, this is just a query for the
1148 * device table size. Otherwise, if it's smaller than the
1149 * device table size it's an error.
1150 */
1151 if (user_size < dev_size)
1152 return user_size == 0 ? 0 : -EINVAL;
1153
1154 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001155 if (!indir)
1156 return -ENOMEM;
1157
Eyal Perry892311f2014-12-02 18:12:10 +02001158 ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001159 if (ret)
1160 goto out;
1161
Ben Hutchings7850f632011-12-15 13:55:01 +00001162 if (copy_to_user(useraddr +
1163 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
1164 indir, dev_size * sizeof(indir[0])))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001165 ret = -EFAULT;
1166
1167out:
1168 kfree(indir);
1169 return ret;
1170}
1171
1172static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
1173 void __user *useraddr)
1174{
Ben Hutchings7850f632011-12-15 13:55:01 +00001175 struct ethtool_rxnfc rx_rings;
1176 u32 user_size, dev_size, i;
1177 u32 *indir;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001178 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001179 int ret;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301180 u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001181
Ben Hutchingsfe62d002014-05-15 01:25:27 +01001182 if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001183 !ops->get_rxnfc)
Ben Hutchings7850f632011-12-15 13:55:01 +00001184 return -EOPNOTSUPP;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001185
1186 dev_size = ops->get_rxfh_indir_size(dev);
Ben Hutchings7850f632011-12-15 13:55:01 +00001187 if (dev_size == 0)
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001188 return -EOPNOTSUPP;
1189
Ben Hutchings7850f632011-12-15 13:55:01 +00001190 if (copy_from_user(&user_size,
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001191 useraddr + offsetof(struct ethtool_rxfh_indir, size),
Ben Hutchings7850f632011-12-15 13:55:01 +00001192 sizeof(user_size)))
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001193 return -EFAULT;
1194
Ben Hutchings278bc422011-12-15 13:56:49 +00001195 if (user_size != 0 && user_size != dev_size)
Ben Hutchings7850f632011-12-15 13:55:01 +00001196 return -EINVAL;
1197
1198 indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001199 if (!indir)
1200 return -ENOMEM;
1201
Ben Hutchings7850f632011-12-15 13:55:01 +00001202 rx_rings.cmd = ETHTOOL_GRXRINGS;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001203 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
Ben Hutchings7850f632011-12-15 13:55:01 +00001204 if (ret)
1205 goto out;
Ben Hutchings278bc422011-12-15 13:56:49 +00001206
1207 if (user_size == 0) {
1208 for (i = 0; i < dev_size; i++)
1209 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1210 } else {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301211 ret = ethtool_copy_validate_indir(indir,
1212 useraddr + ringidx_offset,
1213 &rx_rings,
1214 dev_size);
1215 if (ret)
Ben Hutchings7850f632011-12-15 13:55:01 +00001216 goto out;
Ben Hutchings7850f632011-12-15 13:55:01 +00001217 }
1218
Eyal Perry892311f2014-12-02 18:12:10 +02001219 ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001220 if (ret)
1221 goto out;
1222
1223 /* indicate whether rxfh was set to default */
1224 if (user_size == 0)
1225 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1226 else
1227 dev->priv_flags |= IFF_RXFH_CONFIGURED;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00001228
1229out:
1230 kfree(indir);
1231 return ret;
1232}
1233
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301234static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
1235 void __user *useraddr)
1236{
1237 int ret;
1238 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001239 u32 user_indir_size, user_key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301240 u32 dev_indir_size = 0, dev_key_size = 0;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001241 struct ethtool_rxfh rxfh;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301242 u32 total_size;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001243 u32 indir_bytes;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301244 u32 *indir = NULL;
Eyal Perry892311f2014-12-02 18:12:10 +02001245 u8 dev_hfunc = 0;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301246 u8 *hkey = NULL;
1247 u8 *rss_config;
1248
Eyal Perry892311f2014-12-02 18:12:10 +02001249 if (!ops->get_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301250 return -EOPNOTSUPP;
1251
1252 if (ops->get_rxfh_indir_size)
1253 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301254 if (ops->get_rxfh_key_size)
1255 dev_key_size = ops->get_rxfh_key_size(dev);
1256
Ben Hutchingsf062a382014-05-15 16:28:07 +01001257 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301258 return -EFAULT;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001259 user_indir_size = rxfh.indir_size;
1260 user_key_size = rxfh.key_size;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301261
Ben Hutchingsf062a382014-05-15 16:28:07 +01001262 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001263 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001264 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001265 /* Most drivers don't handle rss_context, check it's 0 as well */
1266 if (rxfh.rss_context && !ops->get_rxfh_context)
1267 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001268
1269 rxfh.indir_size = dev_indir_size;
1270 rxfh.key_size = dev_key_size;
1271 if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301272 return -EFAULT;
1273
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301274 if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
1275 (user_key_size && (user_key_size != dev_key_size)))
1276 return -EINVAL;
1277
1278 indir_bytes = user_indir_size * sizeof(indir[0]);
1279 total_size = indir_bytes + user_key_size;
1280 rss_config = kzalloc(total_size, GFP_USER);
1281 if (!rss_config)
1282 return -ENOMEM;
1283
1284 if (user_indir_size)
1285 indir = (u32 *)rss_config;
1286
1287 if (user_key_size)
1288 hkey = rss_config + indir_bytes;
1289
Edward Cree84a1d9c42018-03-08 15:45:03 +00001290 if (rxfh.rss_context)
1291 ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
1292 &dev_hfunc,
1293 rxfh.rss_context);
1294 else
1295 ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
Eyal Perry892311f2014-12-02 18:12:10 +02001296 if (ret)
1297 goto out;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301298
Eyal Perry892311f2014-12-02 18:12:10 +02001299 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
1300 &dev_hfunc, sizeof(rxfh.hfunc))) {
1301 ret = -EFAULT;
1302 } else if (copy_to_user(useraddr +
1303 offsetof(struct ethtool_rxfh, rss_config[0]),
1304 rss_config, total_size)) {
1305 ret = -EFAULT;
1306 }
1307out:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301308 kfree(rss_config);
1309
1310 return ret;
1311}
1312
1313static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
1314 void __user *useraddr)
1315{
1316 int ret;
1317 const struct ethtool_ops *ops = dev->ethtool_ops;
1318 struct ethtool_rxnfc rx_rings;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001319 struct ethtool_rxfh rxfh;
1320 u32 dev_indir_size = 0, dev_key_size = 0, i;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301321 u32 *indir = NULL, indir_bytes = 0;
1322 u8 *hkey = NULL;
1323 u8 *rss_config;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301324 u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
Edward Cree84a1d9c42018-03-08 15:45:03 +00001325 bool delete = false;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301326
Eyal Perry892311f2014-12-02 18:12:10 +02001327 if (!ops->get_rxnfc || !ops->set_rxfh)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301328 return -EOPNOTSUPP;
1329
1330 if (ops->get_rxfh_indir_size)
1331 dev_indir_size = ops->get_rxfh_indir_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301332 if (ops->get_rxfh_key_size)
Dan Carpenterd340c862015-02-20 13:54:05 +03001333 dev_key_size = ops->get_rxfh_key_size(dev);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301334
Ben Hutchingsf062a382014-05-15 16:28:07 +01001335 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301336 return -EFAULT;
1337
Ben Hutchingsf062a382014-05-15 16:28:07 +01001338 /* Check that reserved fields are 0 for now */
Edward Cree84a1d9c42018-03-08 15:45:03 +00001339 if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
Ben Hutchingsf062a382014-05-15 16:28:07 +01001340 return -EINVAL;
Edward Cree84a1d9c42018-03-08 15:45:03 +00001341 /* Most drivers don't handle rss_context, check it's 0 as well */
1342 if (rxfh.rss_context && !ops->set_rxfh_context)
1343 return -EOPNOTSUPP;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001344
Eyal Perry892311f2014-12-02 18:12:10 +02001345 /* If either indir, hash key or function is valid, proceed further.
1346 * Must request at least one change: indir size, hash key or function.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301347 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001348 if ((rxfh.indir_size &&
1349 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
1350 rxfh.indir_size != dev_indir_size) ||
1351 (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
1352 (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
Eyal Perry892311f2014-12-02 18:12:10 +02001353 rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301354 return -EINVAL;
1355
Ben Hutchingsf062a382014-05-15 16:28:07 +01001356 if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301357 indir_bytes = dev_indir_size * sizeof(indir[0]);
1358
Ben Hutchingsf062a382014-05-15 16:28:07 +01001359 rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301360 if (!rss_config)
1361 return -ENOMEM;
1362
1363 rx_rings.cmd = ETHTOOL_GRXRINGS;
1364 ret = ops->get_rxnfc(dev, &rx_rings, NULL);
1365 if (ret)
1366 goto out;
1367
Edward Cree84a1d9c42018-03-08 15:45:03 +00001368 /* rxfh.indir_size == 0 means reset the indir table to default (master
1369 * context) or delete the context (other RSS contexts).
Ben Hutchingsf062a382014-05-15 16:28:07 +01001370 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301371 */
Ben Hutchingsf062a382014-05-15 16:28:07 +01001372 if (rxfh.indir_size &&
1373 rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301374 indir = (u32 *)rss_config;
1375 ret = ethtool_copy_validate_indir(indir,
1376 useraddr + rss_cfg_offset,
1377 &rx_rings,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001378 rxfh.indir_size);
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301379 if (ret)
1380 goto out;
Ben Hutchingsf062a382014-05-15 16:28:07 +01001381 } else if (rxfh.indir_size == 0) {
Edward Cree84a1d9c42018-03-08 15:45:03 +00001382 if (rxfh.rss_context == 0) {
1383 indir = (u32 *)rss_config;
1384 for (i = 0; i < dev_indir_size; i++)
1385 indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
1386 } else {
1387 delete = true;
1388 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301389 }
1390
Ben Hutchingsf062a382014-05-15 16:28:07 +01001391 if (rxfh.key_size) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301392 hkey = rss_config + indir_bytes;
1393 if (copy_from_user(hkey,
1394 useraddr + rss_cfg_offset + indir_bytes,
Ben Hutchingsf062a382014-05-15 16:28:07 +01001395 rxfh.key_size)) {
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301396 ret = -EFAULT;
1397 goto out;
1398 }
1399 }
1400
Edward Cree84a1d9c42018-03-08 15:45:03 +00001401 if (rxfh.rss_context)
1402 ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
1403 &rxfh.rss_context, delete);
1404 else
1405 ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001406 if (ret)
1407 goto out;
1408
Edward Cree84a1d9c42018-03-08 15:45:03 +00001409 if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
1410 &rxfh.rss_context, sizeof(rxfh.rss_context)))
1411 ret = -EFAULT;
1412
1413 if (!rxfh.rss_context) {
1414 /* indicate whether rxfh was set to default */
1415 if (rxfh.indir_size == 0)
1416 dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
1417 else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
1418 dev->priv_flags |= IFF_RXFH_CONFIGURED;
1419 }
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05301420
1421out:
1422 kfree(rss_config);
1423 return ret;
1424}
1425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
1427{
1428 struct ethtool_regs regs;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001429 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 void *regbuf;
1431 int reglen, ret;
1432
1433 if (!ops->get_regs || !ops->get_regs_len)
1434 return -EOPNOTSUPP;
1435
1436 if (copy_from_user(&regs, useraddr, sizeof(regs)))
1437 return -EFAULT;
1438
1439 reglen = ops->get_regs_len(dev);
1440 if (regs.len > reglen)
1441 regs.len = reglen;
1442
Stanislaw Gruszka3808d342017-02-02 13:32:10 +01001443 regbuf = NULL;
1444 if (reglen) {
1445 regbuf = vzalloc(reglen);
1446 if (!regbuf)
1447 return -ENOMEM;
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 ops->get_regs(dev, &regs, regbuf);
1451
1452 ret = -EFAULT;
1453 if (copy_to_user(useraddr, &regs, sizeof(regs)))
1454 goto out;
1455 useraddr += offsetof(struct ethtool_regs, data);
Ben Hutchings67ae7cf2011-07-21 15:25:30 -07001456 if (regbuf && copy_to_user(useraddr, regbuf, regs.len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 goto out;
1458 ret = 0;
1459
1460 out:
Ben Hutchingsa77f5db2010-09-20 08:42:17 +00001461 vfree(regbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 return ret;
1463}
1464
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00001465static int ethtool_reset(struct net_device *dev, char __user *useraddr)
1466{
1467 struct ethtool_value reset;
1468 int ret;
1469
1470 if (!dev->ethtool_ops->reset)
1471 return -EOPNOTSUPP;
1472
1473 if (copy_from_user(&reset, useraddr, sizeof(reset)))
1474 return -EFAULT;
1475
1476 ret = dev->ethtool_ops->reset(dev, &reset.data);
1477 if (ret)
1478 return ret;
1479
1480 if (copy_to_user(useraddr, &reset, sizeof(reset)))
1481 return -EFAULT;
1482 return 0;
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
1486{
Roland Dreier8e557422010-02-11 12:14:23 -08001487 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 if (!dev->ethtool_ops->get_wol)
1490 return -EOPNOTSUPP;
1491
1492 dev->ethtool_ops->get_wol(dev, &wol);
1493
1494 if (copy_to_user(useraddr, &wol, sizeof(wol)))
1495 return -EFAULT;
1496 return 0;
1497}
1498
1499static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
1500{
1501 struct ethtool_wolinfo wol;
1502
1503 if (!dev->ethtool_ops->set_wol)
1504 return -EOPNOTSUPP;
1505
1506 if (copy_from_user(&wol, useraddr, sizeof(wol)))
1507 return -EFAULT;
1508
1509 return dev->ethtool_ops->set_wol(dev, &wol);
1510}
1511
Yuval Mintz80f12ec2012-06-06 17:13:06 +00001512static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
1513{
1514 struct ethtool_eee edata;
1515 int rc;
1516
1517 if (!dev->ethtool_ops->get_eee)
1518 return -EOPNOTSUPP;
1519
1520 memset(&edata, 0, sizeof(struct ethtool_eee));
1521 edata.cmd = ETHTOOL_GEEE;
1522 rc = dev->ethtool_ops->get_eee(dev, &edata);
1523
1524 if (rc)
1525 return rc;
1526
1527 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1528 return -EFAULT;
1529
1530 return 0;
1531}
1532
1533static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
1534{
1535 struct ethtool_eee edata;
1536
1537 if (!dev->ethtool_ops->set_eee)
1538 return -EOPNOTSUPP;
1539
1540 if (copy_from_user(&edata, useraddr, sizeof(edata)))
1541 return -EFAULT;
1542
1543 return dev->ethtool_ops->set_eee(dev, &edata);
1544}
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546static int ethtool_nway_reset(struct net_device *dev)
1547{
1548 if (!dev->ethtool_ops->nway_reset)
1549 return -EOPNOTSUPP;
1550
1551 return dev->ethtool_ops->nway_reset(dev);
1552}
1553
Ben Hutchingse596e6e2010-12-09 12:08:35 +00001554static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
1555{
1556 struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
1557
1558 if (!dev->ethtool_ops->get_link)
1559 return -EOPNOTSUPP;
1560
1561 edata.data = netif_running(dev) && dev->ethtool_ops->get_link(dev);
1562
1563 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1564 return -EFAULT;
1565 return 0;
1566}
1567
Ben Hutchings081d0942012-04-12 00:42:12 +00001568static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
1569 int (*getter)(struct net_device *,
1570 struct ethtool_eeprom *, u8 *),
1571 u32 total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 struct ethtool_eeprom eeprom;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001574 void __user *userbuf = useraddr + sizeof(eeprom);
1575 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001577 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1580 return -EFAULT;
1581
1582 /* Check for wrap and zero */
1583 if (eeprom.offset + eeprom.len <= eeprom.offset)
1584 return -EINVAL;
1585
1586 /* Check for exceeding total eeprom len */
Ben Hutchings081d0942012-04-12 00:42:12 +00001587 if (eeprom.offset + eeprom.len > total_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 return -EINVAL;
1589
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001590 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (!data)
1592 return -ENOMEM;
1593
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001594 bytes_remaining = eeprom.len;
1595 while (bytes_remaining > 0) {
1596 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Ben Hutchings081d0942012-04-12 00:42:12 +00001598 ret = getter(dev, &eeprom, data);
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001599 if (ret)
1600 break;
1601 if (copy_to_user(userbuf, data, eeprom.len)) {
1602 ret = -EFAULT;
1603 break;
1604 }
1605 userbuf += eeprom.len;
1606 eeprom.offset += eeprom.len;
1607 bytes_remaining -= eeprom.len;
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Mandeep Singh Bainesc5835df2008-04-24 20:55:56 -07001610 eeprom.len = userbuf - (useraddr + sizeof(eeprom));
1611 eeprom.offset -= eeprom.len;
1612 if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
1613 ret = -EFAULT;
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 kfree(data);
1616 return ret;
1617}
1618
Ben Hutchings081d0942012-04-12 00:42:12 +00001619static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
1620{
1621 const struct ethtool_ops *ops = dev->ethtool_ops;
1622
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001623 if (!ops->get_eeprom || !ops->get_eeprom_len ||
1624 !ops->get_eeprom_len(dev))
Ben Hutchings081d0942012-04-12 00:42:12 +00001625 return -EOPNOTSUPP;
1626
1627 return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
1628 ops->get_eeprom_len(dev));
1629}
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
1632{
1633 struct ethtool_eeprom eeprom;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001634 const struct ethtool_ops *ops = dev->ethtool_ops;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001635 void __user *userbuf = useraddr + sizeof(eeprom);
1636 u32 bytes_remaining;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 u8 *data;
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001638 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Guenter Roecke0fb6fb2014-10-30 20:50:15 -07001640 if (!ops->set_eeprom || !ops->get_eeprom_len ||
1641 !ops->get_eeprom_len(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return -EOPNOTSUPP;
1643
1644 if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
1645 return -EFAULT;
1646
1647 /* Check for wrap and zero */
1648 if (eeprom.offset + eeprom.len <= eeprom.offset)
1649 return -EINVAL;
1650
1651 /* Check for exceeding total eeprom len */
1652 if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
1653 return -EINVAL;
1654
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001655 data = kmalloc(PAGE_SIZE, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (!data)
1657 return -ENOMEM;
1658
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001659 bytes_remaining = eeprom.len;
1660 while (bytes_remaining > 0) {
1661 eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Mandeep Singh Bainesb131dd52008-04-15 19:24:17 -07001663 if (copy_from_user(data, userbuf, eeprom.len)) {
1664 ret = -EFAULT;
1665 break;
1666 }
1667 ret = ops->set_eeprom(dev, &eeprom, data);
1668 if (ret)
1669 break;
1670 userbuf += eeprom.len;
1671 eeprom.offset += eeprom.len;
1672 bytes_remaining -= eeprom.len;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 kfree(data);
1676 return ret;
1677}
1678
chavey97f8aef2010-04-07 21:54:42 -07001679static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
1680 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Roland Dreier8e557422010-02-11 12:14:23 -08001682 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 if (!dev->ethtool_ops->get_coalesce)
1685 return -EOPNOTSUPP;
1686
1687 dev->ethtool_ops->get_coalesce(dev, &coalesce);
1688
1689 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
1690 return -EFAULT;
1691 return 0;
1692}
1693
chavey97f8aef2010-04-07 21:54:42 -07001694static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
1695 void __user *useraddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 struct ethtool_coalesce coalesce;
1698
David S. Millerfa04ae52005-06-06 15:07:19 -07001699 if (!dev->ethtool_ops->set_coalesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return -EOPNOTSUPP;
1701
1702 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
1703 return -EFAULT;
1704
1705 return dev->ethtool_ops->set_coalesce(dev, &coalesce);
1706}
1707
1708static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
1709{
Roland Dreier8e557422010-02-11 12:14:23 -08001710 struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 if (!dev->ethtool_ops->get_ringparam)
1713 return -EOPNOTSUPP;
1714
1715 dev->ethtool_ops->get_ringparam(dev, &ringparam);
1716
1717 if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
1718 return -EFAULT;
1719 return 0;
1720}
1721
1722static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
1723{
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001724 struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001726 if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return -EOPNOTSUPP;
1728
1729 if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
1730 return -EFAULT;
1731
Eugenia Emantayev37e2d992018-01-08 16:00:24 +02001732 dev->ethtool_ops->get_ringparam(dev, &max);
1733
1734 /* ensure new ring parameters are within the maximums */
1735 if (ringparam.rx_pending > max.rx_max_pending ||
1736 ringparam.rx_mini_pending > max.rx_mini_max_pending ||
1737 ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
1738 ringparam.tx_pending > max.tx_max_pending)
1739 return -EINVAL;
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return dev->ethtool_ops->set_ringparam(dev, &ringparam);
1742}
1743
amit salecha8b5933c2011-04-07 01:58:42 +00001744static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
1745 void __user *useraddr)
1746{
1747 struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
1748
1749 if (!dev->ethtool_ops->get_channels)
1750 return -EOPNOTSUPP;
1751
1752 dev->ethtool_ops->get_channels(dev, &channels);
1753
1754 if (copy_to_user(useraddr, &channels, sizeof(channels)))
1755 return -EFAULT;
1756 return 0;
1757}
1758
1759static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
1760 void __user *useraddr)
1761{
Eran Ben Elisha31a86d12017-01-17 19:19:17 +02001762 struct ethtool_channels channels, max = { .cmd = ETHTOOL_GCHANNELS };
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001763 u32 max_rx_in_use = 0;
amit salecha8b5933c2011-04-07 01:58:42 +00001764
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001765 if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
amit salecha8b5933c2011-04-07 01:58:42 +00001766 return -EOPNOTSUPP;
1767
1768 if (copy_from_user(&channels, useraddr, sizeof(channels)))
1769 return -EFAULT;
1770
Keller, Jacob E8bf36862016-02-08 16:05:04 -08001771 dev->ethtool_ops->get_channels(dev, &max);
1772
1773 /* ensure new counts are within the maximums */
1774 if ((channels.rx_count > max.max_rx) ||
1775 (channels.tx_count > max.max_tx) ||
1776 (channels.combined_count > max.max_combined) ||
1777 (channels.other_count > max.max_other))
1778 return -EINVAL;
1779
Keller, Jacob Ed4ab4282016-02-08 16:05:03 -08001780 /* ensure the new Rx count fits within the configured Rx flow
1781 * indirection table settings */
1782 if (netif_is_rxfh_configured(dev) &&
1783 !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
1784 (channels.combined_count + channels.rx_count) <= max_rx_in_use)
1785 return -EINVAL;
1786
amit salecha8b5933c2011-04-07 01:58:42 +00001787 return dev->ethtool_ops->set_channels(dev, &channels);
1788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
1791{
1792 struct ethtool_pauseparam pauseparam = { ETHTOOL_GPAUSEPARAM };
1793
1794 if (!dev->ethtool_ops->get_pauseparam)
1795 return -EOPNOTSUPP;
1796
1797 dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
1798
1799 if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
1800 return -EFAULT;
1801 return 0;
1802}
1803
1804static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
1805{
1806 struct ethtool_pauseparam pauseparam;
1807
Jeff Garzike1b90c42006-07-17 12:54:40 -04001808 if (!dev->ethtool_ops->set_pauseparam)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return -EOPNOTSUPP;
1810
1811 if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
1812 return -EFAULT;
1813
1814 return dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
1818{
1819 struct ethtool_test test;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001820 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001822 int ret, test_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001824 if (!ops->self_test || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001825 return -EOPNOTSUPP;
1826
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001827 test_len = ops->get_sset_count(dev, ETH_SS_TEST);
Jeff Garzikff03d492007-08-15 16:01:08 -07001828 if (test_len < 0)
1829 return test_len;
1830 WARN_ON(test_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
1832 if (copy_from_user(&test, useraddr, sizeof(test)))
1833 return -EFAULT;
1834
Jeff Garzikff03d492007-08-15 16:01:08 -07001835 test.len = test_len;
1836 data = kmalloc(test_len * sizeof(u64), GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (!data)
1838 return -ENOMEM;
1839
1840 ops->self_test(dev, &test, data);
1841
1842 ret = -EFAULT;
1843 if (copy_to_user(useraddr, &test, sizeof(test)))
1844 goto out;
1845 useraddr += sizeof(test);
1846 if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
1847 goto out;
1848 ret = 0;
1849
1850 out:
1851 kfree(data);
1852 return ret;
1853}
1854
1855static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
1856{
1857 struct ethtool_gstrings gstrings;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 u8 *data;
1859 int ret;
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
1862 return -EFAULT;
1863
Michał Mirosław340ae162011-02-15 16:59:16 +00001864 ret = __ethtool_get_sset_count(dev, gstrings.string_set);
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001865 if (ret < 0)
1866 return ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001867 if (ret > S32_MAX / ETH_GSTRING_LEN)
1868 return -ENOMEM;
1869 WARN_ON_ONCE(!ret);
Jeff Garzikff03d492007-08-15 16:01:08 -07001870
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001871 gstrings.len = ret;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001872 data = vzalloc(gstrings.len * ETH_GSTRING_LEN);
1873 if (gstrings.len && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 return -ENOMEM;
1875
Michał Mirosław340ae162011-02-15 16:59:16 +00001876 __ethtool_get_strings(dev, gstrings.string_set, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 ret = -EFAULT;
1879 if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
1880 goto out;
1881 useraddr += sizeof(gstrings);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001882 if (gstrings.len &&
1883 copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto out;
1885 ret = 0;
1886
Michał Mirosław340ae162011-02-15 16:59:16 +00001887out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001888 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return ret;
1890}
1891
1892static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
1893{
1894 struct ethtool_value id;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001895 static bool busy;
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001896 const struct ethtool_ops *ops = dev->ethtool_ops;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001897 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001899 if (!ops->set_phys_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return -EOPNOTSUPP;
1901
Ben Hutchings68f512f2011-04-02 00:35:15 +01001902 if (busy)
1903 return -EBUSY;
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (copy_from_user(&id, useraddr, sizeof(id)))
1906 return -EFAULT;
1907
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001908 rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001909 if (rc < 0)
Ben Hutchings68f512f2011-04-02 00:35:15 +01001910 return rc;
1911
1912 /* Drop the RTNL lock while waiting, but prevent reentry or
1913 * removal of the device.
1914 */
1915 busy = true;
1916 dev_hold(dev);
1917 rtnl_unlock();
1918
1919 if (rc == 0) {
1920 /* Driver will handle this itself */
1921 schedule_timeout_interruptible(
Allan, Bruce W143780c2011-04-11 13:01:59 +00001922 id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001923 } else {
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001924 /* Driver expects to be called at twice the frequency in rc */
1925 int n = rc * 2, i, interval = HZ / n;
Ben Hutchings68f512f2011-04-02 00:35:15 +01001926
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001927 /* Count down seconds */
1928 do {
1929 /* Count down iterations per second */
1930 i = n;
1931 do {
1932 rtnl_lock();
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001933 rc = ops->set_phys_id(dev,
Allan, Bruce Wfce55922011-04-13 13:09:10 +00001934 (i & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
1935 rtnl_unlock();
1936 if (rc)
1937 break;
1938 schedule_timeout_interruptible(interval);
1939 } while (!signal_pending(current) && --i != 0);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001940 } while (!signal_pending(current) &&
1941 (id.data == 0 || --id.data != 0));
1942 }
1943
1944 rtnl_lock();
1945 dev_put(dev);
1946 busy = false;
1947
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00001948 (void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
Ben Hutchings68f512f2011-04-02 00:35:15 +01001949 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951
1952static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
1953{
1954 struct ethtool_stats stats;
Stephen Hemminger76fd8592006-09-08 11:16:13 -07001955 const struct ethtool_ops *ops = dev->ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 u64 *data;
Jeff Garzikff03d492007-08-15 16:01:08 -07001957 int ret, n_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001959 if (!ops->get_ethtool_stats || !ops->get_sset_count)
Jeff Garzikff03d492007-08-15 16:01:08 -07001960 return -EOPNOTSUPP;
1961
Ben Hutchingsa9828ec2009-10-01 11:33:03 +00001962 n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
Jeff Garzikff03d492007-08-15 16:01:08 -07001963 if (n_stats < 0)
1964 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001965 if (n_stats > S32_MAX / sizeof(u64))
1966 return -ENOMEM;
1967 WARN_ON_ONCE(!n_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (copy_from_user(&stats, useraddr, sizeof(stats)))
1969 return -EFAULT;
1970
Jeff Garzikff03d492007-08-15 16:01:08 -07001971 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001972 data = vzalloc(n_stats * sizeof(u64));
1973 if (n_stats && !data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return -ENOMEM;
1975
1976 ops->get_ethtool_stats(dev, &stats, data);
1977
1978 ret = -EFAULT;
1979 if (copy_to_user(useraddr, &stats, sizeof(stats)))
1980 goto out;
1981 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001982 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto out;
1984 ret = 0;
1985
1986 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08001987 vfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 return ret;
1989}
1990
Andrew Lunnf3a40942015-12-30 16:28:25 +01001991static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
1992{
1993 struct ethtool_stats stats;
1994 struct phy_device *phydev = dev->phydev;
1995 u64 *data;
1996 int ret, n_stats;
1997
1998 if (!phydev)
1999 return -EOPNOTSUPP;
2000
2001 n_stats = phy_get_sset_count(phydev);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002002 if (n_stats < 0)
2003 return n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002004 if (n_stats > S32_MAX / sizeof(u64))
2005 return -ENOMEM;
2006 WARN_ON_ONCE(!n_stats);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002007
2008 if (copy_from_user(&stats, useraddr, sizeof(stats)))
2009 return -EFAULT;
2010
2011 stats.n_stats = n_stats;
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002012 data = vzalloc(n_stats * sizeof(u64));
2013 if (n_stats && !data)
Andrew Lunnf3a40942015-12-30 16:28:25 +01002014 return -ENOMEM;
2015
2016 mutex_lock(&phydev->lock);
2017 phydev->drv->get_stats(phydev, &stats, data);
2018 mutex_unlock(&phydev->lock);
2019
2020 ret = -EFAULT;
2021 if (copy_to_user(useraddr, &stats, sizeof(stats)))
2022 goto out;
2023 useraddr += sizeof(stats);
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002024 if (n_stats && copy_to_user(useraddr, data, n_stats * sizeof(u64)))
Andrew Lunnf3a40942015-12-30 16:28:25 +01002025 goto out;
2026 ret = 0;
2027
2028 out:
Alexei Starovoitov4d1ceea2017-01-30 18:25:18 -08002029 vfree(data);
Andrew Lunnf3a40942015-12-30 16:28:25 +01002030 return ret;
2031}
2032
viro@ftp.linux.org.uk0bf0519d2005-09-05 03:26:18 +01002033static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
Jon Wetzela6f9a702005-08-20 17:15:54 -07002034{
2035 struct ethtool_perm_addr epaddr;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002036
Matthew Wilcox313674a2007-07-31 14:00:29 -07002037 if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
Jon Wetzela6f9a702005-08-20 17:15:54 -07002038 return -EFAULT;
2039
Matthew Wilcox313674a2007-07-31 14:00:29 -07002040 if (epaddr.size < dev->addr_len)
2041 return -ETOOSMALL;
2042 epaddr.size = dev->addr_len;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002043
Jon Wetzela6f9a702005-08-20 17:15:54 -07002044 if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
Matthew Wilcox313674a2007-07-31 14:00:29 -07002045 return -EFAULT;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002046 useraddr += sizeof(epaddr);
Matthew Wilcox313674a2007-07-31 14:00:29 -07002047 if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
2048 return -EFAULT;
2049 return 0;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002050}
2051
Jeff Garzik13c99b22007-08-15 16:01:56 -07002052static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
2053 u32 cmd, u32 (*actor)(struct net_device *))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002054{
Roland Dreier8e557422010-02-11 12:14:23 -08002055 struct ethtool_value edata = { .cmd = cmd };
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002056
Jeff Garzik13c99b22007-08-15 16:01:56 -07002057 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002058 return -EOPNOTSUPP;
2059
Jeff Garzik13c99b22007-08-15 16:01:56 -07002060 edata.data = actor(dev);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002061
2062 if (copy_to_user(useraddr, &edata, sizeof(edata)))
2063 return -EFAULT;
2064 return 0;
2065}
2066
Jeff Garzik13c99b22007-08-15 16:01:56 -07002067static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
2068 void (*actor)(struct net_device *, u32))
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002069{
2070 struct ethtool_value edata;
2071
Jeff Garzik13c99b22007-08-15 16:01:56 -07002072 if (!actor)
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002073 return -EOPNOTSUPP;
2074
2075 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2076 return -EFAULT;
2077
Jeff Garzik13c99b22007-08-15 16:01:56 -07002078 actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002079 return 0;
2080}
2081
Jeff Garzik13c99b22007-08-15 16:01:56 -07002082static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
2083 int (*actor)(struct net_device *, u32))
Jeff Garzik339bf022007-08-15 16:01:32 -07002084{
2085 struct ethtool_value edata;
2086
Jeff Garzik13c99b22007-08-15 16:01:56 -07002087 if (!actor)
Jeff Garzik339bf022007-08-15 16:01:32 -07002088 return -EOPNOTSUPP;
2089
2090 if (copy_from_user(&edata, useraddr, sizeof(edata)))
2091 return -EFAULT;
2092
Jeff Garzik13c99b22007-08-15 16:01:56 -07002093 return actor(dev, edata.data);
Jeff Garzik339bf022007-08-15 16:01:32 -07002094}
2095
chavey97f8aef2010-04-07 21:54:42 -07002096static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
2097 char __user *useraddr)
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002098{
2099 struct ethtool_flash efl;
2100
2101 if (copy_from_user(&efl, useraddr, sizeof(efl)))
2102 return -EFAULT;
2103
2104 if (!dev->ethtool_ops->flash_device)
2105 return -EOPNOTSUPP;
2106
Ben Hutchings786f5282012-02-01 09:32:25 +00002107 efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
2108
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002109 return dev->ethtool_ops->flash_device(dev, &efl);
2110}
2111
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002112static int ethtool_set_dump(struct net_device *dev,
2113 void __user *useraddr)
2114{
2115 struct ethtool_dump dump;
2116
2117 if (!dev->ethtool_ops->set_dump)
2118 return -EOPNOTSUPP;
2119
2120 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2121 return -EFAULT;
2122
2123 return dev->ethtool_ops->set_dump(dev, &dump);
2124}
2125
2126static int ethtool_get_dump_flag(struct net_device *dev,
2127 void __user *useraddr)
2128{
2129 int ret;
2130 struct ethtool_dump dump;
2131 const struct ethtool_ops *ops = dev->ethtool_ops;
2132
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002133 if (!ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002134 return -EOPNOTSUPP;
2135
2136 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2137 return -EFAULT;
2138
2139 ret = ops->get_dump_flag(dev, &dump);
2140 if (ret)
2141 return ret;
2142
2143 if (copy_to_user(useraddr, &dump, sizeof(dump)))
2144 return -EFAULT;
2145 return 0;
2146}
2147
2148static int ethtool_get_dump_data(struct net_device *dev,
2149 void __user *useraddr)
2150{
2151 int ret;
2152 __u32 len;
2153 struct ethtool_dump dump, tmp;
2154 const struct ethtool_ops *ops = dev->ethtool_ops;
2155 void *data = NULL;
2156
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002157 if (!ops->get_dump_data || !ops->get_dump_flag)
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002158 return -EOPNOTSUPP;
2159
2160 if (copy_from_user(&dump, useraddr, sizeof(dump)))
2161 return -EFAULT;
2162
2163 memset(&tmp, 0, sizeof(tmp));
2164 tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
2165 ret = ops->get_dump_flag(dev, &tmp);
2166 if (ret)
2167 return ret;
2168
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002169 len = min(tmp.len, dump.len);
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002170 if (!len)
2171 return -EFAULT;
2172
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002173 /* Don't ever let the driver think there's more space available
2174 * than it requested with .get_dump_flag().
2175 */
2176 dump.len = len;
2177
2178 /* Always allocate enough space to hold the whole thing so that the
2179 * driver does not need to check the length and bother with partial
2180 * dumping.
2181 */
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002182 data = vzalloc(tmp.len);
2183 if (!data)
2184 return -ENOMEM;
2185 ret = ops->get_dump_data(dev, &dump, data);
2186 if (ret)
2187 goto out;
2188
Michal Schmidtc590b5e2013-07-01 17:23:30 +02002189 /* There are two sane possibilities:
2190 * 1. The driver's .get_dump_data() does not touch dump.len.
2191 * 2. Or it may set dump.len to how much it really writes, which
2192 * should be tmp.len (or len if it can do a partial dump).
2193 * In any case respond to userspace with the actual length of data
2194 * it's receiving.
2195 */
2196 WARN_ON(dump.len != len && dump.len != tmp.len);
2197 dump.len = len;
2198
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002199 if (copy_to_user(useraddr, &dump, sizeof(dump))) {
2200 ret = -EFAULT;
2201 goto out;
2202 }
2203 useraddr += offsetof(struct ethtool_dump, data);
2204 if (copy_to_user(useraddr, data, len))
2205 ret = -EFAULT;
2206out:
2207 vfree(data);
2208 return ret;
2209}
2210
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002211static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
2212{
2213 int err = 0;
2214 struct ethtool_ts_info info;
2215 const struct ethtool_ops *ops = dev->ethtool_ops;
2216 struct phy_device *phydev = dev->phydev;
2217
2218 memset(&info, 0, sizeof(info));
2219 info.cmd = ETHTOOL_GET_TS_INFO;
2220
2221 if (phydev && phydev->drv && phydev->drv->ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002222 err = phydev->drv->ts_info(phydev, &info);
Jiri Pirkoc03a14e2013-01-07 09:02:08 +00002223 } else if (ops->get_ts_info) {
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002224 err = ops->get_ts_info(dev, &info);
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002225 } else {
2226 info.so_timestamping =
2227 SOF_TIMESTAMPING_RX_SOFTWARE |
2228 SOF_TIMESTAMPING_SOFTWARE;
2229 info.phc_index = -1;
2230 }
2231
2232 if (err)
2233 return err;
2234
2235 if (copy_to_user(useraddr, &info, sizeof(info)))
2236 err = -EFAULT;
2237
2238 return err;
2239}
2240
Ed Swierk2f438362015-01-02 17:27:56 -08002241static int __ethtool_get_module_info(struct net_device *dev,
2242 struct ethtool_modinfo *modinfo)
2243{
2244 const struct ethtool_ops *ops = dev->ethtool_ops;
2245 struct phy_device *phydev = dev->phydev;
2246
2247 if (phydev && phydev->drv && phydev->drv->module_info)
2248 return phydev->drv->module_info(phydev, modinfo);
2249
2250 if (ops->get_module_info)
2251 return ops->get_module_info(dev, modinfo);
2252
2253 return -EOPNOTSUPP;
2254}
2255
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002256static int ethtool_get_module_info(struct net_device *dev,
2257 void __user *useraddr)
2258{
2259 int ret;
2260 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002261
2262 if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
2263 return -EFAULT;
2264
Ed Swierk2f438362015-01-02 17:27:56 -08002265 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002266 if (ret)
2267 return ret;
2268
2269 if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
2270 return -EFAULT;
2271
2272 return 0;
2273}
2274
Ed Swierk2f438362015-01-02 17:27:56 -08002275static int __ethtool_get_module_eeprom(struct net_device *dev,
2276 struct ethtool_eeprom *ee, u8 *data)
2277{
2278 const struct ethtool_ops *ops = dev->ethtool_ops;
2279 struct phy_device *phydev = dev->phydev;
2280
2281 if (phydev && phydev->drv && phydev->drv->module_eeprom)
2282 return phydev->drv->module_eeprom(phydev, ee, data);
2283
2284 if (ops->get_module_eeprom)
2285 return ops->get_module_eeprom(dev, ee, data);
2286
2287 return -EOPNOTSUPP;
2288}
2289
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002290static int ethtool_get_module_eeprom(struct net_device *dev,
2291 void __user *useraddr)
2292{
2293 int ret;
2294 struct ethtool_modinfo modinfo;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002295
Ed Swierk2f438362015-01-02 17:27:56 -08002296 ret = __ethtool_get_module_info(dev, &modinfo);
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002297 if (ret)
2298 return ret;
2299
Ed Swierk2f438362015-01-02 17:27:56 -08002300 return ethtool_get_any_eeprom(dev, useraddr,
2301 __ethtool_get_module_eeprom,
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002302 modinfo.eeprom_len);
2303}
2304
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302305static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
2306{
2307 switch (tuna->id) {
2308 case ETHTOOL_RX_COPYBREAK:
Eric Dumazet1255a502014-10-05 12:35:21 +03002309 case ETHTOOL_TX_COPYBREAK:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302310 if (tuna->len != sizeof(u32) ||
2311 tuna->type_id != ETHTOOL_TUNABLE_U32)
2312 return -EINVAL;
2313 break;
2314 default:
2315 return -EINVAL;
2316 }
2317
2318 return 0;
2319}
2320
2321static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
2322{
2323 int ret;
2324 struct ethtool_tunable tuna;
2325 const struct ethtool_ops *ops = dev->ethtool_ops;
2326 void *data;
2327
2328 if (!ops->get_tunable)
2329 return -EOPNOTSUPP;
2330 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2331 return -EFAULT;
2332 ret = ethtool_tunable_valid(&tuna);
2333 if (ret)
2334 return ret;
2335 data = kmalloc(tuna.len, GFP_USER);
2336 if (!data)
2337 return -ENOMEM;
2338 ret = ops->get_tunable(dev, &tuna, data);
2339 if (ret)
2340 goto out;
2341 useraddr += sizeof(tuna);
2342 ret = -EFAULT;
2343 if (copy_to_user(useraddr, data, tuna.len))
2344 goto out;
2345 ret = 0;
2346
2347out:
2348 kfree(data);
2349 return ret;
2350}
2351
2352static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
2353{
2354 int ret;
2355 struct ethtool_tunable tuna;
2356 const struct ethtool_ops *ops = dev->ethtool_ops;
2357 void *data;
2358
2359 if (!ops->set_tunable)
2360 return -EOPNOTSUPP;
2361 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2362 return -EFAULT;
2363 ret = ethtool_tunable_valid(&tuna);
2364 if (ret)
2365 return ret;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302366 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002367 data = memdup_user(useraddr, tuna.len);
2368 if (IS_ERR(data))
2369 return PTR_ERR(data);
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302370 ret = ops->set_tunable(dev, &tuna, data);
2371
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302372 kfree(data);
2373 return ret;
2374}
2375
Kan Liang421797b2016-02-19 09:24:02 -05002376static int ethtool_get_per_queue_coalesce(struct net_device *dev,
2377 void __user *useraddr,
2378 struct ethtool_per_queue_op *per_queue_opt)
2379{
2380 u32 bit;
2381 int ret;
2382 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2383
2384 if (!dev->ethtool_ops->get_per_queue_coalesce)
2385 return -EOPNOTSUPP;
2386
2387 useraddr += sizeof(*per_queue_opt);
2388
Yury Norov3aa56882018-02-06 15:38:06 -08002389 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
2390 MAX_NUM_QUEUE);
Kan Liang421797b2016-02-19 09:24:02 -05002391
2392 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2393 struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
2394
2395 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
2396 if (ret != 0)
2397 return ret;
2398 if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
2399 return -EFAULT;
2400 useraddr += sizeof(coalesce);
2401 }
2402
2403 return 0;
2404}
2405
Kan Liangf38d1382016-02-19 09:24:03 -05002406static int ethtool_set_per_queue_coalesce(struct net_device *dev,
2407 void __user *useraddr,
2408 struct ethtool_per_queue_op *per_queue_opt)
2409{
2410 u32 bit;
2411 int i, ret = 0;
2412 int n_queue;
2413 struct ethtool_coalesce *backup = NULL, *tmp = NULL;
2414 DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
2415
2416 if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
2417 (!dev->ethtool_ops->get_per_queue_coalesce))
2418 return -EOPNOTSUPP;
2419
2420 useraddr += sizeof(*per_queue_opt);
2421
Yury Norov3aa56882018-02-06 15:38:06 -08002422 bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
Kan Liangf38d1382016-02-19 09:24:03 -05002423 n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
2424 tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
2425 if (!backup)
2426 return -ENOMEM;
2427
2428 for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
2429 struct ethtool_coalesce coalesce;
2430
2431 ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
2432 if (ret != 0)
2433 goto roll_back;
2434
2435 tmp++;
2436
2437 if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
2438 ret = -EFAULT;
2439 goto roll_back;
2440 }
2441
2442 ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
2443 if (ret != 0)
2444 goto roll_back;
2445
2446 useraddr += sizeof(coalesce);
2447 }
2448
2449roll_back:
2450 if (ret != 0) {
2451 tmp = backup;
2452 for_each_set_bit(i, queue_mask, bit) {
2453 dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
2454 tmp++;
2455 }
2456 }
2457 kfree(backup);
2458
2459 return ret;
2460}
2461
Kan Liangac2c7ad2016-02-19 09:24:01 -05002462static int ethtool_set_per_queue(struct net_device *dev, void __user *useraddr)
2463{
2464 struct ethtool_per_queue_op per_queue_opt;
2465
2466 if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
2467 return -EFAULT;
2468
2469 switch (per_queue_opt.sub_command) {
Kan Liang421797b2016-02-19 09:24:02 -05002470 case ETHTOOL_GCOALESCE:
2471 return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangf38d1382016-02-19 09:24:03 -05002472 case ETHTOOL_SCOALESCE:
2473 return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
Kan Liangac2c7ad2016-02-19 09:24:01 -05002474 default:
2475 return -EOPNOTSUPP;
2476 };
2477}
2478
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002479static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
2480{
2481 switch (tuna->id) {
Raju Lakkaraju65feddd2016-11-17 13:07:23 +01002482 case ETHTOOL_PHY_DOWNSHIFT:
2483 if (tuna->len != sizeof(u8) ||
2484 tuna->type_id != ETHTOOL_TUNABLE_U8)
2485 return -EINVAL;
2486 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002487 default:
2488 return -EINVAL;
2489 }
2490
2491 return 0;
2492}
2493
2494static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
2495{
2496 int ret;
2497 struct ethtool_tunable tuna;
2498 struct phy_device *phydev = dev->phydev;
2499 void *data;
2500
2501 if (!(phydev && phydev->drv && phydev->drv->get_tunable))
2502 return -EOPNOTSUPP;
2503
2504 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2505 return -EFAULT;
2506 ret = ethtool_phy_tunable_valid(&tuna);
2507 if (ret)
2508 return ret;
2509 data = kmalloc(tuna.len, GFP_USER);
2510 if (!data)
2511 return -ENOMEM;
Florian Fainelli4b652462016-11-22 13:55:31 -08002512 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002513 ret = phydev->drv->get_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002514 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002515 if (ret)
2516 goto out;
2517 useraddr += sizeof(tuna);
2518 ret = -EFAULT;
2519 if (copy_to_user(useraddr, data, tuna.len))
2520 goto out;
2521 ret = 0;
2522
2523out:
2524 kfree(data);
2525 return ret;
2526}
2527
2528static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
2529{
2530 int ret;
2531 struct ethtool_tunable tuna;
2532 struct phy_device *phydev = dev->phydev;
2533 void *data;
2534
2535 if (!(phydev && phydev->drv && phydev->drv->set_tunable))
2536 return -EOPNOTSUPP;
2537 if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
2538 return -EFAULT;
2539 ret = ethtool_phy_tunable_valid(&tuna);
2540 if (ret)
2541 return ret;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002542 useraddr += sizeof(tuna);
Al Viro30e7e3e2017-05-13 18:31:26 -04002543 data = memdup_user(useraddr, tuna.len);
2544 if (IS_ERR(data))
2545 return PTR_ERR(data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002546 mutex_lock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002547 ret = phydev->drv->set_tunable(phydev, &tuna, data);
Florian Fainelli4b652462016-11-22 13:55:31 -08002548 mutex_unlock(&phydev->lock);
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002549
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002550 kfree(data);
2551 return ret;
2552}
2553
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002554static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
2555{
2556 struct ethtool_fecparam fecparam = { ETHTOOL_GFECPARAM };
Edward Creea6d50512018-02-28 19:15:58 +00002557 int rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002558
2559 if (!dev->ethtool_ops->get_fecparam)
2560 return -EOPNOTSUPP;
2561
Edward Creea6d50512018-02-28 19:15:58 +00002562 rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
2563 if (rc)
2564 return rc;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002565
2566 if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
2567 return -EFAULT;
2568 return 0;
2569}
2570
2571static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
2572{
2573 struct ethtool_fecparam fecparam;
2574
2575 if (!dev->ethtool_ops->set_fecparam)
2576 return -EOPNOTSUPP;
2577
2578 if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
2579 return -EFAULT;
2580
2581 return dev->ethtool_ops->set_fecparam(dev, &fecparam);
2582}
2583
Yan Burman600fed52013-06-03 02:03:34 +00002584/* The main entry point in this file. Called from net/core/dev_ioctl.c */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Eric W. Biederman881d9662007-09-17 11:56:21 -07002586int dev_ethtool(struct net *net, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Eric W. Biederman881d9662007-09-17 11:56:21 -07002588 struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 void __user *useraddr = ifr->ifr_data;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002590 u32 ethcmd, sub_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 int rc;
Bjørn Morkb29d3142013-05-01 23:06:42 +00002592 netdev_features_t old_features;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (!dev || !netif_device_present(dev))
2595 return -ENODEV;
2596
chavey97f8aef2010-04-07 21:54:42 -07002597 if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 return -EFAULT;
2599
Kan Liangac2c7ad2016-02-19 09:24:01 -05002600 if (ethcmd == ETHTOOL_PERQUEUE) {
2601 if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
2602 return -EFAULT;
2603 } else {
2604 sub_cmd = ethcmd;
2605 }
Stephen Hemminger75f31232006-09-28 15:13:37 -07002606 /* Allow some commands to be done by anyone */
Kan Liangac2c7ad2016-02-19 09:24:01 -05002607 switch (sub_cmd) {
stephen hemminger0fdc1002010-08-23 10:24:18 +00002608 case ETHTOOL_GSET:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002609 case ETHTOOL_GDRVINFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002610 case ETHTOOL_GMSGLVL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002611 case ETHTOOL_GLINK:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002612 case ETHTOOL_GCOALESCE:
2613 case ETHTOOL_GRINGPARAM:
2614 case ETHTOOL_GPAUSEPARAM:
2615 case ETHTOOL_GRXCSUM:
2616 case ETHTOOL_GTXCSUM:
2617 case ETHTOOL_GSG:
Michał Mirosławf80400a2012-01-22 00:20:40 +00002618 case ETHTOOL_GSSET_INFO:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002619 case ETHTOOL_GSTRINGS:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002620 case ETHTOOL_GSTATS:
Andrew Lunnf3a40942015-12-30 16:28:25 +01002621 case ETHTOOL_GPHYSTATS:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002622 case ETHTOOL_GTSO:
2623 case ETHTOOL_GPERMADDR:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002624 case ETHTOOL_GGSO:
stephen hemminger1cab8192010-02-11 13:48:29 +00002625 case ETHTOOL_GGRO:
Jeff Garzik339bf022007-08-15 16:01:32 -07002626 case ETHTOOL_GFLAGS:
2627 case ETHTOOL_GPFLAGS:
Santwona Behera0853ad62008-07-02 03:47:41 -07002628 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002629 case ETHTOOL_GRXRINGS:
2630 case ETHTOOL_GRXCLSRLCNT:
2631 case ETHTOOL_GRXCLSRULE:
2632 case ETHTOOL_GRXCLSRLALL:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002633 case ETHTOOL_GRXFHINDIR:
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302634 case ETHTOOL_GRSSH:
Michał Mirosław5455c692011-02-15 16:59:17 +00002635 case ETHTOOL_GFEATURES:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002636 case ETHTOOL_GCHANNELS:
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002637 case ETHTOOL_GET_TS_INFO:
Ben Hutchings2da45db2012-06-12 13:05:41 +00002638 case ETHTOOL_GEEE:
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302639 case ETHTOOL_GTUNABLE:
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002640 case ETHTOOL_PHY_GTUNABLE:
Miroslav Lichvar8006f6b2016-11-24 10:55:06 +01002641 case ETHTOOL_GLINKSETTINGS:
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002642 case ETHTOOL_GFECPARAM:
Stephen Hemminger75f31232006-09-28 15:13:37 -07002643 break;
2644 default:
Eric W. Biederman5e1fccc2012-11-16 03:03:04 +00002645 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Stephen Hemminger75f31232006-09-28 15:13:37 -07002646 return -EPERM;
2647 }
2648
chavey97f8aef2010-04-07 21:54:42 -07002649 if (dev->ethtool_ops->begin) {
2650 rc = dev->ethtool_ops->begin(dev);
2651 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return rc;
chavey97f8aef2010-04-07 21:54:42 -07002653 }
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002654 old_features = dev->features;
2655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 switch (ethcmd) {
2657 case ETHTOOL_GSET:
2658 rc = ethtool_get_settings(dev, useraddr);
2659 break;
2660 case ETHTOOL_SSET:
2661 rc = ethtool_set_settings(dev, useraddr);
2662 break;
2663 case ETHTOOL_GDRVINFO:
2664 rc = ethtool_get_drvinfo(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 break;
2666 case ETHTOOL_GREGS:
2667 rc = ethtool_get_regs(dev, useraddr);
2668 break;
2669 case ETHTOOL_GWOL:
2670 rc = ethtool_get_wol(dev, useraddr);
2671 break;
2672 case ETHTOOL_SWOL:
2673 rc = ethtool_set_wol(dev, useraddr);
2674 break;
2675 case ETHTOOL_GMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002676 rc = ethtool_get_value(dev, useraddr, ethcmd,
2677 dev->ethtool_ops->get_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 break;
2679 case ETHTOOL_SMSGLVL:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002680 rc = ethtool_set_value_void(dev, useraddr,
2681 dev->ethtool_ops->set_msglevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 break;
Yuval Mintz80f12ec2012-06-06 17:13:06 +00002683 case ETHTOOL_GEEE:
2684 rc = ethtool_get_eee(dev, useraddr);
2685 break;
2686 case ETHTOOL_SEEE:
2687 rc = ethtool_set_eee(dev, useraddr);
2688 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 case ETHTOOL_NWAY_RST:
2690 rc = ethtool_nway_reset(dev);
2691 break;
2692 case ETHTOOL_GLINK:
Ben Hutchingse596e6e2010-12-09 12:08:35 +00002693 rc = ethtool_get_link(dev, useraddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 break;
2695 case ETHTOOL_GEEPROM:
2696 rc = ethtool_get_eeprom(dev, useraddr);
2697 break;
2698 case ETHTOOL_SEEPROM:
2699 rc = ethtool_set_eeprom(dev, useraddr);
2700 break;
2701 case ETHTOOL_GCOALESCE:
2702 rc = ethtool_get_coalesce(dev, useraddr);
2703 break;
2704 case ETHTOOL_SCOALESCE:
2705 rc = ethtool_set_coalesce(dev, useraddr);
2706 break;
2707 case ETHTOOL_GRINGPARAM:
2708 rc = ethtool_get_ringparam(dev, useraddr);
2709 break;
2710 case ETHTOOL_SRINGPARAM:
2711 rc = ethtool_set_ringparam(dev, useraddr);
2712 break;
2713 case ETHTOOL_GPAUSEPARAM:
2714 rc = ethtool_get_pauseparam(dev, useraddr);
2715 break;
2716 case ETHTOOL_SPAUSEPARAM:
2717 rc = ethtool_set_pauseparam(dev, useraddr);
2718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 case ETHTOOL_TEST:
2720 rc = ethtool_self_test(dev, useraddr);
2721 break;
2722 case ETHTOOL_GSTRINGS:
2723 rc = ethtool_get_strings(dev, useraddr);
2724 break;
2725 case ETHTOOL_PHYS_ID:
2726 rc = ethtool_phys_id(dev, useraddr);
2727 break;
2728 case ETHTOOL_GSTATS:
2729 rc = ethtool_get_stats(dev, useraddr);
2730 break;
Jon Wetzela6f9a702005-08-20 17:15:54 -07002731 case ETHTOOL_GPERMADDR:
2732 rc = ethtool_get_perm_addr(dev, useraddr);
2733 break;
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002734 case ETHTOOL_GFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002735 rc = ethtool_get_value(dev, useraddr, ethcmd,
Michał Mirosławbc5787c62011-11-15 15:29:55 +00002736 __ethtool_get_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002737 break;
2738 case ETHTOOL_SFLAGS:
Michał Mirosławda8ac86c2011-02-15 16:59:18 +00002739 rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
Jeff Garzik3ae7c0b2007-08-15 16:00:51 -07002740 break;
Jeff Garzik339bf022007-08-15 16:01:32 -07002741 case ETHTOOL_GPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002742 rc = ethtool_get_value(dev, useraddr, ethcmd,
2743 dev->ethtool_ops->get_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002744 break;
2745 case ETHTOOL_SPFLAGS:
Jeff Garzik13c99b22007-08-15 16:01:56 -07002746 rc = ethtool_set_value(dev, useraddr,
2747 dev->ethtool_ops->set_priv_flags);
Jeff Garzik339bf022007-08-15 16:01:32 -07002748 break;
Santwona Behera0853ad62008-07-02 03:47:41 -07002749 case ETHTOOL_GRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002750 case ETHTOOL_GRXRINGS:
2751 case ETHTOOL_GRXCLSRLCNT:
2752 case ETHTOOL_GRXCLSRULE:
2753 case ETHTOOL_GRXCLSRLALL:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002754 rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002755 break;
2756 case ETHTOOL_SRXFH:
Santwona Behera59089d82009-02-20 00:58:13 -08002757 case ETHTOOL_SRXCLSRLDEL:
2758 case ETHTOOL_SRXCLSRLINS:
Ben Hutchingsbf988432010-06-28 08:45:58 +00002759 rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
Santwona Behera0853ad62008-07-02 03:47:41 -07002760 break;
Ajit Khaparde05c6a8d2009-09-02 17:02:55 +00002761 case ETHTOOL_FLASHDEV:
2762 rc = ethtool_flash_device(dev, useraddr);
2763 break;
Ben Hutchingsd73d3a82009-10-05 10:59:58 +00002764 case ETHTOOL_RESET:
2765 rc = ethtool_reset(dev, useraddr);
2766 break;
Jeff Garzik723b2f52010-03-03 22:51:50 +00002767 case ETHTOOL_GSSET_INFO:
2768 rc = ethtool_get_sset_info(dev, useraddr);
2769 break;
Ben Hutchingsa5b6ee22010-06-30 05:05:23 +00002770 case ETHTOOL_GRXFHINDIR:
2771 rc = ethtool_get_rxfh_indir(dev, useraddr);
2772 break;
2773 case ETHTOOL_SRXFHINDIR:
2774 rc = ethtool_set_rxfh_indir(dev, useraddr);
2775 break;
Venkata Duvvuru3de0b592014-04-21 15:37:59 +05302776 case ETHTOOL_GRSSH:
2777 rc = ethtool_get_rxfh(dev, useraddr);
2778 break;
2779 case ETHTOOL_SRSSH:
2780 rc = ethtool_set_rxfh(dev, useraddr);
2781 break;
Michał Mirosław5455c692011-02-15 16:59:17 +00002782 case ETHTOOL_GFEATURES:
2783 rc = ethtool_get_features(dev, useraddr);
2784 break;
2785 case ETHTOOL_SFEATURES:
2786 rc = ethtool_set_features(dev, useraddr);
2787 break;
Michał Mirosław0a417702011-02-15 16:59:17 +00002788 case ETHTOOL_GTXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002789 case ETHTOOL_GRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002790 case ETHTOOL_GSG:
2791 case ETHTOOL_GTSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002792 case ETHTOOL_GGSO:
2793 case ETHTOOL_GGRO:
2794 rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
2795 break;
2796 case ETHTOOL_STXCSUM:
Michał Mirosławe83d3602011-02-15 16:59:18 +00002797 case ETHTOOL_SRXCSUM:
Michał Mirosław0a417702011-02-15 16:59:17 +00002798 case ETHTOOL_SSG:
2799 case ETHTOOL_STSO:
Michał Mirosław0a417702011-02-15 16:59:17 +00002800 case ETHTOOL_SGSO:
2801 case ETHTOOL_SGRO:
2802 rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
2803 break;
amit salecha8b5933c2011-04-07 01:58:42 +00002804 case ETHTOOL_GCHANNELS:
2805 rc = ethtool_get_channels(dev, useraddr);
2806 break;
2807 case ETHTOOL_SCHANNELS:
2808 rc = ethtool_set_channels(dev, useraddr);
2809 break;
Anirban Chakraborty29dd54b2011-05-12 12:48:32 +00002810 case ETHTOOL_SET_DUMP:
2811 rc = ethtool_set_dump(dev, useraddr);
2812 break;
2813 case ETHTOOL_GET_DUMP_FLAG:
2814 rc = ethtool_get_dump_flag(dev, useraddr);
2815 break;
2816 case ETHTOOL_GET_DUMP_DATA:
2817 rc = ethtool_get_dump_data(dev, useraddr);
2818 break;
Richard Cochranc8f3a8c2012-04-03 22:59:17 +00002819 case ETHTOOL_GET_TS_INFO:
2820 rc = ethtool_get_ts_info(dev, useraddr);
2821 break;
Stuart Hodgson41c3cb62012-04-19 09:44:42 +01002822 case ETHTOOL_GMODULEINFO:
2823 rc = ethtool_get_module_info(dev, useraddr);
2824 break;
2825 case ETHTOOL_GMODULEEEPROM:
2826 rc = ethtool_get_module_eeprom(dev, useraddr);
2827 break;
Govindarajulu Varadarajanf0db9b02014-09-03 03:17:20 +05302828 case ETHTOOL_GTUNABLE:
2829 rc = ethtool_get_tunable(dev, useraddr);
2830 break;
2831 case ETHTOOL_STUNABLE:
2832 rc = ethtool_set_tunable(dev, useraddr);
2833 break;
Andrew Lunnf3a40942015-12-30 16:28:25 +01002834 case ETHTOOL_GPHYSTATS:
2835 rc = ethtool_get_phy_stats(dev, useraddr);
2836 break;
Kan Liangac2c7ad2016-02-19 09:24:01 -05002837 case ETHTOOL_PERQUEUE:
2838 rc = ethtool_set_per_queue(dev, useraddr);
2839 break;
David Decotigny3f1ac7a2016-02-24 10:57:59 -08002840 case ETHTOOL_GLINKSETTINGS:
2841 rc = ethtool_get_link_ksettings(dev, useraddr);
2842 break;
2843 case ETHTOOL_SLINKSETTINGS:
2844 rc = ethtool_set_link_ksettings(dev, useraddr);
2845 break;
Raju Lakkaraju968ad9d2016-11-17 13:07:21 +01002846 case ETHTOOL_PHY_GTUNABLE:
2847 rc = get_phy_tunable(dev, useraddr);
2848 break;
2849 case ETHTOOL_PHY_STUNABLE:
2850 rc = set_phy_tunable(dev, useraddr);
2851 break;
Vidya Sagar Ravipati1a5f3da2017-07-27 16:47:26 -07002852 case ETHTOOL_GFECPARAM:
2853 rc = ethtool_get_fecparam(dev, useraddr);
2854 break;
2855 case ETHTOOL_SFECPARAM:
2856 rc = ethtool_set_fecparam(dev, useraddr);
2857 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 default:
Matthew Wilcox61a44b92007-07-31 14:00:02 -07002859 rc = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +09002861
Stephen Hemmingere71a4782007-04-10 20:10:33 -07002862 if (dev->ethtool_ops->complete)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 dev->ethtool_ops->complete(dev);
Stephen Hemmingerd8a33ac2005-05-29 14:13:47 -07002864
2865 if (old_features != dev->features)
2866 netdev_features_change(dev);
2867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}