summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/alibaba/eea/eea_ethtool.c
blob: 479779aa2dd88ccf0cb416ec45a098947ddded90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Driver for Alibaba Elastic Ethernet Adapter.
 *
 * Copyright (C) 2025 Alibaba Inc.
 */

#include <linux/ethtool.h>
#include <linux/ethtool_netlink.h>
#include <linux/rtnetlink.h>

#include "eea_adminq.h"
#include "eea_net.h"
#include "eea_pci.h"

struct eea_stat_desc {
	char desc[ETH_GSTRING_LEN];
	size_t offset;
};

#define EEA_TX_STAT(m)	{#m, offsetof(struct eea_tx_stats, m)}
#define EEA_RX_STAT(m)	{#m, offsetof(struct eea_rx_stats, m)}

static const struct eea_stat_desc eea_rx_stats_desc[] = {
	EEA_RX_STAT(descs),
	EEA_RX_STAT(kicks),
};

static const struct eea_stat_desc eea_tx_stats_desc[] = {
	EEA_TX_STAT(descs),
	EEA_TX_STAT(kicks),
};

#define EEA_TX_STATS_LEN	ARRAY_SIZE(eea_tx_stats_desc)
#define EEA_RX_STATS_LEN	ARRAY_SIZE(eea_rx_stats_desc)

static void eea_get_drvinfo(struct net_device *netdev,
			    struct ethtool_drvinfo *info)
{
	struct eea_net *enet = netdev_priv(netdev);
	struct eea_device *edev = enet->edev;

	strscpy(info->driver,   KBUILD_MODNAME,     sizeof(info->driver));
	strscpy(info->bus_info, eea_pci_name(edev), sizeof(info->bus_info));
}

static void eea_get_ringparam(struct net_device *netdev,
			      struct ethtool_ringparam *ring,
			      struct kernel_ethtool_ringparam *kernel_ring,
			      struct netlink_ext_ack *extack)
{
	struct eea_net *enet = netdev_priv(netdev);

	ring->rx_max_pending = enet->cfg_hw.rx_ring_depth;
	ring->tx_max_pending = enet->cfg_hw.tx_ring_depth;
	ring->rx_pending = enet->cfg.rx_ring_depth;
	ring->tx_pending = enet->cfg.tx_ring_depth;

	kernel_ring->tcp_data_split = enet->cfg.split_hdr ?
				      ETHTOOL_TCP_DATA_SPLIT_ENABLED :
				      ETHTOOL_TCP_DATA_SPLIT_DISABLED;
}

static int eea_set_ringparam(struct net_device *netdev,
			     struct ethtool_ringparam *ring,
			     struct kernel_ethtool_ringparam *kernel_ring,
			     struct netlink_ext_ack *extack)
{
	struct eea_net *enet = netdev_priv(netdev);
	struct eea_net_init_ctx ctx;
	bool need_update = false;
	struct eea_net_cfg *cfg;
	bool sh;

	if (ring->rx_pending < EEA_NET_IO_RING_DEPTH_MIN ||
	    ring->tx_pending < EEA_NET_IO_RING_DEPTH_MIN)
		return -EINVAL;

	if (!is_power_of_2(ring->rx_pending) ||
	    !is_power_of_2(ring->tx_pending))
		return -EINVAL;

	eea_init_ctx(enet, &ctx);

	cfg = &ctx.cfg;

	if (ring->rx_pending != cfg->rx_ring_depth)
		need_update = true;

	if (ring->tx_pending != cfg->tx_ring_depth)
		need_update = true;

	sh = false;

	switch (kernel_ring->tcp_data_split) {
	case ETHTOOL_TCP_DATA_SPLIT_ENABLED:
		sh = true;
		break;

	case ETHTOOL_TCP_DATA_SPLIT_DISABLED:
		sh = false;
		break;

	case ETHTOOL_TCP_DATA_SPLIT_UNKNOWN:
		sh = !!cfg->split_hdr;
		break;
	}

	if (sh != !!(cfg->split_hdr))
		need_update = true;

	if (!need_update)
		return 0;

	cfg->rx_ring_depth = ring->rx_pending;
	cfg->tx_ring_depth = ring->tx_pending;

	/* By default, enet->cfg_hw.split_hdr is 128. */
	cfg->split_hdr = sh ? enet->cfg_hw.split_hdr : 0;

	return eea_reset_hw_resources(enet, &ctx);
}

static int eea_set_channels(struct net_device *netdev,
			    struct ethtool_channels *channels)
{
	struct eea_net *enet = netdev_priv(netdev);
	u16 queue_pairs = channels->combined_count;
	struct eea_net_init_ctx ctx;
	struct eea_net_cfg *cfg;

	eea_init_ctx(enet, &ctx);

	cfg = &ctx.cfg;

	cfg->rx_ring_num = queue_pairs;
	cfg->tx_ring_num = queue_pairs;

	return eea_reset_hw_resources(enet, &ctx);
}

