xref: /openbmc/linux/drivers/net/ethernet/synopsys/dwc-xlgmac-ethtool.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1d4d49bc1SJie Deng /* Synopsys DesignWare Core Enterprise Ethernet (XLGMAC) Driver
2d4d49bc1SJie Deng  *
3d4d49bc1SJie Deng  * Copyright (c) 2017 Synopsys, Inc. (www.synopsys.com)
4d4d49bc1SJie Deng  *
5d4d49bc1SJie Deng  * This program is dual-licensed; you may select either version 2 of
6d4d49bc1SJie Deng  * the GNU General Public License ("GPL") or BSD license ("BSD").
7d4d49bc1SJie Deng  *
8d4d49bc1SJie Deng  * This Synopsys DWC XLGMAC software driver and associated documentation
9d4d49bc1SJie Deng  * (hereinafter the "Software") is an unsupported proprietary work of
10d4d49bc1SJie Deng  * Synopsys, Inc. unless otherwise expressly agreed to in writing between
11d4d49bc1SJie Deng  * Synopsys and you. The Software IS NOT an item of Licensed Software or a
12d4d49bc1SJie Deng  * Licensed Product under any End User Software License Agreement or
13d4d49bc1SJie Deng  * Agreement for Licensed Products with Synopsys or any supplement thereto.
14d4d49bc1SJie Deng  * Synopsys is a registered trademark of Synopsys, Inc. Other names included
15d4d49bc1SJie Deng  * in the SOFTWARE may be the trademarks of their respective owners.
16d4d49bc1SJie Deng  */
17d4d49bc1SJie Deng 
18d4d49bc1SJie Deng #include <linux/ethtool.h>
19d4d49bc1SJie Deng #include <linux/kernel.h>
20d4d49bc1SJie Deng #include <linux/netdevice.h>
21d4d49bc1SJie Deng 
22d4d49bc1SJie Deng #include "dwc-xlgmac.h"
23d4d49bc1SJie Deng #include "dwc-xlgmac-reg.h"
24d4d49bc1SJie Deng 
25d4d49bc1SJie Deng struct xlgmac_stats_desc {
26d4d49bc1SJie Deng 	char stat_string[ETH_GSTRING_LEN];
27d4d49bc1SJie Deng 	int stat_offset;
28d4d49bc1SJie Deng };
29d4d49bc1SJie Deng 
30d4d49bc1SJie Deng #define XLGMAC_STAT(str, var)					\
31d4d49bc1SJie Deng 	{							\
32d4d49bc1SJie Deng 		str,						\
33d4d49bc1SJie Deng 		offsetof(struct xlgmac_pdata, stats.var),	\
34d4d49bc1SJie Deng 	}
35d4d49bc1SJie Deng 
36d4d49bc1SJie Deng static const struct xlgmac_stats_desc xlgmac_gstring_stats[] = {
37d4d49bc1SJie Deng 	/* MMC TX counters */
38d4d49bc1SJie Deng 	XLGMAC_STAT("tx_bytes", txoctetcount_gb),
39d4d49bc1SJie Deng 	XLGMAC_STAT("tx_bytes_good", txoctetcount_g),
40d4d49bc1SJie Deng 	XLGMAC_STAT("tx_packets", txframecount_gb),
41d4d49bc1SJie Deng 	XLGMAC_STAT("tx_packets_good", txframecount_g),
42d4d49bc1SJie Deng 	XLGMAC_STAT("tx_unicast_packets", txunicastframes_gb),
43d4d49bc1SJie Deng 	XLGMAC_STAT("tx_broadcast_packets", txbroadcastframes_gb),
44d4d49bc1SJie Deng 	XLGMAC_STAT("tx_broadcast_packets_good", txbroadcastframes_g),
45d4d49bc1SJie Deng 	XLGMAC_STAT("tx_multicast_packets", txmulticastframes_gb),
46d4d49bc1SJie Deng 	XLGMAC_STAT("tx_multicast_packets_good", txmulticastframes_g),
47d4d49bc1SJie Deng 	XLGMAC_STAT("tx_vlan_packets_good", txvlanframes_g),
48d4d49bc1SJie Deng 	XLGMAC_STAT("tx_64_byte_packets", tx64octets_gb),
49d4d49bc1SJie Deng 	XLGMAC_STAT("tx_65_to_127_byte_packets", tx65to127octets_gb),
50d4d49bc1SJie Deng 	XLGMAC_STAT("tx_128_to_255_byte_packets", tx128to255octets_gb),
51d4d49bc1SJie Deng 	XLGMAC_STAT("tx_256_to_511_byte_packets", tx256to511octets_gb),
52d4d49bc1SJie Deng 	XLGMAC_STAT("tx_512_to_1023_byte_packets", tx512to1023octets_gb),
53d4d49bc1SJie Deng 	XLGMAC_STAT("tx_1024_to_max_byte_packets", tx1024tomaxoctets_gb),
54d4d49bc1SJie Deng 	XLGMAC_STAT("tx_underflow_errors", txunderflowerror),
55d4d49bc1SJie Deng 	XLGMAC_STAT("tx_pause_frames", txpauseframes),
56d4d49bc1SJie Deng 
57d4d49bc1SJie Deng 	/* MMC RX counters */
58d4d49bc1SJie Deng 	XLGMAC_STAT("rx_bytes", rxoctetcount_gb),
59d4d49bc1SJie Deng 	XLGMAC_STAT("rx_bytes_good", rxoctetcount_g),
60d4d49bc1SJie Deng 	XLGMAC_STAT("rx_packets", rxframecount_gb),
61d4d49bc1SJie Deng 	XLGMAC_STAT("rx_unicast_packets_good", rxunicastframes_g),
62d4d49bc1SJie Deng 	XLGMAC_STAT("rx_broadcast_packets_good", rxbroadcastframes_g),
63d4d49bc1SJie Deng 	XLGMAC_STAT("rx_multicast_packets_good", rxmulticastframes_g),
64d4d49bc1SJie Deng 	XLGMAC_STAT("rx_vlan_packets", rxvlanframes_gb),
65d4d49bc1SJie Deng 	XLGMAC_STAT("rx_64_byte_packets", rx64octets_gb),
66d4d49bc1SJie Deng 	XLGMAC_STAT("rx_65_to_127_byte_packets", rx65to127octets_gb),
67d4d49bc1SJie Deng 	XLGMAC_STAT("rx_128_to_255_byte_packets", rx128to255octets_gb),
68d4d49bc1SJie Deng 	XLGMAC_STAT("rx_256_to_511_byte_packets", rx256to511octets_gb),
69d4d49bc1SJie Deng 	XLGMAC_STAT("rx_512_to_1023_byte_packets", rx512to1023octets_gb),
70d4d49bc1SJie Deng 	XLGMAC_STAT("rx_1024_to_max_byte_packets", rx1024tomaxoctets_gb),
71d4d49bc1SJie Deng 	XLGMAC_STAT("rx_undersize_packets_good", rxundersize_g),
72d4d49bc1SJie Deng 	XLGMAC_STAT("rx_oversize_packets_good", rxoversize_g),
73d4d49bc1SJie Deng 	XLGMAC_STAT("rx_crc_errors", rxcrcerror),
74d4d49bc1SJie Deng 	XLGMAC_STAT("rx_crc_errors_small_packets", rxrunterror),
75d4d49bc1SJie Deng 	XLGMAC_STAT("rx_crc_errors_giant_packets", rxjabbererror),
76d4d49bc1SJie Deng 	XLGMAC_STAT("rx_length_errors", rxlengtherror),
77d4d49bc1SJie Deng 	XLGMAC_STAT("rx_out_of_range_errors", rxoutofrangetype),
78d4d49bc1SJie Deng 	XLGMAC_STAT("rx_fifo_overflow_errors", rxfifooverflow),
79d4d49bc1SJie Deng 	XLGMAC_STAT("rx_watchdog_errors", rxwatchdogerror),
80d4d49bc1SJie Deng 	XLGMAC_STAT("rx_pause_frames", rxpauseframes),
81d4d49bc1SJie Deng 
82d4d49bc1SJie Deng 	/* Extra counters */
83d4d49bc1SJie Deng 	XLGMAC_STAT("tx_tso_packets", tx_tso_packets),
84d4d49bc1SJie Deng 	XLGMAC_STAT("rx_split_header_packets", rx_split_header_packets),
85d4d49bc1SJie Deng 	XLGMAC_STAT("tx_process_stopped", tx_process_stopped),
86d4d49bc1SJie Deng 	XLGMAC_STAT("rx_process_stopped", rx_process_stopped),
87d4d49bc1SJie Deng 	XLGMAC_STAT("tx_buffer_unavailable", tx_buffer_unavailable),
88d4d49bc1SJie Deng 	XLGMAC_STAT("rx_buffer_unavailable", rx_buffer_unavailable),
89d4d49bc1SJie Deng 	XLGMAC_STAT("fatal_bus_error", fatal_bus_error),
90d4d49bc1SJie Deng 	XLGMAC_STAT("tx_vlan_packets", tx_vlan_packets),
91d4d49bc1SJie Deng 	XLGMAC_STAT("rx_vlan_packets", rx_vlan_packets),
92d4d49bc1SJie Deng 	XLGMAC_STAT("napi_poll_isr", napi_poll_isr),
93d4d49bc1SJie Deng 	XLGMAC_STAT("napi_poll_txtimer", napi_poll_txtimer),
94d4d49bc1SJie Deng };
95d4d49bc1SJie Deng 
96d4d49bc1SJie Deng #define XLGMAC_STATS_COUNT	ARRAY_SIZE(xlgmac_gstring_stats)
97d4d49bc1SJie Deng 
xlgmac_ethtool_get_drvinfo(struct net_device * netdev,struct ethtool_drvinfo * drvinfo)98d4d49bc1SJie Deng static void xlgmac_ethtool_get_drvinfo(struct net_device *netdev,
99d4d49bc1SJie Deng 				       struct ethtool_drvinfo *drvinfo)
100d4d49bc1SJie Deng {
101d4d49bc1SJie Deng 	struct xlgmac_pdata *pdata = netdev_priv(netdev);
102d4d49bc1SJie Deng 	u32 ver = pdata->hw_feat.version;
103d4d49bc1SJie Deng 	u32 snpsver, devid, userver;
104d4d49bc1SJie Deng 
105*f029c781SWolfram Sang 	strscpy(drvinfo->driver, pdata->drv_name, sizeof(drvinfo->driver));
106*f029c781SWolfram Sang 	strscpy(drvinfo->version, pdata->drv_ver, sizeof(drvinfo->version));
107*f029c781SWolfram Sang 	strscpy(drvinfo->bus_info, dev_name(pdata->dev),
108d4d49bc1SJie Deng 		sizeof(drvinfo->bus_info));
109d4d49bc1SJie Deng 	/* S|SNPSVER: Synopsys-defined Version
110d4d49bc1SJie Deng 	 * D|DEVID: Indicates the Device family
111d4d49bc1SJie Deng 	 * U|USERVER: User-defined Version
112d4d49bc1SJie Deng 	 */
113d4d49bc1SJie Deng 	snpsver = XLGMAC_GET_REG_BITS(ver, MAC_VR_SNPSVER_POS,
114d4d49bc1SJie Deng 				      MAC_VR_SNPSVER_LEN);
115d4d49bc1SJie Deng 	devid = XLGMAC_GET_REG_BITS(ver, MAC_VR_DEVID_POS,
116d4d49bc1SJie Deng 				    MAC_VR_DEVID_LEN);
117d4d49bc1SJie Deng 	userver = XLGMAC_GET_REG_BITS(ver, MAC_VR_USERVER_POS,
118d4d49bc1SJie Deng 				      MAC_VR_USERVER_LEN);
119d4d49bc1SJie Deng 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
120d4d49bc1SJie Deng 		 "S.D.U: %x.%x.%x", snpsver, devid, userver);
121d4d49bc1SJie Deng }
122d4d49bc1SJie Deng 
xlgmac_ethtool_get_msglevel(struct net_device * netdev)123d4d49bc1SJie Deng static u32 xlgmac_ethtool_get_msglevel(struct net_device *netdev)
124d4d49bc1SJie Deng {
125d4d49bc1SJie Deng 	struct xlgmac_pdata *pdata = netdev_priv(netdev);
126d4d49bc1SJie Deng 
127d4d49bc1SJie Deng 	return pdata->msg_enable;
128d4d49bc1SJie Deng }
129d4d49bc1SJie Deng 
xlgmac_ethtool_set_msglevel(struct net_device * netdev,u32 msglevel)130d4d49bc1SJie Deng static void xlgmac_ethtool_set_msglevel(struct net_device *netdev,
131d4d49bc1SJie Deng 					u32 msglevel)
132d4d49bc1SJie Deng {
133d4d49bc1SJie Deng 	struct xlgmac_pdata *pdata = netdev_priv(netdev);
134d4d49bc1SJie Deng 
135d4d49bc1SJie Deng 	pdata->msg_enable = msglevel;
136d4d49bc1SJie Deng }
137d4d49bc1SJie Deng 
xlgmac_ethtool_get_channels(struct net_device * netdev,struct ethtool_channels * channel)138d4d49bc1SJie Deng static void xlgmac_ethtool_get_channels(struct net_device *netdev,
139d4d49bc1SJie Deng 					struct ethtool_channels *channel)
140d4d49bc1SJie Deng {
141d4d49bc1SJie Deng 	struct xlgmac_pdata *pdata = netdev_priv(netdev);
142d4d49bc1SJie Deng 
143d4d49bc1SJie Deng 	channel->max_rx = XLGMAC_MAX_DMA_CHANNELS;
144d4d49bc1SJie Deng 	channel->max_tx = XLGMAC_MAX_DMA_CHANNELS;
145d4d49bc1SJie Deng 	channel->rx_count = pdata->rx_q_count;
146d4d49bc1SJie Deng 	channel->tx_count = pdata->tx_q_count;
147d4d49bc1SJie Deng }
148d4d49bc1SJie Deng 
149f3ccfda1SYufeng Mo static int
xlgmac_ethtool_get_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)150f3ccfda1SYufeng Mo xlgmac_ethtool_get_coalesce(struct net_device *netdev,
151f3ccfda1SYufeng Mo 			    struct ethtool_coalesce *ec,
152f3ccfda1SYufeng Mo 			    struct kernel_ethtool_coalesce *kernel_coal,
153f3ccfda1SYufeng Mo 			    struct netlink_ext_ack *extack)
154d4d49bc1SJie Deng {
155d4d49bc1SJie Deng 	struct xlgmac_pdata *pdata = netdev_priv(netdev);
156d4d49bc1SJie Deng 
157d4d49bc1SJie Deng 	ec->rx_coalesce_usecs = pdata->rx_usecs;
158d4d49bc1SJie Deng 	ec->rx_max_coalesced_frames = pdata->rx_frames;
159d4d49bc1SJie Deng 	ec->tx_max_coalesced_frames = pdata->tx_frames;
160d4d49bc1SJie Deng 
161d4d49bc1SJie Deng 	return 0;
162d4d49bc1SJie Deng }
163d4d49bc1SJie Deng 
164f3ccfda1SYufeng Mo static int
xlgmac_ethtool_set_coalesce(struct net_device * netdev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)165f3ccfda1SYufeng Mo xlgmac_ethtool_set_coalesce(struct net_device *netdev,
166f3ccfda1SYufeng Mo 			    struct ethtool_coalesce *ec,
167f3ccfda1SYufeng Mo 			    struct kernel_ethtool_coalesce *kernel_coal,
168f3ccfda1SYufeng Mo 			    struct netlink_ext_ack *extack)
169d4d49bc1SJie Deng {
170d4d49bc1SJie Deng 	struct xlgmac_pdata *pdata = netdev_priv(netdev);
171d4d49bc1SJie Deng 	struct xlgmac_hw_ops *hw_ops = &pdata->hw_ops;
172d4d49bc1SJie Deng 	unsigned int rx_frames, rx_riwt, rx_usecs;
173d4d49bc1SJie Deng 	unsigned int tx_frames;
174d4d49bc1SJie Deng 
175d4d49bc1SJie Deng 	rx_usecs = ec->rx_coalesce_usecs;
176d4d49bc1SJie Deng 	rx_riwt = hw_ops->usec_to_riwt(pdata, rx_usecs);
177d4d49bc1SJie Deng 	rx_frames = ec->rx_max_coalesced_frames;
178d4d49bc1SJie Deng 	tx_frames = ec->tx_max_coalesced_frames;
179d4d49bc1SJie Deng 
180d4d49bc1SJie Deng 	if ((rx_riwt > XLGMAC_MAX_DMA_RIWT) ||
181d4d49bc1SJie Deng 	    (rx_riwt < XLGMAC_MIN_DMA_RIWT) ||
182d4d49bc1SJie Deng 	    (rx_frames > pdata->rx_desc_count))
183d4d49bc1SJie Deng 		return -EINVAL;
184d4d49bc1SJie Deng 
185d4d49bc1SJie Deng 	if (tx_frames > pdata->tx_desc_count)
186d4d49bc1SJie Deng 		return -EINVAL;
187d4d49bc1SJie Deng 
188d4d49bc1SJie Deng 	pdata->rx_riwt = rx_riwt;
189d4d49bc1SJie Deng 	pdata->rx_usecs = rx_usecs;
190d4d49bc1SJie Deng 	pdata->rx_frames = rx_frames;
191d4d49bc1SJie Deng 	hw_ops->config_rx_coalesce(pdata);
192d4d49bc1SJie Deng 
193d4d49bc1SJie Deng 	pdata->tx_frames = tx_frames;
194d4d49bc1SJie Deng 	hw_ops->config_tx_coalesce(pdata);
195d4d49bc1SJie Deng 
196d4d49bc1SJie Deng 	return 0;
197d4d49bc1SJie Deng }
198d4d49bc1SJie Deng 
xlgmac_ethtool_get_strings(struct net_device * netdev,u32 stringset,u8 * data)199d4d49bc1SJie Deng static void xlgmac_ethtool_get_strings(struct net_device *netdev,
200d4d49bc1SJie Deng 				       u32 stringset, u8 *data)
201d4d49bc1SJie Deng {
202d4d49bc1SJie Deng 	int i;
203d4d49bc1SJie Deng 
204d4d49bc1SJie Deng 	switch (stringset) {
205d4d49bc1SJie Deng 	case ETH_SS_STATS:
206d4d49bc1SJie Deng 		for (i = 0; i < XLGMAC_STATS_COUNT; i++) {
207d4d49bc1SJie Deng 			memcpy(data, xlgmac_gstring_stats[i].stat_string,
208d4d49bc1SJie Deng 			       ETH_GSTRING_LEN);
209d4d49bc1SJie Deng 			data += ETH_GSTRING_LEN;
210d4d49bc1SJie Deng 		}
211d4d49bc1SJie Deng 		break;
212d4d49bc1SJie Deng 	default:
213d4d49bc1SJie Deng 		WARN_ON(1);
214d4d49bc1SJie Deng 		break;
215d4d49bc1SJie Deng 	}
216d4d49bc1SJie Deng }
217d4d49bc1SJie Deng 
xlgmac_ethtool_get_sset_count(struct net_device * netdev,int stringset)218d4d49bc1SJie Deng static int xlgmac_ethtool_get_sset_count(struct net_device *netdev,
219d4d49bc1SJie Deng 					 int stringset)
220d4d49bc1SJie Deng {
221d4d49bc1SJie Deng 	int ret;
222d4d49bc1SJie Deng 
223d4d49bc1SJie Deng 	switch (stringset) {
224d4d49bc1SJie Deng 	case ETH_SS_STATS:
225d4d49bc1SJie Deng 		ret = XLGMAC_STATS_COUNT;
226d4d49bc1SJie Deng 		break;
227d4d49bc1SJie Deng 
228d4d49bc1SJie Deng 	default:
229d4d49bc1SJie Deng 		ret = -EOPNOTSUPP;
230d4d49bc1SJie Deng 	}
231d4d49bc1SJie Deng 
232d4d49bc1SJie Deng 	return ret;
233d4d49bc1SJie Deng }
234d4d49bc1SJie Deng 
xlgmac_ethtool_get_ethtool_stats(struct net_device * netdev,struct ethtool_stats * stats,u64 * data)235d4d49bc1SJie Deng static void xlgmac_ethtool_get_ethtool_stats(struct net_device *netdev,
236d4d49bc1SJie Deng 					     struct ethtool_stats *stats,
237d4d49bc1SJie Deng 					     u64 *data)
238d4d49bc1SJie Deng {
239d4d49bc1SJie Deng 	struct xlgmac_pdata *pdata = netdev_priv(netdev);
240d4d49bc1SJie Deng 	u8 *stat;
241d4d49bc1SJie Deng 	int i;
242d4d49bc1SJie Deng 
243d4d49bc1SJie Deng 	pdata->hw_ops.read_mmc_stats(pdata);
244d4d49bc1SJie Deng 	for (i = 0; i < XLGMAC_STATS_COUNT; i++) {
245d4d49bc1SJie Deng 		stat = (u8 *)pdata + xlgmac_gstring_stats[i].stat_offset;
246d4d49bc1SJie Deng 		*data++ = *(u64 *)stat;
247d4d49bc1SJie Deng 	}
248d4d49bc1SJie Deng }
249d4d49bc1SJie Deng 
250d4d49bc1SJie Deng static const struct ethtool_ops xlgmac_ethtool_ops = {
251a5a8758fSJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
252a5a8758fSJakub Kicinski 				     ETHTOOL_COALESCE_MAX_FRAMES,
253d4d49bc1SJie Deng 	.get_drvinfo = xlgmac_ethtool_get_drvinfo,
254d4d49bc1SJie Deng 	.get_link = ethtool_op_get_link,
255d4d49bc1SJie Deng 	.get_msglevel = xlgmac_ethtool_get_msglevel,
256d4d49bc1SJie Deng 	.set_msglevel = xlgmac_ethtool_set_msglevel,
257d4d49bc1SJie Deng 	.get_channels = xlgmac_ethtool_get_channels,
258d4d49bc1SJie Deng 	.get_coalesce = xlgmac_ethtool_get_coalesce,
259d4d49bc1SJie Deng 	.set_coalesce = xlgmac_ethtool_set_coalesce,
260d4d49bc1SJie Deng 	.get_strings = xlgmac_ethtool_get_strings,
261d4d49bc1SJie Deng 	.get_sset_count = xlgmac_ethtool_get_sset_count,
262d4d49bc1SJie Deng 	.get_ethtool_stats = xlgmac_ethtool_get_ethtool_stats,
263d4d49bc1SJie Deng };
264d4d49bc1SJie Deng 
xlgmac_get_ethtool_ops(void)265d4d49bc1SJie Deng const struct ethtool_ops *xlgmac_get_ethtool_ops(void)
266d4d49bc1SJie Deng {
267d4d49bc1SJie Deng 	return &xlgmac_ethtool_ops;
268d4d49bc1SJie Deng }
269