summaryrefslogtreecommitdiffstats
path: root/drivers/net/ethernet/engleder/tsnep_ethtool.c
blob: 65ec1abc944211471f5eef3d564718c2c6b566fd (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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2021 Gerhard Engleder <gerhard@engleder-embedded.com> */

#include "tsnep.h"

static const char tsnep_stats_strings[][ETH_GSTRING_LEN] = {
	"rx_packets",
	"rx_bytes",
	"rx_dropped",
	"rx_multicast",
	"rx_alloc_failed",
	"rx_phy_errors",
	"rx_forwarded_phy_errors",
	"rx_invalid_frame_errors",
	"tx_packets",
	"tx_bytes",
	"tx_dropped",
};

struct tsnep_stats {
	u64 rx_packets;
	u64 rx_bytes;
	u64 rx_dropped;
	u64 rx_multicast;
	u64 rx_alloc_failed;
	u64 rx_phy_errors;
	u64 rx_forwarded_phy_errors;
	u64 rx_invalid_frame_errors;
	u64 tx_packets;
	u64 tx_bytes;
	u64 tx_dropped;
};

#define TSNEP_STATS_COUNT (sizeof(struct tsnep_stats) / sizeof(u64))

static const char tsnep_rx_queue_stats_strings[][ETH_GSTRING_LEN] = {
	"rx_%d_packets",
	"rx_%d_bytes",
	"rx_%d_dropped",
	"rx_%d_multicast",
	"rx_%d_alloc_failed",
	"rx_%d_no_descriptor_errors",
	"rx_%d_buffer_too_small_errors",
	"rx_%d_fifo_overflow_errors",
	"rx_%d_invalid_frame_errors",
};

struct tsnep_rx_queue_stats {
	u64 rx_packets;
	u64 rx_bytes;
	u64 rx_dropped;
	u64 rx_multicast;
	u64 rx_alloc_failed;
	u64 rx_no_descriptor_errors;
	u64 rx_buffer_too_small_errors;
	u64 rx_fifo_overflow_errors;
	u64 rx_invalid_frame_errors;
};

#define TSNEP_RX_QUEUE_STATS_COUNT (sizeof(struct tsnep_rx_queue_stats) / \
				    sizeof(u64))

static const char tsnep_tx_queue_stats_strings[][ETH_GSTRING_LEN] = {
	"tx_%d_packets",
	"tx_%d_bytes",
	"tx_%d_dropped",
};

struct tsnep_tx_queue_stats {
	u64 tx_packets;
	u64 tx_bytes;
	u64 tx_dropped;
};

#define TSNEP_TX_QUEUE_STATS_COUNT (sizeof(struct tsnep_tx_queue_stats) / \
				    sizeof(u64))

static void tsnep_ethtool_get_drvinfo(struct net_device *netdev,
				      struct ethtool_drvinfo *drvinfo)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	strscpy(drvinfo->driver, TSNEP, sizeof(drvinfo->driver));
	strscpy(drvinfo->bus_info, dev_name(&adapter->pdev->dev),
		sizeof(drvinfo->bus_info));
}

static int tsnep_ethtool_get_regs_len(struct net_device *netdev)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	int len;
	int num_additional_queues;

	len = TSNEP_MAC_SIZE;

	/* first queue pair is within TSNEP_MAC_SIZE, only queues additional to
	 * the first queue pair extend the register length by TSNEP_QUEUE_SIZE
	 */
	num_additional_queues =
		max(adapter->num_tx_queues, adapter->num_rx_queues) - 1;
	len += TSNEP_QUEUE_SIZE * num_additional_queues;

	return len;
}

static void tsnep_ethtool_get_regs(struct net_device *netdev,
				   struct ethtool_regs *regs,
				   void *p)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	regs->version = 1;

	memcpy_fromio(p, adapter->addr, regs->len);
}

static u32 tsnep_ethtool_get_msglevel(struct net_device *netdev)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	return adapter->msg_enable;
}

static void tsnep_ethtool_set_msglevel(struct net_device *netdev, u32 data)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	adapter->msg_enable = data;
}

