xref: /linux/drivers/net/ethernet/ti/icssg/icssg_ethtool.c (revision 550ee90ac61c1f0cd987c68a9ac6c4c9833925d7)
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_RX_SOFTWARE |
123 		SOF_TIMESTAMPING_SOFTWARE |
124 		SOF_TIMESTAMPING_RAW_HARDWARE;
125 
126 	info->phc_index = icss_iep_get_ptp_clock_idx(emac->iep);
127 	info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
128 	info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) | BIT(HWTSTAMP_FILTER_ALL);
129 
130 	return 0;
131 }
132 
133 static int emac_set_channels(struct net_device *ndev,
134 			     struct ethtool_channels *ch)
135 {
136 	struct prueth_emac *emac = netdev_priv(ndev);
137 
138 	/* Check if interface is up. Can change the num queues when
139 	 * the interface is down.
140 	 */
141 	if (netif_running(emac->ndev))
142 		return -EBUSY;
143 
144 	emac->tx_ch_num = ch->tx_count;
145 
146 	if (emac->is_sr1)
147 		emac->tx_ch_num++;
148 
149 	return 0;
150 }
151 
152 static void emac_get_channels(struct net_device *ndev,
153 			      struct ethtool_channels *ch)
154 {
155 	struct prueth_emac *emac = netdev_priv(ndev);
156 
157 	ch->max_rx = 1;
158 	ch->max_tx = PRUETH_MAX_TX_QUEUES;
159 
160 	/* Disable multiple TX channels due to timeouts
161 	 * when using more than one queue */
162 	if (emac->is_sr1)
163 		ch->max_tx = 1;
164 
165 	ch->rx_count = 1;
166 	ch->tx_count = emac->tx_ch_num;
167 
168 	if (emac->is_sr1)
169 		ch->tx_count--;
170 }
171 
172 static const struct ethtool_rmon_hist_range emac_rmon_ranges[] = {
173 	{    0,   64},
174 	{   65,  128},
175 	{  129,  256},
176 	{  257,  512},
177 	{  513, PRUETH_MAX_PKT_SIZE},
178 	{}
179 };
180 
181 static void emac_get_rmon_stats(struct net_device *ndev,
182 				struct ethtool_rmon_stats *rmon_stats,
183 				const struct ethtool_rmon_hist_range **ranges)
184 {
185 	struct prueth_emac *emac = netdev_priv(ndev);
186 
187 	*ranges = emac_rmon_ranges;
188 
189 	rmon_stats->undersize_pkts = emac_get_stat_by_name(emac, "rx_bucket1_frames") -
190 				     emac_get_stat_by_name(emac, "rx_64B_frames");
191 
192 	rmon_stats->hist[0] = emac_get_stat_by_name(emac, "rx_bucket1_frames");
193 	rmon_stats->hist[1] = emac_get_stat_by_name(emac, "rx_bucket2_frames");
194 	rmon_stats->hist[2] = emac_get_stat_by_name(emac, "rx_bucket3_frames");
195 	rmon_stats->hist[3] = emac_get_stat_by_name(emac, "rx_bucket4_frames");
196 	rmon_stats->hist[4] = emac_get_stat_by_name(emac, "rx_bucket5_frames");
197 
198 	rmon_stats->hist_tx[0] = emac_get_stat_by_name(emac, "tx_bucket1_frames");
199 	rmon_stats->hist_tx[1] = emac_get_stat_by_name(emac, "tx_bucket2_frames");
200 	rmon_stats->hist_tx[2] = emac_get_stat_by_name(emac, "tx_bucket3_frames");
201 	rmon_stats->hist_tx[3] = emac_get_stat_by_name(emac, "tx_bucket4_frames");
202 	rmon_stats->hist_tx[4] = emac_get_stat_by_name(emac, "tx_bucket5_frames");
203 }
204 
205 static int emac_get_coalesce(struct net_device *ndev,
206 			     struct ethtool_coalesce *coal,
207 			     struct kernel_ethtool_coalesce *kernel_coal,
208 			     struct netlink_ext_ack *extack)
209 {
210 	struct prueth_emac *emac = netdev_priv(ndev);
211 	struct prueth_tx_chn *tx_chn;
212 
213 	tx_chn = &emac->tx_chns[0];
214 
215 	coal->rx_coalesce_usecs = emac->rx_pace_timeout_ns / 1000;
216 	coal->tx_coalesce_usecs = tx_chn->tx_pace_timeout_ns / 1000;
217 
218 	return 0;
219 }
220 
221 static int emac_get_per_queue_coalesce(struct net_device *ndev, u32 queue,
222 				       struct ethtool_coalesce *coal)
223 {
224 	struct prueth_emac *emac = netdev_priv(ndev);
225 	struct prueth_tx_chn *tx_chn;
226 
227 	if (queue >= PRUETH_MAX_TX_QUEUES)
228 		return -EINVAL;
229 
230 	tx_chn = &emac->tx_chns[queue];
231 
232 	coal->tx_coalesce_usecs = tx_chn->tx_pace_timeout_ns / 1000;
233 
234 	return 0;
235 }
236 
237 static int emac_set_coalesce(struct net_device *ndev,
238 			     struct ethtool_coalesce *coal,
239 			     struct kernel_ethtool_coalesce *kernel_coal,
240 			     struct netlink_ext_ack *extack)
241 {
242 	struct prueth_emac *emac = netdev_priv(ndev);
243 	struct prueth *prueth = emac->prueth;
244 	struct prueth_tx_chn *tx_chn;
245 
246 	tx_chn = &emac->tx_chns[0];
247 
248 	if (coal->rx_coalesce_usecs &&
249 	    coal->rx_coalesce_usecs < ICSSG_MIN_COALESCE_USECS) {
250 		dev_info(prueth->dev, "defaulting to min value of %dus for rx-usecs\n",
251 			 ICSSG_MIN_COALESCE_USECS);
252 		coal->rx_coalesce_usecs = ICSSG_MIN_COALESCE_USECS;
253 	}
254 
255 	if (coal->tx_coalesce_usecs &&
256 	    coal->tx_coalesce_usecs < ICSSG_MIN_COALESCE_USECS) {
257 		dev_info(prueth->dev, "defaulting to min value of %dus for tx-usecs\n",
258 			 ICSSG_MIN_COALESCE_USECS);
259 		coal->tx_coalesce_usecs = ICSSG_MIN_COALESCE_USECS;
260 	}
261 
262 	emac->rx_pace_timeout_ns = coal->rx_coalesce_usecs * 1000;
263 	tx_chn->tx_pace_timeout_ns = coal->tx_coalesce_usecs * 1000;
264 
265 	return 0;
266 }
267 
268 static int emac_set_per_queue_coalesce(struct net_device *ndev, u32 queue,
269 				       struct ethtool_coalesce *coal)
270 {
271 	struct prueth_emac *emac = netdev_priv(ndev);
272 	struct prueth *prueth = emac->prueth;
273 	struct prueth_tx_chn *tx_chn;
274 
275 	if (queue >= PRUETH_MAX_TX_QUEUES)
276 		return -EINVAL;
277 
278 	tx_chn = &emac->tx_chns[queue];
279 
280 	if (coal->tx_coalesce_usecs &&
281 	    coal->tx_coalesce_usecs < ICSSG_MIN_COALESCE_USECS) {
282 		dev_info(prueth->dev, "defaulting to min value of %dus for tx-usecs for tx-%u\n",
283 			 ICSSG_MIN_COALESCE_USECS, queue);
284 		coal->tx_coalesce_usecs = ICSSG_MIN_COALESCE_USECS;
285 	}
286 
287 	tx_chn->tx_pace_timeout_ns = coal->tx_coalesce_usecs * 1000;
288 
289 	return 0;
290 }
291 
292 const struct ethtool_ops icssg_ethtool_ops = {
293 	.get_drvinfo = emac_get_drvinfo,
294 	.get_msglevel = emac_get_msglevel,
295 	.set_msglevel = emac_set_msglevel,
296 	.get_sset_count = emac_get_sset_count,
297 	.get_ethtool_stats = emac_get_ethtool_stats,
298 	.get_strings = emac_get_strings,
299 	.get_ts_info = emac_get_ts_info,
300 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
301 				     ETHTOOL_COALESCE_TX_USECS,
302 	.get_coalesce = emac_get_coalesce,
303 	.set_coalesce = emac_set_coalesce,
304 	.get_per_queue_coalesce = emac_get_per_queue_coalesce,
305 	.set_per_queue_coalesce = emac_set_per_queue_coalesce,
306 	.get_channels = emac_get_channels,
307 	.set_channels = emac_set_channels,
308 	.get_link_ksettings = emac_get_link_ksettings,
309 	.set_link_ksettings = emac_set_link_ksettings,
310 	.get_link = ethtool_op_get_link,
311 	.get_eee = emac_get_eee,
312 	.set_eee = emac_set_eee,
313 	.nway_reset = emac_nway_reset,
314 	.get_rmon_stats = emac_get_rmon_stats,
315 };
316 EXPORT_SYMBOL_GPL(icssg_ethtool_ops);
317