xref: /linux/drivers/net/ethernet/freescale/ucc_geth_ethtool.c (revision 95298d63c67673c654c08952672d016212b26054)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2007 Freescale Semiconductor, Inc. All rights reserved.
4  *
5  * Description: QE UCC Gigabit Ethernet Ethtool API Set
6  *
7  * Author: Li Yang <leoli@freescale.com>
8  *
9  * Limitation:
10  * Can only get/set settings of the first queue.
11  * Need to re-open the interface manually after changing some parameters.
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/stddef.h>
17 #include <linux/interrupt.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/skbuff.h>
21 #include <linux/spinlock.h>
22 #include <linux/mm.h>
23 #include <linux/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/ethtool.h>
26 #include <linux/mii.h>
27 #include <linux/phy.h>
28 
29 #include <asm/io.h>
30 #include <asm/irq.h>
31 #include <linux/uaccess.h>
32 #include <asm/types.h>
33 
34 #include "ucc_geth.h"
35 
36 static const char hw_stat_gstrings[][ETH_GSTRING_LEN] = {
37 	"tx-64-frames",
38 	"tx-65-127-frames",
39 	"tx-128-255-frames",
40 	"rx-64-frames",
41 	"rx-65-127-frames",
42 	"rx-128-255-frames",
43 	"tx-bytes-ok",
44 	"tx-pause-frames",
45 	"tx-multicast-frames",
46 	"tx-broadcast-frames",
47 	"rx-frames",
48 	"rx-bytes-ok",
49 	"rx-bytes-all",
50 	"rx-multicast-frames",
51 	"rx-broadcast-frames",
52 	"stats-counter-carry",
53 	"stats-counter-mask",
54 	"rx-dropped-frames",
55 };
56 
57 static const char tx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
58 	"tx-single-collision",
59 	"tx-multiple-collision",
60 	"tx-late-collision",
61 	"tx-aborted-frames",
62 	"tx-lost-frames",
63 	"tx-carrier-sense-errors",
64 	"tx-frames-ok",
65 	"tx-excessive-differ-frames",
66 	"tx-256-511-frames",
67 	"tx-512-1023-frames",
68 	"tx-1024-1518-frames",
69 	"tx-jumbo-frames",
70 };
71 
72 static const char rx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
73 	"rx-crc-errors",
74 	"rx-alignment-errors",
75 	"rx-in-range-length-errors",
76 	"rx-out-of-range-length-errors",
77 	"rx-too-long-frames",
78 	"rx-runt",
79 	"rx-very-long-event",
80 	"rx-symbol-errors",
81 	"rx-busy-drop-frames",
82 	"reserved",
83 	"reserved",
84 	"rx-mismatch-drop-frames",
85 	"rx-small-than-64",
86 	"rx-256-511-frames",
87 	"rx-512-1023-frames",
88 	"rx-1024-1518-frames",
89 	"rx-jumbo-frames",
90 	"rx-mac-error-loss",
91 	"rx-pause-frames",
92 	"reserved",
93 	"rx-vlan-removed",
94 	"rx-vlan-replaced",
95 	"rx-vlan-inserted",
96 	"rx-ip-checksum-errors",
97 };
98 
99 #define UEC_HW_STATS_LEN ARRAY_SIZE(hw_stat_gstrings)
100 #define UEC_TX_FW_STATS_LEN ARRAY_SIZE(tx_fw_stat_gstrings)
101 #define UEC_RX_FW_STATS_LEN ARRAY_SIZE(rx_fw_stat_gstrings)
102 
103 static int
104 uec_get_ksettings(struct net_device *netdev, struct ethtool_link_ksettings *cmd)
105 {
106 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
107 	struct phy_device *phydev = ugeth->phydev;
108 
109 	if (!phydev)
110 		return -ENODEV;
111 
112 	phy_ethtool_ksettings_get(phydev, cmd);
113 
114 	return 0;
115 }
116 
117 static int
118 uec_set_ksettings(struct net_device *netdev,
119 		  const struct ethtool_link_ksettings *cmd)
120 {
121 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
122 	struct phy_device *phydev = ugeth->phydev;
123 
124 	if (!phydev)
125 		return -ENODEV;
126 
127 	return phy_ethtool_ksettings_set(phydev, cmd);
128 }
129 
130 static void
131 uec_get_pauseparam(struct net_device *netdev,
132                      struct ethtool_pauseparam *pause)
133 {
134 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
135 
136 	pause->autoneg = ugeth->phydev->autoneg;
137 
138 	if (ugeth->ug_info->receiveFlowControl)
139 		pause->rx_pause = 1;
140 	if (ugeth->ug_info->transmitFlowControl)
141 		pause->tx_pause = 1;
142 }
143 
144 static int
145 uec_set_pauseparam(struct net_device *netdev,
146                      struct ethtool_pauseparam *pause)
147 {
148 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
149 	int ret = 0;
150 
151 	ugeth->ug_info->receiveFlowControl = pause->rx_pause;
152 	ugeth->ug_info->transmitFlowControl = pause->tx_pause;
153 
154 	if (ugeth->phydev->autoneg) {
155 		if (netif_running(netdev)) {
156 			/* FIXME: automatically restart */
157 			netdev_info(netdev, "Please re-open the interface\n");
158 		}
159 	} else {
160 		struct ucc_geth_info *ug_info = ugeth->ug_info;
161 
162 		ret = init_flow_control_params(ug_info->aufc,
163 					ug_info->receiveFlowControl,
164 					ug_info->transmitFlowControl,
165 					ug_info->pausePeriod,
166 					ug_info->extensionField,
167 					&ugeth->uccf->uf_regs->upsmr,
168 					&ugeth->ug_regs->uempr,
169 					&ugeth->ug_regs->maccfg1);
170 	}
171 
172 	return ret;
173 }
174 
175 static uint32_t
176 uec_get_msglevel(struct net_device *netdev)
177 {
178 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
179 	return ugeth->msg_enable;
180 }
181 
182 static void
183 uec_set_msglevel(struct net_device *netdev, uint32_t data)
184 {
185 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
186 	ugeth->msg_enable = data;
187 }
188 
189 static int
190 uec_get_regs_len(struct net_device *netdev)
191 {
192 	return sizeof(struct ucc_geth);
193 }
194 
195 static void
196 uec_get_regs(struct net_device *netdev,
197                struct ethtool_regs *regs, void *p)
198 {
199 	int i;
200 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
201 	u32 __iomem *ug_regs = (u32 __iomem *)ugeth->ug_regs;
202 	u32 *buff = p;
203 
204 	for (i = 0; i < sizeof(struct ucc_geth) / sizeof(u32); i++)
205 		buff[i] = in_be32(&ug_regs[i]);
206 }
207 
208 static void
209 uec_get_ringparam(struct net_device *netdev,
210                     struct ethtool_ringparam *ring)
211 {
212 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
213 	struct ucc_geth_info *ug_info = ugeth->ug_info;
214 	int queue = 0;
215 
216 	ring->rx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
217 	ring->rx_mini_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
218 	ring->rx_jumbo_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
219 	ring->tx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
220 
221 	ring->rx_pending = ug_info->bdRingLenRx[queue];
222 	ring->rx_mini_pending = ug_info->bdRingLenRx[queue];
223 	ring->rx_jumbo_pending = ug_info->bdRingLenRx[queue];
224 	ring->tx_pending = ug_info->bdRingLenTx[queue];
225 }
226 
227 static int
228 uec_set_ringparam(struct net_device *netdev,
229                     struct ethtool_ringparam *ring)
230 {
231 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
232 	struct ucc_geth_info *ug_info = ugeth->ug_info;
233 	int queue = 0, ret = 0;
234 
235 	if (ring->rx_pending < UCC_GETH_RX_BD_RING_SIZE_MIN) {
236 		netdev_info(netdev, "RxBD ring size must be no smaller than %d\n",
237 			    UCC_GETH_RX_BD_RING_SIZE_MIN);
238 		return -EINVAL;
239 	}
240 	if (ring->rx_pending % UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT) {
241 		netdev_info(netdev, "RxBD ring size must be multiple of %d\n",
242 			    UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT);
243 		return -EINVAL;
244 	}
245 	if (ring->tx_pending < UCC_GETH_TX_BD_RING_SIZE_MIN) {
246 		netdev_info(netdev, "TxBD ring size must be no smaller than %d\n",
247 			    UCC_GETH_TX_BD_RING_SIZE_MIN);
248 		return -EINVAL;
249 	}
250 
251 	if (netif_running(netdev))
252 		return -EBUSY;
253 
254 	ug_info->bdRingLenRx[queue] = ring->rx_pending;
255 	ug_info->bdRingLenTx[queue] = ring->tx_pending;
256 
257 	return ret;
258 }
259 
260 static int uec_get_sset_count(struct net_device *netdev, int sset)
261 {
262 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
263 	u32 stats_mode = ugeth->ug_info->statisticsMode;
264 	int len = 0;
265 
266 	switch (sset) {
267 	case ETH_SS_STATS:
268 		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
269 			len += UEC_HW_STATS_LEN;
270 		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
271 			len += UEC_TX_FW_STATS_LEN;
272 		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
273 			len += UEC_RX_FW_STATS_LEN;
274 
275 		return len;
276 
277 	default:
278 		return -EOPNOTSUPP;
279 	}
280 }
281 
282 static void uec_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
283 {
284 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
285 	u32 stats_mode = ugeth->ug_info->statisticsMode;
286 
287 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
288 		memcpy(buf, hw_stat_gstrings, UEC_HW_STATS_LEN *
289 			       	ETH_GSTRING_LEN);
290 		buf += UEC_HW_STATS_LEN * ETH_GSTRING_LEN;
291 	}
292 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
293 		memcpy(buf, tx_fw_stat_gstrings, UEC_TX_FW_STATS_LEN *
294 			       	ETH_GSTRING_LEN);
295 		buf += UEC_TX_FW_STATS_LEN * ETH_GSTRING_LEN;
296 	}
297 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
298 		memcpy(buf, rx_fw_stat_gstrings, UEC_RX_FW_STATS_LEN *
299 			       	ETH_GSTRING_LEN);
300 }
301 
302 static void uec_get_ethtool_stats(struct net_device *netdev,
303 		struct ethtool_stats *stats, uint64_t *data)
304 {
305 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
306 	u32 stats_mode = ugeth->ug_info->statisticsMode;
307 	u32 __iomem *base;
308 	int i, j = 0;
309 
310 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
311 		if (ugeth->ug_regs)
312 			base = (u32 __iomem *)&ugeth->ug_regs->tx64;
313 		else
314 			base = NULL;
315 
316 		for (i = 0; i < UEC_HW_STATS_LEN; i++)
317 			data[j++] = base ? in_be32(&base[i]) : 0;
318 	}
319 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
320 		base = (u32 __iomem *)ugeth->p_tx_fw_statistics_pram;
321 		for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
322 			data[j++] = base ? in_be32(&base[i]) : 0;
323 	}
324 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
325 		base = (u32 __iomem *)ugeth->p_rx_fw_statistics_pram;
326 		for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
327 			data[j++] = base ? in_be32(&base[i]) : 0;
328 	}
329 }
330 
331 /* Report driver information */
332 static void
333 uec_get_drvinfo(struct net_device *netdev,
334                        struct ethtool_drvinfo *drvinfo)
335 {
336 	strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
337 	strlcpy(drvinfo->bus_info, "QUICC ENGINE", sizeof(drvinfo->bus_info));
338 }
339 
340 #ifdef CONFIG_PM
341 
342 static void uec_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
343 {
344 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
345 	struct phy_device *phydev = ugeth->phydev;
346 
347 	if (phydev && phydev->irq)
348 		wol->supported |= WAKE_PHY;
349 	if (qe_alive_during_sleep())
350 		wol->supported |= WAKE_MAGIC;
351 
352 	wol->wolopts = ugeth->wol_en;
353 }
354 
355 static int uec_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
356 {
357 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
358 	struct phy_device *phydev = ugeth->phydev;
359 
360 	if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
361 		return -EINVAL;
362 	else if (wol->wolopts & WAKE_PHY && (!phydev || !phydev->irq))
363 		return -EINVAL;
364 	else if (wol->wolopts & WAKE_MAGIC && !qe_alive_during_sleep())
365 		return -EINVAL;
366 
367 	ugeth->wol_en = wol->wolopts;
368 	device_set_wakeup_enable(&netdev->dev, ugeth->wol_en);
369 
370 	return 0;
371 }
372 
373 #else
374 #define uec_get_wol NULL
375 #define uec_set_wol NULL
376 #endif /* CONFIG_PM */
377 
378 static const struct ethtool_ops uec_ethtool_ops = {
379 	.get_drvinfo            = uec_get_drvinfo,
380 	.get_regs_len           = uec_get_regs_len,
381 	.get_regs               = uec_get_regs,
382 	.get_msglevel           = uec_get_msglevel,
383 	.set_msglevel           = uec_set_msglevel,
384 	.nway_reset             = phy_ethtool_nway_reset,
385 	.get_link               = ethtool_op_get_link,
386 	.get_ringparam          = uec_get_ringparam,
387 	.set_ringparam          = uec_set_ringparam,
388 	.get_pauseparam         = uec_get_pauseparam,
389 	.set_pauseparam         = uec_set_pauseparam,
390 	.get_sset_count		= uec_get_sset_count,
391 	.get_strings            = uec_get_strings,
392 	.get_ethtool_stats      = uec_get_ethtool_stats,
393 	.get_wol		= uec_get_wol,
394 	.set_wol		= uec_set_wol,
395 	.get_ts_info		= ethtool_op_get_ts_info,
396 	.get_link_ksettings	= uec_get_ksettings,
397 	.set_link_ksettings	= uec_set_ksettings,
398 };
399 
400 void uec_set_ethtool_ops(struct net_device *netdev)
401 {
402 	netdev->ethtool_ops = &uec_ethtool_ops;
403 }
404