blob: 62fa51ed93ff2ec1ff6c186a2505c5afa4d3ae20 [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>
45#include <linux/if_vlan.h>
46#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090047#include <linux/slab.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070048#include "stmmac.h"
49
50#define STMMAC_RESOURCE_NAME "stmmaceth"
51#define PHY_RESOURCE_NAME "stmmacphy"
52
53#undef STMMAC_DEBUG
54/*#define STMMAC_DEBUG*/
55#ifdef STMMAC_DEBUG
56#define DBG(nlevel, klevel, fmt, args...) \
57 ((void)(netif_msg_##nlevel(priv) && \
58 printk(KERN_##klevel fmt, ## args)))
59#else
60#define DBG(nlevel, klevel, fmt, args...) do { } while (0)
61#endif
62
63#undef STMMAC_RX_DEBUG
64/*#define STMMAC_RX_DEBUG*/
65#ifdef STMMAC_RX_DEBUG
66#define RX_DBG(fmt, args...) printk(fmt, ## args)
67#else
68#define RX_DBG(fmt, args...) do { } while (0)
69#endif
70
71#undef STMMAC_XMIT_DEBUG
72/*#define STMMAC_XMIT_DEBUG*/
73#ifdef STMMAC_TX_DEBUG
74#define TX_DBG(fmt, args...) printk(fmt, ## args)
75#else
76#define TX_DBG(fmt, args...) do { } while (0)
77#endif
78
79#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
80#define JUMBO_LEN 9000
81
82/* Module parameters */
83#define TX_TIMEO 5000 /* default 5 seconds */
84static int watchdog = TX_TIMEO;
85module_param(watchdog, int, S_IRUGO | S_IWUSR);
86MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
87
88static int debug = -1; /* -1: default, 0: no output, 16: all */
89module_param(debug, int, S_IRUGO | S_IWUSR);
90MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
91
92static int phyaddr = -1;
93module_param(phyaddr, int, S_IRUGO);
94MODULE_PARM_DESC(phyaddr, "Physical device address");
95
96#define DMA_TX_SIZE 256
97static int dma_txsize = DMA_TX_SIZE;
98module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
99MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
100
101#define DMA_RX_SIZE 256
102static int dma_rxsize = DMA_RX_SIZE;
103module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
105
106static int flow_ctrl = FLOW_OFF;
107module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
108MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
109
110static int pause = PAUSE_TIME;
111module_param(pause, int, S_IRUGO | S_IWUSR);
112MODULE_PARM_DESC(pause, "Flow Control Pause Time");
113
114#define TC_DEFAULT 64
115static int tc = TC_DEFAULT;
116module_param(tc, int, S_IRUGO | S_IWUSR);
117MODULE_PARM_DESC(tc, "DMA threshold control value");
118
119#define RX_NO_COALESCE 1 /* Always interrupt on completion */
120#define TX_NO_COALESCE -1 /* No moderation by default */
121
122/* Pay attention to tune this parameter; take care of both
123 * hardware capability and network stabitily/performance impact.
124 * Many tests showed that ~4ms latency seems to be good enough. */
125#ifdef CONFIG_STMMAC_TIMER
126#define DEFAULT_PERIODIC_RATE 256
127static int tmrate = DEFAULT_PERIODIC_RATE;
128module_param(tmrate, int, S_IRUGO | S_IWUSR);
129MODULE_PARM_DESC(tmrate, "External timer freq. (default: 256Hz)");
130#endif
131
132#define DMA_BUFFER_SIZE BUF_SIZE_2KiB
133static int buf_sz = DMA_BUFFER_SIZE;
134module_param(buf_sz, int, S_IRUGO | S_IWUSR);
135MODULE_PARM_DESC(buf_sz, "DMA buffer size");
136
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700137static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
138 NETIF_MSG_LINK | NETIF_MSG_IFUP |
139 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
140
141static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700142
143/**
144 * stmmac_verify_args - verify the driver parameters.
145 * Description: it verifies if some wrong parameter is passed to the driver.
146 * Note that wrong parameters are replaced with the default values.
147 */
148static void stmmac_verify_args(void)
149{
150 if (unlikely(watchdog < 0))
151 watchdog = TX_TIMEO;
152 if (unlikely(dma_rxsize < 0))
153 dma_rxsize = DMA_RX_SIZE;
154 if (unlikely(dma_txsize < 0))
155 dma_txsize = DMA_TX_SIZE;
156 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
157 buf_sz = DMA_BUFFER_SIZE;
158 if (unlikely(flow_ctrl > 1))
159 flow_ctrl = FLOW_AUTO;
160 else if (likely(flow_ctrl < 0))
161 flow_ctrl = FLOW_OFF;
162 if (unlikely((pause < 0) || (pause > 0xffff)))
163 pause = PAUSE_TIME;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700164}
165
166#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
167static void print_pkt(unsigned char *buf, int len)
168{
169 int j;
170 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
171 for (j = 0; j < len; j++) {
172 if ((j % 16) == 0)
173 pr_info("\n %03x:", j);
174 pr_info(" %02x", buf[j]);
175 }
176 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700177}
178#endif
179
180/* minimum number of free TX descriptors required to wake up TX process */
181#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
182
183static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
184{
185 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
186}
187
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000188/* On some ST platforms, some HW system configuraton registers have to be
189 * set according to the link speed negotiated.
190 */
191static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
192{
193 struct phy_device *phydev = priv->phydev;
194
195 if (likely(priv->plat->fix_mac_speed))
196 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
197 phydev->speed);
198}
199
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700200/**
201 * stmmac_adjust_link
202 * @dev: net device structure
203 * Description: it adjusts the link parameters.
204 */
205static void stmmac_adjust_link(struct net_device *dev)
206{
207 struct stmmac_priv *priv = netdev_priv(dev);
208 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700209 unsigned long flags;
210 int new_state = 0;
211 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
212
213 if (phydev == NULL)
214 return;
215
216 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
217 phydev->addr, phydev->link);
218
219 spin_lock_irqsave(&priv->lock, flags);
220 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000221 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700222
223 /* Now we make sure that we can be in full duplex mode.
224 * If not, we operate in half-duplex mode. */
225 if (phydev->duplex != priv->oldduplex) {
226 new_state = 1;
227 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000228 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700229 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000230 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700231 priv->oldduplex = phydev->duplex;
232 }
233 /* Flow Control operation */
234 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000235 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000236 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700237
238 if (phydev->speed != priv->speed) {
239 new_state = 1;
240 switch (phydev->speed) {
241 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000242 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000243 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000244 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700245 break;
246 case 100:
247 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000248 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000249 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700250 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000251 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700252 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000253 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700254 }
255 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000256 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700257 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000258 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700259 break;
260 default:
261 if (netif_msg_link(priv))
262 pr_warning("%s: Speed (%d) is not 10"
263 " or 100!\n", dev->name, phydev->speed);
264 break;
265 }
266
267 priv->speed = phydev->speed;
268 }
269
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000270 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700271
272 if (!priv->oldlink) {
273 new_state = 1;
274 priv->oldlink = 1;
275 }
276 } else if (priv->oldlink) {
277 new_state = 1;
278 priv->oldlink = 0;
279 priv->speed = 0;
280 priv->oldduplex = -1;
281 }
282
283 if (new_state && netif_msg_link(priv))
284 phy_print_status(phydev);
285
286 spin_unlock_irqrestore(&priv->lock, flags);
287
288 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
289}
290
291/**
292 * stmmac_init_phy - PHY initialization
293 * @dev: net device structure
294 * Description: it initializes the driver's PHY state, and attaches the PHY
295 * to the mac driver.
296 * Return value:
297 * 0 on success
298 */
299static int stmmac_init_phy(struct net_device *dev)
300{
301 struct stmmac_priv *priv = netdev_priv(dev);
302 struct phy_device *phydev;
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000303 char phy_id[MII_BUS_ID_SIZE + 3];
304 char bus_id[MII_BUS_ID_SIZE];
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700305
306 priv->oldlink = 0;
307 priv->speed = 0;
308 priv->oldduplex = -1;
309
310 if (priv->phy_addr == -1) {
311 /* We don't have a PHY, so do nothing */
312 return 0;
313 }
314
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000315 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000316 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
317 priv->phy_addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700318 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
319
320 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
321 priv->phy_interface);
322
323 if (IS_ERR(phydev)) {
324 pr_err("%s: Could not attach to PHY\n", dev->name);
325 return PTR_ERR(phydev);
326 }
327
328 /*
329 * Broken HW is sometimes missing the pull-up resistor on the
330 * MDIO line, which results in reads to non-existent devices returning
331 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
332 * device as well.
333 * Note: phydev->phy_id is the result of reading the UID PHY registers.
334 */
335 if (phydev->phy_id == 0) {
336 phy_disconnect(phydev);
337 return -ENODEV;
338 }
339 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
340 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
341
342 priv->phydev = phydev;
343
344 return 0;
345}
346
avisconti19449bf2010-10-25 18:58:14 +0000347static inline void stmmac_enable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700348{
349 u32 value = readl(ioaddr + MAC_CTRL_REG);
avisconti19449bf2010-10-25 18:58:14 +0000350
351 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700352 writel(value, ioaddr + MAC_CTRL_REG);
353}
354
avisconti19449bf2010-10-25 18:58:14 +0000355static inline void stmmac_disable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700356{
357 u32 value = readl(ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700358
avisconti19449bf2010-10-25 18:58:14 +0000359 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700360 writel(value, ioaddr + MAC_CTRL_REG);
361}
362
363/**
364 * display_ring
365 * @p: pointer to the ring.
366 * @size: size of the ring.
367 * Description: display all the descriptors within the ring.
368 */
369static void display_ring(struct dma_desc *p, int size)
370{
371 struct tmp_s {
372 u64 a;
373 unsigned int b;
374 unsigned int c;
375 };
376 int i;
377 for (i = 0; i < size; i++) {
378 struct tmp_s *x = (struct tmp_s *)(p + i);
379 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
380 i, (unsigned int)virt_to_phys(&p[i]),
381 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
382 x->b, x->c);
383 pr_info("\n");
384 }
385}
386
387/**
388 * init_dma_desc_rings - init the RX/TX descriptor rings
389 * @dev: net device structure
390 * Description: this function initializes the DMA RX/TX descriptors
391 * and allocates the socket buffers.
392 */
393static void init_dma_desc_rings(struct net_device *dev)
394{
395 int i;
396 struct stmmac_priv *priv = netdev_priv(dev);
397 struct sk_buff *skb;
398 unsigned int txsize = priv->dma_tx_size;
399 unsigned int rxsize = priv->dma_rx_size;
400 unsigned int bfsize = priv->dma_buf_sz;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000401 int buff2_needed = 0, dis_ic = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700402
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700403 /* Set the Buffer size according to the MTU;
404 * indeed, in case of jumbo we need to bump-up the buffer sizes.
405 */
406 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
407 bfsize = BUF_SIZE_16KiB;
408 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
409 bfsize = BUF_SIZE_8KiB;
410 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
411 bfsize = BUF_SIZE_4KiB;
412 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
413 bfsize = BUF_SIZE_2KiB;
414 else
415 bfsize = DMA_BUFFER_SIZE;
416
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000417#ifdef CONFIG_STMMAC_TIMER
418 /* Disable interrupts on completion for the reception if timer is on */
419 if (likely(priv->tm->enable))
420 dis_ic = 1;
421#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700422 /* If the MTU exceeds 8k so use the second buffer in the chain */
423 if (bfsize >= BUF_SIZE_8KiB)
424 buff2_needed = 1;
425
426 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
427 txsize, rxsize, bfsize);
428
429 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
430 priv->rx_skbuff =
431 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
432 priv->dma_rx =
433 (struct dma_desc *)dma_alloc_coherent(priv->device,
434 rxsize *
435 sizeof(struct dma_desc),
436 &priv->dma_rx_phy,
437 GFP_KERNEL);
438 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
439 GFP_KERNEL);
440 priv->dma_tx =
441 (struct dma_desc *)dma_alloc_coherent(priv->device,
442 txsize *
443 sizeof(struct dma_desc),
444 &priv->dma_tx_phy,
445 GFP_KERNEL);
446
447 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
448 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
449 return;
450 }
451
452 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
453 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
454 dev->name, priv->dma_rx, priv->dma_tx,
455 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
456
457 /* RX INITIALIZATION */
458 DBG(probe, INFO, "stmmac: SKB addresses:\n"
459 "skb\t\tskb data\tdma data\n");
460
461 for (i = 0; i < rxsize; i++) {
462 struct dma_desc *p = priv->dma_rx + i;
463
464 skb = netdev_alloc_skb_ip_align(dev, bfsize);
465 if (unlikely(skb == NULL)) {
466 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
467 break;
468 }
469 priv->rx_skbuff[i] = skb;
470 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
471 bfsize, DMA_FROM_DEVICE);
472
473 p->des2 = priv->rx_skbuff_dma[i];
474 if (unlikely(buff2_needed))
475 p->des3 = p->des2 + BUF_SIZE_8KiB;
476 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
477 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
478 }
479 priv->cur_rx = 0;
480 priv->dirty_rx = (unsigned int)(i - rxsize);
481 priv->dma_buf_sz = bfsize;
482 buf_sz = bfsize;
483
484 /* TX INITIALIZATION */
485 for (i = 0; i < txsize; i++) {
486 priv->tx_skbuff[i] = NULL;
487 priv->dma_tx[i].des2 = 0;
488 }
489 priv->dirty_tx = 0;
490 priv->cur_tx = 0;
491
492 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000493 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
494 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700495
496 if (netif_msg_hw(priv)) {
497 pr_info("RX descriptor ring:\n");
498 display_ring(priv->dma_rx, rxsize);
499 pr_info("TX descriptor ring:\n");
500 display_ring(priv->dma_tx, txsize);
501 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700502}
503
504static void dma_free_rx_skbufs(struct stmmac_priv *priv)
505{
506 int i;
507
508 for (i = 0; i < priv->dma_rx_size; i++) {
509 if (priv->rx_skbuff[i]) {
510 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
511 priv->dma_buf_sz, DMA_FROM_DEVICE);
512 dev_kfree_skb_any(priv->rx_skbuff[i]);
513 }
514 priv->rx_skbuff[i] = NULL;
515 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700516}
517
518static void dma_free_tx_skbufs(struct stmmac_priv *priv)
519{
520 int i;
521
522 for (i = 0; i < priv->dma_tx_size; i++) {
523 if (priv->tx_skbuff[i] != NULL) {
524 struct dma_desc *p = priv->dma_tx + i;
525 if (p->des2)
526 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000527 priv->hw->desc->get_tx_len(p),
528 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700529 dev_kfree_skb_any(priv->tx_skbuff[i]);
530 priv->tx_skbuff[i] = NULL;
531 }
532 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700533}
534
535static void free_dma_desc_resources(struct stmmac_priv *priv)
536{
537 /* Release the DMA TX/RX socket buffers */
538 dma_free_rx_skbufs(priv);
539 dma_free_tx_skbufs(priv);
540
541 /* Free the region of consistent memory previously allocated for
542 * the DMA */
543 dma_free_coherent(priv->device,
544 priv->dma_tx_size * sizeof(struct dma_desc),
545 priv->dma_tx, priv->dma_tx_phy);
546 dma_free_coherent(priv->device,
547 priv->dma_rx_size * sizeof(struct dma_desc),
548 priv->dma_rx, priv->dma_rx_phy);
549 kfree(priv->rx_skbuff_dma);
550 kfree(priv->rx_skbuff);
551 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700552}
553
554/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700555 * stmmac_dma_operation_mode - HW DMA operation mode
556 * @priv : pointer to the private device structure.
557 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000558 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700559 */
560static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
561{
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000562 if (likely((priv->plat->tx_coe) && (!priv->no_csum_insertion))) {
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000563 /* In case of GMAC, SF mode has to be enabled
564 * to perform the TX COE. This depends on:
565 * 1) TX COE if actually supported
566 * 2) There is no bugged Jumbo frame support
567 * that needs to not insert csum in the TDES.
568 */
569 priv->hw->dma->dma_mode(priv->ioaddr,
570 SF_DMA_MODE, SF_DMA_MODE);
571 tc = SF_DMA_MODE;
572 } else
573 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700574}
575
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700576/**
577 * stmmac_tx:
578 * @priv: private driver structure
579 * Description: it reclaims resources after transmission completes.
580 */
581static void stmmac_tx(struct stmmac_priv *priv)
582{
583 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700584
585 while (priv->dirty_tx != priv->cur_tx) {
586 int last;
587 unsigned int entry = priv->dirty_tx % txsize;
588 struct sk_buff *skb = priv->tx_skbuff[entry];
589 struct dma_desc *p = priv->dma_tx + entry;
590
591 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000592 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700593 break;
594
595 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000596 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700597 if (likely(last)) {
598 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000599 priv->hw->desc->tx_status(&priv->dev->stats,
600 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000601 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700602 if (likely(tx_error == 0)) {
603 priv->dev->stats.tx_packets++;
604 priv->xstats.tx_pkt_n++;
605 } else
606 priv->dev->stats.tx_errors++;
607 }
608 TX_DBG("%s: curr %d, dirty %d\n", __func__,
609 priv->cur_tx, priv->dirty_tx);
610
611 if (likely(p->des2))
612 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000613 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700614 DMA_TO_DEVICE);
615 if (unlikely(p->des3))
616 p->des3 = 0;
617
618 if (likely(skb != NULL)) {
619 /*
620 * If there's room in the queue (limit it to size)
621 * we add this skb back into the pool,
622 * if it's the right size.
623 */
624 if ((skb_queue_len(&priv->rx_recycle) <
625 priv->dma_rx_size) &&
626 skb_recycle_check(skb, priv->dma_buf_sz))
627 __skb_queue_head(&priv->rx_recycle, skb);
628 else
629 dev_kfree_skb(skb);
630
631 priv->tx_skbuff[entry] = NULL;
632 }
633
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000634 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700635
636 entry = (++priv->dirty_tx) % txsize;
637 }
638 if (unlikely(netif_queue_stopped(priv->dev) &&
639 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
640 netif_tx_lock(priv->dev);
641 if (netif_queue_stopped(priv->dev) &&
642 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
643 TX_DBG("%s: restart transmit\n", __func__);
644 netif_wake_queue(priv->dev);
645 }
646 netif_tx_unlock(priv->dev);
647 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700648}
649
650static inline void stmmac_enable_irq(struct stmmac_priv *priv)
651{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000652#ifdef CONFIG_STMMAC_TIMER
653 if (likely(priv->tm->enable))
654 priv->tm->timer_start(tmrate);
655 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700656#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000657 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700658}
659
660static inline void stmmac_disable_irq(struct stmmac_priv *priv)
661{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000662#ifdef CONFIG_STMMAC_TIMER
663 if (likely(priv->tm->enable))
664 priv->tm->timer_stop();
665 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700666#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000667 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700668}
669
670static int stmmac_has_work(struct stmmac_priv *priv)
671{
672 unsigned int has_work = 0;
673 int rxret, tx_work = 0;
674
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000675 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700676 (priv->cur_rx % priv->dma_rx_size));
677
678 if (priv->dirty_tx != priv->cur_tx)
679 tx_work = 1;
680
681 if (likely(!rxret || tx_work))
682 has_work = 1;
683
684 return has_work;
685}
686
687static inline void _stmmac_schedule(struct stmmac_priv *priv)
688{
689 if (likely(stmmac_has_work(priv))) {
690 stmmac_disable_irq(priv);
691 napi_schedule(&priv->napi);
692 }
693}
694
695#ifdef CONFIG_STMMAC_TIMER
696void stmmac_schedule(struct net_device *dev)
697{
698 struct stmmac_priv *priv = netdev_priv(dev);
699
700 priv->xstats.sched_timer_n++;
701
702 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700703}
704
705static void stmmac_no_timer_started(unsigned int x)
706{;
707};
708
709static void stmmac_no_timer_stopped(void)
710{;
711};
712#endif
713
714/**
715 * stmmac_tx_err:
716 * @priv: pointer to the private device structure
717 * Description: it cleans the descriptors and restarts the transmission
718 * in case of errors.
719 */
720static void stmmac_tx_err(struct stmmac_priv *priv)
721{
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000722
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700723 netif_stop_queue(priv->dev);
724
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000725 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700726 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000727 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700728 priv->dirty_tx = 0;
729 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000730 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700731
732 priv->dev->stats.tx_errors++;
733 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700734}
735
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000736
737static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700738{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000739 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700740
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000741 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000742 if (likely(status == handle_tx_rx))
743 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700744
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000745 else if (unlikely(status == tx_hard_error_bump_tc)) {
746 /* Try to bump up the dma threshold on this failure */
747 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
748 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000749 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000750 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700751 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000752 stmmac_tx_err(priv);
753 } else if (unlikely(status == tx_hard_error))
754 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700755}
756
757/**
758 * stmmac_open - open entry point of the driver
759 * @dev : pointer to the device structure.
760 * Description:
761 * This function is the open entry point of the driver.
762 * Return value:
763 * 0 on success and an appropriate (-)ve integer as defined in errno.h
764 * file on failure.
765 */
766static int stmmac_open(struct net_device *dev)
767{
768 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700769 int ret;
770
771 /* Check that the MAC address is valid. If its not, refuse
772 * to bring the device up. The user must specify an
773 * address using the following linux command:
774 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
775 if (!is_valid_ether_addr(dev->dev_addr)) {
776 random_ether_addr(dev->dev_addr);
777 pr_warning("%s: generated random MAC address %pM\n", dev->name,
778 dev->dev_addr);
779 }
780
781 stmmac_verify_args();
782
783 ret = stmmac_init_phy(dev);
784 if (unlikely(ret)) {
785 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
786 return ret;
787 }
788
789 /* Request the IRQ lines */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800790 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700791 IRQF_SHARED, dev->name, dev);
792 if (unlikely(ret < 0)) {
793 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
794 __func__, dev->irq, ret);
795 return ret;
796 }
797
798#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000799 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700800 if (unlikely(priv->tm == NULL)) {
Frans Pop2381a552010-03-24 07:57:36 +0000801 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700802 return -ENOMEM;
803 }
804 priv->tm->freq = tmrate;
805
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000806 /* Test if the external timer can be actually used.
807 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700808 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000809 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700810 priv->tm->freq = 0;
811 priv->tm->timer_start = stmmac_no_timer_started;
812 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000813 } else
814 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700815#endif
816
817 /* Create and initialize the TX/RX descriptors chains. */
818 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
819 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
820 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
821 init_dma_desc_rings(dev);
822
823 /* DMA initialization and SW reset */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000824 if (unlikely(priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000825 priv->dma_tx_phy,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000826 priv->dma_rx_phy) < 0)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700827
828 pr_err("%s: DMA initialization failed\n", __func__);
829 return -1;
830 }
831
832 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000833 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +0000834 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000835 if (priv->plat->bus_setup)
836 priv->plat->bus_setup(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700837 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000838 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700839
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000840 priv->rx_coe = priv->hw->mac->rx_coe(priv->ioaddr);
841 if (priv->rx_coe)
842 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000843 if (priv->plat->tx_coe)
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000844 pr_info("\tTX Checksum insertion supported\n");
Michał Mirosław5e982f32011-04-09 02:46:55 +0000845 netdev_update_features(dev);
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000846
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700847 /* Initialise the MMC (if present) to disable all interrupts. */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000848 writel(0xffffffff, priv->ioaddr + MMC_HIGH_INTR_MASK);
849 writel(0xffffffff, priv->ioaddr + MMC_LOW_INTR_MASK);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700850
851 /* Enable the MAC Rx/Tx */
avisconti19449bf2010-10-25 18:58:14 +0000852 stmmac_enable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700853
854 /* Set the HW DMA mode and the COE */
855 stmmac_dma_operation_mode(priv);
856
857 /* Extra statistics */
858 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
859 priv->xstats.threshold = tc;
860
861 /* Start the ball rolling... */
862 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000863 priv->hw->dma->start_tx(priv->ioaddr);
864 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700865
866#ifdef CONFIG_STMMAC_TIMER
867 priv->tm->timer_start(tmrate);
868#endif
869 /* Dump DMA/MAC registers */
870 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000871 priv->hw->mac->dump_regs(priv->ioaddr);
872 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700873 }
874
875 if (priv->phydev)
876 phy_start(priv->phydev);
877
878 napi_enable(&priv->napi);
879 skb_queue_head_init(&priv->rx_recycle);
880 netif_start_queue(dev);
881 return 0;
882}
883
884/**
885 * stmmac_release - close entry point of the driver
886 * @dev : device pointer.
887 * Description:
888 * This is the stop entry point of the driver.
889 */
890static int stmmac_release(struct net_device *dev)
891{
892 struct stmmac_priv *priv = netdev_priv(dev);
893
894 /* Stop and disconnect the PHY */
895 if (priv->phydev) {
896 phy_stop(priv->phydev);
897 phy_disconnect(priv->phydev);
898 priv->phydev = NULL;
899 }
900
901 netif_stop_queue(dev);
902
903#ifdef CONFIG_STMMAC_TIMER
904 /* Stop and release the timer */
905 stmmac_close_ext_timer();
906 if (priv->tm != NULL)
907 kfree(priv->tm);
908#endif
909 napi_disable(&priv->napi);
910 skb_queue_purge(&priv->rx_recycle);
911
912 /* Free the IRQ lines */
913 free_irq(dev->irq, dev);
914
915 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000916 priv->hw->dma->stop_tx(priv->ioaddr);
917 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700918
919 /* Release and free the Rx/Tx resources */
920 free_dma_desc_resources(priv);
921
avisconti19449bf2010-10-25 18:58:14 +0000922 /* Disable the MAC Rx/Tx */
923 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700924
925 netif_carrier_off(dev);
926
927 return 0;
928}
929
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700930static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
931 struct net_device *dev,
932 int csum_insertion)
933{
934 struct stmmac_priv *priv = netdev_priv(dev);
935 unsigned int nopaged_len = skb_headlen(skb);
936 unsigned int txsize = priv->dma_tx_size;
937 unsigned int entry = priv->cur_tx % txsize;
938 struct dma_desc *desc = priv->dma_tx + entry;
939
940 if (nopaged_len > BUF_SIZE_8KiB) {
941
942 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
943
944 desc->des2 = dma_map_single(priv->device, skb->data,
945 BUF_SIZE_8KiB, DMA_TO_DEVICE);
946 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000947 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
948 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700949
950 entry = (++priv->cur_tx) % txsize;
951 desc = priv->dma_tx + entry;
952
953 desc->des2 = dma_map_single(priv->device,
954 skb->data + BUF_SIZE_8KiB,
955 buf2_size, DMA_TO_DEVICE);
956 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000957 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
958 csum_insertion);
959 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700960 priv->tx_skbuff[entry] = NULL;
961 } else {
962 desc->des2 = dma_map_single(priv->device, skb->data,
963 nopaged_len, DMA_TO_DEVICE);
964 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000965 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
966 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700967 }
968 return entry;
969}
970
971/**
972 * stmmac_xmit:
973 * @skb : the socket buffer
974 * @dev : device pointer
975 * Description : Tx entry point of the driver.
976 */
977static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
978{
979 struct stmmac_priv *priv = netdev_priv(dev);
980 unsigned int txsize = priv->dma_tx_size;
981 unsigned int entry;
982 int i, csum_insertion = 0;
983 int nfrags = skb_shinfo(skb)->nr_frags;
984 struct dma_desc *desc, *first;
985
986 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
987 if (!netif_queue_stopped(dev)) {
988 netif_stop_queue(dev);
989 /* This is a hard error, log it. */
990 pr_err("%s: BUG! Tx Ring full when queue awake\n",
991 __func__);
992 }
993 return NETDEV_TX_BUSY;
994 }
995
996 entry = priv->cur_tx % txsize;
997
998#ifdef STMMAC_XMIT_DEBUG
999 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1000 pr_info("stmmac xmit:\n"
1001 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1002 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1003 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1004 !skb_is_gso(skb) ? "isn't" : "is");
1005#endif
1006
Michał Mirosław5e982f32011-04-09 02:46:55 +00001007 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001008
1009 desc = priv->dma_tx + entry;
1010 first = desc;
1011
1012#ifdef STMMAC_XMIT_DEBUG
1013 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1014 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1015 "\t\tn_frags: %d, ip_summed: %d\n",
1016 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1017#endif
1018 priv->tx_skbuff[entry] = skb;
1019 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1020 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1021 desc = priv->dma_tx + entry;
1022 } else {
1023 unsigned int nopaged_len = skb_headlen(skb);
1024 desc->des2 = dma_map_single(priv->device, skb->data,
1025 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001026 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1027 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001028 }
1029
1030 for (i = 0; i < nfrags; i++) {
1031 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1032 int len = frag->size;
1033
1034 entry = (++priv->cur_tx) % txsize;
1035 desc = priv->dma_tx + entry;
1036
1037 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1038 desc->des2 = dma_map_page(priv->device, frag->page,
1039 frag->page_offset,
1040 len, DMA_TO_DEVICE);
1041 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001042 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
1043 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001044 }
1045
1046 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001047 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001048
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001049#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001050 /* Clean IC while using timer */
1051 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001052 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001053#endif
1054 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001055 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001056
1057 priv->cur_tx++;
1058
1059#ifdef STMMAC_XMIT_DEBUG
1060 if (netif_msg_pktdata(priv)) {
1061 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1062 "first=%p, nfrags=%d\n",
1063 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1064 entry, first, nfrags);
1065 display_ring(priv->dma_tx, txsize);
1066 pr_info(">>> frame to be transmitted: ");
1067 print_pkt(skb->data, skb->len);
1068 }
1069#endif
1070 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1071 TX_DBG("%s: stop transmitted packets\n", __func__);
1072 netif_stop_queue(dev);
1073 }
1074
1075 dev->stats.tx_bytes += skb->len;
1076
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001077 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001078
1079 return NETDEV_TX_OK;
1080}
1081
1082static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1083{
1084 unsigned int rxsize = priv->dma_rx_size;
1085 int bfsize = priv->dma_buf_sz;
1086 struct dma_desc *p = priv->dma_rx;
1087
1088 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1089 unsigned int entry = priv->dirty_rx % rxsize;
1090 if (likely(priv->rx_skbuff[entry] == NULL)) {
1091 struct sk_buff *skb;
1092
1093 skb = __skb_dequeue(&priv->rx_recycle);
1094 if (skb == NULL)
1095 skb = netdev_alloc_skb_ip_align(priv->dev,
1096 bfsize);
1097
1098 if (unlikely(skb == NULL))
1099 break;
1100
1101 priv->rx_skbuff[entry] = skb;
1102 priv->rx_skbuff_dma[entry] =
1103 dma_map_single(priv->device, skb->data, bfsize,
1104 DMA_FROM_DEVICE);
1105
1106 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001107 if (unlikely(priv->plat->has_gmac)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001108 if (bfsize >= BUF_SIZE_8KiB)
1109 (p + entry)->des3 =
1110 (p + entry)->des2 + BUF_SIZE_8KiB;
1111 }
1112 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1113 }
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001114 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001115 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001116}
1117
1118static int stmmac_rx(struct stmmac_priv *priv, int limit)
1119{
1120 unsigned int rxsize = priv->dma_rx_size;
1121 unsigned int entry = priv->cur_rx % rxsize;
1122 unsigned int next_entry;
1123 unsigned int count = 0;
1124 struct dma_desc *p = priv->dma_rx + entry;
1125 struct dma_desc *p_next;
1126
1127#ifdef STMMAC_RX_DEBUG
1128 if (netif_msg_hw(priv)) {
1129 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1130 display_ring(priv->dma_rx, rxsize);
1131 }
1132#endif
1133 count = 0;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001134 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001135 int status;
1136
1137 if (count >= limit)
1138 break;
1139
1140 count++;
1141
1142 next_entry = (++priv->cur_rx) % rxsize;
1143 p_next = priv->dma_rx + next_entry;
1144 prefetch(p_next);
1145
1146 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001147 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1148 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001149 if (unlikely(status == discard_frame))
1150 priv->dev->stats.rx_errors++;
1151 else {
1152 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001153 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001154
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001155 frame_len = priv->hw->desc->get_rx_frame_len(p);
1156 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1157 * Type frames (LLC/LLC-SNAP) */
1158 if (unlikely(status != llc_snap))
1159 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001160#ifdef STMMAC_RX_DEBUG
1161 if (frame_len > ETH_FRAME_LEN)
1162 pr_debug("\tRX frame size %d, COE status: %d\n",
1163 frame_len, status);
1164
1165 if (netif_msg_hw(priv))
1166 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1167 p, entry, p->des2);
1168#endif
1169 skb = priv->rx_skbuff[entry];
1170 if (unlikely(!skb)) {
1171 pr_err("%s: Inconsistent Rx descriptor chain\n",
1172 priv->dev->name);
1173 priv->dev->stats.rx_dropped++;
1174 break;
1175 }
1176 prefetch(skb->data - NET_IP_ALIGN);
1177 priv->rx_skbuff[entry] = NULL;
1178
1179 skb_put(skb, frame_len);
1180 dma_unmap_single(priv->device,
1181 priv->rx_skbuff_dma[entry],
1182 priv->dma_buf_sz, DMA_FROM_DEVICE);
1183#ifdef STMMAC_RX_DEBUG
1184 if (netif_msg_pktdata(priv)) {
1185 pr_info(" frame received (%dbytes)", frame_len);
1186 print_pkt(skb->data, frame_len);
1187 }
1188#endif
1189 skb->protocol = eth_type_trans(skb, priv->dev);
1190
1191 if (unlikely(status == csum_none)) {
1192 /* always for the old mac 10/100 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001193 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001194 netif_receive_skb(skb);
1195 } else {
1196 skb->ip_summed = CHECKSUM_UNNECESSARY;
1197 napi_gro_receive(&priv->napi, skb);
1198 }
1199
1200 priv->dev->stats.rx_packets++;
1201 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001202 }
1203 entry = next_entry;
1204 p = p_next; /* use prefetched values */
1205 }
1206
1207 stmmac_rx_refill(priv);
1208
1209 priv->xstats.rx_pkt_n += count;
1210
1211 return count;
1212}
1213
1214/**
1215 * stmmac_poll - stmmac poll method (NAPI)
1216 * @napi : pointer to the napi structure.
1217 * @budget : maximum number of packets that the current CPU can receive from
1218 * all interfaces.
1219 * Description :
1220 * This function implements the the reception process.
1221 * Also it runs the TX completion thread
1222 */
1223static int stmmac_poll(struct napi_struct *napi, int budget)
1224{
1225 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1226 int work_done = 0;
1227
1228 priv->xstats.poll_n++;
1229 stmmac_tx(priv);
1230 work_done = stmmac_rx(priv, budget);
1231
1232 if (work_done < budget) {
1233 napi_complete(napi);
1234 stmmac_enable_irq(priv);
1235 }
1236 return work_done;
1237}
1238
1239/**
1240 * stmmac_tx_timeout
1241 * @dev : Pointer to net device structure
1242 * Description: this function is called when a packet transmission fails to
1243 * complete within a reasonable tmrate. The driver will mark the error in the
1244 * netdev structure and arrange for the device to be reset to a sane state
1245 * in order to transmit a new packet.
1246 */
1247static void stmmac_tx_timeout(struct net_device *dev)
1248{
1249 struct stmmac_priv *priv = netdev_priv(dev);
1250
1251 /* Clear Tx resources and restart transmitting again */
1252 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001253}
1254
1255/* Configuration changes (passed on by ifconfig) */
1256static int stmmac_config(struct net_device *dev, struct ifmap *map)
1257{
1258 if (dev->flags & IFF_UP) /* can't act on a running interface */
1259 return -EBUSY;
1260
1261 /* Don't allow changing the I/O address */
1262 if (map->base_addr != dev->base_addr) {
1263 pr_warning("%s: can't change I/O address\n", dev->name);
1264 return -EOPNOTSUPP;
1265 }
1266
1267 /* Don't allow changing the IRQ */
1268 if (map->irq != dev->irq) {
1269 pr_warning("%s: can't change IRQ number %d\n",
1270 dev->name, dev->irq);
1271 return -EOPNOTSUPP;
1272 }
1273
1274 /* ignore other fields */
1275 return 0;
1276}
1277
1278/**
1279 * stmmac_multicast_list - entry point for multicast addressing
1280 * @dev : pointer to the device structure
1281 * Description:
1282 * This function is a driver entry point which gets called by the kernel
1283 * whenever multicast addresses must be enabled/disabled.
1284 * Return value:
1285 * void.
1286 */
1287static void stmmac_multicast_list(struct net_device *dev)
1288{
1289 struct stmmac_priv *priv = netdev_priv(dev);
1290
1291 spin_lock(&priv->lock);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001292 priv->hw->mac->set_filter(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001293 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001294}
1295
1296/**
1297 * stmmac_change_mtu - entry point to change MTU size for the device.
1298 * @dev : device pointer.
1299 * @new_mtu : the new MTU size for the device.
1300 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1301 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1302 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1303 * Return value:
1304 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1305 * file on failure.
1306 */
1307static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1308{
1309 struct stmmac_priv *priv = netdev_priv(dev);
1310 int max_mtu;
1311
1312 if (netif_running(dev)) {
1313 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1314 return -EBUSY;
1315 }
1316
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001317 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001318 max_mtu = JUMBO_LEN;
1319 else
1320 max_mtu = ETH_DATA_LEN;
1321
1322 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1323 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1324 return -EINVAL;
1325 }
1326
Michał Mirosław5e982f32011-04-09 02:46:55 +00001327 dev->mtu = new_mtu;
1328 netdev_update_features(dev);
1329
1330 return 0;
1331}
1332
1333static u32 stmmac_fix_features(struct net_device *dev, u32 features)
1334{
1335 struct stmmac_priv *priv = netdev_priv(dev);
1336
1337 if (!priv->rx_coe)
1338 features &= ~NETIF_F_RXCSUM;
1339 if (!priv->plat->tx_coe)
1340 features &= ~NETIF_F_ALL_CSUM;
1341
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001342 /* Some GMAC devices have a bugged Jumbo frame support that
1343 * needs to have the Tx COE disabled for oversized frames
1344 * (due to limited buffer sizes). In this case we disable
1345 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001346 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1347 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001348
Michał Mirosław5e982f32011-04-09 02:46:55 +00001349 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001350}
1351
1352static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1353{
1354 struct net_device *dev = (struct net_device *)dev_id;
1355 struct stmmac_priv *priv = netdev_priv(dev);
1356
1357 if (unlikely(!dev)) {
1358 pr_err("%s: invalid dev pointer\n", __func__);
1359 return IRQ_NONE;
1360 }
1361
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001362 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001363 /* To handle GMAC own interrupts */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001364 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001365
1366 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001367
1368 return IRQ_HANDLED;
1369}
1370
1371#ifdef CONFIG_NET_POLL_CONTROLLER
1372/* Polling receive - used by NETCONSOLE and other diagnostic tools
1373 * to allow network I/O with interrupts disabled. */
1374static void stmmac_poll_controller(struct net_device *dev)
1375{
1376 disable_irq(dev->irq);
1377 stmmac_interrupt(dev->irq, dev);
1378 enable_irq(dev->irq);
1379}
1380#endif
1381
1382/**
1383 * stmmac_ioctl - Entry point for the Ioctl
1384 * @dev: Device pointer.
1385 * @rq: An IOCTL specefic structure, that can contain a pointer to
1386 * a proprietary structure used to pass information to the driver.
1387 * @cmd: IOCTL command
1388 * Description:
1389 * Currently there are no special functionality supported in IOCTL, just the
1390 * phy_mii_ioctl(...) can be invoked.
1391 */
1392static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1393{
1394 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001395 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001396
1397 if (!netif_running(dev))
1398 return -EINVAL;
1399
Richard Cochran28b04112010-07-17 08:48:55 +00001400 if (!priv->phydev)
1401 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001402
Richard Cochran28b04112010-07-17 08:48:55 +00001403 spin_lock(&priv->lock);
1404 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1405 spin_unlock(&priv->lock);
1406
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001407 return ret;
1408}
1409
1410#ifdef STMMAC_VLAN_TAG_USED
1411static void stmmac_vlan_rx_register(struct net_device *dev,
1412 struct vlan_group *grp)
1413{
1414 struct stmmac_priv *priv = netdev_priv(dev);
1415
1416 DBG(probe, INFO, "%s: Setting vlgrp to %p\n", dev->name, grp);
1417
1418 spin_lock(&priv->lock);
1419 priv->vlgrp = grp;
1420 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001421}
1422#endif
1423
1424static const struct net_device_ops stmmac_netdev_ops = {
1425 .ndo_open = stmmac_open,
1426 .ndo_start_xmit = stmmac_xmit,
1427 .ndo_stop = stmmac_release,
1428 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001429 .ndo_fix_features = stmmac_fix_features,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001430 .ndo_set_multicast_list = stmmac_multicast_list,
1431 .ndo_tx_timeout = stmmac_tx_timeout,
1432 .ndo_do_ioctl = stmmac_ioctl,
1433 .ndo_set_config = stmmac_config,
1434#ifdef STMMAC_VLAN_TAG_USED
1435 .ndo_vlan_rx_register = stmmac_vlan_rx_register,
1436#endif
1437#ifdef CONFIG_NET_POLL_CONTROLLER
1438 .ndo_poll_controller = stmmac_poll_controller,
1439#endif
1440 .ndo_set_mac_address = eth_mac_addr,
1441};
1442
1443/**
1444 * stmmac_probe - Initialization of the adapter .
1445 * @dev : device pointer
1446 * Description: The function initializes the network device structure for
1447 * the STMMAC driver. It also calls the low level routines
1448 * in order to init the HW (i.e. the DMA engine)
1449 */
1450static int stmmac_probe(struct net_device *dev)
1451{
1452 int ret = 0;
1453 struct stmmac_priv *priv = netdev_priv(dev);
1454
1455 ether_setup(dev);
1456
1457 dev->netdev_ops = &stmmac_netdev_ops;
1458 stmmac_set_ethtool_ops(dev);
1459
Michał Mirosław5e982f32011-04-09 02:46:55 +00001460 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1461 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001462 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1463#ifdef STMMAC_VLAN_TAG_USED
1464 /* Both mac100 and gmac support receive VLAN tag detection */
1465 dev->features |= NETIF_F_HW_VLAN_RX;
1466#endif
1467 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1468
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001469 if (flow_ctrl)
1470 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1471
1472 priv->pause = pause;
1473 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1474
1475 /* Get the MAC address */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001476 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1477 dev->dev_addr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001478
1479 if (!is_valid_ether_addr(dev->dev_addr))
1480 pr_warning("\tno valid MAC address;"
1481 "please, use ifconfig or nwhwconfig!\n");
1482
Vlad Lunguf8e96162010-11-29 22:52:52 +00001483 spin_lock_init(&priv->lock);
1484
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001485 ret = register_netdev(dev);
1486 if (ret) {
1487 pr_err("%s: ERROR %i registering the device\n",
1488 __func__, ret);
1489 return -ENODEV;
1490 }
1491
1492 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1493 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
Michał Mirosław79032642010-11-30 06:38:00 +00001494 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001495
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001496 return ret;
1497}
1498
1499/**
1500 * stmmac_mac_device_setup
1501 * @dev : device pointer
1502 * Description: select and initialise the mac device (mac100 or Gmac).
1503 */
1504static int stmmac_mac_device_setup(struct net_device *dev)
1505{
1506 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001507
1508 struct mac_device_info *device;
1509
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001510 if (priv->plat->has_gmac)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001511 device = dwmac1000_setup(priv->ioaddr);
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001512 else
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001513 device = dwmac100_setup(priv->ioaddr);
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001514
Dan Carpenter1ff21902010-07-22 01:16:48 +00001515 if (!device)
1516 return -ENOMEM;
1517
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001518 if (priv->plat->enh_desc) {
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001519 device->desc = &enh_desc_ops;
1520 pr_info("\tEnhanced descriptor structure\n");
1521 } else
Giuseppe CAVALLARO56b106a2010-04-13 20:21:12 +00001522 device->desc = &ndesc_ops;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001523
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001524 priv->hw = device;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001525
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001526 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001527 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001528 enable_irq_wake(dev->irq);
1529 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001530
1531 return 0;
1532}
1533
1534static int stmmacphy_dvr_probe(struct platform_device *pdev)
1535{
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001536 struct plat_stmmacphy_data *plat_dat = pdev->dev.platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001537
1538 pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
1539 plat_dat->bus_id);
1540
1541 return 0;
1542}
1543
1544static int stmmacphy_dvr_remove(struct platform_device *pdev)
1545{
1546 return 0;
1547}
1548
1549static struct platform_driver stmmacphy_driver = {
1550 .driver = {
1551 .name = PHY_RESOURCE_NAME,
1552 },
1553 .probe = stmmacphy_dvr_probe,
1554 .remove = stmmacphy_dvr_remove,
1555};
1556
1557/**
1558 * stmmac_associate_phy
1559 * @dev: pointer to device structure
1560 * @data: points to the private structure.
1561 * Description: Scans through all the PHYs we have registered and checks if
1562 * any are associated with our MAC. If so, then just fill in
1563 * the blanks in our local context structure
1564 */
1565static int stmmac_associate_phy(struct device *dev, void *data)
1566{
1567 struct stmmac_priv *priv = (struct stmmac_priv *)data;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001568 struct plat_stmmacphy_data *plat_dat = dev->platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001569
1570 DBG(probe, DEBUG, "%s: checking phy for bus %d\n", __func__,
1571 plat_dat->bus_id);
1572
1573 /* Check that this phy is for the MAC being initialised */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001574 if (priv->plat->bus_id != plat_dat->bus_id)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001575 return 0;
1576
1577 /* OK, this PHY is connected to the MAC.
1578 Go ahead and get the parameters */
1579 DBG(probe, DEBUG, "%s: OK. Found PHY config\n", __func__);
1580 priv->phy_irq =
1581 platform_get_irq_byname(to_platform_device(dev), "phyirq");
1582 DBG(probe, DEBUG, "%s: PHY irq on bus %d is %d\n", __func__,
1583 plat_dat->bus_id, priv->phy_irq);
1584
1585 /* Override with kernel parameters if supplied XXX CRS XXX
1586 * this needs to have multiple instances */
1587 if ((phyaddr >= 0) && (phyaddr <= 31))
1588 plat_dat->phy_addr = phyaddr;
1589
1590 priv->phy_addr = plat_dat->phy_addr;
1591 priv->phy_mask = plat_dat->phy_mask;
1592 priv->phy_interface = plat_dat->interface;
1593 priv->phy_reset = plat_dat->phy_reset;
1594
1595 DBG(probe, DEBUG, "%s: exiting\n", __func__);
1596 return 1; /* forces exit of driver_for_each_device() */
1597}
1598
1599/**
1600 * stmmac_dvr_probe
1601 * @pdev: platform device pointer
1602 * Description: the driver is initialized through platform_device.
1603 */
1604static int stmmac_dvr_probe(struct platform_device *pdev)
1605{
1606 int ret = 0;
1607 struct resource *res;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001608 void __iomem *addr = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001609 struct net_device *ndev = NULL;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001610 struct stmmac_priv *priv = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001611 struct plat_stmmacenet_data *plat_dat;
1612
1613 pr_info("STMMAC driver:\n\tplatform registration... ");
1614 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001615 if (!res)
1616 return -ENODEV;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001617 pr_info("\tdone!\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001618
Dan Carpenterb6222682010-04-07 21:50:08 -07001619 if (!request_mem_region(res->start, resource_size(res),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001620 pdev->name)) {
1621 pr_err("%s: ERROR: memory allocation failed"
1622 "cannot get the I/O addr 0x%x\n",
1623 __func__, (unsigned int)res->start);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001624 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001625 }
1626
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001627 addr = ioremap(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001628 if (!addr) {
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001629 pr_err("%s: ERROR: memory mapping failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001630 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001631 goto out_release_region;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001632 }
1633
1634 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1635 if (!ndev) {
1636 pr_err("%s: ERROR: allocating the device\n", __func__);
1637 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001638 goto out_unmap;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001639 }
1640
1641 SET_NETDEV_DEV(ndev, &pdev->dev);
1642
1643 /* Get the MAC information */
1644 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1645 if (ndev->irq == -ENXIO) {
1646 pr_err("%s: ERROR: MAC IRQ configuration "
1647 "information not found\n", __func__);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001648 ret = -ENXIO;
1649 goto out_free_ndev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001650 }
1651
1652 priv = netdev_priv(ndev);
1653 priv->device = &(pdev->dev);
1654 priv->dev = ndev;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001655 plat_dat = pdev->dev.platform_data;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001656
1657 priv->plat = plat_dat;
1658
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001659 priv->ioaddr = addr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001660
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001661 /* PMT module is not integrated in all the MAC devices. */
1662 if (plat_dat->pmt) {
1663 pr_info("\tPMT module supported\n");
1664 device_set_wakeup_capable(&pdev->dev, 1);
1665 }
1666
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001667 platform_set_drvdata(pdev, ndev);
1668
1669 /* Set the I/O base addr */
1670 ndev->base_addr = (unsigned long)addr;
1671
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001672 /* Custom initialisation */
1673 if (priv->plat->init) {
1674 ret = priv->plat->init(pdev);
1675 if (unlikely(ret))
Dan Carpenter34a52f32010-12-20 21:34:56 +00001676 goto out_free_ndev;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001677 }
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001678
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001679 /* MAC HW revice detection */
1680 ret = stmmac_mac_device_setup(ndev);
1681 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001682 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001683
1684 /* Network Device Registration */
1685 ret = stmmac_probe(ndev);
1686 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001687 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001688
1689 /* associate a PHY - it is provided by another platform bus */
1690 if (!driver_for_each_device
1691 (&(stmmacphy_driver.driver), NULL, (void *)priv,
1692 stmmac_associate_phy)) {
1693 pr_err("No PHY device is associated with this MAC!\n");
1694 ret = -ENODEV;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001695 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001696 }
1697
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001698 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
David S. Miller1f0f6382010-08-30 21:55:17 -07001699 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1700 pdev->id, ndev->irq, addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001701
1702 /* MDIO bus Registration */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001703 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001704 ret = stmmac_mdio_register(ndev);
1705 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001706 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001707 pr_debug("registered!\n");
Dan Carpenter34a52f32010-12-20 21:34:56 +00001708 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001709
Dan Carpenter34a52f32010-12-20 21:34:56 +00001710out_unregister:
1711 unregister_netdev(ndev);
1712out_plat_exit:
1713 if (priv->plat->exit)
1714 priv->plat->exit(pdev);
1715out_free_ndev:
1716 free_netdev(ndev);
1717 platform_set_drvdata(pdev, NULL);
1718out_unmap:
1719 iounmap(addr);
1720out_release_region:
1721 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001722
1723 return ret;
1724}
1725
1726/**
1727 * stmmac_dvr_remove
1728 * @pdev: platform device pointer
1729 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1730 * changes the link status, releases the DMA descriptor rings,
1731 * unregisters the MDIO bus and unmaps the allocated memory.
1732 */
1733static int stmmac_dvr_remove(struct platform_device *pdev)
1734{
1735 struct net_device *ndev = platform_get_drvdata(pdev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001736 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001737 struct resource *res;
1738
1739 pr_info("%s:\n\tremoving driver", __func__);
1740
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001741 priv->hw->dma->stop_rx(priv->ioaddr);
1742 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001743
avisconti19449bf2010-10-25 18:58:14 +00001744 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001745
1746 netif_carrier_off(ndev);
1747
1748 stmmac_mdio_unregister(ndev);
1749
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001750 if (priv->plat->exit)
1751 priv->plat->exit(pdev);
1752
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001753 platform_set_drvdata(pdev, NULL);
1754 unregister_netdev(ndev);
1755
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001756 iounmap((void *)priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001757 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001758 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001759
1760 free_netdev(ndev);
1761
1762 return 0;
1763}
1764
1765#ifdef CONFIG_PM
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001766static int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001767{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001768 struct net_device *ndev = dev_get_drvdata(dev);
1769 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001770 int dis_ic = 0;
1771
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001772 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001773 return 0;
1774
1775 spin_lock(&priv->lock);
1776
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001777 netif_device_detach(ndev);
1778 netif_stop_queue(ndev);
1779 if (priv->phydev)
1780 phy_stop(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001781
1782#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001783 priv->tm->timer_stop();
1784 if (likely(priv->tm->enable))
1785 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001786#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001787 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001788
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001789 /* Stop TX/RX DMA */
1790 priv->hw->dma->stop_tx(priv->ioaddr);
1791 priv->hw->dma->stop_rx(priv->ioaddr);
1792 /* Clear the Rx/Tx descriptors */
1793 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1794 dis_ic);
1795 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001796
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001797 /* Enable Power down mode by programming the PMT regs */
1798 if (device_may_wakeup(priv->device))
1799 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
1800 else
1801 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001802
1803 spin_unlock(&priv->lock);
1804 return 0;
1805}
1806
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001807static int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001808{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001809 struct net_device *ndev = dev_get_drvdata(dev);
1810 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001811
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001812 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001813 return 0;
1814
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02001815 spin_lock(&priv->lock);
1816
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001817 /* Power Down bit, into the PM register, is cleared
1818 * automatically as soon as a magic packet or a Wake-up frame
1819 * is received. Anyway, it's better to manually clear
1820 * this bit because it can generate problems while resuming
1821 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001822 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001823 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001824
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001825 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001826
1827 /* Enable the MAC and DMA */
avisconti19449bf2010-10-25 18:58:14 +00001828 stmmac_enable_mac(priv->ioaddr);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001829 priv->hw->dma->start_tx(priv->ioaddr);
1830 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001831
1832#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001833 if (likely(priv->tm->enable))
1834 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001835#endif
1836 napi_enable(&priv->napi);
1837
1838 if (priv->phydev)
1839 phy_start(priv->phydev);
1840
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001841 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001842
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001843 spin_unlock(&priv->lock);
1844 return 0;
1845}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001846
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001847static int stmmac_freeze(struct device *dev)
1848{
1849 struct net_device *ndev = dev_get_drvdata(dev);
1850
1851 if (!ndev || !netif_running(ndev))
1852 return 0;
1853
1854 return stmmac_release(ndev);
1855}
1856
1857static int stmmac_restore(struct device *dev)
1858{
1859 struct net_device *ndev = dev_get_drvdata(dev);
1860
1861 if (!ndev || !netif_running(ndev))
1862 return 0;
1863
1864 return stmmac_open(ndev);
1865}
1866
1867static const struct dev_pm_ops stmmac_pm_ops = {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001868 .suspend = stmmac_suspend,
1869 .resume = stmmac_resume,
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001870 .freeze = stmmac_freeze,
1871 .thaw = stmmac_restore,
1872 .restore = stmmac_restore,
1873};
1874#else
1875static const struct dev_pm_ops stmmac_pm_ops;
1876#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001877
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001878static struct platform_driver stmmac_driver = {
1879 .probe = stmmac_dvr_probe,
1880 .remove = stmmac_dvr_remove,
1881 .driver = {
1882 .name = STMMAC_RESOURCE_NAME,
1883 .owner = THIS_MODULE,
1884 .pm = &stmmac_pm_ops,
1885 },
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001886};
1887
1888/**
1889 * stmmac_init_module - Entry point for the driver
1890 * Description: This function is the entry point for the driver.
1891 */
1892static int __init stmmac_init_module(void)
1893{
1894 int ret;
1895
1896 if (platform_driver_register(&stmmacphy_driver)) {
1897 pr_err("No PHY devices registered!\n");
1898 return -ENODEV;
1899 }
1900
1901 ret = platform_driver_register(&stmmac_driver);
1902 return ret;
1903}
1904
1905/**
1906 * stmmac_cleanup_module - Cleanup routine for the driver
1907 * Description: This function is the cleanup routine for the driver.
1908 */
1909static void __exit stmmac_cleanup_module(void)
1910{
1911 platform_driver_unregister(&stmmacphy_driver);
1912 platform_driver_unregister(&stmmac_driver);
1913}
1914
1915#ifndef MODULE
1916static int __init stmmac_cmdline_opt(char *str)
1917{
1918 char *opt;
1919
1920 if (!str || !*str)
1921 return -EINVAL;
1922 while ((opt = strsep(&str, ",")) != NULL) {
1923 if (!strncmp(opt, "debug:", 6))
1924 strict_strtoul(opt + 6, 0, (unsigned long *)&debug);
1925 else if (!strncmp(opt, "phyaddr:", 8))
1926 strict_strtoul(opt + 8, 0, (unsigned long *)&phyaddr);
1927 else if (!strncmp(opt, "dma_txsize:", 11))
1928 strict_strtoul(opt + 11, 0,
1929 (unsigned long *)&dma_txsize);
1930 else if (!strncmp(opt, "dma_rxsize:", 11))
1931 strict_strtoul(opt + 11, 0,
1932 (unsigned long *)&dma_rxsize);
1933 else if (!strncmp(opt, "buf_sz:", 7))
1934 strict_strtoul(opt + 7, 0, (unsigned long *)&buf_sz);
1935 else if (!strncmp(opt, "tc:", 3))
1936 strict_strtoul(opt + 3, 0, (unsigned long *)&tc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001937 else if (!strncmp(opt, "watchdog:", 9))
1938 strict_strtoul(opt + 9, 0, (unsigned long *)&watchdog);
1939 else if (!strncmp(opt, "flow_ctrl:", 10))
1940 strict_strtoul(opt + 10, 0,
1941 (unsigned long *)&flow_ctrl);
1942 else if (!strncmp(opt, "pause:", 6))
1943 strict_strtoul(opt + 6, 0, (unsigned long *)&pause);
1944#ifdef CONFIG_STMMAC_TIMER
1945 else if (!strncmp(opt, "tmrate:", 7))
1946 strict_strtoul(opt + 7, 0, (unsigned long *)&tmrate);
1947#endif
1948 }
1949 return 0;
1950}
1951
1952__setup("stmmaceth=", stmmac_cmdline_opt);
1953#endif
1954
1955module_init(stmmac_init_module);
1956module_exit(stmmac_cleanup_module);
1957
1958MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
1959MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
1960MODULE_LICENSE("GPL");