]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/stmmac/stmmac_main.c
stmmac: new descriptor field for the driver's platform
[net-next-2.6.git] / drivers / net / stmmac / stmmac_main.c
CommitLineData
47dd7a54
GC
1/*******************************************************************************
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>
47dd7a54
GC
35#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>
5a0e3ad6 47#include <linux/slab.h>
47dd7a54
GC
48#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
137/* In case of Giga ETH, we can enable/disable the COE for the
138 * transmit HW checksum computation.
139 * Note that, if tx csum is off in HW, SG will be still supported. */
140static int tx_coe = HW_CSUM;
141module_param(tx_coe, int, S_IRUGO | S_IWUSR);
142MODULE_PARM_DESC(tx_coe, "GMAC COE type 2 [on/off]");
143
144static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
145 NETIF_MSG_LINK | NETIF_MSG_IFUP |
146 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
147
148static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
149static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev);
150
151/**
152 * stmmac_verify_args - verify the driver parameters.
153 * Description: it verifies if some wrong parameter is passed to the driver.
154 * Note that wrong parameters are replaced with the default values.
155 */
156static void stmmac_verify_args(void)
157{
158 if (unlikely(watchdog < 0))
159 watchdog = TX_TIMEO;
160 if (unlikely(dma_rxsize < 0))
161 dma_rxsize = DMA_RX_SIZE;
162 if (unlikely(dma_txsize < 0))
163 dma_txsize = DMA_TX_SIZE;
164 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
165 buf_sz = DMA_BUFFER_SIZE;
166 if (unlikely(flow_ctrl > 1))
167 flow_ctrl = FLOW_AUTO;
168 else if (likely(flow_ctrl < 0))
169 flow_ctrl = FLOW_OFF;
170 if (unlikely((pause < 0) || (pause > 0xffff)))
171 pause = PAUSE_TIME;
172
173 return;
174}
175
176#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
177static void print_pkt(unsigned char *buf, int len)
178{
179 int j;
180 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
181 for (j = 0; j < len; j++) {
182 if ((j % 16) == 0)
183 pr_info("\n %03x:", j);
184 pr_info(" %02x", buf[j]);
185 }
186 pr_info("\n");
187 return;
188}
189#endif
190
191/* minimum number of free TX descriptors required to wake up TX process */
192#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
193
194static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
195{
196 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
197}
198
199/**
200 * stmmac_adjust_link
201 * @dev: net device structure
202 * Description: it adjusts the link parameters.
203 */
204static void stmmac_adjust_link(struct net_device *dev)
205{
206 struct stmmac_priv *priv = netdev_priv(dev);
207 struct phy_device *phydev = priv->phydev;
208 unsigned long ioaddr = dev->base_addr;
209 unsigned long flags;
210 int new_state = 0;
211 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
212
213 if (phydev == NULL)
214 return;
215
216 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
217 phydev->addr, phydev->link);
218
219 spin_lock_irqsave(&priv->lock, flags);
220 if (phydev->link) {
221 u32 ctrl = readl(ioaddr + MAC_CTRL_REG);
222
223 /* Now we make sure that we can be in full duplex mode.
224 * If not, we operate in half-duplex mode. */
225 if (phydev->duplex != priv->oldduplex) {
226 new_state = 1;
227 if (!(phydev->duplex))
db98a0b0 228 ctrl &= ~priv->hw->link.duplex;
47dd7a54 229 else
db98a0b0 230 ctrl |= priv->hw->link.duplex;
47dd7a54
GC
231 priv->oldduplex = phydev->duplex;
232 }
233 /* Flow Control operation */
234 if (phydev->pause)
db98a0b0
GC
235 priv->hw->mac->flow_ctrl(ioaddr, phydev->duplex,
236 fc, pause_time);
47dd7a54
GC
237
238 if (phydev->speed != priv->speed) {
239 new_state = 1;
240 switch (phydev->speed) {
241 case 1000:
242 if (likely(priv->is_gmac))
db98a0b0 243 ctrl &= ~priv->hw->link.port;
47dd7a54
GC
244 break;
245 case 100:
246 case 10:
247 if (priv->is_gmac) {
db98a0b0 248 ctrl |= priv->hw->link.port;
47dd7a54 249 if (phydev->speed == SPEED_100) {
db98a0b0 250 ctrl |= priv->hw->link.speed;
47dd7a54 251 } else {
db98a0b0 252 ctrl &= ~(priv->hw->link.speed);
47dd7a54
GC
253 }
254 } else {
db98a0b0 255 ctrl &= ~priv->hw->link.port;
47dd7a54 256 }
65818fa7
GC
257 if (likely(priv->fix_mac_speed))
258 priv->fix_mac_speed(priv->bsp_priv,
259 phydev->speed);
47dd7a54
GC
260 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
271 writel(ctrl, ioaddr + MAC_CTRL_REG);
272
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;
109cdd66
GC
304 char phy_id[MII_BUS_ID_SIZE + 3];
305 char bus_id[MII_BUS_ID_SIZE];
47dd7a54
GC
306
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
316 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->bus_id);
109cdd66
GC
317 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
318 priv->phy_addr);
47dd7a54
GC
319 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
348static inline void stmmac_mac_enable_rx(unsigned long ioaddr)
349{
350 u32 value = readl(ioaddr + MAC_CTRL_REG);
351 value |= MAC_RNABLE_RX;
352 /* Set the RE (receive enable bit into the MAC CTRL register). */
353 writel(value, ioaddr + MAC_CTRL_REG);
354}
355
356static inline void stmmac_mac_enable_tx(unsigned long ioaddr)
357{
358 u32 value = readl(ioaddr + MAC_CTRL_REG);
359 value |= MAC_ENABLE_TX;
360 /* Set the TE (transmit enable bit into the MAC CTRL register). */
361 writel(value, ioaddr + MAC_CTRL_REG);
362}
363
364static inline void stmmac_mac_disable_rx(unsigned long ioaddr)
365{
366 u32 value = readl(ioaddr + MAC_CTRL_REG);
367 value &= ~MAC_RNABLE_RX;
368 writel(value, ioaddr + MAC_CTRL_REG);
369}
370
371static inline void stmmac_mac_disable_tx(unsigned long ioaddr)
372{
373 u32 value = readl(ioaddr + MAC_CTRL_REG);
374 value &= ~MAC_ENABLE_TX;
375 writel(value, ioaddr + MAC_CTRL_REG);
376}
377
378/**
379 * display_ring
380 * @p: pointer to the ring.
381 * @size: size of the ring.
382 * Description: display all the descriptors within the ring.
383 */
384static void display_ring(struct dma_desc *p, int size)
385{
386 struct tmp_s {
387 u64 a;
388 unsigned int b;
389 unsigned int c;
390 };
391 int i;
392 for (i = 0; i < size; i++) {
393 struct tmp_s *x = (struct tmp_s *)(p + i);
394 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
395 i, (unsigned int)virt_to_phys(&p[i]),
396 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
397 x->b, x->c);
398 pr_info("\n");
399 }
400}
401
402/**
403 * init_dma_desc_rings - init the RX/TX descriptor rings
404 * @dev: net device structure
405 * Description: this function initializes the DMA RX/TX descriptors
406 * and allocates the socket buffers.
407 */
408static void init_dma_desc_rings(struct net_device *dev)
409{
410 int i;
411 struct stmmac_priv *priv = netdev_priv(dev);
412 struct sk_buff *skb;
413 unsigned int txsize = priv->dma_tx_size;
414 unsigned int rxsize = priv->dma_rx_size;
415 unsigned int bfsize = priv->dma_buf_sz;
73cfe264 416 int buff2_needed = 0, dis_ic = 0;
47dd7a54 417
47dd7a54
GC
418 /* Set the Buffer size according to the MTU;
419 * indeed, in case of jumbo we need to bump-up the buffer sizes.
420 */
421 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
422 bfsize = BUF_SIZE_16KiB;
423 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
424 bfsize = BUF_SIZE_8KiB;
425 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
426 bfsize = BUF_SIZE_4KiB;
427 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
428 bfsize = BUF_SIZE_2KiB;
429 else
430 bfsize = DMA_BUFFER_SIZE;
431
73cfe264
GC
432#ifdef CONFIG_STMMAC_TIMER
433 /* Disable interrupts on completion for the reception if timer is on */
434 if (likely(priv->tm->enable))
435 dis_ic = 1;
436#endif
47dd7a54
GC
437 /* If the MTU exceeds 8k so use the second buffer in the chain */
438 if (bfsize >= BUF_SIZE_8KiB)
439 buff2_needed = 1;
440
441 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
442 txsize, rxsize, bfsize);
443
444 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
445 priv->rx_skbuff =
446 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
447 priv->dma_rx =
448 (struct dma_desc *)dma_alloc_coherent(priv->device,
449 rxsize *
450 sizeof(struct dma_desc),
451 &priv->dma_rx_phy,
452 GFP_KERNEL);
453 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
454 GFP_KERNEL);
455 priv->dma_tx =
456 (struct dma_desc *)dma_alloc_coherent(priv->device,
457 txsize *
458 sizeof(struct dma_desc),
459 &priv->dma_tx_phy,
460 GFP_KERNEL);
461
462 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
463 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
464 return;
465 }
466
467 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
468 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
469 dev->name, priv->dma_rx, priv->dma_tx,
470 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
471
472 /* RX INITIALIZATION */
473 DBG(probe, INFO, "stmmac: SKB addresses:\n"
474 "skb\t\tskb data\tdma data\n");
475
476 for (i = 0; i < rxsize; i++) {
477 struct dma_desc *p = priv->dma_rx + i;
478
479 skb = netdev_alloc_skb_ip_align(dev, bfsize);
480 if (unlikely(skb == NULL)) {
481 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
482 break;
483 }
484 priv->rx_skbuff[i] = skb;
485 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
486 bfsize, DMA_FROM_DEVICE);
487
488 p->des2 = priv->rx_skbuff_dma[i];
489 if (unlikely(buff2_needed))
490 p->des3 = p->des2 + BUF_SIZE_8KiB;
491 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
492 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
493 }
494 priv->cur_rx = 0;
495 priv->dirty_rx = (unsigned int)(i - rxsize);
496 priv->dma_buf_sz = bfsize;
497 buf_sz = bfsize;
498
499 /* TX INITIALIZATION */
500 for (i = 0; i < txsize; i++) {
501 priv->tx_skbuff[i] = NULL;
502 priv->dma_tx[i].des2 = 0;
503 }
504 priv->dirty_tx = 0;
505 priv->cur_tx = 0;
506
507 /* Clear the Rx/Tx descriptors */
db98a0b0
GC
508 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
509 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
47dd7a54
GC
510
511 if (netif_msg_hw(priv)) {
512 pr_info("RX descriptor ring:\n");
513 display_ring(priv->dma_rx, rxsize);
514 pr_info("TX descriptor ring:\n");
515 display_ring(priv->dma_tx, txsize);
516 }
517 return;
518}
519
520static void dma_free_rx_skbufs(struct stmmac_priv *priv)
521{
522 int i;
523
524 for (i = 0; i < priv->dma_rx_size; i++) {
525 if (priv->rx_skbuff[i]) {
526 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
527 priv->dma_buf_sz, DMA_FROM_DEVICE);
528 dev_kfree_skb_any(priv->rx_skbuff[i]);
529 }
530 priv->rx_skbuff[i] = NULL;
531 }
532 return;
533}
534
535static void dma_free_tx_skbufs(struct stmmac_priv *priv)
536{
537 int i;
538
539 for (i = 0; i < priv->dma_tx_size; i++) {
540 if (priv->tx_skbuff[i] != NULL) {
541 struct dma_desc *p = priv->dma_tx + i;
542 if (p->des2)
543 dma_unmap_single(priv->device, p->des2,
db98a0b0
GC
544 priv->hw->desc->get_tx_len(p),
545 DMA_TO_DEVICE);
47dd7a54
GC
546 dev_kfree_skb_any(priv->tx_skbuff[i]);
547 priv->tx_skbuff[i] = NULL;
548 }
549 }
550 return;
551}
552
553static void free_dma_desc_resources(struct stmmac_priv *priv)
554{
555 /* Release the DMA TX/RX socket buffers */
556 dma_free_rx_skbufs(priv);
557 dma_free_tx_skbufs(priv);
558
559 /* Free the region of consistent memory previously allocated for
560 * the DMA */
561 dma_free_coherent(priv->device,
562 priv->dma_tx_size * sizeof(struct dma_desc),
563 priv->dma_tx, priv->dma_tx_phy);
564 dma_free_coherent(priv->device,
565 priv->dma_rx_size * sizeof(struct dma_desc),
566 priv->dma_rx, priv->dma_rx_phy);
567 kfree(priv->rx_skbuff_dma);
568 kfree(priv->rx_skbuff);
569 kfree(priv->tx_skbuff);
570
571 return;
572}
573
47dd7a54
GC
574/**
575 * stmmac_dma_operation_mode - HW DMA operation mode
576 * @priv : pointer to the private device structure.
577 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
578 * or Store-And-Forward capability. It also verifies the COE for the
579 * transmission in case of Giga ETH.
580 */
581static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
582{
583 if (!priv->is_gmac) {
584 /* MAC 10/100 */
db98a0b0 585 priv->hw->dma->dma_mode(priv->dev->base_addr, tc, 0);
47dd7a54
GC
586 priv->tx_coe = NO_HW_CSUM;
587 } else {
588 if ((priv->dev->mtu <= ETH_DATA_LEN) && (tx_coe)) {
db98a0b0
GC
589 priv->hw->dma->dma_mode(priv->dev->base_addr,
590 SF_DMA_MODE, SF_DMA_MODE);
47dd7a54
GC
591 tc = SF_DMA_MODE;
592 priv->tx_coe = HW_CSUM;
593 } else {
594 /* Checksum computation is performed in software. */
db98a0b0
GC
595 priv->hw->dma->dma_mode(priv->dev->base_addr, tc,
596 SF_DMA_MODE);
47dd7a54
GC
597 priv->tx_coe = NO_HW_CSUM;
598 }
599 }
600 tx_coe = priv->tx_coe;
601
602 return;
603}
604
47dd7a54
GC
605/**
606 * stmmac_tx:
607 * @priv: private driver structure
608 * Description: it reclaims resources after transmission completes.
609 */
610static void stmmac_tx(struct stmmac_priv *priv)
611{
612 unsigned int txsize = priv->dma_tx_size;
613 unsigned long ioaddr = priv->dev->base_addr;
614
615 while (priv->dirty_tx != priv->cur_tx) {
616 int last;
617 unsigned int entry = priv->dirty_tx % txsize;
618 struct sk_buff *skb = priv->tx_skbuff[entry];
619 struct dma_desc *p = priv->dma_tx + entry;
620
621 /* Check if the descriptor is owned by the DMA. */
db98a0b0 622 if (priv->hw->desc->get_tx_owner(p))
47dd7a54
GC
623 break;
624
625 /* Verify tx error by looking at the last segment */
db98a0b0 626 last = priv->hw->desc->get_tx_ls(p);
47dd7a54
GC
627 if (likely(last)) {
628 int tx_error =
db98a0b0
GC
629 priv->hw->desc->tx_status(&priv->dev->stats,
630 &priv->xstats, p,
631 ioaddr);
47dd7a54
GC
632 if (likely(tx_error == 0)) {
633 priv->dev->stats.tx_packets++;
634 priv->xstats.tx_pkt_n++;
635 } else
636 priv->dev->stats.tx_errors++;
637 }
638 TX_DBG("%s: curr %d, dirty %d\n", __func__,
639 priv->cur_tx, priv->dirty_tx);
640
641 if (likely(p->des2))
642 dma_unmap_single(priv->device, p->des2,
db98a0b0 643 priv->hw->desc->get_tx_len(p),
47dd7a54
GC
644 DMA_TO_DEVICE);
645 if (unlikely(p->des3))
646 p->des3 = 0;
647
648 if (likely(skb != NULL)) {
649 /*
650 * If there's room in the queue (limit it to size)
651 * we add this skb back into the pool,
652 * if it's the right size.
653 */
654 if ((skb_queue_len(&priv->rx_recycle) <
655 priv->dma_rx_size) &&
656 skb_recycle_check(skb, priv->dma_buf_sz))
657 __skb_queue_head(&priv->rx_recycle, skb);
658 else
659 dev_kfree_skb(skb);
660
661 priv->tx_skbuff[entry] = NULL;
662 }
663
db98a0b0 664 priv->hw->desc->release_tx_desc(p);
47dd7a54
GC
665
666 entry = (++priv->dirty_tx) % txsize;
667 }
668 if (unlikely(netif_queue_stopped(priv->dev) &&
669 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
670 netif_tx_lock(priv->dev);
671 if (netif_queue_stopped(priv->dev) &&
672 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
673 TX_DBG("%s: restart transmit\n", __func__);
674 netif_wake_queue(priv->dev);
675 }
676 netif_tx_unlock(priv->dev);
677 }
678 return;
679}
680
681static inline void stmmac_enable_irq(struct stmmac_priv *priv)
682{
73cfe264
GC
683#ifdef CONFIG_STMMAC_TIMER
684 if (likely(priv->tm->enable))
685 priv->tm->timer_start(tmrate);
686 else
47dd7a54 687#endif
aec7ff27 688 priv->hw->dma->enable_dma_irq(priv->dev->base_addr);
47dd7a54
GC
689}
690
691static inline void stmmac_disable_irq(struct stmmac_priv *priv)
692{
73cfe264
GC
693#ifdef CONFIG_STMMAC_TIMER
694 if (likely(priv->tm->enable))
695 priv->tm->timer_stop();
696 else
47dd7a54 697#endif
aec7ff27 698 priv->hw->dma->disable_dma_irq(priv->dev->base_addr);
47dd7a54
GC
699}
700
701static int stmmac_has_work(struct stmmac_priv *priv)
702{
703 unsigned int has_work = 0;
704 int rxret, tx_work = 0;
705
db98a0b0 706 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
47dd7a54
GC
707 (priv->cur_rx % priv->dma_rx_size));
708
709 if (priv->dirty_tx != priv->cur_tx)
710 tx_work = 1;
711
712 if (likely(!rxret || tx_work))
713 has_work = 1;
714
715 return has_work;
716}
717
718static inline void _stmmac_schedule(struct stmmac_priv *priv)
719{
720 if (likely(stmmac_has_work(priv))) {
721 stmmac_disable_irq(priv);
722 napi_schedule(&priv->napi);
723 }
724}
725
726#ifdef CONFIG_STMMAC_TIMER
727void stmmac_schedule(struct net_device *dev)
728{
729 struct stmmac_priv *priv = netdev_priv(dev);
730
731 priv->xstats.sched_timer_n++;
732
733 _stmmac_schedule(priv);
734
735 return;
736}
737
738static void stmmac_no_timer_started(unsigned int x)
739{;
740};
741
742static void stmmac_no_timer_stopped(void)
743{;
744};
745#endif
746
747/**
748 * stmmac_tx_err:
749 * @priv: pointer to the private device structure
750 * Description: it cleans the descriptors and restarts the transmission
751 * in case of errors.
752 */
753static void stmmac_tx_err(struct stmmac_priv *priv)
754{
755 netif_stop_queue(priv->dev);
756
aec7ff27 757 priv->hw->dma->stop_tx(priv->dev->base_addr);
47dd7a54 758 dma_free_tx_skbufs(priv);
db98a0b0 759 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
47dd7a54
GC
760 priv->dirty_tx = 0;
761 priv->cur_tx = 0;
aec7ff27 762 priv->hw->dma->start_tx(priv->dev->base_addr);
47dd7a54
GC
763
764 priv->dev->stats.tx_errors++;
765 netif_wake_queue(priv->dev);
766
767 return;
768}
769
47dd7a54 770
aec7ff27
GC
771static void stmmac_dma_interrupt(struct stmmac_priv *priv)
772{
773 unsigned long ioaddr = priv->dev->base_addr;
774 int status;
775
776 status = priv->hw->dma->dma_interrupt(priv->dev->base_addr,
777 &priv->xstats);
778 if (likely(status == handle_tx_rx))
779 _stmmac_schedule(priv);
780
781 else if (unlikely(status == tx_hard_error_bump_tc)) {
782 /* Try to bump up the dma threshold on this failure */
783 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
784 tc += 64;
785 priv->hw->dma->dma_mode(ioaddr, tc, SF_DMA_MODE);
786 priv->xstats.threshold = tc;
47dd7a54 787 }
aec7ff27
GC
788 stmmac_tx_err(priv);
789 } else if (unlikely(status == tx_hard_error))
790 stmmac_tx_err(priv);
47dd7a54
GC
791
792 return;
793}
794
795/**
796 * stmmac_open - open entry point of the driver
797 * @dev : pointer to the device structure.
798 * Description:
799 * This function is the open entry point of the driver.
800 * Return value:
801 * 0 on success and an appropriate (-)ve integer as defined in errno.h
802 * file on failure.
803 */
804static int stmmac_open(struct net_device *dev)
805{
806 struct stmmac_priv *priv = netdev_priv(dev);
807 unsigned long ioaddr = dev->base_addr;
808 int ret;
809
810 /* Check that the MAC address is valid. If its not, refuse
811 * to bring the device up. The user must specify an
812 * address using the following linux command:
813 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
814 if (!is_valid_ether_addr(dev->dev_addr)) {
815 random_ether_addr(dev->dev_addr);
816 pr_warning("%s: generated random MAC address %pM\n", dev->name,
817 dev->dev_addr);
818 }
819
820 stmmac_verify_args();
821
822 ret = stmmac_init_phy(dev);
823 if (unlikely(ret)) {
824 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
825 return ret;
826 }
827
828 /* Request the IRQ lines */
a0607fd3 829 ret = request_irq(dev->irq, stmmac_interrupt,
47dd7a54
GC
830 IRQF_SHARED, dev->name, dev);
831 if (unlikely(ret < 0)) {
832 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
833 __func__, dev->irq, ret);
834 return ret;
835 }
836
837#ifdef CONFIG_STMMAC_TIMER
73cfe264 838 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
47dd7a54 839 if (unlikely(priv->tm == NULL)) {
2381a55c 840 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
47dd7a54
GC
841 return -ENOMEM;
842 }
843 priv->tm->freq = tmrate;
844
73cfe264
GC
845 /* Test if the external timer can be actually used.
846 * In case of failure continue without timer. */
47dd7a54 847 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
73cfe264 848 pr_warning("stmmaceth: cannot attach the external timer.\n");
47dd7a54
GC
849 tmrate = 0;
850 priv->tm->freq = 0;
851 priv->tm->timer_start = stmmac_no_timer_started;
852 priv->tm->timer_stop = stmmac_no_timer_stopped;
73cfe264
GC
853 } else
854 priv->tm->enable = 1;
47dd7a54
GC
855#endif
856
857 /* Create and initialize the TX/RX descriptors chains. */
858 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
859 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
860 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
861 init_dma_desc_rings(dev);
862
863 /* DMA initialization and SW reset */
db98a0b0
GC
864 if (unlikely(priv->hw->dma->init(ioaddr, priv->pbl, priv->dma_tx_phy,
865 priv->dma_rx_phy) < 0)) {
47dd7a54
GC
866
867 pr_err("%s: DMA initialization failed\n", __func__);
868 return -1;
869 }
870
871 /* Copy the MAC addr into the HW */
db98a0b0 872 priv->hw->mac->set_umac_addr(ioaddr, dev->dev_addr, 0);
ca5f12c1
GC
873 /* If required, perform hw setup of the bus. */
874 if (priv->bus_setup)
875 priv->bus_setup(ioaddr);
47dd7a54 876 /* Initialize the MAC Core */
db98a0b0 877 priv->hw->mac->core_init(ioaddr);
47dd7a54
GC
878
879 priv->shutdown = 0;
880
881 /* Initialise the MMC (if present) to disable all interrupts. */
882 writel(0xffffffff, ioaddr + MMC_HIGH_INTR_MASK);
883 writel(0xffffffff, ioaddr + MMC_LOW_INTR_MASK);
884
885 /* Enable the MAC Rx/Tx */
886 stmmac_mac_enable_rx(ioaddr);
887 stmmac_mac_enable_tx(ioaddr);
888
889 /* Set the HW DMA mode and the COE */
890 stmmac_dma_operation_mode(priv);
891
892 /* Extra statistics */
893 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
894 priv->xstats.threshold = tc;
895
896 /* Start the ball rolling... */
897 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
aec7ff27
GC
898 priv->hw->dma->start_tx(ioaddr);
899 priv->hw->dma->start_rx(ioaddr);
47dd7a54
GC
900
901#ifdef CONFIG_STMMAC_TIMER
902 priv->tm->timer_start(tmrate);
903#endif
904 /* Dump DMA/MAC registers */
905 if (netif_msg_hw(priv)) {
db98a0b0
GC
906 priv->hw->mac->dump_regs(ioaddr);
907 priv->hw->dma->dump_regs(ioaddr);
47dd7a54
GC
908 }
909
910 if (priv->phydev)
911 phy_start(priv->phydev);
912
913 napi_enable(&priv->napi);
914 skb_queue_head_init(&priv->rx_recycle);
915 netif_start_queue(dev);
916 return 0;
917}
918
919/**
920 * stmmac_release - close entry point of the driver
921 * @dev : device pointer.
922 * Description:
923 * This is the stop entry point of the driver.
924 */
925static int stmmac_release(struct net_device *dev)
926{
927 struct stmmac_priv *priv = netdev_priv(dev);
928
929 /* Stop and disconnect the PHY */
930 if (priv->phydev) {
931 phy_stop(priv->phydev);
932 phy_disconnect(priv->phydev);
933 priv->phydev = NULL;
934 }
935
936 netif_stop_queue(dev);
937
938#ifdef CONFIG_STMMAC_TIMER
939 /* Stop and release the timer */
940 stmmac_close_ext_timer();
941 if (priv->tm != NULL)
942 kfree(priv->tm);
943#endif
944 napi_disable(&priv->napi);
945 skb_queue_purge(&priv->rx_recycle);
946
947 /* Free the IRQ lines */
948 free_irq(dev->irq, dev);
949
950 /* Stop TX/RX DMA and clear the descriptors */
aec7ff27
GC
951 priv->hw->dma->stop_tx(dev->base_addr);
952 priv->hw->dma->stop_rx(dev->base_addr);
47dd7a54
GC
953
954 /* Release and free the Rx/Tx resources */
955 free_dma_desc_resources(priv);
956
957 /* Disable the MAC core */
958 stmmac_mac_disable_tx(dev->base_addr);
959 stmmac_mac_disable_rx(dev->base_addr);
960
961 netif_carrier_off(dev);
962
963 return 0;
964}
965
966/*
967 * To perform emulated hardware segmentation on skb.
968 */
969static int stmmac_sw_tso(struct stmmac_priv *priv, struct sk_buff *skb)
970{
971 struct sk_buff *segs, *curr_skb;
972 int gso_segs = skb_shinfo(skb)->gso_segs;
973
974 /* Estimate the number of fragments in the worst case */
975 if (unlikely(stmmac_tx_avail(priv) < gso_segs)) {
976 netif_stop_queue(priv->dev);
977 TX_DBG(KERN_ERR "%s: TSO BUG! Tx Ring full when queue awake\n",
978 __func__);
979 if (stmmac_tx_avail(priv) < gso_segs)
980 return NETDEV_TX_BUSY;
981
982 netif_wake_queue(priv->dev);
983 }
984 TX_DBG("\tstmmac_sw_tso: segmenting: skb %p (len %d)\n",
985 skb, skb->len);
986
987 segs = skb_gso_segment(skb, priv->dev->features & ~NETIF_F_TSO);
988 if (unlikely(IS_ERR(segs)))
989 goto sw_tso_end;
990
991 do {
992 curr_skb = segs;
993 segs = segs->next;
994 TX_DBG("\t\tcurrent skb->len: %d, *curr %p,"
995 "*next %p\n", curr_skb->len, curr_skb, segs);
996 curr_skb->next = NULL;
997 stmmac_xmit(curr_skb, priv->dev);
998 } while (segs);
999
1000sw_tso_end:
1001 dev_kfree_skb(skb);
1002
1003 return NETDEV_TX_OK;
1004}
1005
1006static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
1007 struct net_device *dev,
1008 int csum_insertion)
1009{
1010 struct stmmac_priv *priv = netdev_priv(dev);
1011 unsigned int nopaged_len = skb_headlen(skb);
1012 unsigned int txsize = priv->dma_tx_size;
1013 unsigned int entry = priv->cur_tx % txsize;
1014 struct dma_desc *desc = priv->dma_tx + entry;
1015
1016 if (nopaged_len > BUF_SIZE_8KiB) {
1017
1018 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
1019
1020 desc->des2 = dma_map_single(priv->device, skb->data,
1021 BUF_SIZE_8KiB, DMA_TO_DEVICE);
1022 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
db98a0b0
GC
1023 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
1024 csum_insertion);
47dd7a54
GC
1025
1026 entry = (++priv->cur_tx) % txsize;
1027 desc = priv->dma_tx + entry;
1028
1029 desc->des2 = dma_map_single(priv->device,
1030 skb->data + BUF_SIZE_8KiB,
1031 buf2_size, DMA_TO_DEVICE);
1032 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
db98a0b0
GC
1033 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
1034 csum_insertion);
1035 priv->hw->desc->set_tx_owner(desc);
47dd7a54
GC
1036 priv->tx_skbuff[entry] = NULL;
1037 } else {
1038 desc->des2 = dma_map_single(priv->device, skb->data,
1039 nopaged_len, DMA_TO_DEVICE);
1040 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
db98a0b0
GC
1041 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1042 csum_insertion);
47dd7a54
GC
1043 }
1044 return entry;
1045}
1046
1047/**
1048 * stmmac_xmit:
1049 * @skb : the socket buffer
1050 * @dev : device pointer
1051 * Description : Tx entry point of the driver.
1052 */
1053static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1054{
1055 struct stmmac_priv *priv = netdev_priv(dev);
1056 unsigned int txsize = priv->dma_tx_size;
1057 unsigned int entry;
1058 int i, csum_insertion = 0;
1059 int nfrags = skb_shinfo(skb)->nr_frags;
1060 struct dma_desc *desc, *first;
1061
1062 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1063 if (!netif_queue_stopped(dev)) {
1064 netif_stop_queue(dev);
1065 /* This is a hard error, log it. */
1066 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1067 __func__);
1068 }
1069 return NETDEV_TX_BUSY;
1070 }
1071
1072 entry = priv->cur_tx % txsize;
1073
1074#ifdef STMMAC_XMIT_DEBUG
1075 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1076 pr_info("stmmac xmit:\n"
1077 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1078 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1079 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1080 !skb_is_gso(skb) ? "isn't" : "is");
1081#endif
1082
1083 if (unlikely(skb_is_gso(skb)))
1084 return stmmac_sw_tso(priv, skb);
1085
1086 if (likely((skb->ip_summed == CHECKSUM_PARTIAL))) {
1087 if (likely(priv->tx_coe == NO_HW_CSUM))
1088 skb_checksum_help(skb);
1089 else
1090 csum_insertion = 1;
1091 }
1092
1093 desc = priv->dma_tx + entry;
1094 first = desc;
1095
1096#ifdef STMMAC_XMIT_DEBUG
1097 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1098 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1099 "\t\tn_frags: %d, ip_summed: %d\n",
1100 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1101#endif
1102 priv->tx_skbuff[entry] = skb;
1103 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1104 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1105 desc = priv->dma_tx + entry;
1106 } else {
1107 unsigned int nopaged_len = skb_headlen(skb);
1108 desc->des2 = dma_map_single(priv->device, skb->data,
1109 nopaged_len, DMA_TO_DEVICE);
db98a0b0
GC
1110 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1111 csum_insertion);
47dd7a54
GC
1112 }
1113
1114 for (i = 0; i < nfrags; i++) {
1115 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1116 int len = frag->size;
1117
1118 entry = (++priv->cur_tx) % txsize;
1119 desc = priv->dma_tx + entry;
1120
1121 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1122 desc->des2 = dma_map_page(priv->device, frag->page,
1123 frag->page_offset,
1124 len, DMA_TO_DEVICE);
1125 priv->tx_skbuff[entry] = NULL;
db98a0b0
GC
1126 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
1127 priv->hw->desc->set_tx_owner(desc);
47dd7a54
GC
1128 }
1129
1130 /* Interrupt on completition only for the latest segment */
db98a0b0 1131 priv->hw->desc->close_tx_desc(desc);
73cfe264 1132
47dd7a54 1133#ifdef CONFIG_STMMAC_TIMER
73cfe264
GC
1134 /* Clean IC while using timer */
1135 if (likely(priv->tm->enable))
db98a0b0 1136 priv->hw->desc->clear_tx_ic(desc);
47dd7a54
GC
1137#endif
1138 /* To avoid raise condition */
db98a0b0 1139 priv->hw->desc->set_tx_owner(first);
47dd7a54
GC
1140
1141 priv->cur_tx++;
1142
1143#ifdef STMMAC_XMIT_DEBUG
1144 if (netif_msg_pktdata(priv)) {
1145 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1146 "first=%p, nfrags=%d\n",
1147 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1148 entry, first, nfrags);
1149 display_ring(priv->dma_tx, txsize);
1150 pr_info(">>> frame to be transmitted: ");
1151 print_pkt(skb->data, skb->len);
1152 }
1153#endif
1154 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1155 TX_DBG("%s: stop transmitted packets\n", __func__);
1156 netif_stop_queue(dev);
1157 }
1158
1159 dev->stats.tx_bytes += skb->len;
1160
aec7ff27 1161 priv->hw->dma->enable_dma_transmission(dev->base_addr);
47dd7a54
GC
1162
1163 return NETDEV_TX_OK;
1164}
1165
1166static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1167{
1168 unsigned int rxsize = priv->dma_rx_size;
1169 int bfsize = priv->dma_buf_sz;
1170 struct dma_desc *p = priv->dma_rx;
1171
1172 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1173 unsigned int entry = priv->dirty_rx % rxsize;
1174 if (likely(priv->rx_skbuff[entry] == NULL)) {
1175 struct sk_buff *skb;
1176
1177 skb = __skb_dequeue(&priv->rx_recycle);
1178 if (skb == NULL)
1179 skb = netdev_alloc_skb_ip_align(priv->dev,
1180 bfsize);
1181
1182 if (unlikely(skb == NULL))
1183 break;
1184
1185 priv->rx_skbuff[entry] = skb;
1186 priv->rx_skbuff_dma[entry] =
1187 dma_map_single(priv->device, skb->data, bfsize,
1188 DMA_FROM_DEVICE);
1189
1190 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
1191 if (unlikely(priv->is_gmac)) {
1192 if (bfsize >= BUF_SIZE_8KiB)
1193 (p + entry)->des3 =
1194 (p + entry)->des2 + BUF_SIZE_8KiB;
1195 }
1196 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1197 }
db98a0b0 1198 priv->hw->desc->set_rx_owner(p + entry);
47dd7a54
GC
1199 }
1200 return;
1201}
1202
1203static int stmmac_rx(struct stmmac_priv *priv, int limit)
1204{
1205 unsigned int rxsize = priv->dma_rx_size;
1206 unsigned int entry = priv->cur_rx % rxsize;
1207 unsigned int next_entry;
1208 unsigned int count = 0;
1209 struct dma_desc *p = priv->dma_rx + entry;
1210 struct dma_desc *p_next;
1211
1212#ifdef STMMAC_RX_DEBUG
1213 if (netif_msg_hw(priv)) {
1214 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1215 display_ring(priv->dma_rx, rxsize);
1216 }
1217#endif
1218 count = 0;
db98a0b0 1219 while (!priv->hw->desc->get_rx_owner(p)) {
47dd7a54
GC
1220 int status;
1221
1222 if (count >= limit)
1223 break;
1224
1225 count++;
1226
1227 next_entry = (++priv->cur_rx) % rxsize;
1228 p_next = priv->dma_rx + next_entry;
1229 prefetch(p_next);
1230
1231 /* read the status of the incoming frame */
db98a0b0
GC
1232 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1233 &priv->xstats, p));
47dd7a54
GC
1234 if (unlikely(status == discard_frame))
1235 priv->dev->stats.rx_errors++;
1236 else {
1237 struct sk_buff *skb;
1238 /* Length should omit the CRC */
db98a0b0 1239 int frame_len = priv->hw->desc->get_rx_frame_len(p) - 4;
47dd7a54
GC
1240
1241#ifdef STMMAC_RX_DEBUG
1242 if (frame_len > ETH_FRAME_LEN)
1243 pr_debug("\tRX frame size %d, COE status: %d\n",
1244 frame_len, status);
1245
1246 if (netif_msg_hw(priv))
1247 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1248 p, entry, p->des2);
1249#endif
1250 skb = priv->rx_skbuff[entry];
1251 if (unlikely(!skb)) {
1252 pr_err("%s: Inconsistent Rx descriptor chain\n",
1253 priv->dev->name);
1254 priv->dev->stats.rx_dropped++;
1255 break;
1256 }
1257 prefetch(skb->data - NET_IP_ALIGN);
1258 priv->rx_skbuff[entry] = NULL;
1259
1260 skb_put(skb, frame_len);
1261 dma_unmap_single(priv->device,
1262 priv->rx_skbuff_dma[entry],
1263 priv->dma_buf_sz, DMA_FROM_DEVICE);
1264#ifdef STMMAC_RX_DEBUG
1265 if (netif_msg_pktdata(priv)) {
1266 pr_info(" frame received (%dbytes)", frame_len);
1267 print_pkt(skb->data, frame_len);
1268 }
1269#endif
1270 skb->protocol = eth_type_trans(skb, priv->dev);
1271
1272 if (unlikely(status == csum_none)) {
1273 /* always for the old mac 10/100 */
1274 skb->ip_summed = CHECKSUM_NONE;
1275 netif_receive_skb(skb);
1276 } else {
1277 skb->ip_summed = CHECKSUM_UNNECESSARY;
1278 napi_gro_receive(&priv->napi, skb);
1279 }
1280
1281 priv->dev->stats.rx_packets++;
1282 priv->dev->stats.rx_bytes += frame_len;
47dd7a54
GC
1283 }
1284 entry = next_entry;
1285 p = p_next; /* use prefetched values */
1286 }
1287
1288 stmmac_rx_refill(priv);
1289
1290 priv->xstats.rx_pkt_n += count;
1291
1292 return count;
1293}
1294
1295/**
1296 * stmmac_poll - stmmac poll method (NAPI)
1297 * @napi : pointer to the napi structure.
1298 * @budget : maximum number of packets that the current CPU can receive from
1299 * all interfaces.
1300 * Description :
1301 * This function implements the the reception process.
1302 * Also it runs the TX completion thread
1303 */
1304static int stmmac_poll(struct napi_struct *napi, int budget)
1305{
1306 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1307 int work_done = 0;
1308
1309 priv->xstats.poll_n++;
1310 stmmac_tx(priv);
1311 work_done = stmmac_rx(priv, budget);
1312
1313 if (work_done < budget) {
1314 napi_complete(napi);
1315 stmmac_enable_irq(priv);
1316 }
1317 return work_done;
1318}
1319
1320/**
1321 * stmmac_tx_timeout
1322 * @dev : Pointer to net device structure
1323 * Description: this function is called when a packet transmission fails to
1324 * complete within a reasonable tmrate. The driver will mark the error in the
1325 * netdev structure and arrange for the device to be reset to a sane state
1326 * in order to transmit a new packet.
1327 */
1328static void stmmac_tx_timeout(struct net_device *dev)
1329{
1330 struct stmmac_priv *priv = netdev_priv(dev);
1331
1332 /* Clear Tx resources and restart transmitting again */
1333 stmmac_tx_err(priv);
1334 return;
1335}
1336
1337/* Configuration changes (passed on by ifconfig) */
1338static int stmmac_config(struct net_device *dev, struct ifmap *map)
1339{
1340 if (dev->flags & IFF_UP) /* can't act on a running interface */
1341 return -EBUSY;
1342
1343 /* Don't allow changing the I/O address */
1344 if (map->base_addr != dev->base_addr) {
1345 pr_warning("%s: can't change I/O address\n", dev->name);
1346 return -EOPNOTSUPP;
1347 }
1348
1349 /* Don't allow changing the IRQ */
1350 if (map->irq != dev->irq) {
1351 pr_warning("%s: can't change IRQ number %d\n",
1352 dev->name, dev->irq);
1353 return -EOPNOTSUPP;
1354 }
1355
1356 /* ignore other fields */
1357 return 0;
1358}
1359
1360/**
1361 * stmmac_multicast_list - entry point for multicast addressing
1362 * @dev : pointer to the device structure
1363 * Description:
1364 * This function is a driver entry point which gets called by the kernel
1365 * whenever multicast addresses must be enabled/disabled.
1366 * Return value:
1367 * void.
1368 */
1369static void stmmac_multicast_list(struct net_device *dev)
1370{
1371 struct stmmac_priv *priv = netdev_priv(dev);
1372
1373 spin_lock(&priv->lock);
db98a0b0 1374 priv->hw->mac->set_filter(dev);
47dd7a54
GC
1375 spin_unlock(&priv->lock);
1376 return;
1377}
1378
1379/**
1380 * stmmac_change_mtu - entry point to change MTU size for the device.
1381 * @dev : device pointer.
1382 * @new_mtu : the new MTU size for the device.
1383 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1384 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1385 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1386 * Return value:
1387 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1388 * file on failure.
1389 */
1390static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1391{
1392 struct stmmac_priv *priv = netdev_priv(dev);
1393 int max_mtu;
1394
1395 if (netif_running(dev)) {
1396 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1397 return -EBUSY;
1398 }
1399
1400 if (priv->is_gmac)
1401 max_mtu = JUMBO_LEN;
1402 else
1403 max_mtu = ETH_DATA_LEN;
1404
1405 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1406 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1407 return -EINVAL;
1408 }
1409
1410 dev->mtu = new_mtu;
1411
1412 return 0;
1413}
1414
1415static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1416{
1417 struct net_device *dev = (struct net_device *)dev_id;
1418 struct stmmac_priv *priv = netdev_priv(dev);
1419
1420 if (unlikely(!dev)) {
1421 pr_err("%s: invalid dev pointer\n", __func__);
1422 return IRQ_NONE;
1423 }
1424
1425 if (priv->is_gmac) {
1426 unsigned long ioaddr = dev->base_addr;
1427 /* To handle GMAC own interrupts */
db98a0b0 1428 priv->hw->mac->host_irq_status(ioaddr);
47dd7a54 1429 }
aec7ff27
GC
1430
1431 stmmac_dma_interrupt(priv);
47dd7a54
GC
1432
1433 return IRQ_HANDLED;
1434}
1435
1436#ifdef CONFIG_NET_POLL_CONTROLLER
1437/* Polling receive - used by NETCONSOLE and other diagnostic tools
1438 * to allow network I/O with interrupts disabled. */
1439static void stmmac_poll_controller(struct net_device *dev)
1440{
1441 disable_irq(dev->irq);
1442 stmmac_interrupt(dev->irq, dev);
1443 enable_irq(dev->irq);
1444}
1445#endif
1446
1447/**
1448 * stmmac_ioctl - Entry point for the Ioctl
1449 * @dev: Device pointer.
1450 * @rq: An IOCTL specefic structure, that can contain a pointer to
1451 * a proprietary structure used to pass information to the driver.
1452 * @cmd: IOCTL command
1453 * Description:
1454 * Currently there are no special functionality supported in IOCTL, just the
1455 * phy_mii_ioctl(...) can be invoked.
1456 */
1457static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1458{
1459 struct stmmac_priv *priv = netdev_priv(dev);
1460 int ret = -EOPNOTSUPP;
1461
1462 if (!netif_running(dev))
1463 return -EINVAL;
1464
1465 switch (cmd) {
1466 case SIOCGMIIPHY:
1467 case SIOCGMIIREG:
1468 case SIOCSMIIREG:
1469 if (!priv->phydev)
1470 return -EINVAL;
1471
1472 spin_lock(&priv->lock);
1473 ret = phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
1474 spin_unlock(&priv->lock);
1475 default:
1476 break;
1477 }
1478 return ret;
1479}
1480
1481#ifdef STMMAC_VLAN_TAG_USED
1482static void stmmac_vlan_rx_register(struct net_device *dev,
1483 struct vlan_group *grp)
1484{
1485 struct stmmac_priv *priv = netdev_priv(dev);
1486
1487 DBG(probe, INFO, "%s: Setting vlgrp to %p\n", dev->name, grp);
1488
1489 spin_lock(&priv->lock);
1490 priv->vlgrp = grp;
1491 spin_unlock(&priv->lock);
1492
1493 return;
1494}
1495#endif
1496
1497static const struct net_device_ops stmmac_netdev_ops = {
1498 .ndo_open = stmmac_open,
1499 .ndo_start_xmit = stmmac_xmit,
1500 .ndo_stop = stmmac_release,
1501 .ndo_change_mtu = stmmac_change_mtu,
1502 .ndo_set_multicast_list = stmmac_multicast_list,
1503 .ndo_tx_timeout = stmmac_tx_timeout,
1504 .ndo_do_ioctl = stmmac_ioctl,
1505 .ndo_set_config = stmmac_config,
1506#ifdef STMMAC_VLAN_TAG_USED
1507 .ndo_vlan_rx_register = stmmac_vlan_rx_register,
1508#endif
1509#ifdef CONFIG_NET_POLL_CONTROLLER
1510 .ndo_poll_controller = stmmac_poll_controller,
1511#endif
1512 .ndo_set_mac_address = eth_mac_addr,
1513};
1514
1515/**
1516 * stmmac_probe - Initialization of the adapter .
1517 * @dev : device pointer
1518 * Description: The function initializes the network device structure for
1519 * the STMMAC driver. It also calls the low level routines
1520 * in order to init the HW (i.e. the DMA engine)
1521 */
1522static int stmmac_probe(struct net_device *dev)
1523{
1524 int ret = 0;
1525 struct stmmac_priv *priv = netdev_priv(dev);
1526
1527 ether_setup(dev);
1528
1529 dev->netdev_ops = &stmmac_netdev_ops;
1530 stmmac_set_ethtool_ops(dev);
1531
1532 dev->features |= (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA);
1533 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1534#ifdef STMMAC_VLAN_TAG_USED
1535 /* Both mac100 and gmac support receive VLAN tag detection */
1536 dev->features |= NETIF_F_HW_VLAN_RX;
1537#endif
1538 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1539
1540 if (priv->is_gmac)
1541 priv->rx_csum = 1;
1542
1543 if (flow_ctrl)
1544 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1545
1546 priv->pause = pause;
1547 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1548
1549 /* Get the MAC address */
db98a0b0 1550 priv->hw->mac->get_umac_addr(dev->base_addr, dev->dev_addr, 0);
47dd7a54
GC
1551
1552 if (!is_valid_ether_addr(dev->dev_addr))
1553 pr_warning("\tno valid MAC address;"
1554 "please, use ifconfig or nwhwconfig!\n");
1555
1556 ret = register_netdev(dev);
1557 if (ret) {
1558 pr_err("%s: ERROR %i registering the device\n",
1559 __func__, ret);
1560 return -ENODEV;
1561 }
1562
1563 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1564 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
1565 (dev->features & NETIF_F_HW_CSUM) ? "on" : "off");
1566
1567 spin_lock_init(&priv->lock);
1568
1569 return ret;
1570}
1571
1572/**
1573 * stmmac_mac_device_setup
1574 * @dev : device pointer
1575 * Description: select and initialise the mac device (mac100 or Gmac).
1576 */
1577static int stmmac_mac_device_setup(struct net_device *dev)
1578{
1579 struct stmmac_priv *priv = netdev_priv(dev);
1580 unsigned long ioaddr = dev->base_addr;
1581
1582 struct mac_device_info *device;
1583
56b106ae 1584 if (priv->is_gmac) {
21d437cc 1585 device = dwmac1000_setup(ioaddr);
56b106ae
GC
1586 device->desc = &enh_desc_ops;
1587 } else {
7e848ae1 1588 device = dwmac100_setup(ioaddr);
56b106ae
GC
1589 device->desc = &ndesc_ops;
1590 }
47dd7a54
GC
1591
1592 if (!device)
1593 return -ENOMEM;
1594
db98a0b0 1595 priv->hw = device;
47dd7a54 1596
db98a0b0 1597 priv->wolenabled = priv->hw->pmt; /* PMT supported */
47dd7a54
GC
1598 if (priv->wolenabled == PMT_SUPPORTED)
1599 priv->wolopts = WAKE_MAGIC; /* Magic Frame */
1600
1601 return 0;
1602}
1603
1604static int stmmacphy_dvr_probe(struct platform_device *pdev)
1605{
ee7946a7 1606 struct plat_stmmacphy_data *plat_dat = pdev->dev.platform_data;
47dd7a54
GC
1607
1608 pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
1609 plat_dat->bus_id);
1610
1611 return 0;
1612}
1613
1614static int stmmacphy_dvr_remove(struct platform_device *pdev)
1615{
1616 return 0;
1617}
1618
1619static struct platform_driver stmmacphy_driver = {
1620 .driver = {
1621 .name = PHY_RESOURCE_NAME,
1622 },
1623 .probe = stmmacphy_dvr_probe,
1624 .remove = stmmacphy_dvr_remove,
1625};
1626
1627/**
1628 * stmmac_associate_phy
1629 * @dev: pointer to device structure
1630 * @data: points to the private structure.
1631 * Description: Scans through all the PHYs we have registered and checks if
1632 * any are associated with our MAC. If so, then just fill in
1633 * the blanks in our local context structure
1634 */
1635static int stmmac_associate_phy(struct device *dev, void *data)
1636{
1637 struct stmmac_priv *priv = (struct stmmac_priv *)data;
ee7946a7 1638 struct plat_stmmacphy_data *plat_dat = dev->platform_data;
47dd7a54
GC
1639
1640 DBG(probe, DEBUG, "%s: checking phy for bus %d\n", __func__,
1641 plat_dat->bus_id);
1642
1643 /* Check that this phy is for the MAC being initialised */
1644 if (priv->bus_id != plat_dat->bus_id)
1645 return 0;
1646
1647 /* OK, this PHY is connected to the MAC.
1648 Go ahead and get the parameters */
1649 DBG(probe, DEBUG, "%s: OK. Found PHY config\n", __func__);
1650 priv->phy_irq =
1651 platform_get_irq_byname(to_platform_device(dev), "phyirq");
1652 DBG(probe, DEBUG, "%s: PHY irq on bus %d is %d\n", __func__,
1653 plat_dat->bus_id, priv->phy_irq);
1654
1655 /* Override with kernel parameters if supplied XXX CRS XXX
1656 * this needs to have multiple instances */
1657 if ((phyaddr >= 0) && (phyaddr <= 31))
1658 plat_dat->phy_addr = phyaddr;
1659
1660 priv->phy_addr = plat_dat->phy_addr;
1661 priv->phy_mask = plat_dat->phy_mask;
1662 priv->phy_interface = plat_dat->interface;
1663 priv->phy_reset = plat_dat->phy_reset;
1664
1665 DBG(probe, DEBUG, "%s: exiting\n", __func__);
1666 return 1; /* forces exit of driver_for_each_device() */
1667}
1668
1669/**
1670 * stmmac_dvr_probe
1671 * @pdev: platform device pointer
1672 * Description: the driver is initialized through platform_device.
1673 */
1674static int stmmac_dvr_probe(struct platform_device *pdev)
1675{
1676 int ret = 0;
1677 struct resource *res;
1678 unsigned int *addr = NULL;
1679 struct net_device *ndev = NULL;
1680 struct stmmac_priv *priv;
1681 struct plat_stmmacenet_data *plat_dat;
1682
1683 pr_info("STMMAC driver:\n\tplatform registration... ");
1684 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1685 if (!res) {
1686 ret = -ENODEV;
1687 goto out;
1688 }
1689 pr_info("done!\n");
1690
b6222682 1691 if (!request_mem_region(res->start, resource_size(res),
47dd7a54
GC
1692 pdev->name)) {
1693 pr_err("%s: ERROR: memory allocation failed"
1694 "cannot get the I/O addr 0x%x\n",
1695 __func__, (unsigned int)res->start);
1696 ret = -EBUSY;
1697 goto out;
1698 }
1699
7c5365bc 1700 addr = ioremap(res->start, resource_size(res));
47dd7a54 1701 if (!addr) {
7c5365bc 1702 pr_err("%s: ERROR: memory mapping failed\n", __func__);
47dd7a54
GC
1703 ret = -ENOMEM;
1704 goto out;
1705 }
1706
1707 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1708 if (!ndev) {
1709 pr_err("%s: ERROR: allocating the device\n", __func__);
1710 ret = -ENOMEM;
1711 goto out;
1712 }
1713
1714 SET_NETDEV_DEV(ndev, &pdev->dev);
1715
1716 /* Get the MAC information */
1717 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1718 if (ndev->irq == -ENXIO) {
1719 pr_err("%s: ERROR: MAC IRQ configuration "
1720 "information not found\n", __func__);
1721 ret = -ENODEV;
1722 goto out;
1723 }
1724
1725 priv = netdev_priv(ndev);
1726 priv->device = &(pdev->dev);
1727 priv->dev = ndev;
ee7946a7 1728 plat_dat = pdev->dev.platform_data;
47dd7a54
GC
1729 priv->bus_id = plat_dat->bus_id;
1730 priv->pbl = plat_dat->pbl; /* TLI */
1731 priv->is_gmac = plat_dat->has_gmac; /* GMAC is on board */
1732
1733 platform_set_drvdata(pdev, ndev);
1734
1735 /* Set the I/O base addr */
1736 ndev->base_addr = (unsigned long)addr;
1737
ee7946a7
GC
1738 /* Verify embedded resource for the platform */
1739 ret = stmmac_claim_resource(pdev);
1740 if (ret < 0)
1741 goto out;
1742
47dd7a54
GC
1743 /* MAC HW revice detection */
1744 ret = stmmac_mac_device_setup(ndev);
1745 if (ret < 0)
1746 goto out;
1747
1748 /* Network Device Registration */
1749 ret = stmmac_probe(ndev);
1750 if (ret < 0)
1751 goto out;
1752
1753 /* associate a PHY - it is provided by another platform bus */
1754 if (!driver_for_each_device
1755 (&(stmmacphy_driver.driver), NULL, (void *)priv,
1756 stmmac_associate_phy)) {
1757 pr_err("No PHY device is associated with this MAC!\n");
1758 ret = -ENODEV;
1759 goto out;
1760 }
1761
1762 priv->fix_mac_speed = plat_dat->fix_mac_speed;
ee7946a7 1763 priv->bus_setup = plat_dat->bus_setup;
47dd7a54
GC
1764 priv->bsp_priv = plat_dat->bsp_priv;
1765
1766 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
1767 "\tIO base addr: 0x%08x)\n", ndev->name, pdev->name,
1768 pdev->id, ndev->irq, (unsigned int)addr);
1769
1770 /* MDIO bus Registration */
1771 pr_debug("\tMDIO bus (id: %d)...", priv->bus_id);
1772 ret = stmmac_mdio_register(ndev);
1773 if (ret < 0)
1774 goto out;
1775 pr_debug("registered!\n");
1776
1777out:
1778 if (ret < 0) {
1779 platform_set_drvdata(pdev, NULL);
7c5365bc 1780 release_mem_region(res->start, resource_size(res));
47dd7a54
GC
1781 if (addr != NULL)
1782 iounmap(addr);
1783 }
1784
1785 return ret;
1786}
1787
1788/**
1789 * stmmac_dvr_remove
1790 * @pdev: platform device pointer
1791 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1792 * changes the link status, releases the DMA descriptor rings,
1793 * unregisters the MDIO bus and unmaps the allocated memory.
1794 */
1795static int stmmac_dvr_remove(struct platform_device *pdev)
1796{
1797 struct net_device *ndev = platform_get_drvdata(pdev);
aec7ff27 1798 struct stmmac_priv *priv = netdev_priv(ndev);
47dd7a54
GC
1799 struct resource *res;
1800
1801 pr_info("%s:\n\tremoving driver", __func__);
1802
aec7ff27
GC
1803 priv->hw->dma->stop_rx(ndev->base_addr);
1804 priv->hw->dma->stop_tx(ndev->base_addr);
47dd7a54
GC
1805
1806 stmmac_mac_disable_rx(ndev->base_addr);
1807 stmmac_mac_disable_tx(ndev->base_addr);
1808
1809 netif_carrier_off(ndev);
1810
1811 stmmac_mdio_unregister(ndev);
1812
1813 platform_set_drvdata(pdev, NULL);
1814 unregister_netdev(ndev);
1815
1816 iounmap((void *)ndev->base_addr);
1817 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7c5365bc 1818 release_mem_region(res->start, resource_size(res));
47dd7a54
GC
1819
1820 free_netdev(ndev);
1821
1822 return 0;
1823}
1824
1825#ifdef CONFIG_PM
1826static int stmmac_suspend(struct platform_device *pdev, pm_message_t state)
1827{
1828 struct net_device *dev = platform_get_drvdata(pdev);
1829 struct stmmac_priv *priv = netdev_priv(dev);
1830 int dis_ic = 0;
1831
1832 if (!dev || !netif_running(dev))
1833 return 0;
1834
1835 spin_lock(&priv->lock);
1836
1837 if (state.event == PM_EVENT_SUSPEND) {
1838 netif_device_detach(dev);
1839 netif_stop_queue(dev);
1840 if (priv->phydev)
1841 phy_stop(priv->phydev);
1842
1843#ifdef CONFIG_STMMAC_TIMER
1844 priv->tm->timer_stop();
73cfe264
GC
1845 if (likely(priv->tm->enable))
1846 dis_ic = 1;
47dd7a54
GC
1847#endif
1848 napi_disable(&priv->napi);
1849
1850 /* Stop TX/RX DMA */
aec7ff27
GC
1851 priv->hw->dma->stop_tx(dev->base_addr);
1852 priv->hw->dma->stop_rx(dev->base_addr);
47dd7a54 1853 /* Clear the Rx/Tx descriptors */
db98a0b0
GC
1854 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1855 dis_ic);
1856 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
47dd7a54
GC
1857
1858 stmmac_mac_disable_tx(dev->base_addr);
1859
1860 if (device_may_wakeup(&(pdev->dev))) {
1861 /* Enable Power down mode by programming the PMT regs */
1862 if (priv->wolenabled == PMT_SUPPORTED)
db98a0b0
GC
1863 priv->hw->mac->pmt(dev->base_addr,
1864 priv->wolopts);
47dd7a54
GC
1865 } else {
1866 stmmac_mac_disable_rx(dev->base_addr);
1867 }
1868 } else {
1869 priv->shutdown = 1;
1870 /* Although this can appear slightly redundant it actually
1871 * makes fast the standby operation and guarantees the driver
1872 * working if hibernation is on media. */
1873 stmmac_release(dev);
1874 }
1875
1876 spin_unlock(&priv->lock);
1877 return 0;
1878}
1879
1880static int stmmac_resume(struct platform_device *pdev)
1881{
1882 struct net_device *dev = platform_get_drvdata(pdev);
1883 struct stmmac_priv *priv = netdev_priv(dev);
1884 unsigned long ioaddr = dev->base_addr;
1885
1886 if (!netif_running(dev))
1887 return 0;
1888
1889 spin_lock(&priv->lock);
1890
1891 if (priv->shutdown) {
1892 /* Re-open the interface and re-init the MAC/DMA
1893 and the rings. */
1894 stmmac_open(dev);
1895 goto out_resume;
1896 }
1897
1898 /* Power Down bit, into the PM register, is cleared
1899 * automatically as soon as a magic packet or a Wake-up frame
1900 * is received. Anyway, it's better to manually clear
1901 * this bit because it can generate problems while resuming
1902 * from another devices (e.g. serial console). */
1903 if (device_may_wakeup(&(pdev->dev)))
1904 if (priv->wolenabled == PMT_SUPPORTED)
db98a0b0 1905 priv->hw->mac->pmt(dev->base_addr, 0);
47dd7a54
GC
1906
1907 netif_device_attach(dev);
1908
1909 /* Enable the MAC and DMA */
1910 stmmac_mac_enable_rx(ioaddr);
1911 stmmac_mac_enable_tx(ioaddr);
aec7ff27
GC
1912 priv->hw->dma->start_tx(ioaddr);
1913 priv->hw->dma->start_rx(ioaddr);
47dd7a54
GC
1914
1915#ifdef CONFIG_STMMAC_TIMER
1916 priv->tm->timer_start(tmrate);
1917#endif
1918 napi_enable(&priv->napi);
1919
1920 if (priv->phydev)
1921 phy_start(priv->phydev);
1922
1923 netif_start_queue(dev);
1924
1925out_resume:
1926 spin_unlock(&priv->lock);
1927 return 0;
1928}
1929#endif
1930
1931static struct platform_driver stmmac_driver = {
1932 .driver = {
1933 .name = STMMAC_RESOURCE_NAME,
1934 },
1935 .probe = stmmac_dvr_probe,
1936 .remove = stmmac_dvr_remove,
1937#ifdef CONFIG_PM
1938 .suspend = stmmac_suspend,
1939 .resume = stmmac_resume,
1940#endif
1941
1942};
1943
1944/**
1945 * stmmac_init_module - Entry point for the driver
1946 * Description: This function is the entry point for the driver.
1947 */
1948static int __init stmmac_init_module(void)
1949{
1950 int ret;
1951
1952 if (platform_driver_register(&stmmacphy_driver)) {
1953 pr_err("No PHY devices registered!\n");
1954 return -ENODEV;
1955 }
1956
1957 ret = platform_driver_register(&stmmac_driver);
1958 return ret;
1959}
1960
1961/**
1962 * stmmac_cleanup_module - Cleanup routine for the driver
1963 * Description: This function is the cleanup routine for the driver.
1964 */
1965static void __exit stmmac_cleanup_module(void)
1966{
1967 platform_driver_unregister(&stmmacphy_driver);
1968 platform_driver_unregister(&stmmac_driver);
1969}
1970
1971#ifndef MODULE
1972static int __init stmmac_cmdline_opt(char *str)
1973{
1974 char *opt;
1975
1976 if (!str || !*str)
1977 return -EINVAL;
1978 while ((opt = strsep(&str, ",")) != NULL) {
1979 if (!strncmp(opt, "debug:", 6))
1980 strict_strtoul(opt + 6, 0, (unsigned long *)&debug);
1981 else if (!strncmp(opt, "phyaddr:", 8))
1982 strict_strtoul(opt + 8, 0, (unsigned long *)&phyaddr);
1983 else if (!strncmp(opt, "dma_txsize:", 11))
1984 strict_strtoul(opt + 11, 0,
1985 (unsigned long *)&dma_txsize);
1986 else if (!strncmp(opt, "dma_rxsize:", 11))
1987 strict_strtoul(opt + 11, 0,
1988 (unsigned long *)&dma_rxsize);
1989 else if (!strncmp(opt, "buf_sz:", 7))
1990 strict_strtoul(opt + 7, 0, (unsigned long *)&buf_sz);
1991 else if (!strncmp(opt, "tc:", 3))
1992 strict_strtoul(opt + 3, 0, (unsigned long *)&tc);
1993 else if (!strncmp(opt, "tx_coe:", 7))
1994 strict_strtoul(opt + 7, 0, (unsigned long *)&tx_coe);
1995 else if (!strncmp(opt, "watchdog:", 9))
1996 strict_strtoul(opt + 9, 0, (unsigned long *)&watchdog);
1997 else if (!strncmp(opt, "flow_ctrl:", 10))
1998 strict_strtoul(opt + 10, 0,
1999 (unsigned long *)&flow_ctrl);
2000 else if (!strncmp(opt, "pause:", 6))
2001 strict_strtoul(opt + 6, 0, (unsigned long *)&pause);
2002#ifdef CONFIG_STMMAC_TIMER
2003 else if (!strncmp(opt, "tmrate:", 7))
2004 strict_strtoul(opt + 7, 0, (unsigned long *)&tmrate);
2005#endif
2006 }
2007 return 0;
2008}
2009
2010__setup("stmmaceth=", stmmac_cmdline_opt);
2011#endif
2012
2013module_init(stmmac_init_module);
2014module_exit(stmmac_cleanup_module);
2015
2016MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
2017MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2018MODULE_LICENSE("GPL");