static void tsnep_ethtool_get_strings(struct net_device *netdev, u32 stringset,
				      u8 *data)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	int rx_count = adapter->num_rx_queues;
	int tx_count = adapter->num_tx_queues;
	int i, j;

	switch (stringset) {
	case ETH_SS_STATS:
		memcpy(data, tsnep_stats_strings, sizeof(tsnep_stats_strings));
		data += sizeof(tsnep_stats_strings);

		for (i = 0; i < rx_count; i++) {
			for (j = 0; j < TSNEP_RX_QUEUE_STATS_COUNT; j++) {
				snprintf(data, ETH_GSTRING_LEN,
					 tsnep_rx_queue_stats_strings[j], i);
				data += ETH_GSTRING_LEN;
			}
		}

		for (i = 0; i < tx_count; i++) {
			for (j = 0; j < TSNEP_TX_QUEUE_STATS_COUNT; j++) {
				snprintf(data, ETH_GSTRING_LEN,
					 tsnep_tx_queue_stats_strings[j], i);
				data += ETH_GSTRING_LEN;
			}
		}
		break;
	case ETH_SS_TEST:
		tsnep_ethtool_get_test_strings(data);
		break;
	}
}

static void tsnep_ethtool_get_ethtool_stats(struct net_device *netdev,
					    struct ethtool_stats *stats,
					    u64 *data)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	int rx_count = adapter->num_rx_queues;
	int tx_count = adapter->num_tx_queues;
	struct tsnep_stats tsnep_stats;
	struct tsnep_rx_queue_stats tsnep_rx_queue_stats;
	struct tsnep_tx_queue_stats tsnep_tx_queue_stats;
	u32 reg;
	int i;

	memset(&tsnep_stats, 0, sizeof(tsnep_stats));
	for (i = 0; i < adapter->num_rx_queues; i++) {
		tsnep_stats.rx_packets += adapter->rx[i].packets;
		tsnep_stats.rx_bytes += adapter->rx[i].bytes;
		tsnep_stats.rx_dropped += adapter->rx[i].dropped;
		tsnep_stats.rx_multicast += adapter->rx[i].multicast;
		tsnep_stats.rx_alloc_failed += adapter->rx[i].alloc_failed;
	}
	reg = ioread32(adapter->addr + ECM_STAT);
	tsnep_stats.rx_phy_errors =
		(reg & ECM_STAT_RX_ERR_MASK) >> ECM_STAT_RX_ERR_SHIFT;
	tsnep_stats.rx_forwarded_phy_errors =
		(reg & ECM_STAT_FWD_RX_ERR_MASK) >> ECM_STAT_FWD_RX_ERR_SHIFT;
	tsnep_stats.rx_invalid_frame_errors =
		(reg & ECM_STAT_INV_FRM_MASK) >> ECM_STAT_INV_FRM_SHIFT;
	for (i = 0; i < adapter->num_tx_queues; i++) {
		tsnep_stats.tx_packets += adapter->tx[i].packets;
		tsnep_stats.tx_bytes += adapter->tx[i].bytes;
		tsnep_stats.tx_dropped += adapter->tx[i].dropped;
	}
	memcpy(data, &tsnep_stats, sizeof(tsnep_stats));
	data += TSNEP_STATS_COUNT;

	for (i = 0; i < rx_count; i++) {
		memset(&tsnep_rx_queue_stats, 0, sizeof(tsnep_rx_queue_stats));
		tsnep_rx_queue_stats.rx_packets = adapter->rx[i].packets;
		tsnep_rx_queue_stats.rx_bytes = adapter->rx[i].bytes;
		tsnep_rx_queue_stats.rx_dropped = adapter->rx[i].dropped;
		tsnep_rx_queue_stats.rx_multicast = adapter->rx[i].multicast;
		tsnep_rx_queue_stats.rx_alloc_failed =
			adapter->rx[i].alloc_failed;
		reg = ioread32(adapter->addr + TSNEP_QUEUE(i) +
			       TSNEP_RX_STATISTIC);
		tsnep_rx_queue_stats.rx_no_descriptor_errors =
			(reg & TSNEP_RX_STATISTIC_NO_DESC_MASK) >>
			TSNEP_RX_STATISTIC_NO_DESC_SHIFT;
		tsnep_rx_queue_stats.rx_buffer_too_small_errors =
			(reg & TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_MASK) >>
			TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_SHIFT;
		tsnep_rx_queue_stats.rx_fifo_overflow_errors =
			(reg & TSNEP_RX_STATISTIC_FIFO_OVERFLOW_MASK) >>
			TSNEP_RX_STATISTIC_FIFO_OVERFLOW_SHIFT;
		tsnep_rx_queue_stats.rx_invalid_frame_errors =
			(reg & TSNEP_RX_STATISTIC_INVALID_FRAME_MASK) >>
			TSNEP_RX_STATISTIC_INVALID_FRAME_SHIFT;
		memcpy(data, &tsnep_rx_queue_stats,
		       sizeof(tsnep_rx_queue_stats));
		data += TSNEP_RX_QUEUE_STATS_COUNT;
	}

	for (i = 0; i < tx_count; i++) {
		memset(&tsnep_tx_queue_stats, 0, sizeof(tsnep_tx_queue_stats));
		tsnep_tx_queue_stats.tx_packets += adapter->tx[i].packets;
		tsnep_tx_queue_stats.tx_bytes += adapter->tx[i].bytes;
		tsnep_tx_queue_stats.tx_dropped += adapter->tx[i].dropped;
		memcpy(data, &tsnep_tx_queue_stats,
		       sizeof(tsnep_tx_queue_stats));
		data += TSNEP_TX_QUEUE_STATS_COUNT;
	}
}

