blob: 5b85f4ca20044830f6e5b0f42cdf1f96dc44aca0 [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>
Paul Gortmaker70c71602011-05-22 16:47:17 -040048#include <linux/prefetch.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070049#include "stmmac.h"
50
51#define STMMAC_RESOURCE_NAME "stmmaceth"
52#define PHY_RESOURCE_NAME "stmmacphy"
53
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
308 if (priv->phy_addr == -1) {
309 /* We don't have a PHY, so do nothing */
310 return 0;
311 }
312
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000313 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000314 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
315 priv->phy_addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700316 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
317
318 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
319 priv->phy_interface);
320
321 if (IS_ERR(phydev)) {
322 pr_err("%s: Could not attach to PHY\n", dev->name);
323 return PTR_ERR(phydev);
324 }
325
326 /*
327 * Broken HW is sometimes missing the pull-up resistor on the
328 * MDIO line, which results in reads to non-existent devices returning
329 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
330 * device as well.
331 * Note: phydev->phy_id is the result of reading the UID PHY registers.
332 */
333 if (phydev->phy_id == 0) {
334 phy_disconnect(phydev);
335 return -ENODEV;
336 }
337 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
338 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
339
340 priv->phydev = phydev;
341
342 return 0;
343}
344
avisconti19449bf2010-10-25 18:58:14 +0000345static inline void stmmac_enable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700346{
347 u32 value = readl(ioaddr + MAC_CTRL_REG);
avisconti19449bf2010-10-25 18:58:14 +0000348
349 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700350 writel(value, ioaddr + MAC_CTRL_REG);
351}
352
avisconti19449bf2010-10-25 18:58:14 +0000353static inline void stmmac_disable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700354{
355 u32 value = readl(ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700356
avisconti19449bf2010-10-25 18:58:14 +0000357 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700358 writel(value, ioaddr + MAC_CTRL_REG);
359}
360
361/**
362 * display_ring
363 * @p: pointer to the ring.
364 * @size: size of the ring.
365 * Description: display all the descriptors within the ring.
366 */
367static void display_ring(struct dma_desc *p, int size)
368{
369 struct tmp_s {
370 u64 a;
371 unsigned int b;
372 unsigned int c;
373 };
374 int i;
375 for (i = 0; i < size; i++) {
376 struct tmp_s *x = (struct tmp_s *)(p + i);
377 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
378 i, (unsigned int)virt_to_phys(&p[i]),
379 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
380 x->b, x->c);
381 pr_info("\n");
382 }
383}
384
385/**
386 * init_dma_desc_rings - init the RX/TX descriptor rings
387 * @dev: net device structure
388 * Description: this function initializes the DMA RX/TX descriptors
389 * and allocates the socket buffers.
390 */
391static void init_dma_desc_rings(struct net_device *dev)
392{
393 int i;
394 struct stmmac_priv *priv = netdev_priv(dev);
395 struct sk_buff *skb;
396 unsigned int txsize = priv->dma_tx_size;
397 unsigned int rxsize = priv->dma_rx_size;
398 unsigned int bfsize = priv->dma_buf_sz;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000399 int buff2_needed = 0, dis_ic = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700400
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700401 /* Set the Buffer size according to the MTU;
402 * indeed, in case of jumbo we need to bump-up the buffer sizes.
403 */
404 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
405 bfsize = BUF_SIZE_16KiB;
406 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
407 bfsize = BUF_SIZE_8KiB;
408 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
409 bfsize = BUF_SIZE_4KiB;
410 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
411 bfsize = BUF_SIZE_2KiB;
412 else
413 bfsize = DMA_BUFFER_SIZE;
414
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000415#ifdef CONFIG_STMMAC_TIMER
416 /* Disable interrupts on completion for the reception if timer is on */
417 if (likely(priv->tm->enable))
418 dis_ic = 1;
419#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700420 /* If the MTU exceeds 8k so use the second buffer in the chain */
421 if (bfsize >= BUF_SIZE_8KiB)
422 buff2_needed = 1;
423
424 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
425 txsize, rxsize, bfsize);
426
427 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
428 priv->rx_skbuff =
429 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
430 priv->dma_rx =
431 (struct dma_desc *)dma_alloc_coherent(priv->device,
432 rxsize *
433 sizeof(struct dma_desc),
434 &priv->dma_rx_phy,
435 GFP_KERNEL);
436 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
437 GFP_KERNEL);
438 priv->dma_tx =
439 (struct dma_desc *)dma_alloc_coherent(priv->device,
440 txsize *
441 sizeof(struct dma_desc),
442 &priv->dma_tx_phy,
443 GFP_KERNEL);
444
445 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
446 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
447 return;
448 }
449
450 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
451 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
452 dev->name, priv->dma_rx, priv->dma_tx,
453 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
454
455 /* RX INITIALIZATION */
456 DBG(probe, INFO, "stmmac: SKB addresses:\n"
457 "skb\t\tskb data\tdma data\n");
458
459 for (i = 0; i < rxsize; i++) {
460 struct dma_desc *p = priv->dma_rx + i;
461
462 skb = netdev_alloc_skb_ip_align(dev, bfsize);
463 if (unlikely(skb == NULL)) {
464 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
465 break;
466 }
467 priv->rx_skbuff[i] = skb;
468 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
469 bfsize, DMA_FROM_DEVICE);
470
471 p->des2 = priv->rx_skbuff_dma[i];
472 if (unlikely(buff2_needed))
473 p->des3 = p->des2 + BUF_SIZE_8KiB;
474 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
475 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
476 }
477 priv->cur_rx = 0;
478 priv->dirty_rx = (unsigned int)(i - rxsize);
479 priv->dma_buf_sz = bfsize;
480 buf_sz = bfsize;
481
482 /* TX INITIALIZATION */
483 for (i = 0; i < txsize; i++) {
484 priv->tx_skbuff[i] = NULL;
485 priv->dma_tx[i].des2 = 0;
486 }
487 priv->dirty_tx = 0;
488 priv->cur_tx = 0;
489
490 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000491 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
492 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700493
494 if (netif_msg_hw(priv)) {
495 pr_info("RX descriptor ring:\n");
496 display_ring(priv->dma_rx, rxsize);
497 pr_info("TX descriptor ring:\n");
498 display_ring(priv->dma_tx, txsize);
499 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700500}
501
502static void dma_free_rx_skbufs(struct stmmac_priv *priv)
503{
504 int i;
505
506 for (i = 0; i < priv->dma_rx_size; i++) {
507 if (priv->rx_skbuff[i]) {
508 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
509 priv->dma_buf_sz, DMA_FROM_DEVICE);
510 dev_kfree_skb_any(priv->rx_skbuff[i]);
511 }
512 priv->rx_skbuff[i] = NULL;
513 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700514}
515
516static void dma_free_tx_skbufs(struct stmmac_priv *priv)
517{
518 int i;
519
520 for (i = 0; i < priv->dma_tx_size; i++) {
521 if (priv->tx_skbuff[i] != NULL) {
522 struct dma_desc *p = priv->dma_tx + i;
523 if (p->des2)
524 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000525 priv->hw->desc->get_tx_len(p),
526 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700527 dev_kfree_skb_any(priv->tx_skbuff[i]);
528 priv->tx_skbuff[i] = NULL;
529 }
530 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700531}
532
533static void free_dma_desc_resources(struct stmmac_priv *priv)
534{
535 /* Release the DMA TX/RX socket buffers */
536 dma_free_rx_skbufs(priv);
537 dma_free_tx_skbufs(priv);
538
539 /* Free the region of consistent memory previously allocated for
540 * the DMA */
541 dma_free_coherent(priv->device,
542 priv->dma_tx_size * sizeof(struct dma_desc),
543 priv->dma_tx, priv->dma_tx_phy);
544 dma_free_coherent(priv->device,
545 priv->dma_rx_size * sizeof(struct dma_desc),
546 priv->dma_rx, priv->dma_rx_phy);
547 kfree(priv->rx_skbuff_dma);
548 kfree(priv->rx_skbuff);
549 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700550}
551
552/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700553 * stmmac_dma_operation_mode - HW DMA operation mode
554 * @priv : pointer to the private device structure.
555 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000556 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700557 */
558static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
559{
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000560 if (likely((priv->plat->tx_coe) && (!priv->no_csum_insertion))) {
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000561 /* In case of GMAC, SF mode has to be enabled
562 * to perform the TX COE. This depends on:
563 * 1) TX COE if actually supported
564 * 2) There is no bugged Jumbo frame support
565 * that needs to not insert csum in the TDES.
566 */
567 priv->hw->dma->dma_mode(priv->ioaddr,
568 SF_DMA_MODE, SF_DMA_MODE);
569 tc = SF_DMA_MODE;
570 } else
571 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700572}
573
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700574/**
575 * stmmac_tx:
576 * @priv: private driver structure
577 * Description: it reclaims resources after transmission completes.
578 */
579static void stmmac_tx(struct stmmac_priv *priv)
580{
581 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700582
583 while (priv->dirty_tx != priv->cur_tx) {
584 int last;
585 unsigned int entry = priv->dirty_tx % txsize;
586 struct sk_buff *skb = priv->tx_skbuff[entry];
587 struct dma_desc *p = priv->dma_tx + entry;
588
589 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000590 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700591 break;
592
593 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000594 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700595 if (likely(last)) {
596 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000597 priv->hw->desc->tx_status(&priv->dev->stats,
598 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000599 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700600 if (likely(tx_error == 0)) {
601 priv->dev->stats.tx_packets++;
602 priv->xstats.tx_pkt_n++;
603 } else
604 priv->dev->stats.tx_errors++;
605 }
606 TX_DBG("%s: curr %d, dirty %d\n", __func__,
607 priv->cur_tx, priv->dirty_tx);
608
609 if (likely(p->des2))
610 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000611 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700612 DMA_TO_DEVICE);
613 if (unlikely(p->des3))
614 p->des3 = 0;
615
616 if (likely(skb != NULL)) {
617 /*
618 * If there's room in the queue (limit it to size)
619 * we add this skb back into the pool,
620 * if it's the right size.
621 */
622 if ((skb_queue_len(&priv->rx_recycle) <
623 priv->dma_rx_size) &&
624 skb_recycle_check(skb, priv->dma_buf_sz))
625 __skb_queue_head(&priv->rx_recycle, skb);
626 else
627 dev_kfree_skb(skb);
628
629 priv->tx_skbuff[entry] = NULL;
630 }
631
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000632 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700633
634 entry = (++priv->dirty_tx) % txsize;
635 }
636 if (unlikely(netif_queue_stopped(priv->dev) &&
637 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
638 netif_tx_lock(priv->dev);
639 if (netif_queue_stopped(priv->dev) &&
640 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
641 TX_DBG("%s: restart transmit\n", __func__);
642 netif_wake_queue(priv->dev);
643 }
644 netif_tx_unlock(priv->dev);
645 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700646}
647
648static inline void stmmac_enable_irq(struct stmmac_priv *priv)
649{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000650#ifdef CONFIG_STMMAC_TIMER
651 if (likely(priv->tm->enable))
652 priv->tm->timer_start(tmrate);
653 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700654#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000655 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700656}
657
658static inline void stmmac_disable_irq(struct stmmac_priv *priv)
659{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000660#ifdef CONFIG_STMMAC_TIMER
661 if (likely(priv->tm->enable))
662 priv->tm->timer_stop();
663 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700664#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000665 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700666}
667
668static int stmmac_has_work(struct stmmac_priv *priv)
669{
670 unsigned int has_work = 0;
671 int rxret, tx_work = 0;
672
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000673 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700674 (priv->cur_rx % priv->dma_rx_size));
675
676 if (priv->dirty_tx != priv->cur_tx)
677 tx_work = 1;
678
679 if (likely(!rxret || tx_work))
680 has_work = 1;
681
682 return has_work;
683}
684
685static inline void _stmmac_schedule(struct stmmac_priv *priv)
686{
687 if (likely(stmmac_has_work(priv))) {
688 stmmac_disable_irq(priv);
689 napi_schedule(&priv->napi);
690 }
691}
692
693#ifdef CONFIG_STMMAC_TIMER
694void stmmac_schedule(struct net_device *dev)
695{
696 struct stmmac_priv *priv = netdev_priv(dev);
697
698 priv->xstats.sched_timer_n++;
699
700 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700701}
702
703static void stmmac_no_timer_started(unsigned int x)
704{;
705};
706
707static void stmmac_no_timer_stopped(void)
708{;
709};
710#endif
711
712/**
713 * stmmac_tx_err:
714 * @priv: pointer to the private device structure
715 * Description: it cleans the descriptors and restarts the transmission
716 * in case of errors.
717 */
718static void stmmac_tx_err(struct stmmac_priv *priv)
719{
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000720
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700721 netif_stop_queue(priv->dev);
722
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000723 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700724 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000725 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700726 priv->dirty_tx = 0;
727 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000728 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700729
730 priv->dev->stats.tx_errors++;
731 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700732}
733
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000734
735static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700736{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000737 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700738
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000739 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000740 if (likely(status == handle_tx_rx))
741 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700742
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000743 else if (unlikely(status == tx_hard_error_bump_tc)) {
744 /* Try to bump up the dma threshold on this failure */
745 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
746 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000747 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000748 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700749 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000750 } else if (unlikely(status == tx_hard_error))
751 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700752}
753
754/**
755 * stmmac_open - open entry point of the driver
756 * @dev : pointer to the device structure.
757 * Description:
758 * This function is the open entry point of the driver.
759 * Return value:
760 * 0 on success and an appropriate (-)ve integer as defined in errno.h
761 * file on failure.
762 */
763static int stmmac_open(struct net_device *dev)
764{
765 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700766 int ret;
767
768 /* Check that the MAC address is valid. If its not, refuse
769 * to bring the device up. The user must specify an
770 * address using the following linux command:
771 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
772 if (!is_valid_ether_addr(dev->dev_addr)) {
773 random_ether_addr(dev->dev_addr);
774 pr_warning("%s: generated random MAC address %pM\n", dev->name,
775 dev->dev_addr);
776 }
777
778 stmmac_verify_args();
779
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700780#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000781 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700782 if (unlikely(priv->tm == NULL)) {
Frans Pop2381a552010-03-24 07:57:36 +0000783 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700784 return -ENOMEM;
785 }
786 priv->tm->freq = tmrate;
787
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000788 /* Test if the external timer can be actually used.
789 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700790 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000791 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700792 priv->tm->freq = 0;
793 priv->tm->timer_start = stmmac_no_timer_started;
794 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000795 } else
796 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700797#endif
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000798 ret = stmmac_init_phy(dev);
799 if (unlikely(ret)) {
800 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
801 goto open_error;
802 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700803
804 /* Create and initialize the TX/RX descriptors chains. */
805 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
806 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
807 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
808 init_dma_desc_rings(dev);
809
810 /* DMA initialization and SW reset */
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000811 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
812 priv->dma_tx_phy, priv->dma_rx_phy);
813 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700814 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000815 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700816 }
817
818 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000819 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +0000820 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000821 if (priv->plat->bus_setup)
822 priv->plat->bus_setup(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700823 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000824 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700825
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000826 priv->rx_coe = priv->hw->mac->rx_coe(priv->ioaddr);
827 if (priv->rx_coe)
828 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000829 if (priv->plat->tx_coe)
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000830 pr_info("\tTX Checksum insertion supported\n");
Michał Mirosław5e982f32011-04-09 02:46:55 +0000831 netdev_update_features(dev);
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000832
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700833 /* Initialise the MMC (if present) to disable all interrupts. */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000834 writel(0xffffffff, priv->ioaddr + MMC_HIGH_INTR_MASK);
835 writel(0xffffffff, priv->ioaddr + MMC_LOW_INTR_MASK);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700836
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000837 /* Request the IRQ lines */
838 ret = request_irq(dev->irq, stmmac_interrupt,
839 IRQF_SHARED, dev->name, dev);
840 if (unlikely(ret < 0)) {
841 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
842 __func__, dev->irq, ret);
843 goto open_error;
844 }
845
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700846 /* Enable the MAC Rx/Tx */
avisconti19449bf2010-10-25 18:58:14 +0000847 stmmac_enable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700848
849 /* Set the HW DMA mode and the COE */
850 stmmac_dma_operation_mode(priv);
851
852 /* Extra statistics */
853 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
854 priv->xstats.threshold = tc;
855
856 /* Start the ball rolling... */
857 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000858 priv->hw->dma->start_tx(priv->ioaddr);
859 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700860
861#ifdef CONFIG_STMMAC_TIMER
862 priv->tm->timer_start(tmrate);
863#endif
864 /* Dump DMA/MAC registers */
865 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000866 priv->hw->mac->dump_regs(priv->ioaddr);
867 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700868 }
869
870 if (priv->phydev)
871 phy_start(priv->phydev);
872
873 napi_enable(&priv->napi);
874 skb_queue_head_init(&priv->rx_recycle);
875 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000876
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700877 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000878
879open_error:
880#ifdef CONFIG_STMMAC_TIMER
881 kfree(priv->tm);
882#endif
883 if (priv->phydev)
884 phy_disconnect(priv->phydev);
885
886 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700887}
888
889/**
890 * stmmac_release - close entry point of the driver
891 * @dev : device pointer.
892 * Description:
893 * This is the stop entry point of the driver.
894 */
895static int stmmac_release(struct net_device *dev)
896{
897 struct stmmac_priv *priv = netdev_priv(dev);
898
899 /* Stop and disconnect the PHY */
900 if (priv->phydev) {
901 phy_stop(priv->phydev);
902 phy_disconnect(priv->phydev);
903 priv->phydev = NULL;
904 }
905
906 netif_stop_queue(dev);
907
908#ifdef CONFIG_STMMAC_TIMER
909 /* Stop and release the timer */
910 stmmac_close_ext_timer();
911 if (priv->tm != NULL)
912 kfree(priv->tm);
913#endif
914 napi_disable(&priv->napi);
915 skb_queue_purge(&priv->rx_recycle);
916
917 /* Free the IRQ lines */
918 free_irq(dev->irq, dev);
919
920 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000921 priv->hw->dma->stop_tx(priv->ioaddr);
922 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700923
924 /* Release and free the Rx/Tx resources */
925 free_dma_desc_resources(priv);
926
avisconti19449bf2010-10-25 18:58:14 +0000927 /* Disable the MAC Rx/Tx */
928 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700929
930 netif_carrier_off(dev);
931
932 return 0;
933}
934
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700935static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
936 struct net_device *dev,
937 int csum_insertion)
938{
939 struct stmmac_priv *priv = netdev_priv(dev);
940 unsigned int nopaged_len = skb_headlen(skb);
941 unsigned int txsize = priv->dma_tx_size;
942 unsigned int entry = priv->cur_tx % txsize;
943 struct dma_desc *desc = priv->dma_tx + entry;
944
945 if (nopaged_len > BUF_SIZE_8KiB) {
946
947 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
948
949 desc->des2 = dma_map_single(priv->device, skb->data,
950 BUF_SIZE_8KiB, DMA_TO_DEVICE);
951 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000952 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
953 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700954
955 entry = (++priv->cur_tx) % txsize;
956 desc = priv->dma_tx + entry;
957
958 desc->des2 = dma_map_single(priv->device,
959 skb->data + BUF_SIZE_8KiB,
960 buf2_size, DMA_TO_DEVICE);
961 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000962 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
963 csum_insertion);
964 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700965 priv->tx_skbuff[entry] = NULL;
966 } else {
967 desc->des2 = dma_map_single(priv->device, skb->data,
968 nopaged_len, DMA_TO_DEVICE);
969 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000970 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
971 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700972 }
973 return entry;
974}
975
976/**
977 * stmmac_xmit:
978 * @skb : the socket buffer
979 * @dev : device pointer
980 * Description : Tx entry point of the driver.
981 */
982static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
983{
984 struct stmmac_priv *priv = netdev_priv(dev);
985 unsigned int txsize = priv->dma_tx_size;
986 unsigned int entry;
987 int i, csum_insertion = 0;
988 int nfrags = skb_shinfo(skb)->nr_frags;
989 struct dma_desc *desc, *first;
990
991 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
992 if (!netif_queue_stopped(dev)) {
993 netif_stop_queue(dev);
994 /* This is a hard error, log it. */
995 pr_err("%s: BUG! Tx Ring full when queue awake\n",
996 __func__);
997 }
998 return NETDEV_TX_BUSY;
999 }
1000
1001 entry = priv->cur_tx % txsize;
1002
1003#ifdef STMMAC_XMIT_DEBUG
1004 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1005 pr_info("stmmac xmit:\n"
1006 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1007 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1008 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1009 !skb_is_gso(skb) ? "isn't" : "is");
1010#endif
1011
Michał Mirosław5e982f32011-04-09 02:46:55 +00001012 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001013
1014 desc = priv->dma_tx + entry;
1015 first = desc;
1016
1017#ifdef STMMAC_XMIT_DEBUG
1018 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1019 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1020 "\t\tn_frags: %d, ip_summed: %d\n",
1021 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1022#endif
1023 priv->tx_skbuff[entry] = skb;
1024 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1025 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1026 desc = priv->dma_tx + entry;
1027 } else {
1028 unsigned int nopaged_len = skb_headlen(skb);
1029 desc->des2 = dma_map_single(priv->device, skb->data,
1030 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001031 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1032 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001033 }
1034
1035 for (i = 0; i < nfrags; i++) {
1036 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1037 int len = frag->size;
1038
1039 entry = (++priv->cur_tx) % txsize;
1040 desc = priv->dma_tx + entry;
1041
1042 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1043 desc->des2 = dma_map_page(priv->device, frag->page,
1044 frag->page_offset,
1045 len, DMA_TO_DEVICE);
1046 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001047 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
1048 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001049 }
1050
1051 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001052 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001053
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001054#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001055 /* Clean IC while using timer */
1056 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001057 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001058#endif
1059 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001060 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001061
1062 priv->cur_tx++;
1063
1064#ifdef STMMAC_XMIT_DEBUG
1065 if (netif_msg_pktdata(priv)) {
1066 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1067 "first=%p, nfrags=%d\n",
1068 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1069 entry, first, nfrags);
1070 display_ring(priv->dma_tx, txsize);
1071 pr_info(">>> frame to be transmitted: ");
1072 print_pkt(skb->data, skb->len);
1073 }
1074#endif
1075 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1076 TX_DBG("%s: stop transmitted packets\n", __func__);
1077 netif_stop_queue(dev);
1078 }
1079
1080 dev->stats.tx_bytes += skb->len;
1081
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001082 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001083
Richard Cochran3e82ce12011-06-12 02:19:06 +00001084 skb_tx_timestamp(skb);
1085
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001086 return NETDEV_TX_OK;
1087}
1088
1089static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1090{
1091 unsigned int rxsize = priv->dma_rx_size;
1092 int bfsize = priv->dma_buf_sz;
1093 struct dma_desc *p = priv->dma_rx;
1094
1095 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1096 unsigned int entry = priv->dirty_rx % rxsize;
1097 if (likely(priv->rx_skbuff[entry] == NULL)) {
1098 struct sk_buff *skb;
1099
1100 skb = __skb_dequeue(&priv->rx_recycle);
1101 if (skb == NULL)
1102 skb = netdev_alloc_skb_ip_align(priv->dev,
1103 bfsize);
1104
1105 if (unlikely(skb == NULL))
1106 break;
1107
1108 priv->rx_skbuff[entry] = skb;
1109 priv->rx_skbuff_dma[entry] =
1110 dma_map_single(priv->device, skb->data, bfsize,
1111 DMA_FROM_DEVICE);
1112
1113 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001114 if (unlikely(priv->plat->has_gmac)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001115 if (bfsize >= BUF_SIZE_8KiB)
1116 (p + entry)->des3 =
1117 (p + entry)->des2 + BUF_SIZE_8KiB;
1118 }
1119 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1120 }
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001121 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001122 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001123}
1124
1125static int stmmac_rx(struct stmmac_priv *priv, int limit)
1126{
1127 unsigned int rxsize = priv->dma_rx_size;
1128 unsigned int entry = priv->cur_rx % rxsize;
1129 unsigned int next_entry;
1130 unsigned int count = 0;
1131 struct dma_desc *p = priv->dma_rx + entry;
1132 struct dma_desc *p_next;
1133
1134#ifdef STMMAC_RX_DEBUG
1135 if (netif_msg_hw(priv)) {
1136 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1137 display_ring(priv->dma_rx, rxsize);
1138 }
1139#endif
1140 count = 0;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001141 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001142 int status;
1143
1144 if (count >= limit)
1145 break;
1146
1147 count++;
1148
1149 next_entry = (++priv->cur_rx) % rxsize;
1150 p_next = priv->dma_rx + next_entry;
1151 prefetch(p_next);
1152
1153 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001154 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1155 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001156 if (unlikely(status == discard_frame))
1157 priv->dev->stats.rx_errors++;
1158 else {
1159 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001160 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001161
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001162 frame_len = priv->hw->desc->get_rx_frame_len(p);
1163 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1164 * Type frames (LLC/LLC-SNAP) */
1165 if (unlikely(status != llc_snap))
1166 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001167#ifdef STMMAC_RX_DEBUG
1168 if (frame_len > ETH_FRAME_LEN)
1169 pr_debug("\tRX frame size %d, COE status: %d\n",
1170 frame_len, status);
1171
1172 if (netif_msg_hw(priv))
1173 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1174 p, entry, p->des2);
1175#endif
1176 skb = priv->rx_skbuff[entry];
1177 if (unlikely(!skb)) {
1178 pr_err("%s: Inconsistent Rx descriptor chain\n",
1179 priv->dev->name);
1180 priv->dev->stats.rx_dropped++;
1181 break;
1182 }
1183 prefetch(skb->data - NET_IP_ALIGN);
1184 priv->rx_skbuff[entry] = NULL;
1185
1186 skb_put(skb, frame_len);
1187 dma_unmap_single(priv->device,
1188 priv->rx_skbuff_dma[entry],
1189 priv->dma_buf_sz, DMA_FROM_DEVICE);
1190#ifdef STMMAC_RX_DEBUG
1191 if (netif_msg_pktdata(priv)) {
1192 pr_info(" frame received (%dbytes)", frame_len);
1193 print_pkt(skb->data, frame_len);
1194 }
1195#endif
1196 skb->protocol = eth_type_trans(skb, priv->dev);
1197
1198 if (unlikely(status == csum_none)) {
1199 /* always for the old mac 10/100 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001200 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001201 netif_receive_skb(skb);
1202 } else {
1203 skb->ip_summed = CHECKSUM_UNNECESSARY;
1204 napi_gro_receive(&priv->napi, skb);
1205 }
1206
1207 priv->dev->stats.rx_packets++;
1208 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001209 }
1210 entry = next_entry;
1211 p = p_next; /* use prefetched values */
1212 }
1213
1214 stmmac_rx_refill(priv);
1215
1216 priv->xstats.rx_pkt_n += count;
1217
1218 return count;
1219}
1220
1221/**
1222 * stmmac_poll - stmmac poll method (NAPI)
1223 * @napi : pointer to the napi structure.
1224 * @budget : maximum number of packets that the current CPU can receive from
1225 * all interfaces.
1226 * Description :
1227 * This function implements the the reception process.
1228 * Also it runs the TX completion thread
1229 */
1230static int stmmac_poll(struct napi_struct *napi, int budget)
1231{
1232 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1233 int work_done = 0;
1234
1235 priv->xstats.poll_n++;
1236 stmmac_tx(priv);
1237 work_done = stmmac_rx(priv, budget);
1238
1239 if (work_done < budget) {
1240 napi_complete(napi);
1241 stmmac_enable_irq(priv);
1242 }
1243 return work_done;
1244}
1245
1246/**
1247 * stmmac_tx_timeout
1248 * @dev : Pointer to net device structure
1249 * Description: this function is called when a packet transmission fails to
1250 * complete within a reasonable tmrate. The driver will mark the error in the
1251 * netdev structure and arrange for the device to be reset to a sane state
1252 * in order to transmit a new packet.
1253 */
1254static void stmmac_tx_timeout(struct net_device *dev)
1255{
1256 struct stmmac_priv *priv = netdev_priv(dev);
1257
1258 /* Clear Tx resources and restart transmitting again */
1259 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001260}
1261
1262/* Configuration changes (passed on by ifconfig) */
1263static int stmmac_config(struct net_device *dev, struct ifmap *map)
1264{
1265 if (dev->flags & IFF_UP) /* can't act on a running interface */
1266 return -EBUSY;
1267
1268 /* Don't allow changing the I/O address */
1269 if (map->base_addr != dev->base_addr) {
1270 pr_warning("%s: can't change I/O address\n", dev->name);
1271 return -EOPNOTSUPP;
1272 }
1273
1274 /* Don't allow changing the IRQ */
1275 if (map->irq != dev->irq) {
1276 pr_warning("%s: can't change IRQ number %d\n",
1277 dev->name, dev->irq);
1278 return -EOPNOTSUPP;
1279 }
1280
1281 /* ignore other fields */
1282 return 0;
1283}
1284
1285/**
1286 * stmmac_multicast_list - entry point for multicast addressing
1287 * @dev : pointer to the device structure
1288 * Description:
1289 * This function is a driver entry point which gets called by the kernel
1290 * whenever multicast addresses must be enabled/disabled.
1291 * Return value:
1292 * void.
1293 */
1294static void stmmac_multicast_list(struct net_device *dev)
1295{
1296 struct stmmac_priv *priv = netdev_priv(dev);
1297
1298 spin_lock(&priv->lock);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001299 priv->hw->mac->set_filter(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001300 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001301}
1302
1303/**
1304 * stmmac_change_mtu - entry point to change MTU size for the device.
1305 * @dev : device pointer.
1306 * @new_mtu : the new MTU size for the device.
1307 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1308 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1309 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1310 * Return value:
1311 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1312 * file on failure.
1313 */
1314static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1315{
1316 struct stmmac_priv *priv = netdev_priv(dev);
1317 int max_mtu;
1318
1319 if (netif_running(dev)) {
1320 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1321 return -EBUSY;
1322 }
1323
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001324 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001325 max_mtu = JUMBO_LEN;
1326 else
1327 max_mtu = ETH_DATA_LEN;
1328
1329 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1330 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1331 return -EINVAL;
1332 }
1333
Michał Mirosław5e982f32011-04-09 02:46:55 +00001334 dev->mtu = new_mtu;
1335 netdev_update_features(dev);
1336
1337 return 0;
1338}
1339
1340static u32 stmmac_fix_features(struct net_device *dev, u32 features)
1341{
1342 struct stmmac_priv *priv = netdev_priv(dev);
1343
1344 if (!priv->rx_coe)
1345 features &= ~NETIF_F_RXCSUM;
1346 if (!priv->plat->tx_coe)
1347 features &= ~NETIF_F_ALL_CSUM;
1348
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001349 /* Some GMAC devices have a bugged Jumbo frame support that
1350 * needs to have the Tx COE disabled for oversized frames
1351 * (due to limited buffer sizes). In this case we disable
1352 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001353 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1354 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001355
Michał Mirosław5e982f32011-04-09 02:46:55 +00001356 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001357}
1358
1359static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1360{
1361 struct net_device *dev = (struct net_device *)dev_id;
1362 struct stmmac_priv *priv = netdev_priv(dev);
1363
1364 if (unlikely(!dev)) {
1365 pr_err("%s: invalid dev pointer\n", __func__);
1366 return IRQ_NONE;
1367 }
1368
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001369 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001370 /* To handle GMAC own interrupts */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001371 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001372
1373 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001374
1375 return IRQ_HANDLED;
1376}
1377
1378#ifdef CONFIG_NET_POLL_CONTROLLER
1379/* Polling receive - used by NETCONSOLE and other diagnostic tools
1380 * to allow network I/O with interrupts disabled. */
1381static void stmmac_poll_controller(struct net_device *dev)
1382{
1383 disable_irq(dev->irq);
1384 stmmac_interrupt(dev->irq, dev);
1385 enable_irq(dev->irq);
1386}
1387#endif
1388
1389/**
1390 * stmmac_ioctl - Entry point for the Ioctl
1391 * @dev: Device pointer.
1392 * @rq: An IOCTL specefic structure, that can contain a pointer to
1393 * a proprietary structure used to pass information to the driver.
1394 * @cmd: IOCTL command
1395 * Description:
1396 * Currently there are no special functionality supported in IOCTL, just the
1397 * phy_mii_ioctl(...) can be invoked.
1398 */
1399static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1400{
1401 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001402 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001403
1404 if (!netif_running(dev))
1405 return -EINVAL;
1406
Richard Cochran28b04112010-07-17 08:48:55 +00001407 if (!priv->phydev)
1408 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001409
Richard Cochran28b04112010-07-17 08:48:55 +00001410 spin_lock(&priv->lock);
1411 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1412 spin_unlock(&priv->lock);
1413
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001414 return ret;
1415}
1416
1417#ifdef STMMAC_VLAN_TAG_USED
1418static void stmmac_vlan_rx_register(struct net_device *dev,
1419 struct vlan_group *grp)
1420{
1421 struct stmmac_priv *priv = netdev_priv(dev);
1422
1423 DBG(probe, INFO, "%s: Setting vlgrp to %p\n", dev->name, grp);
1424
1425 spin_lock(&priv->lock);
1426 priv->vlgrp = grp;
1427 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001428}
1429#endif
1430
1431static const struct net_device_ops stmmac_netdev_ops = {
1432 .ndo_open = stmmac_open,
1433 .ndo_start_xmit = stmmac_xmit,
1434 .ndo_stop = stmmac_release,
1435 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001436 .ndo_fix_features = stmmac_fix_features,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001437 .ndo_set_multicast_list = stmmac_multicast_list,
1438 .ndo_tx_timeout = stmmac_tx_timeout,
1439 .ndo_do_ioctl = stmmac_ioctl,
1440 .ndo_set_config = stmmac_config,
1441#ifdef STMMAC_VLAN_TAG_USED
1442 .ndo_vlan_rx_register = stmmac_vlan_rx_register,
1443#endif
1444#ifdef CONFIG_NET_POLL_CONTROLLER
1445 .ndo_poll_controller = stmmac_poll_controller,
1446#endif
1447 .ndo_set_mac_address = eth_mac_addr,
1448};
1449
1450/**
1451 * stmmac_probe - Initialization of the adapter .
1452 * @dev : device pointer
1453 * Description: The function initializes the network device structure for
1454 * the STMMAC driver. It also calls the low level routines
1455 * in order to init the HW (i.e. the DMA engine)
1456 */
1457static int stmmac_probe(struct net_device *dev)
1458{
1459 int ret = 0;
1460 struct stmmac_priv *priv = netdev_priv(dev);
1461
1462 ether_setup(dev);
1463
1464 dev->netdev_ops = &stmmac_netdev_ops;
1465 stmmac_set_ethtool_ops(dev);
1466
Michał Mirosław5e982f32011-04-09 02:46:55 +00001467 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1468 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001469 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1470#ifdef STMMAC_VLAN_TAG_USED
1471 /* Both mac100 and gmac support receive VLAN tag detection */
1472 dev->features |= NETIF_F_HW_VLAN_RX;
1473#endif
1474 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1475
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001476 if (flow_ctrl)
1477 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1478
1479 priv->pause = pause;
1480 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1481
1482 /* Get the MAC address */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001483 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1484 dev->dev_addr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001485
1486 if (!is_valid_ether_addr(dev->dev_addr))
1487 pr_warning("\tno valid MAC address;"
1488 "please, use ifconfig or nwhwconfig!\n");
1489
Vlad Lunguf8e96162010-11-29 22:52:52 +00001490 spin_lock_init(&priv->lock);
1491
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001492 ret = register_netdev(dev);
1493 if (ret) {
1494 pr_err("%s: ERROR %i registering the device\n",
1495 __func__, ret);
1496 return -ENODEV;
1497 }
1498
1499 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1500 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
Michał Mirosław79032642010-11-30 06:38:00 +00001501 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001502
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001503 return ret;
1504}
1505
1506/**
1507 * stmmac_mac_device_setup
1508 * @dev : device pointer
1509 * Description: select and initialise the mac device (mac100 or Gmac).
1510 */
1511static int stmmac_mac_device_setup(struct net_device *dev)
1512{
1513 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001514
1515 struct mac_device_info *device;
1516
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001517 if (priv->plat->has_gmac)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001518 device = dwmac1000_setup(priv->ioaddr);
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001519 else
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001520 device = dwmac100_setup(priv->ioaddr);
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001521
Dan Carpenter1ff21902010-07-22 01:16:48 +00001522 if (!device)
1523 return -ENOMEM;
1524
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001525 if (priv->plat->enh_desc) {
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001526 device->desc = &enh_desc_ops;
1527 pr_info("\tEnhanced descriptor structure\n");
1528 } else
Giuseppe CAVALLARO56b106a2010-04-13 20:21:12 +00001529 device->desc = &ndesc_ops;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001530
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001531 priv->hw = device;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001532
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001533 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001534 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001535 enable_irq_wake(dev->irq);
1536 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001537
1538 return 0;
1539}
1540
1541static int stmmacphy_dvr_probe(struct platform_device *pdev)
1542{
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001543 struct plat_stmmacphy_data *plat_dat = pdev->dev.platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001544
1545 pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
1546 plat_dat->bus_id);
1547
1548 return 0;
1549}
1550
1551static int stmmacphy_dvr_remove(struct platform_device *pdev)
1552{
1553 return 0;
1554}
1555
1556static struct platform_driver stmmacphy_driver = {
1557 .driver = {
1558 .name = PHY_RESOURCE_NAME,
1559 },
1560 .probe = stmmacphy_dvr_probe,
1561 .remove = stmmacphy_dvr_remove,
1562};
1563
1564/**
1565 * stmmac_associate_phy
1566 * @dev: pointer to device structure
1567 * @data: points to the private structure.
1568 * Description: Scans through all the PHYs we have registered and checks if
1569 * any are associated with our MAC. If so, then just fill in
1570 * the blanks in our local context structure
1571 */
1572static int stmmac_associate_phy(struct device *dev, void *data)
1573{
1574 struct stmmac_priv *priv = (struct stmmac_priv *)data;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001575 struct plat_stmmacphy_data *plat_dat = dev->platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001576
1577 DBG(probe, DEBUG, "%s: checking phy for bus %d\n", __func__,
1578 plat_dat->bus_id);
1579
1580 /* Check that this phy is for the MAC being initialised */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001581 if (priv->plat->bus_id != plat_dat->bus_id)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001582 return 0;
1583
1584 /* OK, this PHY is connected to the MAC.
1585 Go ahead and get the parameters */
1586 DBG(probe, DEBUG, "%s: OK. Found PHY config\n", __func__);
1587 priv->phy_irq =
1588 platform_get_irq_byname(to_platform_device(dev), "phyirq");
1589 DBG(probe, DEBUG, "%s: PHY irq on bus %d is %d\n", __func__,
1590 plat_dat->bus_id, priv->phy_irq);
1591
1592 /* Override with kernel parameters if supplied XXX CRS XXX
1593 * this needs to have multiple instances */
1594 if ((phyaddr >= 0) && (phyaddr <= 31))
1595 plat_dat->phy_addr = phyaddr;
1596
1597 priv->phy_addr = plat_dat->phy_addr;
1598 priv->phy_mask = plat_dat->phy_mask;
1599 priv->phy_interface = plat_dat->interface;
1600 priv->phy_reset = plat_dat->phy_reset;
1601
1602 DBG(probe, DEBUG, "%s: exiting\n", __func__);
1603 return 1; /* forces exit of driver_for_each_device() */
1604}
1605
1606/**
1607 * stmmac_dvr_probe
1608 * @pdev: platform device pointer
1609 * Description: the driver is initialized through platform_device.
1610 */
1611static int stmmac_dvr_probe(struct platform_device *pdev)
1612{
1613 int ret = 0;
1614 struct resource *res;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001615 void __iomem *addr = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001616 struct net_device *ndev = NULL;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001617 struct stmmac_priv *priv = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001618 struct plat_stmmacenet_data *plat_dat;
1619
1620 pr_info("STMMAC driver:\n\tplatform registration... ");
1621 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001622 if (!res)
1623 return -ENODEV;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001624 pr_info("\tdone!\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001625
Dan Carpenterb6222682010-04-07 21:50:08 -07001626 if (!request_mem_region(res->start, resource_size(res),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001627 pdev->name)) {
1628 pr_err("%s: ERROR: memory allocation failed"
1629 "cannot get the I/O addr 0x%x\n",
1630 __func__, (unsigned int)res->start);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001631 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001632 }
1633
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001634 addr = ioremap(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001635 if (!addr) {
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001636 pr_err("%s: ERROR: memory mapping failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001637 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001638 goto out_release_region;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001639 }
1640
1641 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1642 if (!ndev) {
1643 pr_err("%s: ERROR: allocating the device\n", __func__);
1644 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001645 goto out_unmap;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001646 }
1647
1648 SET_NETDEV_DEV(ndev, &pdev->dev);
1649
1650 /* Get the MAC information */
1651 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1652 if (ndev->irq == -ENXIO) {
1653 pr_err("%s: ERROR: MAC IRQ configuration "
1654 "information not found\n", __func__);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001655 ret = -ENXIO;
1656 goto out_free_ndev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001657 }
1658
1659 priv = netdev_priv(ndev);
1660 priv->device = &(pdev->dev);
1661 priv->dev = ndev;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001662 plat_dat = pdev->dev.platform_data;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001663
1664 priv->plat = plat_dat;
1665
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001666 priv->ioaddr = addr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001667
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001668 /* PMT module is not integrated in all the MAC devices. */
1669 if (plat_dat->pmt) {
1670 pr_info("\tPMT module supported\n");
1671 device_set_wakeup_capable(&pdev->dev, 1);
1672 }
1673
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001674 platform_set_drvdata(pdev, ndev);
1675
1676 /* Set the I/O base addr */
1677 ndev->base_addr = (unsigned long)addr;
1678
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001679 /* Custom initialisation */
1680 if (priv->plat->init) {
1681 ret = priv->plat->init(pdev);
1682 if (unlikely(ret))
Dan Carpenter34a52f32010-12-20 21:34:56 +00001683 goto out_free_ndev;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001684 }
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001685
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001686 /* MAC HW revice detection */
1687 ret = stmmac_mac_device_setup(ndev);
1688 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001689 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001690
1691 /* Network Device Registration */
1692 ret = stmmac_probe(ndev);
1693 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001694 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001695
1696 /* associate a PHY - it is provided by another platform bus */
1697 if (!driver_for_each_device
1698 (&(stmmacphy_driver.driver), NULL, (void *)priv,
1699 stmmac_associate_phy)) {
1700 pr_err("No PHY device is associated with this MAC!\n");
1701 ret = -ENODEV;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001702 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001703 }
1704
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001705 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
David S. Miller1f0f6382010-08-30 21:55:17 -07001706 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1707 pdev->id, ndev->irq, addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001708
1709 /* MDIO bus Registration */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001710 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001711 ret = stmmac_mdio_register(ndev);
1712 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001713 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001714 pr_debug("registered!\n");
Dan Carpenter34a52f32010-12-20 21:34:56 +00001715 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001716
Dan Carpenter34a52f32010-12-20 21:34:56 +00001717out_unregister:
1718 unregister_netdev(ndev);
1719out_plat_exit:
1720 if (priv->plat->exit)
1721 priv->plat->exit(pdev);
1722out_free_ndev:
1723 free_netdev(ndev);
1724 platform_set_drvdata(pdev, NULL);
1725out_unmap:
1726 iounmap(addr);
1727out_release_region:
1728 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001729
1730 return ret;
1731}
1732
1733/**
1734 * stmmac_dvr_remove
1735 * @pdev: platform device pointer
1736 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1737 * changes the link status, releases the DMA descriptor rings,
1738 * unregisters the MDIO bus and unmaps the allocated memory.
1739 */
1740static int stmmac_dvr_remove(struct platform_device *pdev)
1741{
1742 struct net_device *ndev = platform_get_drvdata(pdev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001743 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001744 struct resource *res;
1745
1746 pr_info("%s:\n\tremoving driver", __func__);
1747
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001748 priv->hw->dma->stop_rx(priv->ioaddr);
1749 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001750
avisconti19449bf2010-10-25 18:58:14 +00001751 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001752
1753 netif_carrier_off(ndev);
1754
1755 stmmac_mdio_unregister(ndev);
1756
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001757 if (priv->plat->exit)
1758 priv->plat->exit(pdev);
1759
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001760 platform_set_drvdata(pdev, NULL);
1761 unregister_netdev(ndev);
1762
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001763 iounmap((void *)priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001764 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001765 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001766
1767 free_netdev(ndev);
1768
1769 return 0;
1770}
1771
1772#ifdef CONFIG_PM
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001773static int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001774{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001775 struct net_device *ndev = dev_get_drvdata(dev);
1776 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001777 int dis_ic = 0;
1778
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001779 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001780 return 0;
1781
1782 spin_lock(&priv->lock);
1783
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001784 netif_device_detach(ndev);
1785 netif_stop_queue(ndev);
1786 if (priv->phydev)
1787 phy_stop(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001788
1789#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001790 priv->tm->timer_stop();
1791 if (likely(priv->tm->enable))
1792 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001793#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001794 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001795
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001796 /* Stop TX/RX DMA */
1797 priv->hw->dma->stop_tx(priv->ioaddr);
1798 priv->hw->dma->stop_rx(priv->ioaddr);
1799 /* Clear the Rx/Tx descriptors */
1800 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1801 dis_ic);
1802 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001803
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001804 /* Enable Power down mode by programming the PMT regs */
1805 if (device_may_wakeup(priv->device))
1806 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
1807 else
1808 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001809
1810 spin_unlock(&priv->lock);
1811 return 0;
1812}
1813
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001814static int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001815{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001816 struct net_device *ndev = dev_get_drvdata(dev);
1817 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001818
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001819 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001820 return 0;
1821
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02001822 spin_lock(&priv->lock);
1823
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001824 /* Power Down bit, into the PM register, is cleared
1825 * automatically as soon as a magic packet or a Wake-up frame
1826 * is received. Anyway, it's better to manually clear
1827 * this bit because it can generate problems while resuming
1828 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001829 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001830 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001831
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001832 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001833
1834 /* Enable the MAC and DMA */
avisconti19449bf2010-10-25 18:58:14 +00001835 stmmac_enable_mac(priv->ioaddr);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001836 priv->hw->dma->start_tx(priv->ioaddr);
1837 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001838
1839#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001840 if (likely(priv->tm->enable))
1841 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001842#endif
1843 napi_enable(&priv->napi);
1844
1845 if (priv->phydev)
1846 phy_start(priv->phydev);
1847
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001848 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001849
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001850 spin_unlock(&priv->lock);
1851 return 0;
1852}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001853
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001854static int stmmac_freeze(struct device *dev)
1855{
1856 struct net_device *ndev = dev_get_drvdata(dev);
1857
1858 if (!ndev || !netif_running(ndev))
1859 return 0;
1860
1861 return stmmac_release(ndev);
1862}
1863
1864static int stmmac_restore(struct device *dev)
1865{
1866 struct net_device *ndev = dev_get_drvdata(dev);
1867
1868 if (!ndev || !netif_running(ndev))
1869 return 0;
1870
1871 return stmmac_open(ndev);
1872}
1873
1874static const struct dev_pm_ops stmmac_pm_ops = {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001875 .suspend = stmmac_suspend,
1876 .resume = stmmac_resume,
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001877 .freeze = stmmac_freeze,
1878 .thaw = stmmac_restore,
1879 .restore = stmmac_restore,
1880};
1881#else
1882static const struct dev_pm_ops stmmac_pm_ops;
1883#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001884
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001885static struct platform_driver stmmac_driver = {
1886 .probe = stmmac_dvr_probe,
1887 .remove = stmmac_dvr_remove,
1888 .driver = {
1889 .name = STMMAC_RESOURCE_NAME,
1890 .owner = THIS_MODULE,
1891 .pm = &stmmac_pm_ops,
1892 },
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001893};
1894
1895/**
1896 * stmmac_init_module - Entry point for the driver
1897 * Description: This function is the entry point for the driver.
1898 */
1899static int __init stmmac_init_module(void)
1900{
1901 int ret;
1902
1903 if (platform_driver_register(&stmmacphy_driver)) {
1904 pr_err("No PHY devices registered!\n");
1905 return -ENODEV;
1906 }
1907
1908 ret = platform_driver_register(&stmmac_driver);
1909 return ret;
1910}
1911
1912/**
1913 * stmmac_cleanup_module - Cleanup routine for the driver
1914 * Description: This function is the cleanup routine for the driver.
1915 */
1916static void __exit stmmac_cleanup_module(void)
1917{
1918 platform_driver_unregister(&stmmacphy_driver);
1919 platform_driver_unregister(&stmmac_driver);
1920}
1921
1922#ifndef MODULE
1923static int __init stmmac_cmdline_opt(char *str)
1924{
1925 char *opt;
1926
1927 if (!str || !*str)
1928 return -EINVAL;
1929 while ((opt = strsep(&str, ",")) != NULL) {
1930 if (!strncmp(opt, "debug:", 6))
1931 strict_strtoul(opt + 6, 0, (unsigned long *)&debug);
1932 else if (!strncmp(opt, "phyaddr:", 8))
1933 strict_strtoul(opt + 8, 0, (unsigned long *)&phyaddr);
1934 else if (!strncmp(opt, "dma_txsize:", 11))
1935 strict_strtoul(opt + 11, 0,
1936 (unsigned long *)&dma_txsize);
1937 else if (!strncmp(opt, "dma_rxsize:", 11))
1938 strict_strtoul(opt + 11, 0,
1939 (unsigned long *)&dma_rxsize);
1940 else if (!strncmp(opt, "buf_sz:", 7))
1941 strict_strtoul(opt + 7, 0, (unsigned long *)&buf_sz);
1942 else if (!strncmp(opt, "tc:", 3))
1943 strict_strtoul(opt + 3, 0, (unsigned long *)&tc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001944 else if (!strncmp(opt, "watchdog:", 9))
1945 strict_strtoul(opt + 9, 0, (unsigned long *)&watchdog);
1946 else if (!strncmp(opt, "flow_ctrl:", 10))
1947 strict_strtoul(opt + 10, 0,
1948 (unsigned long *)&flow_ctrl);
1949 else if (!strncmp(opt, "pause:", 6))
1950 strict_strtoul(opt + 6, 0, (unsigned long *)&pause);
1951#ifdef CONFIG_STMMAC_TIMER
1952 else if (!strncmp(opt, "tmrate:", 7))
1953 strict_strtoul(opt + 7, 0, (unsigned long *)&tmrate);
1954#endif
1955 }
1956 return 0;
1957}
1958
1959__setup("stmmaceth=", stmmac_cmdline_opt);
1960#endif
1961
1962module_init(stmmac_init_module);
1963module_exit(stmmac_cleanup_module);
1964
1965MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
1966MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
1967MODULE_LICENSE("GPL");