xref: /kernel/linux/linux-6.6/net/ethtool/stats.c (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#include "netlink.h"
462306a36Sopenharmony_ci#include "common.h"
562306a36Sopenharmony_ci#include "bitset.h"
662306a36Sopenharmony_ci
762306a36Sopenharmony_cistruct stats_req_info {
862306a36Sopenharmony_ci	struct ethnl_req_info		base;
962306a36Sopenharmony_ci	DECLARE_BITMAP(stat_mask, __ETHTOOL_STATS_CNT);
1062306a36Sopenharmony_ci	enum ethtool_mac_stats_src	src;
1162306a36Sopenharmony_ci};
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#define STATS_REQINFO(__req_base) \
1462306a36Sopenharmony_ci	container_of(__req_base, struct stats_req_info, base)
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistruct stats_reply_data {
1762306a36Sopenharmony_ci	struct ethnl_reply_data		base;
1862306a36Sopenharmony_ci	struct_group(stats,
1962306a36Sopenharmony_ci		struct ethtool_eth_phy_stats	phy_stats;
2062306a36Sopenharmony_ci		struct ethtool_eth_mac_stats	mac_stats;
2162306a36Sopenharmony_ci		struct ethtool_eth_ctrl_stats	ctrl_stats;
2262306a36Sopenharmony_ci		struct ethtool_rmon_stats	rmon_stats;
2362306a36Sopenharmony_ci	);
2462306a36Sopenharmony_ci	const struct ethtool_rmon_hist_range	*rmon_ranges;
2562306a36Sopenharmony_ci};
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define STATS_REPDATA(__reply_base) \
2862306a36Sopenharmony_ci	container_of(__reply_base, struct stats_reply_data, base)
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ciconst char stats_std_names[__ETHTOOL_STATS_CNT][ETH_GSTRING_LEN] = {
3162306a36Sopenharmony_ci	[ETHTOOL_STATS_ETH_PHY]			= "eth-phy",
3262306a36Sopenharmony_ci	[ETHTOOL_STATS_ETH_MAC]			= "eth-mac",
3362306a36Sopenharmony_ci	[ETHTOOL_STATS_ETH_CTRL]		= "eth-ctrl",
3462306a36Sopenharmony_ci	[ETHTOOL_STATS_RMON]			= "rmon",
3562306a36Sopenharmony_ci};
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ciconst char stats_eth_phy_names[__ETHTOOL_A_STATS_ETH_PHY_CNT][ETH_GSTRING_LEN] = {
3862306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR]	= "SymbolErrorDuringCarrier",
3962306a36Sopenharmony_ci};
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ciconst char stats_eth_mac_names[__ETHTOOL_A_STATS_ETH_MAC_CNT][ETH_GSTRING_LEN] = {
4262306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_2_TX_PKT]	= "FramesTransmittedOK",
4362306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_3_SINGLE_COL]	= "SingleCollisionFrames",
4462306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_4_MULTI_COL]	= "MultipleCollisionFrames",
4562306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_5_RX_PKT]	= "FramesReceivedOK",
4662306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_6_FCS_ERR]	= "FrameCheckSequenceErrors",
4762306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_7_ALIGN_ERR]	= "AlignmentErrors",
4862306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_8_TX_BYTES]	= "OctetsTransmittedOK",
4962306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_9_TX_DEFER]	= "FramesWithDeferredXmissions",
5062306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_10_LATE_COL]	= "LateCollisions",
5162306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_11_XS_COL]	= "FramesAbortedDueToXSColls",
5262306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_12_TX_INT_ERR]	= "FramesLostDueToIntMACXmitError",
5362306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_13_CS_ERR]	= "CarrierSenseErrors",
5462306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_14_RX_BYTES]	= "OctetsReceivedOK",
5562306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_15_RX_INT_ERR]	= "FramesLostDueToIntMACRcvError",
5662306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_18_TX_MCAST]	= "MulticastFramesXmittedOK",
5762306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_19_TX_BCAST]	= "BroadcastFramesXmittedOK",
5862306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_20_XS_DEFER]	= "FramesWithExcessiveDeferral",
5962306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_21_RX_MCAST]	= "MulticastFramesReceivedOK",
6062306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_22_RX_BCAST]	= "BroadcastFramesReceivedOK",
6162306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_23_IR_LEN_ERR]	= "InRangeLengthErrors",
6262306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_24_OOR_LEN]	= "OutOfRangeLengthField",
6362306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_MAC_25_TOO_LONG_ERR]	= "FrameTooLongErrors",
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ciconst char stats_eth_ctrl_names[__ETHTOOL_A_STATS_ETH_CTRL_CNT][ETH_GSTRING_LEN] = {
6762306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_CTRL_3_TX]		= "MACControlFramesTransmitted",
6862306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_CTRL_4_RX]		= "MACControlFramesReceived",
6962306a36Sopenharmony_ci	[ETHTOOL_A_STATS_ETH_CTRL_5_RX_UNSUP]	= "UnsupportedOpcodesReceived",
7062306a36Sopenharmony_ci};
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ciconst char stats_rmon_names[__ETHTOOL_A_STATS_RMON_CNT][ETH_GSTRING_LEN] = {
7362306a36Sopenharmony_ci	[ETHTOOL_A_STATS_RMON_UNDERSIZE]	= "etherStatsUndersizePkts",
7462306a36Sopenharmony_ci	[ETHTOOL_A_STATS_RMON_OVERSIZE]		= "etherStatsOversizePkts",
7562306a36Sopenharmony_ci	[ETHTOOL_A_STATS_RMON_FRAG]		= "etherStatsFragments",
7662306a36Sopenharmony_ci	[ETHTOOL_A_STATS_RMON_JABBER]		= "etherStatsJabbers",
7762306a36Sopenharmony_ci};
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciconst struct nla_policy ethnl_stats_get_policy[ETHTOOL_A_STATS_SRC + 1] = {
8062306a36Sopenharmony_ci	[ETHTOOL_A_STATS_HEADER]	=
8162306a36Sopenharmony_ci		NLA_POLICY_NESTED(ethnl_header_policy),
8262306a36Sopenharmony_ci	[ETHTOOL_A_STATS_GROUPS]	= { .type = NLA_NESTED },
8362306a36Sopenharmony_ci	[ETHTOOL_A_STATS_SRC]		=
8462306a36Sopenharmony_ci		NLA_POLICY_MAX(NLA_U32, ETHTOOL_MAC_STATS_SRC_PMAC),
8562306a36Sopenharmony_ci};
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_cistatic int stats_parse_request(struct ethnl_req_info *req_base,
8862306a36Sopenharmony_ci			       struct nlattr **tb,
8962306a36Sopenharmony_ci			       struct netlink_ext_ack *extack)
9062306a36Sopenharmony_ci{
9162306a36Sopenharmony_ci	enum ethtool_mac_stats_src src = ETHTOOL_MAC_STATS_SRC_AGGREGATE;
9262306a36Sopenharmony_ci	struct stats_req_info *req_info = STATS_REQINFO(req_base);
9362306a36Sopenharmony_ci	bool mod = false;
9462306a36Sopenharmony_ci	int err;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	err = ethnl_update_bitset(req_info->stat_mask, __ETHTOOL_STATS_CNT,
9762306a36Sopenharmony_ci				  tb[ETHTOOL_A_STATS_GROUPS], stats_std_names,
9862306a36Sopenharmony_ci				  extack, &mod);
9962306a36Sopenharmony_ci	if (err)
10062306a36Sopenharmony_ci		return err;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	if (!mod) {
10362306a36Sopenharmony_ci		NL_SET_ERR_MSG(extack, "no stats requested");
10462306a36Sopenharmony_ci		return -EINVAL;
10562306a36Sopenharmony_ci	}
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	if (tb[ETHTOOL_A_STATS_SRC])
10862306a36Sopenharmony_ci		src = nla_get_u32(tb[ETHTOOL_A_STATS_SRC]);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	req_info->src = src;
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci	return 0;
11362306a36Sopenharmony_ci}
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cistatic int stats_prepare_data(const struct ethnl_req_info *req_base,
11662306a36Sopenharmony_ci			      struct ethnl_reply_data *reply_base,
11762306a36Sopenharmony_ci			      const struct genl_info *info)
11862306a36Sopenharmony_ci{
11962306a36Sopenharmony_ci	const struct stats_req_info *req_info = STATS_REQINFO(req_base);
12062306a36Sopenharmony_ci	struct stats_reply_data *data = STATS_REPDATA(reply_base);
12162306a36Sopenharmony_ci	enum ethtool_mac_stats_src src = req_info->src;
12262306a36Sopenharmony_ci	struct net_device *dev = reply_base->dev;
12362306a36Sopenharmony_ci	int ret;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	ret = ethnl_ops_begin(dev);
12662306a36Sopenharmony_ci	if (ret < 0)
12762306a36Sopenharmony_ci		return ret;
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci	if ((src == ETHTOOL_MAC_STATS_SRC_EMAC ||
13062306a36Sopenharmony_ci	     src == ETHTOOL_MAC_STATS_SRC_PMAC) &&
13162306a36Sopenharmony_ci	    !__ethtool_dev_mm_supported(dev)) {
13262306a36Sopenharmony_ci		NL_SET_ERR_MSG_MOD(info->extack,
13362306a36Sopenharmony_ci				   "Device does not support MAC merge layer");
13462306a36Sopenharmony_ci		ethnl_ops_complete(dev);
13562306a36Sopenharmony_ci		return -EOPNOTSUPP;
13662306a36Sopenharmony_ci	}
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	/* Mark all stats as unset (see ETHTOOL_STAT_NOT_SET) to prevent them
13962306a36Sopenharmony_ci	 * from being reported to user space in case driver did not set them.
14062306a36Sopenharmony_ci	 */
14162306a36Sopenharmony_ci	memset(&data->stats, 0xff, sizeof(data->stats));
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	data->phy_stats.src = src;
14462306a36Sopenharmony_ci	data->mac_stats.src = src;
14562306a36Sopenharmony_ci	data->ctrl_stats.src = src;
14662306a36Sopenharmony_ci	data->rmon_stats.src = src;
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask) &&
14962306a36Sopenharmony_ci	    dev->ethtool_ops->get_eth_phy_stats)
15062306a36Sopenharmony_ci		dev->ethtool_ops->get_eth_phy_stats(dev, &data->phy_stats);
15162306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask) &&
15262306a36Sopenharmony_ci	    dev->ethtool_ops->get_eth_mac_stats)
15362306a36Sopenharmony_ci		dev->ethtool_ops->get_eth_mac_stats(dev, &data->mac_stats);
15462306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask) &&
15562306a36Sopenharmony_ci	    dev->ethtool_ops->get_eth_ctrl_stats)
15662306a36Sopenharmony_ci		dev->ethtool_ops->get_eth_ctrl_stats(dev, &data->ctrl_stats);
15762306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask) &&
15862306a36Sopenharmony_ci	    dev->ethtool_ops->get_rmon_stats)
15962306a36Sopenharmony_ci		dev->ethtool_ops->get_rmon_stats(dev, &data->rmon_stats,
16062306a36Sopenharmony_ci						 &data->rmon_ranges);
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	ethnl_ops_complete(dev);
16362306a36Sopenharmony_ci	return 0;
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic int stats_reply_size(const struct ethnl_req_info *req_base,
16762306a36Sopenharmony_ci			    const struct ethnl_reply_data *reply_base)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	const struct stats_req_info *req_info = STATS_REQINFO(req_base);
17062306a36Sopenharmony_ci	unsigned int n_grps = 0, n_stats = 0;
17162306a36Sopenharmony_ci	int len = 0;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	len += nla_total_size(sizeof(u32)); /* _STATS_SRC */
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask)) {
17662306a36Sopenharmony_ci		n_stats += sizeof(struct ethtool_eth_phy_stats) / sizeof(u64);
17762306a36Sopenharmony_ci		n_grps++;
17862306a36Sopenharmony_ci	}
17962306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask)) {
18062306a36Sopenharmony_ci		n_stats += sizeof(struct ethtool_eth_mac_stats) / sizeof(u64);
18162306a36Sopenharmony_ci		n_grps++;
18262306a36Sopenharmony_ci	}
18362306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask)) {
18462306a36Sopenharmony_ci		n_stats += sizeof(struct ethtool_eth_ctrl_stats) / sizeof(u64);
18562306a36Sopenharmony_ci		n_grps++;
18662306a36Sopenharmony_ci	}
18762306a36Sopenharmony_ci	if (test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask)) {
18862306a36Sopenharmony_ci		n_stats += sizeof(struct ethtool_rmon_stats) / sizeof(u64);
18962306a36Sopenharmony_ci		n_grps++;
19062306a36Sopenharmony_ci		/* Above includes the space for _A_STATS_GRP_HIST_VALs */
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci		len += (nla_total_size(0) +	/* _A_STATS_GRP_HIST */
19362306a36Sopenharmony_ci			nla_total_size(4) +	/* _A_STATS_GRP_HIST_BKT_LOW */
19462306a36Sopenharmony_ci			nla_total_size(4)) *	/* _A_STATS_GRP_HIST_BKT_HI */
19562306a36Sopenharmony_ci			ETHTOOL_RMON_HIST_MAX * 2;
19662306a36Sopenharmony_ci	}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	len += n_grps * (nla_total_size(0) + /* _A_STATS_GRP */
19962306a36Sopenharmony_ci			 nla_total_size(4) + /* _A_STATS_GRP_ID */
20062306a36Sopenharmony_ci			 nla_total_size(4)); /* _A_STATS_GRP_SS_ID */
20162306a36Sopenharmony_ci	len += n_stats * (nla_total_size(0) + /* _A_STATS_GRP_STAT */
20262306a36Sopenharmony_ci			  nla_total_size_64bit(sizeof(u64)));
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	return len;
20562306a36Sopenharmony_ci}
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic int stat_put(struct sk_buff *skb, u16 attrtype, u64 val)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	struct nlattr *nest;
21062306a36Sopenharmony_ci	int ret;
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci	if (val == ETHTOOL_STAT_NOT_SET)
21362306a36Sopenharmony_ci		return 0;
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci	/* We want to start stats attr types from 0, so we don't have a type
21662306a36Sopenharmony_ci	 * for pad inside ETHTOOL_A_STATS_GRP_STAT. Pad things on the outside
21762306a36Sopenharmony_ci	 * of ETHTOOL_A_STATS_GRP_STAT. Since we're one nest away from the
21862306a36Sopenharmony_ci	 * actual attr we're 4B off - nla_need_padding_for_64bit() & co.
21962306a36Sopenharmony_ci	 * can't be used.
22062306a36Sopenharmony_ci	 */
22162306a36Sopenharmony_ci#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
22262306a36Sopenharmony_ci	if (!IS_ALIGNED((unsigned long)skb_tail_pointer(skb), 8))
22362306a36Sopenharmony_ci		if (!nla_reserve(skb, ETHTOOL_A_STATS_GRP_PAD, 0))
22462306a36Sopenharmony_ci			return -EMSGSIZE;
22562306a36Sopenharmony_ci#endif
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP_STAT);
22862306a36Sopenharmony_ci	if (!nest)
22962306a36Sopenharmony_ci		return -EMSGSIZE;
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci	ret = nla_put_u64_64bit(skb, attrtype, val, -1 /* not used */);
23262306a36Sopenharmony_ci	if (ret) {
23362306a36Sopenharmony_ci		nla_nest_cancel(skb, nest);
23462306a36Sopenharmony_ci		return ret;
23562306a36Sopenharmony_ci	}
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	nla_nest_end(skb, nest);
23862306a36Sopenharmony_ci	return 0;
23962306a36Sopenharmony_ci}
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_cistatic int stats_put_phy_stats(struct sk_buff *skb,
24262306a36Sopenharmony_ci			       const struct stats_reply_data *data)
24362306a36Sopenharmony_ci{
24462306a36Sopenharmony_ci	if (stat_put(skb, ETHTOOL_A_STATS_ETH_PHY_5_SYM_ERR,
24562306a36Sopenharmony_ci		     data->phy_stats.SymbolErrorDuringCarrier))
24662306a36Sopenharmony_ci		return -EMSGSIZE;
24762306a36Sopenharmony_ci	return 0;
24862306a36Sopenharmony_ci}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cistatic int stats_put_mac_stats(struct sk_buff *skb,
25162306a36Sopenharmony_ci			       const struct stats_reply_data *data)
25262306a36Sopenharmony_ci{
25362306a36Sopenharmony_ci	if (stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_2_TX_PKT,
25462306a36Sopenharmony_ci		     data->mac_stats.FramesTransmittedOK) ||
25562306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_3_SINGLE_COL,
25662306a36Sopenharmony_ci		     data->mac_stats.SingleCollisionFrames) ||
25762306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_4_MULTI_COL,
25862306a36Sopenharmony_ci		     data->mac_stats.MultipleCollisionFrames) ||
25962306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_5_RX_PKT,
26062306a36Sopenharmony_ci		     data->mac_stats.FramesReceivedOK) ||
26162306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_6_FCS_ERR,
26262306a36Sopenharmony_ci		     data->mac_stats.FrameCheckSequenceErrors) ||
26362306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_7_ALIGN_ERR,
26462306a36Sopenharmony_ci		     data->mac_stats.AlignmentErrors) ||
26562306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_8_TX_BYTES,
26662306a36Sopenharmony_ci		     data->mac_stats.OctetsTransmittedOK) ||
26762306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_9_TX_DEFER,
26862306a36Sopenharmony_ci		     data->mac_stats.FramesWithDeferredXmissions) ||
26962306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_10_LATE_COL,
27062306a36Sopenharmony_ci		     data->mac_stats.LateCollisions) ||
27162306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_11_XS_COL,
27262306a36Sopenharmony_ci		     data->mac_stats.FramesAbortedDueToXSColls) ||
27362306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_12_TX_INT_ERR,
27462306a36Sopenharmony_ci		     data->mac_stats.FramesLostDueToIntMACXmitError) ||
27562306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_13_CS_ERR,
27662306a36Sopenharmony_ci		     data->mac_stats.CarrierSenseErrors) ||
27762306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_14_RX_BYTES,
27862306a36Sopenharmony_ci		     data->mac_stats.OctetsReceivedOK) ||
27962306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_15_RX_INT_ERR,
28062306a36Sopenharmony_ci		     data->mac_stats.FramesLostDueToIntMACRcvError) ||
28162306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_18_TX_MCAST,
28262306a36Sopenharmony_ci		     data->mac_stats.MulticastFramesXmittedOK) ||
28362306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_19_TX_BCAST,
28462306a36Sopenharmony_ci		     data->mac_stats.BroadcastFramesXmittedOK) ||
28562306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_20_XS_DEFER,
28662306a36Sopenharmony_ci		     data->mac_stats.FramesWithExcessiveDeferral) ||
28762306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_21_RX_MCAST,
28862306a36Sopenharmony_ci		     data->mac_stats.MulticastFramesReceivedOK) ||
28962306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_22_RX_BCAST,
29062306a36Sopenharmony_ci		     data->mac_stats.BroadcastFramesReceivedOK) ||
29162306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_23_IR_LEN_ERR,
29262306a36Sopenharmony_ci		     data->mac_stats.InRangeLengthErrors) ||
29362306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_24_OOR_LEN,
29462306a36Sopenharmony_ci		     data->mac_stats.OutOfRangeLengthField) ||
29562306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_MAC_25_TOO_LONG_ERR,
29662306a36Sopenharmony_ci		     data->mac_stats.FrameTooLongErrors))
29762306a36Sopenharmony_ci		return -EMSGSIZE;
29862306a36Sopenharmony_ci	return 0;
29962306a36Sopenharmony_ci}
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_cistatic int stats_put_ctrl_stats(struct sk_buff *skb,
30262306a36Sopenharmony_ci				const struct stats_reply_data *data)
30362306a36Sopenharmony_ci{
30462306a36Sopenharmony_ci	if (stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_3_TX,
30562306a36Sopenharmony_ci		     data->ctrl_stats.MACControlFramesTransmitted) ||
30662306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_4_RX,
30762306a36Sopenharmony_ci		     data->ctrl_stats.MACControlFramesReceived) ||
30862306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_ETH_CTRL_5_RX_UNSUP,
30962306a36Sopenharmony_ci		     data->ctrl_stats.UnsupportedOpcodesReceived))
31062306a36Sopenharmony_ci		return -EMSGSIZE;
31162306a36Sopenharmony_ci	return 0;
31262306a36Sopenharmony_ci}
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_cistatic int stats_put_rmon_hist(struct sk_buff *skb, u32 attr, const u64 *hist,
31562306a36Sopenharmony_ci			       const struct ethtool_rmon_hist_range *ranges)
31662306a36Sopenharmony_ci{
31762306a36Sopenharmony_ci	struct nlattr *nest;
31862306a36Sopenharmony_ci	int i;
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	if (!ranges)
32162306a36Sopenharmony_ci		return 0;
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	for (i = 0; i <	ETHTOOL_RMON_HIST_MAX; i++) {
32462306a36Sopenharmony_ci		if (!ranges[i].low && !ranges[i].high)
32562306a36Sopenharmony_ci			break;
32662306a36Sopenharmony_ci		if (hist[i] == ETHTOOL_STAT_NOT_SET)
32762306a36Sopenharmony_ci			continue;
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci		nest = nla_nest_start(skb, attr);
33062306a36Sopenharmony_ci		if (!nest)
33162306a36Sopenharmony_ci			return -EMSGSIZE;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci		if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_HIST_BKT_LOW,
33462306a36Sopenharmony_ci				ranges[i].low) ||
33562306a36Sopenharmony_ci		    nla_put_u32(skb, ETHTOOL_A_STATS_GRP_HIST_BKT_HI,
33662306a36Sopenharmony_ci				ranges[i].high) ||
33762306a36Sopenharmony_ci		    nla_put_u64_64bit(skb, ETHTOOL_A_STATS_GRP_HIST_VAL,
33862306a36Sopenharmony_ci				      hist[i], ETHTOOL_A_STATS_GRP_PAD))
33962306a36Sopenharmony_ci			goto err_cancel_hist;
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci		nla_nest_end(skb, nest);
34262306a36Sopenharmony_ci	}
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ci	return 0;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_cierr_cancel_hist:
34762306a36Sopenharmony_ci	nla_nest_cancel(skb, nest);
34862306a36Sopenharmony_ci	return -EMSGSIZE;
34962306a36Sopenharmony_ci}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_cistatic int stats_put_rmon_stats(struct sk_buff *skb,
35262306a36Sopenharmony_ci				const struct stats_reply_data *data)
35362306a36Sopenharmony_ci{
35462306a36Sopenharmony_ci	if (stats_put_rmon_hist(skb, ETHTOOL_A_STATS_GRP_HIST_RX,
35562306a36Sopenharmony_ci				data->rmon_stats.hist, data->rmon_ranges) ||
35662306a36Sopenharmony_ci	    stats_put_rmon_hist(skb, ETHTOOL_A_STATS_GRP_HIST_TX,
35762306a36Sopenharmony_ci				data->rmon_stats.hist_tx, data->rmon_ranges))
35862306a36Sopenharmony_ci		return -EMSGSIZE;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	if (stat_put(skb, ETHTOOL_A_STATS_RMON_UNDERSIZE,
36162306a36Sopenharmony_ci		     data->rmon_stats.undersize_pkts) ||
36262306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_RMON_OVERSIZE,
36362306a36Sopenharmony_ci		     data->rmon_stats.oversize_pkts) ||
36462306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_RMON_FRAG,
36562306a36Sopenharmony_ci		     data->rmon_stats.fragments) ||
36662306a36Sopenharmony_ci	    stat_put(skb, ETHTOOL_A_STATS_RMON_JABBER,
36762306a36Sopenharmony_ci		     data->rmon_stats.jabbers))
36862306a36Sopenharmony_ci		return -EMSGSIZE;
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci	return 0;
37162306a36Sopenharmony_ci}
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_cistatic int stats_put_stats(struct sk_buff *skb,
37462306a36Sopenharmony_ci			   const struct stats_reply_data *data,
37562306a36Sopenharmony_ci			   u32 id, u32 ss_id,
37662306a36Sopenharmony_ci			   int (*cb)(struct sk_buff *skb,
37762306a36Sopenharmony_ci				     const struct stats_reply_data *data))
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci	struct nlattr *nest;
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_ci	nest = nla_nest_start(skb, ETHTOOL_A_STATS_GRP);
38262306a36Sopenharmony_ci	if (!nest)
38362306a36Sopenharmony_ci		return -EMSGSIZE;
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci	if (nla_put_u32(skb, ETHTOOL_A_STATS_GRP_ID, id) ||
38662306a36Sopenharmony_ci	    nla_put_u32(skb, ETHTOOL_A_STATS_GRP_SS_ID, ss_id))
38762306a36Sopenharmony_ci		goto err_cancel;
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci	if (cb(skb, data))
39062306a36Sopenharmony_ci		goto err_cancel;
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_ci	nla_nest_end(skb, nest);
39362306a36Sopenharmony_ci	return 0;
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_cierr_cancel:
39662306a36Sopenharmony_ci	nla_nest_cancel(skb, nest);
39762306a36Sopenharmony_ci	return -EMSGSIZE;
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_cistatic int stats_fill_reply(struct sk_buff *skb,
40162306a36Sopenharmony_ci			    const struct ethnl_req_info *req_base,
40262306a36Sopenharmony_ci			    const struct ethnl_reply_data *reply_base)
40362306a36Sopenharmony_ci{
40462306a36Sopenharmony_ci	const struct stats_req_info *req_info = STATS_REQINFO(req_base);
40562306a36Sopenharmony_ci	const struct stats_reply_data *data = STATS_REPDATA(reply_base);
40662306a36Sopenharmony_ci	int ret = 0;
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ci	if (nla_put_u32(skb, ETHTOOL_A_STATS_SRC, req_info->src))
40962306a36Sopenharmony_ci		return -EMSGSIZE;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	if (!ret && test_bit(ETHTOOL_STATS_ETH_PHY, req_info->stat_mask))
41262306a36Sopenharmony_ci		ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_PHY,
41362306a36Sopenharmony_ci				      ETH_SS_STATS_ETH_PHY,
41462306a36Sopenharmony_ci				      stats_put_phy_stats);
41562306a36Sopenharmony_ci	if (!ret && test_bit(ETHTOOL_STATS_ETH_MAC, req_info->stat_mask))
41662306a36Sopenharmony_ci		ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_MAC,
41762306a36Sopenharmony_ci				      ETH_SS_STATS_ETH_MAC,
41862306a36Sopenharmony_ci				      stats_put_mac_stats);
41962306a36Sopenharmony_ci	if (!ret && test_bit(ETHTOOL_STATS_ETH_CTRL, req_info->stat_mask))
42062306a36Sopenharmony_ci		ret = stats_put_stats(skb, data, ETHTOOL_STATS_ETH_CTRL,
42162306a36Sopenharmony_ci				      ETH_SS_STATS_ETH_CTRL,
42262306a36Sopenharmony_ci				      stats_put_ctrl_stats);
42362306a36Sopenharmony_ci	if (!ret && test_bit(ETHTOOL_STATS_RMON, req_info->stat_mask))
42462306a36Sopenharmony_ci		ret = stats_put_stats(skb, data, ETHTOOL_STATS_RMON,
42562306a36Sopenharmony_ci				      ETH_SS_STATS_RMON, stats_put_rmon_stats);
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ci	return ret;
42862306a36Sopenharmony_ci}
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ciconst struct ethnl_request_ops ethnl_stats_request_ops = {
43162306a36Sopenharmony_ci	.request_cmd		= ETHTOOL_MSG_STATS_GET,
43262306a36Sopenharmony_ci	.reply_cmd		= ETHTOOL_MSG_STATS_GET_REPLY,
43362306a36Sopenharmony_ci	.hdr_attr		= ETHTOOL_A_STATS_HEADER,
43462306a36Sopenharmony_ci	.req_info_size		= sizeof(struct stats_req_info),
43562306a36Sopenharmony_ci	.reply_data_size	= sizeof(struct stats_reply_data),
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci	.parse_request		= stats_parse_request,
43862306a36Sopenharmony_ci	.prepare_data		= stats_prepare_data,
43962306a36Sopenharmony_ci	.reply_size		= stats_reply_size,
44062306a36Sopenharmony_ci	.fill_reply		= stats_fill_reply,
44162306a36Sopenharmony_ci};
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_cistatic u64 ethtool_stats_sum(u64 a, u64 b)
44462306a36Sopenharmony_ci{
44562306a36Sopenharmony_ci	if (a == ETHTOOL_STAT_NOT_SET)
44662306a36Sopenharmony_ci		return b;
44762306a36Sopenharmony_ci	if (b == ETHTOOL_STAT_NOT_SET)
44862306a36Sopenharmony_ci		return a;
44962306a36Sopenharmony_ci	return a + b;
45062306a36Sopenharmony_ci}
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ci/* Avoid modifying the aggregation procedure every time a new counter is added
45362306a36Sopenharmony_ci * by treating the structures as an array of u64 statistics.
45462306a36Sopenharmony_ci */
45562306a36Sopenharmony_cistatic void ethtool_aggregate_stats(void *aggr_stats, const void *emac_stats,
45662306a36Sopenharmony_ci				    const void *pmac_stats, size_t stats_size,
45762306a36Sopenharmony_ci				    size_t stats_offset)
45862306a36Sopenharmony_ci{
45962306a36Sopenharmony_ci	size_t num_stats = stats_size / sizeof(u64);
46062306a36Sopenharmony_ci	const u64 *s1 = emac_stats + stats_offset;
46162306a36Sopenharmony_ci	const u64 *s2 = pmac_stats + stats_offset;
46262306a36Sopenharmony_ci	u64 *s = aggr_stats + stats_offset;
46362306a36Sopenharmony_ci	int i;
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci	for (i = 0; i < num_stats; i++)
46662306a36Sopenharmony_ci		s[i] = ethtool_stats_sum(s1[i], s2[i]);
46762306a36Sopenharmony_ci}
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_civoid ethtool_aggregate_mac_stats(struct net_device *dev,
47062306a36Sopenharmony_ci				 struct ethtool_eth_mac_stats *mac_stats)
47162306a36Sopenharmony_ci{
47262306a36Sopenharmony_ci	const struct ethtool_ops *ops = dev->ethtool_ops;
47362306a36Sopenharmony_ci	struct ethtool_eth_mac_stats pmac, emac;
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci	memset(&emac, 0xff, sizeof(emac));
47662306a36Sopenharmony_ci	memset(&pmac, 0xff, sizeof(pmac));
47762306a36Sopenharmony_ci	emac.src = ETHTOOL_MAC_STATS_SRC_EMAC;
47862306a36Sopenharmony_ci	pmac.src = ETHTOOL_MAC_STATS_SRC_PMAC;
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci	ops->get_eth_mac_stats(dev, &emac);
48162306a36Sopenharmony_ci	ops->get_eth_mac_stats(dev, &pmac);
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci	ethtool_aggregate_stats(mac_stats, &emac, &pmac,
48462306a36Sopenharmony_ci				sizeof(mac_stats->stats),
48562306a36Sopenharmony_ci				offsetof(struct ethtool_eth_mac_stats, stats));
48662306a36Sopenharmony_ci}
48762306a36Sopenharmony_ciEXPORT_SYMBOL(ethtool_aggregate_mac_stats);
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_civoid ethtool_aggregate_phy_stats(struct net_device *dev,
49062306a36Sopenharmony_ci				 struct ethtool_eth_phy_stats *phy_stats)
49162306a36Sopenharmony_ci{
49262306a36Sopenharmony_ci	const struct ethtool_ops *ops = dev->ethtool_ops;
49362306a36Sopenharmony_ci	struct ethtool_eth_phy_stats pmac, emac;
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	memset(&emac, 0xff, sizeof(emac));
49662306a36Sopenharmony_ci	memset(&pmac, 0xff, sizeof(pmac));
49762306a36Sopenharmony_ci	emac.src = ETHTOOL_MAC_STATS_SRC_EMAC;
49862306a36Sopenharmony_ci	pmac.src = ETHTOOL_MAC_STATS_SRC_PMAC;
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_ci	ops->get_eth_phy_stats(dev, &emac);
50162306a36Sopenharmony_ci	ops->get_eth_phy_stats(dev, &pmac);
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci	ethtool_aggregate_stats(phy_stats, &emac, &pmac,
50462306a36Sopenharmony_ci				sizeof(phy_stats->stats),
50562306a36Sopenharmony_ci				offsetof(struct ethtool_eth_phy_stats, stats));
50662306a36Sopenharmony_ci}
50762306a36Sopenharmony_ciEXPORT_SYMBOL(ethtool_aggregate_phy_stats);
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_civoid ethtool_aggregate_ctrl_stats(struct net_device *dev,
51062306a36Sopenharmony_ci				  struct ethtool_eth_ctrl_stats *ctrl_stats)
51162306a36Sopenharmony_ci{
51262306a36Sopenharmony_ci	const struct ethtool_ops *ops = dev->ethtool_ops;
51362306a36Sopenharmony_ci	struct ethtool_eth_ctrl_stats pmac, emac;
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci	memset(&emac, 0xff, sizeof(emac));
51662306a36Sopenharmony_ci	memset(&pmac, 0xff, sizeof(pmac));
51762306a36Sopenharmony_ci	emac.src = ETHTOOL_MAC_STATS_SRC_EMAC;
51862306a36Sopenharmony_ci	pmac.src = ETHTOOL_MAC_STATS_SRC_PMAC;
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci	ops->get_eth_ctrl_stats(dev, &emac);
52162306a36Sopenharmony_ci	ops->get_eth_ctrl_stats(dev, &pmac);
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci	ethtool_aggregate_stats(ctrl_stats, &emac, &pmac,
52462306a36Sopenharmony_ci				sizeof(ctrl_stats->stats),
52562306a36Sopenharmony_ci				offsetof(struct ethtool_eth_ctrl_stats, stats));
52662306a36Sopenharmony_ci}
52762306a36Sopenharmony_ciEXPORT_SYMBOL(ethtool_aggregate_ctrl_stats);
52862306a36Sopenharmony_ci
52962306a36Sopenharmony_civoid ethtool_aggregate_pause_stats(struct net_device *dev,
53062306a36Sopenharmony_ci				   struct ethtool_pause_stats *pause_stats)
53162306a36Sopenharmony_ci{
53262306a36Sopenharmony_ci	const struct ethtool_ops *ops = dev->ethtool_ops;
53362306a36Sopenharmony_ci	struct ethtool_pause_stats pmac, emac;
53462306a36Sopenharmony_ci
53562306a36Sopenharmony_ci	memset(&emac, 0xff, sizeof(emac));
53662306a36Sopenharmony_ci	memset(&pmac, 0xff, sizeof(pmac));
53762306a36Sopenharmony_ci	emac.src = ETHTOOL_MAC_STATS_SRC_EMAC;
53862306a36Sopenharmony_ci	pmac.src = ETHTOOL_MAC_STATS_SRC_PMAC;
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci	ops->get_pause_stats(dev, &emac);
54162306a36Sopenharmony_ci	ops->get_pause_stats(dev, &pmac);
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ci	ethtool_aggregate_stats(pause_stats, &emac, &pmac,
54462306a36Sopenharmony_ci				sizeof(pause_stats->stats),
54562306a36Sopenharmony_ci				offsetof(struct ethtool_pause_stats, stats));
54662306a36Sopenharmony_ci}
54762306a36Sopenharmony_ciEXPORT_SYMBOL(ethtool_aggregate_pause_stats);
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_civoid ethtool_aggregate_rmon_stats(struct net_device *dev,
55062306a36Sopenharmony_ci				  struct ethtool_rmon_stats *rmon_stats)
55162306a36Sopenharmony_ci{
55262306a36Sopenharmony_ci	const struct ethtool_ops *ops = dev->ethtool_ops;
55362306a36Sopenharmony_ci	const struct ethtool_rmon_hist_range *dummy;
55462306a36Sopenharmony_ci	struct ethtool_rmon_stats pmac, emac;
55562306a36Sopenharmony_ci
55662306a36Sopenharmony_ci	memset(&emac, 0xff, sizeof(emac));
55762306a36Sopenharmony_ci	memset(&pmac, 0xff, sizeof(pmac));
55862306a36Sopenharmony_ci	emac.src = ETHTOOL_MAC_STATS_SRC_EMAC;
55962306a36Sopenharmony_ci	pmac.src = ETHTOOL_MAC_STATS_SRC_PMAC;
56062306a36Sopenharmony_ci
56162306a36Sopenharmony_ci	ops->get_rmon_stats(dev, &emac, &dummy);
56262306a36Sopenharmony_ci	ops->get_rmon_stats(dev, &pmac, &dummy);
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci	ethtool_aggregate_stats(rmon_stats, &emac, &pmac,
56562306a36Sopenharmony_ci				sizeof(rmon_stats->stats),
56662306a36Sopenharmony_ci				offsetof(struct ethtool_rmon_stats, stats));
56762306a36Sopenharmony_ci}
56862306a36Sopenharmony_ciEXPORT_SYMBOL(ethtool_aggregate_rmon_stats);
569