162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#include "netlink.h"
462306a36Sopenharmony_ci#include "common.h"
562306a36Sopenharmony_ci
662306a36Sopenharmony_cistruct coalesce_req_info {
762306a36Sopenharmony_ci	struct ethnl_req_info		base;
862306a36Sopenharmony_ci};
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cistruct coalesce_reply_data {
1162306a36Sopenharmony_ci	struct ethnl_reply_data		base;
1262306a36Sopenharmony_ci	struct ethtool_coalesce		coalesce;
1362306a36Sopenharmony_ci	struct kernel_ethtool_coalesce	kernel_coalesce;
1462306a36Sopenharmony_ci	u32				supported_params;
1562306a36Sopenharmony_ci};
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define COALESCE_REPDATA(__reply_base) \
1862306a36Sopenharmony_ci	container_of(__reply_base, struct coalesce_reply_data, base)
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#define __SUPPORTED_OFFSET ETHTOOL_A_COALESCE_RX_USECS
2162306a36Sopenharmony_cistatic u32 attr_to_mask(unsigned int attr_type)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci	return BIT(attr_type - __SUPPORTED_OFFSET);
2462306a36Sopenharmony_ci}
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/* build time check that indices in ethtool_ops::supported_coalesce_params
2762306a36Sopenharmony_ci * match corresponding attribute types with an offset
2862306a36Sopenharmony_ci */
2962306a36Sopenharmony_ci#define __CHECK_SUPPORTED_OFFSET(x) \
3062306a36Sopenharmony_ci	static_assert((ETHTOOL_ ## x) == \
3162306a36Sopenharmony_ci		      BIT((ETHTOOL_A_ ## x) - __SUPPORTED_OFFSET))
3262306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS);
3362306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES);
3462306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_IRQ);
3562306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_IRQ);
3662306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS);
3762306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES);
3862306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_IRQ);
3962306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_IRQ);
4062306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_STATS_BLOCK_USECS);
4162306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_USE_ADAPTIVE_RX);
4262306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_USE_ADAPTIVE_TX);
4362306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_PKT_RATE_LOW);
4462306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_LOW);
4562306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_LOW);
4662306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_LOW);
4762306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_LOW);
4862306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_PKT_RATE_HIGH);
4962306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_USECS_HIGH);
5062306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RX_MAX_FRAMES_HIGH);
5162306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_USECS_HIGH);
5262306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_TX_MAX_FRAMES_HIGH);
5362306a36Sopenharmony_ci__CHECK_SUPPORTED_OFFSET(COALESCE_RATE_SAMPLE_INTERVAL);
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciconst struct nla_policy ethnl_coalesce_get_policy[] = {
5662306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_HEADER]		=
5762306a36Sopenharmony_ci		NLA_POLICY_NESTED(ethnl_header_policy),
5862306a36Sopenharmony_ci};
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic int coalesce_prepare_data(const struct ethnl_req_info *req_base,
6162306a36Sopenharmony_ci				 struct ethnl_reply_data *reply_base,
6262306a36Sopenharmony_ci				 const struct genl_info *info)
6362306a36Sopenharmony_ci{
6462306a36Sopenharmony_ci	struct coalesce_reply_data *data = COALESCE_REPDATA(reply_base);
6562306a36Sopenharmony_ci	struct net_device *dev = reply_base->dev;
6662306a36Sopenharmony_ci	int ret;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	if (!dev->ethtool_ops->get_coalesce)
6962306a36Sopenharmony_ci		return -EOPNOTSUPP;
7062306a36Sopenharmony_ci	data->supported_params = dev->ethtool_ops->supported_coalesce_params;
7162306a36Sopenharmony_ci	ret = ethnl_ops_begin(dev);
7262306a36Sopenharmony_ci	if (ret < 0)
7362306a36Sopenharmony_ci		return ret;
7462306a36Sopenharmony_ci	ret = dev->ethtool_ops->get_coalesce(dev, &data->coalesce,
7562306a36Sopenharmony_ci					     &data->kernel_coalesce,
7662306a36Sopenharmony_ci					     info->extack);
7762306a36Sopenharmony_ci	ethnl_ops_complete(dev);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	return ret;
8062306a36Sopenharmony_ci}
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic int coalesce_reply_size(const struct ethnl_req_info *req_base,
8362306a36Sopenharmony_ci			       const struct ethnl_reply_data *reply_base)
8462306a36Sopenharmony_ci{
8562306a36Sopenharmony_ci	return nla_total_size(sizeof(u32)) +	/* _RX_USECS */
8662306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RX_MAX_FRAMES */
8762306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RX_USECS_IRQ */
8862306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RX_MAX_FRAMES_IRQ */
8962306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_USECS */
9062306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_MAX_FRAMES */
9162306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_USECS_IRQ */
9262306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_MAX_FRAMES_IRQ */
9362306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _STATS_BLOCK_USECS */
9462306a36Sopenharmony_ci	       nla_total_size(sizeof(u8)) +	/* _USE_ADAPTIVE_RX */
9562306a36Sopenharmony_ci	       nla_total_size(sizeof(u8)) +	/* _USE_ADAPTIVE_TX */
9662306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _PKT_RATE_LOW */
9762306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RX_USECS_LOW */
9862306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RX_MAX_FRAMES_LOW */
9962306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_USECS_LOW */
10062306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_MAX_FRAMES_LOW */
10162306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _PKT_RATE_HIGH */
10262306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RX_USECS_HIGH */
10362306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RX_MAX_FRAMES_HIGH */
10462306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_USECS_HIGH */
10562306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_MAX_FRAMES_HIGH */
10662306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _RATE_SAMPLE_INTERVAL */
10762306a36Sopenharmony_ci	       nla_total_size(sizeof(u8)) +	/* _USE_CQE_MODE_TX */
10862306a36Sopenharmony_ci	       nla_total_size(sizeof(u8)) +	/* _USE_CQE_MODE_RX */
10962306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_AGGR_MAX_BYTES */
11062306a36Sopenharmony_ci	       nla_total_size(sizeof(u32)) +	/* _TX_AGGR_MAX_FRAMES */
11162306a36Sopenharmony_ci	       nla_total_size(sizeof(u32));	/* _TX_AGGR_TIME_USECS */
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_cistatic bool coalesce_put_u32(struct sk_buff *skb, u16 attr_type, u32 val,
11562306a36Sopenharmony_ci			     u32 supported_params)
11662306a36Sopenharmony_ci{
11762306a36Sopenharmony_ci	if (!val && !(supported_params & attr_to_mask(attr_type)))
11862306a36Sopenharmony_ci		return false;
11962306a36Sopenharmony_ci	return nla_put_u32(skb, attr_type, val);
12062306a36Sopenharmony_ci}
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_cistatic bool coalesce_put_bool(struct sk_buff *skb, u16 attr_type, u32 val,
12362306a36Sopenharmony_ci			      u32 supported_params)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	if (!val && !(supported_params & attr_to_mask(attr_type)))
12662306a36Sopenharmony_ci		return false;
12762306a36Sopenharmony_ci	return nla_put_u8(skb, attr_type, !!val);
12862306a36Sopenharmony_ci}
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_cistatic int coalesce_fill_reply(struct sk_buff *skb,
13162306a36Sopenharmony_ci			       const struct ethnl_req_info *req_base,
13262306a36Sopenharmony_ci			       const struct ethnl_reply_data *reply_base)
13362306a36Sopenharmony_ci{
13462306a36Sopenharmony_ci	const struct coalesce_reply_data *data = COALESCE_REPDATA(reply_base);
13562306a36Sopenharmony_ci	const struct kernel_ethtool_coalesce *kcoal = &data->kernel_coalesce;
13662306a36Sopenharmony_ci	const struct ethtool_coalesce *coal = &data->coalesce;
13762306a36Sopenharmony_ci	u32 supported = data->supported_params;
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci	if (coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS,
14062306a36Sopenharmony_ci			     coal->rx_coalesce_usecs, supported) ||
14162306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES,
14262306a36Sopenharmony_ci			     coal->rx_max_coalesced_frames, supported) ||
14362306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_IRQ,
14462306a36Sopenharmony_ci			     coal->rx_coalesce_usecs_irq, supported) ||
14562306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ,
14662306a36Sopenharmony_ci			     coal->rx_max_coalesced_frames_irq, supported) ||
14762306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS,
14862306a36Sopenharmony_ci			     coal->tx_coalesce_usecs, supported) ||
14962306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES,
15062306a36Sopenharmony_ci			     coal->tx_max_coalesced_frames, supported) ||
15162306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_IRQ,
15262306a36Sopenharmony_ci			     coal->tx_coalesce_usecs_irq, supported) ||
15362306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ,
15462306a36Sopenharmony_ci			     coal->tx_max_coalesced_frames_irq, supported) ||
15562306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_STATS_BLOCK_USECS,
15662306a36Sopenharmony_ci			     coal->stats_block_coalesce_usecs, supported) ||
15762306a36Sopenharmony_ci	    coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX,
15862306a36Sopenharmony_ci			      coal->use_adaptive_rx_coalesce, supported) ||
15962306a36Sopenharmony_ci	    coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX,
16062306a36Sopenharmony_ci			      coal->use_adaptive_tx_coalesce, supported) ||
16162306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_PKT_RATE_LOW,
16262306a36Sopenharmony_ci			     coal->pkt_rate_low, supported) ||
16362306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_LOW,
16462306a36Sopenharmony_ci			     coal->rx_coalesce_usecs_low, supported) ||
16562306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW,
16662306a36Sopenharmony_ci			     coal->rx_max_coalesced_frames_low, supported) ||
16762306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_LOW,
16862306a36Sopenharmony_ci			     coal->tx_coalesce_usecs_low, supported) ||
16962306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW,
17062306a36Sopenharmony_ci			     coal->tx_max_coalesced_frames_low, supported) ||
17162306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_PKT_RATE_HIGH,
17262306a36Sopenharmony_ci			     coal->pkt_rate_high, supported) ||
17362306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_USECS_HIGH,
17462306a36Sopenharmony_ci			     coal->rx_coalesce_usecs_high, supported) ||
17562306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH,
17662306a36Sopenharmony_ci			     coal->rx_max_coalesced_frames_high, supported) ||
17762306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_USECS_HIGH,
17862306a36Sopenharmony_ci			     coal->tx_coalesce_usecs_high, supported) ||
17962306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH,
18062306a36Sopenharmony_ci			     coal->tx_max_coalesced_frames_high, supported) ||
18162306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL,
18262306a36Sopenharmony_ci			     coal->rate_sample_interval, supported) ||
18362306a36Sopenharmony_ci	    coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_CQE_MODE_TX,
18462306a36Sopenharmony_ci			      kcoal->use_cqe_mode_tx, supported) ||
18562306a36Sopenharmony_ci	    coalesce_put_bool(skb, ETHTOOL_A_COALESCE_USE_CQE_MODE_RX,
18662306a36Sopenharmony_ci			      kcoal->use_cqe_mode_rx, supported) ||
18762306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES,
18862306a36Sopenharmony_ci			     kcoal->tx_aggr_max_bytes, supported) ||
18962306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES,
19062306a36Sopenharmony_ci			     kcoal->tx_aggr_max_frames, supported) ||
19162306a36Sopenharmony_ci	    coalesce_put_u32(skb, ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS,
19262306a36Sopenharmony_ci			     kcoal->tx_aggr_time_usecs, supported))
19362306a36Sopenharmony_ci		return -EMSGSIZE;
19462306a36Sopenharmony_ci
19562306a36Sopenharmony_ci	return 0;
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci/* COALESCE_SET */
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ciconst struct nla_policy ethnl_coalesce_set_policy[] = {
20162306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_HEADER]		=
20262306a36Sopenharmony_ci		NLA_POLICY_NESTED(ethnl_header_policy),
20362306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_USECS]		= { .type = NLA_U32 },
20462306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_MAX_FRAMES]	= { .type = NLA_U32 },
20562306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_USECS_IRQ]	= { .type = NLA_U32 },
20662306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ]	= { .type = NLA_U32 },
20762306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_USECS]		= { .type = NLA_U32 },
20862306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_MAX_FRAMES]	= { .type = NLA_U32 },
20962306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_USECS_IRQ]	= { .type = NLA_U32 },
21062306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ]	= { .type = NLA_U32 },
21162306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS]	= { .type = NLA_U32 },
21262306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX]	= { .type = NLA_U8 },
21362306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX]	= { .type = NLA_U8 },
21462306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_PKT_RATE_LOW]	= { .type = NLA_U32 },
21562306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_USECS_LOW]	= { .type = NLA_U32 },
21662306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW]	= { .type = NLA_U32 },
21762306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_USECS_LOW]	= { .type = NLA_U32 },
21862306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW]	= { .type = NLA_U32 },
21962306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_PKT_RATE_HIGH]	= { .type = NLA_U32 },
22062306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_USECS_HIGH]	= { .type = NLA_U32 },
22162306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH]	= { .type = NLA_U32 },
22262306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_USECS_HIGH]	= { .type = NLA_U32 },
22362306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH]	= { .type = NLA_U32 },
22462306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL] = { .type = NLA_U32 },
22562306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_USE_CQE_MODE_TX]	= NLA_POLICY_MAX(NLA_U8, 1),
22662306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_USE_CQE_MODE_RX]	= NLA_POLICY_MAX(NLA_U8, 1),
22762306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES] = { .type = NLA_U32 },
22862306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES] = { .type = NLA_U32 },
22962306a36Sopenharmony_ci	[ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS] = { .type = NLA_U32 },
23062306a36Sopenharmony_ci};
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic int
23362306a36Sopenharmony_ciethnl_set_coalesce_validate(struct ethnl_req_info *req_info,
23462306a36Sopenharmony_ci			    struct genl_info *info)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	const struct ethtool_ops *ops = req_info->dev->ethtool_ops;
23762306a36Sopenharmony_ci	struct nlattr **tb = info->attrs;
23862306a36Sopenharmony_ci	u32 supported_params;
23962306a36Sopenharmony_ci	u16 a;
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci	if (!ops->get_coalesce || !ops->set_coalesce)
24262306a36Sopenharmony_ci		return -EOPNOTSUPP;
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	/* make sure that only supported parameters are present */
24562306a36Sopenharmony_ci	supported_params = ops->supported_coalesce_params;
24662306a36Sopenharmony_ci	for (a = ETHTOOL_A_COALESCE_RX_USECS; a < __ETHTOOL_A_COALESCE_CNT; a++)
24762306a36Sopenharmony_ci		if (tb[a] && !(supported_params & attr_to_mask(a))) {
24862306a36Sopenharmony_ci			NL_SET_ERR_MSG_ATTR(info->extack, tb[a],
24962306a36Sopenharmony_ci					    "cannot modify an unsupported parameter");
25062306a36Sopenharmony_ci			return -EINVAL;
25162306a36Sopenharmony_ci		}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	return 1;
25462306a36Sopenharmony_ci}
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_cistatic int
25762306a36Sopenharmony_ci__ethnl_set_coalesce(struct ethnl_req_info *req_info, struct genl_info *info,
25862306a36Sopenharmony_ci		     bool *dual_change)
25962306a36Sopenharmony_ci{
26062306a36Sopenharmony_ci	struct kernel_ethtool_coalesce kernel_coalesce = {};
26162306a36Sopenharmony_ci	struct net_device *dev = req_info->dev;
26262306a36Sopenharmony_ci	struct ethtool_coalesce coalesce = {};
26362306a36Sopenharmony_ci	bool mod_mode = false, mod = false;
26462306a36Sopenharmony_ci	struct nlattr **tb = info->attrs;
26562306a36Sopenharmony_ci	int ret;
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci	ret = dev->ethtool_ops->get_coalesce(dev, &coalesce, &kernel_coalesce,
26862306a36Sopenharmony_ci					     info->extack);
26962306a36Sopenharmony_ci	if (ret < 0)
27062306a36Sopenharmony_ci		return ret;
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci	/* Update values */
27362306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_coalesce_usecs,
27462306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_USECS], &mod);
27562306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_max_coalesced_frames,
27662306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES], &mod);
27762306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_coalesce_usecs_irq,
27862306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_USECS_IRQ], &mod);
27962306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_max_coalesced_frames_irq,
28062306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_IRQ], &mod);
28162306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_coalesce_usecs,
28262306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_USECS], &mod);
28362306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_max_coalesced_frames,
28462306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES], &mod);
28562306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_coalesce_usecs_irq,
28662306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_USECS_IRQ], &mod);
28762306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_max_coalesced_frames_irq,
28862306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_IRQ], &mod);
28962306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.stats_block_coalesce_usecs,
29062306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_STATS_BLOCK_USECS], &mod);
29162306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.pkt_rate_low,
29262306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_PKT_RATE_LOW], &mod);
29362306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_coalesce_usecs_low,
29462306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_USECS_LOW], &mod);
29562306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_max_coalesced_frames_low,
29662306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_LOW], &mod);
29762306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_coalesce_usecs_low,
29862306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_USECS_LOW], &mod);
29962306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_max_coalesced_frames_low,
30062306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_LOW], &mod);
30162306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.pkt_rate_high,
30262306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_PKT_RATE_HIGH], &mod);
30362306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_coalesce_usecs_high,
30462306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_USECS_HIGH], &mod);
30562306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rx_max_coalesced_frames_high,
30662306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RX_MAX_FRAMES_HIGH], &mod);
30762306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_coalesce_usecs_high,
30862306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_USECS_HIGH], &mod);
30962306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.tx_max_coalesced_frames_high,
31062306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_MAX_FRAMES_HIGH], &mod);
31162306a36Sopenharmony_ci	ethnl_update_u32(&coalesce.rate_sample_interval,
31262306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_RATE_SAMPLE_INTERVAL], &mod);
31362306a36Sopenharmony_ci	ethnl_update_u32(&kernel_coalesce.tx_aggr_max_bytes,
31462306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_AGGR_MAX_BYTES], &mod);
31562306a36Sopenharmony_ci	ethnl_update_u32(&kernel_coalesce.tx_aggr_max_frames,
31662306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_AGGR_MAX_FRAMES], &mod);
31762306a36Sopenharmony_ci	ethnl_update_u32(&kernel_coalesce.tx_aggr_time_usecs,
31862306a36Sopenharmony_ci			 tb[ETHTOOL_A_COALESCE_TX_AGGR_TIME_USECS], &mod);
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_ci	/* Update operation modes */
32162306a36Sopenharmony_ci	ethnl_update_bool32(&coalesce.use_adaptive_rx_coalesce,
32262306a36Sopenharmony_ci			    tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_RX], &mod_mode);
32362306a36Sopenharmony_ci	ethnl_update_bool32(&coalesce.use_adaptive_tx_coalesce,
32462306a36Sopenharmony_ci			    tb[ETHTOOL_A_COALESCE_USE_ADAPTIVE_TX], &mod_mode);
32562306a36Sopenharmony_ci	ethnl_update_u8(&kernel_coalesce.use_cqe_mode_tx,
32662306a36Sopenharmony_ci			tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_TX], &mod_mode);
32762306a36Sopenharmony_ci	ethnl_update_u8(&kernel_coalesce.use_cqe_mode_rx,
32862306a36Sopenharmony_ci			tb[ETHTOOL_A_COALESCE_USE_CQE_MODE_RX], &mod_mode);
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ci	*dual_change = mod && mod_mode;
33162306a36Sopenharmony_ci	if (!mod && !mod_mode)
33262306a36Sopenharmony_ci		return 0;
33362306a36Sopenharmony_ci
33462306a36Sopenharmony_ci	ret = dev->ethtool_ops->set_coalesce(dev, &coalesce, &kernel_coalesce,
33562306a36Sopenharmony_ci					     info->extack);
33662306a36Sopenharmony_ci	return ret < 0 ? ret : 1;
33762306a36Sopenharmony_ci}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_cistatic int
34062306a36Sopenharmony_ciethnl_set_coalesce(struct ethnl_req_info *req_info, struct genl_info *info)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	bool dual_change;
34362306a36Sopenharmony_ci	int err, ret;
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	/* SET_COALESCE may change operation mode and parameters in one call.
34662306a36Sopenharmony_ci	 * Changing operation mode may cause the driver to reset the parameter
34762306a36Sopenharmony_ci	 * values, and therefore ignore user input (driver does not know which
34862306a36Sopenharmony_ci	 * parameters come from user and which are echoed back from ->get).
34962306a36Sopenharmony_ci	 * To not complicate the drivers if user tries to change both the mode
35062306a36Sopenharmony_ci	 * and parameters at once - call the driver twice.
35162306a36Sopenharmony_ci	 */
35262306a36Sopenharmony_ci	err = __ethnl_set_coalesce(req_info, info, &dual_change);
35362306a36Sopenharmony_ci	if (err < 0)
35462306a36Sopenharmony_ci		return err;
35562306a36Sopenharmony_ci	ret = err;
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci	if (ret && dual_change) {
35862306a36Sopenharmony_ci		err = __ethnl_set_coalesce(req_info, info, &dual_change);
35962306a36Sopenharmony_ci		if (err < 0)
36062306a36Sopenharmony_ci			return err;
36162306a36Sopenharmony_ci	}
36262306a36Sopenharmony_ci	return ret;
36362306a36Sopenharmony_ci}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ciconst struct ethnl_request_ops ethnl_coalesce_request_ops = {
36662306a36Sopenharmony_ci	.request_cmd		= ETHTOOL_MSG_COALESCE_GET,
36762306a36Sopenharmony_ci	.reply_cmd		= ETHTOOL_MSG_COALESCE_GET_REPLY,
36862306a36Sopenharmony_ci	.hdr_attr		= ETHTOOL_A_COALESCE_HEADER,
36962306a36Sopenharmony_ci	.req_info_size		= sizeof(struct coalesce_req_info),
37062306a36Sopenharmony_ci	.reply_data_size	= sizeof(struct coalesce_reply_data),
37162306a36Sopenharmony_ci
37262306a36Sopenharmony_ci	.prepare_data		= coalesce_prepare_data,
37362306a36Sopenharmony_ci	.reply_size		= coalesce_reply_size,
37462306a36Sopenharmony_ci	.fill_reply		= coalesce_fill_reply,
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	.set_validate		= ethnl_set_coalesce_validate,
37762306a36Sopenharmony_ci	.set			= ethnl_set_coalesce,
37862306a36Sopenharmony_ci	.set_ntf_cmd		= ETHTOOL_MSG_COALESCE_NTF,
37962306a36Sopenharmony_ci};
380