static int tsnep_ethtool_get_sset_count(struct net_device *netdev, int sset)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	int rx_count;
	int tx_count;

	switch (sset) {
	case ETH_SS_STATS:
		rx_count = adapter->num_rx_queues;
		tx_count = adapter->num_tx_queues;
		return TSNEP_STATS_COUNT +
		       TSNEP_RX_QUEUE_STATS_COUNT * rx_count +
		       TSNEP_TX_QUEUE_STATS_COUNT * tx_count;
	case ETH_SS_TEST:
		return tsnep_ethtool_get_test_count();
	default:
		return -EOPNOTSUPP;
	}
}

static int tsnep_ethtool_get_rxnfc(struct net_device *netdev,
				   struct ethtool_rxnfc *cmd, u32 *rule_locs)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	switch (cmd->cmd) {
	case ETHTOOL_GRXRINGS:
		cmd->data = adapter->num_rx_queues;
		return 0;
	case ETHTOOL_GRXCLSRLCNT:
		cmd->rule_cnt = adapter->rxnfc_count;
		cmd->data = adapter->rxnfc_max;
		cmd->data |= RX_CLS_LOC_SPECIAL;
		return 0;
	case ETHTOOL_GRXCLSRULE:
		return tsnep_rxnfc_get_rule(adapter, cmd);
	case ETHTOOL_GRXCLSRLALL:
		return tsnep_rxnfc_get_all(adapter, cmd, rule_locs);
	default:
		return -EOPNOTSUPP;
	}
}

static int tsnep_ethtool_set_rxnfc(struct net_device *netdev,
				   struct ethtool_rxnfc *cmd)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	switch (cmd->cmd) {
	case ETHTOOL_SRXCLSRLINS:
		return tsnep_rxnfc_add_rule(adapter, cmd);
	case ETHTOOL_SRXCLSRLDEL:
		return tsnep_rxnfc_del_rule(adapter, cmd);
	default:
		return -EOPNOTSUPP;
	}
}

static void tsnep_ethtool_get_channels(struct net_device *netdev,
				       struct ethtool_channels *ch)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	ch->max_combined = adapter->num_queues;
	ch->combined_count = adapter->num_queues;
}

static int tsnep_ethtool_get_ts_info(struct net_device *netdev,
				     struct ethtool_ts_info *info)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);

	info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
				SOF_TIMESTAMPING_RX_SOFTWARE |
				SOF_TIMESTAMPING_SOFTWARE |
				SOF_TIMESTAMPING_TX_HARDWARE |
				SOF_TIMESTAMPING_RX_HARDWARE |
				SOF_TIMESTAMPING_RAW_HARDWARE;

	if (adapter->ptp_clock)
		info->phc_index = ptp_clock_index(adapter->ptp_clock);
	else
		info->phc_index = -1;

	info->tx_types = BIT(HWTSTAMP_TX_OFF) |
			 BIT(HWTSTAMP_TX_ON);
	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
			   BIT(HWTSTAMP_FILTER_ALL);

	return 0;
}

static struct tsnep_queue *tsnep_get_queue_with_tx(struct tsnep_adapter *adapter,
						   int index)
{
	int i;

	for (i = 0; i < adapter->num_queues; i++) {
		if (adapter->queue[i].tx) {
			if (index == 0)
				return &adapter->queue[i];

			index--;
		}
	}

	return NULL;
}

static struct tsnep_queue *tsnep_get_queue_with_rx(struct tsnep_adapter *adapter,
						   int index)
{
	int i;

	for (i = 0; i < adapter->num_queues; i++) {
		if (adapter->queue[i].rx) {
			if (index == 0)
				return &adapter->queue[i];

			index--;
		}
	}

	return NULL;
}

