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