blob: 9c05cf07a432ebf494f989f3f1a78dda534bf6b5 [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>
27#include <linux/mii.h>
28#include <linux/phy.h>
29
30#include "stmmac.h"
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +000031#include "dwmac_dma.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070032
33#define REG_SPACE_SIZE 0x1054
34#define MAC100_ETHTOOL_NAME "st_mac100"
35#define GMAC_ETHTOOL_NAME "st_gmac"
36
37struct stmmac_stats {
38 char stat_string[ETH_GSTRING_LEN];
39 int sizeof_stat;
40 int stat_offset;
41};
42
43#define STMMAC_STAT(m) \
44 { #m, FIELD_SIZEOF(struct stmmac_extra_stats, m), \
45 offsetof(struct stmmac_priv, xstats.m)}
46
47static const struct stmmac_stats stmmac_gstrings_stats[] = {
48 STMMAC_STAT(tx_underflow),
49 STMMAC_STAT(tx_carrier),
50 STMMAC_STAT(tx_losscarrier),
51 STMMAC_STAT(tx_heartbeat),
52 STMMAC_STAT(tx_deferred),
53 STMMAC_STAT(tx_vlan),
54 STMMAC_STAT(rx_vlan),
55 STMMAC_STAT(tx_jabber),
56 STMMAC_STAT(tx_frame_flushed),
57 STMMAC_STAT(tx_payload_error),
58 STMMAC_STAT(tx_ip_header_error),
59 STMMAC_STAT(rx_desc),
60 STMMAC_STAT(rx_partial),
61 STMMAC_STAT(rx_runt),
62 STMMAC_STAT(rx_toolong),
63 STMMAC_STAT(rx_collision),
64 STMMAC_STAT(rx_crc),
Giuseppe Cavallaro1b924032010-02-04 09:33:21 -080065 STMMAC_STAT(rx_length),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070066 STMMAC_STAT(rx_mii),
67 STMMAC_STAT(rx_multicast),
68 STMMAC_STAT(rx_gmac_overflow),
69 STMMAC_STAT(rx_watchdog),
70 STMMAC_STAT(da_rx_filter_fail),
71 STMMAC_STAT(sa_rx_filter_fail),
72 STMMAC_STAT(rx_missed_cntr),
73 STMMAC_STAT(rx_overflow_cntr),
74 STMMAC_STAT(tx_undeflow_irq),
75 STMMAC_STAT(tx_process_stopped_irq),
76 STMMAC_STAT(tx_jabber_irq),
77 STMMAC_STAT(rx_overflow_irq),
78 STMMAC_STAT(rx_buf_unav_irq),
79 STMMAC_STAT(rx_process_stopped_irq),
80 STMMAC_STAT(rx_watchdog_irq),
81 STMMAC_STAT(tx_early_irq),
82 STMMAC_STAT(fatal_bus_error_irq),
83 STMMAC_STAT(threshold),
84 STMMAC_STAT(tx_pkt_n),
85 STMMAC_STAT(rx_pkt_n),
86 STMMAC_STAT(poll_n),
87 STMMAC_STAT(sched_timer_n),
88 STMMAC_STAT(normal_irq_n),
89};
90#define STMMAC_STATS_LEN ARRAY_SIZE(stmmac_gstrings_stats)
91
stephen hemminger8ee17ae2010-10-13 14:50:31 +000092static void stmmac_ethtool_getdrvinfo(struct net_device *dev,
93 struct ethtool_drvinfo *info)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070094{
95 struct stmmac_priv *priv = netdev_priv(dev);
96
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +000097 if (!priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070098 strcpy(info->driver, MAC100_ETHTOOL_NAME);
99 else
100 strcpy(info->driver, GMAC_ETHTOOL_NAME);
101
102 strcpy(info->version, DRV_MODULE_VERSION);
103 info->fw_version[0] = '\0';
104 info->n_stats = STMMAC_STATS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700105}
106
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000107static int stmmac_ethtool_getsettings(struct net_device *dev,
108 struct ethtool_cmd *cmd)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700109{
110 struct stmmac_priv *priv = netdev_priv(dev);
111 struct phy_device *phy = priv->phydev;
112 int rc;
113 if (phy == NULL) {
114 pr_err("%s: %s: PHY is not registered\n",
115 __func__, dev->name);
116 return -ENODEV;
117 }
118 if (!netif_running(dev)) {
119 pr_err("%s: interface is disabled: we cannot track "
120 "link speed / duplex setting\n", dev->name);
121 return -EBUSY;
122 }
123 cmd->transceiver = XCVR_INTERNAL;
124 spin_lock_irq(&priv->lock);
125 rc = phy_ethtool_gset(phy, cmd);
126 spin_unlock_irq(&priv->lock);
127 return rc;
128}
129
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000130static int stmmac_ethtool_setsettings(struct net_device *dev,
131 struct ethtool_cmd *cmd)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700132{
133 struct stmmac_priv *priv = netdev_priv(dev);
134 struct phy_device *phy = priv->phydev;
135 int rc;
136
137 spin_lock(&priv->lock);
138 rc = phy_ethtool_sset(phy, cmd);
139 spin_unlock(&priv->lock);
140
141 return rc;
142}
143
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000144static u32 stmmac_ethtool_getmsglevel(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700145{
146 struct stmmac_priv *priv = netdev_priv(dev);
147 return priv->msg_enable;
148}
149
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000150static void stmmac_ethtool_setmsglevel(struct net_device *dev, u32 level)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700151{
152 struct stmmac_priv *priv = netdev_priv(dev);
153 priv->msg_enable = level;
154
155}
156
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000157static int stmmac_check_if_running(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700158{
159 if (!netif_running(dev))
160 return -EBUSY;
161 return 0;
162}
163
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000164static int stmmac_ethtool_get_regs_len(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700165{
166 return REG_SPACE_SIZE;
167}
168
stephen hemminger8ee17ae2010-10-13 14:50:31 +0000169static void stmmac_ethtool_gregs(struct net_device *dev,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700170 struct ethtool_regs *regs, void *space)
171{
172 int i;
173 u32 *reg_space = (u32 *) space;
174
175 struct stmmac_priv *priv = netdev_priv(dev);
176
177 memset(reg_space, 0x0, REG_SPACE_SIZE);
178
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000179 if (!priv->plat->has_gmac) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700180 /* MAC registers */
181 for (i = 0; i < 12; i++)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000182 reg_space[i] = readl(priv->ioaddr + (i * 4));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700183 /* DMA registers */
184 for (i = 0; i < 9; i++)
185 reg_space[i + 12] =
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000186 readl(priv->ioaddr + (DMA_BUS_MODE + (i * 4)));
187 reg_space[22] = readl(priv->ioaddr + DMA_CUR_TX_BUF_ADDR);
188 reg_space[23] = readl(priv->ioaddr + DMA_CUR_RX_BUF_ADDR);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700189 } else {
190 /* MAC registers */
191 for (i = 0; i < 55; i++)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000192 reg_space[i] = readl(priv->ioaddr + (i * 4));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700193 /* DMA registers */
194 for (i = 0; i < 22; i++)
195 reg_space[i + 55] =
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000196 readl(priv->ioaddr + (DMA_BUS_MODE + (i * 4)));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700197 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700198}
199
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700200static void
201stmmac_get_pauseparam(struct net_device *netdev,
202 struct ethtool_pauseparam *pause)
203{
204 struct stmmac_priv *priv = netdev_priv(netdev);
205
206 spin_lock(&priv->lock);
207
208 pause->rx_pause = 0;
209 pause->tx_pause = 0;
210 pause->autoneg = priv->phydev->autoneg;
211
212 if (priv->flow_ctrl & FLOW_RX)
213 pause->rx_pause = 1;
214 if (priv->flow_ctrl & FLOW_TX)
215 pause->tx_pause = 1;
216
217 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700218}
219
220static int
221stmmac_set_pauseparam(struct net_device *netdev,
222 struct ethtool_pauseparam *pause)
223{
224 struct stmmac_priv *priv = netdev_priv(netdev);
225 struct phy_device *phy = priv->phydev;
226 int new_pause = FLOW_OFF;
227 int ret = 0;
228
229 spin_lock(&priv->lock);
230
231 if (pause->rx_pause)
232 new_pause |= FLOW_RX;
233 if (pause->tx_pause)
234 new_pause |= FLOW_TX;
235
236 priv->flow_ctrl = new_pause;
237
238 if (phy->autoneg) {
David Decotigny1334cb62011-05-12 20:28:04 +0000239 if (netif_running(netdev))
240 ret = phy_start_aneg(phy);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000241 } else
242 priv->hw->mac->flow_ctrl(priv->ioaddr, phy->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000243 priv->flow_ctrl, priv->pause);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700244 spin_unlock(&priv->lock);
245 return ret;
246}
247
248static void stmmac_get_ethtool_stats(struct net_device *dev,
249 struct ethtool_stats *dummy, u64 *data)
250{
251 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700252 int i;
253
254 /* Update HW stats if supported */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000255 priv->hw->dma->dma_diagnostic_fr(&dev->stats, (void *) &priv->xstats,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000256 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700257
258 for (i = 0; i < STMMAC_STATS_LEN; i++) {
259 char *p = (char *)priv + stmmac_gstrings_stats[i].stat_offset;
260 data[i] = (stmmac_gstrings_stats[i].sizeof_stat ==
261 sizeof(u64)) ? (*(u64 *)p) : (*(u32 *)p);
262 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700263}
264
265static int stmmac_get_sset_count(struct net_device *netdev, int sset)
266{
267 switch (sset) {
268 case ETH_SS_STATS:
269 return STMMAC_STATS_LEN;
270 default:
271 return -EOPNOTSUPP;
272 }
273}
274
275static void stmmac_get_strings(struct net_device *dev, u32 stringset, u8 *data)
276{
277 int i;
278 u8 *p = data;
279
280 switch (stringset) {
281 case ETH_SS_STATS:
282 for (i = 0; i < STMMAC_STATS_LEN; i++) {
283 memcpy(p, stmmac_gstrings_stats[i].stat_string,
284 ETH_GSTRING_LEN);
285 p += ETH_GSTRING_LEN;
286 }
287 break;
288 default:
289 WARN_ON(1);
290 break;
291 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700292}
293
294/* Currently only support WOL through Magic packet. */
295static void stmmac_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
296{
297 struct stmmac_priv *priv = netdev_priv(dev);
298
299 spin_lock_irq(&priv->lock);
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700300 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro74ae2fd2011-04-13 11:51:43 -0700301 wol->supported = WAKE_MAGIC | WAKE_UCAST;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700302 wol->wolopts = priv->wolopts;
303 }
304 spin_unlock_irq(&priv->lock);
305}
306
307static int stmmac_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
308{
309 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro74ae2fd2011-04-13 11:51:43 -0700310 u32 support = WAKE_MAGIC | WAKE_UCAST;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700311
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700312 if (!device_can_wakeup(priv->device))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700313 return -EINVAL;
314
315 if (wol->wolopts & ~support)
316 return -EINVAL;
317
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700318 if (wol->wolopts) {
319 pr_info("stmmac: wakeup enable\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700320 device_set_wakeup_enable(priv->device, 1);
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -0700321 enable_irq_wake(dev->irq);
322 } else {
323 device_set_wakeup_enable(priv->device, 0);
324 disable_irq_wake(dev->irq);
325 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700326
327 spin_lock_irq(&priv->lock);
328 priv->wolopts = wol->wolopts;
329 spin_unlock_irq(&priv->lock);
330
331 return 0;
332}
333
334static struct ethtool_ops stmmac_ethtool_ops = {
335 .begin = stmmac_check_if_running,
336 .get_drvinfo = stmmac_ethtool_getdrvinfo,
337 .get_settings = stmmac_ethtool_getsettings,
338 .set_settings = stmmac_ethtool_setsettings,
339 .get_msglevel = stmmac_ethtool_getmsglevel,
340 .set_msglevel = stmmac_ethtool_setmsglevel,
341 .get_regs = stmmac_ethtool_gregs,
342 .get_regs_len = stmmac_ethtool_get_regs_len,
343 .get_link = ethtool_op_get_link,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700344 .get_pauseparam = stmmac_get_pauseparam,
345 .set_pauseparam = stmmac_set_pauseparam,
346 .get_ethtool_stats = stmmac_get_ethtool_stats,
347 .get_strings = stmmac_get_strings,
348 .get_wol = stmmac_get_wol,
349 .set_wol = stmmac_set_wol,
350 .get_sset_count = stmmac_get_sset_count,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700351};
352
353void stmmac_set_ethtool_ops(struct net_device *netdev)
354{
355 SET_ETHTOOL_OPS(netdev, &stmmac_ethtool_ops);
356}