xref: /linux/drivers/net/ethernet/ti/icssg/icssg_stats.c (revision 5fc31936081919a8572a3d644f3fbb258038f337)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Texas Instruments ICSSG Ethernet driver
3  *
4  * Copyright (C) 2018-2021 Texas Instruments Incorporated - https://www.ti.com/
5  *
6  */
7 
8 #include "icssg_prueth.h"
9 #include "icssg_stats.h"
10 #include <linux/regmap.h>
11 
12 #define ICSSG_TX_PACKET_OFFSET	0xA0
13 #define ICSSG_TX_BYTE_OFFSET	0xEC
14 #define ICSSG_FW_STATS_BASE	0x0248
15 
16 static u32 stats_base[] = {	0x54c,	/* Slice 0 stats start */
17 				0xb18,	/* Slice 1 stats start */
18 };
19 
emac_update_hardware_stats(struct prueth_emac * emac)20 void emac_update_hardware_stats(struct prueth_emac *emac)
21 {
22 	struct prueth *prueth = emac->prueth;
23 	int slice = prueth_emac_slice(emac);
24 	u32 base = stats_base[slice];
25 	u32 tx_pkt_cnt = 0;
26 	u32 val, reg;
27 	int i;
28 
29 	spin_lock(&prueth->stats_lock);
30 
31 	for (i = 0; i < ARRAY_SIZE(icssg_all_miig_stats); i++) {
32 		regmap_read(prueth->miig_rt,
33 			    base + icssg_all_miig_stats[i].offset,
34 			    &val);
35 		regmap_write(prueth->miig_rt,
36 			     base + icssg_all_miig_stats[i].offset,
37 			     val);
38 
39 		if (icssg_all_miig_stats[i].offset == ICSSG_TX_PACKET_OFFSET)
40 			tx_pkt_cnt = val;
41 
42 		emac->stats[i] += val;
43 		if (icssg_all_miig_stats[i].offset == ICSSG_TX_BYTE_OFFSET)
44 			emac->stats[i] -= tx_pkt_cnt * 8;
45 	}
46 
47 	if (prueth->pa_stats) {
48 		for (i = 0; i < ARRAY_SIZE(icssg_all_pa_stats); i++) {
49 			reg = ICSSG_FW_STATS_BASE +
50 			      icssg_all_pa_stats[i].offset *
51 			      PRUETH_NUM_MACS + slice * sizeof(u32);
52 			regmap_read(prueth->pa_stats, reg, &val);
53 			emac->pa_stats[i] += val;
54 		}
55 	}
56 
57 	spin_unlock(&prueth->stats_lock);
58 }
59 
icssg_stats_work_handler(struct work_struct * work)60 void icssg_stats_work_handler(struct work_struct *work)
61 {
62 	struct prueth_emac *emac = container_of(work, struct prueth_emac,
63 						stats_work.work);
64 	emac_update_hardware_stats(emac);
65 
66 	queue_delayed_work(system_long_wq, &emac->stats_work,
67 			   msecs_to_jiffies((STATS_TIME_LIMIT_1G_MS * 1000) / emac->speed));
68 }
69 EXPORT_SYMBOL_GPL(icssg_stats_work_handler);
70 
emac_get_stat_by_name(struct prueth_emac * emac,char * stat_name)71 int emac_get_stat_by_name(struct prueth_emac *emac, char *stat_name)
72 {
73 	int i;
74 
75 	for (i = 0; i < ARRAY_SIZE(icssg_all_miig_stats); i++) {
76 		if (!strcmp(icssg_all_miig_stats[i].name, stat_name))
77 			return emac->stats[icssg_all_miig_stats[i].offset / sizeof(u32)];
78 	}
79 
80 	if (emac->prueth->pa_stats) {
81 		for (i = 0; i < ARRAY_SIZE(icssg_all_pa_stats); i++) {
82 			if (!strcmp(icssg_all_pa_stats[i].name, stat_name))
83 				return emac->pa_stats[icssg_all_pa_stats[i].offset / sizeof(u32)];
84 		}
85 	}
86 
87 	netdev_err(emac->ndev, "Invalid stats %s\n", stat_name);
88 	return -EINVAL;
89 }
90