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