blob: c28b90d350076b9373c03c97c52761fe4072eb35 [file] [log] [blame]
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001/*******************************************************************************
2 This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
3 ST Ethernet IPs are built around a Synopsys IP Core.
4
5 Copyright (C) 2007-2009 STMicroelectronics Ltd
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms and conditions of the GNU General Public License,
9 version 2, as published by the Free Software Foundation.
10
11 This program is distributed in the hope it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19
20 The full GNU General Public License is included in this distribution in
21 the file called "COPYING".
22
23 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
24
25 Documentation available at:
26 http://www.stlinux.com
27 Support available at:
28 https://bugzilla.stlinux.com/
29*******************************************************************************/
30
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/interrupt.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070035#include <linux/etherdevice.h>
36#include <linux/platform_device.h>
37#include <linux/ip.h>
38#include <linux/tcp.h>
39#include <linux/skbuff.h>
40#include <linux/ethtool.h>
41#include <linux/if_ether.h>
42#include <linux/crc32.h>
43#include <linux/mii.h>
44#include <linux/phy.h>
Jiri Pirko01789342011-08-16 06:29:00 +000045#include <linux/if.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070046#include <linux/if_vlan.h>
47#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040049#include <linux/prefetch.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070050#include "stmmac.h"
51
52#define STMMAC_RESOURCE_NAME "stmmaceth"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070053
54#undef STMMAC_DEBUG
55/*#define STMMAC_DEBUG*/
56#ifdef STMMAC_DEBUG
57#define DBG(nlevel, klevel, fmt, args...) \
58 ((void)(netif_msg_##nlevel(priv) && \
59 printk(KERN_##klevel fmt, ## args)))
60#else
61#define DBG(nlevel, klevel, fmt, args...) do { } while (0)
62#endif
63
64#undef STMMAC_RX_DEBUG
65/*#define STMMAC_RX_DEBUG*/
66#ifdef STMMAC_RX_DEBUG
67#define RX_DBG(fmt, args...) printk(fmt, ## args)
68#else
69#define RX_DBG(fmt, args...) do { } while (0)
70#endif
71
72#undef STMMAC_XMIT_DEBUG
73/*#define STMMAC_XMIT_DEBUG*/
74#ifdef STMMAC_TX_DEBUG
75#define TX_DBG(fmt, args...) printk(fmt, ## args)
76#else
77#define TX_DBG(fmt, args...) do { } while (0)
78#endif
79
80#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
81#define JUMBO_LEN 9000
82
83/* Module parameters */
84#define TX_TIMEO 5000 /* default 5 seconds */
85static int watchdog = TX_TIMEO;
86module_param(watchdog, int, S_IRUGO | S_IWUSR);
87MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
88
89static int debug = -1; /* -1: default, 0: no output, 16: all */
90module_param(debug, int, S_IRUGO | S_IWUSR);
91MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
92
93static int phyaddr = -1;
94module_param(phyaddr, int, S_IRUGO);
95MODULE_PARM_DESC(phyaddr, "Physical device address");
96
97#define DMA_TX_SIZE 256
98static int dma_txsize = DMA_TX_SIZE;
99module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
100MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
101
102#define DMA_RX_SIZE 256
103static int dma_rxsize = DMA_RX_SIZE;
104module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
105MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
106
107static int flow_ctrl = FLOW_OFF;
108module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
109MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
110
111static int pause = PAUSE_TIME;
112module_param(pause, int, S_IRUGO | S_IWUSR);
113MODULE_PARM_DESC(pause, "Flow Control Pause Time");
114
115#define TC_DEFAULT 64
116static int tc = TC_DEFAULT;
117module_param(tc, int, S_IRUGO | S_IWUSR);
118MODULE_PARM_DESC(tc, "DMA threshold control value");
119
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700120/* Pay attention to tune this parameter; take care of both
121 * hardware capability and network stabitily/performance impact.
122 * Many tests showed that ~4ms latency seems to be good enough. */
123#ifdef CONFIG_STMMAC_TIMER
124#define DEFAULT_PERIODIC_RATE 256
125static int tmrate = DEFAULT_PERIODIC_RATE;
126module_param(tmrate, int, S_IRUGO | S_IWUSR);
127MODULE_PARM_DESC(tmrate, "External timer freq. (default: 256Hz)");
128#endif
129
130#define DMA_BUFFER_SIZE BUF_SIZE_2KiB
131static int buf_sz = DMA_BUFFER_SIZE;
132module_param(buf_sz, int, S_IRUGO | S_IWUSR);
133MODULE_PARM_DESC(buf_sz, "DMA buffer size");
134
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700135static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
136 NETIF_MSG_LINK | NETIF_MSG_IFUP |
137 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
138
139static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700140
141/**
142 * stmmac_verify_args - verify the driver parameters.
143 * Description: it verifies if some wrong parameter is passed to the driver.
144 * Note that wrong parameters are replaced with the default values.
145 */
146static void stmmac_verify_args(void)
147{
148 if (unlikely(watchdog < 0))
149 watchdog = TX_TIMEO;
150 if (unlikely(dma_rxsize < 0))
151 dma_rxsize = DMA_RX_SIZE;
152 if (unlikely(dma_txsize < 0))
153 dma_txsize = DMA_TX_SIZE;
154 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
155 buf_sz = DMA_BUFFER_SIZE;
156 if (unlikely(flow_ctrl > 1))
157 flow_ctrl = FLOW_AUTO;
158 else if (likely(flow_ctrl < 0))
159 flow_ctrl = FLOW_OFF;
160 if (unlikely((pause < 0) || (pause > 0xffff)))
161 pause = PAUSE_TIME;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700162}
163
164#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
165static void print_pkt(unsigned char *buf, int len)
166{
167 int j;
168 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
169 for (j = 0; j < len; j++) {
170 if ((j % 16) == 0)
171 pr_info("\n %03x:", j);
172 pr_info(" %02x", buf[j]);
173 }
174 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700175}
176#endif
177
178/* minimum number of free TX descriptors required to wake up TX process */
179#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
180
181static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
182{
183 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
184}
185
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000186/* On some ST platforms, some HW system configuraton registers have to be
187 * set according to the link speed negotiated.
188 */
189static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
190{
191 struct phy_device *phydev = priv->phydev;
192
193 if (likely(priv->plat->fix_mac_speed))
194 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
195 phydev->speed);
196}
197
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700198/**
199 * stmmac_adjust_link
200 * @dev: net device structure
201 * Description: it adjusts the link parameters.
202 */
203static void stmmac_adjust_link(struct net_device *dev)
204{
205 struct stmmac_priv *priv = netdev_priv(dev);
206 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700207 unsigned long flags;
208 int new_state = 0;
209 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
210
211 if (phydev == NULL)
212 return;
213
214 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
215 phydev->addr, phydev->link);
216
217 spin_lock_irqsave(&priv->lock, flags);
218 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000219 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700220
221 /* Now we make sure that we can be in full duplex mode.
222 * If not, we operate in half-duplex mode. */
223 if (phydev->duplex != priv->oldduplex) {
224 new_state = 1;
225 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000226 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700227 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000228 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700229 priv->oldduplex = phydev->duplex;
230 }
231 /* Flow Control operation */
232 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000233 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000234 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700235
236 if (phydev->speed != priv->speed) {
237 new_state = 1;
238 switch (phydev->speed) {
239 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000240 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000241 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000242 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700243 break;
244 case 100:
245 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000246 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000247 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700248 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000249 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700250 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000251 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700252 }
253 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000254 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700255 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000256 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700257 break;
258 default:
259 if (netif_msg_link(priv))
260 pr_warning("%s: Speed (%d) is not 10"
261 " or 100!\n", dev->name, phydev->speed);
262 break;
263 }
264
265 priv->speed = phydev->speed;
266 }
267
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000268 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700269
270 if (!priv->oldlink) {
271 new_state = 1;
272 priv->oldlink = 1;
273 }
274 } else if (priv->oldlink) {
275 new_state = 1;
276 priv->oldlink = 0;
277 priv->speed = 0;
278 priv->oldduplex = -1;
279 }
280
281 if (new_state && netif_msg_link(priv))
282 phy_print_status(phydev);
283
284 spin_unlock_irqrestore(&priv->lock, flags);
285
286 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
287}
288
289/**
290 * stmmac_init_phy - PHY initialization
291 * @dev: net device structure
292 * Description: it initializes the driver's PHY state, and attaches the PHY
293 * to the mac driver.
294 * Return value:
295 * 0 on success
296 */
297static int stmmac_init_phy(struct net_device *dev)
298{
299 struct stmmac_priv *priv = netdev_priv(dev);
300 struct phy_device *phydev;
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000301 char phy_id[MII_BUS_ID_SIZE + 3];
302 char bus_id[MII_BUS_ID_SIZE];
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700303
304 priv->oldlink = 0;
305 priv->speed = 0;
306 priv->oldduplex = -1;
307
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000308 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000309 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000310 priv->plat->phy_addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700311 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
312
313 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000314 priv->plat->interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700315
316 if (IS_ERR(phydev)) {
317 pr_err("%s: Could not attach to PHY\n", dev->name);
318 return PTR_ERR(phydev);
319 }
320
321 /*
322 * Broken HW is sometimes missing the pull-up resistor on the
323 * MDIO line, which results in reads to non-existent devices returning
324 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
325 * device as well.
326 * Note: phydev->phy_id is the result of reading the UID PHY registers.
327 */
328 if (phydev->phy_id == 0) {
329 phy_disconnect(phydev);
330 return -ENODEV;
331 }
332 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000333 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700334
335 priv->phydev = phydev;
336
337 return 0;
338}
339
avisconti19449bf2010-10-25 18:58:14 +0000340static inline void stmmac_enable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700341{
342 u32 value = readl(ioaddr + MAC_CTRL_REG);
avisconti19449bf2010-10-25 18:58:14 +0000343
344 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700345 writel(value, ioaddr + MAC_CTRL_REG);
346}
347
avisconti19449bf2010-10-25 18:58:14 +0000348static inline void stmmac_disable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700349{
350 u32 value = readl(ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700351
avisconti19449bf2010-10-25 18:58:14 +0000352 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700353 writel(value, ioaddr + MAC_CTRL_REG);
354}
355
356/**
357 * display_ring
358 * @p: pointer to the ring.
359 * @size: size of the ring.
360 * Description: display all the descriptors within the ring.
361 */
362static void display_ring(struct dma_desc *p, int size)
363{
364 struct tmp_s {
365 u64 a;
366 unsigned int b;
367 unsigned int c;
368 };
369 int i;
370 for (i = 0; i < size; i++) {
371 struct tmp_s *x = (struct tmp_s *)(p + i);
372 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
373 i, (unsigned int)virt_to_phys(&p[i]),
374 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
375 x->b, x->c);
376 pr_info("\n");
377 }
378}
379
380/**
381 * init_dma_desc_rings - init the RX/TX descriptor rings
382 * @dev: net device structure
383 * Description: this function initializes the DMA RX/TX descriptors
384 * and allocates the socket buffers.
385 */
386static void init_dma_desc_rings(struct net_device *dev)
387{
388 int i;
389 struct stmmac_priv *priv = netdev_priv(dev);
390 struct sk_buff *skb;
391 unsigned int txsize = priv->dma_tx_size;
392 unsigned int rxsize = priv->dma_rx_size;
393 unsigned int bfsize = priv->dma_buf_sz;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000394 int buff2_needed = 0, dis_ic = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700395
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700396 /* Set the Buffer size according to the MTU;
397 * indeed, in case of jumbo we need to bump-up the buffer sizes.
398 */
399 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
400 bfsize = BUF_SIZE_16KiB;
401 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
402 bfsize = BUF_SIZE_8KiB;
403 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
404 bfsize = BUF_SIZE_4KiB;
405 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
406 bfsize = BUF_SIZE_2KiB;
407 else
408 bfsize = DMA_BUFFER_SIZE;
409
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000410#ifdef CONFIG_STMMAC_TIMER
411 /* Disable interrupts on completion for the reception if timer is on */
412 if (likely(priv->tm->enable))
413 dis_ic = 1;
414#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700415 /* If the MTU exceeds 8k so use the second buffer in the chain */
416 if (bfsize >= BUF_SIZE_8KiB)
417 buff2_needed = 1;
418
419 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
420 txsize, rxsize, bfsize);
421
422 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
423 priv->rx_skbuff =
424 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
425 priv->dma_rx =
426 (struct dma_desc *)dma_alloc_coherent(priv->device,
427 rxsize *
428 sizeof(struct dma_desc),
429 &priv->dma_rx_phy,
430 GFP_KERNEL);
431 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
432 GFP_KERNEL);
433 priv->dma_tx =
434 (struct dma_desc *)dma_alloc_coherent(priv->device,
435 txsize *
436 sizeof(struct dma_desc),
437 &priv->dma_tx_phy,
438 GFP_KERNEL);
439
440 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
441 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
442 return;
443 }
444
445 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
446 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
447 dev->name, priv->dma_rx, priv->dma_tx,
448 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
449
450 /* RX INITIALIZATION */
451 DBG(probe, INFO, "stmmac: SKB addresses:\n"
452 "skb\t\tskb data\tdma data\n");
453
454 for (i = 0; i < rxsize; i++) {
455 struct dma_desc *p = priv->dma_rx + i;
456
457 skb = netdev_alloc_skb_ip_align(dev, bfsize);
458 if (unlikely(skb == NULL)) {
459 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
460 break;
461 }
462 priv->rx_skbuff[i] = skb;
463 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
464 bfsize, DMA_FROM_DEVICE);
465
466 p->des2 = priv->rx_skbuff_dma[i];
467 if (unlikely(buff2_needed))
468 p->des3 = p->des2 + BUF_SIZE_8KiB;
469 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
470 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
471 }
472 priv->cur_rx = 0;
473 priv->dirty_rx = (unsigned int)(i - rxsize);
474 priv->dma_buf_sz = bfsize;
475 buf_sz = bfsize;
476
477 /* TX INITIALIZATION */
478 for (i = 0; i < txsize; i++) {
479 priv->tx_skbuff[i] = NULL;
480 priv->dma_tx[i].des2 = 0;
481 }
482 priv->dirty_tx = 0;
483 priv->cur_tx = 0;
484
485 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000486 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
487 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700488
489 if (netif_msg_hw(priv)) {
490 pr_info("RX descriptor ring:\n");
491 display_ring(priv->dma_rx, rxsize);
492 pr_info("TX descriptor ring:\n");
493 display_ring(priv->dma_tx, txsize);
494 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700495}
496
497static void dma_free_rx_skbufs(struct stmmac_priv *priv)
498{
499 int i;
500
501 for (i = 0; i < priv->dma_rx_size; i++) {
502 if (priv->rx_skbuff[i]) {
503 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
504 priv->dma_buf_sz, DMA_FROM_DEVICE);
505 dev_kfree_skb_any(priv->rx_skbuff[i]);
506 }
507 priv->rx_skbuff[i] = NULL;
508 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700509}
510
511static void dma_free_tx_skbufs(struct stmmac_priv *priv)
512{
513 int i;
514
515 for (i = 0; i < priv->dma_tx_size; i++) {
516 if (priv->tx_skbuff[i] != NULL) {
517 struct dma_desc *p = priv->dma_tx + i;
518 if (p->des2)
519 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000520 priv->hw->desc->get_tx_len(p),
521 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700522 dev_kfree_skb_any(priv->tx_skbuff[i]);
523 priv->tx_skbuff[i] = NULL;
524 }
525 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700526}
527
528static void free_dma_desc_resources(struct stmmac_priv *priv)
529{
530 /* Release the DMA TX/RX socket buffers */
531 dma_free_rx_skbufs(priv);
532 dma_free_tx_skbufs(priv);
533
534 /* Free the region of consistent memory previously allocated for
535 * the DMA */
536 dma_free_coherent(priv->device,
537 priv->dma_tx_size * sizeof(struct dma_desc),
538 priv->dma_tx, priv->dma_tx_phy);
539 dma_free_coherent(priv->device,
540 priv->dma_rx_size * sizeof(struct dma_desc),
541 priv->dma_rx, priv->dma_rx_phy);
542 kfree(priv->rx_skbuff_dma);
543 kfree(priv->rx_skbuff);
544 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700545}
546
547/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700548 * stmmac_dma_operation_mode - HW DMA operation mode
549 * @priv : pointer to the private device structure.
550 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000551 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700552 */
553static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
554{
Srinivas Kandagatla61b80132011-07-17 20:54:09 +0000555 if (likely(priv->plat->force_sf_dma_mode ||
556 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
557 /*
558 * In case of GMAC, SF mode can be enabled
559 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000560 * 1) TX COE if actually supported
561 * 2) There is no bugged Jumbo frame support
562 * that needs to not insert csum in the TDES.
563 */
564 priv->hw->dma->dma_mode(priv->ioaddr,
565 SF_DMA_MODE, SF_DMA_MODE);
566 tc = SF_DMA_MODE;
567 } else
568 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700569}
570
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700571/**
572 * stmmac_tx:
573 * @priv: private driver structure
574 * Description: it reclaims resources after transmission completes.
575 */
576static void stmmac_tx(struct stmmac_priv *priv)
577{
578 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700579
580 while (priv->dirty_tx != priv->cur_tx) {
581 int last;
582 unsigned int entry = priv->dirty_tx % txsize;
583 struct sk_buff *skb = priv->tx_skbuff[entry];
584 struct dma_desc *p = priv->dma_tx + entry;
585
586 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000587 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700588 break;
589
590 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000591 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700592 if (likely(last)) {
593 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000594 priv->hw->desc->tx_status(&priv->dev->stats,
595 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000596 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700597 if (likely(tx_error == 0)) {
598 priv->dev->stats.tx_packets++;
599 priv->xstats.tx_pkt_n++;
600 } else
601 priv->dev->stats.tx_errors++;
602 }
603 TX_DBG("%s: curr %d, dirty %d\n", __func__,
604 priv->cur_tx, priv->dirty_tx);
605
606 if (likely(p->des2))
607 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000608 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700609 DMA_TO_DEVICE);
610 if (unlikely(p->des3))
611 p->des3 = 0;
612
613 if (likely(skb != NULL)) {
614 /*
615 * If there's room in the queue (limit it to size)
616 * we add this skb back into the pool,
617 * if it's the right size.
618 */
619 if ((skb_queue_len(&priv->rx_recycle) <
620 priv->dma_rx_size) &&
621 skb_recycle_check(skb, priv->dma_buf_sz))
622 __skb_queue_head(&priv->rx_recycle, skb);
623 else
624 dev_kfree_skb(skb);
625
626 priv->tx_skbuff[entry] = NULL;
627 }
628
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000629 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700630
631 entry = (++priv->dirty_tx) % txsize;
632 }
633 if (unlikely(netif_queue_stopped(priv->dev) &&
634 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
635 netif_tx_lock(priv->dev);
636 if (netif_queue_stopped(priv->dev) &&
637 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
638 TX_DBG("%s: restart transmit\n", __func__);
639 netif_wake_queue(priv->dev);
640 }
641 netif_tx_unlock(priv->dev);
642 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700643}
644
645static inline void stmmac_enable_irq(struct stmmac_priv *priv)
646{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000647#ifdef CONFIG_STMMAC_TIMER
648 if (likely(priv->tm->enable))
649 priv->tm->timer_start(tmrate);
650 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700651#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000652 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700653}
654
655static inline void stmmac_disable_irq(struct stmmac_priv *priv)
656{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000657#ifdef CONFIG_STMMAC_TIMER
658 if (likely(priv->tm->enable))
659 priv->tm->timer_stop();
660 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700661#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000662 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700663}
664
665static int stmmac_has_work(struct stmmac_priv *priv)
666{
667 unsigned int has_work = 0;
668 int rxret, tx_work = 0;
669
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000670 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700671 (priv->cur_rx % priv->dma_rx_size));
672
673 if (priv->dirty_tx != priv->cur_tx)
674 tx_work = 1;
675
676 if (likely(!rxret || tx_work))
677 has_work = 1;
678
679 return has_work;
680}
681
682static inline void _stmmac_schedule(struct stmmac_priv *priv)
683{
684 if (likely(stmmac_has_work(priv))) {
685 stmmac_disable_irq(priv);
686 napi_schedule(&priv->napi);
687 }
688}
689
690#ifdef CONFIG_STMMAC_TIMER
691void stmmac_schedule(struct net_device *dev)
692{
693 struct stmmac_priv *priv = netdev_priv(dev);
694
695 priv->xstats.sched_timer_n++;
696
697 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700698}
699
700static void stmmac_no_timer_started(unsigned int x)
701{;
702};
703
704static void stmmac_no_timer_stopped(void)
705{;
706};
707#endif
708
709/**
710 * stmmac_tx_err:
711 * @priv: pointer to the private device structure
712 * Description: it cleans the descriptors and restarts the transmission
713 * in case of errors.
714 */
715static void stmmac_tx_err(struct stmmac_priv *priv)
716{
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000717
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700718 netif_stop_queue(priv->dev);
719
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000720 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700721 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000722 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700723 priv->dirty_tx = 0;
724 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000725 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700726
727 priv->dev->stats.tx_errors++;
728 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700729}
730
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000731
732static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700733{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000734 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700735
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000736 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000737 if (likely(status == handle_tx_rx))
738 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700739
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000740 else if (unlikely(status == tx_hard_error_bump_tc)) {
741 /* Try to bump up the dma threshold on this failure */
742 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
743 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000744 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000745 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700746 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000747 } else if (unlikely(status == tx_hard_error))
748 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700749}
750
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000751static void stmmac_mmc_setup(struct stmmac_priv *priv)
752{
753 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
754 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
755
756 /* Do not manage MMC IRQ (FIXME) */
757 dwmac_mmc_intr_all_mask(priv->ioaddr);
758 dwmac_mmc_ctrl(priv->ioaddr, mode);
759 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
760}
761
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700762/**
763 * stmmac_open - open entry point of the driver
764 * @dev : pointer to the device structure.
765 * Description:
766 * This function is the open entry point of the driver.
767 * Return value:
768 * 0 on success and an appropriate (-)ve integer as defined in errno.h
769 * file on failure.
770 */
771static int stmmac_open(struct net_device *dev)
772{
773 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700774 int ret;
775
776 /* Check that the MAC address is valid. If its not, refuse
777 * to bring the device up. The user must specify an
778 * address using the following linux command:
779 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
780 if (!is_valid_ether_addr(dev->dev_addr)) {
781 random_ether_addr(dev->dev_addr);
782 pr_warning("%s: generated random MAC address %pM\n", dev->name,
783 dev->dev_addr);
784 }
785
786 stmmac_verify_args();
787
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700788#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000789 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700790 if (unlikely(priv->tm == NULL)) {
Frans Pop2381a552010-03-24 07:57:36 +0000791 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700792 return -ENOMEM;
793 }
794 priv->tm->freq = tmrate;
795
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000796 /* Test if the external timer can be actually used.
797 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700798 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000799 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700800 priv->tm->freq = 0;
801 priv->tm->timer_start = stmmac_no_timer_started;
802 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000803 } else
804 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700805#endif
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000806 ret = stmmac_init_phy(dev);
807 if (unlikely(ret)) {
808 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
809 goto open_error;
810 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700811
812 /* Create and initialize the TX/RX descriptors chains. */
813 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
814 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
815 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
816 init_dma_desc_rings(dev);
817
818 /* DMA initialization and SW reset */
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000819 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
820 priv->dma_tx_phy, priv->dma_rx_phy);
821 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700822 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000823 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700824 }
825
826 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000827 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +0000828 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000829 if (priv->plat->bus_setup)
830 priv->plat->bus_setup(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700831 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000832 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700833
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000834 priv->rx_coe = priv->hw->mac->rx_coe(priv->ioaddr);
835 if (priv->rx_coe)
836 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000837 if (priv->plat->tx_coe)
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000838 pr_info("\tTX Checksum insertion supported\n");
Michał Mirosław5e982f32011-04-09 02:46:55 +0000839 netdev_update_features(dev);
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000840
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000841 /* Request the IRQ lines */
842 ret = request_irq(dev->irq, stmmac_interrupt,
843 IRQF_SHARED, dev->name, dev);
844 if (unlikely(ret < 0)) {
845 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
846 __func__, dev->irq, ret);
847 goto open_error;
848 }
849
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700850 /* Enable the MAC Rx/Tx */
avisconti19449bf2010-10-25 18:58:14 +0000851 stmmac_enable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700852
853 /* Set the HW DMA mode and the COE */
854 stmmac_dma_operation_mode(priv);
855
856 /* Extra statistics */
857 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
858 priv->xstats.threshold = tc;
859
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000860 stmmac_mmc_setup(priv);
861
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700862 /* Start the ball rolling... */
863 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000864 priv->hw->dma->start_tx(priv->ioaddr);
865 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700866
867#ifdef CONFIG_STMMAC_TIMER
868 priv->tm->timer_start(tmrate);
869#endif
870 /* Dump DMA/MAC registers */
871 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000872 priv->hw->mac->dump_regs(priv->ioaddr);
873 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700874 }
875
876 if (priv->phydev)
877 phy_start(priv->phydev);
878
879 napi_enable(&priv->napi);
880 skb_queue_head_init(&priv->rx_recycle);
881 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000882
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700883 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000884
885open_error:
886#ifdef CONFIG_STMMAC_TIMER
887 kfree(priv->tm);
888#endif
889 if (priv->phydev)
890 phy_disconnect(priv->phydev);
891
892 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700893}
894
895/**
896 * stmmac_release - close entry point of the driver
897 * @dev : device pointer.
898 * Description:
899 * This is the stop entry point of the driver.
900 */
901static int stmmac_release(struct net_device *dev)
902{
903 struct stmmac_priv *priv = netdev_priv(dev);
904
905 /* Stop and disconnect the PHY */
906 if (priv->phydev) {
907 phy_stop(priv->phydev);
908 phy_disconnect(priv->phydev);
909 priv->phydev = NULL;
910 }
911
912 netif_stop_queue(dev);
913
914#ifdef CONFIG_STMMAC_TIMER
915 /* Stop and release the timer */
916 stmmac_close_ext_timer();
917 if (priv->tm != NULL)
918 kfree(priv->tm);
919#endif
920 napi_disable(&priv->napi);
921 skb_queue_purge(&priv->rx_recycle);
922
923 /* Free the IRQ lines */
924 free_irq(dev->irq, dev);
925
926 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000927 priv->hw->dma->stop_tx(priv->ioaddr);
928 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700929
930 /* Release and free the Rx/Tx resources */
931 free_dma_desc_resources(priv);
932
avisconti19449bf2010-10-25 18:58:14 +0000933 /* Disable the MAC Rx/Tx */
934 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700935
936 netif_carrier_off(dev);
937
938 return 0;
939}
940
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700941static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
942 struct net_device *dev,
943 int csum_insertion)
944{
945 struct stmmac_priv *priv = netdev_priv(dev);
946 unsigned int nopaged_len = skb_headlen(skb);
947 unsigned int txsize = priv->dma_tx_size;
948 unsigned int entry = priv->cur_tx % txsize;
949 struct dma_desc *desc = priv->dma_tx + entry;
950
951 if (nopaged_len > BUF_SIZE_8KiB) {
952
953 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
954
955 desc->des2 = dma_map_single(priv->device, skb->data,
956 BUF_SIZE_8KiB, DMA_TO_DEVICE);
957 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000958 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
959 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700960
961 entry = (++priv->cur_tx) % txsize;
962 desc = priv->dma_tx + entry;
963
964 desc->des2 = dma_map_single(priv->device,
965 skb->data + BUF_SIZE_8KiB,
966 buf2_size, DMA_TO_DEVICE);
967 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000968 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
969 csum_insertion);
970 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700971 priv->tx_skbuff[entry] = NULL;
972 } else {
973 desc->des2 = dma_map_single(priv->device, skb->data,
974 nopaged_len, DMA_TO_DEVICE);
975 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000976 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
977 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700978 }
979 return entry;
980}
981
982/**
983 * stmmac_xmit:
984 * @skb : the socket buffer
985 * @dev : device pointer
986 * Description : Tx entry point of the driver.
987 */
988static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
989{
990 struct stmmac_priv *priv = netdev_priv(dev);
991 unsigned int txsize = priv->dma_tx_size;
992 unsigned int entry;
993 int i, csum_insertion = 0;
994 int nfrags = skb_shinfo(skb)->nr_frags;
995 struct dma_desc *desc, *first;
996
997 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
998 if (!netif_queue_stopped(dev)) {
999 netif_stop_queue(dev);
1000 /* This is a hard error, log it. */
1001 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1002 __func__);
1003 }
1004 return NETDEV_TX_BUSY;
1005 }
1006
1007 entry = priv->cur_tx % txsize;
1008
1009#ifdef STMMAC_XMIT_DEBUG
1010 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1011 pr_info("stmmac xmit:\n"
1012 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1013 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1014 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1015 !skb_is_gso(skb) ? "isn't" : "is");
1016#endif
1017
Michał Mirosław5e982f32011-04-09 02:46:55 +00001018 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001019
1020 desc = priv->dma_tx + entry;
1021 first = desc;
1022
1023#ifdef STMMAC_XMIT_DEBUG
1024 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1025 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1026 "\t\tn_frags: %d, ip_summed: %d\n",
1027 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1028#endif
1029 priv->tx_skbuff[entry] = skb;
1030 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1031 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1032 desc = priv->dma_tx + entry;
1033 } else {
1034 unsigned int nopaged_len = skb_headlen(skb);
1035 desc->des2 = dma_map_single(priv->device, skb->data,
1036 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001037 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1038 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001039 }
1040
1041 for (i = 0; i < nfrags; i++) {
1042 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1043 int len = frag->size;
1044
1045 entry = (++priv->cur_tx) % txsize;
1046 desc = priv->dma_tx + entry;
1047
1048 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1049 desc->des2 = dma_map_page(priv->device, frag->page,
1050 frag->page_offset,
1051 len, DMA_TO_DEVICE);
1052 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001053 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001054 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001055 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001056 }
1057
1058 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001059 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001060
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001061#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001062 /* Clean IC while using timer */
1063 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001064 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001065#endif
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001066
1067 wmb();
1068
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001069 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001070 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001071
1072 priv->cur_tx++;
1073
1074#ifdef STMMAC_XMIT_DEBUG
1075 if (netif_msg_pktdata(priv)) {
1076 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1077 "first=%p, nfrags=%d\n",
1078 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1079 entry, first, nfrags);
1080 display_ring(priv->dma_tx, txsize);
1081 pr_info(">>> frame to be transmitted: ");
1082 print_pkt(skb->data, skb->len);
1083 }
1084#endif
1085 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1086 TX_DBG("%s: stop transmitted packets\n", __func__);
1087 netif_stop_queue(dev);
1088 }
1089
1090 dev->stats.tx_bytes += skb->len;
1091
Richard Cochran3e82ce12011-06-12 02:19:06 +00001092 skb_tx_timestamp(skb);
1093
Richard Cochran52f64fa2011-06-19 03:31:43 +00001094 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1095
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001096 return NETDEV_TX_OK;
1097}
1098
1099static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1100{
1101 unsigned int rxsize = priv->dma_rx_size;
1102 int bfsize = priv->dma_buf_sz;
1103 struct dma_desc *p = priv->dma_rx;
1104
1105 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1106 unsigned int entry = priv->dirty_rx % rxsize;
1107 if (likely(priv->rx_skbuff[entry] == NULL)) {
1108 struct sk_buff *skb;
1109
1110 skb = __skb_dequeue(&priv->rx_recycle);
1111 if (skb == NULL)
1112 skb = netdev_alloc_skb_ip_align(priv->dev,
1113 bfsize);
1114
1115 if (unlikely(skb == NULL))
1116 break;
1117
1118 priv->rx_skbuff[entry] = skb;
1119 priv->rx_skbuff_dma[entry] =
1120 dma_map_single(priv->device, skb->data, bfsize,
1121 DMA_FROM_DEVICE);
1122
1123 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001124 if (unlikely(priv->plat->has_gmac)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001125 if (bfsize >= BUF_SIZE_8KiB)
1126 (p + entry)->des3 =
1127 (p + entry)->des2 + BUF_SIZE_8KiB;
1128 }
1129 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1130 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001131 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001132 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001133 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001134}
1135
1136static int stmmac_rx(struct stmmac_priv *priv, int limit)
1137{
1138 unsigned int rxsize = priv->dma_rx_size;
1139 unsigned int entry = priv->cur_rx % rxsize;
1140 unsigned int next_entry;
1141 unsigned int count = 0;
1142 struct dma_desc *p = priv->dma_rx + entry;
1143 struct dma_desc *p_next;
1144
1145#ifdef STMMAC_RX_DEBUG
1146 if (netif_msg_hw(priv)) {
1147 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1148 display_ring(priv->dma_rx, rxsize);
1149 }
1150#endif
1151 count = 0;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001152 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001153 int status;
1154
1155 if (count >= limit)
1156 break;
1157
1158 count++;
1159
1160 next_entry = (++priv->cur_rx) % rxsize;
1161 p_next = priv->dma_rx + next_entry;
1162 prefetch(p_next);
1163
1164 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001165 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1166 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001167 if (unlikely(status == discard_frame))
1168 priv->dev->stats.rx_errors++;
1169 else {
1170 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001171 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001172
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001173 frame_len = priv->hw->desc->get_rx_frame_len(p);
1174 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1175 * Type frames (LLC/LLC-SNAP) */
1176 if (unlikely(status != llc_snap))
1177 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001178#ifdef STMMAC_RX_DEBUG
1179 if (frame_len > ETH_FRAME_LEN)
1180 pr_debug("\tRX frame size %d, COE status: %d\n",
1181 frame_len, status);
1182
1183 if (netif_msg_hw(priv))
1184 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1185 p, entry, p->des2);
1186#endif
1187 skb = priv->rx_skbuff[entry];
1188 if (unlikely(!skb)) {
1189 pr_err("%s: Inconsistent Rx descriptor chain\n",
1190 priv->dev->name);
1191 priv->dev->stats.rx_dropped++;
1192 break;
1193 }
1194 prefetch(skb->data - NET_IP_ALIGN);
1195 priv->rx_skbuff[entry] = NULL;
1196
1197 skb_put(skb, frame_len);
1198 dma_unmap_single(priv->device,
1199 priv->rx_skbuff_dma[entry],
1200 priv->dma_buf_sz, DMA_FROM_DEVICE);
1201#ifdef STMMAC_RX_DEBUG
1202 if (netif_msg_pktdata(priv)) {
1203 pr_info(" frame received (%dbytes)", frame_len);
1204 print_pkt(skb->data, frame_len);
1205 }
1206#endif
1207 skb->protocol = eth_type_trans(skb, priv->dev);
1208
1209 if (unlikely(status == csum_none)) {
1210 /* always for the old mac 10/100 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001211 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001212 netif_receive_skb(skb);
1213 } else {
1214 skb->ip_summed = CHECKSUM_UNNECESSARY;
1215 napi_gro_receive(&priv->napi, skb);
1216 }
1217
1218 priv->dev->stats.rx_packets++;
1219 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001220 }
1221 entry = next_entry;
1222 p = p_next; /* use prefetched values */
1223 }
1224
1225 stmmac_rx_refill(priv);
1226
1227 priv->xstats.rx_pkt_n += count;
1228
1229 return count;
1230}
1231
1232/**
1233 * stmmac_poll - stmmac poll method (NAPI)
1234 * @napi : pointer to the napi structure.
1235 * @budget : maximum number of packets that the current CPU can receive from
1236 * all interfaces.
1237 * Description :
1238 * This function implements the the reception process.
1239 * Also it runs the TX completion thread
1240 */
1241static int stmmac_poll(struct napi_struct *napi, int budget)
1242{
1243 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1244 int work_done = 0;
1245
1246 priv->xstats.poll_n++;
1247 stmmac_tx(priv);
1248 work_done = stmmac_rx(priv, budget);
1249
1250 if (work_done < budget) {
1251 napi_complete(napi);
1252 stmmac_enable_irq(priv);
1253 }
1254 return work_done;
1255}
1256
1257/**
1258 * stmmac_tx_timeout
1259 * @dev : Pointer to net device structure
1260 * Description: this function is called when a packet transmission fails to
1261 * complete within a reasonable tmrate. The driver will mark the error in the
1262 * netdev structure and arrange for the device to be reset to a sane state
1263 * in order to transmit a new packet.
1264 */
1265static void stmmac_tx_timeout(struct net_device *dev)
1266{
1267 struct stmmac_priv *priv = netdev_priv(dev);
1268
1269 /* Clear Tx resources and restart transmitting again */
1270 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001271}
1272
1273/* Configuration changes (passed on by ifconfig) */
1274static int stmmac_config(struct net_device *dev, struct ifmap *map)
1275{
1276 if (dev->flags & IFF_UP) /* can't act on a running interface */
1277 return -EBUSY;
1278
1279 /* Don't allow changing the I/O address */
1280 if (map->base_addr != dev->base_addr) {
1281 pr_warning("%s: can't change I/O address\n", dev->name);
1282 return -EOPNOTSUPP;
1283 }
1284
1285 /* Don't allow changing the IRQ */
1286 if (map->irq != dev->irq) {
1287 pr_warning("%s: can't change IRQ number %d\n",
1288 dev->name, dev->irq);
1289 return -EOPNOTSUPP;
1290 }
1291
1292 /* ignore other fields */
1293 return 0;
1294}
1295
1296/**
Jiri Pirko01789342011-08-16 06:29:00 +00001297 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001298 * @dev : pointer to the device structure
1299 * Description:
1300 * This function is a driver entry point which gets called by the kernel
1301 * whenever multicast addresses must be enabled/disabled.
1302 * Return value:
1303 * void.
1304 */
Jiri Pirko01789342011-08-16 06:29:00 +00001305static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001306{
1307 struct stmmac_priv *priv = netdev_priv(dev);
1308
1309 spin_lock(&priv->lock);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001310 priv->hw->mac->set_filter(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001311 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001312}
1313
1314/**
1315 * stmmac_change_mtu - entry point to change MTU size for the device.
1316 * @dev : device pointer.
1317 * @new_mtu : the new MTU size for the device.
1318 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1319 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1320 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1321 * Return value:
1322 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1323 * file on failure.
1324 */
1325static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1326{
1327 struct stmmac_priv *priv = netdev_priv(dev);
1328 int max_mtu;
1329
1330 if (netif_running(dev)) {
1331 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1332 return -EBUSY;
1333 }
1334
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001335 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001336 max_mtu = JUMBO_LEN;
1337 else
1338 max_mtu = ETH_DATA_LEN;
1339
1340 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1341 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1342 return -EINVAL;
1343 }
1344
Michał Mirosław5e982f32011-04-09 02:46:55 +00001345 dev->mtu = new_mtu;
1346 netdev_update_features(dev);
1347
1348 return 0;
1349}
1350
1351static u32 stmmac_fix_features(struct net_device *dev, u32 features)
1352{
1353 struct stmmac_priv *priv = netdev_priv(dev);
1354
1355 if (!priv->rx_coe)
1356 features &= ~NETIF_F_RXCSUM;
1357 if (!priv->plat->tx_coe)
1358 features &= ~NETIF_F_ALL_CSUM;
1359
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001360 /* Some GMAC devices have a bugged Jumbo frame support that
1361 * needs to have the Tx COE disabled for oversized frames
1362 * (due to limited buffer sizes). In this case we disable
1363 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001364 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1365 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001366
Michał Mirosław5e982f32011-04-09 02:46:55 +00001367 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001368}
1369
1370static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1371{
1372 struct net_device *dev = (struct net_device *)dev_id;
1373 struct stmmac_priv *priv = netdev_priv(dev);
1374
1375 if (unlikely(!dev)) {
1376 pr_err("%s: invalid dev pointer\n", __func__);
1377 return IRQ_NONE;
1378 }
1379
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001380 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001381 /* To handle GMAC own interrupts */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001382 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001383
1384 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001385
1386 return IRQ_HANDLED;
1387}
1388
1389#ifdef CONFIG_NET_POLL_CONTROLLER
1390/* Polling receive - used by NETCONSOLE and other diagnostic tools
1391 * to allow network I/O with interrupts disabled. */
1392static void stmmac_poll_controller(struct net_device *dev)
1393{
1394 disable_irq(dev->irq);
1395 stmmac_interrupt(dev->irq, dev);
1396 enable_irq(dev->irq);
1397}
1398#endif
1399
1400/**
1401 * stmmac_ioctl - Entry point for the Ioctl
1402 * @dev: Device pointer.
1403 * @rq: An IOCTL specefic structure, that can contain a pointer to
1404 * a proprietary structure used to pass information to the driver.
1405 * @cmd: IOCTL command
1406 * Description:
1407 * Currently there are no special functionality supported in IOCTL, just the
1408 * phy_mii_ioctl(...) can be invoked.
1409 */
1410static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1411{
1412 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001413 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001414
1415 if (!netif_running(dev))
1416 return -EINVAL;
1417
Richard Cochran28b04112010-07-17 08:48:55 +00001418 if (!priv->phydev)
1419 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001420
Richard Cochran28b04112010-07-17 08:48:55 +00001421 spin_lock(&priv->lock);
1422 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1423 spin_unlock(&priv->lock);
1424
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001425 return ret;
1426}
1427
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001428static const struct net_device_ops stmmac_netdev_ops = {
1429 .ndo_open = stmmac_open,
1430 .ndo_start_xmit = stmmac_xmit,
1431 .ndo_stop = stmmac_release,
1432 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001433 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00001434 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001435 .ndo_tx_timeout = stmmac_tx_timeout,
1436 .ndo_do_ioctl = stmmac_ioctl,
1437 .ndo_set_config = stmmac_config,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001438#ifdef CONFIG_NET_POLL_CONTROLLER
1439 .ndo_poll_controller = stmmac_poll_controller,
1440#endif
1441 .ndo_set_mac_address = eth_mac_addr,
1442};
1443
1444/**
1445 * stmmac_probe - Initialization of the adapter .
1446 * @dev : device pointer
1447 * Description: The function initializes the network device structure for
1448 * the STMMAC driver. It also calls the low level routines
1449 * in order to init the HW (i.e. the DMA engine)
1450 */
1451static int stmmac_probe(struct net_device *dev)
1452{
1453 int ret = 0;
1454 struct stmmac_priv *priv = netdev_priv(dev);
1455
1456 ether_setup(dev);
1457
1458 dev->netdev_ops = &stmmac_netdev_ops;
1459 stmmac_set_ethtool_ops(dev);
1460
Michał Mirosław5e982f32011-04-09 02:46:55 +00001461 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1462 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001463 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1464#ifdef STMMAC_VLAN_TAG_USED
1465 /* Both mac100 and gmac support receive VLAN tag detection */
1466 dev->features |= NETIF_F_HW_VLAN_RX;
1467#endif
1468 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1469
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001470 if (flow_ctrl)
1471 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1472
1473 priv->pause = pause;
1474 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1475
1476 /* Get the MAC address */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001477 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1478 dev->dev_addr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001479
1480 if (!is_valid_ether_addr(dev->dev_addr))
1481 pr_warning("\tno valid MAC address;"
1482 "please, use ifconfig or nwhwconfig!\n");
1483
Vlad Lunguf8e96162010-11-29 22:52:52 +00001484 spin_lock_init(&priv->lock);
1485
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001486 ret = register_netdev(dev);
1487 if (ret) {
1488 pr_err("%s: ERROR %i registering the device\n",
1489 __func__, ret);
1490 return -ENODEV;
1491 }
1492
1493 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1494 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
Michał Mirosław79032642010-11-30 06:38:00 +00001495 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001496
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001497 return ret;
1498}
1499
1500/**
1501 * stmmac_mac_device_setup
1502 * @dev : device pointer
1503 * Description: select and initialise the mac device (mac100 or Gmac).
1504 */
1505static int stmmac_mac_device_setup(struct net_device *dev)
1506{
1507 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001508
1509 struct mac_device_info *device;
1510
Jiri Pirko01789342011-08-16 06:29:00 +00001511 if (priv->plat->has_gmac) {
1512 dev->priv_flags |= IFF_UNICAST_FLT;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001513 device = dwmac1000_setup(priv->ioaddr);
Jiri Pirko01789342011-08-16 06:29:00 +00001514 } else {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001515 device = dwmac100_setup(priv->ioaddr);
Jiri Pirko01789342011-08-16 06:29:00 +00001516 }
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001517
Dan Carpenter1ff21902010-07-22 01:16:48 +00001518 if (!device)
1519 return -ENOMEM;
1520
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001521 if (priv->plat->enh_desc) {
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001522 device->desc = &enh_desc_ops;
1523 pr_info("\tEnhanced descriptor structure\n");
1524 } else
Giuseppe CAVALLARO56b106a2010-04-13 20:21:12 +00001525 device->desc = &ndesc_ops;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001526
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001527 priv->hw = device;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001528
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001529 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001530 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
Deepak Sikri3172d3a2011-09-01 21:51:37 +00001531 enable_irq_wake(priv->wol_irq);
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001532 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001533
1534 return 0;
1535}
1536
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001537/**
1538 * stmmac_dvr_probe
1539 * @pdev: platform device pointer
1540 * Description: the driver is initialized through platform_device.
1541 */
1542static int stmmac_dvr_probe(struct platform_device *pdev)
1543{
1544 int ret = 0;
1545 struct resource *res;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001546 void __iomem *addr = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001547 struct net_device *ndev = NULL;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001548 struct stmmac_priv *priv = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001549 struct plat_stmmacenet_data *plat_dat;
1550
1551 pr_info("STMMAC driver:\n\tplatform registration... ");
1552 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001553 if (!res)
1554 return -ENODEV;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001555 pr_info("\tdone!\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001556
Dan Carpenterb6222682010-04-07 21:50:08 -07001557 if (!request_mem_region(res->start, resource_size(res),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001558 pdev->name)) {
1559 pr_err("%s: ERROR: memory allocation failed"
1560 "cannot get the I/O addr 0x%x\n",
1561 __func__, (unsigned int)res->start);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001562 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001563 }
1564
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001565 addr = ioremap(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001566 if (!addr) {
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001567 pr_err("%s: ERROR: memory mapping failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001568 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001569 goto out_release_region;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001570 }
1571
1572 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1573 if (!ndev) {
1574 pr_err("%s: ERROR: allocating the device\n", __func__);
1575 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001576 goto out_unmap;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001577 }
1578
1579 SET_NETDEV_DEV(ndev, &pdev->dev);
1580
1581 /* Get the MAC information */
1582 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1583 if (ndev->irq == -ENXIO) {
1584 pr_err("%s: ERROR: MAC IRQ configuration "
1585 "information not found\n", __func__);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001586 ret = -ENXIO;
1587 goto out_free_ndev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001588 }
1589
1590 priv = netdev_priv(ndev);
1591 priv->device = &(pdev->dev);
1592 priv->dev = ndev;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001593 plat_dat = pdev->dev.platform_data;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001594
1595 priv->plat = plat_dat;
1596
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001597 priv->ioaddr = addr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001598
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001599 /* PMT module is not integrated in all the MAC devices. */
1600 if (plat_dat->pmt) {
1601 pr_info("\tPMT module supported\n");
1602 device_set_wakeup_capable(&pdev->dev, 1);
1603 }
Deepak Sikri3172d3a2011-09-01 21:51:37 +00001604 /*
1605 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
1606 * The external wake up irq can be passed through the platform code
1607 * named as "eth_wake_irq"
1608 *
1609 * In case the wake up interrupt is not passed from the platform
1610 * so the driver will continue to use the mac irq (ndev->irq)
1611 */
1612 priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
1613 if (priv->wol_irq == -ENXIO)
1614 priv->wol_irq = ndev->irq;
1615
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001616
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001617 platform_set_drvdata(pdev, ndev);
1618
1619 /* Set the I/O base addr */
1620 ndev->base_addr = (unsigned long)addr;
1621
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001622 /* Custom initialisation */
1623 if (priv->plat->init) {
1624 ret = priv->plat->init(pdev);
1625 if (unlikely(ret))
Dan Carpenter34a52f32010-12-20 21:34:56 +00001626 goto out_free_ndev;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001627 }
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001628
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001629 /* MAC HW revice detection */
1630 ret = stmmac_mac_device_setup(ndev);
1631 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001632 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001633
1634 /* Network Device Registration */
1635 ret = stmmac_probe(ndev);
1636 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001637 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001638
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +00001639 /* Override with kernel parameters if supplied XXX CRS XXX
1640 * this needs to have multiple instances */
1641 if ((phyaddr >= 0) && (phyaddr <= 31))
1642 priv->plat->phy_addr = phyaddr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001643
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001644 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
David S. Miller1f0f6382010-08-30 21:55:17 -07001645 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1646 pdev->id, ndev->irq, addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001647
1648 /* MDIO bus Registration */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001649 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001650 ret = stmmac_mdio_register(ndev);
1651 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001652 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001653 pr_debug("registered!\n");
Dan Carpenter34a52f32010-12-20 21:34:56 +00001654 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001655
Dan Carpenter34a52f32010-12-20 21:34:56 +00001656out_unregister:
1657 unregister_netdev(ndev);
1658out_plat_exit:
1659 if (priv->plat->exit)
1660 priv->plat->exit(pdev);
1661out_free_ndev:
1662 free_netdev(ndev);
1663 platform_set_drvdata(pdev, NULL);
1664out_unmap:
1665 iounmap(addr);
1666out_release_region:
1667 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001668
1669 return ret;
1670}
1671
1672/**
1673 * stmmac_dvr_remove
1674 * @pdev: platform device pointer
1675 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1676 * changes the link status, releases the DMA descriptor rings,
1677 * unregisters the MDIO bus and unmaps the allocated memory.
1678 */
1679static int stmmac_dvr_remove(struct platform_device *pdev)
1680{
1681 struct net_device *ndev = platform_get_drvdata(pdev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001682 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001683 struct resource *res;
1684
1685 pr_info("%s:\n\tremoving driver", __func__);
1686
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001687 priv->hw->dma->stop_rx(priv->ioaddr);
1688 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001689
avisconti19449bf2010-10-25 18:58:14 +00001690 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001691
1692 netif_carrier_off(ndev);
1693
1694 stmmac_mdio_unregister(ndev);
1695
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001696 if (priv->plat->exit)
1697 priv->plat->exit(pdev);
1698
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001699 platform_set_drvdata(pdev, NULL);
1700 unregister_netdev(ndev);
1701
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001702 iounmap((void *)priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001703 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001704 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001705
1706 free_netdev(ndev);
1707
1708 return 0;
1709}
1710
1711#ifdef CONFIG_PM
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001712static int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001713{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001714 struct net_device *ndev = dev_get_drvdata(dev);
1715 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001716 int dis_ic = 0;
1717
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001718 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001719 return 0;
1720
1721 spin_lock(&priv->lock);
1722
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001723 netif_device_detach(ndev);
1724 netif_stop_queue(ndev);
1725 if (priv->phydev)
1726 phy_stop(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001727
1728#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001729 priv->tm->timer_stop();
1730 if (likely(priv->tm->enable))
1731 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001732#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001733 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001734
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001735 /* Stop TX/RX DMA */
1736 priv->hw->dma->stop_tx(priv->ioaddr);
1737 priv->hw->dma->stop_rx(priv->ioaddr);
1738 /* Clear the Rx/Tx descriptors */
1739 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1740 dis_ic);
1741 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001742
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001743 /* Enable Power down mode by programming the PMT regs */
1744 if (device_may_wakeup(priv->device))
1745 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
1746 else
1747 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001748
1749 spin_unlock(&priv->lock);
1750 return 0;
1751}
1752
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001753static int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001754{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001755 struct net_device *ndev = dev_get_drvdata(dev);
1756 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001757
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001758 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001759 return 0;
1760
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02001761 spin_lock(&priv->lock);
1762
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001763 /* Power Down bit, into the PM register, is cleared
1764 * automatically as soon as a magic packet or a Wake-up frame
1765 * is received. Anyway, it's better to manually clear
1766 * this bit because it can generate problems while resuming
1767 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001768 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001769 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001770
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001771 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001772
1773 /* Enable the MAC and DMA */
avisconti19449bf2010-10-25 18:58:14 +00001774 stmmac_enable_mac(priv->ioaddr);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001775 priv->hw->dma->start_tx(priv->ioaddr);
1776 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001777
1778#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001779 if (likely(priv->tm->enable))
1780 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001781#endif
1782 napi_enable(&priv->napi);
1783
1784 if (priv->phydev)
1785 phy_start(priv->phydev);
1786
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001787 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001788
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001789 spin_unlock(&priv->lock);
1790 return 0;
1791}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001792
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001793static int stmmac_freeze(struct device *dev)
1794{
1795 struct net_device *ndev = dev_get_drvdata(dev);
1796
1797 if (!ndev || !netif_running(ndev))
1798 return 0;
1799
1800 return stmmac_release(ndev);
1801}
1802
1803static int stmmac_restore(struct device *dev)
1804{
1805 struct net_device *ndev = dev_get_drvdata(dev);
1806
1807 if (!ndev || !netif_running(ndev))
1808 return 0;
1809
1810 return stmmac_open(ndev);
1811}
1812
1813static const struct dev_pm_ops stmmac_pm_ops = {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001814 .suspend = stmmac_suspend,
1815 .resume = stmmac_resume,
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001816 .freeze = stmmac_freeze,
1817 .thaw = stmmac_restore,
1818 .restore = stmmac_restore,
1819};
1820#else
1821static const struct dev_pm_ops stmmac_pm_ops;
1822#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001823
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001824static struct platform_driver stmmac_driver = {
1825 .probe = stmmac_dvr_probe,
1826 .remove = stmmac_dvr_remove,
1827 .driver = {
1828 .name = STMMAC_RESOURCE_NAME,
1829 .owner = THIS_MODULE,
1830 .pm = &stmmac_pm_ops,
1831 },
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001832};
1833
1834/**
1835 * stmmac_init_module - Entry point for the driver
1836 * Description: This function is the entry point for the driver.
1837 */
1838static int __init stmmac_init_module(void)
1839{
1840 int ret;
1841
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001842 ret = platform_driver_register(&stmmac_driver);
1843 return ret;
1844}
1845
1846/**
1847 * stmmac_cleanup_module - Cleanup routine for the driver
1848 * Description: This function is the cleanup routine for the driver.
1849 */
1850static void __exit stmmac_cleanup_module(void)
1851{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001852 platform_driver_unregister(&stmmac_driver);
1853}
1854
1855#ifndef MODULE
1856static int __init stmmac_cmdline_opt(char *str)
1857{
1858 char *opt;
1859
1860 if (!str || !*str)
1861 return -EINVAL;
1862 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00001863 if (!strncmp(opt, "debug:", 6)) {
1864 if (strict_strtoul(opt + 6, 0, (unsigned long *)&debug))
1865 goto err;
1866 } else if (!strncmp(opt, "phyaddr:", 8)) {
1867 if (strict_strtoul(opt + 8, 0,
1868 (unsigned long *)&phyaddr))
1869 goto err;
1870 } else if (!strncmp(opt, "dma_txsize:", 11)) {
1871 if (strict_strtoul(opt + 11, 0,
1872 (unsigned long *)&dma_txsize))
1873 goto err;
1874 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
1875 if (strict_strtoul(opt + 11, 0,
1876 (unsigned long *)&dma_rxsize))
1877 goto err;
1878 } else if (!strncmp(opt, "buf_sz:", 7)) {
1879 if (strict_strtoul(opt + 7, 0,
1880 (unsigned long *)&buf_sz))
1881 goto err;
1882 } else if (!strncmp(opt, "tc:", 3)) {
1883 if (strict_strtoul(opt + 3, 0, (unsigned long *)&tc))
1884 goto err;
1885 } else if (!strncmp(opt, "watchdog:", 9)) {
1886 if (strict_strtoul(opt + 9, 0,
1887 (unsigned long *)&watchdog))
1888 goto err;
1889 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
1890 if (strict_strtoul(opt + 10, 0,
1891 (unsigned long *)&flow_ctrl))
1892 goto err;
1893 } else if (!strncmp(opt, "pause:", 6)) {
1894 if (strict_strtoul(opt + 6, 0, (unsigned long *)&pause))
1895 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001896#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00001897 } else if (!strncmp(opt, "tmrate:", 7)) {
1898 if (strict_strtoul(opt + 7, 0,
1899 (unsigned long *)&tmrate))
1900 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001901#endif
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00001902 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001903 }
1904 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00001905
1906err:
1907 pr_err("%s: ERROR broken module parameter conversion", __func__);
1908 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001909}
1910
1911__setup("stmmaceth=", stmmac_cmdline_opt);
1912#endif
1913
1914module_init(stmmac_init_module);
1915module_exit(stmmac_cleanup_module);
1916
1917MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
1918MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
1919MODULE_LICENSE("GPL");