xref: /openbmc/linux/net/ethtool/ioctl.c (revision 2b3082c6)
19ce48e5aSMichal Kubecek // SPDX-License-Identifier: GPL-2.0-or-later
29ce48e5aSMichal Kubecek /*
39ce48e5aSMichal Kubecek  * net/core/ethtool.c - Ethtool ioctl handler
49ce48e5aSMichal Kubecek  * Copyright (c) 2003 Matthew Wilcox <matthew@wil.cx>
59ce48e5aSMichal Kubecek  *
69ce48e5aSMichal Kubecek  * This file is where we call all the ethtool_ops commands to get
79ce48e5aSMichal Kubecek  * the information ethtool needs.
89ce48e5aSMichal Kubecek  */
99ce48e5aSMichal Kubecek 
10dd98d289SArnd Bergmann #include <linux/compat.h>
11b6459415SJakub Kicinski #include <linux/etherdevice.h>
129ce48e5aSMichal Kubecek #include <linux/module.h>
139ce48e5aSMichal Kubecek #include <linux/types.h>
149ce48e5aSMichal Kubecek #include <linux/capability.h>
159ce48e5aSMichal Kubecek #include <linux/errno.h>
169ce48e5aSMichal Kubecek #include <linux/ethtool.h>
179ce48e5aSMichal Kubecek #include <linux/netdevice.h>
189ce48e5aSMichal Kubecek #include <linux/net_tstamp.h>
199ce48e5aSMichal Kubecek #include <linux/phy.h>
209ce48e5aSMichal Kubecek #include <linux/bitops.h>
219ce48e5aSMichal Kubecek #include <linux/uaccess.h>
229ce48e5aSMichal Kubecek #include <linux/vmalloc.h>
239ce48e5aSMichal Kubecek #include <linux/sfp.h>
249ce48e5aSMichal Kubecek #include <linux/slab.h>
259ce48e5aSMichal Kubecek #include <linux/rtnetlink.h>
269ce48e5aSMichal Kubecek #include <linux/sched/signal.h>
279ce48e5aSMichal Kubecek #include <linux/net.h>
28f32a2137SHeiner Kallweit #include <linux/pm_runtime.h>
299ce48e5aSMichal Kubecek #include <net/devlink.h>
308cdc3223SKuniyuki Iwashima #include <net/ipv6.h>
31a71506a4SMagnus Karlsson #include <net/xdp_sock_drv.h>
329ce48e5aSMichal Kubecek #include <net/flow_offload.h>
3373286734SMichal Kubecek #include <linux/ethtool_netlink.h>
341c79031fSLeon Romanovsky #include <generated/utsrelease.h>
35d44e1310SMichal Kubecek #include "common.h"
36d44e1310SMichal Kubecek 
37095cfcfeSJakub Kicinski /* State held across locks and calls for commands which have devlink fallback */
38095cfcfeSJakub Kicinski struct ethtool_devlink_compat {
391af0a094SJakub Kicinski 	struct devlink *devlink;
40095cfcfeSJakub Kicinski 	union {
41095cfcfeSJakub Kicinski 		struct ethtool_flash efl;
42095cfcfeSJakub Kicinski 		struct ethtool_drvinfo info;
43095cfcfeSJakub Kicinski 	};
44095cfcfeSJakub Kicinski };
45095cfcfeSJakub Kicinski 
461af0a094SJakub Kicinski static struct devlink *netdev_to_devlink_get(struct net_device *dev)
471af0a094SJakub Kicinski {
488eba37f7SJiri Pirko 	if (!dev->devlink_port)
491af0a094SJakub Kicinski 		return NULL;
508eba37f7SJiri Pirko 	return devlink_try_get(dev->devlink_port->devlink);
511af0a094SJakub Kicinski }
521af0a094SJakub Kicinski 
539ce48e5aSMichal Kubecek /*
549ce48e5aSMichal Kubecek  * Some useful ethtool_ops methods that're device independent.
559ce48e5aSMichal Kubecek  * If we find that all drivers want to do the same thing here,
569ce48e5aSMichal Kubecek  * we can turn these into dev_() function calls.
579ce48e5aSMichal Kubecek  */
589ce48e5aSMichal Kubecek 
599ce48e5aSMichal Kubecek u32 ethtool_op_get_link(struct net_device *dev)
609ce48e5aSMichal Kubecek {
619ce48e5aSMichal Kubecek 	return netif_carrier_ok(dev) ? 1 : 0;
629ce48e5aSMichal Kubecek }
639ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_op_get_link);
649ce48e5aSMichal Kubecek 
659ce48e5aSMichal Kubecek int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
669ce48e5aSMichal Kubecek {
679ce48e5aSMichal Kubecek 	info->so_timestamping =
689ce48e5aSMichal Kubecek 		SOF_TIMESTAMPING_TX_SOFTWARE |
699ce48e5aSMichal Kubecek 		SOF_TIMESTAMPING_RX_SOFTWARE |
709ce48e5aSMichal Kubecek 		SOF_TIMESTAMPING_SOFTWARE;
719ce48e5aSMichal Kubecek 	info->phc_index = -1;
729ce48e5aSMichal Kubecek 	return 0;
739ce48e5aSMichal Kubecek }
749ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_op_get_ts_info);
759ce48e5aSMichal Kubecek 
769ce48e5aSMichal Kubecek /* Handlers for each ethtool command */
779ce48e5aSMichal Kubecek 
789ce48e5aSMichal Kubecek static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
799ce48e5aSMichal Kubecek {
809ce48e5aSMichal Kubecek 	struct ethtool_gfeatures cmd = {
819ce48e5aSMichal Kubecek 		.cmd = ETHTOOL_GFEATURES,
829ce48e5aSMichal Kubecek 		.size = ETHTOOL_DEV_FEATURE_WORDS,
839ce48e5aSMichal Kubecek 	};
849ce48e5aSMichal Kubecek 	struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
859ce48e5aSMichal Kubecek 	u32 __user *sizeaddr;
869ce48e5aSMichal Kubecek 	u32 copy_size;
879ce48e5aSMichal Kubecek 	int i;
889ce48e5aSMichal Kubecek 
899ce48e5aSMichal Kubecek 	/* in case feature bits run out again */
909ce48e5aSMichal Kubecek 	BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
919ce48e5aSMichal Kubecek 
929ce48e5aSMichal Kubecek 	for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
939ce48e5aSMichal Kubecek 		features[i].available = (u32)(dev->hw_features >> (32 * i));
949ce48e5aSMichal Kubecek 		features[i].requested = (u32)(dev->wanted_features >> (32 * i));
959ce48e5aSMichal Kubecek 		features[i].active = (u32)(dev->features >> (32 * i));
969ce48e5aSMichal Kubecek 		features[i].never_changed =
979ce48e5aSMichal Kubecek 			(u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
989ce48e5aSMichal Kubecek 	}
999ce48e5aSMichal Kubecek 
1009ce48e5aSMichal Kubecek 	sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
1019ce48e5aSMichal Kubecek 	if (get_user(copy_size, sizeaddr))
1029ce48e5aSMichal Kubecek 		return -EFAULT;
1039ce48e5aSMichal Kubecek 
1049ce48e5aSMichal Kubecek 	if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
1059ce48e5aSMichal Kubecek 		copy_size = ETHTOOL_DEV_FEATURE_WORDS;
1069ce48e5aSMichal Kubecek 
1079ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
1089ce48e5aSMichal Kubecek 		return -EFAULT;
1099ce48e5aSMichal Kubecek 	useraddr += sizeof(cmd);
110ed717613SGustavo A. R. Silva 	if (copy_to_user(useraddr, features,
111ed717613SGustavo A. R. Silva 			 array_size(copy_size, sizeof(*features))))
1129ce48e5aSMichal Kubecek 		return -EFAULT;
1139ce48e5aSMichal Kubecek 
1149ce48e5aSMichal Kubecek 	return 0;
1159ce48e5aSMichal Kubecek }
1169ce48e5aSMichal Kubecek 
1179ce48e5aSMichal Kubecek static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
1189ce48e5aSMichal Kubecek {
1199ce48e5aSMichal Kubecek 	struct ethtool_sfeatures cmd;
1209ce48e5aSMichal Kubecek 	struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
1219ce48e5aSMichal Kubecek 	netdev_features_t wanted = 0, valid = 0;
1229ce48e5aSMichal Kubecek 	int i, ret = 0;
1239ce48e5aSMichal Kubecek 
1249ce48e5aSMichal Kubecek 	if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
1259ce48e5aSMichal Kubecek 		return -EFAULT;
1269ce48e5aSMichal Kubecek 	useraddr += sizeof(cmd);
1279ce48e5aSMichal Kubecek 
1289ce48e5aSMichal Kubecek 	if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
1299ce48e5aSMichal Kubecek 		return -EINVAL;
1309ce48e5aSMichal Kubecek 
1319ce48e5aSMichal Kubecek 	if (copy_from_user(features, useraddr, sizeof(features)))
1329ce48e5aSMichal Kubecek 		return -EFAULT;
1339ce48e5aSMichal Kubecek 
1349ce48e5aSMichal Kubecek 	for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
1359ce48e5aSMichal Kubecek 		valid |= (netdev_features_t)features[i].valid << (32 * i);
1369ce48e5aSMichal Kubecek 		wanted |= (netdev_features_t)features[i].requested << (32 * i);
1379ce48e5aSMichal Kubecek 	}
1389ce48e5aSMichal Kubecek 
1399ce48e5aSMichal Kubecek 	if (valid & ~NETIF_F_ETHTOOL_BITS)
1409ce48e5aSMichal Kubecek 		return -EINVAL;
1419ce48e5aSMichal Kubecek 
1429ce48e5aSMichal Kubecek 	if (valid & ~dev->hw_features) {
1439ce48e5aSMichal Kubecek 		valid &= dev->hw_features;
1449ce48e5aSMichal Kubecek 		ret |= ETHTOOL_F_UNSUPPORTED;
1459ce48e5aSMichal Kubecek 	}
1469ce48e5aSMichal Kubecek 
1479ce48e5aSMichal Kubecek 	dev->wanted_features &= ~valid;
1489ce48e5aSMichal Kubecek 	dev->wanted_features |= wanted & valid;
1499ce48e5aSMichal Kubecek 	__netdev_update_features(dev);
1509ce48e5aSMichal Kubecek 
1519ce48e5aSMichal Kubecek 	if ((dev->wanted_features ^ dev->features) & valid)
1529ce48e5aSMichal Kubecek 		ret |= ETHTOOL_F_WISH;
1539ce48e5aSMichal Kubecek 
1549ce48e5aSMichal Kubecek 	return ret;
1559ce48e5aSMichal Kubecek }
1569ce48e5aSMichal Kubecek 
1579ce48e5aSMichal Kubecek static int __ethtool_get_sset_count(struct net_device *dev, int sset)
1589ce48e5aSMichal Kubecek {
15917809516SFlorian Fainelli 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
1609ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
1619ce48e5aSMichal Kubecek 
1629ce48e5aSMichal Kubecek 	if (sset == ETH_SS_FEATURES)
1639ce48e5aSMichal Kubecek 		return ARRAY_SIZE(netdev_features_strings);
1649ce48e5aSMichal Kubecek 
1659ce48e5aSMichal Kubecek 	if (sset == ETH_SS_RSS_HASH_FUNCS)
1669ce48e5aSMichal Kubecek 		return ARRAY_SIZE(rss_hash_func_strings);
1679ce48e5aSMichal Kubecek 
1689ce48e5aSMichal Kubecek 	if (sset == ETH_SS_TUNABLES)
1699ce48e5aSMichal Kubecek 		return ARRAY_SIZE(tunable_strings);
1709ce48e5aSMichal Kubecek 
1719ce48e5aSMichal Kubecek 	if (sset == ETH_SS_PHY_TUNABLES)
1729ce48e5aSMichal Kubecek 		return ARRAY_SIZE(phy_tunable_strings);
1739ce48e5aSMichal Kubecek 
1749ce48e5aSMichal Kubecek 	if (sset == ETH_SS_PHY_STATS && dev->phydev &&
17517809516SFlorian Fainelli 	    !ops->get_ethtool_phy_stats &&
17617809516SFlorian Fainelli 	    phy_ops && phy_ops->get_sset_count)
17717809516SFlorian Fainelli 		return phy_ops->get_sset_count(dev->phydev);
1789ce48e5aSMichal Kubecek 
179428c122fSMichal Kubecek 	if (sset == ETH_SS_LINK_MODES)
180428c122fSMichal Kubecek 		return __ETHTOOL_LINK_MODE_MASK_NBITS;
181428c122fSMichal Kubecek 
1829ce48e5aSMichal Kubecek 	if (ops->get_sset_count && ops->get_strings)
1839ce48e5aSMichal Kubecek 		return ops->get_sset_count(dev, sset);
1849ce48e5aSMichal Kubecek 	else
1859ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
1869ce48e5aSMichal Kubecek }
1879ce48e5aSMichal Kubecek 
1889ce48e5aSMichal Kubecek static void __ethtool_get_strings(struct net_device *dev,
1899ce48e5aSMichal Kubecek 	u32 stringset, u8 *data)
1909ce48e5aSMichal Kubecek {
19117809516SFlorian Fainelli 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
1929ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
1939ce48e5aSMichal Kubecek 
1949ce48e5aSMichal Kubecek 	if (stringset == ETH_SS_FEATURES)
1959ce48e5aSMichal Kubecek 		memcpy(data, netdev_features_strings,
1969ce48e5aSMichal Kubecek 			sizeof(netdev_features_strings));
1979ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_RSS_HASH_FUNCS)
1989ce48e5aSMichal Kubecek 		memcpy(data, rss_hash_func_strings,
1999ce48e5aSMichal Kubecek 		       sizeof(rss_hash_func_strings));
2009ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_TUNABLES)
2019ce48e5aSMichal Kubecek 		memcpy(data, tunable_strings, sizeof(tunable_strings));
2029ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_PHY_TUNABLES)
2039ce48e5aSMichal Kubecek 		memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
2049ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
20517809516SFlorian Fainelli 		 !ops->get_ethtool_phy_stats && phy_ops &&
20617809516SFlorian Fainelli 		 phy_ops->get_strings)
20717809516SFlorian Fainelli 		phy_ops->get_strings(dev->phydev, data);
208428c122fSMichal Kubecek 	else if (stringset == ETH_SS_LINK_MODES)
209428c122fSMichal Kubecek 		memcpy(data, link_mode_names,
210428c122fSMichal Kubecek 		       __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
2119ce48e5aSMichal Kubecek 	else
2129ce48e5aSMichal Kubecek 		/* ops->get_strings is valid because checked earlier */
2139ce48e5aSMichal Kubecek 		ops->get_strings(dev, stringset, data);
2149ce48e5aSMichal Kubecek }
2159ce48e5aSMichal Kubecek 
2169ce48e5aSMichal Kubecek static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
2179ce48e5aSMichal Kubecek {
2189ce48e5aSMichal Kubecek 	/* feature masks of legacy discrete ethtool ops */
2199ce48e5aSMichal Kubecek 
2209ce48e5aSMichal Kubecek 	switch (eth_cmd) {
2219ce48e5aSMichal Kubecek 	case ETHTOOL_GTXCSUM:
2229ce48e5aSMichal Kubecek 	case ETHTOOL_STXCSUM:
2239d648fb5SVladyslav Tarasiuk 		return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
224f70bb065SMichal Kubecek 		       NETIF_F_SCTP_CRC;
2259ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCSUM:
2269ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCSUM:
2279ce48e5aSMichal Kubecek 		return NETIF_F_RXCSUM;
2289ce48e5aSMichal Kubecek 	case ETHTOOL_GSG:
2299ce48e5aSMichal Kubecek 	case ETHTOOL_SSG:
230f70bb065SMichal Kubecek 		return NETIF_F_SG | NETIF_F_FRAGLIST;
2319ce48e5aSMichal Kubecek 	case ETHTOOL_GTSO:
2329ce48e5aSMichal Kubecek 	case ETHTOOL_STSO:
2339ce48e5aSMichal Kubecek 		return NETIF_F_ALL_TSO;
2349ce48e5aSMichal Kubecek 	case ETHTOOL_GGSO:
2359ce48e5aSMichal Kubecek 	case ETHTOOL_SGSO:
2369ce48e5aSMichal Kubecek 		return NETIF_F_GSO;
2379ce48e5aSMichal Kubecek 	case ETHTOOL_GGRO:
2389ce48e5aSMichal Kubecek 	case ETHTOOL_SGRO:
2399ce48e5aSMichal Kubecek 		return NETIF_F_GRO;
2409ce48e5aSMichal Kubecek 	default:
2419ce48e5aSMichal Kubecek 		BUG();
2429ce48e5aSMichal Kubecek 	}
2439ce48e5aSMichal Kubecek }
2449ce48e5aSMichal Kubecek 
2459ce48e5aSMichal Kubecek static int ethtool_get_one_feature(struct net_device *dev,
2469ce48e5aSMichal Kubecek 	char __user *useraddr, u32 ethcmd)
2479ce48e5aSMichal Kubecek {
2489ce48e5aSMichal Kubecek 	netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
2499ce48e5aSMichal Kubecek 	struct ethtool_value edata = {
2509ce48e5aSMichal Kubecek 		.cmd = ethcmd,
2519ce48e5aSMichal Kubecek 		.data = !!(dev->features & mask),
2529ce48e5aSMichal Kubecek 	};
2539ce48e5aSMichal Kubecek 
2549ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
2559ce48e5aSMichal Kubecek 		return -EFAULT;
2569ce48e5aSMichal Kubecek 	return 0;
2579ce48e5aSMichal Kubecek }
2589ce48e5aSMichal Kubecek 
2599ce48e5aSMichal Kubecek static int ethtool_set_one_feature(struct net_device *dev,
2609ce48e5aSMichal Kubecek 	void __user *useraddr, u32 ethcmd)
2619ce48e5aSMichal Kubecek {
2629ce48e5aSMichal Kubecek 	struct ethtool_value edata;
2639ce48e5aSMichal Kubecek 	netdev_features_t mask;
2649ce48e5aSMichal Kubecek 
2659ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
2669ce48e5aSMichal Kubecek 		return -EFAULT;
2679ce48e5aSMichal Kubecek 
2689ce48e5aSMichal Kubecek 	mask = ethtool_get_feature_mask(ethcmd);
2699ce48e5aSMichal Kubecek 	mask &= dev->hw_features;
2709ce48e5aSMichal Kubecek 	if (!mask)
2719ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
2729ce48e5aSMichal Kubecek 
2739ce48e5aSMichal Kubecek 	if (edata.data)
2749ce48e5aSMichal Kubecek 		dev->wanted_features |= mask;
2759ce48e5aSMichal Kubecek 	else
2769ce48e5aSMichal Kubecek 		dev->wanted_features &= ~mask;
2779ce48e5aSMichal Kubecek 
2789ce48e5aSMichal Kubecek 	__netdev_update_features(dev);
2799ce48e5aSMichal Kubecek 
2809ce48e5aSMichal Kubecek 	return 0;
2819ce48e5aSMichal Kubecek }
2829ce48e5aSMichal Kubecek 
2839ce48e5aSMichal Kubecek #define ETH_ALL_FLAGS    (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
2849ce48e5aSMichal Kubecek 			  ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
2859ce48e5aSMichal Kubecek #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
2869ce48e5aSMichal Kubecek 			  NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
2879ce48e5aSMichal Kubecek 			  NETIF_F_RXHASH)
2889ce48e5aSMichal Kubecek 
2899ce48e5aSMichal Kubecek static u32 __ethtool_get_flags(struct net_device *dev)
2909ce48e5aSMichal Kubecek {
2919ce48e5aSMichal Kubecek 	u32 flags = 0;
2929ce48e5aSMichal Kubecek 
2939ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_LRO)
2949ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_LRO;
2959ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
2969ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_RXVLAN;
2979ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
2989ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_TXVLAN;
2999ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_NTUPLE)
3009ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_NTUPLE;
3019ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_RXHASH)
3029ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_RXHASH;
3039ce48e5aSMichal Kubecek 
3049ce48e5aSMichal Kubecek 	return flags;
3059ce48e5aSMichal Kubecek }
3069ce48e5aSMichal Kubecek 
3079ce48e5aSMichal Kubecek static int __ethtool_set_flags(struct net_device *dev, u32 data)
3089ce48e5aSMichal Kubecek {
3099ce48e5aSMichal Kubecek 	netdev_features_t features = 0, changed;
3109ce48e5aSMichal Kubecek 
3119ce48e5aSMichal Kubecek 	if (data & ~ETH_ALL_FLAGS)
3129ce48e5aSMichal Kubecek 		return -EINVAL;
3139ce48e5aSMichal Kubecek 
3149ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_LRO)
3159ce48e5aSMichal Kubecek 		features |= NETIF_F_LRO;
3169ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_RXVLAN)
3179ce48e5aSMichal Kubecek 		features |= NETIF_F_HW_VLAN_CTAG_RX;
3189ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_TXVLAN)
3199ce48e5aSMichal Kubecek 		features |= NETIF_F_HW_VLAN_CTAG_TX;
3209ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_NTUPLE)
3219ce48e5aSMichal Kubecek 		features |= NETIF_F_NTUPLE;
3229ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_RXHASH)
3239ce48e5aSMichal Kubecek 		features |= NETIF_F_RXHASH;
3249ce48e5aSMichal Kubecek 
3259ce48e5aSMichal Kubecek 	/* allow changing only bits set in hw_features */
3269ce48e5aSMichal Kubecek 	changed = (features ^ dev->features) & ETH_ALL_FEATURES;
3279ce48e5aSMichal Kubecek 	if (changed & ~dev->hw_features)
3289ce48e5aSMichal Kubecek 		return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
3299ce48e5aSMichal Kubecek 
3309ce48e5aSMichal Kubecek 	dev->wanted_features =
3319ce48e5aSMichal Kubecek 		(dev->wanted_features & ~changed) | (features & changed);
3329ce48e5aSMichal Kubecek 
3339ce48e5aSMichal Kubecek 	__netdev_update_features(dev);
3349ce48e5aSMichal Kubecek 
3359ce48e5aSMichal Kubecek 	return 0;
3369ce48e5aSMichal Kubecek }
3379ce48e5aSMichal Kubecek 
3389ce48e5aSMichal Kubecek /* Given two link masks, AND them together and save the result in dst. */
3399ce48e5aSMichal Kubecek void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
3409ce48e5aSMichal Kubecek 				  struct ethtool_link_ksettings *src)
3419ce48e5aSMichal Kubecek {
3429ce48e5aSMichal Kubecek 	unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
3439ce48e5aSMichal Kubecek 	unsigned int idx = 0;
3449ce48e5aSMichal Kubecek 
3459ce48e5aSMichal Kubecek 	for (; idx < size; idx++) {
3469ce48e5aSMichal Kubecek 		dst->link_modes.supported[idx] &=
3479ce48e5aSMichal Kubecek 			src->link_modes.supported[idx];
3489ce48e5aSMichal Kubecek 		dst->link_modes.advertising[idx] &=
3499ce48e5aSMichal Kubecek 			src->link_modes.advertising[idx];
3509ce48e5aSMichal Kubecek 	}
3519ce48e5aSMichal Kubecek }
3529ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_intersect_link_masks);
3539ce48e5aSMichal Kubecek 
3549ce48e5aSMichal Kubecek void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
3559ce48e5aSMichal Kubecek 					     u32 legacy_u32)
3569ce48e5aSMichal Kubecek {
3574973056cSSean Anderson 	linkmode_zero(dst);
3589ce48e5aSMichal Kubecek 	dst[0] = legacy_u32;
3599ce48e5aSMichal Kubecek }
3609ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
3619ce48e5aSMichal Kubecek 
3629ce48e5aSMichal Kubecek /* return false if src had higher bits set. lower bits always updated. */
3639ce48e5aSMichal Kubecek bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
3649ce48e5aSMichal Kubecek 					     const unsigned long *src)
3659ce48e5aSMichal Kubecek {
3669ce48e5aSMichal Kubecek 	*legacy_u32 = src[0];
36719d62f5eSMarco Bonelli 	return find_next_bit(src, __ETHTOOL_LINK_MODE_MASK_NBITS, 32) ==
36819d62f5eSMarco Bonelli 		__ETHTOOL_LINK_MODE_MASK_NBITS;
3699ce48e5aSMichal Kubecek }
3709ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
3719ce48e5aSMichal Kubecek 
3729ce48e5aSMichal Kubecek /* return false if ksettings link modes had higher bits
3739ce48e5aSMichal Kubecek  * set. legacy_settings always updated (best effort)
3749ce48e5aSMichal Kubecek  */
3759ce48e5aSMichal Kubecek static bool
3769ce48e5aSMichal Kubecek convert_link_ksettings_to_legacy_settings(
3779ce48e5aSMichal Kubecek 	struct ethtool_cmd *legacy_settings,
3789ce48e5aSMichal Kubecek 	const struct ethtool_link_ksettings *link_ksettings)
3799ce48e5aSMichal Kubecek {
3809ce48e5aSMichal Kubecek 	bool retval = true;
3819ce48e5aSMichal Kubecek 
3829ce48e5aSMichal Kubecek 	memset(legacy_settings, 0, sizeof(*legacy_settings));
3839ce48e5aSMichal Kubecek 	/* this also clears the deprecated fields in legacy structure:
3849ce48e5aSMichal Kubecek 	 * __u8		transceiver;
3859ce48e5aSMichal Kubecek 	 * __u32	maxtxpkt;
3869ce48e5aSMichal Kubecek 	 * __u32	maxrxpkt;
3879ce48e5aSMichal Kubecek 	 */
3889ce48e5aSMichal Kubecek 
3899ce48e5aSMichal Kubecek 	retval &= ethtool_convert_link_mode_to_legacy_u32(
3909ce48e5aSMichal Kubecek 		&legacy_settings->supported,
3919ce48e5aSMichal Kubecek 		link_ksettings->link_modes.supported);
3929ce48e5aSMichal Kubecek 	retval &= ethtool_convert_link_mode_to_legacy_u32(
3939ce48e5aSMichal Kubecek 		&legacy_settings->advertising,
3949ce48e5aSMichal Kubecek 		link_ksettings->link_modes.advertising);
3959ce48e5aSMichal Kubecek 	retval &= ethtool_convert_link_mode_to_legacy_u32(
3969ce48e5aSMichal Kubecek 		&legacy_settings->lp_advertising,
3979ce48e5aSMichal Kubecek 		link_ksettings->link_modes.lp_advertising);
3989ce48e5aSMichal Kubecek 	ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
3999ce48e5aSMichal Kubecek 	legacy_settings->duplex
4009ce48e5aSMichal Kubecek 		= link_ksettings->base.duplex;
4019ce48e5aSMichal Kubecek 	legacy_settings->port
4029ce48e5aSMichal Kubecek 		= link_ksettings->base.port;
4039ce48e5aSMichal Kubecek 	legacy_settings->phy_address
4049ce48e5aSMichal Kubecek 		= link_ksettings->base.phy_address;
4059ce48e5aSMichal Kubecek 	legacy_settings->autoneg
4069ce48e5aSMichal Kubecek 		= link_ksettings->base.autoneg;
4079ce48e5aSMichal Kubecek 	legacy_settings->mdio_support
4089ce48e5aSMichal Kubecek 		= link_ksettings->base.mdio_support;
4099ce48e5aSMichal Kubecek 	legacy_settings->eth_tp_mdix
4109ce48e5aSMichal Kubecek 		= link_ksettings->base.eth_tp_mdix;
4119ce48e5aSMichal Kubecek 	legacy_settings->eth_tp_mdix_ctrl
4129ce48e5aSMichal Kubecek 		= link_ksettings->base.eth_tp_mdix_ctrl;
4139ce48e5aSMichal Kubecek 	legacy_settings->transceiver
4149ce48e5aSMichal Kubecek 		= link_ksettings->base.transceiver;
4159ce48e5aSMichal Kubecek 	return retval;
4169ce48e5aSMichal Kubecek }
4179ce48e5aSMichal Kubecek 
4189ce48e5aSMichal Kubecek /* number of 32-bit words to store the user's link mode bitmaps */
4199ce48e5aSMichal Kubecek #define __ETHTOOL_LINK_MODE_MASK_NU32			\
4209ce48e5aSMichal Kubecek 	DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
4219ce48e5aSMichal Kubecek 
4229ce48e5aSMichal Kubecek /* layout of the struct passed from/to userland */
4239ce48e5aSMichal Kubecek struct ethtool_link_usettings {
4249ce48e5aSMichal Kubecek 	struct ethtool_link_settings base;
4259ce48e5aSMichal Kubecek 	struct {
4269ce48e5aSMichal Kubecek 		__u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
4279ce48e5aSMichal Kubecek 		__u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
4289ce48e5aSMichal Kubecek 		__u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
4299ce48e5aSMichal Kubecek 	} link_modes;
4309ce48e5aSMichal Kubecek };
4319ce48e5aSMichal Kubecek 
4329ce48e5aSMichal Kubecek /* Internal kernel helper to query a device ethtool_link_settings. */
4339ce48e5aSMichal Kubecek int __ethtool_get_link_ksettings(struct net_device *dev,
4349ce48e5aSMichal Kubecek 				 struct ethtool_link_ksettings *link_ksettings)
4359ce48e5aSMichal Kubecek {
4369ce48e5aSMichal Kubecek 	ASSERT_RTNL();
4379ce48e5aSMichal Kubecek 
4389ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_link_ksettings)
4399ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
4409ce48e5aSMichal Kubecek 
4419ce48e5aSMichal Kubecek 	memset(link_ksettings, 0, sizeof(*link_ksettings));
442a975d7d8SDanielle Ratson 	return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
4439ce48e5aSMichal Kubecek }
4449ce48e5aSMichal Kubecek EXPORT_SYMBOL(__ethtool_get_link_ksettings);
4459ce48e5aSMichal Kubecek 
4469ce48e5aSMichal Kubecek /* convert ethtool_link_usettings in user space to a kernel internal
4479ce48e5aSMichal Kubecek  * ethtool_link_ksettings. return 0 on success, errno on error.
4489ce48e5aSMichal Kubecek  */
4499ce48e5aSMichal Kubecek static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
4509ce48e5aSMichal Kubecek 					 const void __user *from)
4519ce48e5aSMichal Kubecek {
4529ce48e5aSMichal Kubecek 	struct ethtool_link_usettings link_usettings;
4539ce48e5aSMichal Kubecek 
4549ce48e5aSMichal Kubecek 	if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
4559ce48e5aSMichal Kubecek 		return -EFAULT;
4569ce48e5aSMichal Kubecek 
4579ce48e5aSMichal Kubecek 	memcpy(&to->base, &link_usettings.base, sizeof(to->base));
4589ce48e5aSMichal Kubecek 	bitmap_from_arr32(to->link_modes.supported,
4599ce48e5aSMichal Kubecek 			  link_usettings.link_modes.supported,
4609ce48e5aSMichal Kubecek 			  __ETHTOOL_LINK_MODE_MASK_NBITS);
4619ce48e5aSMichal Kubecek 	bitmap_from_arr32(to->link_modes.advertising,
4629ce48e5aSMichal Kubecek 			  link_usettings.link_modes.advertising,
4639ce48e5aSMichal Kubecek 			  __ETHTOOL_LINK_MODE_MASK_NBITS);
4649ce48e5aSMichal Kubecek 	bitmap_from_arr32(to->link_modes.lp_advertising,
4659ce48e5aSMichal Kubecek 			  link_usettings.link_modes.lp_advertising,
4669ce48e5aSMichal Kubecek 			  __ETHTOOL_LINK_MODE_MASK_NBITS);
4679ce48e5aSMichal Kubecek 
4689ce48e5aSMichal Kubecek 	return 0;
4699ce48e5aSMichal Kubecek }
4709ce48e5aSMichal Kubecek 
47170ae1e12SCris Forno /* Check if the user is trying to change anything besides speed/duplex */
47270ae1e12SCris Forno bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
47370ae1e12SCris Forno {
47470ae1e12SCris Forno 	struct ethtool_link_settings base2 = {};
47570ae1e12SCris Forno 
47670ae1e12SCris Forno 	base2.speed = cmd->base.speed;
47770ae1e12SCris Forno 	base2.port = PORT_OTHER;
47870ae1e12SCris Forno 	base2.duplex = cmd->base.duplex;
47970ae1e12SCris Forno 	base2.cmd = cmd->base.cmd;
48070ae1e12SCris Forno 	base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
48170ae1e12SCris Forno 
48270ae1e12SCris Forno 	return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
48370ae1e12SCris Forno 		bitmap_empty(cmd->link_modes.supported,
48470ae1e12SCris Forno 			     __ETHTOOL_LINK_MODE_MASK_NBITS) &&
48570ae1e12SCris Forno 		bitmap_empty(cmd->link_modes.lp_advertising,
48670ae1e12SCris Forno 			     __ETHTOOL_LINK_MODE_MASK_NBITS);
48770ae1e12SCris Forno }
48870ae1e12SCris Forno 
4899ce48e5aSMichal Kubecek /* convert a kernel internal ethtool_link_ksettings to
4909ce48e5aSMichal Kubecek  * ethtool_link_usettings in user space. return 0 on success, errno on
4919ce48e5aSMichal Kubecek  * error.
4929ce48e5aSMichal Kubecek  */
4939ce48e5aSMichal Kubecek static int
4949ce48e5aSMichal Kubecek store_link_ksettings_for_user(void __user *to,
4959ce48e5aSMichal Kubecek 			      const struct ethtool_link_ksettings *from)
4969ce48e5aSMichal Kubecek {
4979ce48e5aSMichal Kubecek 	struct ethtool_link_usettings link_usettings;
4989ce48e5aSMichal Kubecek 
499c1d9e34eSGustavo A. R. Silva 	memcpy(&link_usettings, from, sizeof(link_usettings));
5009ce48e5aSMichal Kubecek 	bitmap_to_arr32(link_usettings.link_modes.supported,
5019ce48e5aSMichal Kubecek 			from->link_modes.supported,
5029ce48e5aSMichal Kubecek 			__ETHTOOL_LINK_MODE_MASK_NBITS);
5039ce48e5aSMichal Kubecek 	bitmap_to_arr32(link_usettings.link_modes.advertising,
5049ce48e5aSMichal Kubecek 			from->link_modes.advertising,
5059ce48e5aSMichal Kubecek 			__ETHTOOL_LINK_MODE_MASK_NBITS);
5069ce48e5aSMichal Kubecek 	bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
5079ce48e5aSMichal Kubecek 			from->link_modes.lp_advertising,
5089ce48e5aSMichal Kubecek 			__ETHTOOL_LINK_MODE_MASK_NBITS);
5099ce48e5aSMichal Kubecek 
5109ce48e5aSMichal Kubecek 	if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
5119ce48e5aSMichal Kubecek 		return -EFAULT;
5129ce48e5aSMichal Kubecek 
5139ce48e5aSMichal Kubecek 	return 0;
5149ce48e5aSMichal Kubecek }
5159ce48e5aSMichal Kubecek 
5169ce48e5aSMichal Kubecek /* Query device for its ethtool_link_settings. */
5179ce48e5aSMichal Kubecek static int ethtool_get_link_ksettings(struct net_device *dev,
5189ce48e5aSMichal Kubecek 				      void __user *useraddr)
5199ce48e5aSMichal Kubecek {
5209ce48e5aSMichal Kubecek 	int err = 0;
5219ce48e5aSMichal Kubecek 	struct ethtool_link_ksettings link_ksettings;
5229ce48e5aSMichal Kubecek 
5239ce48e5aSMichal Kubecek 	ASSERT_RTNL();
5249ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_link_ksettings)
5259ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
5269ce48e5aSMichal Kubecek 
5279ce48e5aSMichal Kubecek 	/* handle bitmap nbits handshake */
5289ce48e5aSMichal Kubecek 	if (copy_from_user(&link_ksettings.base, useraddr,
5299ce48e5aSMichal Kubecek 			   sizeof(link_ksettings.base)))
5309ce48e5aSMichal Kubecek 		return -EFAULT;
5319ce48e5aSMichal Kubecek 
5329ce48e5aSMichal Kubecek 	if (__ETHTOOL_LINK_MODE_MASK_NU32
5339ce48e5aSMichal Kubecek 	    != link_ksettings.base.link_mode_masks_nwords) {
5349ce48e5aSMichal Kubecek 		/* wrong link mode nbits requested */
5359ce48e5aSMichal Kubecek 		memset(&link_ksettings, 0, sizeof(link_ksettings));
5369ce48e5aSMichal Kubecek 		link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
5379ce48e5aSMichal Kubecek 		/* send back number of words required as negative val */
5389ce48e5aSMichal Kubecek 		compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
5399ce48e5aSMichal Kubecek 				   "need too many bits for link modes!");
5409ce48e5aSMichal Kubecek 		link_ksettings.base.link_mode_masks_nwords
5419ce48e5aSMichal Kubecek 			= -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
5429ce48e5aSMichal Kubecek 
5439ce48e5aSMichal Kubecek 		/* copy the base fields back to user, not the link
5449ce48e5aSMichal Kubecek 		 * mode bitmaps
5459ce48e5aSMichal Kubecek 		 */
5469ce48e5aSMichal Kubecek 		if (copy_to_user(useraddr, &link_ksettings.base,
5479ce48e5aSMichal Kubecek 				 sizeof(link_ksettings.base)))
5489ce48e5aSMichal Kubecek 			return -EFAULT;
5499ce48e5aSMichal Kubecek 
5509ce48e5aSMichal Kubecek 		return 0;
5519ce48e5aSMichal Kubecek 	}
5529ce48e5aSMichal Kubecek 
5539ce48e5aSMichal Kubecek 	/* handshake successful: user/kernel agree on
5549ce48e5aSMichal Kubecek 	 * link_mode_masks_nwords
5559ce48e5aSMichal Kubecek 	 */
5569ce48e5aSMichal Kubecek 
5579ce48e5aSMichal Kubecek 	memset(&link_ksettings, 0, sizeof(link_ksettings));
5589ce48e5aSMichal Kubecek 	err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
5599ce48e5aSMichal Kubecek 	if (err < 0)
5609ce48e5aSMichal Kubecek 		return err;
5619ce48e5aSMichal Kubecek 
5629ce48e5aSMichal Kubecek 	/* make sure we tell the right values to user */
5639ce48e5aSMichal Kubecek 	link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
5649ce48e5aSMichal Kubecek 	link_ksettings.base.link_mode_masks_nwords
5659ce48e5aSMichal Kubecek 		= __ETHTOOL_LINK_MODE_MASK_NU32;
566bdbdac76SOleksij Rempel 	link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
567bdbdac76SOleksij Rempel 	link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
5680c3e10cbSSean Anderson 	link_ksettings.base.rate_matching = RATE_MATCH_NONE;
5699ce48e5aSMichal Kubecek 
5709ce48e5aSMichal Kubecek 	return store_link_ksettings_for_user(useraddr, &link_ksettings);
5719ce48e5aSMichal Kubecek }
5729ce48e5aSMichal Kubecek 
5739ce48e5aSMichal Kubecek /* Update device ethtool_link_settings. */
5749ce48e5aSMichal Kubecek static int ethtool_set_link_ksettings(struct net_device *dev,
5759ce48e5aSMichal Kubecek 				      void __user *useraddr)
5769ce48e5aSMichal Kubecek {
5779ad685dbSIdo Schimmel 	struct ethtool_link_ksettings link_ksettings = {};
5789ce48e5aSMichal Kubecek 	int err;
5799ce48e5aSMichal Kubecek 
5809ce48e5aSMichal Kubecek 	ASSERT_RTNL();
5819ce48e5aSMichal Kubecek 
5829ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_link_ksettings)
5839ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
5849ce48e5aSMichal Kubecek 
5859ce48e5aSMichal Kubecek 	/* make sure nbits field has expected value */
5869ce48e5aSMichal Kubecek 	if (copy_from_user(&link_ksettings.base, useraddr,
5879ce48e5aSMichal Kubecek 			   sizeof(link_ksettings.base)))
5889ce48e5aSMichal Kubecek 		return -EFAULT;
5899ce48e5aSMichal Kubecek 
5909ce48e5aSMichal Kubecek 	if (__ETHTOOL_LINK_MODE_MASK_NU32
5919ce48e5aSMichal Kubecek 	    != link_ksettings.base.link_mode_masks_nwords)
5929ce48e5aSMichal Kubecek 		return -EINVAL;
5939ce48e5aSMichal Kubecek 
5949ce48e5aSMichal Kubecek 	/* copy the whole structure, now that we know it has expected
5959ce48e5aSMichal Kubecek 	 * format
5969ce48e5aSMichal Kubecek 	 */
5979ce48e5aSMichal Kubecek 	err = load_link_ksettings_from_user(&link_ksettings, useraddr);
5989ce48e5aSMichal Kubecek 	if (err)
5999ce48e5aSMichal Kubecek 		return err;
6009ce48e5aSMichal Kubecek 
6019ce48e5aSMichal Kubecek 	/* re-check nwords field, just in case */
6029ce48e5aSMichal Kubecek 	if (__ETHTOOL_LINK_MODE_MASK_NU32
6039ce48e5aSMichal Kubecek 	    != link_ksettings.base.link_mode_masks_nwords)
6049ce48e5aSMichal Kubecek 		return -EINVAL;
6059ce48e5aSMichal Kubecek 
606bdbdac76SOleksij Rempel 	if (link_ksettings.base.master_slave_cfg ||
607bdbdac76SOleksij Rempel 	    link_ksettings.base.master_slave_state)
608bdbdac76SOleksij Rempel 		return -EINVAL;
609bdbdac76SOleksij Rempel 
61073286734SMichal Kubecek 	err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
6111b1b1847SMichal Kubecek 	if (err >= 0) {
61273286734SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
6131b1b1847SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
6141b1b1847SMichal Kubecek 	}
61573286734SMichal Kubecek 	return err;
6169ce48e5aSMichal Kubecek }
6179ce48e5aSMichal Kubecek 
61870ae1e12SCris Forno int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
61970ae1e12SCris Forno 				       const struct ethtool_link_ksettings *cmd,
62070ae1e12SCris Forno 				       u32 *dev_speed, u8 *dev_duplex)
62170ae1e12SCris Forno {
62270ae1e12SCris Forno 	u32 speed;
62370ae1e12SCris Forno 	u8 duplex;
62470ae1e12SCris Forno 
62570ae1e12SCris Forno 	speed = cmd->base.speed;
62670ae1e12SCris Forno 	duplex = cmd->base.duplex;
62770ae1e12SCris Forno 	/* don't allow custom speed and duplex */
62870ae1e12SCris Forno 	if (!ethtool_validate_speed(speed) ||
62970ae1e12SCris Forno 	    !ethtool_validate_duplex(duplex) ||
63070ae1e12SCris Forno 	    !ethtool_virtdev_validate_cmd(cmd))
63170ae1e12SCris Forno 		return -EINVAL;
63270ae1e12SCris Forno 	*dev_speed = speed;
63370ae1e12SCris Forno 	*dev_duplex = duplex;
63470ae1e12SCris Forno 
63570ae1e12SCris Forno 	return 0;
63670ae1e12SCris Forno }
63770ae1e12SCris Forno EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
63870ae1e12SCris Forno 
6399ce48e5aSMichal Kubecek /* Query device for its ethtool_cmd settings.
6409ce48e5aSMichal Kubecek  *
6419ce48e5aSMichal Kubecek  * Backward compatibility note: for compatibility with legacy ethtool, this is
6429ce48e5aSMichal Kubecek  * now implemented via get_link_ksettings. When driver reports higher link mode
6439ce48e5aSMichal Kubecek  * bits, a kernel warning is logged once (with name of 1st driver/device) to
6449ce48e5aSMichal Kubecek  * recommend user to upgrade ethtool, but the command is successful (only the
6459ce48e5aSMichal Kubecek  * lower link mode bits reported back to user). Deprecated fields from
6469ce48e5aSMichal Kubecek  * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
6479ce48e5aSMichal Kubecek  */
6489ce48e5aSMichal Kubecek static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
6499ce48e5aSMichal Kubecek {
6509ce48e5aSMichal Kubecek 	struct ethtool_link_ksettings link_ksettings;
6519ce48e5aSMichal Kubecek 	struct ethtool_cmd cmd;
6529ce48e5aSMichal Kubecek 	int err;
6539ce48e5aSMichal Kubecek 
6549ce48e5aSMichal Kubecek 	ASSERT_RTNL();
6559ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_link_ksettings)
6569ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
6579ce48e5aSMichal Kubecek 
6589ce48e5aSMichal Kubecek 	memset(&link_ksettings, 0, sizeof(link_ksettings));
6599ce48e5aSMichal Kubecek 	err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
6609ce48e5aSMichal Kubecek 	if (err < 0)
6619ce48e5aSMichal Kubecek 		return err;
6629ce48e5aSMichal Kubecek 	convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
6639ce48e5aSMichal Kubecek 
6649ce48e5aSMichal Kubecek 	/* send a sensible cmd tag back to user */
6659ce48e5aSMichal Kubecek 	cmd.cmd = ETHTOOL_GSET;
6669ce48e5aSMichal Kubecek 
6679ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
6689ce48e5aSMichal Kubecek 		return -EFAULT;
6699ce48e5aSMichal Kubecek 
6709ce48e5aSMichal Kubecek 	return 0;
6719ce48e5aSMichal Kubecek }
6729ce48e5aSMichal Kubecek 
6739ce48e5aSMichal Kubecek /* Update device link settings with given ethtool_cmd.
6749ce48e5aSMichal Kubecek  *
6759ce48e5aSMichal Kubecek  * Backward compatibility note: for compatibility with legacy ethtool, this is
6769ce48e5aSMichal Kubecek  * now always implemented via set_link_settings. When user's request updates
6779ce48e5aSMichal Kubecek  * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
6789ce48e5aSMichal Kubecek  * warning is logged once (with name of 1st driver/device) to recommend user to
6799ce48e5aSMichal Kubecek  * upgrade ethtool, and the request is rejected.
6809ce48e5aSMichal Kubecek  */
6819ce48e5aSMichal Kubecek static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
6829ce48e5aSMichal Kubecek {
6839ce48e5aSMichal Kubecek 	struct ethtool_link_ksettings link_ksettings;
6849ce48e5aSMichal Kubecek 	struct ethtool_cmd cmd;
68573286734SMichal Kubecek 	int ret;
6869ce48e5aSMichal Kubecek 
6879ce48e5aSMichal Kubecek 	ASSERT_RTNL();
6889ce48e5aSMichal Kubecek 
6899ce48e5aSMichal Kubecek 	if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
6909ce48e5aSMichal Kubecek 		return -EFAULT;
6919ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_link_ksettings)
6929ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
6939ce48e5aSMichal Kubecek 
6949ce48e5aSMichal Kubecek 	if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
6959ce48e5aSMichal Kubecek 		return -EINVAL;
6969ce48e5aSMichal Kubecek 	link_ksettings.base.link_mode_masks_nwords =
6979ce48e5aSMichal Kubecek 		__ETHTOOL_LINK_MODE_MASK_NU32;
69873286734SMichal Kubecek 	ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
6991b1b1847SMichal Kubecek 	if (ret >= 0) {
70073286734SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
7011b1b1847SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
7021b1b1847SMichal Kubecek 	}
70373286734SMichal Kubecek 	return ret;
7049ce48e5aSMichal Kubecek }
7059ce48e5aSMichal Kubecek 
706095cfcfeSJakub Kicinski static int
707095cfcfeSJakub Kicinski ethtool_get_drvinfo(struct net_device *dev, struct ethtool_devlink_compat *rsp)
7089ce48e5aSMichal Kubecek {
7099ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
710edaf5df2SVincent Mailhol 	struct device *parent = dev->dev.parent;
7119ce48e5aSMichal Kubecek 
712095cfcfeSJakub Kicinski 	rsp->info.cmd = ETHTOOL_GDRVINFO;
713a71af890SWolfram Sang 	strscpy(rsp->info.version, UTS_RELEASE, sizeof(rsp->info.version));
7149ce48e5aSMichal Kubecek 	if (ops->get_drvinfo) {
715095cfcfeSJakub Kicinski 		ops->get_drvinfo(dev, &rsp->info);
716edaf5df2SVincent Mailhol 		if (!rsp->info.bus_info[0] && parent)
717edaf5df2SVincent Mailhol 			strscpy(rsp->info.bus_info, dev_name(parent),
718095cfcfeSJakub Kicinski 				sizeof(rsp->info.bus_info));
719edaf5df2SVincent Mailhol 		if (!rsp->info.driver[0] && parent && parent->driver)
720edaf5df2SVincent Mailhol 			strscpy(rsp->info.driver, parent->driver->name,
721edaf5df2SVincent Mailhol 				sizeof(rsp->info.driver));
722edaf5df2SVincent Mailhol 	} else if (parent && parent->driver) {
723edaf5df2SVincent Mailhol 		strscpy(rsp->info.bus_info, dev_name(parent),
724edaf5df2SVincent Mailhol 			sizeof(rsp->info.bus_info));
725edaf5df2SVincent Mailhol 		strscpy(rsp->info.driver, parent->driver->name,
726095cfcfeSJakub Kicinski 			sizeof(rsp->info.driver));
727bde3b0fdSTonghao Zhang 	} else if (dev->rtnl_link_ops) {
728a71af890SWolfram Sang 		strscpy(rsp->info.driver, dev->rtnl_link_ops->kind,
729bde3b0fdSTonghao Zhang 			sizeof(rsp->info.driver));
7309ce48e5aSMichal Kubecek 	} else {
7319ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
7329ce48e5aSMichal Kubecek 	}
7339ce48e5aSMichal Kubecek 
7349ce48e5aSMichal Kubecek 	/*
7359ce48e5aSMichal Kubecek 	 * this method of obtaining string set info is deprecated;
7369ce48e5aSMichal Kubecek 	 * Use ETHTOOL_GSSET_INFO instead.
7379ce48e5aSMichal Kubecek 	 */
7389ce48e5aSMichal Kubecek 	if (ops->get_sset_count) {
7399ce48e5aSMichal Kubecek 		int rc;
7409ce48e5aSMichal Kubecek 
7419ce48e5aSMichal Kubecek 		rc = ops->get_sset_count(dev, ETH_SS_TEST);
7429ce48e5aSMichal Kubecek 		if (rc >= 0)
743095cfcfeSJakub Kicinski 			rsp->info.testinfo_len = rc;
7449ce48e5aSMichal Kubecek 		rc = ops->get_sset_count(dev, ETH_SS_STATS);
7459ce48e5aSMichal Kubecek 		if (rc >= 0)
746095cfcfeSJakub Kicinski 			rsp->info.n_stats = rc;
7479ce48e5aSMichal Kubecek 		rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
7489ce48e5aSMichal Kubecek 		if (rc >= 0)
749095cfcfeSJakub Kicinski 			rsp->info.n_priv_flags = rc;
7509ce48e5aSMichal Kubecek 	}
7519ce48e5aSMichal Kubecek 	if (ops->get_regs_len) {
7529ce48e5aSMichal Kubecek 		int ret = ops->get_regs_len(dev);
7539ce48e5aSMichal Kubecek 
7549ce48e5aSMichal Kubecek 		if (ret > 0)
755095cfcfeSJakub Kicinski 			rsp->info.regdump_len = ret;
7569ce48e5aSMichal Kubecek 	}
7579ce48e5aSMichal Kubecek 
7589ce48e5aSMichal Kubecek 	if (ops->get_eeprom_len)
759095cfcfeSJakub Kicinski 		rsp->info.eedump_len = ops->get_eeprom_len(dev);
7609ce48e5aSMichal Kubecek 
761095cfcfeSJakub Kicinski 	if (!rsp->info.fw_version[0])
7621af0a094SJakub Kicinski 		rsp->devlink = netdev_to_devlink_get(dev);
7631af0a094SJakub Kicinski 
7649ce48e5aSMichal Kubecek 	return 0;
7659ce48e5aSMichal Kubecek }
7669ce48e5aSMichal Kubecek 
7679ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
7689ce48e5aSMichal Kubecek 						    void __user *useraddr)
7699ce48e5aSMichal Kubecek {
7709ce48e5aSMichal Kubecek 	struct ethtool_sset_info info;
7719ce48e5aSMichal Kubecek 	u64 sset_mask;
7729ce48e5aSMichal Kubecek 	int i, idx = 0, n_bits = 0, ret, rc;
7739ce48e5aSMichal Kubecek 	u32 *info_buf = NULL;
7749ce48e5aSMichal Kubecek 
7759ce48e5aSMichal Kubecek 	if (copy_from_user(&info, useraddr, sizeof(info)))
7769ce48e5aSMichal Kubecek 		return -EFAULT;
7779ce48e5aSMichal Kubecek 
7789ce48e5aSMichal Kubecek 	/* store copy of mask, because we zero struct later on */
7799ce48e5aSMichal Kubecek 	sset_mask = info.sset_mask;
7809ce48e5aSMichal Kubecek 	if (!sset_mask)
7819ce48e5aSMichal Kubecek 		return 0;
7829ce48e5aSMichal Kubecek 
7839ce48e5aSMichal Kubecek 	/* calculate size of return buffer */
7849ce48e5aSMichal Kubecek 	n_bits = hweight64(sset_mask);
7859ce48e5aSMichal Kubecek 
7869ce48e5aSMichal Kubecek 	memset(&info, 0, sizeof(info));
7879ce48e5aSMichal Kubecek 	info.cmd = ETHTOOL_GSSET_INFO;
7889ce48e5aSMichal Kubecek 
7899ce48e5aSMichal Kubecek 	info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
7909ce48e5aSMichal Kubecek 	if (!info_buf)
7919ce48e5aSMichal Kubecek 		return -ENOMEM;
7929ce48e5aSMichal Kubecek 
7939ce48e5aSMichal Kubecek 	/*
7949ce48e5aSMichal Kubecek 	 * fill return buffer based on input bitmask and successful
7959ce48e5aSMichal Kubecek 	 * get_sset_count return
7969ce48e5aSMichal Kubecek 	 */
7979ce48e5aSMichal Kubecek 	for (i = 0; i < 64; i++) {
7989ce48e5aSMichal Kubecek 		if (!(sset_mask & (1ULL << i)))
7999ce48e5aSMichal Kubecek 			continue;
8009ce48e5aSMichal Kubecek 
8019ce48e5aSMichal Kubecek 		rc = __ethtool_get_sset_count(dev, i);
8029ce48e5aSMichal Kubecek 		if (rc >= 0) {
8039ce48e5aSMichal Kubecek 			info.sset_mask |= (1ULL << i);
8049ce48e5aSMichal Kubecek 			info_buf[idx++] = rc;
8059ce48e5aSMichal Kubecek 		}
8069ce48e5aSMichal Kubecek 	}
8079ce48e5aSMichal Kubecek 
8089ce48e5aSMichal Kubecek 	ret = -EFAULT;
8099ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &info, sizeof(info)))
8109ce48e5aSMichal Kubecek 		goto out;
8119ce48e5aSMichal Kubecek 
8129ce48e5aSMichal Kubecek 	useraddr += offsetof(struct ethtool_sset_info, data);
813ed717613SGustavo A. R. Silva 	if (copy_to_user(useraddr, info_buf, array_size(idx, sizeof(u32))))
8149ce48e5aSMichal Kubecek 		goto out;
8159ce48e5aSMichal Kubecek 
8169ce48e5aSMichal Kubecek 	ret = 0;
8179ce48e5aSMichal Kubecek 
8189ce48e5aSMichal Kubecek out:
8199ce48e5aSMichal Kubecek 	kfree(info_buf);
8209ce48e5aSMichal Kubecek 	return ret;
8219ce48e5aSMichal Kubecek }
8229ce48e5aSMichal Kubecek 
823dd98d289SArnd Bergmann static noinline_for_stack int
824dd98d289SArnd Bergmann ethtool_rxnfc_copy_from_compat(struct ethtool_rxnfc *rxnfc,
825dd98d289SArnd Bergmann 			       const struct compat_ethtool_rxnfc __user *useraddr,
826dd98d289SArnd Bergmann 			       size_t size)
827dd98d289SArnd Bergmann {
828dd98d289SArnd Bergmann 	struct compat_ethtool_rxnfc crxnfc = {};
829dd98d289SArnd Bergmann 
830dd98d289SArnd Bergmann 	/* We expect there to be holes between fs.m_ext and
831dd98d289SArnd Bergmann 	 * fs.ring_cookie and at the end of fs, but nowhere else.
832dd98d289SArnd Bergmann 	 * On non-x86, no conversion should be needed.
833dd98d289SArnd Bergmann 	 */
834dd98d289SArnd Bergmann 	BUILD_BUG_ON(!IS_ENABLED(CONFIG_X86_64) &&
835dd98d289SArnd Bergmann 		     sizeof(struct compat_ethtool_rxnfc) !=
836dd98d289SArnd Bergmann 		     sizeof(struct ethtool_rxnfc));
837dd98d289SArnd Bergmann 	BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.m_ext) +
838dd98d289SArnd Bergmann 		     sizeof(useraddr->fs.m_ext) !=
839dd98d289SArnd Bergmann 		     offsetof(struct ethtool_rxnfc, fs.m_ext) +
840dd98d289SArnd Bergmann 		     sizeof(rxnfc->fs.m_ext));
841dd98d289SArnd Bergmann 	BUILD_BUG_ON(offsetof(struct compat_ethtool_rxnfc, fs.location) -
842dd98d289SArnd Bergmann 		     offsetof(struct compat_ethtool_rxnfc, fs.ring_cookie) !=
843dd98d289SArnd Bergmann 		     offsetof(struct ethtool_rxnfc, fs.location) -
844dd98d289SArnd Bergmann 		     offsetof(struct ethtool_rxnfc, fs.ring_cookie));
845dd98d289SArnd Bergmann 
846dd98d289SArnd Bergmann 	if (copy_from_user(&crxnfc, useraddr, min(size, sizeof(crxnfc))))
847dd98d289SArnd Bergmann 		return -EFAULT;
848dd98d289SArnd Bergmann 
849dd98d289SArnd Bergmann 	*rxnfc = (struct ethtool_rxnfc) {
850dd98d289SArnd Bergmann 		.cmd		= crxnfc.cmd,
851dd98d289SArnd Bergmann 		.flow_type	= crxnfc.flow_type,
852dd98d289SArnd Bergmann 		.data		= crxnfc.data,
853dd98d289SArnd Bergmann 		.fs		= {
854dd98d289SArnd Bergmann 			.flow_type	= crxnfc.fs.flow_type,
855dd98d289SArnd Bergmann 			.h_u		= crxnfc.fs.h_u,
856dd98d289SArnd Bergmann 			.h_ext		= crxnfc.fs.h_ext,
857dd98d289SArnd Bergmann 			.m_u		= crxnfc.fs.m_u,
858dd98d289SArnd Bergmann 			.m_ext		= crxnfc.fs.m_ext,
859dd98d289SArnd Bergmann 			.ring_cookie	= crxnfc.fs.ring_cookie,
860dd98d289SArnd Bergmann 			.location	= crxnfc.fs.location,
861dd98d289SArnd Bergmann 		},
862dd98d289SArnd Bergmann 		.rule_cnt	= crxnfc.rule_cnt,
863dd98d289SArnd Bergmann 	};
864dd98d289SArnd Bergmann 
865dd98d289SArnd Bergmann 	return 0;
866dd98d289SArnd Bergmann }
867dd98d289SArnd Bergmann 
868dd98d289SArnd Bergmann static int ethtool_rxnfc_copy_from_user(struct ethtool_rxnfc *rxnfc,
869dd98d289SArnd Bergmann 					const void __user *useraddr,
870dd98d289SArnd Bergmann 					size_t size)
871dd98d289SArnd Bergmann {
872dd98d289SArnd Bergmann 	if (compat_need_64bit_alignment_fixup())
873dd98d289SArnd Bergmann 		return ethtool_rxnfc_copy_from_compat(rxnfc, useraddr, size);
874dd98d289SArnd Bergmann 
875dd98d289SArnd Bergmann 	if (copy_from_user(rxnfc, useraddr, size))
876dd98d289SArnd Bergmann 		return -EFAULT;
877dd98d289SArnd Bergmann 
878dd98d289SArnd Bergmann 	return 0;
879dd98d289SArnd Bergmann }
880dd98d289SArnd Bergmann 
881dd98d289SArnd Bergmann static int ethtool_rxnfc_copy_to_compat(void __user *useraddr,
882dd98d289SArnd Bergmann 					const struct ethtool_rxnfc *rxnfc,
883dd98d289SArnd Bergmann 					size_t size, const u32 *rule_buf)
884dd98d289SArnd Bergmann {
885dd98d289SArnd Bergmann 	struct compat_ethtool_rxnfc crxnfc;
886dd98d289SArnd Bergmann 
887dd98d289SArnd Bergmann 	memset(&crxnfc, 0, sizeof(crxnfc));
888dd98d289SArnd Bergmann 	crxnfc = (struct compat_ethtool_rxnfc) {
889dd98d289SArnd Bergmann 		.cmd		= rxnfc->cmd,
890dd98d289SArnd Bergmann 		.flow_type	= rxnfc->flow_type,
891dd98d289SArnd Bergmann 		.data		= rxnfc->data,
892dd98d289SArnd Bergmann 		.fs		= {
893dd98d289SArnd Bergmann 			.flow_type	= rxnfc->fs.flow_type,
894dd98d289SArnd Bergmann 			.h_u		= rxnfc->fs.h_u,
895dd98d289SArnd Bergmann 			.h_ext		= rxnfc->fs.h_ext,
896dd98d289SArnd Bergmann 			.m_u		= rxnfc->fs.m_u,
897dd98d289SArnd Bergmann 			.m_ext		= rxnfc->fs.m_ext,
898dd98d289SArnd Bergmann 			.ring_cookie	= rxnfc->fs.ring_cookie,
899dd98d289SArnd Bergmann 			.location	= rxnfc->fs.location,
900dd98d289SArnd Bergmann 		},
901dd98d289SArnd Bergmann 		.rule_cnt	= rxnfc->rule_cnt,
902dd98d289SArnd Bergmann 	};
903dd98d289SArnd Bergmann 
904dd98d289SArnd Bergmann 	if (copy_to_user(useraddr, &crxnfc, min(size, sizeof(crxnfc))))
905dd98d289SArnd Bergmann 		return -EFAULT;
906dd98d289SArnd Bergmann 
907dd98d289SArnd Bergmann 	return 0;
908dd98d289SArnd Bergmann }
909dd98d289SArnd Bergmann 
910801b27e8SJoe Damato static int ethtool_rxnfc_copy_struct(u32 cmd, struct ethtool_rxnfc *info,
911801b27e8SJoe Damato 				     size_t *info_size, void __user *useraddr)
912801b27e8SJoe Damato {
913801b27e8SJoe Damato 	/* struct ethtool_rxnfc was originally defined for
914801b27e8SJoe Damato 	 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
915801b27e8SJoe Damato 	 * members.  User-space might still be using that
916801b27e8SJoe Damato 	 * definition.
917801b27e8SJoe Damato 	 */
918801b27e8SJoe Damato 	if (cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH)
919801b27e8SJoe Damato 		*info_size = (offsetof(struct ethtool_rxnfc, data) +
920801b27e8SJoe Damato 			      sizeof(info->data));
921801b27e8SJoe Damato 
922801b27e8SJoe Damato 	if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
923801b27e8SJoe Damato 		return -EFAULT;
924801b27e8SJoe Damato 
925801b27e8SJoe Damato 	if ((cmd == ETHTOOL_GRXFH || cmd == ETHTOOL_SRXFH) && info->flow_type & FLOW_RSS) {
926801b27e8SJoe Damato 		*info_size = sizeof(*info);
927801b27e8SJoe Damato 		if (ethtool_rxnfc_copy_from_user(info, useraddr, *info_size))
928801b27e8SJoe Damato 			return -EFAULT;
929801b27e8SJoe Damato 		/* Since malicious users may modify the original data,
930801b27e8SJoe Damato 		 * we need to check whether FLOW_RSS is still requested.
931801b27e8SJoe Damato 		 */
932801b27e8SJoe Damato 		if (!(info->flow_type & FLOW_RSS))
933801b27e8SJoe Damato 			return -EINVAL;
934801b27e8SJoe Damato 	}
935801b27e8SJoe Damato 
936801b27e8SJoe Damato 	if (info->cmd != cmd)
937801b27e8SJoe Damato 		return -EINVAL;
938801b27e8SJoe Damato 
939801b27e8SJoe Damato 	return 0;
940801b27e8SJoe Damato }
941801b27e8SJoe Damato 
942dd98d289SArnd Bergmann static int ethtool_rxnfc_copy_to_user(void __user *useraddr,
943dd98d289SArnd Bergmann 				      const struct ethtool_rxnfc *rxnfc,
944dd98d289SArnd Bergmann 				      size_t size, const u32 *rule_buf)
945dd98d289SArnd Bergmann {
946dd98d289SArnd Bergmann 	int ret;
947dd98d289SArnd Bergmann 
948dd98d289SArnd Bergmann 	if (compat_need_64bit_alignment_fixup()) {
949dd98d289SArnd Bergmann 		ret = ethtool_rxnfc_copy_to_compat(useraddr, rxnfc, size,
950dd98d289SArnd Bergmann 						   rule_buf);
951dd98d289SArnd Bergmann 		useraddr += offsetof(struct compat_ethtool_rxnfc, rule_locs);
952dd98d289SArnd Bergmann 	} else {
9539b29a161SSaeed Mahameed 		ret = copy_to_user(useraddr, rxnfc, size);
954dd98d289SArnd Bergmann 		useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
955dd98d289SArnd Bergmann 	}
956dd98d289SArnd Bergmann 
957dd98d289SArnd Bergmann 	if (ret)
958dd98d289SArnd Bergmann 		return -EFAULT;
959dd98d289SArnd Bergmann 
960dd98d289SArnd Bergmann 	if (rule_buf) {
961dd98d289SArnd Bergmann 		if (copy_to_user(useraddr, rule_buf,
962dd98d289SArnd Bergmann 				 rxnfc->rule_cnt * sizeof(u32)))
963dd98d289SArnd Bergmann 			return -EFAULT;
964dd98d289SArnd Bergmann 	}
965dd98d289SArnd Bergmann 
966dd98d289SArnd Bergmann 	return 0;
967dd98d289SArnd Bergmann }
968dd98d289SArnd Bergmann 
9699ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
9709ce48e5aSMichal Kubecek 						u32 cmd, void __user *useraddr)
9719ce48e5aSMichal Kubecek {
9729ce48e5aSMichal Kubecek 	struct ethtool_rxnfc info;
9739ce48e5aSMichal Kubecek 	size_t info_size = sizeof(info);
9749ce48e5aSMichal Kubecek 	int rc;
9759ce48e5aSMichal Kubecek 
9769ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_rxnfc)
9779ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
9789ce48e5aSMichal Kubecek 
979801b27e8SJoe Damato 	rc = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
980801b27e8SJoe Damato 	if (rc)
981801b27e8SJoe Damato 		return rc;
9829ce48e5aSMichal Kubecek 
9839ce48e5aSMichal Kubecek 	rc = dev->ethtool_ops->set_rxnfc(dev, &info);
9849ce48e5aSMichal Kubecek 	if (rc)
9859ce48e5aSMichal Kubecek 		return rc;
9869ce48e5aSMichal Kubecek 
9879ce48e5aSMichal Kubecek 	if (cmd == ETHTOOL_SRXCLSRLINS &&
988dd98d289SArnd Bergmann 	    ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, NULL))
9899ce48e5aSMichal Kubecek 		return -EFAULT;
9909ce48e5aSMichal Kubecek 
9919ce48e5aSMichal Kubecek 	return 0;
9929ce48e5aSMichal Kubecek }
9939ce48e5aSMichal Kubecek 
9949ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
9959ce48e5aSMichal Kubecek 						u32 cmd, void __user *useraddr)
9969ce48e5aSMichal Kubecek {
9979ce48e5aSMichal Kubecek 	struct ethtool_rxnfc info;
9989ce48e5aSMichal Kubecek 	size_t info_size = sizeof(info);
9999ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
10009ce48e5aSMichal Kubecek 	int ret;
10019ce48e5aSMichal Kubecek 	void *rule_buf = NULL;
10029ce48e5aSMichal Kubecek 
10039ce48e5aSMichal Kubecek 	if (!ops->get_rxnfc)
10049ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
10059ce48e5aSMichal Kubecek 
1006801b27e8SJoe Damato 	ret = ethtool_rxnfc_copy_struct(cmd, &info, &info_size, useraddr);
1007801b27e8SJoe Damato 	if (ret)
1008801b27e8SJoe Damato 		return ret;
10099ce48e5aSMichal Kubecek 
10109ce48e5aSMichal Kubecek 	if (info.cmd == ETHTOOL_GRXCLSRLALL) {
10119ce48e5aSMichal Kubecek 		if (info.rule_cnt > 0) {
10129ce48e5aSMichal Kubecek 			if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
10139ce48e5aSMichal Kubecek 				rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
10149ce48e5aSMichal Kubecek 						   GFP_USER);
10159ce48e5aSMichal Kubecek 			if (!rule_buf)
10169ce48e5aSMichal Kubecek 				return -ENOMEM;
10179ce48e5aSMichal Kubecek 		}
10189ce48e5aSMichal Kubecek 	}
10199ce48e5aSMichal Kubecek 
10209ce48e5aSMichal Kubecek 	ret = ops->get_rxnfc(dev, &info, rule_buf);
10219ce48e5aSMichal Kubecek 	if (ret < 0)
10229ce48e5aSMichal Kubecek 		goto err_out;
10239ce48e5aSMichal Kubecek 
1024dd98d289SArnd Bergmann 	ret = ethtool_rxnfc_copy_to_user(useraddr, &info, info_size, rule_buf);
10259ce48e5aSMichal Kubecek err_out:
10269ce48e5aSMichal Kubecek 	kfree(rule_buf);
10279ce48e5aSMichal Kubecek 
10289ce48e5aSMichal Kubecek 	return ret;
10299ce48e5aSMichal Kubecek }
10309ce48e5aSMichal Kubecek 
10319ce48e5aSMichal Kubecek static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
10329ce48e5aSMichal Kubecek 					struct ethtool_rxnfc *rx_rings,
10339ce48e5aSMichal Kubecek 					u32 size)
10349ce48e5aSMichal Kubecek {
10359ce48e5aSMichal Kubecek 	int i;
10369ce48e5aSMichal Kubecek 
1037ed717613SGustavo A. R. Silva 	if (copy_from_user(indir, useraddr, array_size(size, sizeof(indir[0]))))
10389ce48e5aSMichal Kubecek 		return -EFAULT;
10399ce48e5aSMichal Kubecek 
10409ce48e5aSMichal Kubecek 	/* Validate ring indices */
10419ce48e5aSMichal Kubecek 	for (i = 0; i < size; i++)
10429ce48e5aSMichal Kubecek 		if (indir[i] >= rx_rings->data)
10439ce48e5aSMichal Kubecek 			return -EINVAL;
10449ce48e5aSMichal Kubecek 
10459ce48e5aSMichal Kubecek 	return 0;
10469ce48e5aSMichal Kubecek }
10479ce48e5aSMichal Kubecek 
10489ce48e5aSMichal Kubecek u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
10499ce48e5aSMichal Kubecek 
10509ce48e5aSMichal Kubecek void netdev_rss_key_fill(void *buffer, size_t len)
10519ce48e5aSMichal Kubecek {
10529ce48e5aSMichal Kubecek 	BUG_ON(len > sizeof(netdev_rss_key));
10539ce48e5aSMichal Kubecek 	net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
10549ce48e5aSMichal Kubecek 	memcpy(buffer, netdev_rss_key, len);
10559ce48e5aSMichal Kubecek }
10569ce48e5aSMichal Kubecek EXPORT_SYMBOL(netdev_rss_key_fill);
10579ce48e5aSMichal Kubecek 
10589ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
10599ce48e5aSMichal Kubecek 						     void __user *useraddr)
10609ce48e5aSMichal Kubecek {
10619ce48e5aSMichal Kubecek 	u32 user_size, dev_size;
10629ce48e5aSMichal Kubecek 	u32 *indir;
10639ce48e5aSMichal Kubecek 	int ret;
10649ce48e5aSMichal Kubecek 
10659ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_rxfh_indir_size ||
10669ce48e5aSMichal Kubecek 	    !dev->ethtool_ops->get_rxfh)
10679ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
10689ce48e5aSMichal Kubecek 	dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
10699ce48e5aSMichal Kubecek 	if (dev_size == 0)
10709ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
10719ce48e5aSMichal Kubecek 
10729ce48e5aSMichal Kubecek 	if (copy_from_user(&user_size,
10739ce48e5aSMichal Kubecek 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
10749ce48e5aSMichal Kubecek 			   sizeof(user_size)))
10759ce48e5aSMichal Kubecek 		return -EFAULT;
10769ce48e5aSMichal Kubecek 
10779ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
10789ce48e5aSMichal Kubecek 			 &dev_size, sizeof(dev_size)))
10799ce48e5aSMichal Kubecek 		return -EFAULT;
10809ce48e5aSMichal Kubecek 
10819ce48e5aSMichal Kubecek 	/* If the user buffer size is 0, this is just a query for the
10829ce48e5aSMichal Kubecek 	 * device table size.  Otherwise, if it's smaller than the
10839ce48e5aSMichal Kubecek 	 * device table size it's an error.
10849ce48e5aSMichal Kubecek 	 */
10859ce48e5aSMichal Kubecek 	if (user_size < dev_size)
10869ce48e5aSMichal Kubecek 		return user_size == 0 ? 0 : -EINVAL;
10879ce48e5aSMichal Kubecek 
10889ce48e5aSMichal Kubecek 	indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
10899ce48e5aSMichal Kubecek 	if (!indir)
10909ce48e5aSMichal Kubecek 		return -ENOMEM;
10919ce48e5aSMichal Kubecek 
10929ce48e5aSMichal Kubecek 	ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
10939ce48e5aSMichal Kubecek 	if (ret)
10949ce48e5aSMichal Kubecek 		goto out;
10959ce48e5aSMichal Kubecek 
10969ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr +
10979ce48e5aSMichal Kubecek 			 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
10989ce48e5aSMichal Kubecek 			 indir, dev_size * sizeof(indir[0])))
10999ce48e5aSMichal Kubecek 		ret = -EFAULT;
11009ce48e5aSMichal Kubecek 
11019ce48e5aSMichal Kubecek out:
11029ce48e5aSMichal Kubecek 	kfree(indir);
11039ce48e5aSMichal Kubecek 	return ret;
11049ce48e5aSMichal Kubecek }
11059ce48e5aSMichal Kubecek 
11069ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
11079ce48e5aSMichal Kubecek 						     void __user *useraddr)
11089ce48e5aSMichal Kubecek {
11099ce48e5aSMichal Kubecek 	struct ethtool_rxnfc rx_rings;
11109ce48e5aSMichal Kubecek 	u32 user_size, dev_size, i;
11119ce48e5aSMichal Kubecek 	u32 *indir;
11129ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
11139ce48e5aSMichal Kubecek 	int ret;
11149ce48e5aSMichal Kubecek 	u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
11159ce48e5aSMichal Kubecek 
11169ce48e5aSMichal Kubecek 	if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
11179ce48e5aSMichal Kubecek 	    !ops->get_rxnfc)
11189ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
11199ce48e5aSMichal Kubecek 
11209ce48e5aSMichal Kubecek 	dev_size = ops->get_rxfh_indir_size(dev);
11219ce48e5aSMichal Kubecek 	if (dev_size == 0)
11229ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
11239ce48e5aSMichal Kubecek 
11249ce48e5aSMichal Kubecek 	if (copy_from_user(&user_size,
11259ce48e5aSMichal Kubecek 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
11269ce48e5aSMichal Kubecek 			   sizeof(user_size)))
11279ce48e5aSMichal Kubecek 		return -EFAULT;
11289ce48e5aSMichal Kubecek 
11299ce48e5aSMichal Kubecek 	if (user_size != 0 && user_size != dev_size)
11309ce48e5aSMichal Kubecek 		return -EINVAL;
11319ce48e5aSMichal Kubecek 
11329ce48e5aSMichal Kubecek 	indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
11339ce48e5aSMichal Kubecek 	if (!indir)
11349ce48e5aSMichal Kubecek 		return -ENOMEM;
11359ce48e5aSMichal Kubecek 
11369ce48e5aSMichal Kubecek 	rx_rings.cmd = ETHTOOL_GRXRINGS;
11379ce48e5aSMichal Kubecek 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
11389ce48e5aSMichal Kubecek 	if (ret)
11399ce48e5aSMichal Kubecek 		goto out;
11409ce48e5aSMichal Kubecek 
11419ce48e5aSMichal Kubecek 	if (user_size == 0) {
11429ce48e5aSMichal Kubecek 		for (i = 0; i < dev_size; i++)
11439ce48e5aSMichal Kubecek 			indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
11449ce48e5aSMichal Kubecek 	} else {
11459ce48e5aSMichal Kubecek 		ret = ethtool_copy_validate_indir(indir,
11469ce48e5aSMichal Kubecek 						  useraddr + ringidx_offset,
11479ce48e5aSMichal Kubecek 						  &rx_rings,
11489ce48e5aSMichal Kubecek 						  dev_size);
11499ce48e5aSMichal Kubecek 		if (ret)
11509ce48e5aSMichal Kubecek 			goto out;
11519ce48e5aSMichal Kubecek 	}
11529ce48e5aSMichal Kubecek 
11539ce48e5aSMichal Kubecek 	ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
11549ce48e5aSMichal Kubecek 	if (ret)
11559ce48e5aSMichal Kubecek 		goto out;
11569ce48e5aSMichal Kubecek 
11579ce48e5aSMichal Kubecek 	/* indicate whether rxfh was set to default */
11589ce48e5aSMichal Kubecek 	if (user_size == 0)
11599ce48e5aSMichal Kubecek 		dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
11609ce48e5aSMichal Kubecek 	else
11619ce48e5aSMichal Kubecek 		dev->priv_flags |= IFF_RXFH_CONFIGURED;
11629ce48e5aSMichal Kubecek 
11639ce48e5aSMichal Kubecek out:
11649ce48e5aSMichal Kubecek 	kfree(indir);
11659ce48e5aSMichal Kubecek 	return ret;
11669ce48e5aSMichal Kubecek }
11679ce48e5aSMichal Kubecek 
11689ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
11699ce48e5aSMichal Kubecek 					       void __user *useraddr)
11709ce48e5aSMichal Kubecek {
11719ce48e5aSMichal Kubecek 	int ret;
11729ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
11739ce48e5aSMichal Kubecek 	u32 user_indir_size, user_key_size;
11749ce48e5aSMichal Kubecek 	u32 dev_indir_size = 0, dev_key_size = 0;
11759ce48e5aSMichal Kubecek 	struct ethtool_rxfh rxfh;
11769ce48e5aSMichal Kubecek 	u32 total_size;
11779ce48e5aSMichal Kubecek 	u32 indir_bytes;
11789ce48e5aSMichal Kubecek 	u32 *indir = NULL;
11799ce48e5aSMichal Kubecek 	u8 dev_hfunc = 0;
11809ce48e5aSMichal Kubecek 	u8 *hkey = NULL;
11819ce48e5aSMichal Kubecek 	u8 *rss_config;
11829ce48e5aSMichal Kubecek 
11839ce48e5aSMichal Kubecek 	if (!ops->get_rxfh)
11849ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
11859ce48e5aSMichal Kubecek 
11869ce48e5aSMichal Kubecek 	if (ops->get_rxfh_indir_size)
11879ce48e5aSMichal Kubecek 		dev_indir_size = ops->get_rxfh_indir_size(dev);
11889ce48e5aSMichal Kubecek 	if (ops->get_rxfh_key_size)
11899ce48e5aSMichal Kubecek 		dev_key_size = ops->get_rxfh_key_size(dev);
11909ce48e5aSMichal Kubecek 
11919ce48e5aSMichal Kubecek 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
11929ce48e5aSMichal Kubecek 		return -EFAULT;
11939ce48e5aSMichal Kubecek 	user_indir_size = rxfh.indir_size;
11949ce48e5aSMichal Kubecek 	user_key_size = rxfh.key_size;
11959ce48e5aSMichal Kubecek 
11969ce48e5aSMichal Kubecek 	/* Check that reserved fields are 0 for now */
11979ce48e5aSMichal Kubecek 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
11989ce48e5aSMichal Kubecek 		return -EINVAL;
11999ce48e5aSMichal Kubecek 	/* Most drivers don't handle rss_context, check it's 0 as well */
12009ce48e5aSMichal Kubecek 	if (rxfh.rss_context && !ops->get_rxfh_context)
12019ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
12029ce48e5aSMichal Kubecek 
12039ce48e5aSMichal Kubecek 	rxfh.indir_size = dev_indir_size;
12049ce48e5aSMichal Kubecek 	rxfh.key_size = dev_key_size;
12059ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
12069ce48e5aSMichal Kubecek 		return -EFAULT;
12079ce48e5aSMichal Kubecek 
12089ce48e5aSMichal Kubecek 	if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
12099ce48e5aSMichal Kubecek 	    (user_key_size && (user_key_size != dev_key_size)))
12109ce48e5aSMichal Kubecek 		return -EINVAL;
12119ce48e5aSMichal Kubecek 
12129ce48e5aSMichal Kubecek 	indir_bytes = user_indir_size * sizeof(indir[0]);
12139ce48e5aSMichal Kubecek 	total_size = indir_bytes + user_key_size;
12149ce48e5aSMichal Kubecek 	rss_config = kzalloc(total_size, GFP_USER);
12159ce48e5aSMichal Kubecek 	if (!rss_config)
12169ce48e5aSMichal Kubecek 		return -ENOMEM;
12179ce48e5aSMichal Kubecek 
12189ce48e5aSMichal Kubecek 	if (user_indir_size)
12199ce48e5aSMichal Kubecek 		indir = (u32 *)rss_config;
12209ce48e5aSMichal Kubecek 
12219ce48e5aSMichal Kubecek 	if (user_key_size)
12229ce48e5aSMichal Kubecek 		hkey = rss_config + indir_bytes;
12239ce48e5aSMichal Kubecek 
12249ce48e5aSMichal Kubecek 	if (rxfh.rss_context)
12259ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
12269ce48e5aSMichal Kubecek 							 &dev_hfunc,
12279ce48e5aSMichal Kubecek 							 rxfh.rss_context);
12289ce48e5aSMichal Kubecek 	else
12299ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
12309ce48e5aSMichal Kubecek 	if (ret)
12319ce48e5aSMichal Kubecek 		goto out;
12329ce48e5aSMichal Kubecek 
12339ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
12349ce48e5aSMichal Kubecek 			 &dev_hfunc, sizeof(rxfh.hfunc))) {
12359ce48e5aSMichal Kubecek 		ret = -EFAULT;
12369ce48e5aSMichal Kubecek 	} else if (copy_to_user(useraddr +
12379ce48e5aSMichal Kubecek 			      offsetof(struct ethtool_rxfh, rss_config[0]),
12389ce48e5aSMichal Kubecek 			      rss_config, total_size)) {
12399ce48e5aSMichal Kubecek 		ret = -EFAULT;
12409ce48e5aSMichal Kubecek 	}
12419ce48e5aSMichal Kubecek out:
12429ce48e5aSMichal Kubecek 	kfree(rss_config);
12439ce48e5aSMichal Kubecek 
12449ce48e5aSMichal Kubecek 	return ret;
12459ce48e5aSMichal Kubecek }
12469ce48e5aSMichal Kubecek 
12479ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
12489ce48e5aSMichal Kubecek 					       void __user *useraddr)
12499ce48e5aSMichal Kubecek {
12509ce48e5aSMichal Kubecek 	int ret;
12519ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
12529ce48e5aSMichal Kubecek 	struct ethtool_rxnfc rx_rings;
12539ce48e5aSMichal Kubecek 	struct ethtool_rxfh rxfh;
12549ce48e5aSMichal Kubecek 	u32 dev_indir_size = 0, dev_key_size = 0, i;
12559ce48e5aSMichal Kubecek 	u32 *indir = NULL, indir_bytes = 0;
12569ce48e5aSMichal Kubecek 	u8 *hkey = NULL;
12579ce48e5aSMichal Kubecek 	u8 *rss_config;
12589ce48e5aSMichal Kubecek 	u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
12599ce48e5aSMichal Kubecek 	bool delete = false;
12609ce48e5aSMichal Kubecek 
12619ce48e5aSMichal Kubecek 	if (!ops->get_rxnfc || !ops->set_rxfh)
12629ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
12639ce48e5aSMichal Kubecek 
12649ce48e5aSMichal Kubecek 	if (ops->get_rxfh_indir_size)
12659ce48e5aSMichal Kubecek 		dev_indir_size = ops->get_rxfh_indir_size(dev);
12669ce48e5aSMichal Kubecek 	if (ops->get_rxfh_key_size)
12679ce48e5aSMichal Kubecek 		dev_key_size = ops->get_rxfh_key_size(dev);
12689ce48e5aSMichal Kubecek 
12699ce48e5aSMichal Kubecek 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
12709ce48e5aSMichal Kubecek 		return -EFAULT;
12719ce48e5aSMichal Kubecek 
12729ce48e5aSMichal Kubecek 	/* Check that reserved fields are 0 for now */
12739ce48e5aSMichal Kubecek 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
12749ce48e5aSMichal Kubecek 		return -EINVAL;
12759ce48e5aSMichal Kubecek 	/* Most drivers don't handle rss_context, check it's 0 as well */
12769ce48e5aSMichal Kubecek 	if (rxfh.rss_context && !ops->set_rxfh_context)
12779ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
12789ce48e5aSMichal Kubecek 
12799ce48e5aSMichal Kubecek 	/* If either indir, hash key or function is valid, proceed further.
12809ce48e5aSMichal Kubecek 	 * Must request at least one change: indir size, hash key or function.
12819ce48e5aSMichal Kubecek 	 */
12829ce48e5aSMichal Kubecek 	if ((rxfh.indir_size &&
12839ce48e5aSMichal Kubecek 	     rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
12849ce48e5aSMichal Kubecek 	     rxfh.indir_size != dev_indir_size) ||
12859ce48e5aSMichal Kubecek 	    (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
12869ce48e5aSMichal Kubecek 	    (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
12879ce48e5aSMichal Kubecek 	     rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
12889ce48e5aSMichal Kubecek 		return -EINVAL;
12899ce48e5aSMichal Kubecek 
12909ce48e5aSMichal Kubecek 	if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
12919ce48e5aSMichal Kubecek 		indir_bytes = dev_indir_size * sizeof(indir[0]);
12929ce48e5aSMichal Kubecek 
12939ce48e5aSMichal Kubecek 	rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
12949ce48e5aSMichal Kubecek 	if (!rss_config)
12959ce48e5aSMichal Kubecek 		return -ENOMEM;
12969ce48e5aSMichal Kubecek 
12979ce48e5aSMichal Kubecek 	rx_rings.cmd = ETHTOOL_GRXRINGS;
12989ce48e5aSMichal Kubecek 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
12999ce48e5aSMichal Kubecek 	if (ret)
13009ce48e5aSMichal Kubecek 		goto out;
13019ce48e5aSMichal Kubecek 
13029ce48e5aSMichal Kubecek 	/* rxfh.indir_size == 0 means reset the indir table to default (master
13039ce48e5aSMichal Kubecek 	 * context) or delete the context (other RSS contexts).
13049ce48e5aSMichal Kubecek 	 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
13059ce48e5aSMichal Kubecek 	 */
13069ce48e5aSMichal Kubecek 	if (rxfh.indir_size &&
13079ce48e5aSMichal Kubecek 	    rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
13089ce48e5aSMichal Kubecek 		indir = (u32 *)rss_config;
13099ce48e5aSMichal Kubecek 		ret = ethtool_copy_validate_indir(indir,
13109ce48e5aSMichal Kubecek 						  useraddr + rss_cfg_offset,
13119ce48e5aSMichal Kubecek 						  &rx_rings,
13129ce48e5aSMichal Kubecek 						  rxfh.indir_size);
13139ce48e5aSMichal Kubecek 		if (ret)
13149ce48e5aSMichal Kubecek 			goto out;
13159ce48e5aSMichal Kubecek 	} else if (rxfh.indir_size == 0) {
13169ce48e5aSMichal Kubecek 		if (rxfh.rss_context == 0) {
13179ce48e5aSMichal Kubecek 			indir = (u32 *)rss_config;
13189ce48e5aSMichal Kubecek 			for (i = 0; i < dev_indir_size; i++)
13199ce48e5aSMichal Kubecek 				indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
13209ce48e5aSMichal Kubecek 		} else {
13219ce48e5aSMichal Kubecek 			delete = true;
13229ce48e5aSMichal Kubecek 		}
13239ce48e5aSMichal Kubecek 	}
13249ce48e5aSMichal Kubecek 
13259ce48e5aSMichal Kubecek 	if (rxfh.key_size) {
13269ce48e5aSMichal Kubecek 		hkey = rss_config + indir_bytes;
13279ce48e5aSMichal Kubecek 		if (copy_from_user(hkey,
13289ce48e5aSMichal Kubecek 				   useraddr + rss_cfg_offset + indir_bytes,
13299ce48e5aSMichal Kubecek 				   rxfh.key_size)) {
13309ce48e5aSMichal Kubecek 			ret = -EFAULT;
13319ce48e5aSMichal Kubecek 			goto out;
13329ce48e5aSMichal Kubecek 		}
13339ce48e5aSMichal Kubecek 	}
13349ce48e5aSMichal Kubecek 
13359ce48e5aSMichal Kubecek 	if (rxfh.rss_context)
13369ce48e5aSMichal Kubecek 		ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
13379ce48e5aSMichal Kubecek 					    &rxfh.rss_context, delete);
13389ce48e5aSMichal Kubecek 	else
13399ce48e5aSMichal Kubecek 		ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
13409ce48e5aSMichal Kubecek 	if (ret)
13419ce48e5aSMichal Kubecek 		goto out;
13429ce48e5aSMichal Kubecek 
13439ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
13449ce48e5aSMichal Kubecek 			 &rxfh.rss_context, sizeof(rxfh.rss_context)))
13459ce48e5aSMichal Kubecek 		ret = -EFAULT;
13469ce48e5aSMichal Kubecek 
13479ce48e5aSMichal Kubecek 	if (!rxfh.rss_context) {
13489ce48e5aSMichal Kubecek 		/* indicate whether rxfh was set to default */
13499ce48e5aSMichal Kubecek 		if (rxfh.indir_size == 0)
13509ce48e5aSMichal Kubecek 			dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
13519ce48e5aSMichal Kubecek 		else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
13529ce48e5aSMichal Kubecek 			dev->priv_flags |= IFF_RXFH_CONFIGURED;
13539ce48e5aSMichal Kubecek 	}
13549ce48e5aSMichal Kubecek 
13559ce48e5aSMichal Kubecek out:
13569ce48e5aSMichal Kubecek 	kfree(rss_config);
13579ce48e5aSMichal Kubecek 	return ret;
13589ce48e5aSMichal Kubecek }
13599ce48e5aSMichal Kubecek 
13609ce48e5aSMichal Kubecek static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
13619ce48e5aSMichal Kubecek {
13629ce48e5aSMichal Kubecek 	struct ethtool_regs regs;
13639ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
13649ce48e5aSMichal Kubecek 	void *regbuf;
13659ce48e5aSMichal Kubecek 	int reglen, ret;
13669ce48e5aSMichal Kubecek 
13679ce48e5aSMichal Kubecek 	if (!ops->get_regs || !ops->get_regs_len)
13689ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
13699ce48e5aSMichal Kubecek 
13709ce48e5aSMichal Kubecek 	if (copy_from_user(&regs, useraddr, sizeof(regs)))
13719ce48e5aSMichal Kubecek 		return -EFAULT;
13729ce48e5aSMichal Kubecek 
13739ce48e5aSMichal Kubecek 	reglen = ops->get_regs_len(dev);
13749ce48e5aSMichal Kubecek 	if (reglen <= 0)
13759ce48e5aSMichal Kubecek 		return reglen;
13769ce48e5aSMichal Kubecek 
13779ce48e5aSMichal Kubecek 	if (regs.len > reglen)
13789ce48e5aSMichal Kubecek 		regs.len = reglen;
13799ce48e5aSMichal Kubecek 
13809ce48e5aSMichal Kubecek 	regbuf = vzalloc(reglen);
13819ce48e5aSMichal Kubecek 	if (!regbuf)
13829ce48e5aSMichal Kubecek 		return -ENOMEM;
13839ce48e5aSMichal Kubecek 
13849ce48e5aSMichal Kubecek 	if (regs.len < reglen)
13859ce48e5aSMichal Kubecek 		reglen = regs.len;
13869ce48e5aSMichal Kubecek 
13879ce48e5aSMichal Kubecek 	ops->get_regs(dev, &regs, regbuf);
13889ce48e5aSMichal Kubecek 
13899ce48e5aSMichal Kubecek 	ret = -EFAULT;
13909ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &regs, sizeof(regs)))
13919ce48e5aSMichal Kubecek 		goto out;
13929ce48e5aSMichal Kubecek 	useraddr += offsetof(struct ethtool_regs, data);
13939ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, regbuf, reglen))
13949ce48e5aSMichal Kubecek 		goto out;
13959ce48e5aSMichal Kubecek 	ret = 0;
13969ce48e5aSMichal Kubecek 
13979ce48e5aSMichal Kubecek  out:
13989ce48e5aSMichal Kubecek 	vfree(regbuf);
13999ce48e5aSMichal Kubecek 	return ret;
14009ce48e5aSMichal Kubecek }
14019ce48e5aSMichal Kubecek 
14029ce48e5aSMichal Kubecek static int ethtool_reset(struct net_device *dev, char __user *useraddr)
14039ce48e5aSMichal Kubecek {
14049ce48e5aSMichal Kubecek 	struct ethtool_value reset;
14059ce48e5aSMichal Kubecek 	int ret;
14069ce48e5aSMichal Kubecek 
14079ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->reset)
14089ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
14099ce48e5aSMichal Kubecek 
14109ce48e5aSMichal Kubecek 	if (copy_from_user(&reset, useraddr, sizeof(reset)))
14119ce48e5aSMichal Kubecek 		return -EFAULT;
14129ce48e5aSMichal Kubecek 
14139ce48e5aSMichal Kubecek 	ret = dev->ethtool_ops->reset(dev, &reset.data);
14149ce48e5aSMichal Kubecek 	if (ret)
14159ce48e5aSMichal Kubecek 		return ret;
14169ce48e5aSMichal Kubecek 
14179ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &reset, sizeof(reset)))
14189ce48e5aSMichal Kubecek 		return -EFAULT;
14199ce48e5aSMichal Kubecek 	return 0;
14209ce48e5aSMichal Kubecek }
14219ce48e5aSMichal Kubecek 
14229ce48e5aSMichal Kubecek static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
14239ce48e5aSMichal Kubecek {
14249ce48e5aSMichal Kubecek 	struct ethtool_wolinfo wol;
14259ce48e5aSMichal Kubecek 
14269ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_wol)
14279ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
14289ce48e5aSMichal Kubecek 
14299ce48e5aSMichal Kubecek 	memset(&wol, 0, sizeof(struct ethtool_wolinfo));
14309ce48e5aSMichal Kubecek 	wol.cmd = ETHTOOL_GWOL;
14319ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_wol(dev, &wol);
14329ce48e5aSMichal Kubecek 
14339ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &wol, sizeof(wol)))
14349ce48e5aSMichal Kubecek 		return -EFAULT;
14359ce48e5aSMichal Kubecek 	return 0;
14369ce48e5aSMichal Kubecek }
14379ce48e5aSMichal Kubecek 
14389ce48e5aSMichal Kubecek static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
14399ce48e5aSMichal Kubecek {
144055b24334SJustin Chen 	struct ethtool_wolinfo wol, cur_wol;
14419ce48e5aSMichal Kubecek 	int ret;
14429ce48e5aSMichal Kubecek 
144355b24334SJustin Chen 	if (!dev->ethtool_ops->get_wol || !dev->ethtool_ops->set_wol)
14449ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
14459ce48e5aSMichal Kubecek 
144655b24334SJustin Chen 	memset(&cur_wol, 0, sizeof(struct ethtool_wolinfo));
144755b24334SJustin Chen 	cur_wol.cmd = ETHTOOL_GWOL;
144855b24334SJustin Chen 	dev->ethtool_ops->get_wol(dev, &cur_wol);
144955b24334SJustin Chen 
14509ce48e5aSMichal Kubecek 	if (copy_from_user(&wol, useraddr, sizeof(wol)))
14519ce48e5aSMichal Kubecek 		return -EFAULT;
14529ce48e5aSMichal Kubecek 
145355b24334SJustin Chen 	if (wol.wolopts & ~cur_wol.supported)
145455b24334SJustin Chen 		return -EINVAL;
145555b24334SJustin Chen 
14562bddad9eSJustin Chen 	if (wol.wolopts == cur_wol.wolopts &&
14572bddad9eSJustin Chen 	    !memcmp(wol.sopass, cur_wol.sopass, sizeof(wol.sopass)))
145855b24334SJustin Chen 		return 0;
145955b24334SJustin Chen 
14609ce48e5aSMichal Kubecek 	ret = dev->ethtool_ops->set_wol(dev, &wol);
14619ce48e5aSMichal Kubecek 	if (ret)
14629ce48e5aSMichal Kubecek 		return ret;
14639ce48e5aSMichal Kubecek 
14649ce48e5aSMichal Kubecek 	dev->wol_enabled = !!wol.wolopts;
146567bffa79SMichal Kubecek 	ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
14669ce48e5aSMichal Kubecek 
14679ce48e5aSMichal Kubecek 	return 0;
14689ce48e5aSMichal Kubecek }
14699ce48e5aSMichal Kubecek 
14709ce48e5aSMichal Kubecek static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
14719ce48e5aSMichal Kubecek {
14729ce48e5aSMichal Kubecek 	struct ethtool_eee edata;
14739ce48e5aSMichal Kubecek 	int rc;
14749ce48e5aSMichal Kubecek 
14759ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_eee)
14769ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
14779ce48e5aSMichal Kubecek 
14789ce48e5aSMichal Kubecek 	memset(&edata, 0, sizeof(struct ethtool_eee));
14799ce48e5aSMichal Kubecek 	edata.cmd = ETHTOOL_GEEE;
14809ce48e5aSMichal Kubecek 	rc = dev->ethtool_ops->get_eee(dev, &edata);
14819ce48e5aSMichal Kubecek 
14829ce48e5aSMichal Kubecek 	if (rc)
14839ce48e5aSMichal Kubecek 		return rc;
14849ce48e5aSMichal Kubecek 
14859ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
14869ce48e5aSMichal Kubecek 		return -EFAULT;
14879ce48e5aSMichal Kubecek 
14889ce48e5aSMichal Kubecek 	return 0;
14899ce48e5aSMichal Kubecek }
14909ce48e5aSMichal Kubecek 
14919ce48e5aSMichal Kubecek static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
14929ce48e5aSMichal Kubecek {
14939ce48e5aSMichal Kubecek 	struct ethtool_eee edata;
14946c5bc8feSMichal Kubecek 	int ret;
14959ce48e5aSMichal Kubecek 
14969ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_eee)
14979ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
14989ce48e5aSMichal Kubecek 
14999ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
15009ce48e5aSMichal Kubecek 		return -EFAULT;
15019ce48e5aSMichal Kubecek 
15026c5bc8feSMichal Kubecek 	ret = dev->ethtool_ops->set_eee(dev, &edata);
15036c5bc8feSMichal Kubecek 	if (!ret)
15046c5bc8feSMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
15056c5bc8feSMichal Kubecek 	return ret;
15069ce48e5aSMichal Kubecek }
15079ce48e5aSMichal Kubecek 
15089ce48e5aSMichal Kubecek static int ethtool_nway_reset(struct net_device *dev)
15099ce48e5aSMichal Kubecek {
15109ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->nway_reset)
15119ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
15129ce48e5aSMichal Kubecek 
15139ce48e5aSMichal Kubecek 	return dev->ethtool_ops->nway_reset(dev);
15149ce48e5aSMichal Kubecek }
15159ce48e5aSMichal Kubecek 
15169ce48e5aSMichal Kubecek static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
15179ce48e5aSMichal Kubecek {
15189ce48e5aSMichal Kubecek 	struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
15193d2b847fSMichal Kubecek 	int link = __ethtool_get_link(dev);
15209ce48e5aSMichal Kubecek 
15213d2b847fSMichal Kubecek 	if (link < 0)
15223d2b847fSMichal Kubecek 		return link;
15239ce48e5aSMichal Kubecek 
15243d2b847fSMichal Kubecek 	edata.data = link;
15259ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
15269ce48e5aSMichal Kubecek 		return -EFAULT;
15279ce48e5aSMichal Kubecek 	return 0;
15289ce48e5aSMichal Kubecek }
15299ce48e5aSMichal Kubecek 
15309ce48e5aSMichal Kubecek static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
15319ce48e5aSMichal Kubecek 				  int (*getter)(struct net_device *,
15329ce48e5aSMichal Kubecek 						struct ethtool_eeprom *, u8 *),
15339ce48e5aSMichal Kubecek 				  u32 total_len)
15349ce48e5aSMichal Kubecek {
15359ce48e5aSMichal Kubecek 	struct ethtool_eeprom eeprom;
15369ce48e5aSMichal Kubecek 	void __user *userbuf = useraddr + sizeof(eeprom);
15379ce48e5aSMichal Kubecek 	u32 bytes_remaining;
15389ce48e5aSMichal Kubecek 	u8 *data;
15399ce48e5aSMichal Kubecek 	int ret = 0;
15409ce48e5aSMichal Kubecek 
15419ce48e5aSMichal Kubecek 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
15429ce48e5aSMichal Kubecek 		return -EFAULT;
15439ce48e5aSMichal Kubecek 
15449ce48e5aSMichal Kubecek 	/* Check for wrap and zero */
15459ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len <= eeprom.offset)
15469ce48e5aSMichal Kubecek 		return -EINVAL;
15479ce48e5aSMichal Kubecek 
15489ce48e5aSMichal Kubecek 	/* Check for exceeding total eeprom len */
15499ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len > total_len)
15509ce48e5aSMichal Kubecek 		return -EINVAL;
15519ce48e5aSMichal Kubecek 
155280ec82e3SAustin Kim 	data = kzalloc(PAGE_SIZE, GFP_USER);
15539ce48e5aSMichal Kubecek 	if (!data)
15549ce48e5aSMichal Kubecek 		return -ENOMEM;
15559ce48e5aSMichal Kubecek 
15569ce48e5aSMichal Kubecek 	bytes_remaining = eeprom.len;
15579ce48e5aSMichal Kubecek 	while (bytes_remaining > 0) {
15589ce48e5aSMichal Kubecek 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
15599ce48e5aSMichal Kubecek 
15609ce48e5aSMichal Kubecek 		ret = getter(dev, &eeprom, data);
15619ce48e5aSMichal Kubecek 		if (ret)
15629ce48e5aSMichal Kubecek 			break;
1563b9bbc4c1SHeiner Kallweit 		if (!eeprom.len) {
1564b9bbc4c1SHeiner Kallweit 			ret = -EIO;
1565b9bbc4c1SHeiner Kallweit 			break;
1566b9bbc4c1SHeiner Kallweit 		}
15679ce48e5aSMichal Kubecek 		if (copy_to_user(userbuf, data, eeprom.len)) {
15689ce48e5aSMichal Kubecek 			ret = -EFAULT;
15699ce48e5aSMichal Kubecek 			break;
15709ce48e5aSMichal Kubecek 		}
15719ce48e5aSMichal Kubecek 		userbuf += eeprom.len;
15729ce48e5aSMichal Kubecek 		eeprom.offset += eeprom.len;
15739ce48e5aSMichal Kubecek 		bytes_remaining -= eeprom.len;
15749ce48e5aSMichal Kubecek 	}
15759ce48e5aSMichal Kubecek 
15769ce48e5aSMichal Kubecek 	eeprom.len = userbuf - (useraddr + sizeof(eeprom));
15779ce48e5aSMichal Kubecek 	eeprom.offset -= eeprom.len;
15789ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
15799ce48e5aSMichal Kubecek 		ret = -EFAULT;
15809ce48e5aSMichal Kubecek 
15819ce48e5aSMichal Kubecek 	kfree(data);
15829ce48e5aSMichal Kubecek 	return ret;
15839ce48e5aSMichal Kubecek }
15849ce48e5aSMichal Kubecek 
15859ce48e5aSMichal Kubecek static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
15869ce48e5aSMichal Kubecek {
15879ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
15889ce48e5aSMichal Kubecek 
15899ce48e5aSMichal Kubecek 	if (!ops->get_eeprom || !ops->get_eeprom_len ||
15909ce48e5aSMichal Kubecek 	    !ops->get_eeprom_len(dev))
15919ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
15929ce48e5aSMichal Kubecek 
15939ce48e5aSMichal Kubecek 	return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
15949ce48e5aSMichal Kubecek 				      ops->get_eeprom_len(dev));
15959ce48e5aSMichal Kubecek }
15969ce48e5aSMichal Kubecek 
15979ce48e5aSMichal Kubecek static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
15989ce48e5aSMichal Kubecek {
15999ce48e5aSMichal Kubecek 	struct ethtool_eeprom eeprom;
16009ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
16019ce48e5aSMichal Kubecek 	void __user *userbuf = useraddr + sizeof(eeprom);
16029ce48e5aSMichal Kubecek 	u32 bytes_remaining;
16039ce48e5aSMichal Kubecek 	u8 *data;
16049ce48e5aSMichal Kubecek 	int ret = 0;
16059ce48e5aSMichal Kubecek 
16069ce48e5aSMichal Kubecek 	if (!ops->set_eeprom || !ops->get_eeprom_len ||
16079ce48e5aSMichal Kubecek 	    !ops->get_eeprom_len(dev))
16089ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
16099ce48e5aSMichal Kubecek 
16109ce48e5aSMichal Kubecek 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
16119ce48e5aSMichal Kubecek 		return -EFAULT;
16129ce48e5aSMichal Kubecek 
16139ce48e5aSMichal Kubecek 	/* Check for wrap and zero */
16149ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len <= eeprom.offset)
16159ce48e5aSMichal Kubecek 		return -EINVAL;
16169ce48e5aSMichal Kubecek 
16179ce48e5aSMichal Kubecek 	/* Check for exceeding total eeprom len */
16189ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
16199ce48e5aSMichal Kubecek 		return -EINVAL;
16209ce48e5aSMichal Kubecek 
162180ec82e3SAustin Kim 	data = kzalloc(PAGE_SIZE, GFP_USER);
16229ce48e5aSMichal Kubecek 	if (!data)
16239ce48e5aSMichal Kubecek 		return -ENOMEM;
16249ce48e5aSMichal Kubecek 
16259ce48e5aSMichal Kubecek 	bytes_remaining = eeprom.len;
16269ce48e5aSMichal Kubecek 	while (bytes_remaining > 0) {
16279ce48e5aSMichal Kubecek 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
16289ce48e5aSMichal Kubecek 
16299ce48e5aSMichal Kubecek 		if (copy_from_user(data, userbuf, eeprom.len)) {
16309ce48e5aSMichal Kubecek 			ret = -EFAULT;
16319ce48e5aSMichal Kubecek 			break;
16329ce48e5aSMichal Kubecek 		}
16339ce48e5aSMichal Kubecek 		ret = ops->set_eeprom(dev, &eeprom, data);
16349ce48e5aSMichal Kubecek 		if (ret)
16359ce48e5aSMichal Kubecek 			break;
16369ce48e5aSMichal Kubecek 		userbuf += eeprom.len;
16379ce48e5aSMichal Kubecek 		eeprom.offset += eeprom.len;
16389ce48e5aSMichal Kubecek 		bytes_remaining -= eeprom.len;
16399ce48e5aSMichal Kubecek 	}
16409ce48e5aSMichal Kubecek 
16419ce48e5aSMichal Kubecek 	kfree(data);
16429ce48e5aSMichal Kubecek 	return ret;
16439ce48e5aSMichal Kubecek }
16449ce48e5aSMichal Kubecek 
16459ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
16469ce48e5aSMichal Kubecek 						   void __user *useraddr)
16479ce48e5aSMichal Kubecek {
16489ce48e5aSMichal Kubecek 	struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
1649f3ccfda1SYufeng Mo 	struct kernel_ethtool_coalesce kernel_coalesce = {};
165031610711SHeiner Kallweit 	int ret;
16519ce48e5aSMichal Kubecek 
16529ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_coalesce)
16539ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
16549ce48e5aSMichal Kubecek 
1655f3ccfda1SYufeng Mo 	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1656f3ccfda1SYufeng Mo 					     NULL);
165731610711SHeiner Kallweit 	if (ret)
165831610711SHeiner Kallweit 		return ret;
16599ce48e5aSMichal Kubecek 
16609ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
16619ce48e5aSMichal Kubecek 		return -EFAULT;
16629ce48e5aSMichal Kubecek 	return 0;
16639ce48e5aSMichal Kubecek }
16649ce48e5aSMichal Kubecek 
166595cddcb5SJakub Kicinski static bool
166695cddcb5SJakub Kicinski ethtool_set_coalesce_supported(struct net_device *dev,
166795cddcb5SJakub Kicinski 			       struct ethtool_coalesce *coalesce)
166895cddcb5SJakub Kicinski {
166995cddcb5SJakub Kicinski 	u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
167095cddcb5SJakub Kicinski 	u32 nonzero_params = 0;
167195cddcb5SJakub Kicinski 
167295cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs)
167395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
167495cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames)
167595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
167695cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs_irq)
167795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
167895cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames_irq)
167995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
168095cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs)
168195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
168295cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames)
168395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
168495cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs_irq)
168595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
168695cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames_irq)
168795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
168895cddcb5SJakub Kicinski 	if (coalesce->stats_block_coalesce_usecs)
168995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
169095cddcb5SJakub Kicinski 	if (coalesce->use_adaptive_rx_coalesce)
169195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
169295cddcb5SJakub Kicinski 	if (coalesce->use_adaptive_tx_coalesce)
169395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
169495cddcb5SJakub Kicinski 	if (coalesce->pkt_rate_low)
169595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
169695cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs_low)
169795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
169895cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames_low)
169995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
170095cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs_low)
170195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
170295cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames_low)
170395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
170495cddcb5SJakub Kicinski 	if (coalesce->pkt_rate_high)
170595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
170695cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs_high)
170795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
170895cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames_high)
170995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
171095cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs_high)
171195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
171295cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames_high)
171395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
171495cddcb5SJakub Kicinski 	if (coalesce->rate_sample_interval)
171595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
171695cddcb5SJakub Kicinski 
171795cddcb5SJakub Kicinski 	return (supported_params & nonzero_params) == nonzero_params;
171895cddcb5SJakub Kicinski }
171995cddcb5SJakub Kicinski 
17209ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
17219ce48e5aSMichal Kubecek 						   void __user *useraddr)
17229ce48e5aSMichal Kubecek {
1723f3ccfda1SYufeng Mo 	struct kernel_ethtool_coalesce kernel_coalesce = {};
17249ce48e5aSMichal Kubecek 	struct ethtool_coalesce coalesce;
17250cf3eac8SMichal Kubecek 	int ret;
17269ce48e5aSMichal Kubecek 
17270276af21SJulian Wiedmann 	if (!dev->ethtool_ops->set_coalesce || !dev->ethtool_ops->get_coalesce)
17289ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
17299ce48e5aSMichal Kubecek 
1730f3ccfda1SYufeng Mo 	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
1731f3ccfda1SYufeng Mo 					     NULL);
1732f3ccfda1SYufeng Mo 	if (ret)
1733f3ccfda1SYufeng Mo 		return ret;
1734f3ccfda1SYufeng Mo 
17359ce48e5aSMichal Kubecek 	if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
17369ce48e5aSMichal Kubecek 		return -EFAULT;
17379ce48e5aSMichal Kubecek 
173895cddcb5SJakub Kicinski 	if (!ethtool_set_coalesce_supported(dev, &coalesce))
173995cddcb5SJakub Kicinski 		return -EOPNOTSUPP;
174095cddcb5SJakub Kicinski 
1741f3ccfda1SYufeng Mo 	ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
1742f3ccfda1SYufeng Mo 					     NULL);
17430cf3eac8SMichal Kubecek 	if (!ret)
17440cf3eac8SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
17450cf3eac8SMichal Kubecek 	return ret;
17469ce48e5aSMichal Kubecek }
17479ce48e5aSMichal Kubecek 
17489ce48e5aSMichal Kubecek static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
17499ce48e5aSMichal Kubecek {
17509ce48e5aSMichal Kubecek 	struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
175174624944SHao Chen 	struct kernel_ethtool_ringparam kernel_ringparam = {};
17529ce48e5aSMichal Kubecek 
17539ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_ringparam)
17549ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
17559ce48e5aSMichal Kubecek 
175674624944SHao Chen 	dev->ethtool_ops->get_ringparam(dev, &ringparam,
175774624944SHao Chen 					&kernel_ringparam, NULL);
17589ce48e5aSMichal Kubecek 
17599ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
17609ce48e5aSMichal Kubecek 		return -EFAULT;
17619ce48e5aSMichal Kubecek 	return 0;
17629ce48e5aSMichal Kubecek }
17639ce48e5aSMichal Kubecek 
17649ce48e5aSMichal Kubecek static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
17659ce48e5aSMichal Kubecek {
17669ce48e5aSMichal Kubecek 	struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
176774624944SHao Chen 	struct kernel_ethtool_ringparam kernel_ringparam;
1768bc9d1c99SMichal Kubecek 	int ret;
17699ce48e5aSMichal Kubecek 
17709ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
17719ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
17729ce48e5aSMichal Kubecek 
17739ce48e5aSMichal Kubecek 	if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
17749ce48e5aSMichal Kubecek 		return -EFAULT;
17759ce48e5aSMichal Kubecek 
177674624944SHao Chen 	dev->ethtool_ops->get_ringparam(dev, &max, &kernel_ringparam, NULL);
17779ce48e5aSMichal Kubecek 
17789ce48e5aSMichal Kubecek 	/* ensure new ring parameters are within the maximums */
17799ce48e5aSMichal Kubecek 	if (ringparam.rx_pending > max.rx_max_pending ||
17809ce48e5aSMichal Kubecek 	    ringparam.rx_mini_pending > max.rx_mini_max_pending ||
17819ce48e5aSMichal Kubecek 	    ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
17829ce48e5aSMichal Kubecek 	    ringparam.tx_pending > max.tx_max_pending)
17839ce48e5aSMichal Kubecek 		return -EINVAL;
17849ce48e5aSMichal Kubecek 
178574624944SHao Chen 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam,
178674624944SHao Chen 					      &kernel_ringparam, NULL);
1787bc9d1c99SMichal Kubecek 	if (!ret)
1788bc9d1c99SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1789bc9d1c99SMichal Kubecek 	return ret;
17909ce48e5aSMichal Kubecek }
17919ce48e5aSMichal Kubecek 
17929ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
17939ce48e5aSMichal Kubecek 						   void __user *useraddr)
17949ce48e5aSMichal Kubecek {
17959ce48e5aSMichal Kubecek 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
17969ce48e5aSMichal Kubecek 
17979ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_channels)
17989ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
17999ce48e5aSMichal Kubecek 
18009ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_channels(dev, &channels);
18019ce48e5aSMichal Kubecek 
18029ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &channels, sizeof(channels)))
18039ce48e5aSMichal Kubecek 		return -EFAULT;
18049ce48e5aSMichal Kubecek 	return 0;
18059ce48e5aSMichal Kubecek }
18069ce48e5aSMichal Kubecek 
18079ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
18089ce48e5aSMichal Kubecek 						   void __user *useraddr)
18099ce48e5aSMichal Kubecek {
18109ce48e5aSMichal Kubecek 	struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
18119ce48e5aSMichal Kubecek 	u16 from_channel, to_channel;
181247f3ecf4SGal Pressman 	u64 max_rxnfc_in_use;
181347f3ecf4SGal Pressman 	u32 max_rxfh_in_use;
18149ce48e5aSMichal Kubecek 	unsigned int i;
1815546379b9SMichal Kubecek 	int ret;
18169ce48e5aSMichal Kubecek 
18179ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
18189ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
18199ce48e5aSMichal Kubecek 
18209ce48e5aSMichal Kubecek 	if (copy_from_user(&channels, useraddr, sizeof(channels)))
18219ce48e5aSMichal Kubecek 		return -EFAULT;
18229ce48e5aSMichal Kubecek 
18239ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_channels(dev, &curr);
18249ce48e5aSMichal Kubecek 
182575c36dbbSJakub Kicinski 	if (channels.rx_count == curr.rx_count &&
182675c36dbbSJakub Kicinski 	    channels.tx_count == curr.tx_count &&
182775c36dbbSJakub Kicinski 	    channels.combined_count == curr.combined_count &&
182875c36dbbSJakub Kicinski 	    channels.other_count == curr.other_count)
182975c36dbbSJakub Kicinski 		return 0;
183075c36dbbSJakub Kicinski 
18319ce48e5aSMichal Kubecek 	/* ensure new counts are within the maximums */
18329ce48e5aSMichal Kubecek 	if (channels.rx_count > curr.max_rx ||
18339ce48e5aSMichal Kubecek 	    channels.tx_count > curr.max_tx ||
18349ce48e5aSMichal Kubecek 	    channels.combined_count > curr.max_combined ||
18359ce48e5aSMichal Kubecek 	    channels.other_count > curr.max_other)
18369ce48e5aSMichal Kubecek 		return -EINVAL;
18379ce48e5aSMichal Kubecek 
18387be92514SJakub Kicinski 	/* ensure there is at least one RX and one TX channel */
18397be92514SJakub Kicinski 	if (!channels.combined_count &&
18407be92514SJakub Kicinski 	    (!channels.rx_count || !channels.tx_count))
18417be92514SJakub Kicinski 		return -EINVAL;
18427be92514SJakub Kicinski 
18439ce48e5aSMichal Kubecek 	/* ensure the new Rx count fits within the configured Rx flow
184447f3ecf4SGal Pressman 	 * indirection table/rxnfc settings */
184547f3ecf4SGal Pressman 	if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use))
184647f3ecf4SGal Pressman 		max_rxnfc_in_use = 0;
184747f3ecf4SGal Pressman 	if (!netif_is_rxfh_configured(dev) ||
184847f3ecf4SGal Pressman 	    ethtool_get_max_rxfh_channel(dev, &max_rxfh_in_use))
184947f3ecf4SGal Pressman 		max_rxfh_in_use = 0;
185047f3ecf4SGal Pressman 	if (channels.combined_count + channels.rx_count <=
185147f3ecf4SGal Pressman 	    max_t(u64, max_rxnfc_in_use, max_rxfh_in_use))
18529ce48e5aSMichal Kubecek 		return -EINVAL;
18539ce48e5aSMichal Kubecek 
18549ce48e5aSMichal Kubecek 	/* Disabling channels, query zero-copy AF_XDP sockets */
18559ce48e5aSMichal Kubecek 	from_channel = channels.combined_count +
18569ce48e5aSMichal Kubecek 		min(channels.rx_count, channels.tx_count);
18579ce48e5aSMichal Kubecek 	to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
18589ce48e5aSMichal Kubecek 	for (i = from_channel; i < to_channel; i++)
1859c4655761SMagnus Karlsson 		if (xsk_get_pool_from_qid(dev, i))
18609ce48e5aSMichal Kubecek 			return -EINVAL;
18619ce48e5aSMichal Kubecek 
1862546379b9SMichal Kubecek 	ret = dev->ethtool_ops->set_channels(dev, &channels);
1863546379b9SMichal Kubecek 	if (!ret)
1864546379b9SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1865546379b9SMichal Kubecek 	return ret;
18669ce48e5aSMichal Kubecek }
18679ce48e5aSMichal Kubecek 
18689ce48e5aSMichal Kubecek static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
18699ce48e5aSMichal Kubecek {
18709ce48e5aSMichal Kubecek 	struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
18719ce48e5aSMichal Kubecek 
18729ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_pauseparam)
18739ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
18749ce48e5aSMichal Kubecek 
18759ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
18769ce48e5aSMichal Kubecek 
18779ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
18789ce48e5aSMichal Kubecek 		return -EFAULT;
18799ce48e5aSMichal Kubecek 	return 0;
18809ce48e5aSMichal Kubecek }
18819ce48e5aSMichal Kubecek 
18829ce48e5aSMichal Kubecek static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
18839ce48e5aSMichal Kubecek {
18849ce48e5aSMichal Kubecek 	struct ethtool_pauseparam pauseparam;
1885bf37faa3SMichal Kubecek 	int ret;
18869ce48e5aSMichal Kubecek 
18879ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_pauseparam)
18889ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
18899ce48e5aSMichal Kubecek 
18909ce48e5aSMichal Kubecek 	if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
18919ce48e5aSMichal Kubecek 		return -EFAULT;
18929ce48e5aSMichal Kubecek 
1893bf37faa3SMichal Kubecek 	ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1894bf37faa3SMichal Kubecek 	if (!ret)
1895bf37faa3SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1896bf37faa3SMichal Kubecek 	return ret;
18979ce48e5aSMichal Kubecek }
18989ce48e5aSMichal Kubecek 
18999ce48e5aSMichal Kubecek static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
19009ce48e5aSMichal Kubecek {
19019ce48e5aSMichal Kubecek 	struct ethtool_test test;
19029ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
19039ce48e5aSMichal Kubecek 	u64 *data;
19049ce48e5aSMichal Kubecek 	int ret, test_len;
19059ce48e5aSMichal Kubecek 
19069ce48e5aSMichal Kubecek 	if (!ops->self_test || !ops->get_sset_count)
19079ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
19089ce48e5aSMichal Kubecek 
19099ce48e5aSMichal Kubecek 	test_len = ops->get_sset_count(dev, ETH_SS_TEST);
19109ce48e5aSMichal Kubecek 	if (test_len < 0)
19119ce48e5aSMichal Kubecek 		return test_len;
19129ce48e5aSMichal Kubecek 	WARN_ON(test_len == 0);
19139ce48e5aSMichal Kubecek 
19149ce48e5aSMichal Kubecek 	if (copy_from_user(&test, useraddr, sizeof(test)))
19159ce48e5aSMichal Kubecek 		return -EFAULT;
19169ce48e5aSMichal Kubecek 
19179ce48e5aSMichal Kubecek 	test.len = test_len;
191880ec82e3SAustin Kim 	data = kcalloc(test_len, sizeof(u64), GFP_USER);
19199ce48e5aSMichal Kubecek 	if (!data)
19209ce48e5aSMichal Kubecek 		return -ENOMEM;
19219ce48e5aSMichal Kubecek 
192277e9b2abSAndrew Lunn 	netif_testing_on(dev);
19239ce48e5aSMichal Kubecek 	ops->self_test(dev, &test, data);
192477e9b2abSAndrew Lunn 	netif_testing_off(dev);
19259ce48e5aSMichal Kubecek 
19269ce48e5aSMichal Kubecek 	ret = -EFAULT;
19279ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &test, sizeof(test)))
19289ce48e5aSMichal Kubecek 		goto out;
19299ce48e5aSMichal Kubecek 	useraddr += sizeof(test);
1930ed717613SGustavo A. R. Silva 	if (copy_to_user(useraddr, data, array_size(test.len, sizeof(u64))))
19319ce48e5aSMichal Kubecek 		goto out;
19329ce48e5aSMichal Kubecek 	ret = 0;
19339ce48e5aSMichal Kubecek 
19349ce48e5aSMichal Kubecek  out:
19359ce48e5aSMichal Kubecek 	kfree(data);
19369ce48e5aSMichal Kubecek 	return ret;
19379ce48e5aSMichal Kubecek }
19389ce48e5aSMichal Kubecek 
19399ce48e5aSMichal Kubecek static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
19409ce48e5aSMichal Kubecek {
19419ce48e5aSMichal Kubecek 	struct ethtool_gstrings gstrings;
19429ce48e5aSMichal Kubecek 	u8 *data;
19439ce48e5aSMichal Kubecek 	int ret;
19449ce48e5aSMichal Kubecek 
19459ce48e5aSMichal Kubecek 	if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
19469ce48e5aSMichal Kubecek 		return -EFAULT;
19479ce48e5aSMichal Kubecek 
19489ce48e5aSMichal Kubecek 	ret = __ethtool_get_sset_count(dev, gstrings.string_set);
19499ce48e5aSMichal Kubecek 	if (ret < 0)
19509ce48e5aSMichal Kubecek 		return ret;
19519ce48e5aSMichal Kubecek 	if (ret > S32_MAX / ETH_GSTRING_LEN)
19529ce48e5aSMichal Kubecek 		return -ENOMEM;
19539ce48e5aSMichal Kubecek 	WARN_ON_ONCE(!ret);
19549ce48e5aSMichal Kubecek 
19559ce48e5aSMichal Kubecek 	gstrings.len = ret;
19569ce48e5aSMichal Kubecek 
19579ce48e5aSMichal Kubecek 	if (gstrings.len) {
19589ce48e5aSMichal Kubecek 		data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
19599ce48e5aSMichal Kubecek 		if (!data)
19609ce48e5aSMichal Kubecek 			return -ENOMEM;
19619ce48e5aSMichal Kubecek 
19629ce48e5aSMichal Kubecek 		__ethtool_get_strings(dev, gstrings.string_set, data);
19639ce48e5aSMichal Kubecek 	} else {
19649ce48e5aSMichal Kubecek 		data = NULL;
19659ce48e5aSMichal Kubecek 	}
19669ce48e5aSMichal Kubecek 
19679ce48e5aSMichal Kubecek 	ret = -EFAULT;
19689ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
19699ce48e5aSMichal Kubecek 		goto out;
19709ce48e5aSMichal Kubecek 	useraddr += sizeof(gstrings);
19719ce48e5aSMichal Kubecek 	if (gstrings.len &&
1972ed717613SGustavo A. R. Silva 	    copy_to_user(useraddr, data,
1973ed717613SGustavo A. R. Silva 			 array_size(gstrings.len, ETH_GSTRING_LEN)))
19749ce48e5aSMichal Kubecek 		goto out;
19759ce48e5aSMichal Kubecek 	ret = 0;
19769ce48e5aSMichal Kubecek 
19779ce48e5aSMichal Kubecek out:
19789ce48e5aSMichal Kubecek 	vfree(data);
19799ce48e5aSMichal Kubecek 	return ret;
19809ce48e5aSMichal Kubecek }
19819ce48e5aSMichal Kubecek 
19827888fe53SAlexander Duyck __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
19837888fe53SAlexander Duyck {
19847888fe53SAlexander Duyck 	va_list args;
19857888fe53SAlexander Duyck 
19867888fe53SAlexander Duyck 	va_start(args, fmt);
19877888fe53SAlexander Duyck 	vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
19887888fe53SAlexander Duyck 	va_end(args);
19897888fe53SAlexander Duyck 
19907888fe53SAlexander Duyck 	*data += ETH_GSTRING_LEN;
19917888fe53SAlexander Duyck }
19927888fe53SAlexander Duyck EXPORT_SYMBOL(ethtool_sprintf);
19937888fe53SAlexander Duyck 
19949ce48e5aSMichal Kubecek static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
19959ce48e5aSMichal Kubecek {
19969ce48e5aSMichal Kubecek 	struct ethtool_value id;
19979ce48e5aSMichal Kubecek 	static bool busy;
19989ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
19995ae21950SEric Dumazet 	netdevice_tracker dev_tracker;
20009ce48e5aSMichal Kubecek 	int rc;
20019ce48e5aSMichal Kubecek 
20029ce48e5aSMichal Kubecek 	if (!ops->set_phys_id)
20039ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
20049ce48e5aSMichal Kubecek 
20059ce48e5aSMichal Kubecek 	if (busy)
20069ce48e5aSMichal Kubecek 		return -EBUSY;
20079ce48e5aSMichal Kubecek 
20089ce48e5aSMichal Kubecek 	if (copy_from_user(&id, useraddr, sizeof(id)))
20099ce48e5aSMichal Kubecek 		return -EFAULT;
20109ce48e5aSMichal Kubecek 
20119ce48e5aSMichal Kubecek 	rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
20129ce48e5aSMichal Kubecek 	if (rc < 0)
20139ce48e5aSMichal Kubecek 		return rc;
20149ce48e5aSMichal Kubecek 
20159ce48e5aSMichal Kubecek 	/* Drop the RTNL lock while waiting, but prevent reentry or
20169ce48e5aSMichal Kubecek 	 * removal of the device.
20179ce48e5aSMichal Kubecek 	 */
20189ce48e5aSMichal Kubecek 	busy = true;
2019d62607c3SJakub Kicinski 	netdev_hold(dev, &dev_tracker, GFP_KERNEL);
20209ce48e5aSMichal Kubecek 	rtnl_unlock();
20219ce48e5aSMichal Kubecek 
20229ce48e5aSMichal Kubecek 	if (rc == 0) {
20239ce48e5aSMichal Kubecek 		/* Driver will handle this itself */
20249ce48e5aSMichal Kubecek 		schedule_timeout_interruptible(
20259ce48e5aSMichal Kubecek 			id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
20269ce48e5aSMichal Kubecek 	} else {
20279ce48e5aSMichal Kubecek 		/* Driver expects to be called at twice the frequency in rc */
20282adc6edcSEdward Cree 		int n = rc * 2, interval = HZ / n;
202964a8f8f7SMaxim Korotkov 		u64 count = mul_u32_u32(n, id.data);
203064a8f8f7SMaxim Korotkov 		u64 i = 0;
20319ce48e5aSMichal Kubecek 
20329ce48e5aSMichal Kubecek 		do {
20339ce48e5aSMichal Kubecek 			rtnl_lock();
20349ce48e5aSMichal Kubecek 			rc = ops->set_phys_id(dev,
20352adc6edcSEdward Cree 				    (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
20369ce48e5aSMichal Kubecek 			rtnl_unlock();
20379ce48e5aSMichal Kubecek 			if (rc)
20389ce48e5aSMichal Kubecek 				break;
20399ce48e5aSMichal Kubecek 			schedule_timeout_interruptible(interval);
20402adc6edcSEdward Cree 		} while (!signal_pending(current) && (!id.data || i < count));
20419ce48e5aSMichal Kubecek 	}
20429ce48e5aSMichal Kubecek 
20439ce48e5aSMichal Kubecek 	rtnl_lock();
2044d62607c3SJakub Kicinski 	netdev_put(dev, &dev_tracker);
20459ce48e5aSMichal Kubecek 	busy = false;
20469ce48e5aSMichal Kubecek 
20479ce48e5aSMichal Kubecek 	(void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
20489ce48e5aSMichal Kubecek 	return rc;
20499ce48e5aSMichal Kubecek }
20509ce48e5aSMichal Kubecek 
20519ce48e5aSMichal Kubecek static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
20529ce48e5aSMichal Kubecek {
20539ce48e5aSMichal Kubecek 	struct ethtool_stats stats;
20549ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
20559ce48e5aSMichal Kubecek 	u64 *data;
20569ce48e5aSMichal Kubecek 	int ret, n_stats;
20579ce48e5aSMichal Kubecek 
20589ce48e5aSMichal Kubecek 	if (!ops->get_ethtool_stats || !ops->get_sset_count)
20599ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
20609ce48e5aSMichal Kubecek 
20619ce48e5aSMichal Kubecek 	n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
20629ce48e5aSMichal Kubecek 	if (n_stats < 0)
20639ce48e5aSMichal Kubecek 		return n_stats;
20649ce48e5aSMichal Kubecek 	if (n_stats > S32_MAX / sizeof(u64))
20659ce48e5aSMichal Kubecek 		return -ENOMEM;
20669ce48e5aSMichal Kubecek 	WARN_ON_ONCE(!n_stats);
20679ce48e5aSMichal Kubecek 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
20689ce48e5aSMichal Kubecek 		return -EFAULT;
20699ce48e5aSMichal Kubecek 
20709ce48e5aSMichal Kubecek 	stats.n_stats = n_stats;
20719ce48e5aSMichal Kubecek 
20729ce48e5aSMichal Kubecek 	if (n_stats) {
20739ce48e5aSMichal Kubecek 		data = vzalloc(array_size(n_stats, sizeof(u64)));
20749ce48e5aSMichal Kubecek 		if (!data)
20759ce48e5aSMichal Kubecek 			return -ENOMEM;
20769ce48e5aSMichal Kubecek 		ops->get_ethtool_stats(dev, &stats, data);
20779ce48e5aSMichal Kubecek 	} else {
20789ce48e5aSMichal Kubecek 		data = NULL;
20799ce48e5aSMichal Kubecek 	}
20809ce48e5aSMichal Kubecek 
20819ce48e5aSMichal Kubecek 	ret = -EFAULT;
20829ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &stats, sizeof(stats)))
20839ce48e5aSMichal Kubecek 		goto out;
20849ce48e5aSMichal Kubecek 	useraddr += sizeof(stats);
20853dd14996SGustavo A. R. Silva 	if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
20869ce48e5aSMichal Kubecek 		goto out;
20879ce48e5aSMichal Kubecek 	ret = 0;
20889ce48e5aSMichal Kubecek 
20899ce48e5aSMichal Kubecek  out:
20909ce48e5aSMichal Kubecek 	vfree(data);
20919ce48e5aSMichal Kubecek 	return ret;
20929ce48e5aSMichal Kubecek }
20939ce48e5aSMichal Kubecek 
2094201ed315SDaniil Tatianin static int ethtool_vzalloc_stats_array(int n_stats, u64 **data)
20959ce48e5aSMichal Kubecek {
20969ce48e5aSMichal Kubecek 	if (n_stats < 0)
20979ce48e5aSMichal Kubecek 		return n_stats;
20989ce48e5aSMichal Kubecek 	if (n_stats > S32_MAX / sizeof(u64))
20999ce48e5aSMichal Kubecek 		return -ENOMEM;
21009deb1e9fSDaniil Tatianin 	if (WARN_ON_ONCE(!n_stats))
21019deb1e9fSDaniil Tatianin 		return -EOPNOTSUPP;
21029ce48e5aSMichal Kubecek 
2103201ed315SDaniil Tatianin 	*data = vzalloc(array_size(n_stats, sizeof(u64)));
2104201ed315SDaniil Tatianin 	if (!*data)
2105201ed315SDaniil Tatianin 		return -ENOMEM;
2106201ed315SDaniil Tatianin 
2107201ed315SDaniil Tatianin 	return 0;
2108201ed315SDaniil Tatianin }
2109201ed315SDaniil Tatianin 
2110201ed315SDaniil Tatianin static int ethtool_get_phy_stats_phydev(struct phy_device *phydev,
2111201ed315SDaniil Tatianin 					 struct ethtool_stats *stats,
2112201ed315SDaniil Tatianin 					 u64 **data)
2113201ed315SDaniil Tatianin  {
2114201ed315SDaniil Tatianin 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
2115201ed315SDaniil Tatianin 	int n_stats, ret;
2116201ed315SDaniil Tatianin 
2117201ed315SDaniil Tatianin 	if (!phy_ops || !phy_ops->get_sset_count || !phy_ops->get_stats)
2118201ed315SDaniil Tatianin 		return -EOPNOTSUPP;
2119201ed315SDaniil Tatianin 
2120201ed315SDaniil Tatianin 	n_stats = phy_ops->get_sset_count(phydev);
2121201ed315SDaniil Tatianin 
2122201ed315SDaniil Tatianin 	ret = ethtool_vzalloc_stats_array(n_stats, data);
2123201ed315SDaniil Tatianin 	if (ret)
2124201ed315SDaniil Tatianin 		return ret;
2125201ed315SDaniil Tatianin 
2126201ed315SDaniil Tatianin 	stats->n_stats = n_stats;
2127201ed315SDaniil Tatianin 	return phy_ops->get_stats(phydev, stats, *data);
2128201ed315SDaniil Tatianin }
2129201ed315SDaniil Tatianin 
2130201ed315SDaniil Tatianin static int ethtool_get_phy_stats_ethtool(struct net_device *dev,
2131201ed315SDaniil Tatianin 					  struct ethtool_stats *stats,
2132201ed315SDaniil Tatianin 					  u64 **data)
2133201ed315SDaniil Tatianin {
2134201ed315SDaniil Tatianin 	const struct ethtool_ops *ops = dev->ethtool_ops;
2135201ed315SDaniil Tatianin 	int n_stats, ret;
2136201ed315SDaniil Tatianin 
2137201ed315SDaniil Tatianin 	if (!ops || !ops->get_sset_count || ops->get_ethtool_phy_stats)
2138201ed315SDaniil Tatianin 		return -EOPNOTSUPP;
2139201ed315SDaniil Tatianin 
2140201ed315SDaniil Tatianin 	n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
2141201ed315SDaniil Tatianin 
2142201ed315SDaniil Tatianin 	ret = ethtool_vzalloc_stats_array(n_stats, data);
2143201ed315SDaniil Tatianin 	if (ret)
2144201ed315SDaniil Tatianin 		return ret;
2145201ed315SDaniil Tatianin 
2146201ed315SDaniil Tatianin 	stats->n_stats = n_stats;
2147201ed315SDaniil Tatianin 	ops->get_ethtool_phy_stats(dev, stats, *data);
2148201ed315SDaniil Tatianin 
2149201ed315SDaniil Tatianin 	return 0;
2150201ed315SDaniil Tatianin }
2151201ed315SDaniil Tatianin 
2152201ed315SDaniil Tatianin static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
2153201ed315SDaniil Tatianin {
2154201ed315SDaniil Tatianin 	struct phy_device *phydev = dev->phydev;
2155201ed315SDaniil Tatianin 	struct ethtool_stats stats;
2156201ed315SDaniil Tatianin 	u64 *data = NULL;
2157201ed315SDaniil Tatianin 	int ret = -EOPNOTSUPP;
2158201ed315SDaniil Tatianin 
21599ce48e5aSMichal Kubecek 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
21609ce48e5aSMichal Kubecek 		return -EFAULT;
21619ce48e5aSMichal Kubecek 
2162201ed315SDaniil Tatianin 	if (phydev)
2163201ed315SDaniil Tatianin 		ret = ethtool_get_phy_stats_phydev(phydev, &stats, &data);
21649ce48e5aSMichal Kubecek 
2165201ed315SDaniil Tatianin 	if (ret == -EOPNOTSUPP)
2166201ed315SDaniil Tatianin 		ret = ethtool_get_phy_stats_ethtool(dev, &stats, &data);
21679ce48e5aSMichal Kubecek 
2168201ed315SDaniil Tatianin 	if (ret)
21699ce48e5aSMichal Kubecek 		goto out;
2170201ed315SDaniil Tatianin 
2171201ed315SDaniil Tatianin 	if (copy_to_user(useraddr, &stats, sizeof(stats))) {
2172201ed315SDaniil Tatianin 		ret = -EFAULT;
2173201ed315SDaniil Tatianin 		goto out;
21749ce48e5aSMichal Kubecek 	}
21759ce48e5aSMichal Kubecek 
21769ce48e5aSMichal Kubecek 	useraddr += sizeof(stats);
2177201ed315SDaniil Tatianin 	if (copy_to_user(useraddr, data, array_size(stats.n_stats, sizeof(u64))))
2178201ed315SDaniil Tatianin 		ret = -EFAULT;
21799ce48e5aSMichal Kubecek 
21809ce48e5aSMichal Kubecek  out:
21819ce48e5aSMichal Kubecek 	vfree(data);
21829ce48e5aSMichal Kubecek 	return ret;
21839ce48e5aSMichal Kubecek }
21849ce48e5aSMichal Kubecek 
21859ce48e5aSMichal Kubecek static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
21869ce48e5aSMichal Kubecek {
21879ce48e5aSMichal Kubecek 	struct ethtool_perm_addr epaddr;
21889ce48e5aSMichal Kubecek 
21899ce48e5aSMichal Kubecek 	if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
21909ce48e5aSMichal Kubecek 		return -EFAULT;
21919ce48e5aSMichal Kubecek 
21929ce48e5aSMichal Kubecek 	if (epaddr.size < dev->addr_len)
21939ce48e5aSMichal Kubecek 		return -ETOOSMALL;
21949ce48e5aSMichal Kubecek 	epaddr.size = dev->addr_len;
21959ce48e5aSMichal Kubecek 
21969ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
21979ce48e5aSMichal Kubecek 		return -EFAULT;
21989ce48e5aSMichal Kubecek 	useraddr += sizeof(epaddr);
21999ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
22009ce48e5aSMichal Kubecek 		return -EFAULT;
22019ce48e5aSMichal Kubecek 	return 0;
22029ce48e5aSMichal Kubecek }
22039ce48e5aSMichal Kubecek 
22049ce48e5aSMichal Kubecek static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
22059ce48e5aSMichal Kubecek 			     u32 cmd, u32 (*actor)(struct net_device *))
22069ce48e5aSMichal Kubecek {
22079ce48e5aSMichal Kubecek 	struct ethtool_value edata = { .cmd = cmd };
22089ce48e5aSMichal Kubecek 
22099ce48e5aSMichal Kubecek 	if (!actor)
22109ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
22119ce48e5aSMichal Kubecek 
22129ce48e5aSMichal Kubecek 	edata.data = actor(dev);
22139ce48e5aSMichal Kubecek 
22149ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
22159ce48e5aSMichal Kubecek 		return -EFAULT;
22169ce48e5aSMichal Kubecek 	return 0;
22179ce48e5aSMichal Kubecek }
22189ce48e5aSMichal Kubecek 
22199ce48e5aSMichal Kubecek static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
22209ce48e5aSMichal Kubecek 			     void (*actor)(struct net_device *, u32))
22219ce48e5aSMichal Kubecek {
22229ce48e5aSMichal Kubecek 	struct ethtool_value edata;
22239ce48e5aSMichal Kubecek 
22249ce48e5aSMichal Kubecek 	if (!actor)
22259ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
22269ce48e5aSMichal Kubecek 
22279ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
22289ce48e5aSMichal Kubecek 		return -EFAULT;
22299ce48e5aSMichal Kubecek 
22309ce48e5aSMichal Kubecek 	actor(dev, edata.data);
22319ce48e5aSMichal Kubecek 	return 0;
22329ce48e5aSMichal Kubecek }
22339ce48e5aSMichal Kubecek 
22349ce48e5aSMichal Kubecek static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
22359ce48e5aSMichal Kubecek 			     int (*actor)(struct net_device *, u32))
22369ce48e5aSMichal Kubecek {
22379ce48e5aSMichal Kubecek 	struct ethtool_value edata;
22389ce48e5aSMichal Kubecek 
22399ce48e5aSMichal Kubecek 	if (!actor)
22409ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
22419ce48e5aSMichal Kubecek 
22429ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
22439ce48e5aSMichal Kubecek 		return -EFAULT;
22449ce48e5aSMichal Kubecek 
22459ce48e5aSMichal Kubecek 	return actor(dev, edata.data);
22469ce48e5aSMichal Kubecek }
22479ce48e5aSMichal Kubecek 
2248095cfcfeSJakub Kicinski static int
2249095cfcfeSJakub Kicinski ethtool_flash_device(struct net_device *dev, struct ethtool_devlink_compat *req)
22509ce48e5aSMichal Kubecek {
22511af0a094SJakub Kicinski 	if (!dev->ethtool_ops->flash_device) {
22521af0a094SJakub Kicinski 		req->devlink = netdev_to_devlink_get(dev);
22531af0a094SJakub Kicinski 		return 0;
22541af0a094SJakub Kicinski 	}
22559ce48e5aSMichal Kubecek 
2256095cfcfeSJakub Kicinski 	return dev->ethtool_ops->flash_device(dev, &req->efl);
22579ce48e5aSMichal Kubecek }
22589ce48e5aSMichal Kubecek 
22599ce48e5aSMichal Kubecek static int ethtool_set_dump(struct net_device *dev,
22609ce48e5aSMichal Kubecek 			void __user *useraddr)
22619ce48e5aSMichal Kubecek {
22629ce48e5aSMichal Kubecek 	struct ethtool_dump dump;
22639ce48e5aSMichal Kubecek 
22649ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_dump)
22659ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
22669ce48e5aSMichal Kubecek 
22679ce48e5aSMichal Kubecek 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
22689ce48e5aSMichal Kubecek 		return -EFAULT;
22699ce48e5aSMichal Kubecek 
22709ce48e5aSMichal Kubecek 	return dev->ethtool_ops->set_dump(dev, &dump);
22719ce48e5aSMichal Kubecek }
22729ce48e5aSMichal Kubecek 
22739ce48e5aSMichal Kubecek static int ethtool_get_dump_flag(struct net_device *dev,
22749ce48e5aSMichal Kubecek 				void __user *useraddr)
22759ce48e5aSMichal Kubecek {
22769ce48e5aSMichal Kubecek 	int ret;
22779ce48e5aSMichal Kubecek 	struct ethtool_dump dump;
22789ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
22799ce48e5aSMichal Kubecek 
22809ce48e5aSMichal Kubecek 	if (!ops->get_dump_flag)
22819ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
22829ce48e5aSMichal Kubecek 
22839ce48e5aSMichal Kubecek 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
22849ce48e5aSMichal Kubecek 		return -EFAULT;
22859ce48e5aSMichal Kubecek 
22869ce48e5aSMichal Kubecek 	ret = ops->get_dump_flag(dev, &dump);
22879ce48e5aSMichal Kubecek 	if (ret)
22889ce48e5aSMichal Kubecek 		return ret;
22899ce48e5aSMichal Kubecek 
22909ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &dump, sizeof(dump)))
22919ce48e5aSMichal Kubecek 		return -EFAULT;
22929ce48e5aSMichal Kubecek 	return 0;
22939ce48e5aSMichal Kubecek }
22949ce48e5aSMichal Kubecek 
22959ce48e5aSMichal Kubecek static int ethtool_get_dump_data(struct net_device *dev,
22969ce48e5aSMichal Kubecek 				void __user *useraddr)
22979ce48e5aSMichal Kubecek {
22989ce48e5aSMichal Kubecek 	int ret;
22999ce48e5aSMichal Kubecek 	__u32 len;
23009ce48e5aSMichal Kubecek 	struct ethtool_dump dump, tmp;
23019ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
23029ce48e5aSMichal Kubecek 	void *data = NULL;
23039ce48e5aSMichal Kubecek 
23049ce48e5aSMichal Kubecek 	if (!ops->get_dump_data || !ops->get_dump_flag)
23059ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
23069ce48e5aSMichal Kubecek 
23079ce48e5aSMichal Kubecek 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
23089ce48e5aSMichal Kubecek 		return -EFAULT;
23099ce48e5aSMichal Kubecek 
23109ce48e5aSMichal Kubecek 	memset(&tmp, 0, sizeof(tmp));
23119ce48e5aSMichal Kubecek 	tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
23129ce48e5aSMichal Kubecek 	ret = ops->get_dump_flag(dev, &tmp);
23139ce48e5aSMichal Kubecek 	if (ret)
23149ce48e5aSMichal Kubecek 		return ret;
23159ce48e5aSMichal Kubecek 
23169ce48e5aSMichal Kubecek 	len = min(tmp.len, dump.len);
23179ce48e5aSMichal Kubecek 	if (!len)
23189ce48e5aSMichal Kubecek 		return -EFAULT;
23199ce48e5aSMichal Kubecek 
23209ce48e5aSMichal Kubecek 	/* Don't ever let the driver think there's more space available
23219ce48e5aSMichal Kubecek 	 * than it requested with .get_dump_flag().
23229ce48e5aSMichal Kubecek 	 */
23239ce48e5aSMichal Kubecek 	dump.len = len;
23249ce48e5aSMichal Kubecek 
23259ce48e5aSMichal Kubecek 	/* Always allocate enough space to hold the whole thing so that the
23269ce48e5aSMichal Kubecek 	 * driver does not need to check the length and bother with partial
23279ce48e5aSMichal Kubecek 	 * dumping.
23289ce48e5aSMichal Kubecek 	 */
23299ce48e5aSMichal Kubecek 	data = vzalloc(tmp.len);
23309ce48e5aSMichal Kubecek 	if (!data)
23319ce48e5aSMichal Kubecek 		return -ENOMEM;
23329ce48e5aSMichal Kubecek 	ret = ops->get_dump_data(dev, &dump, data);
23339ce48e5aSMichal Kubecek 	if (ret)
23349ce48e5aSMichal Kubecek 		goto out;
23359ce48e5aSMichal Kubecek 
23369ce48e5aSMichal Kubecek 	/* There are two sane possibilities:
23379ce48e5aSMichal Kubecek 	 * 1. The driver's .get_dump_data() does not touch dump.len.
23389ce48e5aSMichal Kubecek 	 * 2. Or it may set dump.len to how much it really writes, which
23399ce48e5aSMichal Kubecek 	 *    should be tmp.len (or len if it can do a partial dump).
23409ce48e5aSMichal Kubecek 	 * In any case respond to userspace with the actual length of data
23419ce48e5aSMichal Kubecek 	 * it's receiving.
23429ce48e5aSMichal Kubecek 	 */
23439ce48e5aSMichal Kubecek 	WARN_ON(dump.len != len && dump.len != tmp.len);
23449ce48e5aSMichal Kubecek 	dump.len = len;
23459ce48e5aSMichal Kubecek 
23469ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &dump, sizeof(dump))) {
23479ce48e5aSMichal Kubecek 		ret = -EFAULT;
23489ce48e5aSMichal Kubecek 		goto out;
23499ce48e5aSMichal Kubecek 	}
23509ce48e5aSMichal Kubecek 	useraddr += offsetof(struct ethtool_dump, data);
23519ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, data, len))
23529ce48e5aSMichal Kubecek 		ret = -EFAULT;
23539ce48e5aSMichal Kubecek out:
23549ce48e5aSMichal Kubecek 	vfree(data);
23559ce48e5aSMichal Kubecek 	return ret;
23569ce48e5aSMichal Kubecek }
23579ce48e5aSMichal Kubecek 
23589ce48e5aSMichal Kubecek static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
23599ce48e5aSMichal Kubecek {
23609ce48e5aSMichal Kubecek 	struct ethtool_ts_info info;
23615b071c59SMichal Kubecek 	int err;
23629ce48e5aSMichal Kubecek 
23635b071c59SMichal Kubecek 	err = __ethtool_get_ts_info(dev, &info);
23649ce48e5aSMichal Kubecek 	if (err)
23659ce48e5aSMichal Kubecek 		return err;
23669ce48e5aSMichal Kubecek 
23679ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &info, sizeof(info)))
23685b071c59SMichal Kubecek 		return -EFAULT;
23699ce48e5aSMichal Kubecek 
23705b071c59SMichal Kubecek 	return 0;
23719ce48e5aSMichal Kubecek }
23729ce48e5aSMichal Kubecek 
237395dfc7efSAndrew Lunn int ethtool_get_module_info_call(struct net_device *dev,
23749ce48e5aSMichal Kubecek 				 struct ethtool_modinfo *modinfo)
23759ce48e5aSMichal Kubecek {
23769ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
23779ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
23789ce48e5aSMichal Kubecek 
23799ce48e5aSMichal Kubecek 	if (dev->sfp_bus)
23809ce48e5aSMichal Kubecek 		return sfp_get_module_info(dev->sfp_bus, modinfo);
23819ce48e5aSMichal Kubecek 
23829ce48e5aSMichal Kubecek 	if (phydev && phydev->drv && phydev->drv->module_info)
23839ce48e5aSMichal Kubecek 		return phydev->drv->module_info(phydev, modinfo);
23849ce48e5aSMichal Kubecek 
23859ce48e5aSMichal Kubecek 	if (ops->get_module_info)
23869ce48e5aSMichal Kubecek 		return ops->get_module_info(dev, modinfo);
23879ce48e5aSMichal Kubecek 
23889ce48e5aSMichal Kubecek 	return -EOPNOTSUPP;
23899ce48e5aSMichal Kubecek }
23909ce48e5aSMichal Kubecek 
23919ce48e5aSMichal Kubecek static int ethtool_get_module_info(struct net_device *dev,
23929ce48e5aSMichal Kubecek 				   void __user *useraddr)
23939ce48e5aSMichal Kubecek {
23949ce48e5aSMichal Kubecek 	int ret;
23959ce48e5aSMichal Kubecek 	struct ethtool_modinfo modinfo;
23969ce48e5aSMichal Kubecek 
23979ce48e5aSMichal Kubecek 	if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
23989ce48e5aSMichal Kubecek 		return -EFAULT;
23999ce48e5aSMichal Kubecek 
240095dfc7efSAndrew Lunn 	ret = ethtool_get_module_info_call(dev, &modinfo);
24019ce48e5aSMichal Kubecek 	if (ret)
24029ce48e5aSMichal Kubecek 		return ret;
24039ce48e5aSMichal Kubecek 
24049ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
24059ce48e5aSMichal Kubecek 		return -EFAULT;
24069ce48e5aSMichal Kubecek 
24079ce48e5aSMichal Kubecek 	return 0;
24089ce48e5aSMichal Kubecek }
24099ce48e5aSMichal Kubecek 
241095dfc7efSAndrew Lunn int ethtool_get_module_eeprom_call(struct net_device *dev,
24119ce48e5aSMichal Kubecek 				   struct ethtool_eeprom *ee, u8 *data)
24129ce48e5aSMichal Kubecek {
24139ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
24149ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
24159ce48e5aSMichal Kubecek 
24169ce48e5aSMichal Kubecek 	if (dev->sfp_bus)
24179ce48e5aSMichal Kubecek 		return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
24189ce48e5aSMichal Kubecek 
24199ce48e5aSMichal Kubecek 	if (phydev && phydev->drv && phydev->drv->module_eeprom)
24209ce48e5aSMichal Kubecek 		return phydev->drv->module_eeprom(phydev, ee, data);
24219ce48e5aSMichal Kubecek 
24229ce48e5aSMichal Kubecek 	if (ops->get_module_eeprom)
24239ce48e5aSMichal Kubecek 		return ops->get_module_eeprom(dev, ee, data);
24249ce48e5aSMichal Kubecek 
24259ce48e5aSMichal Kubecek 	return -EOPNOTSUPP;
24269ce48e5aSMichal Kubecek }
24279ce48e5aSMichal Kubecek 
24289ce48e5aSMichal Kubecek static int ethtool_get_module_eeprom(struct net_device *dev,
24299ce48e5aSMichal Kubecek 				     void __user *useraddr)
24309ce48e5aSMichal Kubecek {
24319ce48e5aSMichal Kubecek 	int ret;
24329ce48e5aSMichal Kubecek 	struct ethtool_modinfo modinfo;
24339ce48e5aSMichal Kubecek 
243495dfc7efSAndrew Lunn 	ret = ethtool_get_module_info_call(dev, &modinfo);
24359ce48e5aSMichal Kubecek 	if (ret)
24369ce48e5aSMichal Kubecek 		return ret;
24379ce48e5aSMichal Kubecek 
24389ce48e5aSMichal Kubecek 	return ethtool_get_any_eeprom(dev, useraddr,
243995dfc7efSAndrew Lunn 				      ethtool_get_module_eeprom_call,
24409ce48e5aSMichal Kubecek 				      modinfo.eeprom_len);
24419ce48e5aSMichal Kubecek }
24429ce48e5aSMichal Kubecek 
24439ce48e5aSMichal Kubecek static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
24449ce48e5aSMichal Kubecek {
24459ce48e5aSMichal Kubecek 	switch (tuna->id) {
24469ce48e5aSMichal Kubecek 	case ETHTOOL_RX_COPYBREAK:
24479ce48e5aSMichal Kubecek 	case ETHTOOL_TX_COPYBREAK:
2448448f413aSHao Chen 	case ETHTOOL_TX_COPYBREAK_BUF_SIZE:
24499ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u32) ||
24509ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U32)
24519ce48e5aSMichal Kubecek 			return -EINVAL;
24529ce48e5aSMichal Kubecek 		break;
24539ce48e5aSMichal Kubecek 	case ETHTOOL_PFC_PREVENTION_TOUT:
24549ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u16) ||
24559ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
24569ce48e5aSMichal Kubecek 			return -EINVAL;
24579ce48e5aSMichal Kubecek 		break;
24589ce48e5aSMichal Kubecek 	default:
24599ce48e5aSMichal Kubecek 		return -EINVAL;
24609ce48e5aSMichal Kubecek 	}
24619ce48e5aSMichal Kubecek 
24629ce48e5aSMichal Kubecek 	return 0;
24639ce48e5aSMichal Kubecek }
24649ce48e5aSMichal Kubecek 
24659ce48e5aSMichal Kubecek static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
24669ce48e5aSMichal Kubecek {
24679ce48e5aSMichal Kubecek 	int ret;
24689ce48e5aSMichal Kubecek 	struct ethtool_tunable tuna;
24699ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
24709ce48e5aSMichal Kubecek 	void *data;
24719ce48e5aSMichal Kubecek 
24729ce48e5aSMichal Kubecek 	if (!ops->get_tunable)
24739ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
24749ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
24759ce48e5aSMichal Kubecek 		return -EFAULT;
24769ce48e5aSMichal Kubecek 	ret = ethtool_tunable_valid(&tuna);
24779ce48e5aSMichal Kubecek 	if (ret)
24789ce48e5aSMichal Kubecek 		return ret;
247980ec82e3SAustin Kim 	data = kzalloc(tuna.len, GFP_USER);
24809ce48e5aSMichal Kubecek 	if (!data)
24819ce48e5aSMichal Kubecek 		return -ENOMEM;
24829ce48e5aSMichal Kubecek 	ret = ops->get_tunable(dev, &tuna, data);
24839ce48e5aSMichal Kubecek 	if (ret)
24849ce48e5aSMichal Kubecek 		goto out;
24859ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
24869ce48e5aSMichal Kubecek 	ret = -EFAULT;
24879ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, data, tuna.len))
24889ce48e5aSMichal Kubecek 		goto out;
24899ce48e5aSMichal Kubecek 	ret = 0;
24909ce48e5aSMichal Kubecek 
24919ce48e5aSMichal Kubecek out:
24929ce48e5aSMichal Kubecek 	kfree(data);
24939ce48e5aSMichal Kubecek 	return ret;
24949ce48e5aSMichal Kubecek }
24959ce48e5aSMichal Kubecek 
24969ce48e5aSMichal Kubecek static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
24979ce48e5aSMichal Kubecek {
24989ce48e5aSMichal Kubecek 	int ret;
24999ce48e5aSMichal Kubecek 	struct ethtool_tunable tuna;
25009ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
25019ce48e5aSMichal Kubecek 	void *data;
25029ce48e5aSMichal Kubecek 
25039ce48e5aSMichal Kubecek 	if (!ops->set_tunable)
25049ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
25059ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
25069ce48e5aSMichal Kubecek 		return -EFAULT;
25079ce48e5aSMichal Kubecek 	ret = ethtool_tunable_valid(&tuna);
25089ce48e5aSMichal Kubecek 	if (ret)
25099ce48e5aSMichal Kubecek 		return ret;
25109ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
25119ce48e5aSMichal Kubecek 	data = memdup_user(useraddr, tuna.len);
25129ce48e5aSMichal Kubecek 	if (IS_ERR(data))
25139ce48e5aSMichal Kubecek 		return PTR_ERR(data);
25149ce48e5aSMichal Kubecek 	ret = ops->set_tunable(dev, &tuna, data);
25159ce48e5aSMichal Kubecek 
25169ce48e5aSMichal Kubecek 	kfree(data);
25179ce48e5aSMichal Kubecek 	return ret;
25189ce48e5aSMichal Kubecek }
25199ce48e5aSMichal Kubecek 
25209ce48e5aSMichal Kubecek static noinline_for_stack int
25219ce48e5aSMichal Kubecek ethtool_get_per_queue_coalesce(struct net_device *dev,
25229ce48e5aSMichal Kubecek 			       void __user *useraddr,
25239ce48e5aSMichal Kubecek 			       struct ethtool_per_queue_op *per_queue_opt)
25249ce48e5aSMichal Kubecek {
25259ce48e5aSMichal Kubecek 	u32 bit;
25269ce48e5aSMichal Kubecek 	int ret;
25279ce48e5aSMichal Kubecek 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
25289ce48e5aSMichal Kubecek 
25299ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_per_queue_coalesce)
25309ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
25319ce48e5aSMichal Kubecek 
25329ce48e5aSMichal Kubecek 	useraddr += sizeof(*per_queue_opt);
25339ce48e5aSMichal Kubecek 
25349ce48e5aSMichal Kubecek 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
25359ce48e5aSMichal Kubecek 			  MAX_NUM_QUEUE);
25369ce48e5aSMichal Kubecek 
25379ce48e5aSMichal Kubecek 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
25389ce48e5aSMichal Kubecek 		struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
25399ce48e5aSMichal Kubecek 
25409ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
25419ce48e5aSMichal Kubecek 		if (ret != 0)
25429ce48e5aSMichal Kubecek 			return ret;
25439ce48e5aSMichal Kubecek 		if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
25449ce48e5aSMichal Kubecek 			return -EFAULT;
25459ce48e5aSMichal Kubecek 		useraddr += sizeof(coalesce);
25469ce48e5aSMichal Kubecek 	}
25479ce48e5aSMichal Kubecek 
25489ce48e5aSMichal Kubecek 	return 0;
25499ce48e5aSMichal Kubecek }
25509ce48e5aSMichal Kubecek 
25519ce48e5aSMichal Kubecek static noinline_for_stack int
25529ce48e5aSMichal Kubecek ethtool_set_per_queue_coalesce(struct net_device *dev,
25539ce48e5aSMichal Kubecek 			       void __user *useraddr,
25549ce48e5aSMichal Kubecek 			       struct ethtool_per_queue_op *per_queue_opt)
25559ce48e5aSMichal Kubecek {
25569ce48e5aSMichal Kubecek 	u32 bit;
25579ce48e5aSMichal Kubecek 	int i, ret = 0;
25589ce48e5aSMichal Kubecek 	int n_queue;
25599ce48e5aSMichal Kubecek 	struct ethtool_coalesce *backup = NULL, *tmp = NULL;
25609ce48e5aSMichal Kubecek 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
25619ce48e5aSMichal Kubecek 
25629ce48e5aSMichal Kubecek 	if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
25639ce48e5aSMichal Kubecek 	    (!dev->ethtool_ops->get_per_queue_coalesce))
25649ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
25659ce48e5aSMichal Kubecek 
25669ce48e5aSMichal Kubecek 	useraddr += sizeof(*per_queue_opt);
25679ce48e5aSMichal Kubecek 
25689ce48e5aSMichal Kubecek 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
25699ce48e5aSMichal Kubecek 	n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
25709ce48e5aSMichal Kubecek 	tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
25719ce48e5aSMichal Kubecek 	if (!backup)
25729ce48e5aSMichal Kubecek 		return -ENOMEM;
25739ce48e5aSMichal Kubecek 
25749ce48e5aSMichal Kubecek 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
25759ce48e5aSMichal Kubecek 		struct ethtool_coalesce coalesce;
25769ce48e5aSMichal Kubecek 
25779ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
25789ce48e5aSMichal Kubecek 		if (ret != 0)
25799ce48e5aSMichal Kubecek 			goto roll_back;
25809ce48e5aSMichal Kubecek 
25819ce48e5aSMichal Kubecek 		tmp++;
25829ce48e5aSMichal Kubecek 
25839ce48e5aSMichal Kubecek 		if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
25849ce48e5aSMichal Kubecek 			ret = -EFAULT;
25859ce48e5aSMichal Kubecek 			goto roll_back;
25869ce48e5aSMichal Kubecek 		}
25879ce48e5aSMichal Kubecek 
258895cddcb5SJakub Kicinski 		if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
258995cddcb5SJakub Kicinski 			ret = -EOPNOTSUPP;
259095cddcb5SJakub Kicinski 			goto roll_back;
259195cddcb5SJakub Kicinski 		}
259295cddcb5SJakub Kicinski 
25939ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
25949ce48e5aSMichal Kubecek 		if (ret != 0)
25959ce48e5aSMichal Kubecek 			goto roll_back;
25969ce48e5aSMichal Kubecek 
25979ce48e5aSMichal Kubecek 		useraddr += sizeof(coalesce);
25989ce48e5aSMichal Kubecek 	}
25999ce48e5aSMichal Kubecek 
26009ce48e5aSMichal Kubecek roll_back:
26019ce48e5aSMichal Kubecek 	if (ret != 0) {
26029ce48e5aSMichal Kubecek 		tmp = backup;
26039ce48e5aSMichal Kubecek 		for_each_set_bit(i, queue_mask, bit) {
26049ce48e5aSMichal Kubecek 			dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
26059ce48e5aSMichal Kubecek 			tmp++;
26069ce48e5aSMichal Kubecek 		}
26079ce48e5aSMichal Kubecek 	}
26089ce48e5aSMichal Kubecek 	kfree(backup);
26099ce48e5aSMichal Kubecek 
26109ce48e5aSMichal Kubecek 	return ret;
26119ce48e5aSMichal Kubecek }
26129ce48e5aSMichal Kubecek 
26139ce48e5aSMichal Kubecek static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
26149ce48e5aSMichal Kubecek 				 void __user *useraddr, u32 sub_cmd)
26159ce48e5aSMichal Kubecek {
26169ce48e5aSMichal Kubecek 	struct ethtool_per_queue_op per_queue_opt;
26179ce48e5aSMichal Kubecek 
26189ce48e5aSMichal Kubecek 	if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
26199ce48e5aSMichal Kubecek 		return -EFAULT;
26209ce48e5aSMichal Kubecek 
26219ce48e5aSMichal Kubecek 	if (per_queue_opt.sub_command != sub_cmd)
26229ce48e5aSMichal Kubecek 		return -EINVAL;
26239ce48e5aSMichal Kubecek 
26249ce48e5aSMichal Kubecek 	switch (per_queue_opt.sub_command) {
26259ce48e5aSMichal Kubecek 	case ETHTOOL_GCOALESCE:
26269ce48e5aSMichal Kubecek 		return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
26279ce48e5aSMichal Kubecek 	case ETHTOOL_SCOALESCE:
26289ce48e5aSMichal Kubecek 		return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
26299ce48e5aSMichal Kubecek 	default:
26309ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
26319d253c02STom Rix 	}
26329ce48e5aSMichal Kubecek }
26339ce48e5aSMichal Kubecek 
26349ce48e5aSMichal Kubecek static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
26359ce48e5aSMichal Kubecek {
26369ce48e5aSMichal Kubecek 	switch (tuna->id) {
26379ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_DOWNSHIFT:
26389ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_FAST_LINK_DOWN:
26399ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u8) ||
26409ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U8)
26419ce48e5aSMichal Kubecek 			return -EINVAL;
26429ce48e5aSMichal Kubecek 		break;
26439ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_EDPD:
26449ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u16) ||
26459ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
26469ce48e5aSMichal Kubecek 			return -EINVAL;
26479ce48e5aSMichal Kubecek 		break;
26489ce48e5aSMichal Kubecek 	default:
26499ce48e5aSMichal Kubecek 		return -EINVAL;
26509ce48e5aSMichal Kubecek 	}
26519ce48e5aSMichal Kubecek 
26529ce48e5aSMichal Kubecek 	return 0;
26539ce48e5aSMichal Kubecek }
26549ce48e5aSMichal Kubecek 
26559ce48e5aSMichal Kubecek static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
26569ce48e5aSMichal Kubecek {
26579ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
2658c6db31ffSIgor Russkikh 	struct ethtool_tunable tuna;
2659c6db31ffSIgor Russkikh 	bool phy_drv_tunable;
26609ce48e5aSMichal Kubecek 	void *data;
2661c6db31ffSIgor Russkikh 	int ret;
26629ce48e5aSMichal Kubecek 
2663c6db31ffSIgor Russkikh 	phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2664c6db31ffSIgor Russkikh 	if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
26659ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
26669ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
26679ce48e5aSMichal Kubecek 		return -EFAULT;
26689ce48e5aSMichal Kubecek 	ret = ethtool_phy_tunable_valid(&tuna);
26699ce48e5aSMichal Kubecek 	if (ret)
26709ce48e5aSMichal Kubecek 		return ret;
267180ec82e3SAustin Kim 	data = kzalloc(tuna.len, GFP_USER);
26729ce48e5aSMichal Kubecek 	if (!data)
26739ce48e5aSMichal Kubecek 		return -ENOMEM;
2674c6db31ffSIgor Russkikh 	if (phy_drv_tunable) {
26759ce48e5aSMichal Kubecek 		mutex_lock(&phydev->lock);
26769ce48e5aSMichal Kubecek 		ret = phydev->drv->get_tunable(phydev, &tuna, data);
26779ce48e5aSMichal Kubecek 		mutex_unlock(&phydev->lock);
2678c6db31ffSIgor Russkikh 	} else {
2679c6db31ffSIgor Russkikh 		ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2680c6db31ffSIgor Russkikh 	}
26819ce48e5aSMichal Kubecek 	if (ret)
26829ce48e5aSMichal Kubecek 		goto out;
26839ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
26849ce48e5aSMichal Kubecek 	ret = -EFAULT;
26859ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, data, tuna.len))
26869ce48e5aSMichal Kubecek 		goto out;
26879ce48e5aSMichal Kubecek 	ret = 0;
26889ce48e5aSMichal Kubecek 
26899ce48e5aSMichal Kubecek out:
26909ce48e5aSMichal Kubecek 	kfree(data);
26919ce48e5aSMichal Kubecek 	return ret;
26929ce48e5aSMichal Kubecek }
26939ce48e5aSMichal Kubecek 
26949ce48e5aSMichal Kubecek static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
26959ce48e5aSMichal Kubecek {
26969ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
2697c6db31ffSIgor Russkikh 	struct ethtool_tunable tuna;
2698c6db31ffSIgor Russkikh 	bool phy_drv_tunable;
26999ce48e5aSMichal Kubecek 	void *data;
2700c6db31ffSIgor Russkikh 	int ret;
27019ce48e5aSMichal Kubecek 
2702c6db31ffSIgor Russkikh 	phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2703c6db31ffSIgor Russkikh 	if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
27049ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
27059ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
27069ce48e5aSMichal Kubecek 		return -EFAULT;
27079ce48e5aSMichal Kubecek 	ret = ethtool_phy_tunable_valid(&tuna);
27089ce48e5aSMichal Kubecek 	if (ret)
27099ce48e5aSMichal Kubecek 		return ret;
27109ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
27119ce48e5aSMichal Kubecek 	data = memdup_user(useraddr, tuna.len);
27129ce48e5aSMichal Kubecek 	if (IS_ERR(data))
27139ce48e5aSMichal Kubecek 		return PTR_ERR(data);
2714c6db31ffSIgor Russkikh 	if (phy_drv_tunable) {
27159ce48e5aSMichal Kubecek 		mutex_lock(&phydev->lock);
27169ce48e5aSMichal Kubecek 		ret = phydev->drv->set_tunable(phydev, &tuna, data);
27179ce48e5aSMichal Kubecek 		mutex_unlock(&phydev->lock);
2718c6db31ffSIgor Russkikh 	} else {
2719c6db31ffSIgor Russkikh 		ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2720c6db31ffSIgor Russkikh 	}
27219ce48e5aSMichal Kubecek 
27229ce48e5aSMichal Kubecek 	kfree(data);
27239ce48e5aSMichal Kubecek 	return ret;
27249ce48e5aSMichal Kubecek }
27259ce48e5aSMichal Kubecek 
27269ce48e5aSMichal Kubecek static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
27279ce48e5aSMichal Kubecek {
27289ce48e5aSMichal Kubecek 	struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
27299ce48e5aSMichal Kubecek 	int rc;
27309ce48e5aSMichal Kubecek 
27319ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_fecparam)
27329ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
27339ce48e5aSMichal Kubecek 
27349ce48e5aSMichal Kubecek 	rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
27359ce48e5aSMichal Kubecek 	if (rc)
27369ce48e5aSMichal Kubecek 		return rc;
27379ce48e5aSMichal Kubecek 
2738240e1144SJakub Kicinski 	if (WARN_ON_ONCE(fecparam.reserved))
2739240e1144SJakub Kicinski 		fecparam.reserved = 0;
2740240e1144SJakub Kicinski 
27419ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
27429ce48e5aSMichal Kubecek 		return -EFAULT;
27439ce48e5aSMichal Kubecek 	return 0;
27449ce48e5aSMichal Kubecek }
27459ce48e5aSMichal Kubecek 
27469ce48e5aSMichal Kubecek static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
27479ce48e5aSMichal Kubecek {
27489ce48e5aSMichal Kubecek 	struct ethtool_fecparam fecparam;
27499ce48e5aSMichal Kubecek 
27509ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_fecparam)
27519ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
27529ce48e5aSMichal Kubecek 
27539ce48e5aSMichal Kubecek 	if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
27549ce48e5aSMichal Kubecek 		return -EFAULT;
27559ce48e5aSMichal Kubecek 
2756cf2cc0bfSJakub Kicinski 	if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
275742ce127dSJakub Kicinski 		return -EINVAL;
275842ce127dSJakub Kicinski 
2759d3b37fc8SJakub Kicinski 	fecparam.active_fec = 0;
2760240e1144SJakub Kicinski 	fecparam.reserved = 0;
2761240e1144SJakub Kicinski 
27629ce48e5aSMichal Kubecek 	return dev->ethtool_ops->set_fecparam(dev, &fecparam);
27639ce48e5aSMichal Kubecek }
27649ce48e5aSMichal Kubecek 
27659ce48e5aSMichal Kubecek /* The main entry point in this file.  Called from net/core/dev_ioctl.c */
27669ce48e5aSMichal Kubecek 
2767f49deaa6SJakub Kicinski static int
2768095cfcfeSJakub Kicinski __dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr,
2769095cfcfeSJakub Kicinski 	      u32 ethcmd, struct ethtool_devlink_compat *devlink_state)
27709ce48e5aSMichal Kubecek {
2771095cfcfeSJakub Kicinski 	struct net_device *dev;
2772095cfcfeSJakub Kicinski 	u32 sub_cmd;
27739ce48e5aSMichal Kubecek 	int rc;
27749ce48e5aSMichal Kubecek 	netdev_features_t old_features;
27759ce48e5aSMichal Kubecek 
2776095cfcfeSJakub Kicinski 	dev = __dev_get_by_name(net, ifr->ifr_name);
2777f32a2137SHeiner Kallweit 	if (!dev)
27789ce48e5aSMichal Kubecek 		return -ENODEV;
27799ce48e5aSMichal Kubecek 
27809ce48e5aSMichal Kubecek 	if (ethcmd == ETHTOOL_PERQUEUE) {
27819ce48e5aSMichal Kubecek 		if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
27829ce48e5aSMichal Kubecek 			return -EFAULT;
27839ce48e5aSMichal Kubecek 	} else {
27849ce48e5aSMichal Kubecek 		sub_cmd = ethcmd;
27859ce48e5aSMichal Kubecek 	}
27869ce48e5aSMichal Kubecek 	/* Allow some commands to be done by anyone */
27879ce48e5aSMichal Kubecek 	switch (sub_cmd) {
27889ce48e5aSMichal Kubecek 	case ETHTOOL_GSET:
27899ce48e5aSMichal Kubecek 	case ETHTOOL_GDRVINFO:
27909ce48e5aSMichal Kubecek 	case ETHTOOL_GMSGLVL:
27919ce48e5aSMichal Kubecek 	case ETHTOOL_GLINK:
27929ce48e5aSMichal Kubecek 	case ETHTOOL_GCOALESCE:
27939ce48e5aSMichal Kubecek 	case ETHTOOL_GRINGPARAM:
27949ce48e5aSMichal Kubecek 	case ETHTOOL_GPAUSEPARAM:
27959ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCSUM:
27969ce48e5aSMichal Kubecek 	case ETHTOOL_GTXCSUM:
27979ce48e5aSMichal Kubecek 	case ETHTOOL_GSG:
27989ce48e5aSMichal Kubecek 	case ETHTOOL_GSSET_INFO:
27999ce48e5aSMichal Kubecek 	case ETHTOOL_GSTRINGS:
28009ce48e5aSMichal Kubecek 	case ETHTOOL_GSTATS:
28019ce48e5aSMichal Kubecek 	case ETHTOOL_GPHYSTATS:
28029ce48e5aSMichal Kubecek 	case ETHTOOL_GTSO:
28039ce48e5aSMichal Kubecek 	case ETHTOOL_GPERMADDR:
28049ce48e5aSMichal Kubecek 	case ETHTOOL_GUFO:
28059ce48e5aSMichal Kubecek 	case ETHTOOL_GGSO:
28069ce48e5aSMichal Kubecek 	case ETHTOOL_GGRO:
28079ce48e5aSMichal Kubecek 	case ETHTOOL_GFLAGS:
28089ce48e5aSMichal Kubecek 	case ETHTOOL_GPFLAGS:
28099ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFH:
28109ce48e5aSMichal Kubecek 	case ETHTOOL_GRXRINGS:
28119ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLCNT:
28129ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRULE:
28139ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLALL:
28149ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFHINDIR:
28159ce48e5aSMichal Kubecek 	case ETHTOOL_GRSSH:
28169ce48e5aSMichal Kubecek 	case ETHTOOL_GFEATURES:
28179ce48e5aSMichal Kubecek 	case ETHTOOL_GCHANNELS:
28189ce48e5aSMichal Kubecek 	case ETHTOOL_GET_TS_INFO:
28199ce48e5aSMichal Kubecek 	case ETHTOOL_GEEE:
28209ce48e5aSMichal Kubecek 	case ETHTOOL_GTUNABLE:
28219ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_GTUNABLE:
28229ce48e5aSMichal Kubecek 	case ETHTOOL_GLINKSETTINGS:
28239ce48e5aSMichal Kubecek 	case ETHTOOL_GFECPARAM:
28249ce48e5aSMichal Kubecek 		break;
28259ce48e5aSMichal Kubecek 	default:
28269ce48e5aSMichal Kubecek 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
28279ce48e5aSMichal Kubecek 			return -EPERM;
28289ce48e5aSMichal Kubecek 	}
28299ce48e5aSMichal Kubecek 
2830f32a2137SHeiner Kallweit 	if (dev->dev.parent)
2831f32a2137SHeiner Kallweit 		pm_runtime_get_sync(dev->dev.parent);
2832f32a2137SHeiner Kallweit 
2833f32a2137SHeiner Kallweit 	if (!netif_device_present(dev)) {
2834f32a2137SHeiner Kallweit 		rc = -ENODEV;
2835f32a2137SHeiner Kallweit 		goto out;
2836f32a2137SHeiner Kallweit 	}
2837f32a2137SHeiner Kallweit 
28389ce48e5aSMichal Kubecek 	if (dev->ethtool_ops->begin) {
28399ce48e5aSMichal Kubecek 		rc = dev->ethtool_ops->begin(dev);
28409ce48e5aSMichal Kubecek 		if (rc < 0)
2841f32a2137SHeiner Kallweit 			goto out;
28429ce48e5aSMichal Kubecek 	}
28439ce48e5aSMichal Kubecek 	old_features = dev->features;
28449ce48e5aSMichal Kubecek 
28459ce48e5aSMichal Kubecek 	switch (ethcmd) {
28469ce48e5aSMichal Kubecek 	case ETHTOOL_GSET:
28479ce48e5aSMichal Kubecek 		rc = ethtool_get_settings(dev, useraddr);
28489ce48e5aSMichal Kubecek 		break;
28499ce48e5aSMichal Kubecek 	case ETHTOOL_SSET:
28509ce48e5aSMichal Kubecek 		rc = ethtool_set_settings(dev, useraddr);
28519ce48e5aSMichal Kubecek 		break;
28529ce48e5aSMichal Kubecek 	case ETHTOOL_GDRVINFO:
2853095cfcfeSJakub Kicinski 		rc = ethtool_get_drvinfo(dev, devlink_state);
28549ce48e5aSMichal Kubecek 		break;
28559ce48e5aSMichal Kubecek 	case ETHTOOL_GREGS:
28569ce48e5aSMichal Kubecek 		rc = ethtool_get_regs(dev, useraddr);
28579ce48e5aSMichal Kubecek 		break;
28589ce48e5aSMichal Kubecek 	case ETHTOOL_GWOL:
28599ce48e5aSMichal Kubecek 		rc = ethtool_get_wol(dev, useraddr);
28609ce48e5aSMichal Kubecek 		break;
28619ce48e5aSMichal Kubecek 	case ETHTOOL_SWOL:
28629ce48e5aSMichal Kubecek 		rc = ethtool_set_wol(dev, useraddr);
28639ce48e5aSMichal Kubecek 		break;
28649ce48e5aSMichal Kubecek 	case ETHTOOL_GMSGLVL:
28659ce48e5aSMichal Kubecek 		rc = ethtool_get_value(dev, useraddr, ethcmd,
28669ce48e5aSMichal Kubecek 				       dev->ethtool_ops->get_msglevel);
28679ce48e5aSMichal Kubecek 		break;
28689ce48e5aSMichal Kubecek 	case ETHTOOL_SMSGLVL:
28699ce48e5aSMichal Kubecek 		rc = ethtool_set_value_void(dev, useraddr,
28709ce48e5aSMichal Kubecek 				       dev->ethtool_ops->set_msglevel);
28710bda7af3SMichal Kubecek 		if (!rc)
28720bda7af3SMichal Kubecek 			ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
28739ce48e5aSMichal Kubecek 		break;
28749ce48e5aSMichal Kubecek 	case ETHTOOL_GEEE:
28759ce48e5aSMichal Kubecek 		rc = ethtool_get_eee(dev, useraddr);
28769ce48e5aSMichal Kubecek 		break;
28779ce48e5aSMichal Kubecek 	case ETHTOOL_SEEE:
28789ce48e5aSMichal Kubecek 		rc = ethtool_set_eee(dev, useraddr);
28799ce48e5aSMichal Kubecek 		break;
28809ce48e5aSMichal Kubecek 	case ETHTOOL_NWAY_RST:
28819ce48e5aSMichal Kubecek 		rc = ethtool_nway_reset(dev);
28829ce48e5aSMichal Kubecek 		break;
28839ce48e5aSMichal Kubecek 	case ETHTOOL_GLINK:
28849ce48e5aSMichal Kubecek 		rc = ethtool_get_link(dev, useraddr);
28859ce48e5aSMichal Kubecek 		break;
28869ce48e5aSMichal Kubecek 	case ETHTOOL_GEEPROM:
28879ce48e5aSMichal Kubecek 		rc = ethtool_get_eeprom(dev, useraddr);
28889ce48e5aSMichal Kubecek 		break;
28899ce48e5aSMichal Kubecek 	case ETHTOOL_SEEPROM:
28909ce48e5aSMichal Kubecek 		rc = ethtool_set_eeprom(dev, useraddr);
28919ce48e5aSMichal Kubecek 		break;
28929ce48e5aSMichal Kubecek 	case ETHTOOL_GCOALESCE:
28939ce48e5aSMichal Kubecek 		rc = ethtool_get_coalesce(dev, useraddr);
28949ce48e5aSMichal Kubecek 		break;
28959ce48e5aSMichal Kubecek 	case ETHTOOL_SCOALESCE:
28969ce48e5aSMichal Kubecek 		rc = ethtool_set_coalesce(dev, useraddr);
28979ce48e5aSMichal Kubecek 		break;
28989ce48e5aSMichal Kubecek 	case ETHTOOL_GRINGPARAM:
28999ce48e5aSMichal Kubecek 		rc = ethtool_get_ringparam(dev, useraddr);
29009ce48e5aSMichal Kubecek 		break;
29019ce48e5aSMichal Kubecek 	case ETHTOOL_SRINGPARAM:
29029ce48e5aSMichal Kubecek 		rc = ethtool_set_ringparam(dev, useraddr);
29039ce48e5aSMichal Kubecek 		break;
29049ce48e5aSMichal Kubecek 	case ETHTOOL_GPAUSEPARAM:
29059ce48e5aSMichal Kubecek 		rc = ethtool_get_pauseparam(dev, useraddr);
29069ce48e5aSMichal Kubecek 		break;
29079ce48e5aSMichal Kubecek 	case ETHTOOL_SPAUSEPARAM:
29089ce48e5aSMichal Kubecek 		rc = ethtool_set_pauseparam(dev, useraddr);
29099ce48e5aSMichal Kubecek 		break;
29109ce48e5aSMichal Kubecek 	case ETHTOOL_TEST:
29119ce48e5aSMichal Kubecek 		rc = ethtool_self_test(dev, useraddr);
29129ce48e5aSMichal Kubecek 		break;
29139ce48e5aSMichal Kubecek 	case ETHTOOL_GSTRINGS:
29149ce48e5aSMichal Kubecek 		rc = ethtool_get_strings(dev, useraddr);
29159ce48e5aSMichal Kubecek 		break;
29169ce48e5aSMichal Kubecek 	case ETHTOOL_PHYS_ID:
29179ce48e5aSMichal Kubecek 		rc = ethtool_phys_id(dev, useraddr);
29189ce48e5aSMichal Kubecek 		break;
29199ce48e5aSMichal Kubecek 	case ETHTOOL_GSTATS:
29209ce48e5aSMichal Kubecek 		rc = ethtool_get_stats(dev, useraddr);
29219ce48e5aSMichal Kubecek 		break;
29229ce48e5aSMichal Kubecek 	case ETHTOOL_GPERMADDR:
29239ce48e5aSMichal Kubecek 		rc = ethtool_get_perm_addr(dev, useraddr);
29249ce48e5aSMichal Kubecek 		break;
29259ce48e5aSMichal Kubecek 	case ETHTOOL_GFLAGS:
29269ce48e5aSMichal Kubecek 		rc = ethtool_get_value(dev, useraddr, ethcmd,
29279ce48e5aSMichal Kubecek 					__ethtool_get_flags);
29289ce48e5aSMichal Kubecek 		break;
29299ce48e5aSMichal Kubecek 	case ETHTOOL_SFLAGS:
29309ce48e5aSMichal Kubecek 		rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
29319ce48e5aSMichal Kubecek 		break;
29329ce48e5aSMichal Kubecek 	case ETHTOOL_GPFLAGS:
29339ce48e5aSMichal Kubecek 		rc = ethtool_get_value(dev, useraddr, ethcmd,
29349ce48e5aSMichal Kubecek 				       dev->ethtool_ops->get_priv_flags);
2935111dcba3SMichal Kubecek 		if (!rc)
2936111dcba3SMichal Kubecek 			ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
29379ce48e5aSMichal Kubecek 		break;
29389ce48e5aSMichal Kubecek 	case ETHTOOL_SPFLAGS:
29399ce48e5aSMichal Kubecek 		rc = ethtool_set_value(dev, useraddr,
29409ce48e5aSMichal Kubecek 				       dev->ethtool_ops->set_priv_flags);
29419ce48e5aSMichal Kubecek 		break;
29429ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFH:
29439ce48e5aSMichal Kubecek 	case ETHTOOL_GRXRINGS:
29449ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLCNT:
29459ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRULE:
29469ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLALL:
29479ce48e5aSMichal Kubecek 		rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
29489ce48e5aSMichal Kubecek 		break;
29499ce48e5aSMichal Kubecek 	case ETHTOOL_SRXFH:
29509ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCLSRLDEL:
29519ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCLSRLINS:
29529ce48e5aSMichal Kubecek 		rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
29539ce48e5aSMichal Kubecek 		break;
29549ce48e5aSMichal Kubecek 	case ETHTOOL_FLASHDEV:
2955095cfcfeSJakub Kicinski 		rc = ethtool_flash_device(dev, devlink_state);
29569ce48e5aSMichal Kubecek 		break;
29579ce48e5aSMichal Kubecek 	case ETHTOOL_RESET:
29589ce48e5aSMichal Kubecek 		rc = ethtool_reset(dev, useraddr);
29599ce48e5aSMichal Kubecek 		break;
29609ce48e5aSMichal Kubecek 	case ETHTOOL_GSSET_INFO:
29619ce48e5aSMichal Kubecek 		rc = ethtool_get_sset_info(dev, useraddr);
29629ce48e5aSMichal Kubecek 		break;
29639ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFHINDIR:
29649ce48e5aSMichal Kubecek 		rc = ethtool_get_rxfh_indir(dev, useraddr);
29659ce48e5aSMichal Kubecek 		break;
29669ce48e5aSMichal Kubecek 	case ETHTOOL_SRXFHINDIR:
29679ce48e5aSMichal Kubecek 		rc = ethtool_set_rxfh_indir(dev, useraddr);
29689ce48e5aSMichal Kubecek 		break;
29699ce48e5aSMichal Kubecek 	case ETHTOOL_GRSSH:
29709ce48e5aSMichal Kubecek 		rc = ethtool_get_rxfh(dev, useraddr);
29719ce48e5aSMichal Kubecek 		break;
29729ce48e5aSMichal Kubecek 	case ETHTOOL_SRSSH:
29739ce48e5aSMichal Kubecek 		rc = ethtool_set_rxfh(dev, useraddr);
29749ce48e5aSMichal Kubecek 		break;
29759ce48e5aSMichal Kubecek 	case ETHTOOL_GFEATURES:
29769ce48e5aSMichal Kubecek 		rc = ethtool_get_features(dev, useraddr);
29779ce48e5aSMichal Kubecek 		break;
29789ce48e5aSMichal Kubecek 	case ETHTOOL_SFEATURES:
29799ce48e5aSMichal Kubecek 		rc = ethtool_set_features(dev, useraddr);
29809ce48e5aSMichal Kubecek 		break;
29819ce48e5aSMichal Kubecek 	case ETHTOOL_GTXCSUM:
29829ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCSUM:
29839ce48e5aSMichal Kubecek 	case ETHTOOL_GSG:
29849ce48e5aSMichal Kubecek 	case ETHTOOL_GTSO:
29859ce48e5aSMichal Kubecek 	case ETHTOOL_GGSO:
29869ce48e5aSMichal Kubecek 	case ETHTOOL_GGRO:
29879ce48e5aSMichal Kubecek 		rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
29889ce48e5aSMichal Kubecek 		break;
29899ce48e5aSMichal Kubecek 	case ETHTOOL_STXCSUM:
29909ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCSUM:
29919ce48e5aSMichal Kubecek 	case ETHTOOL_SSG:
29929ce48e5aSMichal Kubecek 	case ETHTOOL_STSO:
29939ce48e5aSMichal Kubecek 	case ETHTOOL_SGSO:
29949ce48e5aSMichal Kubecek 	case ETHTOOL_SGRO:
29959ce48e5aSMichal Kubecek 		rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
29969ce48e5aSMichal Kubecek 		break;
29979ce48e5aSMichal Kubecek 	case ETHTOOL_GCHANNELS:
29989ce48e5aSMichal Kubecek 		rc = ethtool_get_channels(dev, useraddr);
29999ce48e5aSMichal Kubecek 		break;
30009ce48e5aSMichal Kubecek 	case ETHTOOL_SCHANNELS:
30019ce48e5aSMichal Kubecek 		rc = ethtool_set_channels(dev, useraddr);
30029ce48e5aSMichal Kubecek 		break;
30039ce48e5aSMichal Kubecek 	case ETHTOOL_SET_DUMP:
30049ce48e5aSMichal Kubecek 		rc = ethtool_set_dump(dev, useraddr);
30059ce48e5aSMichal Kubecek 		break;
30069ce48e5aSMichal Kubecek 	case ETHTOOL_GET_DUMP_FLAG:
30079ce48e5aSMichal Kubecek 		rc = ethtool_get_dump_flag(dev, useraddr);
30089ce48e5aSMichal Kubecek 		break;
30099ce48e5aSMichal Kubecek 	case ETHTOOL_GET_DUMP_DATA:
30109ce48e5aSMichal Kubecek 		rc = ethtool_get_dump_data(dev, useraddr);
30119ce48e5aSMichal Kubecek 		break;
30129ce48e5aSMichal Kubecek 	case ETHTOOL_GET_TS_INFO:
30139ce48e5aSMichal Kubecek 		rc = ethtool_get_ts_info(dev, useraddr);
30149ce48e5aSMichal Kubecek 		break;
30159ce48e5aSMichal Kubecek 	case ETHTOOL_GMODULEINFO:
30169ce48e5aSMichal Kubecek 		rc = ethtool_get_module_info(dev, useraddr);
30179ce48e5aSMichal Kubecek 		break;
30189ce48e5aSMichal Kubecek 	case ETHTOOL_GMODULEEEPROM:
30199ce48e5aSMichal Kubecek 		rc = ethtool_get_module_eeprom(dev, useraddr);
30209ce48e5aSMichal Kubecek 		break;
30219ce48e5aSMichal Kubecek 	case ETHTOOL_GTUNABLE:
30229ce48e5aSMichal Kubecek 		rc = ethtool_get_tunable(dev, useraddr);
30239ce48e5aSMichal Kubecek 		break;
30249ce48e5aSMichal Kubecek 	case ETHTOOL_STUNABLE:
30259ce48e5aSMichal Kubecek 		rc = ethtool_set_tunable(dev, useraddr);
30269ce48e5aSMichal Kubecek 		break;
30279ce48e5aSMichal Kubecek 	case ETHTOOL_GPHYSTATS:
30289ce48e5aSMichal Kubecek 		rc = ethtool_get_phy_stats(dev, useraddr);
30299ce48e5aSMichal Kubecek 		break;
30309ce48e5aSMichal Kubecek 	case ETHTOOL_PERQUEUE:
30319ce48e5aSMichal Kubecek 		rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
30329ce48e5aSMichal Kubecek 		break;
30339ce48e5aSMichal Kubecek 	case ETHTOOL_GLINKSETTINGS:
30349ce48e5aSMichal Kubecek 		rc = ethtool_get_link_ksettings(dev, useraddr);
30359ce48e5aSMichal Kubecek 		break;
30369ce48e5aSMichal Kubecek 	case ETHTOOL_SLINKSETTINGS:
30379ce48e5aSMichal Kubecek 		rc = ethtool_set_link_ksettings(dev, useraddr);
30389ce48e5aSMichal Kubecek 		break;
30399ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_GTUNABLE:
30409ce48e5aSMichal Kubecek 		rc = get_phy_tunable(dev, useraddr);
30419ce48e5aSMichal Kubecek 		break;
30429ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_STUNABLE:
30439ce48e5aSMichal Kubecek 		rc = set_phy_tunable(dev, useraddr);
30449ce48e5aSMichal Kubecek 		break;
30459ce48e5aSMichal Kubecek 	case ETHTOOL_GFECPARAM:
30469ce48e5aSMichal Kubecek 		rc = ethtool_get_fecparam(dev, useraddr);
30479ce48e5aSMichal Kubecek 		break;
30489ce48e5aSMichal Kubecek 	case ETHTOOL_SFECPARAM:
30499ce48e5aSMichal Kubecek 		rc = ethtool_set_fecparam(dev, useraddr);
30509ce48e5aSMichal Kubecek 		break;
30519ce48e5aSMichal Kubecek 	default:
30529ce48e5aSMichal Kubecek 		rc = -EOPNOTSUPP;
30539ce48e5aSMichal Kubecek 	}
30549ce48e5aSMichal Kubecek 
30559ce48e5aSMichal Kubecek 	if (dev->ethtool_ops->complete)
30569ce48e5aSMichal Kubecek 		dev->ethtool_ops->complete(dev);
30579ce48e5aSMichal Kubecek 
30589ce48e5aSMichal Kubecek 	if (old_features != dev->features)
30599ce48e5aSMichal Kubecek 		netdev_features_change(dev);
3060f32a2137SHeiner Kallweit out:
3061f32a2137SHeiner Kallweit 	if (dev->dev.parent)
3062f32a2137SHeiner Kallweit 		pm_runtime_put(dev->dev.parent);
30639ce48e5aSMichal Kubecek 
30649ce48e5aSMichal Kubecek 	return rc;
30659ce48e5aSMichal Kubecek }
30669ce48e5aSMichal Kubecek 
3067f49deaa6SJakub Kicinski int dev_ethtool(struct net *net, struct ifreq *ifr, void __user *useraddr)
3068f49deaa6SJakub Kicinski {
3069095cfcfeSJakub Kicinski 	struct ethtool_devlink_compat *state;
3070095cfcfeSJakub Kicinski 	u32 ethcmd;
3071f49deaa6SJakub Kicinski 	int rc;
3072f49deaa6SJakub Kicinski 
3073095cfcfeSJakub Kicinski 	if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
3074095cfcfeSJakub Kicinski 		return -EFAULT;
3075f49deaa6SJakub Kicinski 
3076095cfcfeSJakub Kicinski 	state = kzalloc(sizeof(*state), GFP_KERNEL);
3077095cfcfeSJakub Kicinski 	if (!state)
3078095cfcfeSJakub Kicinski 		return -ENOMEM;
3079095cfcfeSJakub Kicinski 
3080095cfcfeSJakub Kicinski 	switch (ethcmd) {
3081095cfcfeSJakub Kicinski 	case ETHTOOL_FLASHDEV:
3082095cfcfeSJakub Kicinski 		if (copy_from_user(&state->efl, useraddr, sizeof(state->efl))) {
3083095cfcfeSJakub Kicinski 			rc = -EFAULT;
3084095cfcfeSJakub Kicinski 			goto exit_free;
3085095cfcfeSJakub Kicinski 		}
3086095cfcfeSJakub Kicinski 		state->efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
3087095cfcfeSJakub Kicinski 		break;
3088095cfcfeSJakub Kicinski 	}
3089095cfcfeSJakub Kicinski 
3090095cfcfeSJakub Kicinski 	rtnl_lock();
3091095cfcfeSJakub Kicinski 	rc = __dev_ethtool(net, ifr, useraddr, ethcmd, state);
3092095cfcfeSJakub Kicinski 	rtnl_unlock();
3093095cfcfeSJakub Kicinski 	if (rc)
3094095cfcfeSJakub Kicinski 		goto exit_free;
3095095cfcfeSJakub Kicinski 
3096095cfcfeSJakub Kicinski 	switch (ethcmd) {
30971af0a094SJakub Kicinski 	case ETHTOOL_FLASHDEV:
30981af0a094SJakub Kicinski 		if (state->devlink)
30991af0a094SJakub Kicinski 			rc = devlink_compat_flash_update(state->devlink,
31001af0a094SJakub Kicinski 							 state->efl.data);
31011af0a094SJakub Kicinski 		break;
3102095cfcfeSJakub Kicinski 	case ETHTOOL_GDRVINFO:
31031af0a094SJakub Kicinski 		if (state->devlink)
31041af0a094SJakub Kicinski 			devlink_compat_running_version(state->devlink,
31051af0a094SJakub Kicinski 						       state->info.fw_version,
31061af0a094SJakub Kicinski 						       sizeof(state->info.fw_version));
3107095cfcfeSJakub Kicinski 		if (copy_to_user(useraddr, &state->info, sizeof(state->info))) {
3108095cfcfeSJakub Kicinski 			rc = -EFAULT;
3109095cfcfeSJakub Kicinski 			goto exit_free;
3110095cfcfeSJakub Kicinski 		}
3111095cfcfeSJakub Kicinski 		break;
3112095cfcfeSJakub Kicinski 	}
3113095cfcfeSJakub Kicinski 
3114095cfcfeSJakub Kicinski exit_free:
31151af0a094SJakub Kicinski 	if (state->devlink)
31161af0a094SJakub Kicinski 		devlink_put(state->devlink);
3117095cfcfeSJakub Kicinski 	kfree(state);
3118f49deaa6SJakub Kicinski 	return rc;
3119f49deaa6SJakub Kicinski }
3120f49deaa6SJakub Kicinski 
31219ce48e5aSMichal Kubecek struct ethtool_rx_flow_key {
31229ce48e5aSMichal Kubecek 	struct flow_dissector_key_basic			basic;
31239ce48e5aSMichal Kubecek 	union {
31249ce48e5aSMichal Kubecek 		struct flow_dissector_key_ipv4_addrs	ipv4;
31259ce48e5aSMichal Kubecek 		struct flow_dissector_key_ipv6_addrs	ipv6;
31269ce48e5aSMichal Kubecek 	};
31279ce48e5aSMichal Kubecek 	struct flow_dissector_key_ports			tp;
31289ce48e5aSMichal Kubecek 	struct flow_dissector_key_ip			ip;
31299ce48e5aSMichal Kubecek 	struct flow_dissector_key_vlan			vlan;
31309ce48e5aSMichal Kubecek 	struct flow_dissector_key_eth_addrs		eth_addrs;
31319ce48e5aSMichal Kubecek } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
31329ce48e5aSMichal Kubecek 
31339ce48e5aSMichal Kubecek struct ethtool_rx_flow_match {
31349ce48e5aSMichal Kubecek 	struct flow_dissector		dissector;
31359ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_key	key;
31369ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_key	mask;
31379ce48e5aSMichal Kubecek };
31389ce48e5aSMichal Kubecek 
31399ce48e5aSMichal Kubecek struct ethtool_rx_flow_rule *
31409ce48e5aSMichal Kubecek ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
31419ce48e5aSMichal Kubecek {
31429ce48e5aSMichal Kubecek 	const struct ethtool_rx_flow_spec *fs = input->fs;
31439ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_match *match;
31449ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_rule *flow;
31459ce48e5aSMichal Kubecek 	struct flow_action_entry *act;
31469ce48e5aSMichal Kubecek 
31479ce48e5aSMichal Kubecek 	flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
31489ce48e5aSMichal Kubecek 		       sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
31499ce48e5aSMichal Kubecek 	if (!flow)
31509ce48e5aSMichal Kubecek 		return ERR_PTR(-ENOMEM);
31519ce48e5aSMichal Kubecek 
31529ce48e5aSMichal Kubecek 	/* ethtool_rx supports only one single action per rule. */
31539ce48e5aSMichal Kubecek 	flow->rule = flow_rule_alloc(1);
31549ce48e5aSMichal Kubecek 	if (!flow->rule) {
31559ce48e5aSMichal Kubecek 		kfree(flow);
31569ce48e5aSMichal Kubecek 		return ERR_PTR(-ENOMEM);
31579ce48e5aSMichal Kubecek 	}
31589ce48e5aSMichal Kubecek 
31599ce48e5aSMichal Kubecek 	match = (struct ethtool_rx_flow_match *)flow->priv;
31609ce48e5aSMichal Kubecek 	flow->rule->match.dissector	= &match->dissector;
31619ce48e5aSMichal Kubecek 	flow->rule->match.mask		= &match->mask;
31629ce48e5aSMichal Kubecek 	flow->rule->match.key		= &match->key;
31639ce48e5aSMichal Kubecek 
31649ce48e5aSMichal Kubecek 	match->mask.basic.n_proto = htons(0xffff);
31659ce48e5aSMichal Kubecek 
31669ce48e5aSMichal Kubecek 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
31679ce48e5aSMichal Kubecek 	case ETHER_FLOW: {
31689ce48e5aSMichal Kubecek 		const struct ethhdr *ether_spec, *ether_m_spec;
31699ce48e5aSMichal Kubecek 
31709ce48e5aSMichal Kubecek 		ether_spec = &fs->h_u.ether_spec;
31719ce48e5aSMichal Kubecek 		ether_m_spec = &fs->m_u.ether_spec;
31729ce48e5aSMichal Kubecek 
31739ce48e5aSMichal Kubecek 		if (!is_zero_ether_addr(ether_m_spec->h_source)) {
31749ce48e5aSMichal Kubecek 			ether_addr_copy(match->key.eth_addrs.src,
31759ce48e5aSMichal Kubecek 					ether_spec->h_source);
31769ce48e5aSMichal Kubecek 			ether_addr_copy(match->mask.eth_addrs.src,
31779ce48e5aSMichal Kubecek 					ether_m_spec->h_source);
31789ce48e5aSMichal Kubecek 		}
31799ce48e5aSMichal Kubecek 		if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
31809ce48e5aSMichal Kubecek 			ether_addr_copy(match->key.eth_addrs.dst,
31819ce48e5aSMichal Kubecek 					ether_spec->h_dest);
31829ce48e5aSMichal Kubecek 			ether_addr_copy(match->mask.eth_addrs.dst,
31839ce48e5aSMichal Kubecek 					ether_m_spec->h_dest);
31849ce48e5aSMichal Kubecek 		}
31859ce48e5aSMichal Kubecek 		if (ether_m_spec->h_proto) {
31869ce48e5aSMichal Kubecek 			match->key.basic.n_proto = ether_spec->h_proto;
31879ce48e5aSMichal Kubecek 			match->mask.basic.n_proto = ether_m_spec->h_proto;
31889ce48e5aSMichal Kubecek 		}
31899ce48e5aSMichal Kubecek 		}
31909ce48e5aSMichal Kubecek 		break;
31919ce48e5aSMichal Kubecek 	case TCP_V4_FLOW:
31929ce48e5aSMichal Kubecek 	case UDP_V4_FLOW: {
31939ce48e5aSMichal Kubecek 		const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
31949ce48e5aSMichal Kubecek 
31959ce48e5aSMichal Kubecek 		match->key.basic.n_proto = htons(ETH_P_IP);
31969ce48e5aSMichal Kubecek 
31979ce48e5aSMichal Kubecek 		v4_spec = &fs->h_u.tcp_ip4_spec;
31989ce48e5aSMichal Kubecek 		v4_m_spec = &fs->m_u.tcp_ip4_spec;
31999ce48e5aSMichal Kubecek 
32009ce48e5aSMichal Kubecek 		if (v4_m_spec->ip4src) {
32019ce48e5aSMichal Kubecek 			match->key.ipv4.src = v4_spec->ip4src;
32029ce48e5aSMichal Kubecek 			match->mask.ipv4.src = v4_m_spec->ip4src;
32039ce48e5aSMichal Kubecek 		}
32049ce48e5aSMichal Kubecek 		if (v4_m_spec->ip4dst) {
32059ce48e5aSMichal Kubecek 			match->key.ipv4.dst = v4_spec->ip4dst;
32069ce48e5aSMichal Kubecek 			match->mask.ipv4.dst = v4_m_spec->ip4dst;
32079ce48e5aSMichal Kubecek 		}
32089ce48e5aSMichal Kubecek 		if (v4_m_spec->ip4src ||
32099ce48e5aSMichal Kubecek 		    v4_m_spec->ip4dst) {
32109ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
3211*2b3082c6SRatheesh Kannoth 				BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
32129ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
32139ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ipv4);
32149ce48e5aSMichal Kubecek 		}
32159ce48e5aSMichal Kubecek 		if (v4_m_spec->psrc) {
32169ce48e5aSMichal Kubecek 			match->key.tp.src = v4_spec->psrc;
32179ce48e5aSMichal Kubecek 			match->mask.tp.src = v4_m_spec->psrc;
32189ce48e5aSMichal Kubecek 		}
32199ce48e5aSMichal Kubecek 		if (v4_m_spec->pdst) {
32209ce48e5aSMichal Kubecek 			match->key.tp.dst = v4_spec->pdst;
32219ce48e5aSMichal Kubecek 			match->mask.tp.dst = v4_m_spec->pdst;
32229ce48e5aSMichal Kubecek 		}
32239ce48e5aSMichal Kubecek 		if (v4_m_spec->psrc ||
32249ce48e5aSMichal Kubecek 		    v4_m_spec->pdst) {
32259ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
3226*2b3082c6SRatheesh Kannoth 				BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
32279ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
32289ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, tp);
32299ce48e5aSMichal Kubecek 		}
32309ce48e5aSMichal Kubecek 		if (v4_m_spec->tos) {
32319ce48e5aSMichal Kubecek 			match->key.ip.tos = v4_spec->tos;
32329ce48e5aSMichal Kubecek 			match->mask.ip.tos = v4_m_spec->tos;
32339ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
32349ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_IP);
32359ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
32369ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ip);
32379ce48e5aSMichal Kubecek 		}
32389ce48e5aSMichal Kubecek 		}
32399ce48e5aSMichal Kubecek 		break;
32409ce48e5aSMichal Kubecek 	case TCP_V6_FLOW:
32419ce48e5aSMichal Kubecek 	case UDP_V6_FLOW: {
32429ce48e5aSMichal Kubecek 		const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
32439ce48e5aSMichal Kubecek 
32449ce48e5aSMichal Kubecek 		match->key.basic.n_proto = htons(ETH_P_IPV6);
32459ce48e5aSMichal Kubecek 
32469ce48e5aSMichal Kubecek 		v6_spec = &fs->h_u.tcp_ip6_spec;
32479ce48e5aSMichal Kubecek 		v6_m_spec = &fs->m_u.tcp_ip6_spec;
32488cdc3223SKuniyuki Iwashima 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src)) {
32499ce48e5aSMichal Kubecek 			memcpy(&match->key.ipv6.src, v6_spec->ip6src,
32509ce48e5aSMichal Kubecek 			       sizeof(match->key.ipv6.src));
32519ce48e5aSMichal Kubecek 			memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
32529ce48e5aSMichal Kubecek 			       sizeof(match->mask.ipv6.src));
32539ce48e5aSMichal Kubecek 		}
32548cdc3223SKuniyuki Iwashima 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
32559ce48e5aSMichal Kubecek 			memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
32569ce48e5aSMichal Kubecek 			       sizeof(match->key.ipv6.dst));
32579ce48e5aSMichal Kubecek 			memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
32589ce48e5aSMichal Kubecek 			       sizeof(match->mask.ipv6.dst));
32599ce48e5aSMichal Kubecek 		}
32608cdc3223SKuniyuki Iwashima 		if (!ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6src) ||
32618cdc3223SKuniyuki Iwashima 		    !ipv6_addr_any((struct in6_addr *)v6_m_spec->ip6dst)) {
32629ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
3263*2b3082c6SRatheesh Kannoth 				BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
32649ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
32659ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ipv6);
32669ce48e5aSMichal Kubecek 		}
32679ce48e5aSMichal Kubecek 		if (v6_m_spec->psrc) {
32689ce48e5aSMichal Kubecek 			match->key.tp.src = v6_spec->psrc;
32699ce48e5aSMichal Kubecek 			match->mask.tp.src = v6_m_spec->psrc;
32709ce48e5aSMichal Kubecek 		}
32719ce48e5aSMichal Kubecek 		if (v6_m_spec->pdst) {
32729ce48e5aSMichal Kubecek 			match->key.tp.dst = v6_spec->pdst;
32739ce48e5aSMichal Kubecek 			match->mask.tp.dst = v6_m_spec->pdst;
32749ce48e5aSMichal Kubecek 		}
32759ce48e5aSMichal Kubecek 		if (v6_m_spec->psrc ||
32769ce48e5aSMichal Kubecek 		    v6_m_spec->pdst) {
32779ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
3278*2b3082c6SRatheesh Kannoth 				BIT_ULL(FLOW_DISSECTOR_KEY_PORTS);
32799ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
32809ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, tp);
32819ce48e5aSMichal Kubecek 		}
32829ce48e5aSMichal Kubecek 		if (v6_m_spec->tclass) {
32839ce48e5aSMichal Kubecek 			match->key.ip.tos = v6_spec->tclass;
32849ce48e5aSMichal Kubecek 			match->mask.ip.tos = v6_m_spec->tclass;
32859ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
3286*2b3082c6SRatheesh Kannoth 				BIT_ULL(FLOW_DISSECTOR_KEY_IP);
32879ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
32889ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ip);
32899ce48e5aSMichal Kubecek 		}
32909ce48e5aSMichal Kubecek 		}
32919ce48e5aSMichal Kubecek 		break;
32929ce48e5aSMichal Kubecek 	default:
32939ce48e5aSMichal Kubecek 		ethtool_rx_flow_rule_destroy(flow);
32949ce48e5aSMichal Kubecek 		return ERR_PTR(-EINVAL);
32959ce48e5aSMichal Kubecek 	}
32969ce48e5aSMichal Kubecek 
32979ce48e5aSMichal Kubecek 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
32989ce48e5aSMichal Kubecek 	case TCP_V4_FLOW:
32999ce48e5aSMichal Kubecek 	case TCP_V6_FLOW:
33009ce48e5aSMichal Kubecek 		match->key.basic.ip_proto = IPPROTO_TCP;
3301d0a84e1fSVishal Kulkarni 		match->mask.basic.ip_proto = 0xff;
33029ce48e5aSMichal Kubecek 		break;
33039ce48e5aSMichal Kubecek 	case UDP_V4_FLOW:
33049ce48e5aSMichal Kubecek 	case UDP_V6_FLOW:
33059ce48e5aSMichal Kubecek 		match->key.basic.ip_proto = IPPROTO_UDP;
3306d0a84e1fSVishal Kulkarni 		match->mask.basic.ip_proto = 0xff;
33079ce48e5aSMichal Kubecek 		break;
33089ce48e5aSMichal Kubecek 	}
33099ce48e5aSMichal Kubecek 
3310*2b3082c6SRatheesh Kannoth 	match->dissector.used_keys |= BIT_ULL(FLOW_DISSECTOR_KEY_BASIC);
33119ce48e5aSMichal Kubecek 	match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
33129ce48e5aSMichal Kubecek 		offsetof(struct ethtool_rx_flow_key, basic);
33139ce48e5aSMichal Kubecek 
33149ce48e5aSMichal Kubecek 	if (fs->flow_type & FLOW_EXT) {
33159ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
33169ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
33179ce48e5aSMichal Kubecek 
33189ce48e5aSMichal Kubecek 		if (ext_m_spec->vlan_etype) {
33199ce48e5aSMichal Kubecek 			match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
33209ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
33219ce48e5aSMichal Kubecek 		}
33229ce48e5aSMichal Kubecek 
33239ce48e5aSMichal Kubecek 		if (ext_m_spec->vlan_tci) {
33249ce48e5aSMichal Kubecek 			match->key.vlan.vlan_id =
33259ce48e5aSMichal Kubecek 				ntohs(ext_h_spec->vlan_tci) & 0x0fff;
33269ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_id =
33279ce48e5aSMichal Kubecek 				ntohs(ext_m_spec->vlan_tci) & 0x0fff;
33289ce48e5aSMichal Kubecek 
33299ce48e5aSMichal Kubecek 			match->key.vlan.vlan_dei =
33309ce48e5aSMichal Kubecek 				!!(ext_h_spec->vlan_tci & htons(0x1000));
33319ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_dei =
33329ce48e5aSMichal Kubecek 				!!(ext_m_spec->vlan_tci & htons(0x1000));
33339ce48e5aSMichal Kubecek 
33349ce48e5aSMichal Kubecek 			match->key.vlan.vlan_priority =
33359ce48e5aSMichal Kubecek 				(ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
33369ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_priority =
33379ce48e5aSMichal Kubecek 				(ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
33389ce48e5aSMichal Kubecek 		}
33399ce48e5aSMichal Kubecek 
33409ce48e5aSMichal Kubecek 		if (ext_m_spec->vlan_etype ||
33419ce48e5aSMichal Kubecek 		    ext_m_spec->vlan_tci) {
33429ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
3343*2b3082c6SRatheesh Kannoth 				BIT_ULL(FLOW_DISSECTOR_KEY_VLAN);
33449ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
33459ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, vlan);
33469ce48e5aSMichal Kubecek 		}
33479ce48e5aSMichal Kubecek 	}
33489ce48e5aSMichal Kubecek 	if (fs->flow_type & FLOW_MAC_EXT) {
33499ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
33509ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
33519ce48e5aSMichal Kubecek 
33529ce48e5aSMichal Kubecek 		memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
33539ce48e5aSMichal Kubecek 		       ETH_ALEN);
33549ce48e5aSMichal Kubecek 		memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
33559ce48e5aSMichal Kubecek 		       ETH_ALEN);
33569ce48e5aSMichal Kubecek 
33579ce48e5aSMichal Kubecek 		match->dissector.used_keys |=
3358*2b3082c6SRatheesh Kannoth 			BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS);
33599ce48e5aSMichal Kubecek 		match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
33609ce48e5aSMichal Kubecek 			offsetof(struct ethtool_rx_flow_key, eth_addrs);
33619ce48e5aSMichal Kubecek 	}
33629ce48e5aSMichal Kubecek 
33639ce48e5aSMichal Kubecek 	act = &flow->rule->action.entries[0];
33649ce48e5aSMichal Kubecek 	switch (fs->ring_cookie) {
33659ce48e5aSMichal Kubecek 	case RX_CLS_FLOW_DISC:
33669ce48e5aSMichal Kubecek 		act->id = FLOW_ACTION_DROP;
33679ce48e5aSMichal Kubecek 		break;
33689ce48e5aSMichal Kubecek 	case RX_CLS_FLOW_WAKE:
33699ce48e5aSMichal Kubecek 		act->id = FLOW_ACTION_WAKE;
33709ce48e5aSMichal Kubecek 		break;
33719ce48e5aSMichal Kubecek 	default:
33729ce48e5aSMichal Kubecek 		act->id = FLOW_ACTION_QUEUE;
33739ce48e5aSMichal Kubecek 		if (fs->flow_type & FLOW_RSS)
33749ce48e5aSMichal Kubecek 			act->queue.ctx = input->rss_ctx;
33759ce48e5aSMichal Kubecek 
33769ce48e5aSMichal Kubecek 		act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
33779ce48e5aSMichal Kubecek 		act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
33789ce48e5aSMichal Kubecek 		break;
33799ce48e5aSMichal Kubecek 	}
33809ce48e5aSMichal Kubecek 
33819ce48e5aSMichal Kubecek 	return flow;
33829ce48e5aSMichal Kubecek }
33839ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
33849ce48e5aSMichal Kubecek 
33859ce48e5aSMichal Kubecek void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
33869ce48e5aSMichal Kubecek {
33879ce48e5aSMichal Kubecek 	kfree(flow->rule);
33889ce48e5aSMichal Kubecek 	kfree(flow);
33899ce48e5aSMichal Kubecek }
33909ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);
3391