blob: 38f7b619c44c15e6a3dcda62ea511cad61d5451c [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);
142static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev);
143
144/**
145 * stmmac_verify_args - verify the driver parameters.
146 * Description: it verifies if some wrong parameter is passed to the driver.
147 * Note that wrong parameters are replaced with the default values.
148 */
149static void stmmac_verify_args(void)
150{
151 if (unlikely(watchdog < 0))
152 watchdog = TX_TIMEO;
153 if (unlikely(dma_rxsize < 0))
154 dma_rxsize = DMA_RX_SIZE;
155 if (unlikely(dma_txsize < 0))
156 dma_txsize = DMA_TX_SIZE;
157 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
158 buf_sz = DMA_BUFFER_SIZE;
159 if (unlikely(flow_ctrl > 1))
160 flow_ctrl = FLOW_AUTO;
161 else if (likely(flow_ctrl < 0))
162 flow_ctrl = FLOW_OFF;
163 if (unlikely((pause < 0) || (pause > 0xffff)))
164 pause = PAUSE_TIME;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700165}
166
167#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
168static void print_pkt(unsigned char *buf, int len)
169{
170 int j;
171 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
172 for (j = 0; j < len; j++) {
173 if ((j % 16) == 0)
174 pr_info("\n %03x:", j);
175 pr_info(" %02x", buf[j]);
176 }
177 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700178}
179#endif
180
181/* minimum number of free TX descriptors required to wake up TX process */
182#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
183
184static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
185{
186 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
187}
188
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000189/* On some ST platforms, some HW system configuraton registers have to be
190 * set according to the link speed negotiated.
191 */
192static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
193{
194 struct phy_device *phydev = priv->phydev;
195
196 if (likely(priv->plat->fix_mac_speed))
197 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
198 phydev->speed);
199}
200
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700201/**
202 * stmmac_adjust_link
203 * @dev: net device structure
204 * Description: it adjusts the link parameters.
205 */
206static void stmmac_adjust_link(struct net_device *dev)
207{
208 struct stmmac_priv *priv = netdev_priv(dev);
209 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700210 unsigned long flags;
211 int new_state = 0;
212 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
213
214 if (phydev == NULL)
215 return;
216
217 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
218 phydev->addr, phydev->link);
219
220 spin_lock_irqsave(&priv->lock, flags);
221 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000222 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700223
224 /* Now we make sure that we can be in full duplex mode.
225 * If not, we operate in half-duplex mode. */
226 if (phydev->duplex != priv->oldduplex) {
227 new_state = 1;
228 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000229 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700230 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000231 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700232 priv->oldduplex = phydev->duplex;
233 }
234 /* Flow Control operation */
235 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000236 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000237 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700238
239 if (phydev->speed != priv->speed) {
240 new_state = 1;
241 switch (phydev->speed) {
242 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000243 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000244 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000245 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700246 break;
247 case 100:
248 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000249 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000250 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700251 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000252 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700253 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000254 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700255 }
256 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000257 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700258 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000259 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700260 break;
261 default:
262 if (netif_msg_link(priv))
263 pr_warning("%s: Speed (%d) is not 10"
264 " or 100!\n", dev->name, phydev->speed);
265 break;
266 }
267
268 priv->speed = phydev->speed;
269 }
270
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000271 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700272
273 if (!priv->oldlink) {
274 new_state = 1;
275 priv->oldlink = 1;
276 }
277 } else if (priv->oldlink) {
278 new_state = 1;
279 priv->oldlink = 0;
280 priv->speed = 0;
281 priv->oldduplex = -1;
282 }
283
284 if (new_state && netif_msg_link(priv))
285 phy_print_status(phydev);
286
287 spin_unlock_irqrestore(&priv->lock, flags);
288
289 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
290}
291
292/**
293 * stmmac_init_phy - PHY initialization
294 * @dev: net device structure
295 * Description: it initializes the driver's PHY state, and attaches the PHY
296 * to the mac driver.
297 * Return value:
298 * 0 on success
299 */
300static int stmmac_init_phy(struct net_device *dev)
301{
302 struct stmmac_priv *priv = netdev_priv(dev);
303 struct phy_device *phydev;
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000304 char phy_id[MII_BUS_ID_SIZE + 3];
305 char bus_id[MII_BUS_ID_SIZE];
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700306
307 priv->oldlink = 0;
308 priv->speed = 0;
309 priv->oldduplex = -1;
310
311 if (priv->phy_addr == -1) {
312 /* We don't have a PHY, so do nothing */
313 return 0;
314 }
315
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000316 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000317 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
318 priv->phy_addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700319 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
320
321 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
322 priv->phy_interface);
323
324 if (IS_ERR(phydev)) {
325 pr_err("%s: Could not attach to PHY\n", dev->name);
326 return PTR_ERR(phydev);
327 }
328
329 /*
330 * Broken HW is sometimes missing the pull-up resistor on the
331 * MDIO line, which results in reads to non-existent devices returning
332 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
333 * device as well.
334 * Note: phydev->phy_id is the result of reading the UID PHY registers.
335 */
336 if (phydev->phy_id == 0) {
337 phy_disconnect(phydev);
338 return -ENODEV;
339 }
340 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
341 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
342
343 priv->phydev = phydev;
344
345 return 0;
346}
347
avisconti19449bf2010-10-25 18:58:14 +0000348static inline void stmmac_enable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700349{
350 u32 value = readl(ioaddr + MAC_CTRL_REG);
avisconti19449bf2010-10-25 18:58:14 +0000351
352 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700353 writel(value, ioaddr + MAC_CTRL_REG);
354}
355
avisconti19449bf2010-10-25 18:58:14 +0000356static inline void stmmac_disable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700357{
358 u32 value = readl(ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700359
avisconti19449bf2010-10-25 18:58:14 +0000360 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700361 writel(value, ioaddr + MAC_CTRL_REG);
362}
363
364/**
365 * display_ring
366 * @p: pointer to the ring.
367 * @size: size of the ring.
368 * Description: display all the descriptors within the ring.
369 */
370static void display_ring(struct dma_desc *p, int size)
371{
372 struct tmp_s {
373 u64 a;
374 unsigned int b;
375 unsigned int c;
376 };
377 int i;
378 for (i = 0; i < size; i++) {
379 struct tmp_s *x = (struct tmp_s *)(p + i);
380 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
381 i, (unsigned int)virt_to_phys(&p[i]),
382 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
383 x->b, x->c);
384 pr_info("\n");
385 }
386}
387
388/**
389 * init_dma_desc_rings - init the RX/TX descriptor rings
390 * @dev: net device structure
391 * Description: this function initializes the DMA RX/TX descriptors
392 * and allocates the socket buffers.
393 */
394static void init_dma_desc_rings(struct net_device *dev)
395{
396 int i;
397 struct stmmac_priv *priv = netdev_priv(dev);
398 struct sk_buff *skb;
399 unsigned int txsize = priv->dma_tx_size;
400 unsigned int rxsize = priv->dma_rx_size;
401 unsigned int bfsize = priv->dma_buf_sz;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000402 int buff2_needed = 0, dis_ic = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700403
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700404 /* Set the Buffer size according to the MTU;
405 * indeed, in case of jumbo we need to bump-up the buffer sizes.
406 */
407 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
408 bfsize = BUF_SIZE_16KiB;
409 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
410 bfsize = BUF_SIZE_8KiB;
411 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
412 bfsize = BUF_SIZE_4KiB;
413 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
414 bfsize = BUF_SIZE_2KiB;
415 else
416 bfsize = DMA_BUFFER_SIZE;
417
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000418#ifdef CONFIG_STMMAC_TIMER
419 /* Disable interrupts on completion for the reception if timer is on */
420 if (likely(priv->tm->enable))
421 dis_ic = 1;
422#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700423 /* If the MTU exceeds 8k so use the second buffer in the chain */
424 if (bfsize >= BUF_SIZE_8KiB)
425 buff2_needed = 1;
426
427 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
428 txsize, rxsize, bfsize);
429
430 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
431 priv->rx_skbuff =
432 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
433 priv->dma_rx =
434 (struct dma_desc *)dma_alloc_coherent(priv->device,
435 rxsize *
436 sizeof(struct dma_desc),
437 &priv->dma_rx_phy,
438 GFP_KERNEL);
439 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
440 GFP_KERNEL);
441 priv->dma_tx =
442 (struct dma_desc *)dma_alloc_coherent(priv->device,
443 txsize *
444 sizeof(struct dma_desc),
445 &priv->dma_tx_phy,
446 GFP_KERNEL);
447
448 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
449 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
450 return;
451 }
452
453 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
454 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
455 dev->name, priv->dma_rx, priv->dma_tx,
456 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
457
458 /* RX INITIALIZATION */
459 DBG(probe, INFO, "stmmac: SKB addresses:\n"
460 "skb\t\tskb data\tdma data\n");
461
462 for (i = 0; i < rxsize; i++) {
463 struct dma_desc *p = priv->dma_rx + i;
464
465 skb = netdev_alloc_skb_ip_align(dev, bfsize);
466 if (unlikely(skb == NULL)) {
467 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
468 break;
469 }
470 priv->rx_skbuff[i] = skb;
471 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
472 bfsize, DMA_FROM_DEVICE);
473
474 p->des2 = priv->rx_skbuff_dma[i];
475 if (unlikely(buff2_needed))
476 p->des3 = p->des2 + BUF_SIZE_8KiB;
477 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
478 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
479 }
480 priv->cur_rx = 0;
481 priv->dirty_rx = (unsigned int)(i - rxsize);
482 priv->dma_buf_sz = bfsize;
483 buf_sz = bfsize;
484
485 /* TX INITIALIZATION */
486 for (i = 0; i < txsize; i++) {
487 priv->tx_skbuff[i] = NULL;
488 priv->dma_tx[i].des2 = 0;
489 }
490 priv->dirty_tx = 0;
491 priv->cur_tx = 0;
492
493 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000494 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
495 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700496
497 if (netif_msg_hw(priv)) {
498 pr_info("RX descriptor ring:\n");
499 display_ring(priv->dma_rx, rxsize);
500 pr_info("TX descriptor ring:\n");
501 display_ring(priv->dma_tx, txsize);
502 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700503}
504
505static void dma_free_rx_skbufs(struct stmmac_priv *priv)
506{
507 int i;
508
509 for (i = 0; i < priv->dma_rx_size; i++) {
510 if (priv->rx_skbuff[i]) {
511 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
512 priv->dma_buf_sz, DMA_FROM_DEVICE);
513 dev_kfree_skb_any(priv->rx_skbuff[i]);
514 }
515 priv->rx_skbuff[i] = NULL;
516 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700517}
518
519static void dma_free_tx_skbufs(struct stmmac_priv *priv)
520{
521 int i;
522
523 for (i = 0; i < priv->dma_tx_size; i++) {
524 if (priv->tx_skbuff[i] != NULL) {
525 struct dma_desc *p = priv->dma_tx + i;
526 if (p->des2)
527 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000528 priv->hw->desc->get_tx_len(p),
529 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700530 dev_kfree_skb_any(priv->tx_skbuff[i]);
531 priv->tx_skbuff[i] = NULL;
532 }
533 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700534}
535
536static void free_dma_desc_resources(struct stmmac_priv *priv)
537{
538 /* Release the DMA TX/RX socket buffers */
539 dma_free_rx_skbufs(priv);
540 dma_free_tx_skbufs(priv);
541
542 /* Free the region of consistent memory previously allocated for
543 * the DMA */
544 dma_free_coherent(priv->device,
545 priv->dma_tx_size * sizeof(struct dma_desc),
546 priv->dma_tx, priv->dma_tx_phy);
547 dma_free_coherent(priv->device,
548 priv->dma_rx_size * sizeof(struct dma_desc),
549 priv->dma_rx, priv->dma_rx_phy);
550 kfree(priv->rx_skbuff_dma);
551 kfree(priv->rx_skbuff);
552 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700553}
554
555/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700556 * stmmac_dma_operation_mode - HW DMA operation mode
557 * @priv : pointer to the private device structure.
558 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000559 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700560 */
561static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
562{
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000563 if (likely((priv->plat->tx_coe) && (!priv->no_csum_insertion))) {
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000564 /* In case of GMAC, SF mode has to be enabled
565 * to perform the TX COE. This depends on:
566 * 1) TX COE if actually supported
567 * 2) There is no bugged Jumbo frame support
568 * that needs to not insert csum in the TDES.
569 */
570 priv->hw->dma->dma_mode(priv->ioaddr,
571 SF_DMA_MODE, SF_DMA_MODE);
572 tc = SF_DMA_MODE;
573 } else
574 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700575}
576
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700577/**
578 * stmmac_tx:
579 * @priv: private driver structure
580 * Description: it reclaims resources after transmission completes.
581 */
582static void stmmac_tx(struct stmmac_priv *priv)
583{
584 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700585
586 while (priv->dirty_tx != priv->cur_tx) {
587 int last;
588 unsigned int entry = priv->dirty_tx % txsize;
589 struct sk_buff *skb = priv->tx_skbuff[entry];
590 struct dma_desc *p = priv->dma_tx + entry;
591
592 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000593 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700594 break;
595
596 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000597 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700598 if (likely(last)) {
599 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000600 priv->hw->desc->tx_status(&priv->dev->stats,
601 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000602 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700603 if (likely(tx_error == 0)) {
604 priv->dev->stats.tx_packets++;
605 priv->xstats.tx_pkt_n++;
606 } else
607 priv->dev->stats.tx_errors++;
608 }
609 TX_DBG("%s: curr %d, dirty %d\n", __func__,
610 priv->cur_tx, priv->dirty_tx);
611
612 if (likely(p->des2))
613 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000614 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700615 DMA_TO_DEVICE);
616 if (unlikely(p->des3))
617 p->des3 = 0;
618
619 if (likely(skb != NULL)) {
620 /*
621 * If there's room in the queue (limit it to size)
622 * we add this skb back into the pool,
623 * if it's the right size.
624 */
625 if ((skb_queue_len(&priv->rx_recycle) <
626 priv->dma_rx_size) &&
627 skb_recycle_check(skb, priv->dma_buf_sz))
628 __skb_queue_head(&priv->rx_recycle, skb);
629 else
630 dev_kfree_skb(skb);
631
632 priv->tx_skbuff[entry] = NULL;
633 }
634
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000635 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700636
637 entry = (++priv->dirty_tx) % txsize;
638 }
639 if (unlikely(netif_queue_stopped(priv->dev) &&
640 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
641 netif_tx_lock(priv->dev);
642 if (netif_queue_stopped(priv->dev) &&
643 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
644 TX_DBG("%s: restart transmit\n", __func__);
645 netif_wake_queue(priv->dev);
646 }
647 netif_tx_unlock(priv->dev);
648 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700649}
650
651static inline void stmmac_enable_irq(struct stmmac_priv *priv)
652{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000653#ifdef CONFIG_STMMAC_TIMER
654 if (likely(priv->tm->enable))
655 priv->tm->timer_start(tmrate);
656 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700657#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000658 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700659}
660
661static inline void stmmac_disable_irq(struct stmmac_priv *priv)
662{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000663#ifdef CONFIG_STMMAC_TIMER
664 if (likely(priv->tm->enable))
665 priv->tm->timer_stop();
666 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700667#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000668 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700669}
670
671static int stmmac_has_work(struct stmmac_priv *priv)
672{
673 unsigned int has_work = 0;
674 int rxret, tx_work = 0;
675
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000676 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700677 (priv->cur_rx % priv->dma_rx_size));
678
679 if (priv->dirty_tx != priv->cur_tx)
680 tx_work = 1;
681
682 if (likely(!rxret || tx_work))
683 has_work = 1;
684
685 return has_work;
686}
687
688static inline void _stmmac_schedule(struct stmmac_priv *priv)
689{
690 if (likely(stmmac_has_work(priv))) {
691 stmmac_disable_irq(priv);
692 napi_schedule(&priv->napi);
693 }
694}
695
696#ifdef CONFIG_STMMAC_TIMER
697void stmmac_schedule(struct net_device *dev)
698{
699 struct stmmac_priv *priv = netdev_priv(dev);
700
701 priv->xstats.sched_timer_n++;
702
703 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700704}
705
706static void stmmac_no_timer_started(unsigned int x)
707{;
708};
709
710static void stmmac_no_timer_stopped(void)
711{;
712};
713#endif
714
715/**
716 * stmmac_tx_err:
717 * @priv: pointer to the private device structure
718 * Description: it cleans the descriptors and restarts the transmission
719 * in case of errors.
720 */
721static void stmmac_tx_err(struct stmmac_priv *priv)
722{
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000723
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700724 netif_stop_queue(priv->dev);
725
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000726 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700727 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000728 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700729 priv->dirty_tx = 0;
730 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000731 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700732
733 priv->dev->stats.tx_errors++;
734 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700735}
736
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000737
738static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700739{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000740 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700741
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000742 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000743 if (likely(status == handle_tx_rx))
744 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700745
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000746 else if (unlikely(status == tx_hard_error_bump_tc)) {
747 /* Try to bump up the dma threshold on this failure */
748 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
749 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000750 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000751 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700752 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000753 stmmac_tx_err(priv);
754 } else if (unlikely(status == tx_hard_error))
755 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700756}
757
758/**
759 * stmmac_open - open entry point of the driver
760 * @dev : pointer to the device structure.
761 * Description:
762 * This function is the open entry point of the driver.
763 * Return value:
764 * 0 on success and an appropriate (-)ve integer as defined in errno.h
765 * file on failure.
766 */
767static int stmmac_open(struct net_device *dev)
768{
769 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700770 int ret;
771
772 /* Check that the MAC address is valid. If its not, refuse
773 * to bring the device up. The user must specify an
774 * address using the following linux command:
775 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
776 if (!is_valid_ether_addr(dev->dev_addr)) {
777 random_ether_addr(dev->dev_addr);
778 pr_warning("%s: generated random MAC address %pM\n", dev->name,
779 dev->dev_addr);
780 }
781
782 stmmac_verify_args();
783
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700784#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000785 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700786 if (unlikely(priv->tm == NULL)) {
Frans Pop2381a552010-03-24 07:57:36 +0000787 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700788 return -ENOMEM;
789 }
790 priv->tm->freq = tmrate;
791
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000792 /* Test if the external timer can be actually used.
793 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700794 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000795 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700796 priv->tm->freq = 0;
797 priv->tm->timer_start = stmmac_no_timer_started;
798 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000799 } else
800 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700801#endif
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000802 ret = stmmac_init_phy(dev);
803 if (unlikely(ret)) {
804 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
805 goto open_error;
806 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700807
808 /* Create and initialize the TX/RX descriptors chains. */
809 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
810 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
811 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
812 init_dma_desc_rings(dev);
813
814 /* DMA initialization and SW reset */
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000815 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
816 priv->dma_tx_phy, priv->dma_rx_phy);
817 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700818 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000819 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700820 }
821
822 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000823 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +0000824 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000825 if (priv->plat->bus_setup)
826 priv->plat->bus_setup(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700827 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000828 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700829
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000830 priv->rx_coe = priv->hw->mac->rx_coe(priv->ioaddr);
831 if (priv->rx_coe)
832 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000833 if (priv->plat->tx_coe)
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000834 pr_info("\tTX Checksum insertion supported\n");
835
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700836 /* Initialise the MMC (if present) to disable all interrupts. */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000837 writel(0xffffffff, priv->ioaddr + MMC_HIGH_INTR_MASK);
838 writel(0xffffffff, priv->ioaddr + MMC_LOW_INTR_MASK);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700839
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000840 /* Request the IRQ lines */
841 ret = request_irq(dev->irq, stmmac_interrupt,
842 IRQF_SHARED, dev->name, dev);
843 if (unlikely(ret < 0)) {
844 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
845 __func__, dev->irq, ret);
846 goto open_error;
847 }
848
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700849 /* Enable the MAC Rx/Tx */
avisconti19449bf2010-10-25 18:58:14 +0000850 stmmac_enable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700851
852 /* Set the HW DMA mode and the COE */
853 stmmac_dma_operation_mode(priv);
854
855 /* Extra statistics */
856 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
857 priv->xstats.threshold = tc;
858
859 /* Start the ball rolling... */
860 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000861 priv->hw->dma->start_tx(priv->ioaddr);
862 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700863
864#ifdef CONFIG_STMMAC_TIMER
865 priv->tm->timer_start(tmrate);
866#endif
867 /* Dump DMA/MAC registers */
868 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000869 priv->hw->mac->dump_regs(priv->ioaddr);
870 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700871 }
872
873 if (priv->phydev)
874 phy_start(priv->phydev);
875
876 napi_enable(&priv->napi);
877 skb_queue_head_init(&priv->rx_recycle);
878 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000879
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700880 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000881
882open_error:
883#ifdef CONFIG_STMMAC_TIMER
884 kfree(priv->tm);
885#endif
886 if (priv->phydev)
887 phy_disconnect(priv->phydev);
888
889 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700890}
891
892/**
893 * stmmac_release - close entry point of the driver
894 * @dev : device pointer.
895 * Description:
896 * This is the stop entry point of the driver.
897 */
898static int stmmac_release(struct net_device *dev)
899{
900 struct stmmac_priv *priv = netdev_priv(dev);
901
902 /* Stop and disconnect the PHY */
903 if (priv->phydev) {
904 phy_stop(priv->phydev);
905 phy_disconnect(priv->phydev);
906 priv->phydev = NULL;
907 }
908
909 netif_stop_queue(dev);
910
911#ifdef CONFIG_STMMAC_TIMER
912 /* Stop and release the timer */
913 stmmac_close_ext_timer();
914 if (priv->tm != NULL)
915 kfree(priv->tm);
916#endif
917 napi_disable(&priv->napi);
918 skb_queue_purge(&priv->rx_recycle);
919
920 /* Free the IRQ lines */
921 free_irq(dev->irq, dev);
922
923 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000924 priv->hw->dma->stop_tx(priv->ioaddr);
925 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700926
927 /* Release and free the Rx/Tx resources */
928 free_dma_desc_resources(priv);
929
avisconti19449bf2010-10-25 18:58:14 +0000930 /* Disable the MAC Rx/Tx */
931 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700932
933 netif_carrier_off(dev);
934
935 return 0;
936}
937
938/*
939 * To perform emulated hardware segmentation on skb.
940 */
941static int stmmac_sw_tso(struct stmmac_priv *priv, struct sk_buff *skb)
942{
943 struct sk_buff *segs, *curr_skb;
944 int gso_segs = skb_shinfo(skb)->gso_segs;
945
946 /* Estimate the number of fragments in the worst case */
947 if (unlikely(stmmac_tx_avail(priv) < gso_segs)) {
948 netif_stop_queue(priv->dev);
949 TX_DBG(KERN_ERR "%s: TSO BUG! Tx Ring full when queue awake\n",
950 __func__);
951 if (stmmac_tx_avail(priv) < gso_segs)
952 return NETDEV_TX_BUSY;
953
954 netif_wake_queue(priv->dev);
955 }
956 TX_DBG("\tstmmac_sw_tso: segmenting: skb %p (len %d)\n",
957 skb, skb->len);
958
959 segs = skb_gso_segment(skb, priv->dev->features & ~NETIF_F_TSO);
Tobias Klauser0e51d672010-12-09 04:50:22 +0000960 if (IS_ERR(segs))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700961 goto sw_tso_end;
962
963 do {
964 curr_skb = segs;
965 segs = segs->next;
966 TX_DBG("\t\tcurrent skb->len: %d, *curr %p,"
967 "*next %p\n", curr_skb->len, curr_skb, segs);
968 curr_skb->next = NULL;
969 stmmac_xmit(curr_skb, priv->dev);
970 } while (segs);
971
972sw_tso_end:
973 dev_kfree_skb(skb);
974
975 return NETDEV_TX_OK;
976}
977
978static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
979 struct net_device *dev,
980 int csum_insertion)
981{
982 struct stmmac_priv *priv = netdev_priv(dev);
983 unsigned int nopaged_len = skb_headlen(skb);
984 unsigned int txsize = priv->dma_tx_size;
985 unsigned int entry = priv->cur_tx % txsize;
986 struct dma_desc *desc = priv->dma_tx + entry;
987
988 if (nopaged_len > BUF_SIZE_8KiB) {
989
990 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
991
992 desc->des2 = dma_map_single(priv->device, skb->data,
993 BUF_SIZE_8KiB, DMA_TO_DEVICE);
994 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000995 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
996 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700997
998 entry = (++priv->cur_tx) % txsize;
999 desc = priv->dma_tx + entry;
1000
1001 desc->des2 = dma_map_single(priv->device,
1002 skb->data + BUF_SIZE_8KiB,
1003 buf2_size, DMA_TO_DEVICE);
1004 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001005 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
1006 csum_insertion);
1007 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001008 priv->tx_skbuff[entry] = NULL;
1009 } else {
1010 desc->des2 = dma_map_single(priv->device, skb->data,
1011 nopaged_len, DMA_TO_DEVICE);
1012 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001013 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1014 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001015 }
1016 return entry;
1017}
1018
1019/**
1020 * stmmac_xmit:
1021 * @skb : the socket buffer
1022 * @dev : device pointer
1023 * Description : Tx entry point of the driver.
1024 */
1025static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1026{
1027 struct stmmac_priv *priv = netdev_priv(dev);
1028 unsigned int txsize = priv->dma_tx_size;
1029 unsigned int entry;
1030 int i, csum_insertion = 0;
1031 int nfrags = skb_shinfo(skb)->nr_frags;
1032 struct dma_desc *desc, *first;
1033
1034 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1035 if (!netif_queue_stopped(dev)) {
1036 netif_stop_queue(dev);
1037 /* This is a hard error, log it. */
1038 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1039 __func__);
1040 }
1041 return NETDEV_TX_BUSY;
1042 }
1043
1044 entry = priv->cur_tx % txsize;
1045
1046#ifdef STMMAC_XMIT_DEBUG
1047 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1048 pr_info("stmmac xmit:\n"
1049 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1050 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1051 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1052 !skb_is_gso(skb) ? "isn't" : "is");
1053#endif
1054
1055 if (unlikely(skb_is_gso(skb)))
1056 return stmmac_sw_tso(priv, skb);
1057
1058 if (likely((skb->ip_summed == CHECKSUM_PARTIAL))) {
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001059 if (unlikely((!priv->plat->tx_coe) ||
1060 (priv->no_csum_insertion)))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001061 skb_checksum_help(skb);
1062 else
1063 csum_insertion = 1;
1064 }
1065
1066 desc = priv->dma_tx + entry;
1067 first = desc;
1068
1069#ifdef STMMAC_XMIT_DEBUG
1070 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1071 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1072 "\t\tn_frags: %d, ip_summed: %d\n",
1073 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1074#endif
1075 priv->tx_skbuff[entry] = skb;
1076 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1077 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1078 desc = priv->dma_tx + entry;
1079 } else {
1080 unsigned int nopaged_len = skb_headlen(skb);
1081 desc->des2 = dma_map_single(priv->device, skb->data,
1082 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001083 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1084 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001085 }
1086
1087 for (i = 0; i < nfrags; i++) {
1088 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1089 int len = frag->size;
1090
1091 entry = (++priv->cur_tx) % txsize;
1092 desc = priv->dma_tx + entry;
1093
1094 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1095 desc->des2 = dma_map_page(priv->device, frag->page,
1096 frag->page_offset,
1097 len, DMA_TO_DEVICE);
1098 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001099 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
1100 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001101 }
1102
1103 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001104 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001105
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001106#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001107 /* Clean IC while using timer */
1108 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001109 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001110#endif
1111 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001112 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001113
1114 priv->cur_tx++;
1115
1116#ifdef STMMAC_XMIT_DEBUG
1117 if (netif_msg_pktdata(priv)) {
1118 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1119 "first=%p, nfrags=%d\n",
1120 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1121 entry, first, nfrags);
1122 display_ring(priv->dma_tx, txsize);
1123 pr_info(">>> frame to be transmitted: ");
1124 print_pkt(skb->data, skb->len);
1125 }
1126#endif
1127 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1128 TX_DBG("%s: stop transmitted packets\n", __func__);
1129 netif_stop_queue(dev);
1130 }
1131
1132 dev->stats.tx_bytes += skb->len;
1133
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001134 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001135
1136 return NETDEV_TX_OK;
1137}
1138
1139static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1140{
1141 unsigned int rxsize = priv->dma_rx_size;
1142 int bfsize = priv->dma_buf_sz;
1143 struct dma_desc *p = priv->dma_rx;
1144
1145 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1146 unsigned int entry = priv->dirty_rx % rxsize;
1147 if (likely(priv->rx_skbuff[entry] == NULL)) {
1148 struct sk_buff *skb;
1149
1150 skb = __skb_dequeue(&priv->rx_recycle);
1151 if (skb == NULL)
1152 skb = netdev_alloc_skb_ip_align(priv->dev,
1153 bfsize);
1154
1155 if (unlikely(skb == NULL))
1156 break;
1157
1158 priv->rx_skbuff[entry] = skb;
1159 priv->rx_skbuff_dma[entry] =
1160 dma_map_single(priv->device, skb->data, bfsize,
1161 DMA_FROM_DEVICE);
1162
1163 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001164 if (unlikely(priv->plat->has_gmac)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001165 if (bfsize >= BUF_SIZE_8KiB)
1166 (p + entry)->des3 =
1167 (p + entry)->des2 + BUF_SIZE_8KiB;
1168 }
1169 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1170 }
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001171 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001172 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001173}
1174
1175static int stmmac_rx(struct stmmac_priv *priv, int limit)
1176{
1177 unsigned int rxsize = priv->dma_rx_size;
1178 unsigned int entry = priv->cur_rx % rxsize;
1179 unsigned int next_entry;
1180 unsigned int count = 0;
1181 struct dma_desc *p = priv->dma_rx + entry;
1182 struct dma_desc *p_next;
1183
1184#ifdef STMMAC_RX_DEBUG
1185 if (netif_msg_hw(priv)) {
1186 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1187 display_ring(priv->dma_rx, rxsize);
1188 }
1189#endif
1190 count = 0;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001191 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001192 int status;
1193
1194 if (count >= limit)
1195 break;
1196
1197 count++;
1198
1199 next_entry = (++priv->cur_rx) % rxsize;
1200 p_next = priv->dma_rx + next_entry;
1201 prefetch(p_next);
1202
1203 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001204 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1205 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001206 if (unlikely(status == discard_frame))
1207 priv->dev->stats.rx_errors++;
1208 else {
1209 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001210 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001211
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001212 frame_len = priv->hw->desc->get_rx_frame_len(p);
1213 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1214 * Type frames (LLC/LLC-SNAP) */
1215 if (unlikely(status != llc_snap))
1216 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001217#ifdef STMMAC_RX_DEBUG
1218 if (frame_len > ETH_FRAME_LEN)
1219 pr_debug("\tRX frame size %d, COE status: %d\n",
1220 frame_len, status);
1221
1222 if (netif_msg_hw(priv))
1223 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1224 p, entry, p->des2);
1225#endif
1226 skb = priv->rx_skbuff[entry];
1227 if (unlikely(!skb)) {
1228 pr_err("%s: Inconsistent Rx descriptor chain\n",
1229 priv->dev->name);
1230 priv->dev->stats.rx_dropped++;
1231 break;
1232 }
1233 prefetch(skb->data - NET_IP_ALIGN);
1234 priv->rx_skbuff[entry] = NULL;
1235
1236 skb_put(skb, frame_len);
1237 dma_unmap_single(priv->device,
1238 priv->rx_skbuff_dma[entry],
1239 priv->dma_buf_sz, DMA_FROM_DEVICE);
1240#ifdef STMMAC_RX_DEBUG
1241 if (netif_msg_pktdata(priv)) {
1242 pr_info(" frame received (%dbytes)", frame_len);
1243 print_pkt(skb->data, frame_len);
1244 }
1245#endif
1246 skb->protocol = eth_type_trans(skb, priv->dev);
1247
1248 if (unlikely(status == csum_none)) {
1249 /* always for the old mac 10/100 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001250 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001251 netif_receive_skb(skb);
1252 } else {
1253 skb->ip_summed = CHECKSUM_UNNECESSARY;
1254 napi_gro_receive(&priv->napi, skb);
1255 }
1256
1257 priv->dev->stats.rx_packets++;
1258 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001259 }
1260 entry = next_entry;
1261 p = p_next; /* use prefetched values */
1262 }
1263
1264 stmmac_rx_refill(priv);
1265
1266 priv->xstats.rx_pkt_n += count;
1267
1268 return count;
1269}
1270
1271/**
1272 * stmmac_poll - stmmac poll method (NAPI)
1273 * @napi : pointer to the napi structure.
1274 * @budget : maximum number of packets that the current CPU can receive from
1275 * all interfaces.
1276 * Description :
1277 * This function implements the the reception process.
1278 * Also it runs the TX completion thread
1279 */
1280static int stmmac_poll(struct napi_struct *napi, int budget)
1281{
1282 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1283 int work_done = 0;
1284
1285 priv->xstats.poll_n++;
1286 stmmac_tx(priv);
1287 work_done = stmmac_rx(priv, budget);
1288
1289 if (work_done < budget) {
1290 napi_complete(napi);
1291 stmmac_enable_irq(priv);
1292 }
1293 return work_done;
1294}
1295
1296/**
1297 * stmmac_tx_timeout
1298 * @dev : Pointer to net device structure
1299 * Description: this function is called when a packet transmission fails to
1300 * complete within a reasonable tmrate. The driver will mark the error in the
1301 * netdev structure and arrange for the device to be reset to a sane state
1302 * in order to transmit a new packet.
1303 */
1304static void stmmac_tx_timeout(struct net_device *dev)
1305{
1306 struct stmmac_priv *priv = netdev_priv(dev);
1307
1308 /* Clear Tx resources and restart transmitting again */
1309 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001310}
1311
1312/* Configuration changes (passed on by ifconfig) */
1313static int stmmac_config(struct net_device *dev, struct ifmap *map)
1314{
1315 if (dev->flags & IFF_UP) /* can't act on a running interface */
1316 return -EBUSY;
1317
1318 /* Don't allow changing the I/O address */
1319 if (map->base_addr != dev->base_addr) {
1320 pr_warning("%s: can't change I/O address\n", dev->name);
1321 return -EOPNOTSUPP;
1322 }
1323
1324 /* Don't allow changing the IRQ */
1325 if (map->irq != dev->irq) {
1326 pr_warning("%s: can't change IRQ number %d\n",
1327 dev->name, dev->irq);
1328 return -EOPNOTSUPP;
1329 }
1330
1331 /* ignore other fields */
1332 return 0;
1333}
1334
1335/**
1336 * stmmac_multicast_list - entry point for multicast addressing
1337 * @dev : pointer to the device structure
1338 * Description:
1339 * This function is a driver entry point which gets called by the kernel
1340 * whenever multicast addresses must be enabled/disabled.
1341 * Return value:
1342 * void.
1343 */
1344static void stmmac_multicast_list(struct net_device *dev)
1345{
1346 struct stmmac_priv *priv = netdev_priv(dev);
1347
1348 spin_lock(&priv->lock);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001349 priv->hw->mac->set_filter(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001350 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001351}
1352
1353/**
1354 * stmmac_change_mtu - entry point to change MTU size for the device.
1355 * @dev : device pointer.
1356 * @new_mtu : the new MTU size for the device.
1357 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1358 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1359 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1360 * Return value:
1361 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1362 * file on failure.
1363 */
1364static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1365{
1366 struct stmmac_priv *priv = netdev_priv(dev);
1367 int max_mtu;
1368
1369 if (netif_running(dev)) {
1370 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1371 return -EBUSY;
1372 }
1373
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001374 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001375 max_mtu = JUMBO_LEN;
1376 else
1377 max_mtu = ETH_DATA_LEN;
1378
1379 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1380 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1381 return -EINVAL;
1382 }
1383
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001384 /* Some GMAC devices have a bugged Jumbo frame support that
1385 * needs to have the Tx COE disabled for oversized frames
1386 * (due to limited buffer sizes). In this case we disable
1387 * the TX csum insertionin the TDES and not use SF. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001388 if ((priv->plat->bugged_jumbo) && (priv->dev->mtu > ETH_DATA_LEN))
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001389 priv->no_csum_insertion = 1;
1390 else
1391 priv->no_csum_insertion = 0;
1392
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001393 dev->mtu = new_mtu;
1394
1395 return 0;
1396}
1397
1398static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1399{
1400 struct net_device *dev = (struct net_device *)dev_id;
1401 struct stmmac_priv *priv = netdev_priv(dev);
1402
1403 if (unlikely(!dev)) {
1404 pr_err("%s: invalid dev pointer\n", __func__);
1405 return IRQ_NONE;
1406 }
1407
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001408 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001409 /* To handle GMAC own interrupts */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001410 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001411
1412 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001413
1414 return IRQ_HANDLED;
1415}
1416
1417#ifdef CONFIG_NET_POLL_CONTROLLER
1418/* Polling receive - used by NETCONSOLE and other diagnostic tools
1419 * to allow network I/O with interrupts disabled. */
1420static void stmmac_poll_controller(struct net_device *dev)
1421{
1422 disable_irq(dev->irq);
1423 stmmac_interrupt(dev->irq, dev);
1424 enable_irq(dev->irq);
1425}
1426#endif
1427
1428/**
1429 * stmmac_ioctl - Entry point for the Ioctl
1430 * @dev: Device pointer.
1431 * @rq: An IOCTL specefic structure, that can contain a pointer to
1432 * a proprietary structure used to pass information to the driver.
1433 * @cmd: IOCTL command
1434 * Description:
1435 * Currently there are no special functionality supported in IOCTL, just the
1436 * phy_mii_ioctl(...) can be invoked.
1437 */
1438static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1439{
1440 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001441 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001442
1443 if (!netif_running(dev))
1444 return -EINVAL;
1445
Richard Cochran28b04112010-07-17 08:48:55 +00001446 if (!priv->phydev)
1447 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001448
Richard Cochran28b04112010-07-17 08:48:55 +00001449 spin_lock(&priv->lock);
1450 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1451 spin_unlock(&priv->lock);
1452
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001453 return ret;
1454}
1455
1456#ifdef STMMAC_VLAN_TAG_USED
1457static void stmmac_vlan_rx_register(struct net_device *dev,
1458 struct vlan_group *grp)
1459{
1460 struct stmmac_priv *priv = netdev_priv(dev);
1461
1462 DBG(probe, INFO, "%s: Setting vlgrp to %p\n", dev->name, grp);
1463
1464 spin_lock(&priv->lock);
1465 priv->vlgrp = grp;
1466 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001467}
1468#endif
1469
1470static const struct net_device_ops stmmac_netdev_ops = {
1471 .ndo_open = stmmac_open,
1472 .ndo_start_xmit = stmmac_xmit,
1473 .ndo_stop = stmmac_release,
1474 .ndo_change_mtu = stmmac_change_mtu,
1475 .ndo_set_multicast_list = stmmac_multicast_list,
1476 .ndo_tx_timeout = stmmac_tx_timeout,
1477 .ndo_do_ioctl = stmmac_ioctl,
1478 .ndo_set_config = stmmac_config,
1479#ifdef STMMAC_VLAN_TAG_USED
1480 .ndo_vlan_rx_register = stmmac_vlan_rx_register,
1481#endif
1482#ifdef CONFIG_NET_POLL_CONTROLLER
1483 .ndo_poll_controller = stmmac_poll_controller,
1484#endif
1485 .ndo_set_mac_address = eth_mac_addr,
1486};
1487
1488/**
1489 * stmmac_probe - Initialization of the adapter .
1490 * @dev : device pointer
1491 * Description: The function initializes the network device structure for
1492 * the STMMAC driver. It also calls the low level routines
1493 * in order to init the HW (i.e. the DMA engine)
1494 */
1495static int stmmac_probe(struct net_device *dev)
1496{
1497 int ret = 0;
1498 struct stmmac_priv *priv = netdev_priv(dev);
1499
1500 ether_setup(dev);
1501
1502 dev->netdev_ops = &stmmac_netdev_ops;
1503 stmmac_set_ethtool_ops(dev);
1504
Michał Mirosław79032642010-11-30 06:38:00 +00001505 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA |
1506 NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001507 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1508#ifdef STMMAC_VLAN_TAG_USED
1509 /* Both mac100 and gmac support receive VLAN tag detection */
1510 dev->features |= NETIF_F_HW_VLAN_RX;
1511#endif
1512 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1513
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001514 if (flow_ctrl)
1515 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1516
1517 priv->pause = pause;
1518 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1519
1520 /* Get the MAC address */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001521 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1522 dev->dev_addr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001523
1524 if (!is_valid_ether_addr(dev->dev_addr))
1525 pr_warning("\tno valid MAC address;"
1526 "please, use ifconfig or nwhwconfig!\n");
1527
Vlad Lunguf8e96162010-11-29 22:52:52 +00001528 spin_lock_init(&priv->lock);
1529
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001530 ret = register_netdev(dev);
1531 if (ret) {
1532 pr_err("%s: ERROR %i registering the device\n",
1533 __func__, ret);
1534 return -ENODEV;
1535 }
1536
1537 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1538 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
Michał Mirosław79032642010-11-30 06:38:00 +00001539 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001540
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001541 return ret;
1542}
1543
1544/**
1545 * stmmac_mac_device_setup
1546 * @dev : device pointer
1547 * Description: select and initialise the mac device (mac100 or Gmac).
1548 */
1549static int stmmac_mac_device_setup(struct net_device *dev)
1550{
1551 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001552
1553 struct mac_device_info *device;
1554
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001555 if (priv->plat->has_gmac)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001556 device = dwmac1000_setup(priv->ioaddr);
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001557 else
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001558 device = dwmac100_setup(priv->ioaddr);
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001559
Dan Carpenter1ff21902010-07-22 01:16:48 +00001560 if (!device)
1561 return -ENOMEM;
1562
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001563 if (priv->plat->enh_desc) {
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001564 device->desc = &enh_desc_ops;
1565 pr_info("\tEnhanced descriptor structure\n");
1566 } else
Giuseppe CAVALLARO56b106a2010-04-13 20:21:12 +00001567 device->desc = &ndesc_ops;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001568
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001569 priv->hw = device;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001570
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001571 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001572 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001573 enable_irq_wake(dev->irq);
1574 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001575
1576 return 0;
1577}
1578
1579static int stmmacphy_dvr_probe(struct platform_device *pdev)
1580{
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001581 struct plat_stmmacphy_data *plat_dat = pdev->dev.platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001582
1583 pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
1584 plat_dat->bus_id);
1585
1586 return 0;
1587}
1588
1589static int stmmacphy_dvr_remove(struct platform_device *pdev)
1590{
1591 return 0;
1592}
1593
1594static struct platform_driver stmmacphy_driver = {
1595 .driver = {
1596 .name = PHY_RESOURCE_NAME,
1597 },
1598 .probe = stmmacphy_dvr_probe,
1599 .remove = stmmacphy_dvr_remove,
1600};
1601
1602/**
1603 * stmmac_associate_phy
1604 * @dev: pointer to device structure
1605 * @data: points to the private structure.
1606 * Description: Scans through all the PHYs we have registered and checks if
1607 * any are associated with our MAC. If so, then just fill in
1608 * the blanks in our local context structure
1609 */
1610static int stmmac_associate_phy(struct device *dev, void *data)
1611{
1612 struct stmmac_priv *priv = (struct stmmac_priv *)data;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001613 struct plat_stmmacphy_data *plat_dat = dev->platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001614
1615 DBG(probe, DEBUG, "%s: checking phy for bus %d\n", __func__,
1616 plat_dat->bus_id);
1617
1618 /* Check that this phy is for the MAC being initialised */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001619 if (priv->plat->bus_id != plat_dat->bus_id)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001620 return 0;
1621
1622 /* OK, this PHY is connected to the MAC.
1623 Go ahead and get the parameters */
1624 DBG(probe, DEBUG, "%s: OK. Found PHY config\n", __func__);
1625 priv->phy_irq =
1626 platform_get_irq_byname(to_platform_device(dev), "phyirq");
1627 DBG(probe, DEBUG, "%s: PHY irq on bus %d is %d\n", __func__,
1628 plat_dat->bus_id, priv->phy_irq);
1629
1630 /* Override with kernel parameters if supplied XXX CRS XXX
1631 * this needs to have multiple instances */
1632 if ((phyaddr >= 0) && (phyaddr <= 31))
1633 plat_dat->phy_addr = phyaddr;
1634
1635 priv->phy_addr = plat_dat->phy_addr;
1636 priv->phy_mask = plat_dat->phy_mask;
1637 priv->phy_interface = plat_dat->interface;
1638 priv->phy_reset = plat_dat->phy_reset;
1639
1640 DBG(probe, DEBUG, "%s: exiting\n", __func__);
1641 return 1; /* forces exit of driver_for_each_device() */
1642}
1643
1644/**
1645 * stmmac_dvr_probe
1646 * @pdev: platform device pointer
1647 * Description: the driver is initialized through platform_device.
1648 */
1649static int stmmac_dvr_probe(struct platform_device *pdev)
1650{
1651 int ret = 0;
1652 struct resource *res;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001653 void __iomem *addr = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001654 struct net_device *ndev = NULL;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001655 struct stmmac_priv *priv = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001656 struct plat_stmmacenet_data *plat_dat;
1657
1658 pr_info("STMMAC driver:\n\tplatform registration... ");
1659 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001660 if (!res)
1661 return -ENODEV;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001662 pr_info("\tdone!\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001663
Dan Carpenterb6222682010-04-07 21:50:08 -07001664 if (!request_mem_region(res->start, resource_size(res),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001665 pdev->name)) {
1666 pr_err("%s: ERROR: memory allocation failed"
1667 "cannot get the I/O addr 0x%x\n",
1668 __func__, (unsigned int)res->start);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001669 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001670 }
1671
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001672 addr = ioremap(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001673 if (!addr) {
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001674 pr_err("%s: ERROR: memory mapping failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001675 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001676 goto out_release_region;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001677 }
1678
1679 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1680 if (!ndev) {
1681 pr_err("%s: ERROR: allocating the device\n", __func__);
1682 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001683 goto out_unmap;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001684 }
1685
1686 SET_NETDEV_DEV(ndev, &pdev->dev);
1687
1688 /* Get the MAC information */
1689 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1690 if (ndev->irq == -ENXIO) {
1691 pr_err("%s: ERROR: MAC IRQ configuration "
1692 "information not found\n", __func__);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001693 ret = -ENXIO;
1694 goto out_free_ndev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001695 }
1696
1697 priv = netdev_priv(ndev);
1698 priv->device = &(pdev->dev);
1699 priv->dev = ndev;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001700 plat_dat = pdev->dev.platform_data;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001701
1702 priv->plat = plat_dat;
1703
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001704 priv->ioaddr = addr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001705
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001706 /* PMT module is not integrated in all the MAC devices. */
1707 if (plat_dat->pmt) {
1708 pr_info("\tPMT module supported\n");
1709 device_set_wakeup_capable(&pdev->dev, 1);
1710 }
1711
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001712 platform_set_drvdata(pdev, ndev);
1713
1714 /* Set the I/O base addr */
1715 ndev->base_addr = (unsigned long)addr;
1716
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001717 /* Custom initialisation */
1718 if (priv->plat->init) {
1719 ret = priv->plat->init(pdev);
1720 if (unlikely(ret))
Dan Carpenter34a52f32010-12-20 21:34:56 +00001721 goto out_free_ndev;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001722 }
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001723
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001724 /* MAC HW revice detection */
1725 ret = stmmac_mac_device_setup(ndev);
1726 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001727 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001728
1729 /* Network Device Registration */
1730 ret = stmmac_probe(ndev);
1731 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001732 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001733
1734 /* associate a PHY - it is provided by another platform bus */
1735 if (!driver_for_each_device
1736 (&(stmmacphy_driver.driver), NULL, (void *)priv,
1737 stmmac_associate_phy)) {
1738 pr_err("No PHY device is associated with this MAC!\n");
1739 ret = -ENODEV;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001740 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001741 }
1742
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001743 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
David S. Miller1f0f6382010-08-30 21:55:17 -07001744 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1745 pdev->id, ndev->irq, addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001746
1747 /* MDIO bus Registration */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001748 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001749 ret = stmmac_mdio_register(ndev);
1750 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001751 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001752 pr_debug("registered!\n");
Dan Carpenter34a52f32010-12-20 21:34:56 +00001753 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001754
Dan Carpenter34a52f32010-12-20 21:34:56 +00001755out_unregister:
1756 unregister_netdev(ndev);
1757out_plat_exit:
1758 if (priv->plat->exit)
1759 priv->plat->exit(pdev);
1760out_free_ndev:
1761 free_netdev(ndev);
1762 platform_set_drvdata(pdev, NULL);
1763out_unmap:
1764 iounmap(addr);
1765out_release_region:
1766 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001767
1768 return ret;
1769}
1770
1771/**
1772 * stmmac_dvr_remove
1773 * @pdev: platform device pointer
1774 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1775 * changes the link status, releases the DMA descriptor rings,
1776 * unregisters the MDIO bus and unmaps the allocated memory.
1777 */
1778static int stmmac_dvr_remove(struct platform_device *pdev)
1779{
1780 struct net_device *ndev = platform_get_drvdata(pdev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001781 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001782 struct resource *res;
1783
1784 pr_info("%s:\n\tremoving driver", __func__);
1785
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001786 priv->hw->dma->stop_rx(priv->ioaddr);
1787 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001788
avisconti19449bf2010-10-25 18:58:14 +00001789 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001790
1791 netif_carrier_off(ndev);
1792
1793 stmmac_mdio_unregister(ndev);
1794
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001795 if (priv->plat->exit)
1796 priv->plat->exit(pdev);
1797
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001798 platform_set_drvdata(pdev, NULL);
1799 unregister_netdev(ndev);
1800
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001801 iounmap((void *)priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001802 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001803 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001804
1805 free_netdev(ndev);
1806
1807 return 0;
1808}
1809
1810#ifdef CONFIG_PM
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001811static int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001812{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001813 struct net_device *ndev = dev_get_drvdata(dev);
1814 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001815 int dis_ic = 0;
1816
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001817 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001818 return 0;
1819
1820 spin_lock(&priv->lock);
1821
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001822 netif_device_detach(ndev);
1823 netif_stop_queue(ndev);
1824 if (priv->phydev)
1825 phy_stop(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001826
1827#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001828 priv->tm->timer_stop();
1829 if (likely(priv->tm->enable))
1830 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001831#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001832 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001833
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001834 /* Stop TX/RX DMA */
1835 priv->hw->dma->stop_tx(priv->ioaddr);
1836 priv->hw->dma->stop_rx(priv->ioaddr);
1837 /* Clear the Rx/Tx descriptors */
1838 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1839 dis_ic);
1840 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001841
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001842 /* Enable Power down mode by programming the PMT regs */
1843 if (device_may_wakeup(priv->device))
1844 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
1845 else
1846 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001847
1848 spin_unlock(&priv->lock);
1849 return 0;
1850}
1851
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001852static int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001853{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001854 struct net_device *ndev = dev_get_drvdata(dev);
1855 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001856
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001857 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001858 return 0;
1859
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02001860 spin_lock(&priv->lock);
1861
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001862 /* Power Down bit, into the PM register, is cleared
1863 * automatically as soon as a magic packet or a Wake-up frame
1864 * is received. Anyway, it's better to manually clear
1865 * this bit because it can generate problems while resuming
1866 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001867 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001868 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001869
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001870 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001871
1872 /* Enable the MAC and DMA */
avisconti19449bf2010-10-25 18:58:14 +00001873 stmmac_enable_mac(priv->ioaddr);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001874 priv->hw->dma->start_tx(priv->ioaddr);
1875 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001876
1877#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001878 if (likely(priv->tm->enable))
1879 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001880#endif
1881 napi_enable(&priv->napi);
1882
1883 if (priv->phydev)
1884 phy_start(priv->phydev);
1885
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001886 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001887
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001888 spin_unlock(&priv->lock);
1889 return 0;
1890}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001891
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001892static int stmmac_freeze(struct device *dev)
1893{
1894 struct net_device *ndev = dev_get_drvdata(dev);
1895
1896 if (!ndev || !netif_running(ndev))
1897 return 0;
1898
1899 return stmmac_release(ndev);
1900}
1901
1902static int stmmac_restore(struct device *dev)
1903{
1904 struct net_device *ndev = dev_get_drvdata(dev);
1905
1906 if (!ndev || !netif_running(ndev))
1907 return 0;
1908
1909 return stmmac_open(ndev);
1910}
1911
1912static const struct dev_pm_ops stmmac_pm_ops = {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001913 .suspend = stmmac_suspend,
1914 .resume = stmmac_resume,
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001915 .freeze = stmmac_freeze,
1916 .thaw = stmmac_restore,
1917 .restore = stmmac_restore,
1918};
1919#else
1920static const struct dev_pm_ops stmmac_pm_ops;
1921#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001922
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001923static struct platform_driver stmmac_driver = {
1924 .probe = stmmac_dvr_probe,
1925 .remove = stmmac_dvr_remove,
1926 .driver = {
1927 .name = STMMAC_RESOURCE_NAME,
1928 .owner = THIS_MODULE,
1929 .pm = &stmmac_pm_ops,
1930 },
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001931};
1932
1933/**
1934 * stmmac_init_module - Entry point for the driver
1935 * Description: This function is the entry point for the driver.
1936 */
1937static int __init stmmac_init_module(void)
1938{
1939 int ret;
1940
1941 if (platform_driver_register(&stmmacphy_driver)) {
1942 pr_err("No PHY devices registered!\n");
1943 return -ENODEV;
1944 }
1945
1946 ret = platform_driver_register(&stmmac_driver);
1947 return ret;
1948}
1949
1950/**
1951 * stmmac_cleanup_module - Cleanup routine for the driver
1952 * Description: This function is the cleanup routine for the driver.
1953 */
1954static void __exit stmmac_cleanup_module(void)
1955{
1956 platform_driver_unregister(&stmmacphy_driver);
1957 platform_driver_unregister(&stmmac_driver);
1958}
1959
1960#ifndef MODULE
1961static int __init stmmac_cmdline_opt(char *str)
1962{
1963 char *opt;
1964
1965 if (!str || !*str)
1966 return -EINVAL;
1967 while ((opt = strsep(&str, ",")) != NULL) {
1968 if (!strncmp(opt, "debug:", 6))
1969 strict_strtoul(opt + 6, 0, (unsigned long *)&debug);
1970 else if (!strncmp(opt, "phyaddr:", 8))
1971 strict_strtoul(opt + 8, 0, (unsigned long *)&phyaddr);
1972 else if (!strncmp(opt, "dma_txsize:", 11))
1973 strict_strtoul(opt + 11, 0,
1974 (unsigned long *)&dma_txsize);
1975 else if (!strncmp(opt, "dma_rxsize:", 11))
1976 strict_strtoul(opt + 11, 0,
1977 (unsigned long *)&dma_rxsize);
1978 else if (!strncmp(opt, "buf_sz:", 7))
1979 strict_strtoul(opt + 7, 0, (unsigned long *)&buf_sz);
1980 else if (!strncmp(opt, "tc:", 3))
1981 strict_strtoul(opt + 3, 0, (unsigned long *)&tc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001982 else if (!strncmp(opt, "watchdog:", 9))
1983 strict_strtoul(opt + 9, 0, (unsigned long *)&watchdog);
1984 else if (!strncmp(opt, "flow_ctrl:", 10))
1985 strict_strtoul(opt + 10, 0,
1986 (unsigned long *)&flow_ctrl);
1987 else if (!strncmp(opt, "pause:", 6))
1988 strict_strtoul(opt + 6, 0, (unsigned long *)&pause);
1989#ifdef CONFIG_STMMAC_TIMER
1990 else if (!strncmp(opt, "tmrate:", 7))
1991 strict_strtoul(opt + 7, 0, (unsigned long *)&tmrate);
1992#endif
1993 }
1994 return 0;
1995}
1996
1997__setup("stmmaceth=", stmmac_cmdline_opt);
1998#endif
1999
2000module_init(stmmac_init_module);
2001module_exit(stmmac_cleanup_module);
2002
2003MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
2004MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2005MODULE_LICENSE("GPL");