xref: /linux/drivers/net/ethernet/freescale/ucc_geth_ethtool.c (revision e814f3fd16acfb7f9966773953de8f740a1e3202)
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 
108 	return phylink_ethtool_ksettings_get(ugeth->phylink, cmd);
109 }
110 
111 static int
112 uec_set_ksettings(struct net_device *netdev,
113 		  const struct ethtool_link_ksettings *cmd)
114 {
115 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
116 
117 	return phylink_ethtool_ksettings_set(ugeth->phylink, cmd);
118 }
119 
120 static void
121 uec_get_pauseparam(struct net_device *netdev,
122                      struct ethtool_pauseparam *pause)
123 {
124 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
125 
126 	return phylink_ethtool_get_pauseparam(ugeth->phylink, pause);
127 }
128 
129 static int
130 uec_set_pauseparam(struct net_device *netdev,
131                      struct ethtool_pauseparam *pause)
132 {
133 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
134 
135 	ugeth->ug_info->receiveFlowControl = pause->rx_pause;
136 	ugeth->ug_info->transmitFlowControl = pause->tx_pause;
137 
138 	return phylink_ethtool_set_pauseparam(ugeth->phylink, pause);
139 }
140 
141 static uint32_t
142 uec_get_msglevel(struct net_device *netdev)
143 {
144 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
145 	return ugeth->msg_enable;
146 }
147 
148 static void
149 uec_set_msglevel(struct net_device *netdev, uint32_t data)
150 {
151 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
152 	ugeth->msg_enable = data;
153 }
154 
155 static int
156 uec_get_regs_len(struct net_device *netdev)
157 {
158 	return sizeof(struct ucc_geth);
159 }
160 
161 static void
162 uec_get_regs(struct net_device *netdev,
163                struct ethtool_regs *regs, void *p)
164 {
165 	int i;
166 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
167 	u32 __iomem *ug_regs = (u32 __iomem *)ugeth->ug_regs;
168 	u32 *buff = p;
169 
170 	for (i = 0; i < sizeof(struct ucc_geth) / sizeof(u32); i++)
171 		buff[i] = in_be32(&ug_regs[i]);
172 }
173 
174 static void
175 uec_get_ringparam(struct net_device *netdev,
176 		  struct ethtool_ringparam *ring,
177 		  struct kernel_ethtool_ringparam *kernel_ring,
178 		  struct netlink_ext_ack *extack)
179 {
180 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
181 	struct ucc_geth_info *ug_info = ugeth->ug_info;
182 	int queue = 0;
183 
184 	ring->rx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
185 	ring->rx_mini_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
186 	ring->rx_jumbo_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
187 	ring->tx_max_pending = UCC_GETH_BD_RING_SIZE_MAX;
188 
189 	ring->rx_pending = ug_info->bdRingLenRx[queue];
190 	ring->rx_mini_pending = ug_info->bdRingLenRx[queue];
191 	ring->rx_jumbo_pending = ug_info->bdRingLenRx[queue];
192 	ring->tx_pending = ug_info->bdRingLenTx[queue];
193 }
194 
195 static int
196 uec_set_ringparam(struct net_device *netdev,
197 		  struct ethtool_ringparam *ring,
198 		  struct kernel_ethtool_ringparam *kernel_ring,
199 		  struct netlink_ext_ack *extack)
200 {
201 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
202 	struct ucc_geth_info *ug_info = ugeth->ug_info;
203 	int queue = 0, ret = 0;
204 
205 	if (ring->rx_pending < UCC_GETH_RX_BD_RING_SIZE_MIN) {
206 		netdev_info(netdev, "RxBD ring size must be no smaller than %d\n",
207 			    UCC_GETH_RX_BD_RING_SIZE_MIN);
208 		return -EINVAL;
209 	}
210 	if (ring->rx_pending % UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT) {
211 		netdev_info(netdev, "RxBD ring size must be multiple of %d\n",
212 			    UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT);
213 		return -EINVAL;
214 	}
215 	if (ring->tx_pending < UCC_GETH_TX_BD_RING_SIZE_MIN) {
216 		netdev_info(netdev, "TxBD ring size must be no smaller than %d\n",
217 			    UCC_GETH_TX_BD_RING_SIZE_MIN);
218 		return -EINVAL;
219 	}
220 
221 	if (netif_running(netdev))
222 		return -EBUSY;
223 
224 	ug_info->bdRingLenRx[queue] = ring->rx_pending;
225 	ug_info->bdRingLenTx[queue] = ring->tx_pending;
226 
227 	return ret;
228 }
229 
230 static int uec_get_sset_count(struct net_device *netdev, int sset)
231 {
232 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
233 	u32 stats_mode = ugeth->ug_info->statisticsMode;
234 	int len = 0;
235 
236 	switch (sset) {
237 	case ETH_SS_STATS:
238 		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
239 			len += UEC_HW_STATS_LEN;
240 		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
241 			len += UEC_TX_FW_STATS_LEN;
242 		if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
243 			len += UEC_RX_FW_STATS_LEN;
244 
245 		return len;
246 
247 	default:
248 		return -EOPNOTSUPP;
249 	}
250 }
251 
252 static void uec_get_strings(struct net_device *netdev, u32 stringset, u8 *buf)
253 {
254 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
255 	u32 stats_mode = ugeth->ug_info->statisticsMode;
256 	int i;
257 
258 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE)
259 		for (i = 0; i < UEC_HW_STATS_LEN; i++)
260 			ethtool_puts(&buf, hw_stat_gstrings[i]);
261 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX)
262 		for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
263 			ethtool_puts(&buf, tx_fw_stat_gstrings[i]);
264 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX)
265 		for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
266 			ethtool_puts(&buf, rx_fw_stat_gstrings[i]);
267 }
268 
269 static void uec_get_ethtool_stats(struct net_device *netdev,
270 		struct ethtool_stats *stats, uint64_t *data)
271 {
272 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
273 	u32 stats_mode = ugeth->ug_info->statisticsMode;
274 	u32 __iomem *base;
275 	int i, j = 0;
276 
277 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE) {
278 		if (ugeth->ug_regs)
279 			base = (u32 __iomem *)&ugeth->ug_regs->tx64;
280 		else
281 			base = NULL;
282 
283 		for (i = 0; i < UEC_HW_STATS_LEN; i++)
284 			data[j++] = base ? in_be32(&base[i]) : 0;
285 	}
286 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
287 		base = (u32 __iomem *)ugeth->p_tx_fw_statistics_pram;
288 		for (i = 0; i < UEC_TX_FW_STATS_LEN; i++)
289 			data[j++] = base ? in_be32(&base[i]) : 0;
290 	}
291 	if (stats_mode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
292 		base = (u32 __iomem *)ugeth->p_rx_fw_statistics_pram;
293 		for (i = 0; i < UEC_RX_FW_STATS_LEN; i++)
294 			data[j++] = base ? in_be32(&base[i]) : 0;
295 	}
296 }
297 
298 /* Report driver information */
299 static void
300 uec_get_drvinfo(struct net_device *netdev,
301                        struct ethtool_drvinfo *drvinfo)
302 {
303 	strscpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
304 	strscpy(drvinfo->bus_info, "QUICC ENGINE", sizeof(drvinfo->bus_info));
305 }
306 
307 #ifdef CONFIG_PM
308 
309 static void uec_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
310 {
311 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
312 
313 	phylink_ethtool_get_wol(ugeth->phylink, wol);
314 
315 	if (qe_alive_during_sleep())
316 		wol->supported |= WAKE_MAGIC;
317 
318 	wol->wolopts |= ugeth->wol_en;
319 }
320 
321 static int uec_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
322 {
323 	struct ucc_geth_private *ugeth = netdev_priv(netdev);
324 	int ret = 0;
325 
326 	ret = phylink_ethtool_set_wol(ugeth->phylink, wol);
327 	if (ret == -EOPNOTSUPP) {
328 		ugeth->phy_wol_en = 0;
329 	} else if (ret) {
330 		return ret;
331 	} else {
332 		ugeth->phy_wol_en = wol->wolopts;
333 		goto out;
334 	}
335 
336 	/* If the PHY isn't handling the WoL and the MAC is asked to more than
337 	 * WAKE_MAGIC, error-out
338 	 */
339 	if (!ugeth->phy_wol_en &&
340 	    wol->wolopts & ~WAKE_MAGIC)
341 		return -EINVAL;
342 
343 	if (wol->wolopts & WAKE_MAGIC &&
344 	    !qe_alive_during_sleep())
345 		return -EINVAL;
346 
347 out:
348 	ugeth->wol_en = wol->wolopts;
349 	device_set_wakeup_enable(&netdev->dev, ugeth->wol_en);
350 
351 	return 0;
352 }
353 
354 #else
355 #define uec_get_wol NULL
356 #define uec_set_wol NULL
357 #endif /* CONFIG_PM */
358 
359 static const struct ethtool_ops uec_ethtool_ops = {
360 	.get_drvinfo            = uec_get_drvinfo,
361 	.get_regs_len           = uec_get_regs_len,
362 	.get_regs               = uec_get_regs,
363 	.get_msglevel           = uec_get_msglevel,
364 	.set_msglevel           = uec_set_msglevel,
365 	.nway_reset             = phy_ethtool_nway_reset,
366 	.get_link               = ethtool_op_get_link,
367 	.get_ringparam          = uec_get_ringparam,
368 	.set_ringparam          = uec_set_ringparam,
369 	.get_pauseparam         = uec_get_pauseparam,
370 	.set_pauseparam         = uec_set_pauseparam,
371 	.get_sset_count		= uec_get_sset_count,
372 	.get_strings            = uec_get_strings,
373 	.get_ethtool_stats      = uec_get_ethtool_stats,
374 	.get_wol		= uec_get_wol,
375 	.set_wol		= uec_set_wol,
376 	.get_ts_info		= ethtool_op_get_ts_info,
377 	.get_link_ksettings	= uec_get_ksettings,
378 	.set_link_ksettings	= uec_set_ksettings,
379 };
380 
381 void uec_set_ethtool_ops(struct net_device *netdev)
382 {
383 	netdev->ethtool_ops = &uec_ethtool_ops;
384 }
385