blob: 7ed8fb6c2117250e7c55ceb6eef9a009e3223dae [file] [log] [blame]
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001/*******************************************************************************
2 STMMAC Ethtool support
3
4 Copyright (C) 2007-2009 STMicroelectronics Ltd
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23*******************************************************************************/
24
25#include <linux/etherdevice.h>
26#include <linux/ethtool.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000027#include <linux/interrupt.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070028#include <linux/mii.h>
29#include <linux/phy.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000030#include <asm/io.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070031
32#include "stmmac.h"
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000033#include "dwmac_dma.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070034
35#define REG_SPACE_SIZE 0x1054
36#define MAC100_ETHTOOL_NAME "st_mac100"
37#define GMAC_ETHTOOL_NAME "st_gmac"
38
39struct stmmac_stats {
40 char stat_string[ETH_GSTRING_LEN];
41 int sizeof_stat;
42 int stat_offset;
43};
44
45#define STMMAC_STAT(m) \
46 { #m, FIELD_SIZEOF(struct stmmac_extra_stats, m), \
47 offsetof(struct stmmac_priv, xstats.m)}
48
49static const struct stmmac_stats stmmac_gstrings_stats[] = {
50 STMMAC_STAT(tx_underflow),
51 STMMAC_STAT(tx_carrier),
52 STMMAC_STAT(tx_losscarrier),
53 STMMAC_STAT(tx_heartbeat),
54 STMMAC_STAT(tx_deferred),
55 STMMAC_STAT(tx_vlan),
56 STMMAC_STAT(rx_vlan),
57 STMMAC_STAT(tx_jabber),
58 STMMAC_STAT(tx_frame_flushed),
59 STMMAC_STAT(tx_payload_error),
60 STMMAC_STAT(tx_ip_header_error),
61 STMMAC_STAT(rx_desc),
62 STMMAC_STAT(rx_partial),
63 STMMAC_STAT(rx_runt),
64 STMMAC_STAT(rx_toolong),
65 STMMAC_STAT(rx_collision),
66 STMMAC_STAT(rx_crc),
Giuseppe Cavallaro1b924032010-02-04 09:33:21 -080067 STMMAC_STAT(rx_length),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070068 STMMAC_STAT(rx_mii),
69 STMMAC_STAT(rx_multicast),
70 STMMAC_STAT(rx_gmac_overflow),
71 STMMAC_STAT(rx_watchdog),
72 STMMAC_STAT(da_rx_filter_fail),
73 STMMAC_STAT(sa_rx_filter_fail),
74 STMMAC_STAT(rx_missed_cntr),
75 STMMAC_STAT(rx_overflow_cntr),
76 STMMAC_STAT(tx_undeflow_irq),
77 STMMAC_STAT(tx_process_stopped_irq),
78 STMMAC_STAT(tx_jabber_irq),
79 STMMAC_STAT(rx_overflow_irq),
80 STMMAC_STAT(rx_buf_unav_irq),
81 STMMAC_STAT(rx_process_stopped_irq),
82 STMMAC_STAT(rx_watchdog_irq),
83 STMMAC_STAT(tx_early_irq),
84 STMMAC_STAT(fatal_bus_error_irq),
85 STMMAC_STAT(threshold),
86 STMMAC_STAT(tx_pkt_n),
87 STMMAC_STAT(rx_pkt_n),
88 STMMAC_STAT(poll_n),
89 STMMAC_STAT(sched_timer_n),
90 STMMAC_STAT(normal_irq_n),
91};
92#define STMMAC_STATS_LEN ARRAY_SIZE(stmmac_gstrings_stats)
93
stephen hemminger8ee17ae2010-10-13 14:50:31 +000094static void stmmac_ethtool_getdrvinfo(struct net_device *dev,
95 struct ethtool_drvinfo *info)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070096{
97 struct stmmac_priv *priv = netdev_priv(dev);
98
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +000099 if (!priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700100 strcpy(info->driver, MAC100_ETHTOOL_NAME);
101 else
102 strcpy(info->driver, GMAC_ETHTOOL_NAME);
103
104 strcpy(info->version, DRV_MODULE_VERSION);
105 info->fw_version[0] = '\0';
106 info->n_stats = STMMAC_STATS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700107}
108
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000109static int stmmac_ethtool_getsettings(struct net_device *dev,
110 struct ethtool_cmd *cmd)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700111{
112 struct stmmac_priv *priv = netdev_priv(dev);
113 struct phy_device *phy = priv->phydev;
114 int rc;
115 if (phy == NULL) {
116 pr_err("%s: %s: PHY is not registered\n",
117 __func__, dev->name);
118 return -ENODEV;
119 }
120 if (!netif_running(dev)) {
121 pr_err("%s: interface is disabled: we cannot track "
122 "link speed / duplex setting\n", dev->name);
123 return -EBUSY;
124 }
125 cmd->transceiver = XCVR_INTERNAL;
126 spin_lock_irq(&priv->lock);
127 rc = phy_ethtool_gset(phy, cmd);
128 spin_unlock_irq(&priv->lock);
129 return rc;
130}
131
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000132static int stmmac_ethtool_setsettings(struct net_device *dev,
133 struct ethtool_cmd *cmd)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700134{
135 struct stmmac_priv *priv = netdev_priv(dev);
136 struct phy_device *phy = priv->phydev;
137 int rc;
138
139 spin_lock(&priv->lock);
140 rc = phy_ethtool_sset(phy, cmd);
141 spin_unlock(&priv->lock);
142
143 return rc;
144}
145
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000146static u32 stmmac_ethtool_getmsglevel(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700147{
148 struct stmmac_priv *priv = netdev_priv(dev);
149 return priv->msg_enable;
150}
151
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000152static void stmmac_ethtool_setmsglevel(struct net_device *dev, u32 level)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700153{
154 struct stmmac_priv *priv = netdev_priv(dev);
155 priv->msg_enable = level;
156
157}
158
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000159static int stmmac_check_if_running(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700160{
161 if (!netif_running(dev))
162 return -EBUSY;
163 return 0;
164}
165
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000166static int stmmac_ethtool_get_regs_len(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700167{
168 return REG_SPACE_SIZE;
169}
170
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000171static void stmmac_ethtool_gregs(struct net_device *dev,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700172 struct ethtool_regs *regs, void *space)
173{
174 int i;
175 u32 *reg_space = (u32 *) space;
176
177 struct stmmac_priv *priv = netdev_priv(dev);
178
179 memset(reg_space, 0x0, REG_SPACE_SIZE);
180
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000181 if (!priv->plat->has_gmac) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700182 /* MAC registers */
183 for (i = 0; i < 12; i++)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000184 reg_space[i] = readl(priv->ioaddr + (i * 4));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700185 /* DMA registers */
186 for (i = 0; i < 9; i++)
187 reg_space[i + 12] =
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000188 readl(priv->ioaddr + (DMA_BUS_MODE + (i * 4)));
189 reg_space[22] = readl(priv->ioaddr + DMA_CUR_TX_BUF_ADDR);
190 reg_space[23] = readl(priv->ioaddr + DMA_CUR_RX_BUF_ADDR);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700191 } else {
192 /* MAC registers */
193 for (i = 0; i < 55; i++)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000194 reg_space[i] = readl(priv->ioaddr + (i * 4));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700195 /* DMA registers */
196 for (i = 0; i < 22; i++)
197 reg_space[i + 55] =
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000198 readl(priv->ioaddr + (DMA_BUS_MODE + (i * 4)));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700199 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700200}
201
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700202static void
203stmmac_get_pauseparam(struct net_device *netdev,
204 struct ethtool_pauseparam *pause)
205{
206 struct stmmac_priv *priv = netdev_priv(netdev);
207
208 spin_lock(&priv->lock);
209
210 pause->rx_pause = 0;
211 pause->tx_pause = 0;
212 pause->autoneg = priv->phydev->autoneg;
213
214 if (priv->flow_ctrl & FLOW_RX)
215 pause->rx_pause = 1;
216 if (priv->flow_ctrl & FLOW_TX)
217 pause->tx_pause = 1;
218
219 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700220}
221
222static int
223stmmac_set_pauseparam(struct net_device *netdev,
224 struct ethtool_pauseparam *pause)
225{
226 struct stmmac_priv *priv = netdev_priv(netdev);
227 struct phy_device *phy = priv->phydev;
228 int new_pause = FLOW_OFF;
229 int ret = 0;
230
231 spin_lock(&priv->lock);
232
233 if (pause->rx_pause)
234 new_pause |= FLOW_RX;
235 if (pause->tx_pause)
236 new_pause |= FLOW_TX;
237
238 priv->flow_ctrl = new_pause;
Giuseppe CAVALLARO64c7f302011-05-12 20:28:05 +0000239 phy->autoneg = pause->autoneg;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700240
241 if (phy->autoneg) {
David Decotigny1334cb62011-05-12 20:28:04 +0000242 if (netif_running(netdev))
243 ret = phy_start_aneg(phy);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000244 } else
245 priv->hw->mac->flow_ctrl(priv->ioaddr, phy->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000246 priv->flow_ctrl, priv->pause);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700247 spin_unlock(&priv->lock);
248 return ret;
249}
250
251static void stmmac_get_ethtool_stats(struct net_device *dev,
252 struct ethtool_stats *dummy, u64 *data)
253{
254 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700255 int i;
256
257 /* Update HW stats if supported */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000258 priv->hw->dma->dma_diagnostic_fr(&dev->stats, (void *) &priv->xstats,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000259 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700260
261 for (i = 0; i < STMMAC_STATS_LEN; i++) {
262 char *p = (char *)priv + stmmac_gstrings_stats[i].stat_offset;
263 data[i] = (stmmac_gstrings_stats[i].sizeof_stat ==
264 sizeof(u64)) ? (*(u64 *)p) : (*(u32 *)p);
265 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700266}
267
268static int stmmac_get_sset_count(struct net_device *netdev, int sset)
269{
270 switch (sset) {
271 case ETH_SS_STATS:
272 return STMMAC_STATS_LEN;
273 default:
274 return -EOPNOTSUPP;
275 }
276}
277
278static void stmmac_get_strings(struct net_device *dev, u32 stringset, u8 *data)
279{
280 int i;
281 u8 *p = data;
282
283 switch (stringset) {
284 case ETH_SS_STATS:
285 for (i = 0; i < STMMAC_STATS_LEN; i++) {
286 memcpy(p, stmmac_gstrings_stats[i].stat_string,
287 ETH_GSTRING_LEN);
288 p += ETH_GSTRING_LEN;
289 }
290 break;
291 default:
292 WARN_ON(1);
293 break;
294 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700295}
296
297/* Currently only support WOL through Magic packet. */
298static void stmmac_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
299{
300 struct stmmac_priv *priv = netdev_priv(dev);
301
302 spin_lock_irq(&priv->lock);
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700303 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro74ae2fd2011-04-13 11:51:43 -0700304 wol->supported = WAKE_MAGIC | WAKE_UCAST;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700305 wol->wolopts = priv->wolopts;
306 }
307 spin_unlock_irq(&priv->lock);
308}
309
310static int stmmac_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
311{
312 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro74ae2fd2011-04-13 11:51:43 -0700313 u32 support = WAKE_MAGIC | WAKE_UCAST;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700314
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700315 if (!device_can_wakeup(priv->device))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700316 return -EINVAL;
317
318 if (wol->wolopts & ~support)
319 return -EINVAL;
320
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700321 if (wol->wolopts) {
322 pr_info("stmmac: wakeup enable\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700323 device_set_wakeup_enable(priv->device, 1);
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700324 enable_irq_wake(dev->irq);
325 } else {
326 device_set_wakeup_enable(priv->device, 0);
327 disable_irq_wake(dev->irq);
328 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700329
330 spin_lock_irq(&priv->lock);
331 priv->wolopts = wol->wolopts;
332 spin_unlock_irq(&priv->lock);
333
334 return 0;
335}
336
337static struct ethtool_ops stmmac_ethtool_ops = {
338 .begin = stmmac_check_if_running,
339 .get_drvinfo = stmmac_ethtool_getdrvinfo,
340 .get_settings = stmmac_ethtool_getsettings,
341 .set_settings = stmmac_ethtool_setsettings,
342 .get_msglevel = stmmac_ethtool_getmsglevel,
343 .set_msglevel = stmmac_ethtool_setmsglevel,
344 .get_regs = stmmac_ethtool_gregs,
345 .get_regs_len = stmmac_ethtool_get_regs_len,
346 .get_link = ethtool_op_get_link,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700347 .get_pauseparam = stmmac_get_pauseparam,
348 .set_pauseparam = stmmac_set_pauseparam,
349 .get_ethtool_stats = stmmac_get_ethtool_stats,
350 .get_strings = stmmac_get_strings,
351 .get_wol = stmmac_get_wol,
352 .set_wol = stmmac_set_wol,
353 .get_sset_count = stmmac_get_sset_count,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700354};
355
356void stmmac_set_ethtool_ops(struct net_device *netdev)
357{
358 SET_ETHTOOL_OPS(netdev, &stmmac_ethtool_ops);
359}