720c5a1531bc2901716f5977b9aa502862f3bbb2
[linux-3.10.git] / drivers / net / 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
31 #include "stmmac.h"
32 #include "dwmac_dma.h"
33
34 #define REG_SPACE_SIZE  0x1054
35 #define MAC100_ETHTOOL_NAME     "st_mac100"
36 #define GMAC_ETHTOOL_NAME       "st_gmac"
37
38 struct stmmac_stats {
39         char stat_string[ETH_GSTRING_LEN];
40         int sizeof_stat;
41         int stat_offset;
42 };
43
44 #define STMMAC_STAT(m)  \
45         { #m, FIELD_SIZEOF(struct stmmac_extra_stats, m),       \
46         offsetof(struct stmmac_priv, xstats.m)}
47
48 static const struct  stmmac_stats stmmac_gstrings_stats[] = {
49         STMMAC_STAT(tx_underflow),
50         STMMAC_STAT(tx_carrier),
51         STMMAC_STAT(tx_losscarrier),
52         STMMAC_STAT(tx_heartbeat),
53         STMMAC_STAT(tx_deferred),
54         STMMAC_STAT(tx_vlan),
55         STMMAC_STAT(rx_vlan),
56         STMMAC_STAT(tx_jabber),
57         STMMAC_STAT(tx_frame_flushed),
58         STMMAC_STAT(tx_payload_error),
59         STMMAC_STAT(tx_ip_header_error),
60         STMMAC_STAT(rx_desc),
61         STMMAC_STAT(rx_partial),
62         STMMAC_STAT(rx_runt),
63         STMMAC_STAT(rx_toolong),
64         STMMAC_STAT(rx_collision),
65         STMMAC_STAT(rx_crc),
66         STMMAC_STAT(rx_length),
67         STMMAC_STAT(rx_mii),
68         STMMAC_STAT(rx_multicast),
69         STMMAC_STAT(rx_gmac_overflow),
70         STMMAC_STAT(rx_watchdog),
71         STMMAC_STAT(da_rx_filter_fail),
72         STMMAC_STAT(sa_rx_filter_fail),
73         STMMAC_STAT(rx_missed_cntr),
74         STMMAC_STAT(rx_overflow_cntr),
75         STMMAC_STAT(tx_undeflow_irq),
76         STMMAC_STAT(tx_process_stopped_irq),
77         STMMAC_STAT(tx_jabber_irq),
78         STMMAC_STAT(rx_overflow_irq),
79         STMMAC_STAT(rx_buf_unav_irq),
80         STMMAC_STAT(rx_process_stopped_irq),
81         STMMAC_STAT(rx_watchdog_irq),
82         STMMAC_STAT(tx_early_irq),
83         STMMAC_STAT(fatal_bus_error_irq),
84         STMMAC_STAT(threshold),
85         STMMAC_STAT(tx_pkt_n),
86         STMMAC_STAT(rx_pkt_n),
87         STMMAC_STAT(poll_n),
88         STMMAC_STAT(sched_timer_n),
89         STMMAC_STAT(normal_irq_n),
90 };
91 #define STMMAC_STATS_LEN ARRAY_SIZE(stmmac_gstrings_stats)
92
93 static void stmmac_ethtool_getdrvinfo(struct net_device *dev,
94                                       struct ethtool_drvinfo *info)
95 {
96         struct stmmac_priv *priv = netdev_priv(dev);
97
98         if (!priv->plat->has_gmac)
99                 strcpy(info->driver, MAC100_ETHTOOL_NAME);
100         else
101                 strcpy(info->driver, GMAC_ETHTOOL_NAME);
102
103         strcpy(info->version, DRV_MODULE_VERSION);
104         info->fw_version[0] = '\0';
105         info->n_stats = STMMAC_STATS_LEN;
106 }
107
108 static int stmmac_ethtool_getsettings(struct net_device *dev,
109                                       struct ethtool_cmd *cmd)
110 {
111         struct stmmac_priv *priv = netdev_priv(dev);
112         struct phy_device *phy = priv->phydev;
113         int rc;
114         if (phy == NULL) {
115                 pr_err("%s: %s: PHY is not registered\n",
116                        __func__, dev->name);
117                 return -ENODEV;
118         }
119         if (!netif_running(dev)) {
120                 pr_err("%s: interface is disabled: we cannot track "
121                 "link speed / duplex setting\n", dev->name);
122                 return -EBUSY;
123         }
124         cmd->transceiver = XCVR_INTERNAL;
125         spin_lock_irq(&priv->lock);
126         rc = phy_ethtool_gset(phy, cmd);
127         spin_unlock_irq(&priv->lock);
128         return rc;
129 }
130
131 static int stmmac_ethtool_setsettings(struct net_device *dev,
132                                       struct ethtool_cmd *cmd)
133 {
134         struct stmmac_priv *priv = netdev_priv(dev);
135         struct phy_device *phy = priv->phydev;
136         int rc;
137
138         spin_lock(&priv->lock);
139         rc = phy_ethtool_sset(phy, cmd);
140         spin_unlock(&priv->lock);
141
142         return rc;
143 }
144
145 static u32 stmmac_ethtool_getmsglevel(struct net_device *dev)
146 {
147         struct stmmac_priv *priv = netdev_priv(dev);
148         return priv->msg_enable;
149 }
150
151 static void stmmac_ethtool_setmsglevel(struct net_device *dev, u32 level)
152 {
153         struct stmmac_priv *priv = netdev_priv(dev);
154         priv->msg_enable = level;
155
156 }
157
158 static int stmmac_check_if_running(struct net_device *dev)
159 {
160         if (!netif_running(dev))
161                 return -EBUSY;
162         return 0;
163 }
164
165 static int stmmac_ethtool_get_regs_len(struct net_device *dev)
166 {
167         return REG_SPACE_SIZE;
168 }
169
170 static void stmmac_ethtool_gregs(struct net_device *dev,
171                           struct ethtool_regs *regs, void *space)
172 {
173         int i;
174         u32 *reg_space = (u32 *) space;
175
176         struct stmmac_priv *priv = netdev_priv(dev);
177
178         memset(reg_space, 0x0, REG_SPACE_SIZE);
179
180         if (!priv->plat->has_gmac) {
181                 /* MAC registers */
182                 for (i = 0; i < 12; i++)
183                         reg_space[i] = readl(priv->ioaddr + (i * 4));
184                 /* DMA registers */
185                 for (i = 0; i < 9; i++)
186                         reg_space[i + 12] =
187                             readl(priv->ioaddr + (DMA_BUS_MODE + (i * 4)));
188                 reg_space[22] = readl(priv->ioaddr + DMA_CUR_TX_BUF_ADDR);
189                 reg_space[23] = readl(priv->ioaddr + DMA_CUR_RX_BUF_ADDR);
190         } else {
191                 /* MAC registers */
192                 for (i = 0; i < 55; i++)
193                         reg_space[i] = readl(priv->ioaddr + (i * 4));
194                 /* DMA registers */
195                 for (i = 0; i < 22; i++)
196                         reg_space[i + 55] =
197                             readl(priv->ioaddr + (DMA_BUS_MODE + (i * 4)));
198         }
199 }
200
201 static void
202 stmmac_get_pauseparam(struct net_device *netdev,
203                       struct ethtool_pauseparam *pause)
204 {
205         struct stmmac_priv *priv = netdev_priv(netdev);
206
207         spin_lock(&priv->lock);
208
209         pause->rx_pause = 0;
210         pause->tx_pause = 0;
211         pause->autoneg = priv->phydev->autoneg;
212
213         if (priv->flow_ctrl & FLOW_RX)
214                 pause->rx_pause = 1;
215         if (priv->flow_ctrl & FLOW_TX)
216                 pause->tx_pause = 1;
217
218         spin_unlock(&priv->lock);
219 }
220
221 static int
222 stmmac_set_pauseparam(struct net_device *netdev,
223                       struct ethtool_pauseparam *pause)
224 {
225         struct stmmac_priv *priv = netdev_priv(netdev);
226         struct phy_device *phy = priv->phydev;
227         int new_pause = FLOW_OFF;
228         int ret = 0;
229
230         spin_lock(&priv->lock);
231
232         if (pause->rx_pause)
233                 new_pause |= FLOW_RX;
234         if (pause->tx_pause)
235                 new_pause |= FLOW_TX;
236
237         priv->flow_ctrl = new_pause;
238         phy->autoneg = pause->autoneg;
239
240         if (phy->autoneg) {
241                 if (netif_running(netdev))
242                         ret = phy_start_aneg(phy);
243         } else
244                 priv->hw->mac->flow_ctrl(priv->ioaddr, phy->duplex,
245                                          priv->flow_ctrl, priv->pause);
246         spin_unlock(&priv->lock);
247         return ret;
248 }
249
250 static void stmmac_get_ethtool_stats(struct net_device *dev,
251                                  struct ethtool_stats *dummy, u64 *data)
252 {
253         struct stmmac_priv *priv = netdev_priv(dev);
254         int i;
255
256         /* Update HW stats if supported */
257         priv->hw->dma->dma_diagnostic_fr(&dev->stats, (void *) &priv->xstats,
258                                          priv->ioaddr);
259
260         for (i = 0; i < STMMAC_STATS_LEN; i++) {
261                 char *p = (char *)priv + stmmac_gstrings_stats[i].stat_offset;
262                 data[i] = (stmmac_gstrings_stats[i].sizeof_stat ==
263                 sizeof(u64)) ? (*(u64 *)p) : (*(u32 *)p);
264         }
265 }
266
267 static int stmmac_get_sset_count(struct net_device *netdev, int sset)
268 {
269         switch (sset) {
270         case ETH_SS_STATS:
271                 return STMMAC_STATS_LEN;
272         default:
273                 return -EOPNOTSUPP;
274         }
275 }
276
277 static void stmmac_get_strings(struct net_device *dev, u32 stringset, u8 *data)
278 {
279         int i;
280         u8 *p = data;
281
282         switch (stringset) {
283         case ETH_SS_STATS:
284                 for (i = 0; i < STMMAC_STATS_LEN; i++) {
285                         memcpy(p, stmmac_gstrings_stats[i].stat_string,
286                                 ETH_GSTRING_LEN);
287                         p += ETH_GSTRING_LEN;
288                 }
289                 break;
290         default:
291                 WARN_ON(1);
292                 break;
293         }
294 }
295
296 /* Currently only support WOL through Magic packet. */
297 static void stmmac_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
298 {
299         struct stmmac_priv *priv = netdev_priv(dev);
300
301         spin_lock_irq(&priv->lock);
302         if (device_can_wakeup(priv->device)) {
303                 wol->supported = WAKE_MAGIC | WAKE_UCAST;
304                 wol->wolopts = priv->wolopts;
305         }
306         spin_unlock_irq(&priv->lock);
307 }
308
309 static int stmmac_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
310 {
311         struct stmmac_priv *priv = netdev_priv(dev);
312         u32 support = WAKE_MAGIC | WAKE_UCAST;
313
314         if (!device_can_wakeup(priv->device))
315                 return -EINVAL;
316
317         if (wol->wolopts & ~support)
318                 return -EINVAL;
319
320         if (wol->wolopts) {
321                 pr_info("stmmac: wakeup enable\n");
322                 device_set_wakeup_enable(priv->device, 1);
323                 enable_irq_wake(dev->irq);
324         } else {
325                 device_set_wakeup_enable(priv->device, 0);
326                 disable_irq_wake(dev->irq);
327         }
328
329         spin_lock_irq(&priv->lock);
330         priv->wolopts = wol->wolopts;
331         spin_unlock_irq(&priv->lock);
332
333         return 0;
334 }
335
336 static struct ethtool_ops stmmac_ethtool_ops = {
337         .begin = stmmac_check_if_running,
338         .get_drvinfo = stmmac_ethtool_getdrvinfo,
339         .get_settings = stmmac_ethtool_getsettings,
340         .set_settings = stmmac_ethtool_setsettings,
341         .get_msglevel = stmmac_ethtool_getmsglevel,
342         .set_msglevel = stmmac_ethtool_setmsglevel,
343         .get_regs = stmmac_ethtool_gregs,
344         .get_regs_len = stmmac_ethtool_get_regs_len,
345         .get_link = ethtool_op_get_link,
346         .get_pauseparam = stmmac_get_pauseparam,
347         .set_pauseparam = stmmac_set_pauseparam,
348         .get_ethtool_stats = stmmac_get_ethtool_stats,
349         .get_strings = stmmac_get_strings,
350         .get_wol = stmmac_get_wol,
351         .set_wol = stmmac_set_wol,
352         .get_sset_count = stmmac_get_sset_count,
353 };
354
355 void stmmac_set_ethtool_ops(struct net_device *netdev)
356 {
357         SET_ETHTOOL_OPS(netdev, &stmmac_ethtool_ops);
358 }