static void eea_get_channels(struct net_device *netdev,
			     struct ethtool_channels *channels)
{
	struct eea_net *enet = netdev_priv(netdev);

	channels->combined_count = enet->cfg.rx_ring_num;
	channels->max_combined   = enet->cfg_hw.rx_ring_num;
}

static void eea_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
{
	struct eea_net *enet = netdev_priv(netdev);
	u8 *p = data;
	u32 i, j;

	if (stringset != ETH_SS_STATS)
		return;

	for (i = 0; i < enet->cfg.rx_ring_num; i++) {
		for (j = 0; j < EEA_RX_STATS_LEN; j++)
			ethtool_sprintf(&p, "rx%u_%s", i,
					eea_rx_stats_desc[j].desc);
	}

	for (i = 0; i < enet->cfg.tx_ring_num; i++) {
		for (j = 0; j < EEA_TX_STATS_LEN; j++)
			ethtool_sprintf(&p, "tx%u_%s", i,
					eea_tx_stats_desc[j].desc);
	}
}

static int eea_get_sset_count(struct net_device *netdev, int sset)
{
	struct eea_net *enet = netdev_priv(netdev);

	if (sset != ETH_SS_STATS)
		return -EOPNOTSUPP;

	return enet->cfg.rx_ring_num * EEA_RX_STATS_LEN +
		enet->cfg.tx_ring_num * EEA_TX_STATS_LEN;
}

static void eea_stats_fill_for_q(struct u64_stats_sync *syncp, u32 num,
				 const struct eea_stat_desc *desc,
				 u64 *data, u32 idx)
{
	void *stats_base = syncp;
	u32 start, i;

	do {
		start = u64_stats_fetch_begin(syncp);
		for (i = 0; i < num; i++)
			data[idx + i] =
				u64_stats_read(stats_base + desc[i].offset);

	} while (u64_stats_fetch_retry(syncp, start));

	BUILD_BUG_ON(offsetof(struct eea_tx_stats, syncp));
	BUILD_BUG_ON(offsetof(struct eea_rx_stats, syncp));
}

static void eea_get_ethtool_stats(struct net_device *netdev,
				  struct ethtool_stats *stats, u64 *data)
{
	struct eea_net *enet = netdev_priv(netdev);
	u32 i, idx = 0;

	ASSERT_RTNL();

	if (enet->rx) {
		for (i = 0; i < enet->cfg.rx_ring_num; i++) {
			struct eea_net_rx *rx = enet->rx[i];

			eea_stats_fill_for_q(&rx->stats.syncp, EEA_RX_STATS_LEN,
					     eea_rx_stats_desc, data, idx);

			idx += EEA_RX_STATS_LEN;
		}
	}

	if (enet->tx) {
		for (i = 0; i < enet->cfg.tx_ring_num; i++) {
			struct eea_net_tx *tx = &enet->tx[i];

			eea_stats_fill_for_q(&tx->stats.syncp, EEA_TX_STATS_LEN,
					     eea_tx_stats_desc, data, idx);

			idx += EEA_TX_STATS_LEN;
		}
	}
}

void eea_update_rx_stats(struct eea_rx_stats *rx_stats,
			 struct eea_rx_ctx_stats *stats)
{
	u64_stats_update_begin(&rx_stats->syncp);
	u64_stats_add(&rx_stats->descs,             stats->descs);
	u64_stats_add(&rx_stats->packets,           stats->packets);
	u64_stats_add(&rx_stats->bytes,             stats->bytes);
	u64_stats_add(&rx_stats->drops,             stats->drops);
	u64_stats_add(&rx_stats->split_hdr_bytes,   stats->split_hdr_bytes);
	u64_stats_add(&rx_stats->split_hdr_packets, stats->split_hdr_packets);
	u64_stats_add(&rx_stats->length_errors,     stats->length_errors);
	u64_stats_add(&rx_stats->kicks,             stats->kicks);
	u64_stats_update_end(&rx_stats->syncp);
}

static int eea_get_link_ksettings(struct net_device *netdev,
				  struct ethtool_link_ksettings *cmd)
{
	struct eea_net *enet = netdev_priv(netdev);

	cmd->base.speed  = enet->speed;
	cmd->base.duplex = enet->duplex;
	cmd->base.port   = PORT_OTHER;

	return 0;
}

const struct ethtool_ops eea_ethtool_ops = {
	.supported_ring_params = ETHTOOL_RING_USE_TCP_DATA_SPLIT,
	.get_drvinfo        = eea_get_drvinfo,
	.get_link           = ethtool_op_get_link,
	.get_ringparam      = eea_get_ringparam,
	.set_ringparam      = eea_set_ringparam,
	.set_channels       = eea_set_channels,
	.get_channels       = eea_get_channels,
	.get_strings        = eea_get_strings,
	.get_sset_count     = eea_get_sset_count,
	.get_ethtool_stats  = eea_get_ethtool_stats,
	.get_link_ksettings = eea_get_link_ksettings,
};