stmmac: support wake up irq from external sources (v3)
[linux-2.6.git] / drivers / net / ethernet / stmicro / stmmac / stmmac_ethtool.c
1 /*******************************************************************************
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/interrupt.h>
28 #include <linux/mii.h>
29 #include <linux/phy.h>
30 #include <asm/io.h>
31
32 #include "stmmac.h"
33 #include "dwmac_dma.h"
34
35 #define REG_SPACE_SIZE  0x1054
36 #define MAC100_ETHTOOL_NAME     "st_mac100"
37 #define GMAC_ETHTOOL_NAME       "st_gmac"
38
39 struct 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
49 static 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),
67         STMMAC_STAT(rx_length),
68         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
94 static void stmmac_ethtool_getdrvinfo(struct net_device *dev,
95                                       struct ethtool_drvinfo *info)
96 {
97         struct stmmac_priv *priv = netdev_priv(dev);
98
99         if (!priv->plat->has_gmac)
100                 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;
107 }
108
109 static int stmmac_ethtool_getsettings(struct net_device *dev,
110                                       struct ethtool_cmd *cmd)
111 {
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
132 static int stmmac_ethtool_setsettings(struct net_device *dev,
133                                       struct ethtool_cmd *cmd)
134 {
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
146 static u32 stmmac_ethtool_getmsglevel(struct net_device *dev)
147 {
148         struct stmmac_priv *priv = netdev_priv(dev);
149         return priv->msg_enable;
150 }
151
152 static void stmmac_ethtool_setmsglevel(struct net_device *dev, u32 level)
153 {
154         struct stmmac_priv *priv = netdev_priv(dev);
155         priv->msg_enable = level;
156
157 }
158
159 static int stmmac_check_if_running(struct net_device *dev)
160 {
161         if (!netif_running(dev))
162                 return -EBUSY;
163         return 0;
164 }
165
166 static int stmmac_ethtool_get_regs_len(struct net_device *dev)
167 {
168         return REG_SPACE_SIZE;
169 }
170
171 static void stmmac_ethtool_gregs(struct net_device *dev,
172                           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
181         if (!priv->plat->has_gmac) {
182                 /* MAC registers */
183                 for (i = 0; i < 12; i++)
184                         reg_space[i] = readl(priv->ioaddr + (i * 4));
185                 /* DMA registers */
186                 for (i = 0; i < 9; i++)
187                         reg_space[i + 12] =
188                             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);
191         } else {
192                 /* MAC registers */
193                 for (i = 0; i < 55; i++)
194                         reg_space[i] = readl(priv->ioaddr + (i * 4));
195                 /* DMA registers */
196                 for (i = 0; i < 22; i++)
197                         reg_space[i + 55] =
198                             readl(priv->ioaddr + (DMA_BUS_MODE + (i * 4)));
199         }
200 }
201
202 static void
203 stmmac_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);
220 }
221
222 static int
223 stmmac_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;
239         phy->autoneg = pause->autoneg;
240
241         if (phy->autoneg) {
242                 if (netif_running(netdev))
243                         ret = phy_start_aneg(phy);
244         } else
245                 priv->hw->mac->flow_ctrl(priv->ioaddr, phy->duplex,
246                                          priv->flow_ctrl, priv->pause);
247         spin_unlock(&priv->lock);
248         return ret;
249 }
250
251 static 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);
255         int i;
256
257         /* Update HW stats if supported */
258         priv->hw->dma->dma_diagnostic_fr(&dev->stats, (void *) &priv->xstats,
259                                          priv->ioaddr);
260
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         }
266 }
267
268 static 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
278 static 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         }
295 }
296
297 /* Currently only support WOL through Magic packet. */
298 static 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);
303         if (device_can_wakeup(priv->device)) {
304                 wol->supported = WAKE_MAGIC | WAKE_UCAST;
305                 wol->wolopts = priv->wolopts;
306         }
307         spin_unlock_irq(&priv->lock);
308 }
309
310 static int stmmac_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
311 {
312         struct stmmac_priv *priv = netdev_priv(dev);
313         u32 support = WAKE_MAGIC | WAKE_UCAST;
314
315         if (!device_can_wakeup(priv->device))
316                 return -EINVAL;
317
318         if (wol->wolopts & ~support)
319                 return -EINVAL;
320
321         if (wol->wolopts) {
322                 pr_info("stmmac: wakeup enable\n");
323                 device_set_wakeup_enable(priv->device, 1);
324                 enable_irq_wake(priv->wol_irq);
325         } else {
326                 device_set_wakeup_enable(priv->device, 0);
327                 disable_irq_wake(priv->wol_irq);
328         }
329
330         spin_lock_irq(&priv->lock);
331         priv->wolopts = wol->wolopts;
332         spin_unlock_irq(&priv->lock);
333
334         return 0;
335 }
336
337 static 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,
347         .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,
354 };
355
356 void stmmac_set_ethtool_ops(struct net_device *netdev)
357 {
358         SET_ETHTOOL_OPS(netdev, &stmmac_ethtool_ops);
359 }