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