blob: 14c08a8681903878916069cf3dde5aac160a4af4 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Li Yangac421852007-07-19 11:47:47 +08002/*
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 *
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -04009 * Limitation:
Justin P. Mattock70f23fd2011-05-10 10:16:21 +020010 * Can only get/set settings of the first queue.
Martin Olsson98a17082009-04-22 18:21:29 +020011 * Need to re-open the interface manually after changing some parameters.
Li Yangac421852007-07-19 11:47:47 +080012 */
13
14#include <linux/kernel.h>
Li Yangac421852007-07-19 11:47:47 +080015#include <linux/errno.h>
Li Yangac421852007-07-19 11:47:47 +080016#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>
Li Yangac421852007-07-19 11:47:47 +080025#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>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080031#include <linux/uaccess.h>
Li Yangac421852007-07-19 11:47:47 +080032#include <asm/types.h>
Li Yangac421852007-07-19 11:47:47 +080033
34#include "ucc_geth.h"
Li Yangac421852007-07-19 11:47:47 +080035
Joe Perchesc84d8052013-04-13 19:03:19 +000036static const char hw_stat_gstrings[][ETH_GSTRING_LEN] = {
Li Yangac421852007-07-19 11:47:47 +080037 "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
Joe Perchesc84d8052013-04-13 19:03:19 +000057static const char tx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
Li Yangac421852007-07-19 11:47:47 +080058 "tx-single-collision",
59 "tx-multiple-collision",
Colin Ian Kingf944ad12018-04-28 10:57:07 +010060 "tx-late-collision",
Li Yangac421852007-07-19 11:47:47 +080061 "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",
Li Yang08722bc2008-05-23 18:11:27 +080067 "tx-512-1023-frames",
Li Yangac421852007-07-19 11:47:47 +080068 "tx-1024-1518-frames",
69 "tx-jumbo-frames",
70};
71
Joe Perchesc84d8052013-04-13 19:03:19 +000072static const char rx_fw_stat_gstrings[][ETH_GSTRING_LEN] = {
Li Yangac421852007-07-19 11:47:47 +080073 "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
Li Yangac421852007-07-19 11:47:47 +0800103static int
Philippe Reynes5e74bf2d92016-05-01 17:08:09 +0200104uec_get_ksettings(struct net_device *netdev, struct ethtool_link_ksettings *cmd)
Li Yangac421852007-07-19 11:47:47 +0800105{
106 struct ucc_geth_private *ugeth = netdev_priv(netdev);
107 struct phy_device *phydev = ugeth->phydev;
Li Yangac421852007-07-19 11:47:47 +0800108
109 if (!phydev)
110 return -ENODEV;
111
yuval.shaia@oracle.com55141742017-06-13 10:09:46 +0300112 phy_ethtool_ksettings_get(phydev, cmd);
113
114 return 0;
Li Yangac421852007-07-19 11:47:47 +0800115}
116
117static int
Philippe Reynes5e74bf2d92016-05-01 17:08:09 +0200118uec_set_ksettings(struct net_device *netdev,
119 const struct ethtool_link_ksettings *cmd)
Li Yangac421852007-07-19 11:47:47 +0800120{
121 struct ucc_geth_private *ugeth = netdev_priv(netdev);
122 struct phy_device *phydev = ugeth->phydev;
123
124 if (!phydev)
125 return -ENODEV;
126
Philippe Reynes5e74bf2d92016-05-01 17:08:09 +0200127 return phy_ethtool_ksettings_set(phydev, cmd);
Li Yangac421852007-07-19 11:47:47 +0800128}
129
130static void
131uec_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
144static int
145uec_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;
Jeff Garzik7d2e3cb2008-05-13 01:41:58 -0400153
Li Yangac421852007-07-19 11:47:47 +0800154 if (ugeth->phydev->autoneg) {
155 if (netif_running(netdev)) {
156 /* FIXME: automatically restart */
Joe Perchesc84d8052013-04-13 19:03:19 +0000157 netdev_info(netdev, "Please re-open the interface\n");
Li Yangac421852007-07-19 11:47:47 +0800158 }
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
175static uint32_t
176uec_get_msglevel(struct net_device *netdev)
177{
178 struct ucc_geth_private *ugeth = netdev_priv(netdev);
179 return ugeth->msg_enable;
180}
181
182static void
183uec_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
189static int
190uec_get_regs_len(struct net_device *netdev)
191{
192 return sizeof(struct ucc_geth);
193}
194
195static void
196uec_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
208static void
209uec_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
227static int
228uec_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) {
Joe Perchesc84d8052013-04-13 19:03:19 +0000236 netdev_info(netdev, "RxBD ring size must be no smaller than %d\n",
237 UCC_GETH_RX_BD_RING_SIZE_MIN);
Li Yangac421852007-07-19 11:47:47 +0800238 return -EINVAL;
239 }
240 if (ring->rx_pending % UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT) {
Joe Perchesc84d8052013-04-13 19:03:19 +0000241 netdev_info(netdev, "RxBD ring size must be multiple of %d\n",
242 UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT);
Li Yangac421852007-07-19 11:47:47 +0800243 return -EINVAL;
244 }
245 if (ring->tx_pending < UCC_GETH_TX_BD_RING_SIZE_MIN) {
Joe Perchesc84d8052013-04-13 19:03:19 +0000246 netdev_info(netdev, "TxBD ring size must be no smaller than %d\n",
247 UCC_GETH_TX_BD_RING_SIZE_MIN);
Li Yangac421852007-07-19 11:47:47 +0800248 return -EINVAL;
249 }
250
Christophe Leroyee0df192019-05-03 13:33:23 +0000251 if (netif_running(netdev))
252 return -EBUSY;
253
Li Yangac421852007-07-19 11:47:47 +0800254 ug_info->bdRingLenRx[queue] = ring->rx_pending;
255 ug_info->bdRingLenTx[queue] = ring->tx_pending;
256
Li Yangac421852007-07-19 11:47:47 +0800257 return ret;
258}
259
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700260static int uec_get_sset_count(struct net_device *netdev, int sset)
Li Yangac421852007-07-19 11:47:47 +0800261{
262 struct ucc_geth_private *ugeth = netdev_priv(netdev);
263 u32 stats_mode = ugeth->ug_info->statisticsMode;
264 int len = 0;
265
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700266 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;
Li Yangac421852007-07-19 11:47:47 +0800274
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700275 return len;
276
277 default:
278 return -EOPNOTSUPP;
279 }
Li Yangac421852007-07-19 11:47:47 +0800280}
281
282static 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)
Joakim Tjernlund25f03dc2008-05-23 18:11:26 +0800298 memcpy(buf, rx_fw_stat_gstrings, UEC_RX_FW_STATS_LEN *
Li Yangac421852007-07-19 11:47:47 +0800299 ETH_GSTRING_LEN);
300}
301
302static 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) {
Anton Vorontsove0ad2cd2009-08-27 07:35:47 +0000311 if (ugeth->ug_regs)
312 base = (u32 __iomem *)&ugeth->ug_regs->tx64;
313 else
314 base = NULL;
315
Li Yangac421852007-07-19 11:47:47 +0800316 for (i = 0; i < UEC_HW_STATS_LEN; i++)
Anton Vorontsove0ad2cd2009-08-27 07:35:47 +0000317 data[j++] = base ? in_be32(&base[i]) : 0;
Li Yangac421852007-07-19 11:47:47 +0800318 }
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++)
Anton Vorontsov18acfa22008-11-13 21:26:27 +0300322 data[j++] = base ? in_be32(&base[i]) : 0;
Li Yangac421852007-07-19 11:47:47 +0800323 }
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++)
Anton Vorontsov18acfa22008-11-13 21:26:27 +0300327 data[j++] = base ? in_be32(&base[i]) : 0;
Li Yangac421852007-07-19 11:47:47 +0800328 }
329}
330
Li Yangac421852007-07-19 11:47:47 +0800331/* Report driver information */
332static void
333uec_get_drvinfo(struct net_device *netdev,
334 struct ethtool_drvinfo *drvinfo)
335{
Jiri Pirko7826d432013-01-06 00:44:26 +0000336 strlcpy(drvinfo->driver, DRV_NAME, sizeof(drvinfo->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000337 strlcpy(drvinfo->bus_info, "QUICC ENGINE", sizeof(drvinfo->bus_info));
Li Yangac421852007-07-19 11:47:47 +0800338}
339
Anton Vorontsov23949052009-08-27 07:35:57 +0000340#ifdef CONFIG_PM
341
342static 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
355static 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
Li Yangac421852007-07-19 11:47:47 +0800378static const struct ethtool_ops uec_ethtool_ops = {
Li Yangac421852007-07-19 11:47:47 +0800379 .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,
Florian Fainellie06986f2016-11-15 10:06:41 -0800384 .nway_reset = phy_ethtool_nway_reset,
Li Yangac421852007-07-19 11:47:47 +0800385 .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,
Jeff Garzikb9f2c042007-10-03 18:07:32 -0700390 .get_sset_count = uec_get_sset_count,
Li Yangac421852007-07-19 11:47:47 +0800391 .get_strings = uec_get_strings,
392 .get_ethtool_stats = uec_get_ethtool_stats,
Anton Vorontsov23949052009-08-27 07:35:57 +0000393 .get_wol = uec_get_wol,
394 .set_wol = uec_set_wol,
Richard Cochrane117d6d2012-04-03 22:59:40 +0000395 .get_ts_info = ethtool_op_get_ts_info,
Philippe Reynes5e74bf2d92016-05-01 17:08:09 +0200396 .get_link_ksettings = uec_get_ksettings,
397 .set_link_ksettings = uec_set_ksettings,
Li Yangac421852007-07-19 11:47:47 +0800398};
399
400void uec_set_ethtool_ops(struct net_device *netdev)
401{
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +0000402 netdev->ethtool_ops = &uec_ethtool_ops;
Li Yangac421852007-07-19 11:47:47 +0800403}