cris: add arch/cris/include/asm/serial.h
[linux-2.6/next.git] / drivers / net / stmmac / stmmac_ethtool.c
blob7ed8fb6c2117250e7c55ceb6eef9a009e3223dae
1 /*******************************************************************************
2 STMMAC Ethtool support
4 Copyright (C) 2007-2009 STMicroelectronics Ltd
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.
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.
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.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
22 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
23 *******************************************************************************/
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>
32 #include "stmmac.h"
33 #include "dwmac_dma.h"
35 #define REG_SPACE_SIZE 0x1054
36 #define MAC100_ETHTOOL_NAME "st_mac100"
37 #define GMAC_ETHTOOL_NAME "st_gmac"
39 struct stmmac_stats {
40 char stat_string[ETH_GSTRING_LEN];
41 int sizeof_stat;
42 int stat_offset;
45 #define STMMAC_STAT(m) \
46 { #m, FIELD_SIZEOF(struct stmmac_extra_stats, m), \
47 offsetof(struct stmmac_priv, xstats.m)}
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),
92 #define STMMAC_STATS_LEN ARRAY_SIZE(stmmac_gstrings_stats)
94 static void stmmac_ethtool_getdrvinfo(struct net_device *dev,
95 struct ethtool_drvinfo *info)
97 struct stmmac_priv *priv = netdev_priv(dev);
99 if (!priv->plat->has_gmac)
100 strcpy(info->driver, MAC100_ETHTOOL_NAME);
101 else
102 strcpy(info->driver, GMAC_ETHTOOL_NAME);
104 strcpy(info->version, DRV_MODULE_VERSION);
105 info->fw_version[0] = '\0';
106 info->n_stats = STMMAC_STATS_LEN;
109 static int stmmac_ethtool_getsettings(struct net_device *dev,
110 struct ethtool_cmd *cmd)
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;
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;
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;
132 static int stmmac_ethtool_setsettings(struct net_device *dev,
133 struct ethtool_cmd *cmd)
135 struct stmmac_priv *priv = netdev_priv(dev);
136 struct phy_device *phy = priv->phydev;
137 int rc;
139 spin_lock(&priv->lock);
140 rc = phy_ethtool_sset(phy, cmd);
141 spin_unlock(&priv->lock);
143 return rc;
146 static u32 stmmac_ethtool_getmsglevel(struct net_device *dev)
148 struct stmmac_priv *priv = netdev_priv(dev);
149 return priv->msg_enable;
152 static void stmmac_ethtool_setmsglevel(struct net_device *dev, u32 level)
154 struct stmmac_priv *priv = netdev_priv(dev);
155 priv->msg_enable = level;
159 static int stmmac_check_if_running(struct net_device *dev)
161 if (!netif_running(dev))
162 return -EBUSY;
163 return 0;
166 static int stmmac_ethtool_get_regs_len(struct net_device *dev)
168 return REG_SPACE_SIZE;
171 static void stmmac_ethtool_gregs(struct net_device *dev,
172 struct ethtool_regs *regs, void *space)
174 int i;
175 u32 *reg_space = (u32 *) space;
177 struct stmmac_priv *priv = netdev_priv(dev);
179 memset(reg_space, 0x0, REG_SPACE_SIZE);
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)));
202 static void
203 stmmac_get_pauseparam(struct net_device *netdev,
204 struct ethtool_pauseparam *pause)
206 struct stmmac_priv *priv = netdev_priv(netdev);
208 spin_lock(&priv->lock);
210 pause->rx_pause = 0;
211 pause->tx_pause = 0;
212 pause->autoneg = priv->phydev->autoneg;
214 if (priv->flow_ctrl & FLOW_RX)
215 pause->rx_pause = 1;
216 if (priv->flow_ctrl & FLOW_TX)
217 pause->tx_pause = 1;
219 spin_unlock(&priv->lock);
222 static int
223 stmmac_set_pauseparam(struct net_device *netdev,
224 struct ethtool_pauseparam *pause)
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;
231 spin_lock(&priv->lock);
233 if (pause->rx_pause)
234 new_pause |= FLOW_RX;
235 if (pause->tx_pause)
236 new_pause |= FLOW_TX;
238 priv->flow_ctrl = new_pause;
239 phy->autoneg = pause->autoneg;
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;
251 static void stmmac_get_ethtool_stats(struct net_device *dev,
252 struct ethtool_stats *dummy, u64 *data)
254 struct stmmac_priv *priv = netdev_priv(dev);
255 int i;
257 /* Update HW stats if supported */
258 priv->hw->dma->dma_diagnostic_fr(&dev->stats, (void *) &priv->xstats,
259 priv->ioaddr);
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);
268 static int stmmac_get_sset_count(struct net_device *netdev, int sset)
270 switch (sset) {
271 case ETH_SS_STATS:
272 return STMMAC_STATS_LEN;
273 default:
274 return -EOPNOTSUPP;
278 static void stmmac_get_strings(struct net_device *dev, u32 stringset, u8 *data)
280 int i;
281 u8 *p = data;
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;
290 break;
291 default:
292 WARN_ON(1);
293 break;
297 /* Currently only support WOL through Magic packet. */
298 static void stmmac_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
300 struct stmmac_priv *priv = netdev_priv(dev);
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;
307 spin_unlock_irq(&priv->lock);
310 static int stmmac_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
312 struct stmmac_priv *priv = netdev_priv(dev);
313 u32 support = WAKE_MAGIC | WAKE_UCAST;
315 if (!device_can_wakeup(priv->device))
316 return -EINVAL;
318 if (wol->wolopts & ~support)
319 return -EINVAL;
321 if (wol->wolopts) {
322 pr_info("stmmac: wakeup enable\n");
323 device_set_wakeup_enable(priv->device, 1);
324 enable_irq_wake(dev->irq);
325 } else {
326 device_set_wakeup_enable(priv->device, 0);
327 disable_irq_wake(dev->irq);
330 spin_lock_irq(&priv->lock);
331 priv->wolopts = wol->wolopts;
332 spin_unlock_irq(&priv->lock);
334 return 0;
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,
356 void stmmac_set_ethtool_ops(struct net_device *netdev)
358 SET_ETHTOOL_OPS(netdev, &stmmac_ethtool_ops);