1 /*******************************************************************************
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
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/mii.h>
28 #include <linux/phy.h>
32 #define REG_SPACE_SIZE 0x1054
33 #define MAC100_ETHTOOL_NAME "st_mac100"
34 #define GMAC_ETHTOOL_NAME "st_gmac"
37 char stat_string
[ETH_GSTRING_LEN
];
42 #define STMMAC_STAT(m) \
43 { #m, FIELD_SIZEOF(struct stmmac_extra_stats, m), \
44 offsetof(struct stmmac_priv, xstats.m)}
46 static const struct stmmac_stats stmmac_gstrings_stats
[] = {
47 STMMAC_STAT(tx_underflow
),
48 STMMAC_STAT(tx_carrier
),
49 STMMAC_STAT(tx_losscarrier
),
50 STMMAC_STAT(tx_heartbeat
),
51 STMMAC_STAT(tx_deferred
),
54 STMMAC_STAT(tx_jabber
),
55 STMMAC_STAT(tx_frame_flushed
),
56 STMMAC_STAT(tx_payload_error
),
57 STMMAC_STAT(tx_ip_header_error
),
59 STMMAC_STAT(rx_partial
),
61 STMMAC_STAT(rx_toolong
),
62 STMMAC_STAT(rx_collision
),
64 STMMAC_STAT(rx_lenght
),
66 STMMAC_STAT(rx_multicast
),
67 STMMAC_STAT(rx_gmac_overflow
),
68 STMMAC_STAT(rx_watchdog
),
69 STMMAC_STAT(da_rx_filter_fail
),
70 STMMAC_STAT(sa_rx_filter_fail
),
71 STMMAC_STAT(rx_missed_cntr
),
72 STMMAC_STAT(rx_overflow_cntr
),
73 STMMAC_STAT(tx_undeflow_irq
),
74 STMMAC_STAT(tx_process_stopped_irq
),
75 STMMAC_STAT(tx_jabber_irq
),
76 STMMAC_STAT(rx_overflow_irq
),
77 STMMAC_STAT(rx_buf_unav_irq
),
78 STMMAC_STAT(rx_process_stopped_irq
),
79 STMMAC_STAT(rx_watchdog_irq
),
80 STMMAC_STAT(tx_early_irq
),
81 STMMAC_STAT(fatal_bus_error_irq
),
82 STMMAC_STAT(threshold
),
83 STMMAC_STAT(tx_pkt_n
),
84 STMMAC_STAT(rx_pkt_n
),
86 STMMAC_STAT(sched_timer_n
),
87 STMMAC_STAT(normal_irq_n
),
89 #define STMMAC_STATS_LEN ARRAY_SIZE(stmmac_gstrings_stats)
91 void stmmac_ethtool_getdrvinfo(struct net_device
*dev
,
92 struct ethtool_drvinfo
*info
)
94 struct stmmac_priv
*priv
= netdev_priv(dev
);
97 strcpy(info
->driver
, MAC100_ETHTOOL_NAME
);
99 strcpy(info
->driver
, GMAC_ETHTOOL_NAME
);
101 strcpy(info
->version
, DRV_MODULE_VERSION
);
102 info
->fw_version
[0] = '\0';
103 info
->n_stats
= STMMAC_STATS_LEN
;
107 int stmmac_ethtool_getsettings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
109 struct stmmac_priv
*priv
= netdev_priv(dev
);
110 struct phy_device
*phy
= priv
->phydev
;
113 pr_err("%s: %s: PHY is not registered\n",
114 __func__
, dev
->name
);
117 if (!netif_running(dev
)) {
118 pr_err("%s: interface is disabled: we cannot track "
119 "link speed / duplex setting\n", dev
->name
);
122 cmd
->transceiver
= XCVR_INTERNAL
;
123 spin_lock_irq(&priv
->lock
);
124 rc
= phy_ethtool_gset(phy
, cmd
);
125 spin_unlock_irq(&priv
->lock
);
129 int stmmac_ethtool_setsettings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
131 struct stmmac_priv
*priv
= netdev_priv(dev
);
132 struct phy_device
*phy
= priv
->phydev
;
135 spin_lock(&priv
->lock
);
136 rc
= phy_ethtool_sset(phy
, cmd
);
137 spin_unlock(&priv
->lock
);
142 u32
stmmac_ethtool_getmsglevel(struct net_device
*dev
)
144 struct stmmac_priv
*priv
= netdev_priv(dev
);
145 return priv
->msg_enable
;
148 void stmmac_ethtool_setmsglevel(struct net_device
*dev
, u32 level
)
150 struct stmmac_priv
*priv
= netdev_priv(dev
);
151 priv
->msg_enable
= level
;
155 int stmmac_check_if_running(struct net_device
*dev
)
157 if (!netif_running(dev
))
162 int stmmac_ethtool_get_regs_len(struct net_device
*dev
)
164 return REG_SPACE_SIZE
;
167 void stmmac_ethtool_gregs(struct net_device
*dev
,
168 struct ethtool_regs
*regs
, void *space
)
171 u32
*reg_space
= (u32
*) space
;
173 struct stmmac_priv
*priv
= netdev_priv(dev
);
175 memset(reg_space
, 0x0, REG_SPACE_SIZE
);
177 if (!priv
->is_gmac
) {
179 for (i
= 0; i
< 12; i
++)
180 reg_space
[i
] = readl(dev
->base_addr
+ (i
* 4));
182 for (i
= 0; i
< 9; i
++)
184 readl(dev
->base_addr
+ (DMA_BUS_MODE
+ (i
* 4)));
185 reg_space
[22] = readl(dev
->base_addr
+ DMA_CUR_TX_BUF_ADDR
);
186 reg_space
[23] = readl(dev
->base_addr
+ DMA_CUR_RX_BUF_ADDR
);
189 for (i
= 0; i
< 55; i
++)
190 reg_space
[i
] = readl(dev
->base_addr
+ (i
* 4));
192 for (i
= 0; i
< 22; i
++)
194 readl(dev
->base_addr
+ (DMA_BUS_MODE
+ (i
* 4)));
200 int stmmac_ethtool_set_tx_csum(struct net_device
*netdev
, u32 data
)
203 netdev
->features
|= NETIF_F_HW_CSUM
;
205 netdev
->features
&= ~NETIF_F_HW_CSUM
;
210 u32
stmmac_ethtool_get_rx_csum(struct net_device
*dev
)
212 struct stmmac_priv
*priv
= netdev_priv(dev
);
214 return priv
->rx_csum
;
218 stmmac_get_pauseparam(struct net_device
*netdev
,
219 struct ethtool_pauseparam
*pause
)
221 struct stmmac_priv
*priv
= netdev_priv(netdev
);
223 spin_lock(&priv
->lock
);
227 pause
->autoneg
= priv
->phydev
->autoneg
;
229 if (priv
->flow_ctrl
& FLOW_RX
)
231 if (priv
->flow_ctrl
& FLOW_TX
)
234 spin_unlock(&priv
->lock
);
239 stmmac_set_pauseparam(struct net_device
*netdev
,
240 struct ethtool_pauseparam
*pause
)
242 struct stmmac_priv
*priv
= netdev_priv(netdev
);
243 struct phy_device
*phy
= priv
->phydev
;
244 int new_pause
= FLOW_OFF
;
247 spin_lock(&priv
->lock
);
250 new_pause
|= FLOW_RX
;
252 new_pause
|= FLOW_TX
;
254 priv
->flow_ctrl
= new_pause
;
257 if (netif_running(netdev
)) {
258 struct ethtool_cmd cmd
;
259 /* auto-negotiation automatically restarted */
260 cmd
.cmd
= ETHTOOL_NWAY_RST
;
261 cmd
.supported
= phy
->supported
;
262 cmd
.advertising
= phy
->advertising
;
263 cmd
.autoneg
= phy
->autoneg
;
264 cmd
.speed
= phy
->speed
;
265 cmd
.duplex
= phy
->duplex
;
266 cmd
.phy_address
= phy
->addr
;
267 ret
= phy_ethtool_sset(phy
, &cmd
);
270 unsigned long ioaddr
= netdev
->base_addr
;
271 priv
->mac_type
->ops
->flow_ctrl(ioaddr
, phy
->duplex
,
272 priv
->flow_ctrl
, priv
->pause
);
274 spin_unlock(&priv
->lock
);
278 static void stmmac_get_ethtool_stats(struct net_device
*dev
,
279 struct ethtool_stats
*dummy
, u64
*data
)
281 struct stmmac_priv
*priv
= netdev_priv(dev
);
282 unsigned long ioaddr
= dev
->base_addr
;
285 /* Update HW stats if supported */
286 priv
->mac_type
->ops
->dma_diagnostic_fr(&dev
->stats
, &priv
->xstats
,
289 for (i
= 0; i
< STMMAC_STATS_LEN
; i
++) {
290 char *p
= (char *)priv
+ stmmac_gstrings_stats
[i
].stat_offset
;
291 data
[i
] = (stmmac_gstrings_stats
[i
].sizeof_stat
==
292 sizeof(u64
)) ? (*(u64
*)p
) : (*(u32
*)p
);
298 static int stmmac_get_sset_count(struct net_device
*netdev
, int sset
)
302 return STMMAC_STATS_LEN
;
308 static void stmmac_get_strings(struct net_device
*dev
, u32 stringset
, u8
*data
)
315 for (i
= 0; i
< STMMAC_STATS_LEN
; i
++) {
316 memcpy(p
, stmmac_gstrings_stats
[i
].stat_string
,
318 p
+= ETH_GSTRING_LEN
;
328 /* Currently only support WOL through Magic packet. */
329 static void stmmac_get_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
331 struct stmmac_priv
*priv
= netdev_priv(dev
);
333 spin_lock_irq(&priv
->lock
);
334 if (priv
->wolenabled
== PMT_SUPPORTED
) {
335 wol
->supported
= WAKE_MAGIC
;
336 wol
->wolopts
= priv
->wolopts
;
338 spin_unlock_irq(&priv
->lock
);
341 static int stmmac_set_wol(struct net_device
*dev
, struct ethtool_wolinfo
*wol
)
343 struct stmmac_priv
*priv
= netdev_priv(dev
);
344 u32 support
= WAKE_MAGIC
;
346 if (priv
->wolenabled
== PMT_NOT_SUPPORTED
)
349 if (wol
->wolopts
& ~support
)
352 if (wol
->wolopts
== 0)
353 device_set_wakeup_enable(priv
->device
, 0);
355 device_set_wakeup_enable(priv
->device
, 1);
357 spin_lock_irq(&priv
->lock
);
358 priv
->wolopts
= wol
->wolopts
;
359 spin_unlock_irq(&priv
->lock
);
364 static struct ethtool_ops stmmac_ethtool_ops
= {
365 .begin
= stmmac_check_if_running
,
366 .get_drvinfo
= stmmac_ethtool_getdrvinfo
,
367 .get_settings
= stmmac_ethtool_getsettings
,
368 .set_settings
= stmmac_ethtool_setsettings
,
369 .get_msglevel
= stmmac_ethtool_getmsglevel
,
370 .set_msglevel
= stmmac_ethtool_setmsglevel
,
371 .get_regs
= stmmac_ethtool_gregs
,
372 .get_regs_len
= stmmac_ethtool_get_regs_len
,
373 .get_link
= ethtool_op_get_link
,
374 .get_rx_csum
= stmmac_ethtool_get_rx_csum
,
375 .get_tx_csum
= ethtool_op_get_tx_csum
,
376 .set_tx_csum
= stmmac_ethtool_set_tx_csum
,
377 .get_sg
= ethtool_op_get_sg
,
378 .set_sg
= ethtool_op_set_sg
,
379 .get_pauseparam
= stmmac_get_pauseparam
,
380 .set_pauseparam
= stmmac_set_pauseparam
,
381 .get_ethtool_stats
= stmmac_get_ethtool_stats
,
382 .get_strings
= stmmac_get_strings
,
383 .get_wol
= stmmac_get_wol
,
384 .set_wol
= stmmac_set_wol
,
385 .get_sset_count
= stmmac_get_sset_count
,
387 .get_tso
= ethtool_op_get_tso
,
388 .set_tso
= ethtool_op_set_tso
,
392 void stmmac_set_ethtool_ops(struct net_device
*netdev
)
394 SET_ETHTOOL_OPS(netdev
, &stmmac_ethtool_ops
);