xref: /openbmc/linux/net/ethtool/ioctl.c (revision 80ec82e3)
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 
109ce48e5aSMichal Kubecek #include <linux/module.h>
119ce48e5aSMichal Kubecek #include <linux/types.h>
129ce48e5aSMichal Kubecek #include <linux/capability.h>
139ce48e5aSMichal Kubecek #include <linux/errno.h>
149ce48e5aSMichal Kubecek #include <linux/ethtool.h>
159ce48e5aSMichal Kubecek #include <linux/netdevice.h>
169ce48e5aSMichal Kubecek #include <linux/net_tstamp.h>
179ce48e5aSMichal Kubecek #include <linux/phy.h>
189ce48e5aSMichal Kubecek #include <linux/bitops.h>
199ce48e5aSMichal Kubecek #include <linux/uaccess.h>
209ce48e5aSMichal Kubecek #include <linux/vmalloc.h>
219ce48e5aSMichal Kubecek #include <linux/sfp.h>
229ce48e5aSMichal Kubecek #include <linux/slab.h>
239ce48e5aSMichal Kubecek #include <linux/rtnetlink.h>
249ce48e5aSMichal Kubecek #include <linux/sched/signal.h>
259ce48e5aSMichal Kubecek #include <linux/net.h>
269ce48e5aSMichal Kubecek #include <net/devlink.h>
27a71506a4SMagnus Karlsson #include <net/xdp_sock_drv.h>
289ce48e5aSMichal Kubecek #include <net/flow_offload.h>
2973286734SMichal Kubecek #include <linux/ethtool_netlink.h>
301c79031fSLeon Romanovsky #include <generated/utsrelease.h>
31d44e1310SMichal Kubecek #include "common.h"
32d44e1310SMichal Kubecek 
339ce48e5aSMichal Kubecek /*
349ce48e5aSMichal Kubecek  * Some useful ethtool_ops methods that're device independent.
359ce48e5aSMichal Kubecek  * If we find that all drivers want to do the same thing here,
369ce48e5aSMichal Kubecek  * we can turn these into dev_() function calls.
379ce48e5aSMichal Kubecek  */
389ce48e5aSMichal Kubecek 
399ce48e5aSMichal Kubecek u32 ethtool_op_get_link(struct net_device *dev)
409ce48e5aSMichal Kubecek {
419ce48e5aSMichal Kubecek 	return netif_carrier_ok(dev) ? 1 : 0;
429ce48e5aSMichal Kubecek }
439ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_op_get_link);
449ce48e5aSMichal Kubecek 
459ce48e5aSMichal Kubecek int ethtool_op_get_ts_info(struct net_device *dev, struct ethtool_ts_info *info)
469ce48e5aSMichal Kubecek {
479ce48e5aSMichal Kubecek 	info->so_timestamping =
489ce48e5aSMichal Kubecek 		SOF_TIMESTAMPING_TX_SOFTWARE |
499ce48e5aSMichal Kubecek 		SOF_TIMESTAMPING_RX_SOFTWARE |
509ce48e5aSMichal Kubecek 		SOF_TIMESTAMPING_SOFTWARE;
519ce48e5aSMichal Kubecek 	info->phc_index = -1;
529ce48e5aSMichal Kubecek 	return 0;
539ce48e5aSMichal Kubecek }
549ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_op_get_ts_info);
559ce48e5aSMichal Kubecek 
569ce48e5aSMichal Kubecek /* Handlers for each ethtool command */
579ce48e5aSMichal Kubecek 
589ce48e5aSMichal Kubecek static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
599ce48e5aSMichal Kubecek {
609ce48e5aSMichal Kubecek 	struct ethtool_gfeatures cmd = {
619ce48e5aSMichal Kubecek 		.cmd = ETHTOOL_GFEATURES,
629ce48e5aSMichal Kubecek 		.size = ETHTOOL_DEV_FEATURE_WORDS,
639ce48e5aSMichal Kubecek 	};
649ce48e5aSMichal Kubecek 	struct ethtool_get_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
659ce48e5aSMichal Kubecek 	u32 __user *sizeaddr;
669ce48e5aSMichal Kubecek 	u32 copy_size;
679ce48e5aSMichal Kubecek 	int i;
689ce48e5aSMichal Kubecek 
699ce48e5aSMichal Kubecek 	/* in case feature bits run out again */
709ce48e5aSMichal Kubecek 	BUILD_BUG_ON(ETHTOOL_DEV_FEATURE_WORDS * sizeof(u32) > sizeof(netdev_features_t));
719ce48e5aSMichal Kubecek 
729ce48e5aSMichal Kubecek 	for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
739ce48e5aSMichal Kubecek 		features[i].available = (u32)(dev->hw_features >> (32 * i));
749ce48e5aSMichal Kubecek 		features[i].requested = (u32)(dev->wanted_features >> (32 * i));
759ce48e5aSMichal Kubecek 		features[i].active = (u32)(dev->features >> (32 * i));
769ce48e5aSMichal Kubecek 		features[i].never_changed =
779ce48e5aSMichal Kubecek 			(u32)(NETIF_F_NEVER_CHANGE >> (32 * i));
789ce48e5aSMichal Kubecek 	}
799ce48e5aSMichal Kubecek 
809ce48e5aSMichal Kubecek 	sizeaddr = useraddr + offsetof(struct ethtool_gfeatures, size);
819ce48e5aSMichal Kubecek 	if (get_user(copy_size, sizeaddr))
829ce48e5aSMichal Kubecek 		return -EFAULT;
839ce48e5aSMichal Kubecek 
849ce48e5aSMichal Kubecek 	if (copy_size > ETHTOOL_DEV_FEATURE_WORDS)
859ce48e5aSMichal Kubecek 		copy_size = ETHTOOL_DEV_FEATURE_WORDS;
869ce48e5aSMichal Kubecek 
879ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
889ce48e5aSMichal Kubecek 		return -EFAULT;
899ce48e5aSMichal Kubecek 	useraddr += sizeof(cmd);
909ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, features, copy_size * sizeof(*features)))
919ce48e5aSMichal Kubecek 		return -EFAULT;
929ce48e5aSMichal Kubecek 
939ce48e5aSMichal Kubecek 	return 0;
949ce48e5aSMichal Kubecek }
959ce48e5aSMichal Kubecek 
969ce48e5aSMichal Kubecek static int ethtool_set_features(struct net_device *dev, void __user *useraddr)
979ce48e5aSMichal Kubecek {
989ce48e5aSMichal Kubecek 	struct ethtool_sfeatures cmd;
999ce48e5aSMichal Kubecek 	struct ethtool_set_features_block features[ETHTOOL_DEV_FEATURE_WORDS];
1009ce48e5aSMichal Kubecek 	netdev_features_t wanted = 0, valid = 0;
1019ce48e5aSMichal Kubecek 	int i, ret = 0;
1029ce48e5aSMichal Kubecek 
1039ce48e5aSMichal Kubecek 	if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
1049ce48e5aSMichal Kubecek 		return -EFAULT;
1059ce48e5aSMichal Kubecek 	useraddr += sizeof(cmd);
1069ce48e5aSMichal Kubecek 
1079ce48e5aSMichal Kubecek 	if (cmd.size != ETHTOOL_DEV_FEATURE_WORDS)
1089ce48e5aSMichal Kubecek 		return -EINVAL;
1099ce48e5aSMichal Kubecek 
1109ce48e5aSMichal Kubecek 	if (copy_from_user(features, useraddr, sizeof(features)))
1119ce48e5aSMichal Kubecek 		return -EFAULT;
1129ce48e5aSMichal Kubecek 
1139ce48e5aSMichal Kubecek 	for (i = 0; i < ETHTOOL_DEV_FEATURE_WORDS; ++i) {
1149ce48e5aSMichal Kubecek 		valid |= (netdev_features_t)features[i].valid << (32 * i);
1159ce48e5aSMichal Kubecek 		wanted |= (netdev_features_t)features[i].requested << (32 * i);
1169ce48e5aSMichal Kubecek 	}
1179ce48e5aSMichal Kubecek 
1189ce48e5aSMichal Kubecek 	if (valid & ~NETIF_F_ETHTOOL_BITS)
1199ce48e5aSMichal Kubecek 		return -EINVAL;
1209ce48e5aSMichal Kubecek 
1219ce48e5aSMichal Kubecek 	if (valid & ~dev->hw_features) {
1229ce48e5aSMichal Kubecek 		valid &= dev->hw_features;
1239ce48e5aSMichal Kubecek 		ret |= ETHTOOL_F_UNSUPPORTED;
1249ce48e5aSMichal Kubecek 	}
1259ce48e5aSMichal Kubecek 
1269ce48e5aSMichal Kubecek 	dev->wanted_features &= ~valid;
1279ce48e5aSMichal Kubecek 	dev->wanted_features |= wanted & valid;
1289ce48e5aSMichal Kubecek 	__netdev_update_features(dev);
1299ce48e5aSMichal Kubecek 
1309ce48e5aSMichal Kubecek 	if ((dev->wanted_features ^ dev->features) & valid)
1319ce48e5aSMichal Kubecek 		ret |= ETHTOOL_F_WISH;
1329ce48e5aSMichal Kubecek 
1339ce48e5aSMichal Kubecek 	return ret;
1349ce48e5aSMichal Kubecek }
1359ce48e5aSMichal Kubecek 
1369ce48e5aSMichal Kubecek static int __ethtool_get_sset_count(struct net_device *dev, int sset)
1379ce48e5aSMichal Kubecek {
13817809516SFlorian Fainelli 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
1399ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
1409ce48e5aSMichal Kubecek 
1419ce48e5aSMichal Kubecek 	if (sset == ETH_SS_FEATURES)
1429ce48e5aSMichal Kubecek 		return ARRAY_SIZE(netdev_features_strings);
1439ce48e5aSMichal Kubecek 
1449ce48e5aSMichal Kubecek 	if (sset == ETH_SS_RSS_HASH_FUNCS)
1459ce48e5aSMichal Kubecek 		return ARRAY_SIZE(rss_hash_func_strings);
1469ce48e5aSMichal Kubecek 
1479ce48e5aSMichal Kubecek 	if (sset == ETH_SS_TUNABLES)
1489ce48e5aSMichal Kubecek 		return ARRAY_SIZE(tunable_strings);
1499ce48e5aSMichal Kubecek 
1509ce48e5aSMichal Kubecek 	if (sset == ETH_SS_PHY_TUNABLES)
1519ce48e5aSMichal Kubecek 		return ARRAY_SIZE(phy_tunable_strings);
1529ce48e5aSMichal Kubecek 
1539ce48e5aSMichal Kubecek 	if (sset == ETH_SS_PHY_STATS && dev->phydev &&
15417809516SFlorian Fainelli 	    !ops->get_ethtool_phy_stats &&
15517809516SFlorian Fainelli 	    phy_ops && phy_ops->get_sset_count)
15617809516SFlorian Fainelli 		return phy_ops->get_sset_count(dev->phydev);
1579ce48e5aSMichal Kubecek 
158428c122fSMichal Kubecek 	if (sset == ETH_SS_LINK_MODES)
159428c122fSMichal Kubecek 		return __ETHTOOL_LINK_MODE_MASK_NBITS;
160428c122fSMichal Kubecek 
1619ce48e5aSMichal Kubecek 	if (ops->get_sset_count && ops->get_strings)
1629ce48e5aSMichal Kubecek 		return ops->get_sset_count(dev, sset);
1639ce48e5aSMichal Kubecek 	else
1649ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
1659ce48e5aSMichal Kubecek }
1669ce48e5aSMichal Kubecek 
1679ce48e5aSMichal Kubecek static void __ethtool_get_strings(struct net_device *dev,
1689ce48e5aSMichal Kubecek 	u32 stringset, u8 *data)
1699ce48e5aSMichal Kubecek {
17017809516SFlorian Fainelli 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
1719ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
1729ce48e5aSMichal Kubecek 
1739ce48e5aSMichal Kubecek 	if (stringset == ETH_SS_FEATURES)
1749ce48e5aSMichal Kubecek 		memcpy(data, netdev_features_strings,
1759ce48e5aSMichal Kubecek 			sizeof(netdev_features_strings));
1769ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_RSS_HASH_FUNCS)
1779ce48e5aSMichal Kubecek 		memcpy(data, rss_hash_func_strings,
1789ce48e5aSMichal Kubecek 		       sizeof(rss_hash_func_strings));
1799ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_TUNABLES)
1809ce48e5aSMichal Kubecek 		memcpy(data, tunable_strings, sizeof(tunable_strings));
1819ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_PHY_TUNABLES)
1829ce48e5aSMichal Kubecek 		memcpy(data, phy_tunable_strings, sizeof(phy_tunable_strings));
1839ce48e5aSMichal Kubecek 	else if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
18417809516SFlorian Fainelli 		 !ops->get_ethtool_phy_stats && phy_ops &&
18517809516SFlorian Fainelli 		 phy_ops->get_strings)
18617809516SFlorian Fainelli 		phy_ops->get_strings(dev->phydev, data);
187428c122fSMichal Kubecek 	else if (stringset == ETH_SS_LINK_MODES)
188428c122fSMichal Kubecek 		memcpy(data, link_mode_names,
189428c122fSMichal Kubecek 		       __ETHTOOL_LINK_MODE_MASK_NBITS * ETH_GSTRING_LEN);
1909ce48e5aSMichal Kubecek 	else
1919ce48e5aSMichal Kubecek 		/* ops->get_strings is valid because checked earlier */
1929ce48e5aSMichal Kubecek 		ops->get_strings(dev, stringset, data);
1939ce48e5aSMichal Kubecek }
1949ce48e5aSMichal Kubecek 
1959ce48e5aSMichal Kubecek static netdev_features_t ethtool_get_feature_mask(u32 eth_cmd)
1969ce48e5aSMichal Kubecek {
1979ce48e5aSMichal Kubecek 	/* feature masks of legacy discrete ethtool ops */
1989ce48e5aSMichal Kubecek 
1999ce48e5aSMichal Kubecek 	switch (eth_cmd) {
2009ce48e5aSMichal Kubecek 	case ETHTOOL_GTXCSUM:
2019ce48e5aSMichal Kubecek 	case ETHTOOL_STXCSUM:
2029d648fb5SVladyslav Tarasiuk 		return NETIF_F_CSUM_MASK | NETIF_F_FCOE_CRC |
203f70bb065SMichal Kubecek 		       NETIF_F_SCTP_CRC;
2049ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCSUM:
2059ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCSUM:
2069ce48e5aSMichal Kubecek 		return NETIF_F_RXCSUM;
2079ce48e5aSMichal Kubecek 	case ETHTOOL_GSG:
2089ce48e5aSMichal Kubecek 	case ETHTOOL_SSG:
209f70bb065SMichal Kubecek 		return NETIF_F_SG | NETIF_F_FRAGLIST;
2109ce48e5aSMichal Kubecek 	case ETHTOOL_GTSO:
2119ce48e5aSMichal Kubecek 	case ETHTOOL_STSO:
2129ce48e5aSMichal Kubecek 		return NETIF_F_ALL_TSO;
2139ce48e5aSMichal Kubecek 	case ETHTOOL_GGSO:
2149ce48e5aSMichal Kubecek 	case ETHTOOL_SGSO:
2159ce48e5aSMichal Kubecek 		return NETIF_F_GSO;
2169ce48e5aSMichal Kubecek 	case ETHTOOL_GGRO:
2179ce48e5aSMichal Kubecek 	case ETHTOOL_SGRO:
2189ce48e5aSMichal Kubecek 		return NETIF_F_GRO;
2199ce48e5aSMichal Kubecek 	default:
2209ce48e5aSMichal Kubecek 		BUG();
2219ce48e5aSMichal Kubecek 	}
2229ce48e5aSMichal Kubecek }
2239ce48e5aSMichal Kubecek 
2249ce48e5aSMichal Kubecek static int ethtool_get_one_feature(struct net_device *dev,
2259ce48e5aSMichal Kubecek 	char __user *useraddr, u32 ethcmd)
2269ce48e5aSMichal Kubecek {
2279ce48e5aSMichal Kubecek 	netdev_features_t mask = ethtool_get_feature_mask(ethcmd);
2289ce48e5aSMichal Kubecek 	struct ethtool_value edata = {
2299ce48e5aSMichal Kubecek 		.cmd = ethcmd,
2309ce48e5aSMichal Kubecek 		.data = !!(dev->features & mask),
2319ce48e5aSMichal Kubecek 	};
2329ce48e5aSMichal Kubecek 
2339ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
2349ce48e5aSMichal Kubecek 		return -EFAULT;
2359ce48e5aSMichal Kubecek 	return 0;
2369ce48e5aSMichal Kubecek }
2379ce48e5aSMichal Kubecek 
2389ce48e5aSMichal Kubecek static int ethtool_set_one_feature(struct net_device *dev,
2399ce48e5aSMichal Kubecek 	void __user *useraddr, u32 ethcmd)
2409ce48e5aSMichal Kubecek {
2419ce48e5aSMichal Kubecek 	struct ethtool_value edata;
2429ce48e5aSMichal Kubecek 	netdev_features_t mask;
2439ce48e5aSMichal Kubecek 
2449ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
2459ce48e5aSMichal Kubecek 		return -EFAULT;
2469ce48e5aSMichal Kubecek 
2479ce48e5aSMichal Kubecek 	mask = ethtool_get_feature_mask(ethcmd);
2489ce48e5aSMichal Kubecek 	mask &= dev->hw_features;
2499ce48e5aSMichal Kubecek 	if (!mask)
2509ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
2519ce48e5aSMichal Kubecek 
2529ce48e5aSMichal Kubecek 	if (edata.data)
2539ce48e5aSMichal Kubecek 		dev->wanted_features |= mask;
2549ce48e5aSMichal Kubecek 	else
2559ce48e5aSMichal Kubecek 		dev->wanted_features &= ~mask;
2569ce48e5aSMichal Kubecek 
2579ce48e5aSMichal Kubecek 	__netdev_update_features(dev);
2589ce48e5aSMichal Kubecek 
2599ce48e5aSMichal Kubecek 	return 0;
2609ce48e5aSMichal Kubecek }
2619ce48e5aSMichal Kubecek 
2629ce48e5aSMichal Kubecek #define ETH_ALL_FLAGS    (ETH_FLAG_LRO | ETH_FLAG_RXVLAN | ETH_FLAG_TXVLAN | \
2639ce48e5aSMichal Kubecek 			  ETH_FLAG_NTUPLE | ETH_FLAG_RXHASH)
2649ce48e5aSMichal Kubecek #define ETH_ALL_FEATURES (NETIF_F_LRO | NETIF_F_HW_VLAN_CTAG_RX | \
2659ce48e5aSMichal Kubecek 			  NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_NTUPLE | \
2669ce48e5aSMichal Kubecek 			  NETIF_F_RXHASH)
2679ce48e5aSMichal Kubecek 
2689ce48e5aSMichal Kubecek static u32 __ethtool_get_flags(struct net_device *dev)
2699ce48e5aSMichal Kubecek {
2709ce48e5aSMichal Kubecek 	u32 flags = 0;
2719ce48e5aSMichal Kubecek 
2729ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_LRO)
2739ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_LRO;
2749ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
2759ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_RXVLAN;
2769ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_HW_VLAN_CTAG_TX)
2779ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_TXVLAN;
2789ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_NTUPLE)
2799ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_NTUPLE;
2809ce48e5aSMichal Kubecek 	if (dev->features & NETIF_F_RXHASH)
2819ce48e5aSMichal Kubecek 		flags |= ETH_FLAG_RXHASH;
2829ce48e5aSMichal Kubecek 
2839ce48e5aSMichal Kubecek 	return flags;
2849ce48e5aSMichal Kubecek }
2859ce48e5aSMichal Kubecek 
2869ce48e5aSMichal Kubecek static int __ethtool_set_flags(struct net_device *dev, u32 data)
2879ce48e5aSMichal Kubecek {
2889ce48e5aSMichal Kubecek 	netdev_features_t features = 0, changed;
2899ce48e5aSMichal Kubecek 
2909ce48e5aSMichal Kubecek 	if (data & ~ETH_ALL_FLAGS)
2919ce48e5aSMichal Kubecek 		return -EINVAL;
2929ce48e5aSMichal Kubecek 
2939ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_LRO)
2949ce48e5aSMichal Kubecek 		features |= NETIF_F_LRO;
2959ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_RXVLAN)
2969ce48e5aSMichal Kubecek 		features |= NETIF_F_HW_VLAN_CTAG_RX;
2979ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_TXVLAN)
2989ce48e5aSMichal Kubecek 		features |= NETIF_F_HW_VLAN_CTAG_TX;
2999ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_NTUPLE)
3009ce48e5aSMichal Kubecek 		features |= NETIF_F_NTUPLE;
3019ce48e5aSMichal Kubecek 	if (data & ETH_FLAG_RXHASH)
3029ce48e5aSMichal Kubecek 		features |= NETIF_F_RXHASH;
3039ce48e5aSMichal Kubecek 
3049ce48e5aSMichal Kubecek 	/* allow changing only bits set in hw_features */
3059ce48e5aSMichal Kubecek 	changed = (features ^ dev->features) & ETH_ALL_FEATURES;
3069ce48e5aSMichal Kubecek 	if (changed & ~dev->hw_features)
3079ce48e5aSMichal Kubecek 		return (changed & dev->hw_features) ? -EINVAL : -EOPNOTSUPP;
3089ce48e5aSMichal Kubecek 
3099ce48e5aSMichal Kubecek 	dev->wanted_features =
3109ce48e5aSMichal Kubecek 		(dev->wanted_features & ~changed) | (features & changed);
3119ce48e5aSMichal Kubecek 
3129ce48e5aSMichal Kubecek 	__netdev_update_features(dev);
3139ce48e5aSMichal Kubecek 
3149ce48e5aSMichal Kubecek 	return 0;
3159ce48e5aSMichal Kubecek }
3169ce48e5aSMichal Kubecek 
3179ce48e5aSMichal Kubecek /* Given two link masks, AND them together and save the result in dst. */
3189ce48e5aSMichal Kubecek void ethtool_intersect_link_masks(struct ethtool_link_ksettings *dst,
3199ce48e5aSMichal Kubecek 				  struct ethtool_link_ksettings *src)
3209ce48e5aSMichal Kubecek {
3219ce48e5aSMichal Kubecek 	unsigned int size = BITS_TO_LONGS(__ETHTOOL_LINK_MODE_MASK_NBITS);
3229ce48e5aSMichal Kubecek 	unsigned int idx = 0;
3239ce48e5aSMichal Kubecek 
3249ce48e5aSMichal Kubecek 	for (; idx < size; idx++) {
3259ce48e5aSMichal Kubecek 		dst->link_modes.supported[idx] &=
3269ce48e5aSMichal Kubecek 			src->link_modes.supported[idx];
3279ce48e5aSMichal Kubecek 		dst->link_modes.advertising[idx] &=
3289ce48e5aSMichal Kubecek 			src->link_modes.advertising[idx];
3299ce48e5aSMichal Kubecek 	}
3309ce48e5aSMichal Kubecek }
3319ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_intersect_link_masks);
3329ce48e5aSMichal Kubecek 
3339ce48e5aSMichal Kubecek void ethtool_convert_legacy_u32_to_link_mode(unsigned long *dst,
3349ce48e5aSMichal Kubecek 					     u32 legacy_u32)
3359ce48e5aSMichal Kubecek {
3369ce48e5aSMichal Kubecek 	bitmap_zero(dst, __ETHTOOL_LINK_MODE_MASK_NBITS);
3379ce48e5aSMichal Kubecek 	dst[0] = legacy_u32;
3389ce48e5aSMichal Kubecek }
3399ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_convert_legacy_u32_to_link_mode);
3409ce48e5aSMichal Kubecek 
3419ce48e5aSMichal Kubecek /* return false if src had higher bits set. lower bits always updated. */
3429ce48e5aSMichal Kubecek bool ethtool_convert_link_mode_to_legacy_u32(u32 *legacy_u32,
3439ce48e5aSMichal Kubecek 					     const unsigned long *src)
3449ce48e5aSMichal Kubecek {
3459ce48e5aSMichal Kubecek 	bool retval = true;
3469ce48e5aSMichal Kubecek 
3479ce48e5aSMichal Kubecek 	/* TODO: following test will soon always be true */
3489ce48e5aSMichal Kubecek 	if (__ETHTOOL_LINK_MODE_MASK_NBITS > 32) {
3499ce48e5aSMichal Kubecek 		__ETHTOOL_DECLARE_LINK_MODE_MASK(ext);
3509ce48e5aSMichal Kubecek 
3519ce48e5aSMichal Kubecek 		bitmap_zero(ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
3529ce48e5aSMichal Kubecek 		bitmap_fill(ext, 32);
3539ce48e5aSMichal Kubecek 		bitmap_complement(ext, ext, __ETHTOOL_LINK_MODE_MASK_NBITS);
3549ce48e5aSMichal Kubecek 		if (bitmap_intersects(ext, src,
3559ce48e5aSMichal Kubecek 				      __ETHTOOL_LINK_MODE_MASK_NBITS)) {
3569ce48e5aSMichal Kubecek 			/* src mask goes beyond bit 31 */
3579ce48e5aSMichal Kubecek 			retval = false;
3589ce48e5aSMichal Kubecek 		}
3599ce48e5aSMichal Kubecek 	}
3609ce48e5aSMichal Kubecek 	*legacy_u32 = src[0];
3619ce48e5aSMichal Kubecek 	return retval;
3629ce48e5aSMichal Kubecek }
3639ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_convert_link_mode_to_legacy_u32);
3649ce48e5aSMichal Kubecek 
3659ce48e5aSMichal Kubecek /* return false if ksettings link modes had higher bits
3669ce48e5aSMichal Kubecek  * set. legacy_settings always updated (best effort)
3679ce48e5aSMichal Kubecek  */
3689ce48e5aSMichal Kubecek static bool
3699ce48e5aSMichal Kubecek convert_link_ksettings_to_legacy_settings(
3709ce48e5aSMichal Kubecek 	struct ethtool_cmd *legacy_settings,
3719ce48e5aSMichal Kubecek 	const struct ethtool_link_ksettings *link_ksettings)
3729ce48e5aSMichal Kubecek {
3739ce48e5aSMichal Kubecek 	bool retval = true;
3749ce48e5aSMichal Kubecek 
3759ce48e5aSMichal Kubecek 	memset(legacy_settings, 0, sizeof(*legacy_settings));
3769ce48e5aSMichal Kubecek 	/* this also clears the deprecated fields in legacy structure:
3779ce48e5aSMichal Kubecek 	 * __u8		transceiver;
3789ce48e5aSMichal Kubecek 	 * __u32	maxtxpkt;
3799ce48e5aSMichal Kubecek 	 * __u32	maxrxpkt;
3809ce48e5aSMichal Kubecek 	 */
3819ce48e5aSMichal Kubecek 
3829ce48e5aSMichal Kubecek 	retval &= ethtool_convert_link_mode_to_legacy_u32(
3839ce48e5aSMichal Kubecek 		&legacy_settings->supported,
3849ce48e5aSMichal Kubecek 		link_ksettings->link_modes.supported);
3859ce48e5aSMichal Kubecek 	retval &= ethtool_convert_link_mode_to_legacy_u32(
3869ce48e5aSMichal Kubecek 		&legacy_settings->advertising,
3879ce48e5aSMichal Kubecek 		link_ksettings->link_modes.advertising);
3889ce48e5aSMichal Kubecek 	retval &= ethtool_convert_link_mode_to_legacy_u32(
3899ce48e5aSMichal Kubecek 		&legacy_settings->lp_advertising,
3909ce48e5aSMichal Kubecek 		link_ksettings->link_modes.lp_advertising);
3919ce48e5aSMichal Kubecek 	ethtool_cmd_speed_set(legacy_settings, link_ksettings->base.speed);
3929ce48e5aSMichal Kubecek 	legacy_settings->duplex
3939ce48e5aSMichal Kubecek 		= link_ksettings->base.duplex;
3949ce48e5aSMichal Kubecek 	legacy_settings->port
3959ce48e5aSMichal Kubecek 		= link_ksettings->base.port;
3969ce48e5aSMichal Kubecek 	legacy_settings->phy_address
3979ce48e5aSMichal Kubecek 		= link_ksettings->base.phy_address;
3989ce48e5aSMichal Kubecek 	legacy_settings->autoneg
3999ce48e5aSMichal Kubecek 		= link_ksettings->base.autoneg;
4009ce48e5aSMichal Kubecek 	legacy_settings->mdio_support
4019ce48e5aSMichal Kubecek 		= link_ksettings->base.mdio_support;
4029ce48e5aSMichal Kubecek 	legacy_settings->eth_tp_mdix
4039ce48e5aSMichal Kubecek 		= link_ksettings->base.eth_tp_mdix;
4049ce48e5aSMichal Kubecek 	legacy_settings->eth_tp_mdix_ctrl
4059ce48e5aSMichal Kubecek 		= link_ksettings->base.eth_tp_mdix_ctrl;
4069ce48e5aSMichal Kubecek 	legacy_settings->transceiver
4079ce48e5aSMichal Kubecek 		= link_ksettings->base.transceiver;
4089ce48e5aSMichal Kubecek 	return retval;
4099ce48e5aSMichal Kubecek }
4109ce48e5aSMichal Kubecek 
4119ce48e5aSMichal Kubecek /* number of 32-bit words to store the user's link mode bitmaps */
4129ce48e5aSMichal Kubecek #define __ETHTOOL_LINK_MODE_MASK_NU32			\
4139ce48e5aSMichal Kubecek 	DIV_ROUND_UP(__ETHTOOL_LINK_MODE_MASK_NBITS, 32)
4149ce48e5aSMichal Kubecek 
4159ce48e5aSMichal Kubecek /* layout of the struct passed from/to userland */
4169ce48e5aSMichal Kubecek struct ethtool_link_usettings {
4179ce48e5aSMichal Kubecek 	struct ethtool_link_settings base;
4189ce48e5aSMichal Kubecek 	struct {
4199ce48e5aSMichal Kubecek 		__u32 supported[__ETHTOOL_LINK_MODE_MASK_NU32];
4209ce48e5aSMichal Kubecek 		__u32 advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
4219ce48e5aSMichal Kubecek 		__u32 lp_advertising[__ETHTOOL_LINK_MODE_MASK_NU32];
4229ce48e5aSMichal Kubecek 	} link_modes;
4239ce48e5aSMichal Kubecek };
4249ce48e5aSMichal Kubecek 
4259ce48e5aSMichal Kubecek /* Internal kernel helper to query a device ethtool_link_settings. */
4269ce48e5aSMichal Kubecek int __ethtool_get_link_ksettings(struct net_device *dev,
4279ce48e5aSMichal Kubecek 				 struct ethtool_link_ksettings *link_ksettings)
4289ce48e5aSMichal Kubecek {
4299ce48e5aSMichal Kubecek 	ASSERT_RTNL();
4309ce48e5aSMichal Kubecek 
4319ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_link_ksettings)
4329ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
4339ce48e5aSMichal Kubecek 
4349ce48e5aSMichal Kubecek 	memset(link_ksettings, 0, sizeof(*link_ksettings));
435a975d7d8SDanielle Ratson 	return dev->ethtool_ops->get_link_ksettings(dev, link_ksettings);
4369ce48e5aSMichal Kubecek }
4379ce48e5aSMichal Kubecek EXPORT_SYMBOL(__ethtool_get_link_ksettings);
4389ce48e5aSMichal Kubecek 
4399ce48e5aSMichal Kubecek /* convert ethtool_link_usettings in user space to a kernel internal
4409ce48e5aSMichal Kubecek  * ethtool_link_ksettings. return 0 on success, errno on error.
4419ce48e5aSMichal Kubecek  */
4429ce48e5aSMichal Kubecek static int load_link_ksettings_from_user(struct ethtool_link_ksettings *to,
4439ce48e5aSMichal Kubecek 					 const void __user *from)
4449ce48e5aSMichal Kubecek {
4459ce48e5aSMichal Kubecek 	struct ethtool_link_usettings link_usettings;
4469ce48e5aSMichal Kubecek 
4479ce48e5aSMichal Kubecek 	if (copy_from_user(&link_usettings, from, sizeof(link_usettings)))
4489ce48e5aSMichal Kubecek 		return -EFAULT;
4499ce48e5aSMichal Kubecek 
4509ce48e5aSMichal Kubecek 	memcpy(&to->base, &link_usettings.base, sizeof(to->base));
4519ce48e5aSMichal Kubecek 	bitmap_from_arr32(to->link_modes.supported,
4529ce48e5aSMichal Kubecek 			  link_usettings.link_modes.supported,
4539ce48e5aSMichal Kubecek 			  __ETHTOOL_LINK_MODE_MASK_NBITS);
4549ce48e5aSMichal Kubecek 	bitmap_from_arr32(to->link_modes.advertising,
4559ce48e5aSMichal Kubecek 			  link_usettings.link_modes.advertising,
4569ce48e5aSMichal Kubecek 			  __ETHTOOL_LINK_MODE_MASK_NBITS);
4579ce48e5aSMichal Kubecek 	bitmap_from_arr32(to->link_modes.lp_advertising,
4589ce48e5aSMichal Kubecek 			  link_usettings.link_modes.lp_advertising,
4599ce48e5aSMichal Kubecek 			  __ETHTOOL_LINK_MODE_MASK_NBITS);
4609ce48e5aSMichal Kubecek 
4619ce48e5aSMichal Kubecek 	return 0;
4629ce48e5aSMichal Kubecek }
4639ce48e5aSMichal Kubecek 
46470ae1e12SCris Forno /* Check if the user is trying to change anything besides speed/duplex */
46570ae1e12SCris Forno bool ethtool_virtdev_validate_cmd(const struct ethtool_link_ksettings *cmd)
46670ae1e12SCris Forno {
46770ae1e12SCris Forno 	struct ethtool_link_settings base2 = {};
46870ae1e12SCris Forno 
46970ae1e12SCris Forno 	base2.speed = cmd->base.speed;
47070ae1e12SCris Forno 	base2.port = PORT_OTHER;
47170ae1e12SCris Forno 	base2.duplex = cmd->base.duplex;
47270ae1e12SCris Forno 	base2.cmd = cmd->base.cmd;
47370ae1e12SCris Forno 	base2.link_mode_masks_nwords = cmd->base.link_mode_masks_nwords;
47470ae1e12SCris Forno 
47570ae1e12SCris Forno 	return !memcmp(&base2, &cmd->base, sizeof(base2)) &&
47670ae1e12SCris Forno 		bitmap_empty(cmd->link_modes.supported,
47770ae1e12SCris Forno 			     __ETHTOOL_LINK_MODE_MASK_NBITS) &&
47870ae1e12SCris Forno 		bitmap_empty(cmd->link_modes.lp_advertising,
47970ae1e12SCris Forno 			     __ETHTOOL_LINK_MODE_MASK_NBITS);
48070ae1e12SCris Forno }
48170ae1e12SCris Forno 
4829ce48e5aSMichal Kubecek /* convert a kernel internal ethtool_link_ksettings to
4839ce48e5aSMichal Kubecek  * ethtool_link_usettings in user space. return 0 on success, errno on
4849ce48e5aSMichal Kubecek  * error.
4859ce48e5aSMichal Kubecek  */
4869ce48e5aSMichal Kubecek static int
4879ce48e5aSMichal Kubecek store_link_ksettings_for_user(void __user *to,
4889ce48e5aSMichal Kubecek 			      const struct ethtool_link_ksettings *from)
4899ce48e5aSMichal Kubecek {
4909ce48e5aSMichal Kubecek 	struct ethtool_link_usettings link_usettings;
4919ce48e5aSMichal Kubecek 
492c1d9e34eSGustavo A. R. Silva 	memcpy(&link_usettings, from, sizeof(link_usettings));
4939ce48e5aSMichal Kubecek 	bitmap_to_arr32(link_usettings.link_modes.supported,
4949ce48e5aSMichal Kubecek 			from->link_modes.supported,
4959ce48e5aSMichal Kubecek 			__ETHTOOL_LINK_MODE_MASK_NBITS);
4969ce48e5aSMichal Kubecek 	bitmap_to_arr32(link_usettings.link_modes.advertising,
4979ce48e5aSMichal Kubecek 			from->link_modes.advertising,
4989ce48e5aSMichal Kubecek 			__ETHTOOL_LINK_MODE_MASK_NBITS);
4999ce48e5aSMichal Kubecek 	bitmap_to_arr32(link_usettings.link_modes.lp_advertising,
5009ce48e5aSMichal Kubecek 			from->link_modes.lp_advertising,
5019ce48e5aSMichal Kubecek 			__ETHTOOL_LINK_MODE_MASK_NBITS);
5029ce48e5aSMichal Kubecek 
5039ce48e5aSMichal Kubecek 	if (copy_to_user(to, &link_usettings, sizeof(link_usettings)))
5049ce48e5aSMichal Kubecek 		return -EFAULT;
5059ce48e5aSMichal Kubecek 
5069ce48e5aSMichal Kubecek 	return 0;
5079ce48e5aSMichal Kubecek }
5089ce48e5aSMichal Kubecek 
5099ce48e5aSMichal Kubecek /* Query device for its ethtool_link_settings. */
5109ce48e5aSMichal Kubecek static int ethtool_get_link_ksettings(struct net_device *dev,
5119ce48e5aSMichal Kubecek 				      void __user *useraddr)
5129ce48e5aSMichal Kubecek {
5139ce48e5aSMichal Kubecek 	int err = 0;
5149ce48e5aSMichal Kubecek 	struct ethtool_link_ksettings link_ksettings;
5159ce48e5aSMichal Kubecek 
5169ce48e5aSMichal Kubecek 	ASSERT_RTNL();
5179ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_link_ksettings)
5189ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
5199ce48e5aSMichal Kubecek 
5209ce48e5aSMichal Kubecek 	/* handle bitmap nbits handshake */
5219ce48e5aSMichal Kubecek 	if (copy_from_user(&link_ksettings.base, useraddr,
5229ce48e5aSMichal Kubecek 			   sizeof(link_ksettings.base)))
5239ce48e5aSMichal Kubecek 		return -EFAULT;
5249ce48e5aSMichal Kubecek 
5259ce48e5aSMichal Kubecek 	if (__ETHTOOL_LINK_MODE_MASK_NU32
5269ce48e5aSMichal Kubecek 	    != link_ksettings.base.link_mode_masks_nwords) {
5279ce48e5aSMichal Kubecek 		/* wrong link mode nbits requested */
5289ce48e5aSMichal Kubecek 		memset(&link_ksettings, 0, sizeof(link_ksettings));
5299ce48e5aSMichal Kubecek 		link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
5309ce48e5aSMichal Kubecek 		/* send back number of words required as negative val */
5319ce48e5aSMichal Kubecek 		compiletime_assert(__ETHTOOL_LINK_MODE_MASK_NU32 <= S8_MAX,
5329ce48e5aSMichal Kubecek 				   "need too many bits for link modes!");
5339ce48e5aSMichal Kubecek 		link_ksettings.base.link_mode_masks_nwords
5349ce48e5aSMichal Kubecek 			= -((s8)__ETHTOOL_LINK_MODE_MASK_NU32);
5359ce48e5aSMichal Kubecek 
5369ce48e5aSMichal Kubecek 		/* copy the base fields back to user, not the link
5379ce48e5aSMichal Kubecek 		 * mode bitmaps
5389ce48e5aSMichal Kubecek 		 */
5399ce48e5aSMichal Kubecek 		if (copy_to_user(useraddr, &link_ksettings.base,
5409ce48e5aSMichal Kubecek 				 sizeof(link_ksettings.base)))
5419ce48e5aSMichal Kubecek 			return -EFAULT;
5429ce48e5aSMichal Kubecek 
5439ce48e5aSMichal Kubecek 		return 0;
5449ce48e5aSMichal Kubecek 	}
5459ce48e5aSMichal Kubecek 
5469ce48e5aSMichal Kubecek 	/* handshake successful: user/kernel agree on
5479ce48e5aSMichal Kubecek 	 * link_mode_masks_nwords
5489ce48e5aSMichal Kubecek 	 */
5499ce48e5aSMichal Kubecek 
5509ce48e5aSMichal Kubecek 	memset(&link_ksettings, 0, sizeof(link_ksettings));
5519ce48e5aSMichal Kubecek 	err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
5529ce48e5aSMichal Kubecek 	if (err < 0)
5539ce48e5aSMichal Kubecek 		return err;
5549ce48e5aSMichal Kubecek 
5559ce48e5aSMichal Kubecek 	/* make sure we tell the right values to user */
5569ce48e5aSMichal Kubecek 	link_ksettings.base.cmd = ETHTOOL_GLINKSETTINGS;
5579ce48e5aSMichal Kubecek 	link_ksettings.base.link_mode_masks_nwords
5589ce48e5aSMichal Kubecek 		= __ETHTOOL_LINK_MODE_MASK_NU32;
559bdbdac76SOleksij Rempel 	link_ksettings.base.master_slave_cfg = MASTER_SLAVE_CFG_UNSUPPORTED;
560bdbdac76SOleksij Rempel 	link_ksettings.base.master_slave_state = MASTER_SLAVE_STATE_UNSUPPORTED;
5619ce48e5aSMichal Kubecek 
5629ce48e5aSMichal Kubecek 	return store_link_ksettings_for_user(useraddr, &link_ksettings);
5639ce48e5aSMichal Kubecek }
5649ce48e5aSMichal Kubecek 
5659ce48e5aSMichal Kubecek /* Update device ethtool_link_settings. */
5669ce48e5aSMichal Kubecek static int ethtool_set_link_ksettings(struct net_device *dev,
5679ce48e5aSMichal Kubecek 				      void __user *useraddr)
5689ce48e5aSMichal Kubecek {
5699ce48e5aSMichal Kubecek 	int err;
5709ce48e5aSMichal Kubecek 	struct ethtool_link_ksettings link_ksettings;
5719ce48e5aSMichal Kubecek 
5729ce48e5aSMichal Kubecek 	ASSERT_RTNL();
5739ce48e5aSMichal Kubecek 
5749ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_link_ksettings)
5759ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
5769ce48e5aSMichal Kubecek 
5779ce48e5aSMichal Kubecek 	/* make sure nbits field has expected value */
5789ce48e5aSMichal Kubecek 	if (copy_from_user(&link_ksettings.base, useraddr,
5799ce48e5aSMichal Kubecek 			   sizeof(link_ksettings.base)))
5809ce48e5aSMichal Kubecek 		return -EFAULT;
5819ce48e5aSMichal Kubecek 
5829ce48e5aSMichal Kubecek 	if (__ETHTOOL_LINK_MODE_MASK_NU32
5839ce48e5aSMichal Kubecek 	    != link_ksettings.base.link_mode_masks_nwords)
5849ce48e5aSMichal Kubecek 		return -EINVAL;
5859ce48e5aSMichal Kubecek 
5869ce48e5aSMichal Kubecek 	/* copy the whole structure, now that we know it has expected
5879ce48e5aSMichal Kubecek 	 * format
5889ce48e5aSMichal Kubecek 	 */
5899ce48e5aSMichal Kubecek 	err = load_link_ksettings_from_user(&link_ksettings, useraddr);
5909ce48e5aSMichal Kubecek 	if (err)
5919ce48e5aSMichal Kubecek 		return err;
5929ce48e5aSMichal Kubecek 
5939ce48e5aSMichal Kubecek 	/* re-check nwords field, just in case */
5949ce48e5aSMichal Kubecek 	if (__ETHTOOL_LINK_MODE_MASK_NU32
5959ce48e5aSMichal Kubecek 	    != link_ksettings.base.link_mode_masks_nwords)
5969ce48e5aSMichal Kubecek 		return -EINVAL;
5979ce48e5aSMichal Kubecek 
598bdbdac76SOleksij Rempel 	if (link_ksettings.base.master_slave_cfg ||
599bdbdac76SOleksij Rempel 	    link_ksettings.base.master_slave_state)
600bdbdac76SOleksij Rempel 		return -EINVAL;
601bdbdac76SOleksij Rempel 
60273286734SMichal Kubecek 	err = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
6031b1b1847SMichal Kubecek 	if (err >= 0) {
60473286734SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
6051b1b1847SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
6061b1b1847SMichal Kubecek 	}
60773286734SMichal Kubecek 	return err;
6089ce48e5aSMichal Kubecek }
6099ce48e5aSMichal Kubecek 
61070ae1e12SCris Forno int ethtool_virtdev_set_link_ksettings(struct net_device *dev,
61170ae1e12SCris Forno 				       const struct ethtool_link_ksettings *cmd,
61270ae1e12SCris Forno 				       u32 *dev_speed, u8 *dev_duplex)
61370ae1e12SCris Forno {
61470ae1e12SCris Forno 	u32 speed;
61570ae1e12SCris Forno 	u8 duplex;
61670ae1e12SCris Forno 
61770ae1e12SCris Forno 	speed = cmd->base.speed;
61870ae1e12SCris Forno 	duplex = cmd->base.duplex;
61970ae1e12SCris Forno 	/* don't allow custom speed and duplex */
62070ae1e12SCris Forno 	if (!ethtool_validate_speed(speed) ||
62170ae1e12SCris Forno 	    !ethtool_validate_duplex(duplex) ||
62270ae1e12SCris Forno 	    !ethtool_virtdev_validate_cmd(cmd))
62370ae1e12SCris Forno 		return -EINVAL;
62470ae1e12SCris Forno 	*dev_speed = speed;
62570ae1e12SCris Forno 	*dev_duplex = duplex;
62670ae1e12SCris Forno 
62770ae1e12SCris Forno 	return 0;
62870ae1e12SCris Forno }
62970ae1e12SCris Forno EXPORT_SYMBOL(ethtool_virtdev_set_link_ksettings);
63070ae1e12SCris Forno 
6319ce48e5aSMichal Kubecek /* Query device for its ethtool_cmd settings.
6329ce48e5aSMichal Kubecek  *
6339ce48e5aSMichal Kubecek  * Backward compatibility note: for compatibility with legacy ethtool, this is
6349ce48e5aSMichal Kubecek  * now implemented via get_link_ksettings. When driver reports higher link mode
6359ce48e5aSMichal Kubecek  * bits, a kernel warning is logged once (with name of 1st driver/device) to
6369ce48e5aSMichal Kubecek  * recommend user to upgrade ethtool, but the command is successful (only the
6379ce48e5aSMichal Kubecek  * lower link mode bits reported back to user). Deprecated fields from
6389ce48e5aSMichal Kubecek  * ethtool_cmd (transceiver/maxrxpkt/maxtxpkt) are always set to zero.
6399ce48e5aSMichal Kubecek  */
6409ce48e5aSMichal Kubecek static int ethtool_get_settings(struct net_device *dev, void __user *useraddr)
6419ce48e5aSMichal Kubecek {
6429ce48e5aSMichal Kubecek 	struct ethtool_link_ksettings link_ksettings;
6439ce48e5aSMichal Kubecek 	struct ethtool_cmd cmd;
6449ce48e5aSMichal Kubecek 	int err;
6459ce48e5aSMichal Kubecek 
6469ce48e5aSMichal Kubecek 	ASSERT_RTNL();
6479ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_link_ksettings)
6489ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
6499ce48e5aSMichal Kubecek 
6509ce48e5aSMichal Kubecek 	memset(&link_ksettings, 0, sizeof(link_ksettings));
6519ce48e5aSMichal Kubecek 	err = dev->ethtool_ops->get_link_ksettings(dev, &link_ksettings);
6529ce48e5aSMichal Kubecek 	if (err < 0)
6539ce48e5aSMichal Kubecek 		return err;
6549ce48e5aSMichal Kubecek 	convert_link_ksettings_to_legacy_settings(&cmd, &link_ksettings);
6559ce48e5aSMichal Kubecek 
6569ce48e5aSMichal Kubecek 	/* send a sensible cmd tag back to user */
6579ce48e5aSMichal Kubecek 	cmd.cmd = ETHTOOL_GSET;
6589ce48e5aSMichal Kubecek 
6599ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &cmd, sizeof(cmd)))
6609ce48e5aSMichal Kubecek 		return -EFAULT;
6619ce48e5aSMichal Kubecek 
6629ce48e5aSMichal Kubecek 	return 0;
6639ce48e5aSMichal Kubecek }
6649ce48e5aSMichal Kubecek 
6659ce48e5aSMichal Kubecek /* Update device link settings with given ethtool_cmd.
6669ce48e5aSMichal Kubecek  *
6679ce48e5aSMichal Kubecek  * Backward compatibility note: for compatibility with legacy ethtool, this is
6689ce48e5aSMichal Kubecek  * now always implemented via set_link_settings. When user's request updates
6699ce48e5aSMichal Kubecek  * deprecated ethtool_cmd fields (transceiver/maxrxpkt/maxtxpkt), a kernel
6709ce48e5aSMichal Kubecek  * warning is logged once (with name of 1st driver/device) to recommend user to
6719ce48e5aSMichal Kubecek  * upgrade ethtool, and the request is rejected.
6729ce48e5aSMichal Kubecek  */
6739ce48e5aSMichal Kubecek static int ethtool_set_settings(struct net_device *dev, void __user *useraddr)
6749ce48e5aSMichal Kubecek {
6759ce48e5aSMichal Kubecek 	struct ethtool_link_ksettings link_ksettings;
6769ce48e5aSMichal Kubecek 	struct ethtool_cmd cmd;
67773286734SMichal Kubecek 	int ret;
6789ce48e5aSMichal Kubecek 
6799ce48e5aSMichal Kubecek 	ASSERT_RTNL();
6809ce48e5aSMichal Kubecek 
6819ce48e5aSMichal Kubecek 	if (copy_from_user(&cmd, useraddr, sizeof(cmd)))
6829ce48e5aSMichal Kubecek 		return -EFAULT;
6839ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_link_ksettings)
6849ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
6859ce48e5aSMichal Kubecek 
6869ce48e5aSMichal Kubecek 	if (!convert_legacy_settings_to_link_ksettings(&link_ksettings, &cmd))
6879ce48e5aSMichal Kubecek 		return -EINVAL;
6889ce48e5aSMichal Kubecek 	link_ksettings.base.link_mode_masks_nwords =
6899ce48e5aSMichal Kubecek 		__ETHTOOL_LINK_MODE_MASK_NU32;
69073286734SMichal Kubecek 	ret = dev->ethtool_ops->set_link_ksettings(dev, &link_ksettings);
6911b1b1847SMichal Kubecek 	if (ret >= 0) {
69273286734SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKINFO_NTF, NULL);
6931b1b1847SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_LINKMODES_NTF, NULL);
6941b1b1847SMichal Kubecek 	}
69573286734SMichal Kubecek 	return ret;
6969ce48e5aSMichal Kubecek }
6979ce48e5aSMichal Kubecek 
6989ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_drvinfo(struct net_device *dev,
6999ce48e5aSMichal Kubecek 						  void __user *useraddr)
7009ce48e5aSMichal Kubecek {
7019ce48e5aSMichal Kubecek 	struct ethtool_drvinfo info;
7029ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
7039ce48e5aSMichal Kubecek 
7049ce48e5aSMichal Kubecek 	memset(&info, 0, sizeof(info));
7059ce48e5aSMichal Kubecek 	info.cmd = ETHTOOL_GDRVINFO;
7066a7e25c7SLeon Romanovsky 	strlcpy(info.version, UTS_RELEASE, sizeof(info.version));
7079ce48e5aSMichal Kubecek 	if (ops->get_drvinfo) {
7089ce48e5aSMichal Kubecek 		ops->get_drvinfo(dev, &info);
7099ce48e5aSMichal Kubecek 	} else if (dev->dev.parent && dev->dev.parent->driver) {
7109ce48e5aSMichal Kubecek 		strlcpy(info.bus_info, dev_name(dev->dev.parent),
7119ce48e5aSMichal Kubecek 			sizeof(info.bus_info));
7129ce48e5aSMichal Kubecek 		strlcpy(info.driver, dev->dev.parent->driver->name,
7139ce48e5aSMichal Kubecek 			sizeof(info.driver));
7149ce48e5aSMichal Kubecek 	} else {
7159ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
7169ce48e5aSMichal Kubecek 	}
7179ce48e5aSMichal Kubecek 
7189ce48e5aSMichal Kubecek 	/*
7199ce48e5aSMichal Kubecek 	 * this method of obtaining string set info is deprecated;
7209ce48e5aSMichal Kubecek 	 * Use ETHTOOL_GSSET_INFO instead.
7219ce48e5aSMichal Kubecek 	 */
7229ce48e5aSMichal Kubecek 	if (ops->get_sset_count) {
7239ce48e5aSMichal Kubecek 		int rc;
7249ce48e5aSMichal Kubecek 
7259ce48e5aSMichal Kubecek 		rc = ops->get_sset_count(dev, ETH_SS_TEST);
7269ce48e5aSMichal Kubecek 		if (rc >= 0)
7279ce48e5aSMichal Kubecek 			info.testinfo_len = rc;
7289ce48e5aSMichal Kubecek 		rc = ops->get_sset_count(dev, ETH_SS_STATS);
7299ce48e5aSMichal Kubecek 		if (rc >= 0)
7309ce48e5aSMichal Kubecek 			info.n_stats = rc;
7319ce48e5aSMichal Kubecek 		rc = ops->get_sset_count(dev, ETH_SS_PRIV_FLAGS);
7329ce48e5aSMichal Kubecek 		if (rc >= 0)
7339ce48e5aSMichal Kubecek 			info.n_priv_flags = rc;
7349ce48e5aSMichal Kubecek 	}
7359ce48e5aSMichal Kubecek 	if (ops->get_regs_len) {
7369ce48e5aSMichal Kubecek 		int ret = ops->get_regs_len(dev);
7379ce48e5aSMichal Kubecek 
7389ce48e5aSMichal Kubecek 		if (ret > 0)
7399ce48e5aSMichal Kubecek 			info.regdump_len = ret;
7409ce48e5aSMichal Kubecek 	}
7419ce48e5aSMichal Kubecek 
7429ce48e5aSMichal Kubecek 	if (ops->get_eeprom_len)
7439ce48e5aSMichal Kubecek 		info.eedump_len = ops->get_eeprom_len(dev);
7449ce48e5aSMichal Kubecek 
7459ce48e5aSMichal Kubecek 	if (!info.fw_version[0])
7469ce48e5aSMichal Kubecek 		devlink_compat_running_version(dev, info.fw_version,
7479ce48e5aSMichal Kubecek 					       sizeof(info.fw_version));
7489ce48e5aSMichal Kubecek 
7499ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &info, sizeof(info)))
7509ce48e5aSMichal Kubecek 		return -EFAULT;
7519ce48e5aSMichal Kubecek 	return 0;
7529ce48e5aSMichal Kubecek }
7539ce48e5aSMichal Kubecek 
7549ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_sset_info(struct net_device *dev,
7559ce48e5aSMichal Kubecek 						    void __user *useraddr)
7569ce48e5aSMichal Kubecek {
7579ce48e5aSMichal Kubecek 	struct ethtool_sset_info info;
7589ce48e5aSMichal Kubecek 	u64 sset_mask;
7599ce48e5aSMichal Kubecek 	int i, idx = 0, n_bits = 0, ret, rc;
7609ce48e5aSMichal Kubecek 	u32 *info_buf = NULL;
7619ce48e5aSMichal Kubecek 
7629ce48e5aSMichal Kubecek 	if (copy_from_user(&info, useraddr, sizeof(info)))
7639ce48e5aSMichal Kubecek 		return -EFAULT;
7649ce48e5aSMichal Kubecek 
7659ce48e5aSMichal Kubecek 	/* store copy of mask, because we zero struct later on */
7669ce48e5aSMichal Kubecek 	sset_mask = info.sset_mask;
7679ce48e5aSMichal Kubecek 	if (!sset_mask)
7689ce48e5aSMichal Kubecek 		return 0;
7699ce48e5aSMichal Kubecek 
7709ce48e5aSMichal Kubecek 	/* calculate size of return buffer */
7719ce48e5aSMichal Kubecek 	n_bits = hweight64(sset_mask);
7729ce48e5aSMichal Kubecek 
7739ce48e5aSMichal Kubecek 	memset(&info, 0, sizeof(info));
7749ce48e5aSMichal Kubecek 	info.cmd = ETHTOOL_GSSET_INFO;
7759ce48e5aSMichal Kubecek 
7769ce48e5aSMichal Kubecek 	info_buf = kcalloc(n_bits, sizeof(u32), GFP_USER);
7779ce48e5aSMichal Kubecek 	if (!info_buf)
7789ce48e5aSMichal Kubecek 		return -ENOMEM;
7799ce48e5aSMichal Kubecek 
7809ce48e5aSMichal Kubecek 	/*
7819ce48e5aSMichal Kubecek 	 * fill return buffer based on input bitmask and successful
7829ce48e5aSMichal Kubecek 	 * get_sset_count return
7839ce48e5aSMichal Kubecek 	 */
7849ce48e5aSMichal Kubecek 	for (i = 0; i < 64; i++) {
7859ce48e5aSMichal Kubecek 		if (!(sset_mask & (1ULL << i)))
7869ce48e5aSMichal Kubecek 			continue;
7879ce48e5aSMichal Kubecek 
7889ce48e5aSMichal Kubecek 		rc = __ethtool_get_sset_count(dev, i);
7899ce48e5aSMichal Kubecek 		if (rc >= 0) {
7909ce48e5aSMichal Kubecek 			info.sset_mask |= (1ULL << i);
7919ce48e5aSMichal Kubecek 			info_buf[idx++] = rc;
7929ce48e5aSMichal Kubecek 		}
7939ce48e5aSMichal Kubecek 	}
7949ce48e5aSMichal Kubecek 
7959ce48e5aSMichal Kubecek 	ret = -EFAULT;
7969ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &info, sizeof(info)))
7979ce48e5aSMichal Kubecek 		goto out;
7989ce48e5aSMichal Kubecek 
7999ce48e5aSMichal Kubecek 	useraddr += offsetof(struct ethtool_sset_info, data);
8009ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, info_buf, idx * sizeof(u32)))
8019ce48e5aSMichal Kubecek 		goto out;
8029ce48e5aSMichal Kubecek 
8039ce48e5aSMichal Kubecek 	ret = 0;
8049ce48e5aSMichal Kubecek 
8059ce48e5aSMichal Kubecek out:
8069ce48e5aSMichal Kubecek 	kfree(info_buf);
8079ce48e5aSMichal Kubecek 	return ret;
8089ce48e5aSMichal Kubecek }
8099ce48e5aSMichal Kubecek 
8109ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_rxnfc(struct net_device *dev,
8119ce48e5aSMichal Kubecek 						u32 cmd, void __user *useraddr)
8129ce48e5aSMichal Kubecek {
8139ce48e5aSMichal Kubecek 	struct ethtool_rxnfc info;
8149ce48e5aSMichal Kubecek 	size_t info_size = sizeof(info);
8159ce48e5aSMichal Kubecek 	int rc;
8169ce48e5aSMichal Kubecek 
8179ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_rxnfc)
8189ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
8199ce48e5aSMichal Kubecek 
8209ce48e5aSMichal Kubecek 	/* struct ethtool_rxnfc was originally defined for
8219ce48e5aSMichal Kubecek 	 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
8229ce48e5aSMichal Kubecek 	 * members.  User-space might still be using that
8239ce48e5aSMichal Kubecek 	 * definition. */
8249ce48e5aSMichal Kubecek 	if (cmd == ETHTOOL_SRXFH)
8259ce48e5aSMichal Kubecek 		info_size = (offsetof(struct ethtool_rxnfc, data) +
8269ce48e5aSMichal Kubecek 			     sizeof(info.data));
8279ce48e5aSMichal Kubecek 
8289ce48e5aSMichal Kubecek 	if (copy_from_user(&info, useraddr, info_size))
8299ce48e5aSMichal Kubecek 		return -EFAULT;
8309ce48e5aSMichal Kubecek 
8319ce48e5aSMichal Kubecek 	rc = dev->ethtool_ops->set_rxnfc(dev, &info);
8329ce48e5aSMichal Kubecek 	if (rc)
8339ce48e5aSMichal Kubecek 		return rc;
8349ce48e5aSMichal Kubecek 
8359ce48e5aSMichal Kubecek 	if (cmd == ETHTOOL_SRXCLSRLINS &&
8369ce48e5aSMichal Kubecek 	    copy_to_user(useraddr, &info, info_size))
8379ce48e5aSMichal Kubecek 		return -EFAULT;
8389ce48e5aSMichal Kubecek 
8399ce48e5aSMichal Kubecek 	return 0;
8409ce48e5aSMichal Kubecek }
8419ce48e5aSMichal Kubecek 
8429ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_rxnfc(struct net_device *dev,
8439ce48e5aSMichal Kubecek 						u32 cmd, void __user *useraddr)
8449ce48e5aSMichal Kubecek {
8459ce48e5aSMichal Kubecek 	struct ethtool_rxnfc info;
8469ce48e5aSMichal Kubecek 	size_t info_size = sizeof(info);
8479ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
8489ce48e5aSMichal Kubecek 	int ret;
8499ce48e5aSMichal Kubecek 	void *rule_buf = NULL;
8509ce48e5aSMichal Kubecek 
8519ce48e5aSMichal Kubecek 	if (!ops->get_rxnfc)
8529ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
8539ce48e5aSMichal Kubecek 
8549ce48e5aSMichal Kubecek 	/* struct ethtool_rxnfc was originally defined for
8559ce48e5aSMichal Kubecek 	 * ETHTOOL_{G,S}RXFH with only the cmd, flow_type and data
8569ce48e5aSMichal Kubecek 	 * members.  User-space might still be using that
8579ce48e5aSMichal Kubecek 	 * definition. */
8589ce48e5aSMichal Kubecek 	if (cmd == ETHTOOL_GRXFH)
8599ce48e5aSMichal Kubecek 		info_size = (offsetof(struct ethtool_rxnfc, data) +
8609ce48e5aSMichal Kubecek 			     sizeof(info.data));
8619ce48e5aSMichal Kubecek 
8629ce48e5aSMichal Kubecek 	if (copy_from_user(&info, useraddr, info_size))
8639ce48e5aSMichal Kubecek 		return -EFAULT;
8649ce48e5aSMichal Kubecek 
8659ce48e5aSMichal Kubecek 	/* If FLOW_RSS was requested then user-space must be using the
8669ce48e5aSMichal Kubecek 	 * new definition, as FLOW_RSS is newer.
8679ce48e5aSMichal Kubecek 	 */
8689ce48e5aSMichal Kubecek 	if (cmd == ETHTOOL_GRXFH && info.flow_type & FLOW_RSS) {
8699ce48e5aSMichal Kubecek 		info_size = sizeof(info);
8709ce48e5aSMichal Kubecek 		if (copy_from_user(&info, useraddr, info_size))
8719ce48e5aSMichal Kubecek 			return -EFAULT;
8729ce48e5aSMichal Kubecek 		/* Since malicious users may modify the original data,
8739ce48e5aSMichal Kubecek 		 * we need to check whether FLOW_RSS is still requested.
8749ce48e5aSMichal Kubecek 		 */
8759ce48e5aSMichal Kubecek 		if (!(info.flow_type & FLOW_RSS))
8769ce48e5aSMichal Kubecek 			return -EINVAL;
8779ce48e5aSMichal Kubecek 	}
8789ce48e5aSMichal Kubecek 
8799ce48e5aSMichal Kubecek 	if (info.cmd != cmd)
8809ce48e5aSMichal Kubecek 		return -EINVAL;
8819ce48e5aSMichal Kubecek 
8829ce48e5aSMichal Kubecek 	if (info.cmd == ETHTOOL_GRXCLSRLALL) {
8839ce48e5aSMichal Kubecek 		if (info.rule_cnt > 0) {
8849ce48e5aSMichal Kubecek 			if (info.rule_cnt <= KMALLOC_MAX_SIZE / sizeof(u32))
8859ce48e5aSMichal Kubecek 				rule_buf = kcalloc(info.rule_cnt, sizeof(u32),
8869ce48e5aSMichal Kubecek 						   GFP_USER);
8879ce48e5aSMichal Kubecek 			if (!rule_buf)
8889ce48e5aSMichal Kubecek 				return -ENOMEM;
8899ce48e5aSMichal Kubecek 		}
8909ce48e5aSMichal Kubecek 	}
8919ce48e5aSMichal Kubecek 
8929ce48e5aSMichal Kubecek 	ret = ops->get_rxnfc(dev, &info, rule_buf);
8939ce48e5aSMichal Kubecek 	if (ret < 0)
8949ce48e5aSMichal Kubecek 		goto err_out;
8959ce48e5aSMichal Kubecek 
8969ce48e5aSMichal Kubecek 	ret = -EFAULT;
8979ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &info, info_size))
8989ce48e5aSMichal Kubecek 		goto err_out;
8999ce48e5aSMichal Kubecek 
9009ce48e5aSMichal Kubecek 	if (rule_buf) {
9019ce48e5aSMichal Kubecek 		useraddr += offsetof(struct ethtool_rxnfc, rule_locs);
9029ce48e5aSMichal Kubecek 		if (copy_to_user(useraddr, rule_buf,
9039ce48e5aSMichal Kubecek 				 info.rule_cnt * sizeof(u32)))
9049ce48e5aSMichal Kubecek 			goto err_out;
9059ce48e5aSMichal Kubecek 	}
9069ce48e5aSMichal Kubecek 	ret = 0;
9079ce48e5aSMichal Kubecek 
9089ce48e5aSMichal Kubecek err_out:
9099ce48e5aSMichal Kubecek 	kfree(rule_buf);
9109ce48e5aSMichal Kubecek 
9119ce48e5aSMichal Kubecek 	return ret;
9129ce48e5aSMichal Kubecek }
9139ce48e5aSMichal Kubecek 
9149ce48e5aSMichal Kubecek static int ethtool_copy_validate_indir(u32 *indir, void __user *useraddr,
9159ce48e5aSMichal Kubecek 					struct ethtool_rxnfc *rx_rings,
9169ce48e5aSMichal Kubecek 					u32 size)
9179ce48e5aSMichal Kubecek {
9189ce48e5aSMichal Kubecek 	int i;
9199ce48e5aSMichal Kubecek 
9209ce48e5aSMichal Kubecek 	if (copy_from_user(indir, useraddr, size * sizeof(indir[0])))
9219ce48e5aSMichal Kubecek 		return -EFAULT;
9229ce48e5aSMichal Kubecek 
9239ce48e5aSMichal Kubecek 	/* Validate ring indices */
9249ce48e5aSMichal Kubecek 	for (i = 0; i < size; i++)
9259ce48e5aSMichal Kubecek 		if (indir[i] >= rx_rings->data)
9269ce48e5aSMichal Kubecek 			return -EINVAL;
9279ce48e5aSMichal Kubecek 
9289ce48e5aSMichal Kubecek 	return 0;
9299ce48e5aSMichal Kubecek }
9309ce48e5aSMichal Kubecek 
9319ce48e5aSMichal Kubecek u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
9329ce48e5aSMichal Kubecek 
9339ce48e5aSMichal Kubecek void netdev_rss_key_fill(void *buffer, size_t len)
9349ce48e5aSMichal Kubecek {
9359ce48e5aSMichal Kubecek 	BUG_ON(len > sizeof(netdev_rss_key));
9369ce48e5aSMichal Kubecek 	net_get_random_once(netdev_rss_key, sizeof(netdev_rss_key));
9379ce48e5aSMichal Kubecek 	memcpy(buffer, netdev_rss_key, len);
9389ce48e5aSMichal Kubecek }
9399ce48e5aSMichal Kubecek EXPORT_SYMBOL(netdev_rss_key_fill);
9409ce48e5aSMichal Kubecek 
9419ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_rxfh_indir(struct net_device *dev,
9429ce48e5aSMichal Kubecek 						     void __user *useraddr)
9439ce48e5aSMichal Kubecek {
9449ce48e5aSMichal Kubecek 	u32 user_size, dev_size;
9459ce48e5aSMichal Kubecek 	u32 *indir;
9469ce48e5aSMichal Kubecek 	int ret;
9479ce48e5aSMichal Kubecek 
9489ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_rxfh_indir_size ||
9499ce48e5aSMichal Kubecek 	    !dev->ethtool_ops->get_rxfh)
9509ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
9519ce48e5aSMichal Kubecek 	dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
9529ce48e5aSMichal Kubecek 	if (dev_size == 0)
9539ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
9549ce48e5aSMichal Kubecek 
9559ce48e5aSMichal Kubecek 	if (copy_from_user(&user_size,
9569ce48e5aSMichal Kubecek 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
9579ce48e5aSMichal Kubecek 			   sizeof(user_size)))
9589ce48e5aSMichal Kubecek 		return -EFAULT;
9599ce48e5aSMichal Kubecek 
9609ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh_indir, size),
9619ce48e5aSMichal Kubecek 			 &dev_size, sizeof(dev_size)))
9629ce48e5aSMichal Kubecek 		return -EFAULT;
9639ce48e5aSMichal Kubecek 
9649ce48e5aSMichal Kubecek 	/* If the user buffer size is 0, this is just a query for the
9659ce48e5aSMichal Kubecek 	 * device table size.  Otherwise, if it's smaller than the
9669ce48e5aSMichal Kubecek 	 * device table size it's an error.
9679ce48e5aSMichal Kubecek 	 */
9689ce48e5aSMichal Kubecek 	if (user_size < dev_size)
9699ce48e5aSMichal Kubecek 		return user_size == 0 ? 0 : -EINVAL;
9709ce48e5aSMichal Kubecek 
9719ce48e5aSMichal Kubecek 	indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
9729ce48e5aSMichal Kubecek 	if (!indir)
9739ce48e5aSMichal Kubecek 		return -ENOMEM;
9749ce48e5aSMichal Kubecek 
9759ce48e5aSMichal Kubecek 	ret = dev->ethtool_ops->get_rxfh(dev, indir, NULL, NULL);
9769ce48e5aSMichal Kubecek 	if (ret)
9779ce48e5aSMichal Kubecek 		goto out;
9789ce48e5aSMichal Kubecek 
9799ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr +
9809ce48e5aSMichal Kubecek 			 offsetof(struct ethtool_rxfh_indir, ring_index[0]),
9819ce48e5aSMichal Kubecek 			 indir, dev_size * sizeof(indir[0])))
9829ce48e5aSMichal Kubecek 		ret = -EFAULT;
9839ce48e5aSMichal Kubecek 
9849ce48e5aSMichal Kubecek out:
9859ce48e5aSMichal Kubecek 	kfree(indir);
9869ce48e5aSMichal Kubecek 	return ret;
9879ce48e5aSMichal Kubecek }
9889ce48e5aSMichal Kubecek 
9899ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_rxfh_indir(struct net_device *dev,
9909ce48e5aSMichal Kubecek 						     void __user *useraddr)
9919ce48e5aSMichal Kubecek {
9929ce48e5aSMichal Kubecek 	struct ethtool_rxnfc rx_rings;
9939ce48e5aSMichal Kubecek 	u32 user_size, dev_size, i;
9949ce48e5aSMichal Kubecek 	u32 *indir;
9959ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
9969ce48e5aSMichal Kubecek 	int ret;
9979ce48e5aSMichal Kubecek 	u32 ringidx_offset = offsetof(struct ethtool_rxfh_indir, ring_index[0]);
9989ce48e5aSMichal Kubecek 
9999ce48e5aSMichal Kubecek 	if (!ops->get_rxfh_indir_size || !ops->set_rxfh ||
10009ce48e5aSMichal Kubecek 	    !ops->get_rxnfc)
10019ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
10029ce48e5aSMichal Kubecek 
10039ce48e5aSMichal Kubecek 	dev_size = ops->get_rxfh_indir_size(dev);
10049ce48e5aSMichal Kubecek 	if (dev_size == 0)
10059ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
10069ce48e5aSMichal Kubecek 
10079ce48e5aSMichal Kubecek 	if (copy_from_user(&user_size,
10089ce48e5aSMichal Kubecek 			   useraddr + offsetof(struct ethtool_rxfh_indir, size),
10099ce48e5aSMichal Kubecek 			   sizeof(user_size)))
10109ce48e5aSMichal Kubecek 		return -EFAULT;
10119ce48e5aSMichal Kubecek 
10129ce48e5aSMichal Kubecek 	if (user_size != 0 && user_size != dev_size)
10139ce48e5aSMichal Kubecek 		return -EINVAL;
10149ce48e5aSMichal Kubecek 
10159ce48e5aSMichal Kubecek 	indir = kcalloc(dev_size, sizeof(indir[0]), GFP_USER);
10169ce48e5aSMichal Kubecek 	if (!indir)
10179ce48e5aSMichal Kubecek 		return -ENOMEM;
10189ce48e5aSMichal Kubecek 
10199ce48e5aSMichal Kubecek 	rx_rings.cmd = ETHTOOL_GRXRINGS;
10209ce48e5aSMichal Kubecek 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
10219ce48e5aSMichal Kubecek 	if (ret)
10229ce48e5aSMichal Kubecek 		goto out;
10239ce48e5aSMichal Kubecek 
10249ce48e5aSMichal Kubecek 	if (user_size == 0) {
10259ce48e5aSMichal Kubecek 		for (i = 0; i < dev_size; i++)
10269ce48e5aSMichal Kubecek 			indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
10279ce48e5aSMichal Kubecek 	} else {
10289ce48e5aSMichal Kubecek 		ret = ethtool_copy_validate_indir(indir,
10299ce48e5aSMichal Kubecek 						  useraddr + ringidx_offset,
10309ce48e5aSMichal Kubecek 						  &rx_rings,
10319ce48e5aSMichal Kubecek 						  dev_size);
10329ce48e5aSMichal Kubecek 		if (ret)
10339ce48e5aSMichal Kubecek 			goto out;
10349ce48e5aSMichal Kubecek 	}
10359ce48e5aSMichal Kubecek 
10369ce48e5aSMichal Kubecek 	ret = ops->set_rxfh(dev, indir, NULL, ETH_RSS_HASH_NO_CHANGE);
10379ce48e5aSMichal Kubecek 	if (ret)
10389ce48e5aSMichal Kubecek 		goto out;
10399ce48e5aSMichal Kubecek 
10409ce48e5aSMichal Kubecek 	/* indicate whether rxfh was set to default */
10419ce48e5aSMichal Kubecek 	if (user_size == 0)
10429ce48e5aSMichal Kubecek 		dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
10439ce48e5aSMichal Kubecek 	else
10449ce48e5aSMichal Kubecek 		dev->priv_flags |= IFF_RXFH_CONFIGURED;
10459ce48e5aSMichal Kubecek 
10469ce48e5aSMichal Kubecek out:
10479ce48e5aSMichal Kubecek 	kfree(indir);
10489ce48e5aSMichal Kubecek 	return ret;
10499ce48e5aSMichal Kubecek }
10509ce48e5aSMichal Kubecek 
10519ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_rxfh(struct net_device *dev,
10529ce48e5aSMichal Kubecek 					       void __user *useraddr)
10539ce48e5aSMichal Kubecek {
10549ce48e5aSMichal Kubecek 	int ret;
10559ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
10569ce48e5aSMichal Kubecek 	u32 user_indir_size, user_key_size;
10579ce48e5aSMichal Kubecek 	u32 dev_indir_size = 0, dev_key_size = 0;
10589ce48e5aSMichal Kubecek 	struct ethtool_rxfh rxfh;
10599ce48e5aSMichal Kubecek 	u32 total_size;
10609ce48e5aSMichal Kubecek 	u32 indir_bytes;
10619ce48e5aSMichal Kubecek 	u32 *indir = NULL;
10629ce48e5aSMichal Kubecek 	u8 dev_hfunc = 0;
10639ce48e5aSMichal Kubecek 	u8 *hkey = NULL;
10649ce48e5aSMichal Kubecek 	u8 *rss_config;
10659ce48e5aSMichal Kubecek 
10669ce48e5aSMichal Kubecek 	if (!ops->get_rxfh)
10679ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
10689ce48e5aSMichal Kubecek 
10699ce48e5aSMichal Kubecek 	if (ops->get_rxfh_indir_size)
10709ce48e5aSMichal Kubecek 		dev_indir_size = ops->get_rxfh_indir_size(dev);
10719ce48e5aSMichal Kubecek 	if (ops->get_rxfh_key_size)
10729ce48e5aSMichal Kubecek 		dev_key_size = ops->get_rxfh_key_size(dev);
10739ce48e5aSMichal Kubecek 
10749ce48e5aSMichal Kubecek 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
10759ce48e5aSMichal Kubecek 		return -EFAULT;
10769ce48e5aSMichal Kubecek 	user_indir_size = rxfh.indir_size;
10779ce48e5aSMichal Kubecek 	user_key_size = rxfh.key_size;
10789ce48e5aSMichal Kubecek 
10799ce48e5aSMichal Kubecek 	/* Check that reserved fields are 0 for now */
10809ce48e5aSMichal Kubecek 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
10819ce48e5aSMichal Kubecek 		return -EINVAL;
10829ce48e5aSMichal Kubecek 	/* Most drivers don't handle rss_context, check it's 0 as well */
10839ce48e5aSMichal Kubecek 	if (rxfh.rss_context && !ops->get_rxfh_context)
10849ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
10859ce48e5aSMichal Kubecek 
10869ce48e5aSMichal Kubecek 	rxfh.indir_size = dev_indir_size;
10879ce48e5aSMichal Kubecek 	rxfh.key_size = dev_key_size;
10889ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &rxfh, sizeof(rxfh)))
10899ce48e5aSMichal Kubecek 		return -EFAULT;
10909ce48e5aSMichal Kubecek 
10919ce48e5aSMichal Kubecek 	if ((user_indir_size && (user_indir_size != dev_indir_size)) ||
10929ce48e5aSMichal Kubecek 	    (user_key_size && (user_key_size != dev_key_size)))
10939ce48e5aSMichal Kubecek 		return -EINVAL;
10949ce48e5aSMichal Kubecek 
10959ce48e5aSMichal Kubecek 	indir_bytes = user_indir_size * sizeof(indir[0]);
10969ce48e5aSMichal Kubecek 	total_size = indir_bytes + user_key_size;
10979ce48e5aSMichal Kubecek 	rss_config = kzalloc(total_size, GFP_USER);
10989ce48e5aSMichal Kubecek 	if (!rss_config)
10999ce48e5aSMichal Kubecek 		return -ENOMEM;
11009ce48e5aSMichal Kubecek 
11019ce48e5aSMichal Kubecek 	if (user_indir_size)
11029ce48e5aSMichal Kubecek 		indir = (u32 *)rss_config;
11039ce48e5aSMichal Kubecek 
11049ce48e5aSMichal Kubecek 	if (user_key_size)
11059ce48e5aSMichal Kubecek 		hkey = rss_config + indir_bytes;
11069ce48e5aSMichal Kubecek 
11079ce48e5aSMichal Kubecek 	if (rxfh.rss_context)
11089ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_rxfh_context(dev, indir, hkey,
11099ce48e5aSMichal Kubecek 							 &dev_hfunc,
11109ce48e5aSMichal Kubecek 							 rxfh.rss_context);
11119ce48e5aSMichal Kubecek 	else
11129ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_rxfh(dev, indir, hkey, &dev_hfunc);
11139ce48e5aSMichal Kubecek 	if (ret)
11149ce48e5aSMichal Kubecek 		goto out;
11159ce48e5aSMichal Kubecek 
11169ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, hfunc),
11179ce48e5aSMichal Kubecek 			 &dev_hfunc, sizeof(rxfh.hfunc))) {
11189ce48e5aSMichal Kubecek 		ret = -EFAULT;
11199ce48e5aSMichal Kubecek 	} else if (copy_to_user(useraddr +
11209ce48e5aSMichal Kubecek 			      offsetof(struct ethtool_rxfh, rss_config[0]),
11219ce48e5aSMichal Kubecek 			      rss_config, total_size)) {
11229ce48e5aSMichal Kubecek 		ret = -EFAULT;
11239ce48e5aSMichal Kubecek 	}
11249ce48e5aSMichal Kubecek out:
11259ce48e5aSMichal Kubecek 	kfree(rss_config);
11269ce48e5aSMichal Kubecek 
11279ce48e5aSMichal Kubecek 	return ret;
11289ce48e5aSMichal Kubecek }
11299ce48e5aSMichal Kubecek 
11309ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
11319ce48e5aSMichal Kubecek 					       void __user *useraddr)
11329ce48e5aSMichal Kubecek {
11339ce48e5aSMichal Kubecek 	int ret;
11349ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
11359ce48e5aSMichal Kubecek 	struct ethtool_rxnfc rx_rings;
11369ce48e5aSMichal Kubecek 	struct ethtool_rxfh rxfh;
11379ce48e5aSMichal Kubecek 	u32 dev_indir_size = 0, dev_key_size = 0, i;
11389ce48e5aSMichal Kubecek 	u32 *indir = NULL, indir_bytes = 0;
11399ce48e5aSMichal Kubecek 	u8 *hkey = NULL;
11409ce48e5aSMichal Kubecek 	u8 *rss_config;
11419ce48e5aSMichal Kubecek 	u32 rss_cfg_offset = offsetof(struct ethtool_rxfh, rss_config[0]);
11429ce48e5aSMichal Kubecek 	bool delete = false;
11439ce48e5aSMichal Kubecek 
11449ce48e5aSMichal Kubecek 	if (!ops->get_rxnfc || !ops->set_rxfh)
11459ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
11469ce48e5aSMichal Kubecek 
11479ce48e5aSMichal Kubecek 	if (ops->get_rxfh_indir_size)
11489ce48e5aSMichal Kubecek 		dev_indir_size = ops->get_rxfh_indir_size(dev);
11499ce48e5aSMichal Kubecek 	if (ops->get_rxfh_key_size)
11509ce48e5aSMichal Kubecek 		dev_key_size = ops->get_rxfh_key_size(dev);
11519ce48e5aSMichal Kubecek 
11529ce48e5aSMichal Kubecek 	if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
11539ce48e5aSMichal Kubecek 		return -EFAULT;
11549ce48e5aSMichal Kubecek 
11559ce48e5aSMichal Kubecek 	/* Check that reserved fields are 0 for now */
11569ce48e5aSMichal Kubecek 	if (rxfh.rsvd8[0] || rxfh.rsvd8[1] || rxfh.rsvd8[2] || rxfh.rsvd32)
11579ce48e5aSMichal Kubecek 		return -EINVAL;
11589ce48e5aSMichal Kubecek 	/* Most drivers don't handle rss_context, check it's 0 as well */
11599ce48e5aSMichal Kubecek 	if (rxfh.rss_context && !ops->set_rxfh_context)
11609ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
11619ce48e5aSMichal Kubecek 
11629ce48e5aSMichal Kubecek 	/* If either indir, hash key or function is valid, proceed further.
11639ce48e5aSMichal Kubecek 	 * Must request at least one change: indir size, hash key or function.
11649ce48e5aSMichal Kubecek 	 */
11659ce48e5aSMichal Kubecek 	if ((rxfh.indir_size &&
11669ce48e5aSMichal Kubecek 	     rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE &&
11679ce48e5aSMichal Kubecek 	     rxfh.indir_size != dev_indir_size) ||
11689ce48e5aSMichal Kubecek 	    (rxfh.key_size && (rxfh.key_size != dev_key_size)) ||
11699ce48e5aSMichal Kubecek 	    (rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE &&
11709ce48e5aSMichal Kubecek 	     rxfh.key_size == 0 && rxfh.hfunc == ETH_RSS_HASH_NO_CHANGE))
11719ce48e5aSMichal Kubecek 		return -EINVAL;
11729ce48e5aSMichal Kubecek 
11739ce48e5aSMichal Kubecek 	if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
11749ce48e5aSMichal Kubecek 		indir_bytes = dev_indir_size * sizeof(indir[0]);
11759ce48e5aSMichal Kubecek 
11769ce48e5aSMichal Kubecek 	rss_config = kzalloc(indir_bytes + rxfh.key_size, GFP_USER);
11779ce48e5aSMichal Kubecek 	if (!rss_config)
11789ce48e5aSMichal Kubecek 		return -ENOMEM;
11799ce48e5aSMichal Kubecek 
11809ce48e5aSMichal Kubecek 	rx_rings.cmd = ETHTOOL_GRXRINGS;
11819ce48e5aSMichal Kubecek 	ret = ops->get_rxnfc(dev, &rx_rings, NULL);
11829ce48e5aSMichal Kubecek 	if (ret)
11839ce48e5aSMichal Kubecek 		goto out;
11849ce48e5aSMichal Kubecek 
11859ce48e5aSMichal Kubecek 	/* rxfh.indir_size == 0 means reset the indir table to default (master
11869ce48e5aSMichal Kubecek 	 * context) or delete the context (other RSS contexts).
11879ce48e5aSMichal Kubecek 	 * rxfh.indir_size == ETH_RXFH_INDIR_NO_CHANGE means leave it unchanged.
11889ce48e5aSMichal Kubecek 	 */
11899ce48e5aSMichal Kubecek 	if (rxfh.indir_size &&
11909ce48e5aSMichal Kubecek 	    rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE) {
11919ce48e5aSMichal Kubecek 		indir = (u32 *)rss_config;
11929ce48e5aSMichal Kubecek 		ret = ethtool_copy_validate_indir(indir,
11939ce48e5aSMichal Kubecek 						  useraddr + rss_cfg_offset,
11949ce48e5aSMichal Kubecek 						  &rx_rings,
11959ce48e5aSMichal Kubecek 						  rxfh.indir_size);
11969ce48e5aSMichal Kubecek 		if (ret)
11979ce48e5aSMichal Kubecek 			goto out;
11989ce48e5aSMichal Kubecek 	} else if (rxfh.indir_size == 0) {
11999ce48e5aSMichal Kubecek 		if (rxfh.rss_context == 0) {
12009ce48e5aSMichal Kubecek 			indir = (u32 *)rss_config;
12019ce48e5aSMichal Kubecek 			for (i = 0; i < dev_indir_size; i++)
12029ce48e5aSMichal Kubecek 				indir[i] = ethtool_rxfh_indir_default(i, rx_rings.data);
12039ce48e5aSMichal Kubecek 		} else {
12049ce48e5aSMichal Kubecek 			delete = true;
12059ce48e5aSMichal Kubecek 		}
12069ce48e5aSMichal Kubecek 	}
12079ce48e5aSMichal Kubecek 
12089ce48e5aSMichal Kubecek 	if (rxfh.key_size) {
12099ce48e5aSMichal Kubecek 		hkey = rss_config + indir_bytes;
12109ce48e5aSMichal Kubecek 		if (copy_from_user(hkey,
12119ce48e5aSMichal Kubecek 				   useraddr + rss_cfg_offset + indir_bytes,
12129ce48e5aSMichal Kubecek 				   rxfh.key_size)) {
12139ce48e5aSMichal Kubecek 			ret = -EFAULT;
12149ce48e5aSMichal Kubecek 			goto out;
12159ce48e5aSMichal Kubecek 		}
12169ce48e5aSMichal Kubecek 	}
12179ce48e5aSMichal Kubecek 
12189ce48e5aSMichal Kubecek 	if (rxfh.rss_context)
12199ce48e5aSMichal Kubecek 		ret = ops->set_rxfh_context(dev, indir, hkey, rxfh.hfunc,
12209ce48e5aSMichal Kubecek 					    &rxfh.rss_context, delete);
12219ce48e5aSMichal Kubecek 	else
12229ce48e5aSMichal Kubecek 		ret = ops->set_rxfh(dev, indir, hkey, rxfh.hfunc);
12239ce48e5aSMichal Kubecek 	if (ret)
12249ce48e5aSMichal Kubecek 		goto out;
12259ce48e5aSMichal Kubecek 
12269ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr + offsetof(struct ethtool_rxfh, rss_context),
12279ce48e5aSMichal Kubecek 			 &rxfh.rss_context, sizeof(rxfh.rss_context)))
12289ce48e5aSMichal Kubecek 		ret = -EFAULT;
12299ce48e5aSMichal Kubecek 
12309ce48e5aSMichal Kubecek 	if (!rxfh.rss_context) {
12319ce48e5aSMichal Kubecek 		/* indicate whether rxfh was set to default */
12329ce48e5aSMichal Kubecek 		if (rxfh.indir_size == 0)
12339ce48e5aSMichal Kubecek 			dev->priv_flags &= ~IFF_RXFH_CONFIGURED;
12349ce48e5aSMichal Kubecek 		else if (rxfh.indir_size != ETH_RXFH_INDIR_NO_CHANGE)
12359ce48e5aSMichal Kubecek 			dev->priv_flags |= IFF_RXFH_CONFIGURED;
12369ce48e5aSMichal Kubecek 	}
12379ce48e5aSMichal Kubecek 
12389ce48e5aSMichal Kubecek out:
12399ce48e5aSMichal Kubecek 	kfree(rss_config);
12409ce48e5aSMichal Kubecek 	return ret;
12419ce48e5aSMichal Kubecek }
12429ce48e5aSMichal Kubecek 
12439ce48e5aSMichal Kubecek static int ethtool_get_regs(struct net_device *dev, char __user *useraddr)
12449ce48e5aSMichal Kubecek {
12459ce48e5aSMichal Kubecek 	struct ethtool_regs regs;
12469ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
12479ce48e5aSMichal Kubecek 	void *regbuf;
12489ce48e5aSMichal Kubecek 	int reglen, ret;
12499ce48e5aSMichal Kubecek 
12509ce48e5aSMichal Kubecek 	if (!ops->get_regs || !ops->get_regs_len)
12519ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
12529ce48e5aSMichal Kubecek 
12539ce48e5aSMichal Kubecek 	if (copy_from_user(&regs, useraddr, sizeof(regs)))
12549ce48e5aSMichal Kubecek 		return -EFAULT;
12559ce48e5aSMichal Kubecek 
12569ce48e5aSMichal Kubecek 	reglen = ops->get_regs_len(dev);
12579ce48e5aSMichal Kubecek 	if (reglen <= 0)
12589ce48e5aSMichal Kubecek 		return reglen;
12599ce48e5aSMichal Kubecek 
12609ce48e5aSMichal Kubecek 	if (regs.len > reglen)
12619ce48e5aSMichal Kubecek 		regs.len = reglen;
12629ce48e5aSMichal Kubecek 
12639ce48e5aSMichal Kubecek 	regbuf = vzalloc(reglen);
12649ce48e5aSMichal Kubecek 	if (!regbuf)
12659ce48e5aSMichal Kubecek 		return -ENOMEM;
12669ce48e5aSMichal Kubecek 
12679ce48e5aSMichal Kubecek 	if (regs.len < reglen)
12689ce48e5aSMichal Kubecek 		reglen = regs.len;
12699ce48e5aSMichal Kubecek 
12709ce48e5aSMichal Kubecek 	ops->get_regs(dev, &regs, regbuf);
12719ce48e5aSMichal Kubecek 
12729ce48e5aSMichal Kubecek 	ret = -EFAULT;
12739ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &regs, sizeof(regs)))
12749ce48e5aSMichal Kubecek 		goto out;
12759ce48e5aSMichal Kubecek 	useraddr += offsetof(struct ethtool_regs, data);
12769ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, regbuf, reglen))
12779ce48e5aSMichal Kubecek 		goto out;
12789ce48e5aSMichal Kubecek 	ret = 0;
12799ce48e5aSMichal Kubecek 
12809ce48e5aSMichal Kubecek  out:
12819ce48e5aSMichal Kubecek 	vfree(regbuf);
12829ce48e5aSMichal Kubecek 	return ret;
12839ce48e5aSMichal Kubecek }
12849ce48e5aSMichal Kubecek 
12859ce48e5aSMichal Kubecek static int ethtool_reset(struct net_device *dev, char __user *useraddr)
12869ce48e5aSMichal Kubecek {
12879ce48e5aSMichal Kubecek 	struct ethtool_value reset;
12889ce48e5aSMichal Kubecek 	int ret;
12899ce48e5aSMichal Kubecek 
12909ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->reset)
12919ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
12929ce48e5aSMichal Kubecek 
12939ce48e5aSMichal Kubecek 	if (copy_from_user(&reset, useraddr, sizeof(reset)))
12949ce48e5aSMichal Kubecek 		return -EFAULT;
12959ce48e5aSMichal Kubecek 
12969ce48e5aSMichal Kubecek 	ret = dev->ethtool_ops->reset(dev, &reset.data);
12979ce48e5aSMichal Kubecek 	if (ret)
12989ce48e5aSMichal Kubecek 		return ret;
12999ce48e5aSMichal Kubecek 
13009ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &reset, sizeof(reset)))
13019ce48e5aSMichal Kubecek 		return -EFAULT;
13029ce48e5aSMichal Kubecek 	return 0;
13039ce48e5aSMichal Kubecek }
13049ce48e5aSMichal Kubecek 
13059ce48e5aSMichal Kubecek static int ethtool_get_wol(struct net_device *dev, char __user *useraddr)
13069ce48e5aSMichal Kubecek {
13079ce48e5aSMichal Kubecek 	struct ethtool_wolinfo wol;
13089ce48e5aSMichal Kubecek 
13099ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_wol)
13109ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
13119ce48e5aSMichal Kubecek 
13129ce48e5aSMichal Kubecek 	memset(&wol, 0, sizeof(struct ethtool_wolinfo));
13139ce48e5aSMichal Kubecek 	wol.cmd = ETHTOOL_GWOL;
13149ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_wol(dev, &wol);
13159ce48e5aSMichal Kubecek 
13169ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &wol, sizeof(wol)))
13179ce48e5aSMichal Kubecek 		return -EFAULT;
13189ce48e5aSMichal Kubecek 	return 0;
13199ce48e5aSMichal Kubecek }
13209ce48e5aSMichal Kubecek 
13219ce48e5aSMichal Kubecek static int ethtool_set_wol(struct net_device *dev, char __user *useraddr)
13229ce48e5aSMichal Kubecek {
13239ce48e5aSMichal Kubecek 	struct ethtool_wolinfo wol;
13249ce48e5aSMichal Kubecek 	int ret;
13259ce48e5aSMichal Kubecek 
13269ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_wol)
13279ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
13289ce48e5aSMichal Kubecek 
13299ce48e5aSMichal Kubecek 	if (copy_from_user(&wol, useraddr, sizeof(wol)))
13309ce48e5aSMichal Kubecek 		return -EFAULT;
13319ce48e5aSMichal Kubecek 
13329ce48e5aSMichal Kubecek 	ret = dev->ethtool_ops->set_wol(dev, &wol);
13339ce48e5aSMichal Kubecek 	if (ret)
13349ce48e5aSMichal Kubecek 		return ret;
13359ce48e5aSMichal Kubecek 
13369ce48e5aSMichal Kubecek 	dev->wol_enabled = !!wol.wolopts;
133767bffa79SMichal Kubecek 	ethtool_notify(dev, ETHTOOL_MSG_WOL_NTF, NULL);
13389ce48e5aSMichal Kubecek 
13399ce48e5aSMichal Kubecek 	return 0;
13409ce48e5aSMichal Kubecek }
13419ce48e5aSMichal Kubecek 
13429ce48e5aSMichal Kubecek static int ethtool_get_eee(struct net_device *dev, char __user *useraddr)
13439ce48e5aSMichal Kubecek {
13449ce48e5aSMichal Kubecek 	struct ethtool_eee edata;
13459ce48e5aSMichal Kubecek 	int rc;
13469ce48e5aSMichal Kubecek 
13479ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_eee)
13489ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
13499ce48e5aSMichal Kubecek 
13509ce48e5aSMichal Kubecek 	memset(&edata, 0, sizeof(struct ethtool_eee));
13519ce48e5aSMichal Kubecek 	edata.cmd = ETHTOOL_GEEE;
13529ce48e5aSMichal Kubecek 	rc = dev->ethtool_ops->get_eee(dev, &edata);
13539ce48e5aSMichal Kubecek 
13549ce48e5aSMichal Kubecek 	if (rc)
13559ce48e5aSMichal Kubecek 		return rc;
13569ce48e5aSMichal Kubecek 
13579ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
13589ce48e5aSMichal Kubecek 		return -EFAULT;
13599ce48e5aSMichal Kubecek 
13609ce48e5aSMichal Kubecek 	return 0;
13619ce48e5aSMichal Kubecek }
13629ce48e5aSMichal Kubecek 
13639ce48e5aSMichal Kubecek static int ethtool_set_eee(struct net_device *dev, char __user *useraddr)
13649ce48e5aSMichal Kubecek {
13659ce48e5aSMichal Kubecek 	struct ethtool_eee edata;
13666c5bc8feSMichal Kubecek 	int ret;
13679ce48e5aSMichal Kubecek 
13689ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_eee)
13699ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
13709ce48e5aSMichal Kubecek 
13719ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
13729ce48e5aSMichal Kubecek 		return -EFAULT;
13739ce48e5aSMichal Kubecek 
13746c5bc8feSMichal Kubecek 	ret = dev->ethtool_ops->set_eee(dev, &edata);
13756c5bc8feSMichal Kubecek 	if (!ret)
13766c5bc8feSMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_EEE_NTF, NULL);
13776c5bc8feSMichal Kubecek 	return ret;
13789ce48e5aSMichal Kubecek }
13799ce48e5aSMichal Kubecek 
13809ce48e5aSMichal Kubecek static int ethtool_nway_reset(struct net_device *dev)
13819ce48e5aSMichal Kubecek {
13829ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->nway_reset)
13839ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
13849ce48e5aSMichal Kubecek 
13859ce48e5aSMichal Kubecek 	return dev->ethtool_ops->nway_reset(dev);
13869ce48e5aSMichal Kubecek }
13879ce48e5aSMichal Kubecek 
13889ce48e5aSMichal Kubecek static int ethtool_get_link(struct net_device *dev, char __user *useraddr)
13899ce48e5aSMichal Kubecek {
13909ce48e5aSMichal Kubecek 	struct ethtool_value edata = { .cmd = ETHTOOL_GLINK };
13913d2b847fSMichal Kubecek 	int link = __ethtool_get_link(dev);
13929ce48e5aSMichal Kubecek 
13933d2b847fSMichal Kubecek 	if (link < 0)
13943d2b847fSMichal Kubecek 		return link;
13959ce48e5aSMichal Kubecek 
13963d2b847fSMichal Kubecek 	edata.data = link;
13979ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
13989ce48e5aSMichal Kubecek 		return -EFAULT;
13999ce48e5aSMichal Kubecek 	return 0;
14009ce48e5aSMichal Kubecek }
14019ce48e5aSMichal Kubecek 
14029ce48e5aSMichal Kubecek static int ethtool_get_any_eeprom(struct net_device *dev, void __user *useraddr,
14039ce48e5aSMichal Kubecek 				  int (*getter)(struct net_device *,
14049ce48e5aSMichal Kubecek 						struct ethtool_eeprom *, u8 *),
14059ce48e5aSMichal Kubecek 				  u32 total_len)
14069ce48e5aSMichal Kubecek {
14079ce48e5aSMichal Kubecek 	struct ethtool_eeprom eeprom;
14089ce48e5aSMichal Kubecek 	void __user *userbuf = useraddr + sizeof(eeprom);
14099ce48e5aSMichal Kubecek 	u32 bytes_remaining;
14109ce48e5aSMichal Kubecek 	u8 *data;
14119ce48e5aSMichal Kubecek 	int ret = 0;
14129ce48e5aSMichal Kubecek 
14139ce48e5aSMichal Kubecek 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
14149ce48e5aSMichal Kubecek 		return -EFAULT;
14159ce48e5aSMichal Kubecek 
14169ce48e5aSMichal Kubecek 	/* Check for wrap and zero */
14179ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len <= eeprom.offset)
14189ce48e5aSMichal Kubecek 		return -EINVAL;
14199ce48e5aSMichal Kubecek 
14209ce48e5aSMichal Kubecek 	/* Check for exceeding total eeprom len */
14219ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len > total_len)
14229ce48e5aSMichal Kubecek 		return -EINVAL;
14239ce48e5aSMichal Kubecek 
1424*80ec82e3SAustin Kim 	data = kzalloc(PAGE_SIZE, GFP_USER);
14259ce48e5aSMichal Kubecek 	if (!data)
14269ce48e5aSMichal Kubecek 		return -ENOMEM;
14279ce48e5aSMichal Kubecek 
14289ce48e5aSMichal Kubecek 	bytes_remaining = eeprom.len;
14299ce48e5aSMichal Kubecek 	while (bytes_remaining > 0) {
14309ce48e5aSMichal Kubecek 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
14319ce48e5aSMichal Kubecek 
14329ce48e5aSMichal Kubecek 		ret = getter(dev, &eeprom, data);
14339ce48e5aSMichal Kubecek 		if (ret)
14349ce48e5aSMichal Kubecek 			break;
14359ce48e5aSMichal Kubecek 		if (copy_to_user(userbuf, data, eeprom.len)) {
14369ce48e5aSMichal Kubecek 			ret = -EFAULT;
14379ce48e5aSMichal Kubecek 			break;
14389ce48e5aSMichal Kubecek 		}
14399ce48e5aSMichal Kubecek 		userbuf += eeprom.len;
14409ce48e5aSMichal Kubecek 		eeprom.offset += eeprom.len;
14419ce48e5aSMichal Kubecek 		bytes_remaining -= eeprom.len;
14429ce48e5aSMichal Kubecek 	}
14439ce48e5aSMichal Kubecek 
14449ce48e5aSMichal Kubecek 	eeprom.len = userbuf - (useraddr + sizeof(eeprom));
14459ce48e5aSMichal Kubecek 	eeprom.offset -= eeprom.len;
14469ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &eeprom, sizeof(eeprom)))
14479ce48e5aSMichal Kubecek 		ret = -EFAULT;
14489ce48e5aSMichal Kubecek 
14499ce48e5aSMichal Kubecek 	kfree(data);
14509ce48e5aSMichal Kubecek 	return ret;
14519ce48e5aSMichal Kubecek }
14529ce48e5aSMichal Kubecek 
14539ce48e5aSMichal Kubecek static int ethtool_get_eeprom(struct net_device *dev, void __user *useraddr)
14549ce48e5aSMichal Kubecek {
14559ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
14569ce48e5aSMichal Kubecek 
14579ce48e5aSMichal Kubecek 	if (!ops->get_eeprom || !ops->get_eeprom_len ||
14589ce48e5aSMichal Kubecek 	    !ops->get_eeprom_len(dev))
14599ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
14609ce48e5aSMichal Kubecek 
14619ce48e5aSMichal Kubecek 	return ethtool_get_any_eeprom(dev, useraddr, ops->get_eeprom,
14629ce48e5aSMichal Kubecek 				      ops->get_eeprom_len(dev));
14639ce48e5aSMichal Kubecek }
14649ce48e5aSMichal Kubecek 
14659ce48e5aSMichal Kubecek static int ethtool_set_eeprom(struct net_device *dev, void __user *useraddr)
14669ce48e5aSMichal Kubecek {
14679ce48e5aSMichal Kubecek 	struct ethtool_eeprom eeprom;
14689ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
14699ce48e5aSMichal Kubecek 	void __user *userbuf = useraddr + sizeof(eeprom);
14709ce48e5aSMichal Kubecek 	u32 bytes_remaining;
14719ce48e5aSMichal Kubecek 	u8 *data;
14729ce48e5aSMichal Kubecek 	int ret = 0;
14739ce48e5aSMichal Kubecek 
14749ce48e5aSMichal Kubecek 	if (!ops->set_eeprom || !ops->get_eeprom_len ||
14759ce48e5aSMichal Kubecek 	    !ops->get_eeprom_len(dev))
14769ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
14779ce48e5aSMichal Kubecek 
14789ce48e5aSMichal Kubecek 	if (copy_from_user(&eeprom, useraddr, sizeof(eeprom)))
14799ce48e5aSMichal Kubecek 		return -EFAULT;
14809ce48e5aSMichal Kubecek 
14819ce48e5aSMichal Kubecek 	/* Check for wrap and zero */
14829ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len <= eeprom.offset)
14839ce48e5aSMichal Kubecek 		return -EINVAL;
14849ce48e5aSMichal Kubecek 
14859ce48e5aSMichal Kubecek 	/* Check for exceeding total eeprom len */
14869ce48e5aSMichal Kubecek 	if (eeprom.offset + eeprom.len > ops->get_eeprom_len(dev))
14879ce48e5aSMichal Kubecek 		return -EINVAL;
14889ce48e5aSMichal Kubecek 
1489*80ec82e3SAustin Kim 	data = kzalloc(PAGE_SIZE, GFP_USER);
14909ce48e5aSMichal Kubecek 	if (!data)
14919ce48e5aSMichal Kubecek 		return -ENOMEM;
14929ce48e5aSMichal Kubecek 
14939ce48e5aSMichal Kubecek 	bytes_remaining = eeprom.len;
14949ce48e5aSMichal Kubecek 	while (bytes_remaining > 0) {
14959ce48e5aSMichal Kubecek 		eeprom.len = min(bytes_remaining, (u32)PAGE_SIZE);
14969ce48e5aSMichal Kubecek 
14979ce48e5aSMichal Kubecek 		if (copy_from_user(data, userbuf, eeprom.len)) {
14989ce48e5aSMichal Kubecek 			ret = -EFAULT;
14999ce48e5aSMichal Kubecek 			break;
15009ce48e5aSMichal Kubecek 		}
15019ce48e5aSMichal Kubecek 		ret = ops->set_eeprom(dev, &eeprom, data);
15029ce48e5aSMichal Kubecek 		if (ret)
15039ce48e5aSMichal Kubecek 			break;
15049ce48e5aSMichal Kubecek 		userbuf += eeprom.len;
15059ce48e5aSMichal Kubecek 		eeprom.offset += eeprom.len;
15069ce48e5aSMichal Kubecek 		bytes_remaining -= eeprom.len;
15079ce48e5aSMichal Kubecek 	}
15089ce48e5aSMichal Kubecek 
15099ce48e5aSMichal Kubecek 	kfree(data);
15109ce48e5aSMichal Kubecek 	return ret;
15119ce48e5aSMichal Kubecek }
15129ce48e5aSMichal Kubecek 
15139ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_coalesce(struct net_device *dev,
15149ce48e5aSMichal Kubecek 						   void __user *useraddr)
15159ce48e5aSMichal Kubecek {
15169ce48e5aSMichal Kubecek 	struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
151731610711SHeiner Kallweit 	int ret;
15189ce48e5aSMichal Kubecek 
15199ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_coalesce)
15209ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
15219ce48e5aSMichal Kubecek 
152231610711SHeiner Kallweit 	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce);
152331610711SHeiner Kallweit 	if (ret)
152431610711SHeiner Kallweit 		return ret;
15259ce48e5aSMichal Kubecek 
15269ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
15279ce48e5aSMichal Kubecek 		return -EFAULT;
15289ce48e5aSMichal Kubecek 	return 0;
15299ce48e5aSMichal Kubecek }
15309ce48e5aSMichal Kubecek 
153195cddcb5SJakub Kicinski static bool
153295cddcb5SJakub Kicinski ethtool_set_coalesce_supported(struct net_device *dev,
153395cddcb5SJakub Kicinski 			       struct ethtool_coalesce *coalesce)
153495cddcb5SJakub Kicinski {
153595cddcb5SJakub Kicinski 	u32 supported_params = dev->ethtool_ops->supported_coalesce_params;
153695cddcb5SJakub Kicinski 	u32 nonzero_params = 0;
153795cddcb5SJakub Kicinski 
153895cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs)
153995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS;
154095cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames)
154195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES;
154295cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs_irq)
154395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_IRQ;
154495cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames_irq)
154595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_IRQ;
154695cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs)
154795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS;
154895cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames)
154995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES;
155095cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs_irq)
155195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_IRQ;
155295cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames_irq)
155395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_IRQ;
155495cddcb5SJakub Kicinski 	if (coalesce->stats_block_coalesce_usecs)
155595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_STATS_BLOCK_USECS;
155695cddcb5SJakub Kicinski 	if (coalesce->use_adaptive_rx_coalesce)
155795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_RX;
155895cddcb5SJakub Kicinski 	if (coalesce->use_adaptive_tx_coalesce)
155995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_USE_ADAPTIVE_TX;
156095cddcb5SJakub Kicinski 	if (coalesce->pkt_rate_low)
156195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_LOW;
156295cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs_low)
156395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_LOW;
156495cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames_low)
156595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_LOW;
156695cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs_low)
156795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_LOW;
156895cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames_low)
156995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_LOW;
157095cddcb5SJakub Kicinski 	if (coalesce->pkt_rate_high)
157195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_PKT_RATE_HIGH;
157295cddcb5SJakub Kicinski 	if (coalesce->rx_coalesce_usecs_high)
157395cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_USECS_HIGH;
157495cddcb5SJakub Kicinski 	if (coalesce->rx_max_coalesced_frames_high)
157595cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RX_MAX_FRAMES_HIGH;
157695cddcb5SJakub Kicinski 	if (coalesce->tx_coalesce_usecs_high)
157795cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_USECS_HIGH;
157895cddcb5SJakub Kicinski 	if (coalesce->tx_max_coalesced_frames_high)
157995cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_TX_MAX_FRAMES_HIGH;
158095cddcb5SJakub Kicinski 	if (coalesce->rate_sample_interval)
158195cddcb5SJakub Kicinski 		nonzero_params |= ETHTOOL_COALESCE_RATE_SAMPLE_INTERVAL;
158295cddcb5SJakub Kicinski 
158395cddcb5SJakub Kicinski 	return (supported_params & nonzero_params) == nonzero_params;
158495cddcb5SJakub Kicinski }
158595cddcb5SJakub Kicinski 
15869ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_coalesce(struct net_device *dev,
15879ce48e5aSMichal Kubecek 						   void __user *useraddr)
15889ce48e5aSMichal Kubecek {
15899ce48e5aSMichal Kubecek 	struct ethtool_coalesce coalesce;
15900cf3eac8SMichal Kubecek 	int ret;
15919ce48e5aSMichal Kubecek 
15929ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_coalesce)
15939ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
15949ce48e5aSMichal Kubecek 
15959ce48e5aSMichal Kubecek 	if (copy_from_user(&coalesce, useraddr, sizeof(coalesce)))
15969ce48e5aSMichal Kubecek 		return -EFAULT;
15979ce48e5aSMichal Kubecek 
159895cddcb5SJakub Kicinski 	if (!ethtool_set_coalesce_supported(dev, &coalesce))
159995cddcb5SJakub Kicinski 		return -EOPNOTSUPP;
160095cddcb5SJakub Kicinski 
16010cf3eac8SMichal Kubecek 	ret = dev->ethtool_ops->set_coalesce(dev, &coalesce);
16020cf3eac8SMichal Kubecek 	if (!ret)
16030cf3eac8SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_COALESCE_NTF, NULL);
16040cf3eac8SMichal Kubecek 	return ret;
16059ce48e5aSMichal Kubecek }
16069ce48e5aSMichal Kubecek 
16079ce48e5aSMichal Kubecek static int ethtool_get_ringparam(struct net_device *dev, void __user *useraddr)
16089ce48e5aSMichal Kubecek {
16099ce48e5aSMichal Kubecek 	struct ethtool_ringparam ringparam = { .cmd = ETHTOOL_GRINGPARAM };
16109ce48e5aSMichal Kubecek 
16119ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_ringparam)
16129ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
16139ce48e5aSMichal Kubecek 
16149ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_ringparam(dev, &ringparam);
16159ce48e5aSMichal Kubecek 
16169ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &ringparam, sizeof(ringparam)))
16179ce48e5aSMichal Kubecek 		return -EFAULT;
16189ce48e5aSMichal Kubecek 	return 0;
16199ce48e5aSMichal Kubecek }
16209ce48e5aSMichal Kubecek 
16219ce48e5aSMichal Kubecek static int ethtool_set_ringparam(struct net_device *dev, void __user *useraddr)
16229ce48e5aSMichal Kubecek {
16239ce48e5aSMichal Kubecek 	struct ethtool_ringparam ringparam, max = { .cmd = ETHTOOL_GRINGPARAM };
1624bc9d1c99SMichal Kubecek 	int ret;
16259ce48e5aSMichal Kubecek 
16269ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_ringparam || !dev->ethtool_ops->get_ringparam)
16279ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
16289ce48e5aSMichal Kubecek 
16299ce48e5aSMichal Kubecek 	if (copy_from_user(&ringparam, useraddr, sizeof(ringparam)))
16309ce48e5aSMichal Kubecek 		return -EFAULT;
16319ce48e5aSMichal Kubecek 
16329ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_ringparam(dev, &max);
16339ce48e5aSMichal Kubecek 
16349ce48e5aSMichal Kubecek 	/* ensure new ring parameters are within the maximums */
16359ce48e5aSMichal Kubecek 	if (ringparam.rx_pending > max.rx_max_pending ||
16369ce48e5aSMichal Kubecek 	    ringparam.rx_mini_pending > max.rx_mini_max_pending ||
16379ce48e5aSMichal Kubecek 	    ringparam.rx_jumbo_pending > max.rx_jumbo_max_pending ||
16389ce48e5aSMichal Kubecek 	    ringparam.tx_pending > max.tx_max_pending)
16399ce48e5aSMichal Kubecek 		return -EINVAL;
16409ce48e5aSMichal Kubecek 
1641bc9d1c99SMichal Kubecek 	ret = dev->ethtool_ops->set_ringparam(dev, &ringparam);
1642bc9d1c99SMichal Kubecek 	if (!ret)
1643bc9d1c99SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_RINGS_NTF, NULL);
1644bc9d1c99SMichal Kubecek 	return ret;
16459ce48e5aSMichal Kubecek }
16469ce48e5aSMichal Kubecek 
16479ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_get_channels(struct net_device *dev,
16489ce48e5aSMichal Kubecek 						   void __user *useraddr)
16499ce48e5aSMichal Kubecek {
16509ce48e5aSMichal Kubecek 	struct ethtool_channels channels = { .cmd = ETHTOOL_GCHANNELS };
16519ce48e5aSMichal Kubecek 
16529ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_channels)
16539ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
16549ce48e5aSMichal Kubecek 
16559ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_channels(dev, &channels);
16569ce48e5aSMichal Kubecek 
16579ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &channels, sizeof(channels)))
16589ce48e5aSMichal Kubecek 		return -EFAULT;
16599ce48e5aSMichal Kubecek 	return 0;
16609ce48e5aSMichal Kubecek }
16619ce48e5aSMichal Kubecek 
16629ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_set_channels(struct net_device *dev,
16639ce48e5aSMichal Kubecek 						   void __user *useraddr)
16649ce48e5aSMichal Kubecek {
16659ce48e5aSMichal Kubecek 	struct ethtool_channels channels, curr = { .cmd = ETHTOOL_GCHANNELS };
16669ce48e5aSMichal Kubecek 	u16 from_channel, to_channel;
16679ce48e5aSMichal Kubecek 	u32 max_rx_in_use = 0;
16689ce48e5aSMichal Kubecek 	unsigned int i;
1669546379b9SMichal Kubecek 	int ret;
16709ce48e5aSMichal Kubecek 
16719ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_channels || !dev->ethtool_ops->get_channels)
16729ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
16739ce48e5aSMichal Kubecek 
16749ce48e5aSMichal Kubecek 	if (copy_from_user(&channels, useraddr, sizeof(channels)))
16759ce48e5aSMichal Kubecek 		return -EFAULT;
16769ce48e5aSMichal Kubecek 
16779ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_channels(dev, &curr);
16789ce48e5aSMichal Kubecek 
167975c36dbbSJakub Kicinski 	if (channels.rx_count == curr.rx_count &&
168075c36dbbSJakub Kicinski 	    channels.tx_count == curr.tx_count &&
168175c36dbbSJakub Kicinski 	    channels.combined_count == curr.combined_count &&
168275c36dbbSJakub Kicinski 	    channels.other_count == curr.other_count)
168375c36dbbSJakub Kicinski 		return 0;
168475c36dbbSJakub Kicinski 
16859ce48e5aSMichal Kubecek 	/* ensure new counts are within the maximums */
16869ce48e5aSMichal Kubecek 	if (channels.rx_count > curr.max_rx ||
16879ce48e5aSMichal Kubecek 	    channels.tx_count > curr.max_tx ||
16889ce48e5aSMichal Kubecek 	    channels.combined_count > curr.max_combined ||
16899ce48e5aSMichal Kubecek 	    channels.other_count > curr.max_other)
16909ce48e5aSMichal Kubecek 		return -EINVAL;
16919ce48e5aSMichal Kubecek 
16927be92514SJakub Kicinski 	/* ensure there is at least one RX and one TX channel */
16937be92514SJakub Kicinski 	if (!channels.combined_count &&
16947be92514SJakub Kicinski 	    (!channels.rx_count || !channels.tx_count))
16957be92514SJakub Kicinski 		return -EINVAL;
16967be92514SJakub Kicinski 
16979ce48e5aSMichal Kubecek 	/* ensure the new Rx count fits within the configured Rx flow
16989ce48e5aSMichal Kubecek 	 * indirection table settings */
16999ce48e5aSMichal Kubecek 	if (netif_is_rxfh_configured(dev) &&
17009ce48e5aSMichal Kubecek 	    !ethtool_get_max_rxfh_channel(dev, &max_rx_in_use) &&
17019ce48e5aSMichal Kubecek 	    (channels.combined_count + channels.rx_count) <= max_rx_in_use)
17029ce48e5aSMichal Kubecek 	    return -EINVAL;
17039ce48e5aSMichal Kubecek 
17049ce48e5aSMichal Kubecek 	/* Disabling channels, query zero-copy AF_XDP sockets */
17059ce48e5aSMichal Kubecek 	from_channel = channels.combined_count +
17069ce48e5aSMichal Kubecek 		min(channels.rx_count, channels.tx_count);
17079ce48e5aSMichal Kubecek 	to_channel = curr.combined_count + max(curr.rx_count, curr.tx_count);
17089ce48e5aSMichal Kubecek 	for (i = from_channel; i < to_channel; i++)
1709c4655761SMagnus Karlsson 		if (xsk_get_pool_from_qid(dev, i))
17109ce48e5aSMichal Kubecek 			return -EINVAL;
17119ce48e5aSMichal Kubecek 
1712546379b9SMichal Kubecek 	ret = dev->ethtool_ops->set_channels(dev, &channels);
1713546379b9SMichal Kubecek 	if (!ret)
1714546379b9SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_CHANNELS_NTF, NULL);
1715546379b9SMichal Kubecek 	return ret;
17169ce48e5aSMichal Kubecek }
17179ce48e5aSMichal Kubecek 
17189ce48e5aSMichal Kubecek static int ethtool_get_pauseparam(struct net_device *dev, void __user *useraddr)
17199ce48e5aSMichal Kubecek {
17209ce48e5aSMichal Kubecek 	struct ethtool_pauseparam pauseparam = { .cmd = ETHTOOL_GPAUSEPARAM };
17219ce48e5aSMichal Kubecek 
17229ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_pauseparam)
17239ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
17249ce48e5aSMichal Kubecek 
17259ce48e5aSMichal Kubecek 	dev->ethtool_ops->get_pauseparam(dev, &pauseparam);
17269ce48e5aSMichal Kubecek 
17279ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &pauseparam, sizeof(pauseparam)))
17289ce48e5aSMichal Kubecek 		return -EFAULT;
17299ce48e5aSMichal Kubecek 	return 0;
17309ce48e5aSMichal Kubecek }
17319ce48e5aSMichal Kubecek 
17329ce48e5aSMichal Kubecek static int ethtool_set_pauseparam(struct net_device *dev, void __user *useraddr)
17339ce48e5aSMichal Kubecek {
17349ce48e5aSMichal Kubecek 	struct ethtool_pauseparam pauseparam;
1735bf37faa3SMichal Kubecek 	int ret;
17369ce48e5aSMichal Kubecek 
17379ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_pauseparam)
17389ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
17399ce48e5aSMichal Kubecek 
17409ce48e5aSMichal Kubecek 	if (copy_from_user(&pauseparam, useraddr, sizeof(pauseparam)))
17419ce48e5aSMichal Kubecek 		return -EFAULT;
17429ce48e5aSMichal Kubecek 
1743bf37faa3SMichal Kubecek 	ret = dev->ethtool_ops->set_pauseparam(dev, &pauseparam);
1744bf37faa3SMichal Kubecek 	if (!ret)
1745bf37faa3SMichal Kubecek 		ethtool_notify(dev, ETHTOOL_MSG_PAUSE_NTF, NULL);
1746bf37faa3SMichal Kubecek 	return ret;
17479ce48e5aSMichal Kubecek }
17489ce48e5aSMichal Kubecek 
17499ce48e5aSMichal Kubecek static int ethtool_self_test(struct net_device *dev, char __user *useraddr)
17509ce48e5aSMichal Kubecek {
17519ce48e5aSMichal Kubecek 	struct ethtool_test test;
17529ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
17539ce48e5aSMichal Kubecek 	u64 *data;
17549ce48e5aSMichal Kubecek 	int ret, test_len;
17559ce48e5aSMichal Kubecek 
17569ce48e5aSMichal Kubecek 	if (!ops->self_test || !ops->get_sset_count)
17579ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
17589ce48e5aSMichal Kubecek 
17599ce48e5aSMichal Kubecek 	test_len = ops->get_sset_count(dev, ETH_SS_TEST);
17609ce48e5aSMichal Kubecek 	if (test_len < 0)
17619ce48e5aSMichal Kubecek 		return test_len;
17629ce48e5aSMichal Kubecek 	WARN_ON(test_len == 0);
17639ce48e5aSMichal Kubecek 
17649ce48e5aSMichal Kubecek 	if (copy_from_user(&test, useraddr, sizeof(test)))
17659ce48e5aSMichal Kubecek 		return -EFAULT;
17669ce48e5aSMichal Kubecek 
17679ce48e5aSMichal Kubecek 	test.len = test_len;
1768*80ec82e3SAustin Kim 	data = kcalloc(test_len, sizeof(u64), GFP_USER);
17699ce48e5aSMichal Kubecek 	if (!data)
17709ce48e5aSMichal Kubecek 		return -ENOMEM;
17719ce48e5aSMichal Kubecek 
177277e9b2abSAndrew Lunn 	netif_testing_on(dev);
17739ce48e5aSMichal Kubecek 	ops->self_test(dev, &test, data);
177477e9b2abSAndrew Lunn 	netif_testing_off(dev);
17759ce48e5aSMichal Kubecek 
17769ce48e5aSMichal Kubecek 	ret = -EFAULT;
17779ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &test, sizeof(test)))
17789ce48e5aSMichal Kubecek 		goto out;
17799ce48e5aSMichal Kubecek 	useraddr += sizeof(test);
17809ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, data, test.len * sizeof(u64)))
17819ce48e5aSMichal Kubecek 		goto out;
17829ce48e5aSMichal Kubecek 	ret = 0;
17839ce48e5aSMichal Kubecek 
17849ce48e5aSMichal Kubecek  out:
17859ce48e5aSMichal Kubecek 	kfree(data);
17869ce48e5aSMichal Kubecek 	return ret;
17879ce48e5aSMichal Kubecek }
17889ce48e5aSMichal Kubecek 
17899ce48e5aSMichal Kubecek static int ethtool_get_strings(struct net_device *dev, void __user *useraddr)
17909ce48e5aSMichal Kubecek {
17919ce48e5aSMichal Kubecek 	struct ethtool_gstrings gstrings;
17929ce48e5aSMichal Kubecek 	u8 *data;
17939ce48e5aSMichal Kubecek 	int ret;
17949ce48e5aSMichal Kubecek 
17959ce48e5aSMichal Kubecek 	if (copy_from_user(&gstrings, useraddr, sizeof(gstrings)))
17969ce48e5aSMichal Kubecek 		return -EFAULT;
17979ce48e5aSMichal Kubecek 
17989ce48e5aSMichal Kubecek 	ret = __ethtool_get_sset_count(dev, gstrings.string_set);
17999ce48e5aSMichal Kubecek 	if (ret < 0)
18009ce48e5aSMichal Kubecek 		return ret;
18019ce48e5aSMichal Kubecek 	if (ret > S32_MAX / ETH_GSTRING_LEN)
18029ce48e5aSMichal Kubecek 		return -ENOMEM;
18039ce48e5aSMichal Kubecek 	WARN_ON_ONCE(!ret);
18049ce48e5aSMichal Kubecek 
18059ce48e5aSMichal Kubecek 	gstrings.len = ret;
18069ce48e5aSMichal Kubecek 
18079ce48e5aSMichal Kubecek 	if (gstrings.len) {
18089ce48e5aSMichal Kubecek 		data = vzalloc(array_size(gstrings.len, ETH_GSTRING_LEN));
18099ce48e5aSMichal Kubecek 		if (!data)
18109ce48e5aSMichal Kubecek 			return -ENOMEM;
18119ce48e5aSMichal Kubecek 
18129ce48e5aSMichal Kubecek 		__ethtool_get_strings(dev, gstrings.string_set, data);
18139ce48e5aSMichal Kubecek 	} else {
18149ce48e5aSMichal Kubecek 		data = NULL;
18159ce48e5aSMichal Kubecek 	}
18169ce48e5aSMichal Kubecek 
18179ce48e5aSMichal Kubecek 	ret = -EFAULT;
18189ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &gstrings, sizeof(gstrings)))
18199ce48e5aSMichal Kubecek 		goto out;
18209ce48e5aSMichal Kubecek 	useraddr += sizeof(gstrings);
18219ce48e5aSMichal Kubecek 	if (gstrings.len &&
18229ce48e5aSMichal Kubecek 	    copy_to_user(useraddr, data, gstrings.len * ETH_GSTRING_LEN))
18239ce48e5aSMichal Kubecek 		goto out;
18249ce48e5aSMichal Kubecek 	ret = 0;
18259ce48e5aSMichal Kubecek 
18269ce48e5aSMichal Kubecek out:
18279ce48e5aSMichal Kubecek 	vfree(data);
18289ce48e5aSMichal Kubecek 	return ret;
18299ce48e5aSMichal Kubecek }
18309ce48e5aSMichal Kubecek 
18317888fe53SAlexander Duyck __printf(2, 3) void ethtool_sprintf(u8 **data, const char *fmt, ...)
18327888fe53SAlexander Duyck {
18337888fe53SAlexander Duyck 	va_list args;
18347888fe53SAlexander Duyck 
18357888fe53SAlexander Duyck 	va_start(args, fmt);
18367888fe53SAlexander Duyck 	vsnprintf(*data, ETH_GSTRING_LEN, fmt, args);
18377888fe53SAlexander Duyck 	va_end(args);
18387888fe53SAlexander Duyck 
18397888fe53SAlexander Duyck 	*data += ETH_GSTRING_LEN;
18407888fe53SAlexander Duyck }
18417888fe53SAlexander Duyck EXPORT_SYMBOL(ethtool_sprintf);
18427888fe53SAlexander Duyck 
18439ce48e5aSMichal Kubecek static int ethtool_phys_id(struct net_device *dev, void __user *useraddr)
18449ce48e5aSMichal Kubecek {
18459ce48e5aSMichal Kubecek 	struct ethtool_value id;
18469ce48e5aSMichal Kubecek 	static bool busy;
18479ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
18489ce48e5aSMichal Kubecek 	int rc;
18499ce48e5aSMichal Kubecek 
18509ce48e5aSMichal Kubecek 	if (!ops->set_phys_id)
18519ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
18529ce48e5aSMichal Kubecek 
18539ce48e5aSMichal Kubecek 	if (busy)
18549ce48e5aSMichal Kubecek 		return -EBUSY;
18559ce48e5aSMichal Kubecek 
18569ce48e5aSMichal Kubecek 	if (copy_from_user(&id, useraddr, sizeof(id)))
18579ce48e5aSMichal Kubecek 		return -EFAULT;
18589ce48e5aSMichal Kubecek 
18599ce48e5aSMichal Kubecek 	rc = ops->set_phys_id(dev, ETHTOOL_ID_ACTIVE);
18609ce48e5aSMichal Kubecek 	if (rc < 0)
18619ce48e5aSMichal Kubecek 		return rc;
18629ce48e5aSMichal Kubecek 
18639ce48e5aSMichal Kubecek 	/* Drop the RTNL lock while waiting, but prevent reentry or
18649ce48e5aSMichal Kubecek 	 * removal of the device.
18659ce48e5aSMichal Kubecek 	 */
18669ce48e5aSMichal Kubecek 	busy = true;
18679ce48e5aSMichal Kubecek 	dev_hold(dev);
18689ce48e5aSMichal Kubecek 	rtnl_unlock();
18699ce48e5aSMichal Kubecek 
18709ce48e5aSMichal Kubecek 	if (rc == 0) {
18719ce48e5aSMichal Kubecek 		/* Driver will handle this itself */
18729ce48e5aSMichal Kubecek 		schedule_timeout_interruptible(
18739ce48e5aSMichal Kubecek 			id.data ? (id.data * HZ) : MAX_SCHEDULE_TIMEOUT);
18749ce48e5aSMichal Kubecek 	} else {
18759ce48e5aSMichal Kubecek 		/* Driver expects to be called at twice the frequency in rc */
18762adc6edcSEdward Cree 		int n = rc * 2, interval = HZ / n;
18772adc6edcSEdward Cree 		u64 count = n * id.data, i = 0;
18789ce48e5aSMichal Kubecek 
18799ce48e5aSMichal Kubecek 		do {
18809ce48e5aSMichal Kubecek 			rtnl_lock();
18819ce48e5aSMichal Kubecek 			rc = ops->set_phys_id(dev,
18822adc6edcSEdward Cree 				    (i++ & 1) ? ETHTOOL_ID_OFF : ETHTOOL_ID_ON);
18839ce48e5aSMichal Kubecek 			rtnl_unlock();
18849ce48e5aSMichal Kubecek 			if (rc)
18859ce48e5aSMichal Kubecek 				break;
18869ce48e5aSMichal Kubecek 			schedule_timeout_interruptible(interval);
18872adc6edcSEdward Cree 		} while (!signal_pending(current) && (!id.data || i < count));
18889ce48e5aSMichal Kubecek 	}
18899ce48e5aSMichal Kubecek 
18909ce48e5aSMichal Kubecek 	rtnl_lock();
18919ce48e5aSMichal Kubecek 	dev_put(dev);
18929ce48e5aSMichal Kubecek 	busy = false;
18939ce48e5aSMichal Kubecek 
18949ce48e5aSMichal Kubecek 	(void) ops->set_phys_id(dev, ETHTOOL_ID_INACTIVE);
18959ce48e5aSMichal Kubecek 	return rc;
18969ce48e5aSMichal Kubecek }
18979ce48e5aSMichal Kubecek 
18989ce48e5aSMichal Kubecek static int ethtool_get_stats(struct net_device *dev, void __user *useraddr)
18999ce48e5aSMichal Kubecek {
19009ce48e5aSMichal Kubecek 	struct ethtool_stats stats;
19019ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
19029ce48e5aSMichal Kubecek 	u64 *data;
19039ce48e5aSMichal Kubecek 	int ret, n_stats;
19049ce48e5aSMichal Kubecek 
19059ce48e5aSMichal Kubecek 	if (!ops->get_ethtool_stats || !ops->get_sset_count)
19069ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
19079ce48e5aSMichal Kubecek 
19089ce48e5aSMichal Kubecek 	n_stats = ops->get_sset_count(dev, ETH_SS_STATS);
19099ce48e5aSMichal Kubecek 	if (n_stats < 0)
19109ce48e5aSMichal Kubecek 		return n_stats;
19119ce48e5aSMichal Kubecek 	if (n_stats > S32_MAX / sizeof(u64))
19129ce48e5aSMichal Kubecek 		return -ENOMEM;
19139ce48e5aSMichal Kubecek 	WARN_ON_ONCE(!n_stats);
19149ce48e5aSMichal Kubecek 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
19159ce48e5aSMichal Kubecek 		return -EFAULT;
19169ce48e5aSMichal Kubecek 
19179ce48e5aSMichal Kubecek 	stats.n_stats = n_stats;
19189ce48e5aSMichal Kubecek 
19199ce48e5aSMichal Kubecek 	if (n_stats) {
19209ce48e5aSMichal Kubecek 		data = vzalloc(array_size(n_stats, sizeof(u64)));
19219ce48e5aSMichal Kubecek 		if (!data)
19229ce48e5aSMichal Kubecek 			return -ENOMEM;
19239ce48e5aSMichal Kubecek 		ops->get_ethtool_stats(dev, &stats, data);
19249ce48e5aSMichal Kubecek 	} else {
19259ce48e5aSMichal Kubecek 		data = NULL;
19269ce48e5aSMichal Kubecek 	}
19279ce48e5aSMichal Kubecek 
19289ce48e5aSMichal Kubecek 	ret = -EFAULT;
19299ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &stats, sizeof(stats)))
19309ce48e5aSMichal Kubecek 		goto out;
19319ce48e5aSMichal Kubecek 	useraddr += sizeof(stats);
19323dd14996SGustavo A. R. Silva 	if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
19339ce48e5aSMichal Kubecek 		goto out;
19349ce48e5aSMichal Kubecek 	ret = 0;
19359ce48e5aSMichal Kubecek 
19369ce48e5aSMichal Kubecek  out:
19379ce48e5aSMichal Kubecek 	vfree(data);
19389ce48e5aSMichal Kubecek 	return ret;
19399ce48e5aSMichal Kubecek }
19409ce48e5aSMichal Kubecek 
19419ce48e5aSMichal Kubecek static int ethtool_get_phy_stats(struct net_device *dev, void __user *useraddr)
19429ce48e5aSMichal Kubecek {
194317809516SFlorian Fainelli 	const struct ethtool_phy_ops *phy_ops = ethtool_phy_ops;
19449ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
19459ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
19469ce48e5aSMichal Kubecek 	struct ethtool_stats stats;
19479ce48e5aSMichal Kubecek 	u64 *data;
19489ce48e5aSMichal Kubecek 	int ret, n_stats;
19499ce48e5aSMichal Kubecek 
19509ce48e5aSMichal Kubecek 	if (!phydev && (!ops->get_ethtool_phy_stats || !ops->get_sset_count))
19519ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
19529ce48e5aSMichal Kubecek 
195317809516SFlorian Fainelli 	if (dev->phydev && !ops->get_ethtool_phy_stats &&
195417809516SFlorian Fainelli 	    phy_ops && phy_ops->get_sset_count)
195517809516SFlorian Fainelli 		n_stats = phy_ops->get_sset_count(dev->phydev);
19569ce48e5aSMichal Kubecek 	else
19579ce48e5aSMichal Kubecek 		n_stats = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
19589ce48e5aSMichal Kubecek 	if (n_stats < 0)
19599ce48e5aSMichal Kubecek 		return n_stats;
19609ce48e5aSMichal Kubecek 	if (n_stats > S32_MAX / sizeof(u64))
19619ce48e5aSMichal Kubecek 		return -ENOMEM;
19629ce48e5aSMichal Kubecek 	WARN_ON_ONCE(!n_stats);
19639ce48e5aSMichal Kubecek 
19649ce48e5aSMichal Kubecek 	if (copy_from_user(&stats, useraddr, sizeof(stats)))
19659ce48e5aSMichal Kubecek 		return -EFAULT;
19669ce48e5aSMichal Kubecek 
19679ce48e5aSMichal Kubecek 	stats.n_stats = n_stats;
19689ce48e5aSMichal Kubecek 
19699ce48e5aSMichal Kubecek 	if (n_stats) {
19709ce48e5aSMichal Kubecek 		data = vzalloc(array_size(n_stats, sizeof(u64)));
19719ce48e5aSMichal Kubecek 		if (!data)
19729ce48e5aSMichal Kubecek 			return -ENOMEM;
19739ce48e5aSMichal Kubecek 
197417809516SFlorian Fainelli 		if (dev->phydev && !ops->get_ethtool_phy_stats &&
197517809516SFlorian Fainelli 		    phy_ops && phy_ops->get_stats) {
197617809516SFlorian Fainelli 			ret = phy_ops->get_stats(dev->phydev, &stats, data);
19779ce48e5aSMichal Kubecek 			if (ret < 0)
19789ce48e5aSMichal Kubecek 				goto out;
19799ce48e5aSMichal Kubecek 		} else {
19809ce48e5aSMichal Kubecek 			ops->get_ethtool_phy_stats(dev, &stats, data);
19819ce48e5aSMichal Kubecek 		}
19829ce48e5aSMichal Kubecek 	} else {
19839ce48e5aSMichal Kubecek 		data = NULL;
19849ce48e5aSMichal Kubecek 	}
19859ce48e5aSMichal Kubecek 
19869ce48e5aSMichal Kubecek 	ret = -EFAULT;
19879ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &stats, sizeof(stats)))
19889ce48e5aSMichal Kubecek 		goto out;
19899ce48e5aSMichal Kubecek 	useraddr += sizeof(stats);
19903dd14996SGustavo A. R. Silva 	if (n_stats && copy_to_user(useraddr, data, array_size(n_stats, sizeof(u64))))
19919ce48e5aSMichal Kubecek 		goto out;
19929ce48e5aSMichal Kubecek 	ret = 0;
19939ce48e5aSMichal Kubecek 
19949ce48e5aSMichal Kubecek  out:
19959ce48e5aSMichal Kubecek 	vfree(data);
19969ce48e5aSMichal Kubecek 	return ret;
19979ce48e5aSMichal Kubecek }
19989ce48e5aSMichal Kubecek 
19999ce48e5aSMichal Kubecek static int ethtool_get_perm_addr(struct net_device *dev, void __user *useraddr)
20009ce48e5aSMichal Kubecek {
20019ce48e5aSMichal Kubecek 	struct ethtool_perm_addr epaddr;
20029ce48e5aSMichal Kubecek 
20039ce48e5aSMichal Kubecek 	if (copy_from_user(&epaddr, useraddr, sizeof(epaddr)))
20049ce48e5aSMichal Kubecek 		return -EFAULT;
20059ce48e5aSMichal Kubecek 
20069ce48e5aSMichal Kubecek 	if (epaddr.size < dev->addr_len)
20079ce48e5aSMichal Kubecek 		return -ETOOSMALL;
20089ce48e5aSMichal Kubecek 	epaddr.size = dev->addr_len;
20099ce48e5aSMichal Kubecek 
20109ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &epaddr, sizeof(epaddr)))
20119ce48e5aSMichal Kubecek 		return -EFAULT;
20129ce48e5aSMichal Kubecek 	useraddr += sizeof(epaddr);
20139ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, dev->perm_addr, epaddr.size))
20149ce48e5aSMichal Kubecek 		return -EFAULT;
20159ce48e5aSMichal Kubecek 	return 0;
20169ce48e5aSMichal Kubecek }
20179ce48e5aSMichal Kubecek 
20189ce48e5aSMichal Kubecek static int ethtool_get_value(struct net_device *dev, char __user *useraddr,
20199ce48e5aSMichal Kubecek 			     u32 cmd, u32 (*actor)(struct net_device *))
20209ce48e5aSMichal Kubecek {
20219ce48e5aSMichal Kubecek 	struct ethtool_value edata = { .cmd = cmd };
20229ce48e5aSMichal Kubecek 
20239ce48e5aSMichal Kubecek 	if (!actor)
20249ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
20259ce48e5aSMichal Kubecek 
20269ce48e5aSMichal Kubecek 	edata.data = actor(dev);
20279ce48e5aSMichal Kubecek 
20289ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &edata, sizeof(edata)))
20299ce48e5aSMichal Kubecek 		return -EFAULT;
20309ce48e5aSMichal Kubecek 	return 0;
20319ce48e5aSMichal Kubecek }
20329ce48e5aSMichal Kubecek 
20339ce48e5aSMichal Kubecek static int ethtool_set_value_void(struct net_device *dev, char __user *useraddr,
20349ce48e5aSMichal Kubecek 			     void (*actor)(struct net_device *, u32))
20359ce48e5aSMichal Kubecek {
20369ce48e5aSMichal Kubecek 	struct ethtool_value edata;
20379ce48e5aSMichal Kubecek 
20389ce48e5aSMichal Kubecek 	if (!actor)
20399ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
20409ce48e5aSMichal Kubecek 
20419ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
20429ce48e5aSMichal Kubecek 		return -EFAULT;
20439ce48e5aSMichal Kubecek 
20449ce48e5aSMichal Kubecek 	actor(dev, edata.data);
20459ce48e5aSMichal Kubecek 	return 0;
20469ce48e5aSMichal Kubecek }
20479ce48e5aSMichal Kubecek 
20489ce48e5aSMichal Kubecek static int ethtool_set_value(struct net_device *dev, char __user *useraddr,
20499ce48e5aSMichal Kubecek 			     int (*actor)(struct net_device *, u32))
20509ce48e5aSMichal Kubecek {
20519ce48e5aSMichal Kubecek 	struct ethtool_value edata;
20529ce48e5aSMichal Kubecek 
20539ce48e5aSMichal Kubecek 	if (!actor)
20549ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
20559ce48e5aSMichal Kubecek 
20569ce48e5aSMichal Kubecek 	if (copy_from_user(&edata, useraddr, sizeof(edata)))
20579ce48e5aSMichal Kubecek 		return -EFAULT;
20589ce48e5aSMichal Kubecek 
20599ce48e5aSMichal Kubecek 	return actor(dev, edata.data);
20609ce48e5aSMichal Kubecek }
20619ce48e5aSMichal Kubecek 
20629ce48e5aSMichal Kubecek static noinline_for_stack int ethtool_flash_device(struct net_device *dev,
20639ce48e5aSMichal Kubecek 						   char __user *useraddr)
20649ce48e5aSMichal Kubecek {
20659ce48e5aSMichal Kubecek 	struct ethtool_flash efl;
20669ce48e5aSMichal Kubecek 
20679ce48e5aSMichal Kubecek 	if (copy_from_user(&efl, useraddr, sizeof(efl)))
20689ce48e5aSMichal Kubecek 		return -EFAULT;
20699ce48e5aSMichal Kubecek 	efl.data[ETHTOOL_FLASH_MAX_FILENAME - 1] = 0;
20709ce48e5aSMichal Kubecek 
20719ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->flash_device)
20729ce48e5aSMichal Kubecek 		return devlink_compat_flash_update(dev, efl.data);
20739ce48e5aSMichal Kubecek 
20749ce48e5aSMichal Kubecek 	return dev->ethtool_ops->flash_device(dev, &efl);
20759ce48e5aSMichal Kubecek }
20769ce48e5aSMichal Kubecek 
20779ce48e5aSMichal Kubecek static int ethtool_set_dump(struct net_device *dev,
20789ce48e5aSMichal Kubecek 			void __user *useraddr)
20799ce48e5aSMichal Kubecek {
20809ce48e5aSMichal Kubecek 	struct ethtool_dump dump;
20819ce48e5aSMichal Kubecek 
20829ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_dump)
20839ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
20849ce48e5aSMichal Kubecek 
20859ce48e5aSMichal Kubecek 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
20869ce48e5aSMichal Kubecek 		return -EFAULT;
20879ce48e5aSMichal Kubecek 
20889ce48e5aSMichal Kubecek 	return dev->ethtool_ops->set_dump(dev, &dump);
20899ce48e5aSMichal Kubecek }
20909ce48e5aSMichal Kubecek 
20919ce48e5aSMichal Kubecek static int ethtool_get_dump_flag(struct net_device *dev,
20929ce48e5aSMichal Kubecek 				void __user *useraddr)
20939ce48e5aSMichal Kubecek {
20949ce48e5aSMichal Kubecek 	int ret;
20959ce48e5aSMichal Kubecek 	struct ethtool_dump dump;
20969ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
20979ce48e5aSMichal Kubecek 
20989ce48e5aSMichal Kubecek 	if (!ops->get_dump_flag)
20999ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
21009ce48e5aSMichal Kubecek 
21019ce48e5aSMichal Kubecek 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
21029ce48e5aSMichal Kubecek 		return -EFAULT;
21039ce48e5aSMichal Kubecek 
21049ce48e5aSMichal Kubecek 	ret = ops->get_dump_flag(dev, &dump);
21059ce48e5aSMichal Kubecek 	if (ret)
21069ce48e5aSMichal Kubecek 		return ret;
21079ce48e5aSMichal Kubecek 
21089ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &dump, sizeof(dump)))
21099ce48e5aSMichal Kubecek 		return -EFAULT;
21109ce48e5aSMichal Kubecek 	return 0;
21119ce48e5aSMichal Kubecek }
21129ce48e5aSMichal Kubecek 
21139ce48e5aSMichal Kubecek static int ethtool_get_dump_data(struct net_device *dev,
21149ce48e5aSMichal Kubecek 				void __user *useraddr)
21159ce48e5aSMichal Kubecek {
21169ce48e5aSMichal Kubecek 	int ret;
21179ce48e5aSMichal Kubecek 	__u32 len;
21189ce48e5aSMichal Kubecek 	struct ethtool_dump dump, tmp;
21199ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
21209ce48e5aSMichal Kubecek 	void *data = NULL;
21219ce48e5aSMichal Kubecek 
21229ce48e5aSMichal Kubecek 	if (!ops->get_dump_data || !ops->get_dump_flag)
21239ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
21249ce48e5aSMichal Kubecek 
21259ce48e5aSMichal Kubecek 	if (copy_from_user(&dump, useraddr, sizeof(dump)))
21269ce48e5aSMichal Kubecek 		return -EFAULT;
21279ce48e5aSMichal Kubecek 
21289ce48e5aSMichal Kubecek 	memset(&tmp, 0, sizeof(tmp));
21299ce48e5aSMichal Kubecek 	tmp.cmd = ETHTOOL_GET_DUMP_FLAG;
21309ce48e5aSMichal Kubecek 	ret = ops->get_dump_flag(dev, &tmp);
21319ce48e5aSMichal Kubecek 	if (ret)
21329ce48e5aSMichal Kubecek 		return ret;
21339ce48e5aSMichal Kubecek 
21349ce48e5aSMichal Kubecek 	len = min(tmp.len, dump.len);
21359ce48e5aSMichal Kubecek 	if (!len)
21369ce48e5aSMichal Kubecek 		return -EFAULT;
21379ce48e5aSMichal Kubecek 
21389ce48e5aSMichal Kubecek 	/* Don't ever let the driver think there's more space available
21399ce48e5aSMichal Kubecek 	 * than it requested with .get_dump_flag().
21409ce48e5aSMichal Kubecek 	 */
21419ce48e5aSMichal Kubecek 	dump.len = len;
21429ce48e5aSMichal Kubecek 
21439ce48e5aSMichal Kubecek 	/* Always allocate enough space to hold the whole thing so that the
21449ce48e5aSMichal Kubecek 	 * driver does not need to check the length and bother with partial
21459ce48e5aSMichal Kubecek 	 * dumping.
21469ce48e5aSMichal Kubecek 	 */
21479ce48e5aSMichal Kubecek 	data = vzalloc(tmp.len);
21489ce48e5aSMichal Kubecek 	if (!data)
21499ce48e5aSMichal Kubecek 		return -ENOMEM;
21509ce48e5aSMichal Kubecek 	ret = ops->get_dump_data(dev, &dump, data);
21519ce48e5aSMichal Kubecek 	if (ret)
21529ce48e5aSMichal Kubecek 		goto out;
21539ce48e5aSMichal Kubecek 
21549ce48e5aSMichal Kubecek 	/* There are two sane possibilities:
21559ce48e5aSMichal Kubecek 	 * 1. The driver's .get_dump_data() does not touch dump.len.
21569ce48e5aSMichal Kubecek 	 * 2. Or it may set dump.len to how much it really writes, which
21579ce48e5aSMichal Kubecek 	 *    should be tmp.len (or len if it can do a partial dump).
21589ce48e5aSMichal Kubecek 	 * In any case respond to userspace with the actual length of data
21599ce48e5aSMichal Kubecek 	 * it's receiving.
21609ce48e5aSMichal Kubecek 	 */
21619ce48e5aSMichal Kubecek 	WARN_ON(dump.len != len && dump.len != tmp.len);
21629ce48e5aSMichal Kubecek 	dump.len = len;
21639ce48e5aSMichal Kubecek 
21649ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &dump, sizeof(dump))) {
21659ce48e5aSMichal Kubecek 		ret = -EFAULT;
21669ce48e5aSMichal Kubecek 		goto out;
21679ce48e5aSMichal Kubecek 	}
21689ce48e5aSMichal Kubecek 	useraddr += offsetof(struct ethtool_dump, data);
21699ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, data, len))
21709ce48e5aSMichal Kubecek 		ret = -EFAULT;
21719ce48e5aSMichal Kubecek out:
21729ce48e5aSMichal Kubecek 	vfree(data);
21739ce48e5aSMichal Kubecek 	return ret;
21749ce48e5aSMichal Kubecek }
21759ce48e5aSMichal Kubecek 
21769ce48e5aSMichal Kubecek static int ethtool_get_ts_info(struct net_device *dev, void __user *useraddr)
21779ce48e5aSMichal Kubecek {
21789ce48e5aSMichal Kubecek 	struct ethtool_ts_info info;
21795b071c59SMichal Kubecek 	int err;
21809ce48e5aSMichal Kubecek 
21815b071c59SMichal Kubecek 	err = __ethtool_get_ts_info(dev, &info);
21829ce48e5aSMichal Kubecek 	if (err)
21839ce48e5aSMichal Kubecek 		return err;
21849ce48e5aSMichal Kubecek 
21859ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &info, sizeof(info)))
21865b071c59SMichal Kubecek 		return -EFAULT;
21879ce48e5aSMichal Kubecek 
21885b071c59SMichal Kubecek 	return 0;
21899ce48e5aSMichal Kubecek }
21909ce48e5aSMichal Kubecek 
219195dfc7efSAndrew Lunn int ethtool_get_module_info_call(struct net_device *dev,
21929ce48e5aSMichal Kubecek 				 struct ethtool_modinfo *modinfo)
21939ce48e5aSMichal Kubecek {
21949ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
21959ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
21969ce48e5aSMichal Kubecek 
21979ce48e5aSMichal Kubecek 	if (dev->sfp_bus)
21989ce48e5aSMichal Kubecek 		return sfp_get_module_info(dev->sfp_bus, modinfo);
21999ce48e5aSMichal Kubecek 
22009ce48e5aSMichal Kubecek 	if (phydev && phydev->drv && phydev->drv->module_info)
22019ce48e5aSMichal Kubecek 		return phydev->drv->module_info(phydev, modinfo);
22029ce48e5aSMichal Kubecek 
22039ce48e5aSMichal Kubecek 	if (ops->get_module_info)
22049ce48e5aSMichal Kubecek 		return ops->get_module_info(dev, modinfo);
22059ce48e5aSMichal Kubecek 
22069ce48e5aSMichal Kubecek 	return -EOPNOTSUPP;
22079ce48e5aSMichal Kubecek }
22089ce48e5aSMichal Kubecek 
22099ce48e5aSMichal Kubecek static int ethtool_get_module_info(struct net_device *dev,
22109ce48e5aSMichal Kubecek 				   void __user *useraddr)
22119ce48e5aSMichal Kubecek {
22129ce48e5aSMichal Kubecek 	int ret;
22139ce48e5aSMichal Kubecek 	struct ethtool_modinfo modinfo;
22149ce48e5aSMichal Kubecek 
22159ce48e5aSMichal Kubecek 	if (copy_from_user(&modinfo, useraddr, sizeof(modinfo)))
22169ce48e5aSMichal Kubecek 		return -EFAULT;
22179ce48e5aSMichal Kubecek 
221895dfc7efSAndrew Lunn 	ret = ethtool_get_module_info_call(dev, &modinfo);
22199ce48e5aSMichal Kubecek 	if (ret)
22209ce48e5aSMichal Kubecek 		return ret;
22219ce48e5aSMichal Kubecek 
22229ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &modinfo, sizeof(modinfo)))
22239ce48e5aSMichal Kubecek 		return -EFAULT;
22249ce48e5aSMichal Kubecek 
22259ce48e5aSMichal Kubecek 	return 0;
22269ce48e5aSMichal Kubecek }
22279ce48e5aSMichal Kubecek 
222895dfc7efSAndrew Lunn int ethtool_get_module_eeprom_call(struct net_device *dev,
22299ce48e5aSMichal Kubecek 				   struct ethtool_eeprom *ee, u8 *data)
22309ce48e5aSMichal Kubecek {
22319ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
22329ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
22339ce48e5aSMichal Kubecek 
22349ce48e5aSMichal Kubecek 	if (dev->sfp_bus)
22359ce48e5aSMichal Kubecek 		return sfp_get_module_eeprom(dev->sfp_bus, ee, data);
22369ce48e5aSMichal Kubecek 
22379ce48e5aSMichal Kubecek 	if (phydev && phydev->drv && phydev->drv->module_eeprom)
22389ce48e5aSMichal Kubecek 		return phydev->drv->module_eeprom(phydev, ee, data);
22399ce48e5aSMichal Kubecek 
22409ce48e5aSMichal Kubecek 	if (ops->get_module_eeprom)
22419ce48e5aSMichal Kubecek 		return ops->get_module_eeprom(dev, ee, data);
22429ce48e5aSMichal Kubecek 
22439ce48e5aSMichal Kubecek 	return -EOPNOTSUPP;
22449ce48e5aSMichal Kubecek }
22459ce48e5aSMichal Kubecek 
22469ce48e5aSMichal Kubecek static int ethtool_get_module_eeprom(struct net_device *dev,
22479ce48e5aSMichal Kubecek 				     void __user *useraddr)
22489ce48e5aSMichal Kubecek {
22499ce48e5aSMichal Kubecek 	int ret;
22509ce48e5aSMichal Kubecek 	struct ethtool_modinfo modinfo;
22519ce48e5aSMichal Kubecek 
225295dfc7efSAndrew Lunn 	ret = ethtool_get_module_info_call(dev, &modinfo);
22539ce48e5aSMichal Kubecek 	if (ret)
22549ce48e5aSMichal Kubecek 		return ret;
22559ce48e5aSMichal Kubecek 
22569ce48e5aSMichal Kubecek 	return ethtool_get_any_eeprom(dev, useraddr,
225795dfc7efSAndrew Lunn 				      ethtool_get_module_eeprom_call,
22589ce48e5aSMichal Kubecek 				      modinfo.eeprom_len);
22599ce48e5aSMichal Kubecek }
22609ce48e5aSMichal Kubecek 
22619ce48e5aSMichal Kubecek static int ethtool_tunable_valid(const struct ethtool_tunable *tuna)
22629ce48e5aSMichal Kubecek {
22639ce48e5aSMichal Kubecek 	switch (tuna->id) {
22649ce48e5aSMichal Kubecek 	case ETHTOOL_RX_COPYBREAK:
22659ce48e5aSMichal Kubecek 	case ETHTOOL_TX_COPYBREAK:
22669ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u32) ||
22679ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U32)
22689ce48e5aSMichal Kubecek 			return -EINVAL;
22699ce48e5aSMichal Kubecek 		break;
22709ce48e5aSMichal Kubecek 	case ETHTOOL_PFC_PREVENTION_TOUT:
22719ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u16) ||
22729ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
22739ce48e5aSMichal Kubecek 			return -EINVAL;
22749ce48e5aSMichal Kubecek 		break;
22759ce48e5aSMichal Kubecek 	default:
22769ce48e5aSMichal Kubecek 		return -EINVAL;
22779ce48e5aSMichal Kubecek 	}
22789ce48e5aSMichal Kubecek 
22799ce48e5aSMichal Kubecek 	return 0;
22809ce48e5aSMichal Kubecek }
22819ce48e5aSMichal Kubecek 
22829ce48e5aSMichal Kubecek static int ethtool_get_tunable(struct net_device *dev, void __user *useraddr)
22839ce48e5aSMichal Kubecek {
22849ce48e5aSMichal Kubecek 	int ret;
22859ce48e5aSMichal Kubecek 	struct ethtool_tunable tuna;
22869ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
22879ce48e5aSMichal Kubecek 	void *data;
22889ce48e5aSMichal Kubecek 
22899ce48e5aSMichal Kubecek 	if (!ops->get_tunable)
22909ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
22919ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
22929ce48e5aSMichal Kubecek 		return -EFAULT;
22939ce48e5aSMichal Kubecek 	ret = ethtool_tunable_valid(&tuna);
22949ce48e5aSMichal Kubecek 	if (ret)
22959ce48e5aSMichal Kubecek 		return ret;
2296*80ec82e3SAustin Kim 	data = kzalloc(tuna.len, GFP_USER);
22979ce48e5aSMichal Kubecek 	if (!data)
22989ce48e5aSMichal Kubecek 		return -ENOMEM;
22999ce48e5aSMichal Kubecek 	ret = ops->get_tunable(dev, &tuna, data);
23009ce48e5aSMichal Kubecek 	if (ret)
23019ce48e5aSMichal Kubecek 		goto out;
23029ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
23039ce48e5aSMichal Kubecek 	ret = -EFAULT;
23049ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, data, tuna.len))
23059ce48e5aSMichal Kubecek 		goto out;
23069ce48e5aSMichal Kubecek 	ret = 0;
23079ce48e5aSMichal Kubecek 
23089ce48e5aSMichal Kubecek out:
23099ce48e5aSMichal Kubecek 	kfree(data);
23109ce48e5aSMichal Kubecek 	return ret;
23119ce48e5aSMichal Kubecek }
23129ce48e5aSMichal Kubecek 
23139ce48e5aSMichal Kubecek static int ethtool_set_tunable(struct net_device *dev, void __user *useraddr)
23149ce48e5aSMichal Kubecek {
23159ce48e5aSMichal Kubecek 	int ret;
23169ce48e5aSMichal Kubecek 	struct ethtool_tunable tuna;
23179ce48e5aSMichal Kubecek 	const struct ethtool_ops *ops = dev->ethtool_ops;
23189ce48e5aSMichal Kubecek 	void *data;
23199ce48e5aSMichal Kubecek 
23209ce48e5aSMichal Kubecek 	if (!ops->set_tunable)
23219ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
23229ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
23239ce48e5aSMichal Kubecek 		return -EFAULT;
23249ce48e5aSMichal Kubecek 	ret = ethtool_tunable_valid(&tuna);
23259ce48e5aSMichal Kubecek 	if (ret)
23269ce48e5aSMichal Kubecek 		return ret;
23279ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
23289ce48e5aSMichal Kubecek 	data = memdup_user(useraddr, tuna.len);
23299ce48e5aSMichal Kubecek 	if (IS_ERR(data))
23309ce48e5aSMichal Kubecek 		return PTR_ERR(data);
23319ce48e5aSMichal Kubecek 	ret = ops->set_tunable(dev, &tuna, data);
23329ce48e5aSMichal Kubecek 
23339ce48e5aSMichal Kubecek 	kfree(data);
23349ce48e5aSMichal Kubecek 	return ret;
23359ce48e5aSMichal Kubecek }
23369ce48e5aSMichal Kubecek 
23379ce48e5aSMichal Kubecek static noinline_for_stack int
23389ce48e5aSMichal Kubecek ethtool_get_per_queue_coalesce(struct net_device *dev,
23399ce48e5aSMichal Kubecek 			       void __user *useraddr,
23409ce48e5aSMichal Kubecek 			       struct ethtool_per_queue_op *per_queue_opt)
23419ce48e5aSMichal Kubecek {
23429ce48e5aSMichal Kubecek 	u32 bit;
23439ce48e5aSMichal Kubecek 	int ret;
23449ce48e5aSMichal Kubecek 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
23459ce48e5aSMichal Kubecek 
23469ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_per_queue_coalesce)
23479ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
23489ce48e5aSMichal Kubecek 
23499ce48e5aSMichal Kubecek 	useraddr += sizeof(*per_queue_opt);
23509ce48e5aSMichal Kubecek 
23519ce48e5aSMichal Kubecek 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask,
23529ce48e5aSMichal Kubecek 			  MAX_NUM_QUEUE);
23539ce48e5aSMichal Kubecek 
23549ce48e5aSMichal Kubecek 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
23559ce48e5aSMichal Kubecek 		struct ethtool_coalesce coalesce = { .cmd = ETHTOOL_GCOALESCE };
23569ce48e5aSMichal Kubecek 
23579ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, &coalesce);
23589ce48e5aSMichal Kubecek 		if (ret != 0)
23599ce48e5aSMichal Kubecek 			return ret;
23609ce48e5aSMichal Kubecek 		if (copy_to_user(useraddr, &coalesce, sizeof(coalesce)))
23619ce48e5aSMichal Kubecek 			return -EFAULT;
23629ce48e5aSMichal Kubecek 		useraddr += sizeof(coalesce);
23639ce48e5aSMichal Kubecek 	}
23649ce48e5aSMichal Kubecek 
23659ce48e5aSMichal Kubecek 	return 0;
23669ce48e5aSMichal Kubecek }
23679ce48e5aSMichal Kubecek 
23689ce48e5aSMichal Kubecek static noinline_for_stack int
23699ce48e5aSMichal Kubecek ethtool_set_per_queue_coalesce(struct net_device *dev,
23709ce48e5aSMichal Kubecek 			       void __user *useraddr,
23719ce48e5aSMichal Kubecek 			       struct ethtool_per_queue_op *per_queue_opt)
23729ce48e5aSMichal Kubecek {
23739ce48e5aSMichal Kubecek 	u32 bit;
23749ce48e5aSMichal Kubecek 	int i, ret = 0;
23759ce48e5aSMichal Kubecek 	int n_queue;
23769ce48e5aSMichal Kubecek 	struct ethtool_coalesce *backup = NULL, *tmp = NULL;
23779ce48e5aSMichal Kubecek 	DECLARE_BITMAP(queue_mask, MAX_NUM_QUEUE);
23789ce48e5aSMichal Kubecek 
23799ce48e5aSMichal Kubecek 	if ((!dev->ethtool_ops->set_per_queue_coalesce) ||
23809ce48e5aSMichal Kubecek 	    (!dev->ethtool_ops->get_per_queue_coalesce))
23819ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
23829ce48e5aSMichal Kubecek 
23839ce48e5aSMichal Kubecek 	useraddr += sizeof(*per_queue_opt);
23849ce48e5aSMichal Kubecek 
23859ce48e5aSMichal Kubecek 	bitmap_from_arr32(queue_mask, per_queue_opt->queue_mask, MAX_NUM_QUEUE);
23869ce48e5aSMichal Kubecek 	n_queue = bitmap_weight(queue_mask, MAX_NUM_QUEUE);
23879ce48e5aSMichal Kubecek 	tmp = backup = kmalloc_array(n_queue, sizeof(*backup), GFP_KERNEL);
23889ce48e5aSMichal Kubecek 	if (!backup)
23899ce48e5aSMichal Kubecek 		return -ENOMEM;
23909ce48e5aSMichal Kubecek 
23919ce48e5aSMichal Kubecek 	for_each_set_bit(bit, queue_mask, MAX_NUM_QUEUE) {
23929ce48e5aSMichal Kubecek 		struct ethtool_coalesce coalesce;
23939ce48e5aSMichal Kubecek 
23949ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->get_per_queue_coalesce(dev, bit, tmp);
23959ce48e5aSMichal Kubecek 		if (ret != 0)
23969ce48e5aSMichal Kubecek 			goto roll_back;
23979ce48e5aSMichal Kubecek 
23989ce48e5aSMichal Kubecek 		tmp++;
23999ce48e5aSMichal Kubecek 
24009ce48e5aSMichal Kubecek 		if (copy_from_user(&coalesce, useraddr, sizeof(coalesce))) {
24019ce48e5aSMichal Kubecek 			ret = -EFAULT;
24029ce48e5aSMichal Kubecek 			goto roll_back;
24039ce48e5aSMichal Kubecek 		}
24049ce48e5aSMichal Kubecek 
240595cddcb5SJakub Kicinski 		if (!ethtool_set_coalesce_supported(dev, &coalesce)) {
240695cddcb5SJakub Kicinski 			ret = -EOPNOTSUPP;
240795cddcb5SJakub Kicinski 			goto roll_back;
240895cddcb5SJakub Kicinski 		}
240995cddcb5SJakub Kicinski 
24109ce48e5aSMichal Kubecek 		ret = dev->ethtool_ops->set_per_queue_coalesce(dev, bit, &coalesce);
24119ce48e5aSMichal Kubecek 		if (ret != 0)
24129ce48e5aSMichal Kubecek 			goto roll_back;
24139ce48e5aSMichal Kubecek 
24149ce48e5aSMichal Kubecek 		useraddr += sizeof(coalesce);
24159ce48e5aSMichal Kubecek 	}
24169ce48e5aSMichal Kubecek 
24179ce48e5aSMichal Kubecek roll_back:
24189ce48e5aSMichal Kubecek 	if (ret != 0) {
24199ce48e5aSMichal Kubecek 		tmp = backup;
24209ce48e5aSMichal Kubecek 		for_each_set_bit(i, queue_mask, bit) {
24219ce48e5aSMichal Kubecek 			dev->ethtool_ops->set_per_queue_coalesce(dev, i, tmp);
24229ce48e5aSMichal Kubecek 			tmp++;
24239ce48e5aSMichal Kubecek 		}
24249ce48e5aSMichal Kubecek 	}
24259ce48e5aSMichal Kubecek 	kfree(backup);
24269ce48e5aSMichal Kubecek 
24279ce48e5aSMichal Kubecek 	return ret;
24289ce48e5aSMichal Kubecek }
24299ce48e5aSMichal Kubecek 
24309ce48e5aSMichal Kubecek static int noinline_for_stack ethtool_set_per_queue(struct net_device *dev,
24319ce48e5aSMichal Kubecek 				 void __user *useraddr, u32 sub_cmd)
24329ce48e5aSMichal Kubecek {
24339ce48e5aSMichal Kubecek 	struct ethtool_per_queue_op per_queue_opt;
24349ce48e5aSMichal Kubecek 
24359ce48e5aSMichal Kubecek 	if (copy_from_user(&per_queue_opt, useraddr, sizeof(per_queue_opt)))
24369ce48e5aSMichal Kubecek 		return -EFAULT;
24379ce48e5aSMichal Kubecek 
24389ce48e5aSMichal Kubecek 	if (per_queue_opt.sub_command != sub_cmd)
24399ce48e5aSMichal Kubecek 		return -EINVAL;
24409ce48e5aSMichal Kubecek 
24419ce48e5aSMichal Kubecek 	switch (per_queue_opt.sub_command) {
24429ce48e5aSMichal Kubecek 	case ETHTOOL_GCOALESCE:
24439ce48e5aSMichal Kubecek 		return ethtool_get_per_queue_coalesce(dev, useraddr, &per_queue_opt);
24449ce48e5aSMichal Kubecek 	case ETHTOOL_SCOALESCE:
24459ce48e5aSMichal Kubecek 		return ethtool_set_per_queue_coalesce(dev, useraddr, &per_queue_opt);
24469ce48e5aSMichal Kubecek 	default:
24479ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
24489d253c02STom Rix 	}
24499ce48e5aSMichal Kubecek }
24509ce48e5aSMichal Kubecek 
24519ce48e5aSMichal Kubecek static int ethtool_phy_tunable_valid(const struct ethtool_tunable *tuna)
24529ce48e5aSMichal Kubecek {
24539ce48e5aSMichal Kubecek 	switch (tuna->id) {
24549ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_DOWNSHIFT:
24559ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_FAST_LINK_DOWN:
24569ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u8) ||
24579ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U8)
24589ce48e5aSMichal Kubecek 			return -EINVAL;
24599ce48e5aSMichal Kubecek 		break;
24609ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_EDPD:
24619ce48e5aSMichal Kubecek 		if (tuna->len != sizeof(u16) ||
24629ce48e5aSMichal Kubecek 		    tuna->type_id != ETHTOOL_TUNABLE_U16)
24639ce48e5aSMichal Kubecek 			return -EINVAL;
24649ce48e5aSMichal Kubecek 		break;
24659ce48e5aSMichal Kubecek 	default:
24669ce48e5aSMichal Kubecek 		return -EINVAL;
24679ce48e5aSMichal Kubecek 	}
24689ce48e5aSMichal Kubecek 
24699ce48e5aSMichal Kubecek 	return 0;
24709ce48e5aSMichal Kubecek }
24719ce48e5aSMichal Kubecek 
24729ce48e5aSMichal Kubecek static int get_phy_tunable(struct net_device *dev, void __user *useraddr)
24739ce48e5aSMichal Kubecek {
24749ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
2475c6db31ffSIgor Russkikh 	struct ethtool_tunable tuna;
2476c6db31ffSIgor Russkikh 	bool phy_drv_tunable;
24779ce48e5aSMichal Kubecek 	void *data;
2478c6db31ffSIgor Russkikh 	int ret;
24799ce48e5aSMichal Kubecek 
2480c6db31ffSIgor Russkikh 	phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2481c6db31ffSIgor Russkikh 	if (!phy_drv_tunable && !dev->ethtool_ops->get_phy_tunable)
24829ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
24839ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
24849ce48e5aSMichal Kubecek 		return -EFAULT;
24859ce48e5aSMichal Kubecek 	ret = ethtool_phy_tunable_valid(&tuna);
24869ce48e5aSMichal Kubecek 	if (ret)
24879ce48e5aSMichal Kubecek 		return ret;
2488*80ec82e3SAustin Kim 	data = kzalloc(tuna.len, GFP_USER);
24899ce48e5aSMichal Kubecek 	if (!data)
24909ce48e5aSMichal Kubecek 		return -ENOMEM;
2491c6db31ffSIgor Russkikh 	if (phy_drv_tunable) {
24929ce48e5aSMichal Kubecek 		mutex_lock(&phydev->lock);
24939ce48e5aSMichal Kubecek 		ret = phydev->drv->get_tunable(phydev, &tuna, data);
24949ce48e5aSMichal Kubecek 		mutex_unlock(&phydev->lock);
2495c6db31ffSIgor Russkikh 	} else {
2496c6db31ffSIgor Russkikh 		ret = dev->ethtool_ops->get_phy_tunable(dev, &tuna, data);
2497c6db31ffSIgor Russkikh 	}
24989ce48e5aSMichal Kubecek 	if (ret)
24999ce48e5aSMichal Kubecek 		goto out;
25009ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
25019ce48e5aSMichal Kubecek 	ret = -EFAULT;
25029ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, data, tuna.len))
25039ce48e5aSMichal Kubecek 		goto out;
25049ce48e5aSMichal Kubecek 	ret = 0;
25059ce48e5aSMichal Kubecek 
25069ce48e5aSMichal Kubecek out:
25079ce48e5aSMichal Kubecek 	kfree(data);
25089ce48e5aSMichal Kubecek 	return ret;
25099ce48e5aSMichal Kubecek }
25109ce48e5aSMichal Kubecek 
25119ce48e5aSMichal Kubecek static int set_phy_tunable(struct net_device *dev, void __user *useraddr)
25129ce48e5aSMichal Kubecek {
25139ce48e5aSMichal Kubecek 	struct phy_device *phydev = dev->phydev;
2514c6db31ffSIgor Russkikh 	struct ethtool_tunable tuna;
2515c6db31ffSIgor Russkikh 	bool phy_drv_tunable;
25169ce48e5aSMichal Kubecek 	void *data;
2517c6db31ffSIgor Russkikh 	int ret;
25189ce48e5aSMichal Kubecek 
2519c6db31ffSIgor Russkikh 	phy_drv_tunable = phydev && phydev->drv && phydev->drv->get_tunable;
2520c6db31ffSIgor Russkikh 	if (!phy_drv_tunable && !dev->ethtool_ops->set_phy_tunable)
25219ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
25229ce48e5aSMichal Kubecek 	if (copy_from_user(&tuna, useraddr, sizeof(tuna)))
25239ce48e5aSMichal Kubecek 		return -EFAULT;
25249ce48e5aSMichal Kubecek 	ret = ethtool_phy_tunable_valid(&tuna);
25259ce48e5aSMichal Kubecek 	if (ret)
25269ce48e5aSMichal Kubecek 		return ret;
25279ce48e5aSMichal Kubecek 	useraddr += sizeof(tuna);
25289ce48e5aSMichal Kubecek 	data = memdup_user(useraddr, tuna.len);
25299ce48e5aSMichal Kubecek 	if (IS_ERR(data))
25309ce48e5aSMichal Kubecek 		return PTR_ERR(data);
2531c6db31ffSIgor Russkikh 	if (phy_drv_tunable) {
25329ce48e5aSMichal Kubecek 		mutex_lock(&phydev->lock);
25339ce48e5aSMichal Kubecek 		ret = phydev->drv->set_tunable(phydev, &tuna, data);
25349ce48e5aSMichal Kubecek 		mutex_unlock(&phydev->lock);
2535c6db31ffSIgor Russkikh 	} else {
2536c6db31ffSIgor Russkikh 		ret = dev->ethtool_ops->set_phy_tunable(dev, &tuna, data);
2537c6db31ffSIgor Russkikh 	}
25389ce48e5aSMichal Kubecek 
25399ce48e5aSMichal Kubecek 	kfree(data);
25409ce48e5aSMichal Kubecek 	return ret;
25419ce48e5aSMichal Kubecek }
25429ce48e5aSMichal Kubecek 
25439ce48e5aSMichal Kubecek static int ethtool_get_fecparam(struct net_device *dev, void __user *useraddr)
25449ce48e5aSMichal Kubecek {
25459ce48e5aSMichal Kubecek 	struct ethtool_fecparam fecparam = { .cmd = ETHTOOL_GFECPARAM };
25469ce48e5aSMichal Kubecek 	int rc;
25479ce48e5aSMichal Kubecek 
25489ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->get_fecparam)
25499ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
25509ce48e5aSMichal Kubecek 
25519ce48e5aSMichal Kubecek 	rc = dev->ethtool_ops->get_fecparam(dev, &fecparam);
25529ce48e5aSMichal Kubecek 	if (rc)
25539ce48e5aSMichal Kubecek 		return rc;
25549ce48e5aSMichal Kubecek 
2555240e1144SJakub Kicinski 	if (WARN_ON_ONCE(fecparam.reserved))
2556240e1144SJakub Kicinski 		fecparam.reserved = 0;
2557240e1144SJakub Kicinski 
25589ce48e5aSMichal Kubecek 	if (copy_to_user(useraddr, &fecparam, sizeof(fecparam)))
25599ce48e5aSMichal Kubecek 		return -EFAULT;
25609ce48e5aSMichal Kubecek 	return 0;
25619ce48e5aSMichal Kubecek }
25629ce48e5aSMichal Kubecek 
25639ce48e5aSMichal Kubecek static int ethtool_set_fecparam(struct net_device *dev, void __user *useraddr)
25649ce48e5aSMichal Kubecek {
25659ce48e5aSMichal Kubecek 	struct ethtool_fecparam fecparam;
25669ce48e5aSMichal Kubecek 
25679ce48e5aSMichal Kubecek 	if (!dev->ethtool_ops->set_fecparam)
25689ce48e5aSMichal Kubecek 		return -EOPNOTSUPP;
25699ce48e5aSMichal Kubecek 
25709ce48e5aSMichal Kubecek 	if (copy_from_user(&fecparam, useraddr, sizeof(fecparam)))
25719ce48e5aSMichal Kubecek 		return -EFAULT;
25729ce48e5aSMichal Kubecek 
2573cf2cc0bfSJakub Kicinski 	if (!fecparam.fec || fecparam.fec & ETHTOOL_FEC_NONE)
257442ce127dSJakub Kicinski 		return -EINVAL;
257542ce127dSJakub Kicinski 
2576d3b37fc8SJakub Kicinski 	fecparam.active_fec = 0;
2577240e1144SJakub Kicinski 	fecparam.reserved = 0;
2578240e1144SJakub Kicinski 
25799ce48e5aSMichal Kubecek 	return dev->ethtool_ops->set_fecparam(dev, &fecparam);
25809ce48e5aSMichal Kubecek }
25819ce48e5aSMichal Kubecek 
25829ce48e5aSMichal Kubecek /* The main entry point in this file.  Called from net/core/dev_ioctl.c */
25839ce48e5aSMichal Kubecek 
25849ce48e5aSMichal Kubecek int dev_ethtool(struct net *net, struct ifreq *ifr)
25859ce48e5aSMichal Kubecek {
25869ce48e5aSMichal Kubecek 	struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name);
25879ce48e5aSMichal Kubecek 	void __user *useraddr = ifr->ifr_data;
25889ce48e5aSMichal Kubecek 	u32 ethcmd, sub_cmd;
25899ce48e5aSMichal Kubecek 	int rc;
25909ce48e5aSMichal Kubecek 	netdev_features_t old_features;
25919ce48e5aSMichal Kubecek 
25929ce48e5aSMichal Kubecek 	if (!dev || !netif_device_present(dev))
25939ce48e5aSMichal Kubecek 		return -ENODEV;
25949ce48e5aSMichal Kubecek 
25959ce48e5aSMichal Kubecek 	if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
25969ce48e5aSMichal Kubecek 		return -EFAULT;
25979ce48e5aSMichal Kubecek 
25989ce48e5aSMichal Kubecek 	if (ethcmd == ETHTOOL_PERQUEUE) {
25999ce48e5aSMichal Kubecek 		if (copy_from_user(&sub_cmd, useraddr + sizeof(ethcmd), sizeof(sub_cmd)))
26009ce48e5aSMichal Kubecek 			return -EFAULT;
26019ce48e5aSMichal Kubecek 	} else {
26029ce48e5aSMichal Kubecek 		sub_cmd = ethcmd;
26039ce48e5aSMichal Kubecek 	}
26049ce48e5aSMichal Kubecek 	/* Allow some commands to be done by anyone */
26059ce48e5aSMichal Kubecek 	switch (sub_cmd) {
26069ce48e5aSMichal Kubecek 	case ETHTOOL_GSET:
26079ce48e5aSMichal Kubecek 	case ETHTOOL_GDRVINFO:
26089ce48e5aSMichal Kubecek 	case ETHTOOL_GMSGLVL:
26099ce48e5aSMichal Kubecek 	case ETHTOOL_GLINK:
26109ce48e5aSMichal Kubecek 	case ETHTOOL_GCOALESCE:
26119ce48e5aSMichal Kubecek 	case ETHTOOL_GRINGPARAM:
26129ce48e5aSMichal Kubecek 	case ETHTOOL_GPAUSEPARAM:
26139ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCSUM:
26149ce48e5aSMichal Kubecek 	case ETHTOOL_GTXCSUM:
26159ce48e5aSMichal Kubecek 	case ETHTOOL_GSG:
26169ce48e5aSMichal Kubecek 	case ETHTOOL_GSSET_INFO:
26179ce48e5aSMichal Kubecek 	case ETHTOOL_GSTRINGS:
26189ce48e5aSMichal Kubecek 	case ETHTOOL_GSTATS:
26199ce48e5aSMichal Kubecek 	case ETHTOOL_GPHYSTATS:
26209ce48e5aSMichal Kubecek 	case ETHTOOL_GTSO:
26219ce48e5aSMichal Kubecek 	case ETHTOOL_GPERMADDR:
26229ce48e5aSMichal Kubecek 	case ETHTOOL_GUFO:
26239ce48e5aSMichal Kubecek 	case ETHTOOL_GGSO:
26249ce48e5aSMichal Kubecek 	case ETHTOOL_GGRO:
26259ce48e5aSMichal Kubecek 	case ETHTOOL_GFLAGS:
26269ce48e5aSMichal Kubecek 	case ETHTOOL_GPFLAGS:
26279ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFH:
26289ce48e5aSMichal Kubecek 	case ETHTOOL_GRXRINGS:
26299ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLCNT:
26309ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRULE:
26319ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLALL:
26329ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFHINDIR:
26339ce48e5aSMichal Kubecek 	case ETHTOOL_GRSSH:
26349ce48e5aSMichal Kubecek 	case ETHTOOL_GFEATURES:
26359ce48e5aSMichal Kubecek 	case ETHTOOL_GCHANNELS:
26369ce48e5aSMichal Kubecek 	case ETHTOOL_GET_TS_INFO:
26379ce48e5aSMichal Kubecek 	case ETHTOOL_GEEE:
26389ce48e5aSMichal Kubecek 	case ETHTOOL_GTUNABLE:
26399ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_GTUNABLE:
26409ce48e5aSMichal Kubecek 	case ETHTOOL_GLINKSETTINGS:
26419ce48e5aSMichal Kubecek 	case ETHTOOL_GFECPARAM:
26429ce48e5aSMichal Kubecek 		break;
26439ce48e5aSMichal Kubecek 	default:
26449ce48e5aSMichal Kubecek 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
26459ce48e5aSMichal Kubecek 			return -EPERM;
26469ce48e5aSMichal Kubecek 	}
26479ce48e5aSMichal Kubecek 
26489ce48e5aSMichal Kubecek 	if (dev->ethtool_ops->begin) {
26499ce48e5aSMichal Kubecek 		rc = dev->ethtool_ops->begin(dev);
26509ce48e5aSMichal Kubecek 		if (rc  < 0)
26519ce48e5aSMichal Kubecek 			return rc;
26529ce48e5aSMichal Kubecek 	}
26539ce48e5aSMichal Kubecek 	old_features = dev->features;
26549ce48e5aSMichal Kubecek 
26559ce48e5aSMichal Kubecek 	switch (ethcmd) {
26569ce48e5aSMichal Kubecek 	case ETHTOOL_GSET:
26579ce48e5aSMichal Kubecek 		rc = ethtool_get_settings(dev, useraddr);
26589ce48e5aSMichal Kubecek 		break;
26599ce48e5aSMichal Kubecek 	case ETHTOOL_SSET:
26609ce48e5aSMichal Kubecek 		rc = ethtool_set_settings(dev, useraddr);
26619ce48e5aSMichal Kubecek 		break;
26629ce48e5aSMichal Kubecek 	case ETHTOOL_GDRVINFO:
26639ce48e5aSMichal Kubecek 		rc = ethtool_get_drvinfo(dev, useraddr);
26649ce48e5aSMichal Kubecek 		break;
26659ce48e5aSMichal Kubecek 	case ETHTOOL_GREGS:
26669ce48e5aSMichal Kubecek 		rc = ethtool_get_regs(dev, useraddr);
26679ce48e5aSMichal Kubecek 		break;
26689ce48e5aSMichal Kubecek 	case ETHTOOL_GWOL:
26699ce48e5aSMichal Kubecek 		rc = ethtool_get_wol(dev, useraddr);
26709ce48e5aSMichal Kubecek 		break;
26719ce48e5aSMichal Kubecek 	case ETHTOOL_SWOL:
26729ce48e5aSMichal Kubecek 		rc = ethtool_set_wol(dev, useraddr);
26739ce48e5aSMichal Kubecek 		break;
26749ce48e5aSMichal Kubecek 	case ETHTOOL_GMSGLVL:
26759ce48e5aSMichal Kubecek 		rc = ethtool_get_value(dev, useraddr, ethcmd,
26769ce48e5aSMichal Kubecek 				       dev->ethtool_ops->get_msglevel);
26779ce48e5aSMichal Kubecek 		break;
26789ce48e5aSMichal Kubecek 	case ETHTOOL_SMSGLVL:
26799ce48e5aSMichal Kubecek 		rc = ethtool_set_value_void(dev, useraddr,
26809ce48e5aSMichal Kubecek 				       dev->ethtool_ops->set_msglevel);
26810bda7af3SMichal Kubecek 		if (!rc)
26820bda7af3SMichal Kubecek 			ethtool_notify(dev, ETHTOOL_MSG_DEBUG_NTF, NULL);
26839ce48e5aSMichal Kubecek 		break;
26849ce48e5aSMichal Kubecek 	case ETHTOOL_GEEE:
26859ce48e5aSMichal Kubecek 		rc = ethtool_get_eee(dev, useraddr);
26869ce48e5aSMichal Kubecek 		break;
26879ce48e5aSMichal Kubecek 	case ETHTOOL_SEEE:
26889ce48e5aSMichal Kubecek 		rc = ethtool_set_eee(dev, useraddr);
26899ce48e5aSMichal Kubecek 		break;
26909ce48e5aSMichal Kubecek 	case ETHTOOL_NWAY_RST:
26919ce48e5aSMichal Kubecek 		rc = ethtool_nway_reset(dev);
26929ce48e5aSMichal Kubecek 		break;
26939ce48e5aSMichal Kubecek 	case ETHTOOL_GLINK:
26949ce48e5aSMichal Kubecek 		rc = ethtool_get_link(dev, useraddr);
26959ce48e5aSMichal Kubecek 		break;
26969ce48e5aSMichal Kubecek 	case ETHTOOL_GEEPROM:
26979ce48e5aSMichal Kubecek 		rc = ethtool_get_eeprom(dev, useraddr);
26989ce48e5aSMichal Kubecek 		break;
26999ce48e5aSMichal Kubecek 	case ETHTOOL_SEEPROM:
27009ce48e5aSMichal Kubecek 		rc = ethtool_set_eeprom(dev, useraddr);
27019ce48e5aSMichal Kubecek 		break;
27029ce48e5aSMichal Kubecek 	case ETHTOOL_GCOALESCE:
27039ce48e5aSMichal Kubecek 		rc = ethtool_get_coalesce(dev, useraddr);
27049ce48e5aSMichal Kubecek 		break;
27059ce48e5aSMichal Kubecek 	case ETHTOOL_SCOALESCE:
27069ce48e5aSMichal Kubecek 		rc = ethtool_set_coalesce(dev, useraddr);
27079ce48e5aSMichal Kubecek 		break;
27089ce48e5aSMichal Kubecek 	case ETHTOOL_GRINGPARAM:
27099ce48e5aSMichal Kubecek 		rc = ethtool_get_ringparam(dev, useraddr);
27109ce48e5aSMichal Kubecek 		break;
27119ce48e5aSMichal Kubecek 	case ETHTOOL_SRINGPARAM:
27129ce48e5aSMichal Kubecek 		rc = ethtool_set_ringparam(dev, useraddr);
27139ce48e5aSMichal Kubecek 		break;
27149ce48e5aSMichal Kubecek 	case ETHTOOL_GPAUSEPARAM:
27159ce48e5aSMichal Kubecek 		rc = ethtool_get_pauseparam(dev, useraddr);
27169ce48e5aSMichal Kubecek 		break;
27179ce48e5aSMichal Kubecek 	case ETHTOOL_SPAUSEPARAM:
27189ce48e5aSMichal Kubecek 		rc = ethtool_set_pauseparam(dev, useraddr);
27199ce48e5aSMichal Kubecek 		break;
27209ce48e5aSMichal Kubecek 	case ETHTOOL_TEST:
27219ce48e5aSMichal Kubecek 		rc = ethtool_self_test(dev, useraddr);
27229ce48e5aSMichal Kubecek 		break;
27239ce48e5aSMichal Kubecek 	case ETHTOOL_GSTRINGS:
27249ce48e5aSMichal Kubecek 		rc = ethtool_get_strings(dev, useraddr);
27259ce48e5aSMichal Kubecek 		break;
27269ce48e5aSMichal Kubecek 	case ETHTOOL_PHYS_ID:
27279ce48e5aSMichal Kubecek 		rc = ethtool_phys_id(dev, useraddr);
27289ce48e5aSMichal Kubecek 		break;
27299ce48e5aSMichal Kubecek 	case ETHTOOL_GSTATS:
27309ce48e5aSMichal Kubecek 		rc = ethtool_get_stats(dev, useraddr);
27319ce48e5aSMichal Kubecek 		break;
27329ce48e5aSMichal Kubecek 	case ETHTOOL_GPERMADDR:
27339ce48e5aSMichal Kubecek 		rc = ethtool_get_perm_addr(dev, useraddr);
27349ce48e5aSMichal Kubecek 		break;
27359ce48e5aSMichal Kubecek 	case ETHTOOL_GFLAGS:
27369ce48e5aSMichal Kubecek 		rc = ethtool_get_value(dev, useraddr, ethcmd,
27379ce48e5aSMichal Kubecek 					__ethtool_get_flags);
27389ce48e5aSMichal Kubecek 		break;
27399ce48e5aSMichal Kubecek 	case ETHTOOL_SFLAGS:
27409ce48e5aSMichal Kubecek 		rc = ethtool_set_value(dev, useraddr, __ethtool_set_flags);
27419ce48e5aSMichal Kubecek 		break;
27429ce48e5aSMichal Kubecek 	case ETHTOOL_GPFLAGS:
27439ce48e5aSMichal Kubecek 		rc = ethtool_get_value(dev, useraddr, ethcmd,
27449ce48e5aSMichal Kubecek 				       dev->ethtool_ops->get_priv_flags);
2745111dcba3SMichal Kubecek 		if (!rc)
2746111dcba3SMichal Kubecek 			ethtool_notify(dev, ETHTOOL_MSG_PRIVFLAGS_NTF, NULL);
27479ce48e5aSMichal Kubecek 		break;
27489ce48e5aSMichal Kubecek 	case ETHTOOL_SPFLAGS:
27499ce48e5aSMichal Kubecek 		rc = ethtool_set_value(dev, useraddr,
27509ce48e5aSMichal Kubecek 				       dev->ethtool_ops->set_priv_flags);
27519ce48e5aSMichal Kubecek 		break;
27529ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFH:
27539ce48e5aSMichal Kubecek 	case ETHTOOL_GRXRINGS:
27549ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLCNT:
27559ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRULE:
27569ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCLSRLALL:
27579ce48e5aSMichal Kubecek 		rc = ethtool_get_rxnfc(dev, ethcmd, useraddr);
27589ce48e5aSMichal Kubecek 		break;
27599ce48e5aSMichal Kubecek 	case ETHTOOL_SRXFH:
27609ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCLSRLDEL:
27619ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCLSRLINS:
27629ce48e5aSMichal Kubecek 		rc = ethtool_set_rxnfc(dev, ethcmd, useraddr);
27639ce48e5aSMichal Kubecek 		break;
27649ce48e5aSMichal Kubecek 	case ETHTOOL_FLASHDEV:
27659ce48e5aSMichal Kubecek 		rc = ethtool_flash_device(dev, useraddr);
27669ce48e5aSMichal Kubecek 		break;
27679ce48e5aSMichal Kubecek 	case ETHTOOL_RESET:
27689ce48e5aSMichal Kubecek 		rc = ethtool_reset(dev, useraddr);
27699ce48e5aSMichal Kubecek 		break;
27709ce48e5aSMichal Kubecek 	case ETHTOOL_GSSET_INFO:
27719ce48e5aSMichal Kubecek 		rc = ethtool_get_sset_info(dev, useraddr);
27729ce48e5aSMichal Kubecek 		break;
27739ce48e5aSMichal Kubecek 	case ETHTOOL_GRXFHINDIR:
27749ce48e5aSMichal Kubecek 		rc = ethtool_get_rxfh_indir(dev, useraddr);
27759ce48e5aSMichal Kubecek 		break;
27769ce48e5aSMichal Kubecek 	case ETHTOOL_SRXFHINDIR:
27779ce48e5aSMichal Kubecek 		rc = ethtool_set_rxfh_indir(dev, useraddr);
27789ce48e5aSMichal Kubecek 		break;
27799ce48e5aSMichal Kubecek 	case ETHTOOL_GRSSH:
27809ce48e5aSMichal Kubecek 		rc = ethtool_get_rxfh(dev, useraddr);
27819ce48e5aSMichal Kubecek 		break;
27829ce48e5aSMichal Kubecek 	case ETHTOOL_SRSSH:
27839ce48e5aSMichal Kubecek 		rc = ethtool_set_rxfh(dev, useraddr);
27849ce48e5aSMichal Kubecek 		break;
27859ce48e5aSMichal Kubecek 	case ETHTOOL_GFEATURES:
27869ce48e5aSMichal Kubecek 		rc = ethtool_get_features(dev, useraddr);
27879ce48e5aSMichal Kubecek 		break;
27889ce48e5aSMichal Kubecek 	case ETHTOOL_SFEATURES:
27899ce48e5aSMichal Kubecek 		rc = ethtool_set_features(dev, useraddr);
27909ce48e5aSMichal Kubecek 		break;
27919ce48e5aSMichal Kubecek 	case ETHTOOL_GTXCSUM:
27929ce48e5aSMichal Kubecek 	case ETHTOOL_GRXCSUM:
27939ce48e5aSMichal Kubecek 	case ETHTOOL_GSG:
27949ce48e5aSMichal Kubecek 	case ETHTOOL_GTSO:
27959ce48e5aSMichal Kubecek 	case ETHTOOL_GGSO:
27969ce48e5aSMichal Kubecek 	case ETHTOOL_GGRO:
27979ce48e5aSMichal Kubecek 		rc = ethtool_get_one_feature(dev, useraddr, ethcmd);
27989ce48e5aSMichal Kubecek 		break;
27999ce48e5aSMichal Kubecek 	case ETHTOOL_STXCSUM:
28009ce48e5aSMichal Kubecek 	case ETHTOOL_SRXCSUM:
28019ce48e5aSMichal Kubecek 	case ETHTOOL_SSG:
28029ce48e5aSMichal Kubecek 	case ETHTOOL_STSO:
28039ce48e5aSMichal Kubecek 	case ETHTOOL_SGSO:
28049ce48e5aSMichal Kubecek 	case ETHTOOL_SGRO:
28059ce48e5aSMichal Kubecek 		rc = ethtool_set_one_feature(dev, useraddr, ethcmd);
28069ce48e5aSMichal Kubecek 		break;
28079ce48e5aSMichal Kubecek 	case ETHTOOL_GCHANNELS:
28089ce48e5aSMichal Kubecek 		rc = ethtool_get_channels(dev, useraddr);
28099ce48e5aSMichal Kubecek 		break;
28109ce48e5aSMichal Kubecek 	case ETHTOOL_SCHANNELS:
28119ce48e5aSMichal Kubecek 		rc = ethtool_set_channels(dev, useraddr);
28129ce48e5aSMichal Kubecek 		break;
28139ce48e5aSMichal Kubecek 	case ETHTOOL_SET_DUMP:
28149ce48e5aSMichal Kubecek 		rc = ethtool_set_dump(dev, useraddr);
28159ce48e5aSMichal Kubecek 		break;
28169ce48e5aSMichal Kubecek 	case ETHTOOL_GET_DUMP_FLAG:
28179ce48e5aSMichal Kubecek 		rc = ethtool_get_dump_flag(dev, useraddr);
28189ce48e5aSMichal Kubecek 		break;
28199ce48e5aSMichal Kubecek 	case ETHTOOL_GET_DUMP_DATA:
28209ce48e5aSMichal Kubecek 		rc = ethtool_get_dump_data(dev, useraddr);
28219ce48e5aSMichal Kubecek 		break;
28229ce48e5aSMichal Kubecek 	case ETHTOOL_GET_TS_INFO:
28239ce48e5aSMichal Kubecek 		rc = ethtool_get_ts_info(dev, useraddr);
28249ce48e5aSMichal Kubecek 		break;
28259ce48e5aSMichal Kubecek 	case ETHTOOL_GMODULEINFO:
28269ce48e5aSMichal Kubecek 		rc = ethtool_get_module_info(dev, useraddr);
28279ce48e5aSMichal Kubecek 		break;
28289ce48e5aSMichal Kubecek 	case ETHTOOL_GMODULEEEPROM:
28299ce48e5aSMichal Kubecek 		rc = ethtool_get_module_eeprom(dev, useraddr);
28309ce48e5aSMichal Kubecek 		break;
28319ce48e5aSMichal Kubecek 	case ETHTOOL_GTUNABLE:
28329ce48e5aSMichal Kubecek 		rc = ethtool_get_tunable(dev, useraddr);
28339ce48e5aSMichal Kubecek 		break;
28349ce48e5aSMichal Kubecek 	case ETHTOOL_STUNABLE:
28359ce48e5aSMichal Kubecek 		rc = ethtool_set_tunable(dev, useraddr);
28369ce48e5aSMichal Kubecek 		break;
28379ce48e5aSMichal Kubecek 	case ETHTOOL_GPHYSTATS:
28389ce48e5aSMichal Kubecek 		rc = ethtool_get_phy_stats(dev, useraddr);
28399ce48e5aSMichal Kubecek 		break;
28409ce48e5aSMichal Kubecek 	case ETHTOOL_PERQUEUE:
28419ce48e5aSMichal Kubecek 		rc = ethtool_set_per_queue(dev, useraddr, sub_cmd);
28429ce48e5aSMichal Kubecek 		break;
28439ce48e5aSMichal Kubecek 	case ETHTOOL_GLINKSETTINGS:
28449ce48e5aSMichal Kubecek 		rc = ethtool_get_link_ksettings(dev, useraddr);
28459ce48e5aSMichal Kubecek 		break;
28469ce48e5aSMichal Kubecek 	case ETHTOOL_SLINKSETTINGS:
28479ce48e5aSMichal Kubecek 		rc = ethtool_set_link_ksettings(dev, useraddr);
28489ce48e5aSMichal Kubecek 		break;
28499ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_GTUNABLE:
28509ce48e5aSMichal Kubecek 		rc = get_phy_tunable(dev, useraddr);
28519ce48e5aSMichal Kubecek 		break;
28529ce48e5aSMichal Kubecek 	case ETHTOOL_PHY_STUNABLE:
28539ce48e5aSMichal Kubecek 		rc = set_phy_tunable(dev, useraddr);
28549ce48e5aSMichal Kubecek 		break;
28559ce48e5aSMichal Kubecek 	case ETHTOOL_GFECPARAM:
28569ce48e5aSMichal Kubecek 		rc = ethtool_get_fecparam(dev, useraddr);
28579ce48e5aSMichal Kubecek 		break;
28589ce48e5aSMichal Kubecek 	case ETHTOOL_SFECPARAM:
28599ce48e5aSMichal Kubecek 		rc = ethtool_set_fecparam(dev, useraddr);
28609ce48e5aSMichal Kubecek 		break;
28619ce48e5aSMichal Kubecek 	default:
28629ce48e5aSMichal Kubecek 		rc = -EOPNOTSUPP;
28639ce48e5aSMichal Kubecek 	}
28649ce48e5aSMichal Kubecek 
28659ce48e5aSMichal Kubecek 	if (dev->ethtool_ops->complete)
28669ce48e5aSMichal Kubecek 		dev->ethtool_ops->complete(dev);
28679ce48e5aSMichal Kubecek 
28689ce48e5aSMichal Kubecek 	if (old_features != dev->features)
28699ce48e5aSMichal Kubecek 		netdev_features_change(dev);
28709ce48e5aSMichal Kubecek 
28719ce48e5aSMichal Kubecek 	return rc;
28729ce48e5aSMichal Kubecek }
28739ce48e5aSMichal Kubecek 
28749ce48e5aSMichal Kubecek struct ethtool_rx_flow_key {
28759ce48e5aSMichal Kubecek 	struct flow_dissector_key_basic			basic;
28769ce48e5aSMichal Kubecek 	union {
28779ce48e5aSMichal Kubecek 		struct flow_dissector_key_ipv4_addrs	ipv4;
28789ce48e5aSMichal Kubecek 		struct flow_dissector_key_ipv6_addrs	ipv6;
28799ce48e5aSMichal Kubecek 	};
28809ce48e5aSMichal Kubecek 	struct flow_dissector_key_ports			tp;
28819ce48e5aSMichal Kubecek 	struct flow_dissector_key_ip			ip;
28829ce48e5aSMichal Kubecek 	struct flow_dissector_key_vlan			vlan;
28839ce48e5aSMichal Kubecek 	struct flow_dissector_key_eth_addrs		eth_addrs;
28849ce48e5aSMichal Kubecek } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
28859ce48e5aSMichal Kubecek 
28869ce48e5aSMichal Kubecek struct ethtool_rx_flow_match {
28879ce48e5aSMichal Kubecek 	struct flow_dissector		dissector;
28889ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_key	key;
28899ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_key	mask;
28909ce48e5aSMichal Kubecek };
28919ce48e5aSMichal Kubecek 
28929ce48e5aSMichal Kubecek struct ethtool_rx_flow_rule *
28939ce48e5aSMichal Kubecek ethtool_rx_flow_rule_create(const struct ethtool_rx_flow_spec_input *input)
28949ce48e5aSMichal Kubecek {
28959ce48e5aSMichal Kubecek 	const struct ethtool_rx_flow_spec *fs = input->fs;
28969ce48e5aSMichal Kubecek 	static struct in6_addr zero_addr = {};
28979ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_match *match;
28989ce48e5aSMichal Kubecek 	struct ethtool_rx_flow_rule *flow;
28999ce48e5aSMichal Kubecek 	struct flow_action_entry *act;
29009ce48e5aSMichal Kubecek 
29019ce48e5aSMichal Kubecek 	flow = kzalloc(sizeof(struct ethtool_rx_flow_rule) +
29029ce48e5aSMichal Kubecek 		       sizeof(struct ethtool_rx_flow_match), GFP_KERNEL);
29039ce48e5aSMichal Kubecek 	if (!flow)
29049ce48e5aSMichal Kubecek 		return ERR_PTR(-ENOMEM);
29059ce48e5aSMichal Kubecek 
29069ce48e5aSMichal Kubecek 	/* ethtool_rx supports only one single action per rule. */
29079ce48e5aSMichal Kubecek 	flow->rule = flow_rule_alloc(1);
29089ce48e5aSMichal Kubecek 	if (!flow->rule) {
29099ce48e5aSMichal Kubecek 		kfree(flow);
29109ce48e5aSMichal Kubecek 		return ERR_PTR(-ENOMEM);
29119ce48e5aSMichal Kubecek 	}
29129ce48e5aSMichal Kubecek 
29139ce48e5aSMichal Kubecek 	match = (struct ethtool_rx_flow_match *)flow->priv;
29149ce48e5aSMichal Kubecek 	flow->rule->match.dissector	= &match->dissector;
29159ce48e5aSMichal Kubecek 	flow->rule->match.mask		= &match->mask;
29169ce48e5aSMichal Kubecek 	flow->rule->match.key		= &match->key;
29179ce48e5aSMichal Kubecek 
29189ce48e5aSMichal Kubecek 	match->mask.basic.n_proto = htons(0xffff);
29199ce48e5aSMichal Kubecek 
29209ce48e5aSMichal Kubecek 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
29219ce48e5aSMichal Kubecek 	case ETHER_FLOW: {
29229ce48e5aSMichal Kubecek 		const struct ethhdr *ether_spec, *ether_m_spec;
29239ce48e5aSMichal Kubecek 
29249ce48e5aSMichal Kubecek 		ether_spec = &fs->h_u.ether_spec;
29259ce48e5aSMichal Kubecek 		ether_m_spec = &fs->m_u.ether_spec;
29269ce48e5aSMichal Kubecek 
29279ce48e5aSMichal Kubecek 		if (!is_zero_ether_addr(ether_m_spec->h_source)) {
29289ce48e5aSMichal Kubecek 			ether_addr_copy(match->key.eth_addrs.src,
29299ce48e5aSMichal Kubecek 					ether_spec->h_source);
29309ce48e5aSMichal Kubecek 			ether_addr_copy(match->mask.eth_addrs.src,
29319ce48e5aSMichal Kubecek 					ether_m_spec->h_source);
29329ce48e5aSMichal Kubecek 		}
29339ce48e5aSMichal Kubecek 		if (!is_zero_ether_addr(ether_m_spec->h_dest)) {
29349ce48e5aSMichal Kubecek 			ether_addr_copy(match->key.eth_addrs.dst,
29359ce48e5aSMichal Kubecek 					ether_spec->h_dest);
29369ce48e5aSMichal Kubecek 			ether_addr_copy(match->mask.eth_addrs.dst,
29379ce48e5aSMichal Kubecek 					ether_m_spec->h_dest);
29389ce48e5aSMichal Kubecek 		}
29399ce48e5aSMichal Kubecek 		if (ether_m_spec->h_proto) {
29409ce48e5aSMichal Kubecek 			match->key.basic.n_proto = ether_spec->h_proto;
29419ce48e5aSMichal Kubecek 			match->mask.basic.n_proto = ether_m_spec->h_proto;
29429ce48e5aSMichal Kubecek 		}
29439ce48e5aSMichal Kubecek 		}
29449ce48e5aSMichal Kubecek 		break;
29459ce48e5aSMichal Kubecek 	case TCP_V4_FLOW:
29469ce48e5aSMichal Kubecek 	case UDP_V4_FLOW: {
29479ce48e5aSMichal Kubecek 		const struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
29489ce48e5aSMichal Kubecek 
29499ce48e5aSMichal Kubecek 		match->key.basic.n_proto = htons(ETH_P_IP);
29509ce48e5aSMichal Kubecek 
29519ce48e5aSMichal Kubecek 		v4_spec = &fs->h_u.tcp_ip4_spec;
29529ce48e5aSMichal Kubecek 		v4_m_spec = &fs->m_u.tcp_ip4_spec;
29539ce48e5aSMichal Kubecek 
29549ce48e5aSMichal Kubecek 		if (v4_m_spec->ip4src) {
29559ce48e5aSMichal Kubecek 			match->key.ipv4.src = v4_spec->ip4src;
29569ce48e5aSMichal Kubecek 			match->mask.ipv4.src = v4_m_spec->ip4src;
29579ce48e5aSMichal Kubecek 		}
29589ce48e5aSMichal Kubecek 		if (v4_m_spec->ip4dst) {
29599ce48e5aSMichal Kubecek 			match->key.ipv4.dst = v4_spec->ip4dst;
29609ce48e5aSMichal Kubecek 			match->mask.ipv4.dst = v4_m_spec->ip4dst;
29619ce48e5aSMichal Kubecek 		}
29629ce48e5aSMichal Kubecek 		if (v4_m_spec->ip4src ||
29639ce48e5aSMichal Kubecek 		    v4_m_spec->ip4dst) {
29649ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
29659ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS);
29669ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV4_ADDRS] =
29679ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ipv4);
29689ce48e5aSMichal Kubecek 		}
29699ce48e5aSMichal Kubecek 		if (v4_m_spec->psrc) {
29709ce48e5aSMichal Kubecek 			match->key.tp.src = v4_spec->psrc;
29719ce48e5aSMichal Kubecek 			match->mask.tp.src = v4_m_spec->psrc;
29729ce48e5aSMichal Kubecek 		}
29739ce48e5aSMichal Kubecek 		if (v4_m_spec->pdst) {
29749ce48e5aSMichal Kubecek 			match->key.tp.dst = v4_spec->pdst;
29759ce48e5aSMichal Kubecek 			match->mask.tp.dst = v4_m_spec->pdst;
29769ce48e5aSMichal Kubecek 		}
29779ce48e5aSMichal Kubecek 		if (v4_m_spec->psrc ||
29789ce48e5aSMichal Kubecek 		    v4_m_spec->pdst) {
29799ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
29809ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_PORTS);
29819ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
29829ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, tp);
29839ce48e5aSMichal Kubecek 		}
29849ce48e5aSMichal Kubecek 		if (v4_m_spec->tos) {
29859ce48e5aSMichal Kubecek 			match->key.ip.tos = v4_spec->tos;
29869ce48e5aSMichal Kubecek 			match->mask.ip.tos = v4_m_spec->tos;
29879ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
29889ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_IP);
29899ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
29909ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ip);
29919ce48e5aSMichal Kubecek 		}
29929ce48e5aSMichal Kubecek 		}
29939ce48e5aSMichal Kubecek 		break;
29949ce48e5aSMichal Kubecek 	case TCP_V6_FLOW:
29959ce48e5aSMichal Kubecek 	case UDP_V6_FLOW: {
29969ce48e5aSMichal Kubecek 		const struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
29979ce48e5aSMichal Kubecek 
29989ce48e5aSMichal Kubecek 		match->key.basic.n_proto = htons(ETH_P_IPV6);
29999ce48e5aSMichal Kubecek 
30009ce48e5aSMichal Kubecek 		v6_spec = &fs->h_u.tcp_ip6_spec;
30019ce48e5aSMichal Kubecek 		v6_m_spec = &fs->m_u.tcp_ip6_spec;
30029ce48e5aSMichal Kubecek 		if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr))) {
30039ce48e5aSMichal Kubecek 			memcpy(&match->key.ipv6.src, v6_spec->ip6src,
30049ce48e5aSMichal Kubecek 			       sizeof(match->key.ipv6.src));
30059ce48e5aSMichal Kubecek 			memcpy(&match->mask.ipv6.src, v6_m_spec->ip6src,
30069ce48e5aSMichal Kubecek 			       sizeof(match->mask.ipv6.src));
30079ce48e5aSMichal Kubecek 		}
30089ce48e5aSMichal Kubecek 		if (memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
30099ce48e5aSMichal Kubecek 			memcpy(&match->key.ipv6.dst, v6_spec->ip6dst,
30109ce48e5aSMichal Kubecek 			       sizeof(match->key.ipv6.dst));
30119ce48e5aSMichal Kubecek 			memcpy(&match->mask.ipv6.dst, v6_m_spec->ip6dst,
30129ce48e5aSMichal Kubecek 			       sizeof(match->mask.ipv6.dst));
30139ce48e5aSMichal Kubecek 		}
30149ce48e5aSMichal Kubecek 		if (memcmp(v6_m_spec->ip6src, &zero_addr, sizeof(zero_addr)) ||
301521a739c6SGaurav Singh 		    memcmp(v6_m_spec->ip6dst, &zero_addr, sizeof(zero_addr))) {
30169ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
30179ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS);
30189ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IPV6_ADDRS] =
30199ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ipv6);
30209ce48e5aSMichal Kubecek 		}
30219ce48e5aSMichal Kubecek 		if (v6_m_spec->psrc) {
30229ce48e5aSMichal Kubecek 			match->key.tp.src = v6_spec->psrc;
30239ce48e5aSMichal Kubecek 			match->mask.tp.src = v6_m_spec->psrc;
30249ce48e5aSMichal Kubecek 		}
30259ce48e5aSMichal Kubecek 		if (v6_m_spec->pdst) {
30269ce48e5aSMichal Kubecek 			match->key.tp.dst = v6_spec->pdst;
30279ce48e5aSMichal Kubecek 			match->mask.tp.dst = v6_m_spec->pdst;
30289ce48e5aSMichal Kubecek 		}
30299ce48e5aSMichal Kubecek 		if (v6_m_spec->psrc ||
30309ce48e5aSMichal Kubecek 		    v6_m_spec->pdst) {
30319ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
30329ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_PORTS);
30339ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_PORTS] =
30349ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, tp);
30359ce48e5aSMichal Kubecek 		}
30369ce48e5aSMichal Kubecek 		if (v6_m_spec->tclass) {
30379ce48e5aSMichal Kubecek 			match->key.ip.tos = v6_spec->tclass;
30389ce48e5aSMichal Kubecek 			match->mask.ip.tos = v6_m_spec->tclass;
30399ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
30409ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_IP);
30419ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_IP] =
30429ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, ip);
30439ce48e5aSMichal Kubecek 		}
30449ce48e5aSMichal Kubecek 		}
30459ce48e5aSMichal Kubecek 		break;
30469ce48e5aSMichal Kubecek 	default:
30479ce48e5aSMichal Kubecek 		ethtool_rx_flow_rule_destroy(flow);
30489ce48e5aSMichal Kubecek 		return ERR_PTR(-EINVAL);
30499ce48e5aSMichal Kubecek 	}
30509ce48e5aSMichal Kubecek 
30519ce48e5aSMichal Kubecek 	switch (fs->flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS)) {
30529ce48e5aSMichal Kubecek 	case TCP_V4_FLOW:
30539ce48e5aSMichal Kubecek 	case TCP_V6_FLOW:
30549ce48e5aSMichal Kubecek 		match->key.basic.ip_proto = IPPROTO_TCP;
3055d0a84e1fSVishal Kulkarni 		match->mask.basic.ip_proto = 0xff;
30569ce48e5aSMichal Kubecek 		break;
30579ce48e5aSMichal Kubecek 	case UDP_V4_FLOW:
30589ce48e5aSMichal Kubecek 	case UDP_V6_FLOW:
30599ce48e5aSMichal Kubecek 		match->key.basic.ip_proto = IPPROTO_UDP;
3060d0a84e1fSVishal Kulkarni 		match->mask.basic.ip_proto = 0xff;
30619ce48e5aSMichal Kubecek 		break;
30629ce48e5aSMichal Kubecek 	}
30639ce48e5aSMichal Kubecek 
30649ce48e5aSMichal Kubecek 	match->dissector.used_keys |= BIT(FLOW_DISSECTOR_KEY_BASIC);
30659ce48e5aSMichal Kubecek 	match->dissector.offset[FLOW_DISSECTOR_KEY_BASIC] =
30669ce48e5aSMichal Kubecek 		offsetof(struct ethtool_rx_flow_key, basic);
30679ce48e5aSMichal Kubecek 
30689ce48e5aSMichal Kubecek 	if (fs->flow_type & FLOW_EXT) {
30699ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
30709ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
30719ce48e5aSMichal Kubecek 
30729ce48e5aSMichal Kubecek 		if (ext_m_spec->vlan_etype) {
30739ce48e5aSMichal Kubecek 			match->key.vlan.vlan_tpid = ext_h_spec->vlan_etype;
30749ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_tpid = ext_m_spec->vlan_etype;
30759ce48e5aSMichal Kubecek 		}
30769ce48e5aSMichal Kubecek 
30779ce48e5aSMichal Kubecek 		if (ext_m_spec->vlan_tci) {
30789ce48e5aSMichal Kubecek 			match->key.vlan.vlan_id =
30799ce48e5aSMichal Kubecek 				ntohs(ext_h_spec->vlan_tci) & 0x0fff;
30809ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_id =
30819ce48e5aSMichal Kubecek 				ntohs(ext_m_spec->vlan_tci) & 0x0fff;
30829ce48e5aSMichal Kubecek 
30839ce48e5aSMichal Kubecek 			match->key.vlan.vlan_dei =
30849ce48e5aSMichal Kubecek 				!!(ext_h_spec->vlan_tci & htons(0x1000));
30859ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_dei =
30869ce48e5aSMichal Kubecek 				!!(ext_m_spec->vlan_tci & htons(0x1000));
30879ce48e5aSMichal Kubecek 
30889ce48e5aSMichal Kubecek 			match->key.vlan.vlan_priority =
30899ce48e5aSMichal Kubecek 				(ntohs(ext_h_spec->vlan_tci) & 0xe000) >> 13;
30909ce48e5aSMichal Kubecek 			match->mask.vlan.vlan_priority =
30919ce48e5aSMichal Kubecek 				(ntohs(ext_m_spec->vlan_tci) & 0xe000) >> 13;
30929ce48e5aSMichal Kubecek 		}
30939ce48e5aSMichal Kubecek 
30949ce48e5aSMichal Kubecek 		if (ext_m_spec->vlan_etype ||
30959ce48e5aSMichal Kubecek 		    ext_m_spec->vlan_tci) {
30969ce48e5aSMichal Kubecek 			match->dissector.used_keys |=
30979ce48e5aSMichal Kubecek 				BIT(FLOW_DISSECTOR_KEY_VLAN);
30989ce48e5aSMichal Kubecek 			match->dissector.offset[FLOW_DISSECTOR_KEY_VLAN] =
30999ce48e5aSMichal Kubecek 				offsetof(struct ethtool_rx_flow_key, vlan);
31009ce48e5aSMichal Kubecek 		}
31019ce48e5aSMichal Kubecek 	}
31029ce48e5aSMichal Kubecek 	if (fs->flow_type & FLOW_MAC_EXT) {
31039ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_h_spec = &fs->h_ext;
31049ce48e5aSMichal Kubecek 		const struct ethtool_flow_ext *ext_m_spec = &fs->m_ext;
31059ce48e5aSMichal Kubecek 
31069ce48e5aSMichal Kubecek 		memcpy(match->key.eth_addrs.dst, ext_h_spec->h_dest,
31079ce48e5aSMichal Kubecek 		       ETH_ALEN);
31089ce48e5aSMichal Kubecek 		memcpy(match->mask.eth_addrs.dst, ext_m_spec->h_dest,
31099ce48e5aSMichal Kubecek 		       ETH_ALEN);
31109ce48e5aSMichal Kubecek 
31119ce48e5aSMichal Kubecek 		match->dissector.used_keys |=
31129ce48e5aSMichal Kubecek 			BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS);
31139ce48e5aSMichal Kubecek 		match->dissector.offset[FLOW_DISSECTOR_KEY_ETH_ADDRS] =
31149ce48e5aSMichal Kubecek 			offsetof(struct ethtool_rx_flow_key, eth_addrs);
31159ce48e5aSMichal Kubecek 	}
31169ce48e5aSMichal Kubecek 
31179ce48e5aSMichal Kubecek 	act = &flow->rule->action.entries[0];
31189ce48e5aSMichal Kubecek 	switch (fs->ring_cookie) {
31199ce48e5aSMichal Kubecek 	case RX_CLS_FLOW_DISC:
31209ce48e5aSMichal Kubecek 		act->id = FLOW_ACTION_DROP;
31219ce48e5aSMichal Kubecek 		break;
31229ce48e5aSMichal Kubecek 	case RX_CLS_FLOW_WAKE:
31239ce48e5aSMichal Kubecek 		act->id = FLOW_ACTION_WAKE;
31249ce48e5aSMichal Kubecek 		break;
31259ce48e5aSMichal Kubecek 	default:
31269ce48e5aSMichal Kubecek 		act->id = FLOW_ACTION_QUEUE;
31279ce48e5aSMichal Kubecek 		if (fs->flow_type & FLOW_RSS)
31289ce48e5aSMichal Kubecek 			act->queue.ctx = input->rss_ctx;
31299ce48e5aSMichal Kubecek 
31309ce48e5aSMichal Kubecek 		act->queue.vf = ethtool_get_flow_spec_ring_vf(fs->ring_cookie);
31319ce48e5aSMichal Kubecek 		act->queue.index = ethtool_get_flow_spec_ring(fs->ring_cookie);
31329ce48e5aSMichal Kubecek 		break;
31339ce48e5aSMichal Kubecek 	}
31349ce48e5aSMichal Kubecek 
31359ce48e5aSMichal Kubecek 	return flow;
31369ce48e5aSMichal Kubecek }
31379ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_rx_flow_rule_create);
31389ce48e5aSMichal Kubecek 
31399ce48e5aSMichal Kubecek void ethtool_rx_flow_rule_destroy(struct ethtool_rx_flow_rule *flow)
31409ce48e5aSMichal Kubecek {
31419ce48e5aSMichal Kubecek 	kfree(flow->rule);
31429ce48e5aSMichal Kubecek 	kfree(flow);
31439ce48e5aSMichal Kubecek }
31449ce48e5aSMichal Kubecek EXPORT_SYMBOL(ethtool_rx_flow_rule_destroy);
3145