xref: /linux/drivers/net/ethernet/ti/icssg/icssg_ethtool.c (revision c5dbb6aeefbda74d8b523f291a7ac081c4c00aca)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Texas Instruments ICSSG Ethernet driver
3  *
4  * Copyright (C) 2018-2022 Texas Instruments Incorporated - https://www.ti.com/
5  *
6  */
7 
8 #include "icssg_prueth.h"
9 #include "icssg_stats.h"
10 
11 static void emac_get_drvinfo(struct net_device *ndev,
12 			     struct ethtool_drvinfo *info)
13 {
14 	struct prueth_emac *emac = netdev_priv(ndev);
15 	struct prueth *prueth = emac->prueth;
16 
17 	strscpy(info->driver, dev_driver_string(prueth->dev),
18 		sizeof(info->driver));
19 	strscpy(info->bus_info, dev_name(prueth->dev), sizeof(info->bus_info));
20 }
21 
22 static u32 emac_get_msglevel(struct net_device *ndev)
23 {
24 	struct prueth_emac *emac = netdev_priv(ndev);
25 
26 	return emac->msg_enable;
27 }
28 
29 static void emac_set_msglevel(struct net_device *ndev, u32 value)
30 {
31 	struct prueth_emac *emac = netdev_priv(ndev);
32 
33 	emac->msg_enable = value;
34 }
35 
36 static int emac_get_link_ksettings(struct net_device *ndev,
37 				   struct ethtool_link_ksettings *ecmd)
38 {
39 	return phy_ethtool_get_link_ksettings(ndev, ecmd);
40 }
41 
42 static int emac_set_link_ksettings(struct net_device *ndev,
43 				   const struct ethtool_link_ksettings *ecmd)
44 {
45 	return phy_ethtool_set_link_ksettings(ndev, ecmd);
46 }
47 
48 static int emac_get_eee(struct net_device *ndev, struct ethtool_keee *edata)
49 {
50 	if (!ndev->phydev)
51 		return -EOPNOTSUPP;
52 
53 	return phy_ethtool_get_eee(ndev->phydev, edata);
54 }
55 
56 static int emac_set_eee(struct net_device *ndev, struct ethtool_keee *edata)
57 {
58 	if (!ndev->phydev)
59 		return -EOPNOTSUPP;
60 
61 	return phy_ethtool_set_eee(ndev->phydev, edata);
62 }
63 
64 static int emac_nway_reset(struct net_device *ndev)
65 {
66 	return phy_ethtool_nway_reset(ndev);
67 }
68 
69 static int emac_get_sset_count(struct net_device *ndev, int stringset)
70 {
71 	switch (stringset) {
72 	case ETH_SS_STATS:
73 		return ICSSG_NUM_ETHTOOL_STATS;
74 	default:
75 		return -EOPNOTSUPP;
76 	}
77 }
78 
79 static void emac_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
80 {
81 	u8 *p = data;
82 	int i;
83 
84 	switch (stringset) {
85 	case ETH_SS_STATS:
86 		for (i = 0; i < ARRAY_SIZE(icssg_all_miig_stats); i++)
87 			if (!icssg_all_miig_stats[i].standard_stats)
88 				ethtool_puts(&p, icssg_all_miig_stats[i].name);
89 		for (i = 0; i < ARRAY_SIZE(icssg_all_pa_stats); i++)
90 			ethtool_puts(&p, icssg_all_pa_stats[i].name);
91 		break;
92 	default:
93 		break;
94 	}
95 }
96 
97 static void emac_get_ethtool_stats(struct net_device *ndev,
98 				   struct ethtool_stats *stats, u64 *data)
99 {
100 	struct prueth_emac *emac = netdev_priv(ndev);
101 	int i;
102 
103 	emac_update_hardware_stats(emac);
104 
105 	for (i = 0; i < ARRAY_SIZE(icssg_all_miig_stats); i++)
106 		if (!icssg_all_miig_stats[i].standard_stats)
107 			*(data++) = emac->stats[i];
108 
109 	for (i = 0; i < ARRAY_SIZE(icssg_all_pa_stats); i++)
110 		*(data++) = emac->pa_stats[i];
111 }
112 
113 static int emac_get_ts_info(struct net_device *ndev,
114 			    struct kernel_ethtool_ts_info *info)
115 {
116 	struct prueth_emac *emac = netdev_priv(ndev);
117 
118 	info->so_timestamping =
119 		SOF_TIMESTAMPING_TX_HARDWARE |
120 		SOF_TIMESTAMPING_TX_SOFTWARE |
121 		SOF_TIMESTAMPING_RX_HARDWARE |
122 		SOF_TIMESTAMPING_RAW_HARDWARE;
123 
124 	info->phc_index = icss_iep_get_ptp_clock_idx(emac->iep);
125 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
126 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
127 
128 	return 0;
129 }
130 
131 static int emac_set_channels(struct net_device *ndev,
132 			     struct ethtool_channels *ch)
133 {
134 	struct prueth_emac *emac = netdev_priv(ndev);
135 
136 	/* Check if interface is up. Can change the num queues when
137 	 * the interface is down.
138 	 */
139 	if (netif_running(emac->ndev))
140 		return -EBUSY;
141 
142 	emac->tx_ch_num = ch->tx_count;
143 
144 	if (emac->is_sr1)
145 		emac->tx_ch_num++;
146 
147 	return 0;
148 }
149 
150 static void emac_get_channels(struct net_device *ndev,
151 			      struct ethtool_channels *ch)
152 {
153 	struct prueth_emac *emac = netdev_priv(ndev);
154 
155 	ch->max_rx = 1;
156 	ch->max_tx = PRUETH_MAX_TX_QUEUES;
157 
158 	/* Disable multiple TX channels due to timeouts
159 	 * when using more than one queue */
160 	if (emac->is_sr1)
161 		ch->max_tx = 1;
162 
163 	ch->rx_count = 1;
164 	ch->tx_count = emac->tx_ch_num;
165 
166 	if (emac->is_sr1)
167 		ch->tx_count--;
168 }
169 
170 static const struct ethtool_rmon_hist_range emac_rmon_ranges[] = {
171 	{    0,   64},
172 	{   65,  128},
173 	{  129,  256},
174 	{  257,  512},
175 	{  513, PRUETH_MAX_PKT_SIZE},
176 	{}
177 };
178 
179 static void emac_get_rmon_stats(struct net_device *ndev,
180 				struct ethtool_rmon_stats *rmon_stats,
181 				const struct ethtool_rmon_hist_range **ranges)
182 {
183 	struct prueth_emac *emac = netdev_priv(ndev);
184 
185 	*ranges = emac_rmon_ranges;
186 
187 	rmon_stats->undersize_pkts = emac_get_stat_by_name(emac, "rx_bucket1_frames") -
188 				     emac_get_stat_by_name(emac, "rx_64B_frames");
189 
190 	rmon_stats->hist[0] = emac_get_stat_by_name(emac, "rx_bucket1_frames");
191 	rmon_stats->hist[1] = emac_get_stat_by_name(emac, "rx_bucket2_frames");
192 	rmon_stats->hist[2] = emac_get_stat_by_name(emac, "rx_bucket3_frames");
193 	rmon_stats->hist[3] = emac_get_stat_by_name(emac, "rx_bucket4_frames");
194 	rmon_stats->hist[4] = emac_get_stat_by_name(emac, "rx_bucket5_frames");
195 
196 	rmon_stats->hist_tx[0] = emac_get_stat_by_name(emac, "tx_bucket1_frames");
197 	rmon_stats->hist_tx[1] = emac_get_stat_by_name(emac, "tx_bucket2_frames");
198 	rmon_stats->hist_tx[2] = emac_get_stat_by_name(emac, "tx_bucket3_frames");
199 	rmon_stats->hist_tx[3] = emac_get_stat_by_name(emac, "tx_bucket4_frames");
200 	rmon_stats->hist_tx[4] = emac_get_stat_by_name(emac, "tx_bucket5_frames");
201 }
202 
203 static int emac_get_coalesce(struct net_device *ndev,
204 			     struct ethtool_coalesce *coal,
205 			     struct kernel_ethtool_coalesce *kernel_coal,
206 			     struct netlink_ext_ack *extack)
207 {
208 	struct prueth_emac *emac = netdev_priv(ndev);
209 	struct prueth_tx_chn *tx_chn;
210 
211 	tx_chn = &emac->tx_chns[0];
212 
213 	coal->rx_coalesce_usecs = emac->rx_pace_timeout_ns / 1000;
214 	coal->tx_coalesce_usecs = tx_chn->tx_pace_timeout_ns / 1000;
215 
216 	return 0;
217 }
218 
219 static int emac_get_per_queue_coalesce(struct net_device *ndev, u32 queue,
220 				       struct ethtool_coalesce *coal)
221 {
222 	struct prueth_emac *emac = netdev_priv(ndev);
223 	struct prueth_tx_chn *tx_chn;
224 
225 	if (queue >= PRUETH_MAX_TX_QUEUES)
226 		return -EINVAL;
227 
228 	tx_chn = &emac->tx_chns[queue];
229 
230 	coal->tx_coalesce_usecs = tx_chn->tx_pace_timeout_ns / 1000;
231 
232 	return 0;
233 }
234 
235 static int emac_set_coalesce(struct net_device *ndev,
236 			     struct ethtool_coalesce *coal,
237 			     struct kernel_ethtool_coalesce *kernel_coal,
238 			     struct netlink_ext_ack *extack)
239 {
240 	struct prueth_emac *emac = netdev_priv(ndev);
241 	struct prueth *prueth = emac->prueth;
242 	struct prueth_tx_chn *tx_chn;
243 
244 	tx_chn = &emac->tx_chns[0];
245 
246 	if (coal->rx_coalesce_usecs &&
247 	    coal->rx_coalesce_usecs < ICSSG_MIN_COALESCE_USECS) {
248 		dev_info(prueth->dev, "defaulting to min value of %dus for rx-usecs\n",
249 			 ICSSG_MIN_COALESCE_USECS);
250 		coal->rx_coalesce_usecs = ICSSG_MIN_COALESCE_USECS;
251 	}
252 
253 	if (coal->tx_coalesce_usecs &&
254 	    coal->tx_coalesce_usecs < ICSSG_MIN_COALESCE_USECS) {
255 		dev_info(prueth->dev, "defaulting to min value of %dus for tx-usecs\n",
256 			 ICSSG_MIN_COALESCE_USECS);
257 		coal->tx_coalesce_usecs = ICSSG_MIN_COALESCE_USECS;
258 	}
259 
260 	emac->rx_pace_timeout_ns = coal->rx_coalesce_usecs * 1000;
261 	tx_chn->tx_pace_timeout_ns = coal->tx_coalesce_usecs * 1000;
262 
263 	return 0;
264 }
265 
266 static int emac_set_per_queue_coalesce(struct net_device *ndev, u32 queue,
267 				       struct ethtool_coalesce *coal)
268 {
269 	struct prueth_emac *emac = netdev_priv(ndev);
270 	struct prueth *prueth = emac->prueth;
271 	struct prueth_tx_chn *tx_chn;
272 
273 	if (queue >= PRUETH_MAX_TX_QUEUES)
274 		return -EINVAL;
275 
276 	tx_chn = &emac->tx_chns[queue];
277 
278 	if (coal->tx_coalesce_usecs &&
279 	    coal->tx_coalesce_usecs < ICSSG_MIN_COALESCE_USECS) {
280 		dev_info(prueth->dev, "defaulting to min value of %dus for tx-usecs for tx-%u\n",
281 			 ICSSG_MIN_COALESCE_USECS, queue);
282 		coal->tx_coalesce_usecs = ICSSG_MIN_COALESCE_USECS;
283 	}
284 
285 	tx_chn->tx_pace_timeout_ns = coal->tx_coalesce_usecs * 1000;
286 
287 	return 0;
288 }
289 
290 const struct ethtool_ops icssg_ethtool_ops = {
291 	.get_drvinfo = emac_get_drvinfo,
292 	.get_msglevel = emac_get_msglevel,
293 	.set_msglevel = emac_set_msglevel,
294 	.get_sset_count = emac_get_sset_count,
295 	.get_ethtool_stats = emac_get_ethtool_stats,
296 	.get_strings = emac_get_strings,
297 	.get_ts_info = emac_get_ts_info,
298 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
299 				     ETHTOOL_COALESCE_TX_USECS,
300 	.get_coalesce = emac_get_coalesce,
301 	.set_coalesce = emac_set_coalesce,
302 	.get_per_queue_coalesce = emac_get_per_queue_coalesce,
303 	.set_per_queue_coalesce = emac_set_per_queue_coalesce,
304 	.get_channels = emac_get_channels,
305 	.set_channels = emac_set_channels,
306 	.get_link_ksettings = emac_get_link_ksettings,
307 	.set_link_ksettings = emac_set_link_ksettings,
308 	.get_link = ethtool_op_get_link,
309 	.get_eee = emac_get_eee,
310 	.set_eee = emac_set_eee,
311 	.nway_reset = emac_nway_reset,
312 	.get_rmon_stats = emac_get_rmon_stats,
313 };
314 EXPORT_SYMBOL_GPL(icssg_ethtool_ops);
315