static int tsnep_ethtool_get_coalesce(struct net_device *netdev,
				      struct ethtool_coalesce *ec,
				      struct kernel_ethtool_coalesce *kernel_coal,
				      struct netlink_ext_ack *extack)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	struct tsnep_queue *queue;

	queue = tsnep_get_queue_with_rx(adapter, 0);
	if (queue)
		ec->rx_coalesce_usecs = tsnep_get_irq_coalesce(queue);

	queue = tsnep_get_queue_with_tx(adapter, 0);
	if (queue)
		ec->tx_coalesce_usecs = tsnep_get_irq_coalesce(queue);

	return 0;
}

static int tsnep_ethtool_set_coalesce(struct net_device *netdev,
				      struct ethtool_coalesce *ec,
				      struct kernel_ethtool_coalesce *kernel_coal,
				      struct netlink_ext_ack *extack)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	int i;
	int retval;

	for (i = 0; i < adapter->num_queues; i++) {
		/* RX coalesce has priority for queues with TX and RX */
		if (adapter->queue[i].rx)
			retval = tsnep_set_irq_coalesce(&adapter->queue[i],
							ec->rx_coalesce_usecs);
		else
			retval = tsnep_set_irq_coalesce(&adapter->queue[i],
							ec->tx_coalesce_usecs);
		if (retval != 0)
			return retval;
	}

	return 0;
}

static int tsnep_ethtool_get_per_queue_coalesce(struct net_device *netdev,
						u32 queue,
						struct ethtool_coalesce *ec)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	struct tsnep_queue *queue_with_rx;
	struct tsnep_queue *queue_with_tx;

	if (queue >= max(adapter->num_tx_queues, adapter->num_rx_queues))
		return -EINVAL;

	queue_with_rx = tsnep_get_queue_with_rx(adapter, queue);
	if (queue_with_rx)
		ec->rx_coalesce_usecs = tsnep_get_irq_coalesce(queue_with_rx);

	queue_with_tx = tsnep_get_queue_with_tx(adapter, queue);
	if (queue_with_tx)
		ec->tx_coalesce_usecs = tsnep_get_irq_coalesce(queue_with_tx);

	return 0;
}

static int tsnep_ethtool_set_per_queue_coalesce(struct net_device *netdev,
						u32 queue,
						struct ethtool_coalesce *ec)
{
	struct tsnep_adapter *adapter = netdev_priv(netdev);
	struct tsnep_queue *queue_with_rx;
	struct tsnep_queue *queue_with_tx;
	int retval;

	if (queue >= max(adapter->num_tx_queues, adapter->num_rx_queues))
		return -EINVAL;

	queue_with_rx = tsnep_get_queue_with_rx(adapter, queue);
	if (queue_with_rx) {
		retval = tsnep_set_irq_coalesce(queue_with_rx, ec->rx_coalesce_usecs);
		if (retval != 0)
			return retval;
	}

	/* RX coalesce has priority for queues with TX and RX */
	queue_with_tx = tsnep_get_queue_with_tx(adapter, queue);
	if (queue_with_tx && !queue_with_tx->rx) {
		retval = tsnep_set_irq_coalesce(queue_with_tx, ec->tx_coalesce_usecs);
		if (retval != 0)
			return retval;
	}

	return 0;
}

const struct ethtool_ops tsnep_ethtool_ops = {
	.supported_coalesce_params = ETHTOOL_COALESCE_USECS,
	.get_drvinfo = tsnep_ethtool_get_drvinfo,
	.get_regs_len = tsnep_ethtool_get_regs_len,
	.get_regs = tsnep_ethtool_get_regs,
	.get_msglevel = tsnep_ethtool_get_msglevel,
	.set_msglevel = tsnep_ethtool_set_msglevel,
	.nway_reset = phy_ethtool_nway_reset,
	.get_link = ethtool_op_get_link,
	.self_test = tsnep_ethtool_self_test,
	.get_strings = tsnep_ethtool_get_strings,
	.get_ethtool_stats = tsnep_ethtool_get_ethtool_stats,
	.get_sset_count = tsnep_ethtool_get_sset_count,
	.get_rxnfc = tsnep_ethtool_get_rxnfc,
	.set_rxnfc = tsnep_ethtool_set_rxnfc,
	.get_channels = tsnep_ethtool_get_channels,
	.get_ts_info = tsnep_ethtool_get_ts_info,
	.get_coalesce = tsnep_ethtool_get_coalesce,
	.set_coalesce = tsnep_ethtool_set_coalesce,
	.get_per_queue_coalesce = tsnep_ethtool_get_per_queue_coalesce,
	.set_per_queue_coalesce = tsnep_ethtool_set_per_queue_coalesce,
	.get_link_ksettings = phy_ethtool_get_link_ksettings,
	.set_link_ksettings = phy_ethtool_set_link_ksettings,
};