xref: /openbmc/linux/drivers/net/ethernet/apm/xgene-v2/ethtool.c (revision 8ed211af28d9dee766a8f5bf42d2f1ff6eebe8e1)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Applied Micro X-Gene SoC Ethernet v2 Driver
4  *
5  * Copyright (c) 2017, Applied Micro Circuits Corporation
6  * Author(s): Iyappan Subramanian <isubramanian@apm.com>
7  *	      Keyur Chudgar <kchudgar@apm.com>
8  */
9 
10 #include "main.h"
11 
12 #define XGE_STAT(m)		{ #m, offsetof(struct xge_pdata, stats.m) }
13 #define XGE_EXTD_STAT(m, n)					\
14 	{							\
15 		#m,						\
16 		n,						\
17 		0						\
18 	}
19 
20 static const struct xge_gstrings_stats gstrings_stats[] = {
21 	XGE_STAT(rx_packets),
22 	XGE_STAT(tx_packets),
23 	XGE_STAT(rx_bytes),
24 	XGE_STAT(tx_bytes),
25 	XGE_STAT(rx_errors)
26 };
27 
28 static struct xge_gstrings_extd_stats gstrings_extd_stats[] = {
29 	XGE_EXTD_STAT(tx_rx_64b_frame_cntr, TR64),
30 	XGE_EXTD_STAT(tx_rx_127b_frame_cntr, TR127),
31 	XGE_EXTD_STAT(tx_rx_255b_frame_cntr, TR255),
32 	XGE_EXTD_STAT(tx_rx_511b_frame_cntr, TR511),
33 	XGE_EXTD_STAT(tx_rx_1023b_frame_cntr, TR1K),
34 	XGE_EXTD_STAT(tx_rx_1518b_frame_cntr, TRMAX),
35 	XGE_EXTD_STAT(tx_rx_1522b_frame_cntr, TRMGV),
36 	XGE_EXTD_STAT(rx_fcs_error_cntr, RFCS),
37 	XGE_EXTD_STAT(rx_multicast_pkt_cntr, RMCA),
38 	XGE_EXTD_STAT(rx_broadcast_pkt_cntr, RBCA),
39 	XGE_EXTD_STAT(rx_ctrl_frame_pkt_cntr, RXCF),
40 	XGE_EXTD_STAT(rx_pause_frame_pkt_cntr, RXPF),
41 	XGE_EXTD_STAT(rx_unk_opcode_cntr, RXUO),
42 	XGE_EXTD_STAT(rx_align_err_cntr, RALN),
43 	XGE_EXTD_STAT(rx_frame_len_err_cntr, RFLR),
44 	XGE_EXTD_STAT(rx_code_err_cntr, RCDE),
45 	XGE_EXTD_STAT(rx_carrier_sense_err_cntr, RCSE),
46 	XGE_EXTD_STAT(rx_undersize_pkt_cntr, RUND),
47 	XGE_EXTD_STAT(rx_oversize_pkt_cntr, ROVR),
48 	XGE_EXTD_STAT(rx_fragments_cntr, RFRG),
49 	XGE_EXTD_STAT(rx_jabber_cntr, RJBR),
50 	XGE_EXTD_STAT(rx_dropped_pkt_cntr, RDRP),
51 	XGE_EXTD_STAT(tx_multicast_pkt_cntr, TMCA),
52 	XGE_EXTD_STAT(tx_broadcast_pkt_cntr, TBCA),
53 	XGE_EXTD_STAT(tx_pause_ctrl_frame_cntr, TXPF),
54 	XGE_EXTD_STAT(tx_defer_pkt_cntr, TDFR),
55 	XGE_EXTD_STAT(tx_excv_defer_pkt_cntr, TEDF),
56 	XGE_EXTD_STAT(tx_single_col_pkt_cntr, TSCL),
57 	XGE_EXTD_STAT(tx_multi_col_pkt_cntr, TMCL),
58 	XGE_EXTD_STAT(tx_late_col_pkt_cntr, TLCL),
59 	XGE_EXTD_STAT(tx_excv_col_pkt_cntr, TXCL),
60 	XGE_EXTD_STAT(tx_total_col_cntr, TNCL),
61 	XGE_EXTD_STAT(tx_pause_frames_hnrd_cntr, TPFH),
62 	XGE_EXTD_STAT(tx_drop_frame_cntr, TDRP),
63 	XGE_EXTD_STAT(tx_jabber_frame_cntr, TJBR),
64 	XGE_EXTD_STAT(tx_fcs_error_cntr, TFCS),
65 	XGE_EXTD_STAT(tx_ctrl_frame_cntr, TXCF),
66 	XGE_EXTD_STAT(tx_oversize_frame_cntr, TOVR),
67 	XGE_EXTD_STAT(tx_undersize_frame_cntr, TUND),
68 	XGE_EXTD_STAT(tx_fragments_cntr, TFRG)
69 };
70 
71 #define XGE_STATS_LEN		ARRAY_SIZE(gstrings_stats)
72 #define XGE_EXTD_STATS_LEN	ARRAY_SIZE(gstrings_extd_stats)
73 
74 static void xge_mac_get_extd_stats(struct xge_pdata *pdata)
75 {
76 	u32 data;
77 	int i;
78 
79 	for (i = 0; i < XGE_EXTD_STATS_LEN; i++) {
80 		data = xge_rd_csr(pdata, gstrings_extd_stats[i].addr);
81 		gstrings_extd_stats[i].value += data;
82 	}
83 }
84 
85 static void xge_get_drvinfo(struct net_device *ndev,
86 			    struct ethtool_drvinfo *info)
87 {
88 	struct xge_pdata *pdata = netdev_priv(ndev);
89 	struct platform_device *pdev = pdata->pdev;
90 
91 	strcpy(info->driver, "xgene-enet-v2");
92 	snprintf(info->fw_version, ETHTOOL_FWVERS_LEN, "N/A");
93 	sprintf(info->bus_info, "%s", pdev->name);
94 }
95 
96 static void xge_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
97 {
98 	u8 *p = data;
99 	int i;
100 
101 	if (stringset != ETH_SS_STATS)
102 		return;
103 
104 	for (i = 0; i < XGE_STATS_LEN; i++) {
105 		memcpy(p, gstrings_stats[i].name, ETH_GSTRING_LEN);
106 		p += ETH_GSTRING_LEN;
107 	}
108 
109 	for (i = 0; i < XGE_EXTD_STATS_LEN; i++) {
110 		memcpy(p, gstrings_extd_stats[i].name, ETH_GSTRING_LEN);
111 		p += ETH_GSTRING_LEN;
112 	}
113 }
114 
115 static int xge_get_sset_count(struct net_device *ndev, int sset)
116 {
117 	if (sset != ETH_SS_STATS)
118 		return -EINVAL;
119 
120 	return XGE_STATS_LEN + XGE_EXTD_STATS_LEN;
121 }
122 
123 static void xge_get_ethtool_stats(struct net_device *ndev,
124 				  struct ethtool_stats *dummy,
125 				  u64 *data)
126 {
127 	void *pdata = netdev_priv(ndev);
128 	int i;
129 
130 	for (i = 0; i < XGE_STATS_LEN; i++)
131 		*data++ = *(u64 *)(pdata + gstrings_stats[i].offset);
132 
133 	xge_mac_get_extd_stats(pdata);
134 
135 	for (i = 0; i < XGE_EXTD_STATS_LEN; i++)
136 		*data++ = gstrings_extd_stats[i].value;
137 }
138 
139 static int xge_get_link_ksettings(struct net_device *ndev,
140 				  struct ethtool_link_ksettings *cmd)
141 {
142 	struct phy_device *phydev = ndev->phydev;
143 
144 	if (!phydev)
145 		return -ENODEV;
146 
147 	phy_ethtool_ksettings_get(phydev, cmd);
148 
149 	return 0;
150 }
151 
152 static int xge_set_link_ksettings(struct net_device *ndev,
153 				  const struct ethtool_link_ksettings *cmd)
154 {
155 	struct phy_device *phydev = ndev->phydev;
156 
157 	if (!phydev)
158 		return -ENODEV;
159 
160 	return phy_ethtool_ksettings_set(phydev, cmd);
161 }
162 
163 static const struct ethtool_ops xge_ethtool_ops = {
164 	.get_drvinfo = xge_get_drvinfo,
165 	.get_link = ethtool_op_get_link,
166 	.get_strings = xge_get_strings,
167 	.get_sset_count = xge_get_sset_count,
168 	.get_ethtool_stats = xge_get_ethtool_stats,
169 	.get_link_ksettings = xge_get_link_ksettings,
170 	.set_link_ksettings = xge_set_link_ksettings,
171 };
172 
173 void xge_set_ethtool_ops(struct net_device *ndev)
174 {
175 	ndev->ethtool_ops = &xge_ethtool_ops;
176 }
177