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.
5 Copyright (C) 2007-2009 STMicroelectronics Ltd
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.
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
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.
20 The full GNU General Public License is included in this distribution in
21 the file called "COPYING".
23 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
25 Documentation available at:
26 http://www.stlinux.com
28 https://bugzilla.stlinux.com/
29 *******************************************************************************/
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/kernel.h>
34 #include <linux/interrupt.h>
35 #include <linux/etherdevice.h>
36 #include <linux/platform_device.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>
47 #include <linux/slab.h>
48 #include <linux/prefetch.h>
51 #define STMMAC_RESOURCE_NAME "stmmaceth"
52 #define PHY_RESOURCE_NAME "stmmacphy"
55 /*#define STMMAC_DEBUG*/
57 #define DBG(nlevel, klevel, fmt, args...) \
58 ((void)(netif_msg_##nlevel(priv) && \
59 printk(KERN_##klevel fmt, ## args)))
61 #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
64 #undef STMMAC_RX_DEBUG
65 /*#define STMMAC_RX_DEBUG*/
66 #ifdef STMMAC_RX_DEBUG
67 #define RX_DBG(fmt, args...) printk(fmt, ## args)
69 #define RX_DBG(fmt, args...) do { } while (0)
72 #undef STMMAC_XMIT_DEBUG
73 /*#define STMMAC_XMIT_DEBUG*/
74 #ifdef STMMAC_TX_DEBUG
75 #define TX_DBG(fmt, args...) printk(fmt, ## args)
77 #define TX_DBG(fmt, args...) do { } while (0)
80 #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
81 #define JUMBO_LEN 9000
83 /* Module parameters */
84 #define TX_TIMEO 5000 /* default 5 seconds */
85 static int watchdog
= TX_TIMEO
;
86 module_param(watchdog
, int, S_IRUGO
| S_IWUSR
);
87 MODULE_PARM_DESC(watchdog
, "Transmit timeout in milliseconds");
89 static int debug
= -1; /* -1: default, 0: no output, 16: all */
90 module_param(debug
, int, S_IRUGO
| S_IWUSR
);
91 MODULE_PARM_DESC(debug
, "Message Level (0: no output, 16: all)");
93 static int phyaddr
= -1;
94 module_param(phyaddr
, int, S_IRUGO
);
95 MODULE_PARM_DESC(phyaddr
, "Physical device address");
97 #define DMA_TX_SIZE 256
98 static int dma_txsize
= DMA_TX_SIZE
;
99 module_param(dma_txsize
, int, S_IRUGO
| S_IWUSR
);
100 MODULE_PARM_DESC(dma_txsize
, "Number of descriptors in the TX list");
102 #define DMA_RX_SIZE 256
103 static int dma_rxsize
= DMA_RX_SIZE
;
104 module_param(dma_rxsize
, int, S_IRUGO
| S_IWUSR
);
105 MODULE_PARM_DESC(dma_rxsize
, "Number of descriptors in the RX list");
107 static int flow_ctrl
= FLOW_OFF
;
108 module_param(flow_ctrl
, int, S_IRUGO
| S_IWUSR
);
109 MODULE_PARM_DESC(flow_ctrl
, "Flow control ability [on/off]");
111 static int pause
= PAUSE_TIME
;
112 module_param(pause
, int, S_IRUGO
| S_IWUSR
);
113 MODULE_PARM_DESC(pause
, "Flow Control Pause Time");
115 #define TC_DEFAULT 64
116 static int tc
= TC_DEFAULT
;
117 module_param(tc
, int, S_IRUGO
| S_IWUSR
);
118 MODULE_PARM_DESC(tc
, "DMA threshold control value");
120 /* Pay attention to tune this parameter; take care of both
121 * hardware capability and network stabitily/performance impact.
122 * Many tests showed that ~4ms latency seems to be good enough. */
123 #ifdef CONFIG_STMMAC_TIMER
124 #define DEFAULT_PERIODIC_RATE 256
125 static int tmrate
= DEFAULT_PERIODIC_RATE
;
126 module_param(tmrate
, int, S_IRUGO
| S_IWUSR
);
127 MODULE_PARM_DESC(tmrate
, "External timer freq. (default: 256Hz)");
130 #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
131 static int buf_sz
= DMA_BUFFER_SIZE
;
132 module_param(buf_sz
, int, S_IRUGO
| S_IWUSR
);
133 MODULE_PARM_DESC(buf_sz
, "DMA buffer size");
135 static const u32 default_msg_level
= (NETIF_MSG_DRV
| NETIF_MSG_PROBE
|
136 NETIF_MSG_LINK
| NETIF_MSG_IFUP
|
137 NETIF_MSG_IFDOWN
| NETIF_MSG_TIMER
);
139 static irqreturn_t
stmmac_interrupt(int irq
, void *dev_id
);
142 * stmmac_verify_args - verify the driver parameters.
143 * Description: it verifies if some wrong parameter is passed to the driver.
144 * Note that wrong parameters are replaced with the default values.
146 static void stmmac_verify_args(void)
148 if (unlikely(watchdog
< 0))
150 if (unlikely(dma_rxsize
< 0))
151 dma_rxsize
= DMA_RX_SIZE
;
152 if (unlikely(dma_txsize
< 0))
153 dma_txsize
= DMA_TX_SIZE
;
154 if (unlikely((buf_sz
< DMA_BUFFER_SIZE
) || (buf_sz
> BUF_SIZE_16KiB
)))
155 buf_sz
= DMA_BUFFER_SIZE
;
156 if (unlikely(flow_ctrl
> 1))
157 flow_ctrl
= FLOW_AUTO
;
158 else if (likely(flow_ctrl
< 0))
159 flow_ctrl
= FLOW_OFF
;
160 if (unlikely((pause
< 0) || (pause
> 0xffff)))
164 #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
165 static void print_pkt(unsigned char *buf
, int len
)
168 pr_info("len = %d byte, buf addr: 0x%p", len
, buf
);
169 for (j
= 0; j
< len
; j
++) {
171 pr_info("\n %03x:", j
);
172 pr_info(" %02x", buf
[j
]);
178 /* minimum number of free TX descriptors required to wake up TX process */
179 #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
181 static inline u32
stmmac_tx_avail(struct stmmac_priv
*priv
)
183 return priv
->dirty_tx
+ priv
->dma_tx_size
- priv
->cur_tx
- 1;
186 /* On some ST platforms, some HW system configuraton registers have to be
187 * set according to the link speed negotiated.
189 static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv
*priv
)
191 struct phy_device
*phydev
= priv
->phydev
;
193 if (likely(priv
->plat
->fix_mac_speed
))
194 priv
->plat
->fix_mac_speed(priv
->plat
->bsp_priv
,
200 * @dev: net device structure
201 * Description: it adjusts the link parameters.
203 static void stmmac_adjust_link(struct net_device
*dev
)
205 struct stmmac_priv
*priv
= netdev_priv(dev
);
206 struct phy_device
*phydev
= priv
->phydev
;
209 unsigned int fc
= priv
->flow_ctrl
, pause_time
= priv
->pause
;
214 DBG(probe
, DEBUG
, "stmmac_adjust_link: called. address %d link %d\n",
215 phydev
->addr
, phydev
->link
);
217 spin_lock_irqsave(&priv
->lock
, flags
);
219 u32 ctrl
= readl(priv
->ioaddr
+ MAC_CTRL_REG
);
221 /* Now we make sure that we can be in full duplex mode.
222 * If not, we operate in half-duplex mode. */
223 if (phydev
->duplex
!= priv
->oldduplex
) {
225 if (!(phydev
->duplex
))
226 ctrl
&= ~priv
->hw
->link
.duplex
;
228 ctrl
|= priv
->hw
->link
.duplex
;
229 priv
->oldduplex
= phydev
->duplex
;
231 /* Flow Control operation */
233 priv
->hw
->mac
->flow_ctrl(priv
->ioaddr
, phydev
->duplex
,
236 if (phydev
->speed
!= priv
->speed
) {
238 switch (phydev
->speed
) {
240 if (likely(priv
->plat
->has_gmac
))
241 ctrl
&= ~priv
->hw
->link
.port
;
242 stmmac_hw_fix_mac_speed(priv
);
246 if (priv
->plat
->has_gmac
) {
247 ctrl
|= priv
->hw
->link
.port
;
248 if (phydev
->speed
== SPEED_100
) {
249 ctrl
|= priv
->hw
->link
.speed
;
251 ctrl
&= ~(priv
->hw
->link
.speed
);
254 ctrl
&= ~priv
->hw
->link
.port
;
256 stmmac_hw_fix_mac_speed(priv
);
259 if (netif_msg_link(priv
))
260 pr_warning("%s: Speed (%d) is not 10"
261 " or 100!\n", dev
->name
, phydev
->speed
);
265 priv
->speed
= phydev
->speed
;
268 writel(ctrl
, priv
->ioaddr
+ MAC_CTRL_REG
);
270 if (!priv
->oldlink
) {
274 } else if (priv
->oldlink
) {
278 priv
->oldduplex
= -1;
281 if (new_state
&& netif_msg_link(priv
))
282 phy_print_status(phydev
);
284 spin_unlock_irqrestore(&priv
->lock
, flags
);
286 DBG(probe
, DEBUG
, "stmmac_adjust_link: exiting\n");
290 * stmmac_init_phy - PHY initialization
291 * @dev: net device structure
292 * Description: it initializes the driver's PHY state, and attaches the PHY
297 static int stmmac_init_phy(struct net_device
*dev
)
299 struct stmmac_priv
*priv
= netdev_priv(dev
);
300 struct phy_device
*phydev
;
301 char phy_id
[MII_BUS_ID_SIZE
+ 3];
302 char bus_id
[MII_BUS_ID_SIZE
];
306 priv
->oldduplex
= -1;
308 if (priv
->phy_addr
== -1) {
309 /* We don't have a PHY, so do nothing */
313 snprintf(bus_id
, MII_BUS_ID_SIZE
, "%x", priv
->plat
->bus_id
);
314 snprintf(phy_id
, MII_BUS_ID_SIZE
+ 3, PHY_ID_FMT
, bus_id
,
316 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id
);
318 phydev
= phy_connect(dev
, phy_id
, &stmmac_adjust_link
, 0,
319 priv
->phy_interface
);
321 if (IS_ERR(phydev
)) {
322 pr_err("%s: Could not attach to PHY\n", dev
->name
);
323 return PTR_ERR(phydev
);
327 * Broken HW is sometimes missing the pull-up resistor on the
328 * MDIO line, which results in reads to non-existent devices returning
329 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
331 * Note: phydev->phy_id is the result of reading the UID PHY registers.
333 if (phydev
->phy_id
== 0) {
334 phy_disconnect(phydev
);
337 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
338 " Link = %d\n", dev
->name
, phydev
->phy_id
, phydev
->link
);
340 priv
->phydev
= phydev
;
345 static inline void stmmac_enable_mac(void __iomem
*ioaddr
)
347 u32 value
= readl(ioaddr
+ MAC_CTRL_REG
);
349 value
|= MAC_RNABLE_RX
| MAC_ENABLE_TX
;
350 writel(value
, ioaddr
+ MAC_CTRL_REG
);
353 static inline void stmmac_disable_mac(void __iomem
*ioaddr
)
355 u32 value
= readl(ioaddr
+ MAC_CTRL_REG
);
357 value
&= ~(MAC_ENABLE_TX
| MAC_RNABLE_RX
);
358 writel(value
, ioaddr
+ MAC_CTRL_REG
);
363 * @p: pointer to the ring.
364 * @size: size of the ring.
365 * Description: display all the descriptors within the ring.
367 static void display_ring(struct dma_desc
*p
, int size
)
375 for (i
= 0; i
< size
; i
++) {
376 struct tmp_s
*x
= (struct tmp_s
*)(p
+ i
);
377 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
378 i
, (unsigned int)virt_to_phys(&p
[i
]),
379 (unsigned int)(x
->a
), (unsigned int)((x
->a
) >> 32),
386 * init_dma_desc_rings - init the RX/TX descriptor rings
387 * @dev: net device structure
388 * Description: this function initializes the DMA RX/TX descriptors
389 * and allocates the socket buffers.
391 static void init_dma_desc_rings(struct net_device
*dev
)
394 struct stmmac_priv
*priv
= netdev_priv(dev
);
396 unsigned int txsize
= priv
->dma_tx_size
;
397 unsigned int rxsize
= priv
->dma_rx_size
;
398 unsigned int bfsize
= priv
->dma_buf_sz
;
399 int buff2_needed
= 0, dis_ic
= 0;
401 /* Set the Buffer size according to the MTU;
402 * indeed, in case of jumbo we need to bump-up the buffer sizes.
404 if (unlikely(dev
->mtu
>= BUF_SIZE_8KiB
))
405 bfsize
= BUF_SIZE_16KiB
;
406 else if (unlikely(dev
->mtu
>= BUF_SIZE_4KiB
))
407 bfsize
= BUF_SIZE_8KiB
;
408 else if (unlikely(dev
->mtu
>= BUF_SIZE_2KiB
))
409 bfsize
= BUF_SIZE_4KiB
;
410 else if (unlikely(dev
->mtu
>= DMA_BUFFER_SIZE
))
411 bfsize
= BUF_SIZE_2KiB
;
413 bfsize
= DMA_BUFFER_SIZE
;
415 #ifdef CONFIG_STMMAC_TIMER
416 /* Disable interrupts on completion for the reception if timer is on */
417 if (likely(priv
->tm
->enable
))
420 /* If the MTU exceeds 8k so use the second buffer in the chain */
421 if (bfsize
>= BUF_SIZE_8KiB
)
424 DBG(probe
, INFO
, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
425 txsize
, rxsize
, bfsize
);
427 priv
->rx_skbuff_dma
= kmalloc(rxsize
* sizeof(dma_addr_t
), GFP_KERNEL
);
429 kmalloc(sizeof(struct sk_buff
*) * rxsize
, GFP_KERNEL
);
431 (struct dma_desc
*)dma_alloc_coherent(priv
->device
,
433 sizeof(struct dma_desc
),
436 priv
->tx_skbuff
= kmalloc(sizeof(struct sk_buff
*) * txsize
,
439 (struct dma_desc
*)dma_alloc_coherent(priv
->device
,
441 sizeof(struct dma_desc
),
445 if ((priv
->dma_rx
== NULL
) || (priv
->dma_tx
== NULL
)) {
446 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__
);
450 DBG(probe
, INFO
, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
451 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
452 dev
->name
, priv
->dma_rx
, priv
->dma_tx
,
453 (unsigned int)priv
->dma_rx_phy
, (unsigned int)priv
->dma_tx_phy
);
455 /* RX INITIALIZATION */
456 DBG(probe
, INFO
, "stmmac: SKB addresses:\n"
457 "skb\t\tskb data\tdma data\n");
459 for (i
= 0; i
< rxsize
; i
++) {
460 struct dma_desc
*p
= priv
->dma_rx
+ i
;
462 skb
= netdev_alloc_skb_ip_align(dev
, bfsize
);
463 if (unlikely(skb
== NULL
)) {
464 pr_err("%s: Rx init fails; skb is NULL\n", __func__
);
467 priv
->rx_skbuff
[i
] = skb
;
468 priv
->rx_skbuff_dma
[i
] = dma_map_single(priv
->device
, skb
->data
,
469 bfsize
, DMA_FROM_DEVICE
);
471 p
->des2
= priv
->rx_skbuff_dma
[i
];
472 if (unlikely(buff2_needed
))
473 p
->des3
= p
->des2
+ BUF_SIZE_8KiB
;
474 DBG(probe
, INFO
, "[%p]\t[%p]\t[%x]\n", priv
->rx_skbuff
[i
],
475 priv
->rx_skbuff
[i
]->data
, priv
->rx_skbuff_dma
[i
]);
478 priv
->dirty_rx
= (unsigned int)(i
- rxsize
);
479 priv
->dma_buf_sz
= bfsize
;
482 /* TX INITIALIZATION */
483 for (i
= 0; i
< txsize
; i
++) {
484 priv
->tx_skbuff
[i
] = NULL
;
485 priv
->dma_tx
[i
].des2
= 0;
490 /* Clear the Rx/Tx descriptors */
491 priv
->hw
->desc
->init_rx_desc(priv
->dma_rx
, rxsize
, dis_ic
);
492 priv
->hw
->desc
->init_tx_desc(priv
->dma_tx
, txsize
);
494 if (netif_msg_hw(priv
)) {
495 pr_info("RX descriptor ring:\n");
496 display_ring(priv
->dma_rx
, rxsize
);
497 pr_info("TX descriptor ring:\n");
498 display_ring(priv
->dma_tx
, txsize
);
502 static void dma_free_rx_skbufs(struct stmmac_priv
*priv
)
506 for (i
= 0; i
< priv
->dma_rx_size
; i
++) {
507 if (priv
->rx_skbuff
[i
]) {
508 dma_unmap_single(priv
->device
, priv
->rx_skbuff_dma
[i
],
509 priv
->dma_buf_sz
, DMA_FROM_DEVICE
);
510 dev_kfree_skb_any(priv
->rx_skbuff
[i
]);
512 priv
->rx_skbuff
[i
] = NULL
;
516 static void dma_free_tx_skbufs(struct stmmac_priv
*priv
)
520 for (i
= 0; i
< priv
->dma_tx_size
; i
++) {
521 if (priv
->tx_skbuff
[i
] != NULL
) {
522 struct dma_desc
*p
= priv
->dma_tx
+ i
;
524 dma_unmap_single(priv
->device
, p
->des2
,
525 priv
->hw
->desc
->get_tx_len(p
),
527 dev_kfree_skb_any(priv
->tx_skbuff
[i
]);
528 priv
->tx_skbuff
[i
] = NULL
;
533 static void free_dma_desc_resources(struct stmmac_priv
*priv
)
535 /* Release the DMA TX/RX socket buffers */
536 dma_free_rx_skbufs(priv
);
537 dma_free_tx_skbufs(priv
);
539 /* Free the region of consistent memory previously allocated for
541 dma_free_coherent(priv
->device
,
542 priv
->dma_tx_size
* sizeof(struct dma_desc
),
543 priv
->dma_tx
, priv
->dma_tx_phy
);
544 dma_free_coherent(priv
->device
,
545 priv
->dma_rx_size
* sizeof(struct dma_desc
),
546 priv
->dma_rx
, priv
->dma_rx_phy
);
547 kfree(priv
->rx_skbuff_dma
);
548 kfree(priv
->rx_skbuff
);
549 kfree(priv
->tx_skbuff
);
553 * stmmac_dma_operation_mode - HW DMA operation mode
554 * @priv : pointer to the private device structure.
555 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
556 * or Store-And-Forward capability.
558 static void stmmac_dma_operation_mode(struct stmmac_priv
*priv
)
560 if (likely((priv
->plat
->tx_coe
) && (!priv
->no_csum_insertion
))) {
561 /* In case of GMAC, SF mode has to be enabled
562 * to perform the TX COE. This depends on:
563 * 1) TX COE if actually supported
564 * 2) There is no bugged Jumbo frame support
565 * that needs to not insert csum in the TDES.
567 priv
->hw
->dma
->dma_mode(priv
->ioaddr
,
568 SF_DMA_MODE
, SF_DMA_MODE
);
571 priv
->hw
->dma
->dma_mode(priv
->ioaddr
, tc
, SF_DMA_MODE
);
576 * @priv: private driver structure
577 * Description: it reclaims resources after transmission completes.
579 static void stmmac_tx(struct stmmac_priv
*priv
)
581 unsigned int txsize
= priv
->dma_tx_size
;
583 while (priv
->dirty_tx
!= priv
->cur_tx
) {
585 unsigned int entry
= priv
->dirty_tx
% txsize
;
586 struct sk_buff
*skb
= priv
->tx_skbuff
[entry
];
587 struct dma_desc
*p
= priv
->dma_tx
+ entry
;
589 /* Check if the descriptor is owned by the DMA. */
590 if (priv
->hw
->desc
->get_tx_owner(p
))
593 /* Verify tx error by looking at the last segment */
594 last
= priv
->hw
->desc
->get_tx_ls(p
);
597 priv
->hw
->desc
->tx_status(&priv
->dev
->stats
,
600 if (likely(tx_error
== 0)) {
601 priv
->dev
->stats
.tx_packets
++;
602 priv
->xstats
.tx_pkt_n
++;
604 priv
->dev
->stats
.tx_errors
++;
606 TX_DBG("%s: curr %d, dirty %d\n", __func__
,
607 priv
->cur_tx
, priv
->dirty_tx
);
610 dma_unmap_single(priv
->device
, p
->des2
,
611 priv
->hw
->desc
->get_tx_len(p
),
613 if (unlikely(p
->des3
))
616 if (likely(skb
!= NULL
)) {
618 * If there's room in the queue (limit it to size)
619 * we add this skb back into the pool,
620 * if it's the right size.
622 if ((skb_queue_len(&priv
->rx_recycle
) <
623 priv
->dma_rx_size
) &&
624 skb_recycle_check(skb
, priv
->dma_buf_sz
))
625 __skb_queue_head(&priv
->rx_recycle
, skb
);
629 priv
->tx_skbuff
[entry
] = NULL
;
632 priv
->hw
->desc
->release_tx_desc(p
);
634 entry
= (++priv
->dirty_tx
) % txsize
;
636 if (unlikely(netif_queue_stopped(priv
->dev
) &&
637 stmmac_tx_avail(priv
) > STMMAC_TX_THRESH(priv
))) {
638 netif_tx_lock(priv
->dev
);
639 if (netif_queue_stopped(priv
->dev
) &&
640 stmmac_tx_avail(priv
) > STMMAC_TX_THRESH(priv
)) {
641 TX_DBG("%s: restart transmit\n", __func__
);
642 netif_wake_queue(priv
->dev
);
644 netif_tx_unlock(priv
->dev
);
648 static inline void stmmac_enable_irq(struct stmmac_priv
*priv
)
650 #ifdef CONFIG_STMMAC_TIMER
651 if (likely(priv
->tm
->enable
))
652 priv
->tm
->timer_start(tmrate
);
655 priv
->hw
->dma
->enable_dma_irq(priv
->ioaddr
);
658 static inline void stmmac_disable_irq(struct stmmac_priv
*priv
)
660 #ifdef CONFIG_STMMAC_TIMER
661 if (likely(priv
->tm
->enable
))
662 priv
->tm
->timer_stop();
665 priv
->hw
->dma
->disable_dma_irq(priv
->ioaddr
);
668 static int stmmac_has_work(struct stmmac_priv
*priv
)
670 unsigned int has_work
= 0;
671 int rxret
, tx_work
= 0;
673 rxret
= priv
->hw
->desc
->get_rx_owner(priv
->dma_rx
+
674 (priv
->cur_rx
% priv
->dma_rx_size
));
676 if (priv
->dirty_tx
!= priv
->cur_tx
)
679 if (likely(!rxret
|| tx_work
))
685 static inline void _stmmac_schedule(struct stmmac_priv
*priv
)
687 if (likely(stmmac_has_work(priv
))) {
688 stmmac_disable_irq(priv
);
689 napi_schedule(&priv
->napi
);
693 #ifdef CONFIG_STMMAC_TIMER
694 void stmmac_schedule(struct net_device
*dev
)
696 struct stmmac_priv
*priv
= netdev_priv(dev
);
698 priv
->xstats
.sched_timer_n
++;
700 _stmmac_schedule(priv
);
703 static void stmmac_no_timer_started(unsigned int x
)
707 static void stmmac_no_timer_stopped(void)
714 * @priv: pointer to the private device structure
715 * Description: it cleans the descriptors and restarts the transmission
718 static void stmmac_tx_err(struct stmmac_priv
*priv
)
721 netif_stop_queue(priv
->dev
);
723 priv
->hw
->dma
->stop_tx(priv
->ioaddr
);
724 dma_free_tx_skbufs(priv
);
725 priv
->hw
->desc
->init_tx_desc(priv
->dma_tx
, priv
->dma_tx_size
);
728 priv
->hw
->dma
->start_tx(priv
->ioaddr
);
730 priv
->dev
->stats
.tx_errors
++;
731 netif_wake_queue(priv
->dev
);
735 static void stmmac_dma_interrupt(struct stmmac_priv
*priv
)
739 status
= priv
->hw
->dma
->dma_interrupt(priv
->ioaddr
, &priv
->xstats
);
740 if (likely(status
== handle_tx_rx
))
741 _stmmac_schedule(priv
);
743 else if (unlikely(status
== tx_hard_error_bump_tc
)) {
744 /* Try to bump up the dma threshold on this failure */
745 if (unlikely(tc
!= SF_DMA_MODE
) && (tc
<= 256)) {
747 priv
->hw
->dma
->dma_mode(priv
->ioaddr
, tc
, SF_DMA_MODE
);
748 priv
->xstats
.threshold
= tc
;
750 } else if (unlikely(status
== tx_hard_error
))
755 * stmmac_open - open entry point of the driver
756 * @dev : pointer to the device structure.
758 * This function is the open entry point of the driver.
760 * 0 on success and an appropriate (-)ve integer as defined in errno.h
763 static int stmmac_open(struct net_device
*dev
)
765 struct stmmac_priv
*priv
= netdev_priv(dev
);
768 /* Check that the MAC address is valid. If its not, refuse
769 * to bring the device up. The user must specify an
770 * address using the following linux command:
771 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
772 if (!is_valid_ether_addr(dev
->dev_addr
)) {
773 random_ether_addr(dev
->dev_addr
);
774 pr_warning("%s: generated random MAC address %pM\n", dev
->name
,
778 stmmac_verify_args();
780 #ifdef CONFIG_STMMAC_TIMER
781 priv
->tm
= kzalloc(sizeof(struct stmmac_timer
*), GFP_KERNEL
);
782 if (unlikely(priv
->tm
== NULL
)) {
783 pr_err("%s: ERROR: timer memory alloc failed\n", __func__
);
786 priv
->tm
->freq
= tmrate
;
788 /* Test if the external timer can be actually used.
789 * In case of failure continue without timer. */
790 if (unlikely((stmmac_open_ext_timer(dev
, priv
->tm
)) < 0)) {
791 pr_warning("stmmaceth: cannot attach the external timer.\n");
793 priv
->tm
->timer_start
= stmmac_no_timer_started
;
794 priv
->tm
->timer_stop
= stmmac_no_timer_stopped
;
796 priv
->tm
->enable
= 1;
798 ret
= stmmac_init_phy(dev
);
800 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__
, ret
);
804 /* Create and initialize the TX/RX descriptors chains. */
805 priv
->dma_tx_size
= STMMAC_ALIGN(dma_txsize
);
806 priv
->dma_rx_size
= STMMAC_ALIGN(dma_rxsize
);
807 priv
->dma_buf_sz
= STMMAC_ALIGN(buf_sz
);
808 init_dma_desc_rings(dev
);
810 /* DMA initialization and SW reset */
811 ret
= priv
->hw
->dma
->init(priv
->ioaddr
, priv
->plat
->pbl
,
812 priv
->dma_tx_phy
, priv
->dma_rx_phy
);
814 pr_err("%s: DMA initialization failed\n", __func__
);
818 /* Copy the MAC addr into the HW */
819 priv
->hw
->mac
->set_umac_addr(priv
->ioaddr
, dev
->dev_addr
, 0);
820 /* If required, perform hw setup of the bus. */
821 if (priv
->plat
->bus_setup
)
822 priv
->plat
->bus_setup(priv
->ioaddr
);
823 /* Initialize the MAC Core */
824 priv
->hw
->mac
->core_init(priv
->ioaddr
);
826 priv
->rx_coe
= priv
->hw
->mac
->rx_coe(priv
->ioaddr
);
828 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
829 if (priv
->plat
->tx_coe
)
830 pr_info("\tTX Checksum insertion supported\n");
831 netdev_update_features(dev
);
833 /* Initialise the MMC (if present) to disable all interrupts. */
834 writel(0xffffffff, priv
->ioaddr
+ MMC_HIGH_INTR_MASK
);
835 writel(0xffffffff, priv
->ioaddr
+ MMC_LOW_INTR_MASK
);
837 /* Request the IRQ lines */
838 ret
= request_irq(dev
->irq
, stmmac_interrupt
,
839 IRQF_SHARED
, dev
->name
, dev
);
840 if (unlikely(ret
< 0)) {
841 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
842 __func__
, dev
->irq
, ret
);
846 /* Enable the MAC Rx/Tx */
847 stmmac_enable_mac(priv
->ioaddr
);
849 /* Set the HW DMA mode and the COE */
850 stmmac_dma_operation_mode(priv
);
852 /* Extra statistics */
853 memset(&priv
->xstats
, 0, sizeof(struct stmmac_extra_stats
));
854 priv
->xstats
.threshold
= tc
;
856 /* Start the ball rolling... */
857 DBG(probe
, DEBUG
, "%s: DMA RX/TX processes started...\n", dev
->name
);
858 priv
->hw
->dma
->start_tx(priv
->ioaddr
);
859 priv
->hw
->dma
->start_rx(priv
->ioaddr
);
861 #ifdef CONFIG_STMMAC_TIMER
862 priv
->tm
->timer_start(tmrate
);
864 /* Dump DMA/MAC registers */
865 if (netif_msg_hw(priv
)) {
866 priv
->hw
->mac
->dump_regs(priv
->ioaddr
);
867 priv
->hw
->dma
->dump_regs(priv
->ioaddr
);
871 phy_start(priv
->phydev
);
873 napi_enable(&priv
->napi
);
874 skb_queue_head_init(&priv
->rx_recycle
);
875 netif_start_queue(dev
);
880 #ifdef CONFIG_STMMAC_TIMER
884 phy_disconnect(priv
->phydev
);
890 * stmmac_release - close entry point of the driver
891 * @dev : device pointer.
893 * This is the stop entry point of the driver.
895 static int stmmac_release(struct net_device
*dev
)
897 struct stmmac_priv
*priv
= netdev_priv(dev
);
899 /* Stop and disconnect the PHY */
901 phy_stop(priv
->phydev
);
902 phy_disconnect(priv
->phydev
);
906 netif_stop_queue(dev
);
908 #ifdef CONFIG_STMMAC_TIMER
909 /* Stop and release the timer */
910 stmmac_close_ext_timer();
911 if (priv
->tm
!= NULL
)
914 napi_disable(&priv
->napi
);
915 skb_queue_purge(&priv
->rx_recycle
);
917 /* Free the IRQ lines */
918 free_irq(dev
->irq
, dev
);
920 /* Stop TX/RX DMA and clear the descriptors */
921 priv
->hw
->dma
->stop_tx(priv
->ioaddr
);
922 priv
->hw
->dma
->stop_rx(priv
->ioaddr
);
924 /* Release and free the Rx/Tx resources */
925 free_dma_desc_resources(priv
);
927 /* Disable the MAC Rx/Tx */
928 stmmac_disable_mac(priv
->ioaddr
);
930 netif_carrier_off(dev
);
935 static unsigned int stmmac_handle_jumbo_frames(struct sk_buff
*skb
,
936 struct net_device
*dev
,
939 struct stmmac_priv
*priv
= netdev_priv(dev
);
940 unsigned int nopaged_len
= skb_headlen(skb
);
941 unsigned int txsize
= priv
->dma_tx_size
;
942 unsigned int entry
= priv
->cur_tx
% txsize
;
943 struct dma_desc
*desc
= priv
->dma_tx
+ entry
;
945 if (nopaged_len
> BUF_SIZE_8KiB
) {
947 int buf2_size
= nopaged_len
- BUF_SIZE_8KiB
;
949 desc
->des2
= dma_map_single(priv
->device
, skb
->data
,
950 BUF_SIZE_8KiB
, DMA_TO_DEVICE
);
951 desc
->des3
= desc
->des2
+ BUF_SIZE_4KiB
;
952 priv
->hw
->desc
->prepare_tx_desc(desc
, 1, BUF_SIZE_8KiB
,
955 entry
= (++priv
->cur_tx
) % txsize
;
956 desc
= priv
->dma_tx
+ entry
;
958 desc
->des2
= dma_map_single(priv
->device
,
959 skb
->data
+ BUF_SIZE_8KiB
,
960 buf2_size
, DMA_TO_DEVICE
);
961 desc
->des3
= desc
->des2
+ BUF_SIZE_4KiB
;
962 priv
->hw
->desc
->prepare_tx_desc(desc
, 0, buf2_size
,
964 priv
->hw
->desc
->set_tx_owner(desc
);
965 priv
->tx_skbuff
[entry
] = NULL
;
967 desc
->des2
= dma_map_single(priv
->device
, skb
->data
,
968 nopaged_len
, DMA_TO_DEVICE
);
969 desc
->des3
= desc
->des2
+ BUF_SIZE_4KiB
;
970 priv
->hw
->desc
->prepare_tx_desc(desc
, 1, nopaged_len
,
978 * @skb : the socket buffer
979 * @dev : device pointer
980 * Description : Tx entry point of the driver.
982 static netdev_tx_t
stmmac_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
984 struct stmmac_priv
*priv
= netdev_priv(dev
);
985 unsigned int txsize
= priv
->dma_tx_size
;
987 int i
, csum_insertion
= 0;
988 int nfrags
= skb_shinfo(skb
)->nr_frags
;
989 struct dma_desc
*desc
, *first
;
991 if (unlikely(stmmac_tx_avail(priv
) < nfrags
+ 1)) {
992 if (!netif_queue_stopped(dev
)) {
993 netif_stop_queue(dev
);
994 /* This is a hard error, log it. */
995 pr_err("%s: BUG! Tx Ring full when queue awake\n",
998 return NETDEV_TX_BUSY
;
1001 entry
= priv
->cur_tx
% txsize
;
1003 #ifdef STMMAC_XMIT_DEBUG
1004 if ((skb
->len
> ETH_FRAME_LEN
) || nfrags
)
1005 pr_info("stmmac xmit:\n"
1006 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1007 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1008 skb
, skb
->len
, skb_headlen(skb
), nfrags
, skb
->ip_summed
,
1009 !skb_is_gso(skb
) ? "isn't" : "is");
1012 csum_insertion
= (skb
->ip_summed
== CHECKSUM_PARTIAL
);
1014 desc
= priv
->dma_tx
+ entry
;
1017 #ifdef STMMAC_XMIT_DEBUG
1018 if ((nfrags
> 0) || (skb
->len
> ETH_FRAME_LEN
))
1019 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1020 "\t\tn_frags: %d, ip_summed: %d\n",
1021 skb
->len
, skb_headlen(skb
), nfrags
, skb
->ip_summed
);
1023 priv
->tx_skbuff
[entry
] = skb
;
1024 if (unlikely(skb
->len
>= BUF_SIZE_4KiB
)) {
1025 entry
= stmmac_handle_jumbo_frames(skb
, dev
, csum_insertion
);
1026 desc
= priv
->dma_tx
+ entry
;
1028 unsigned int nopaged_len
= skb_headlen(skb
);
1029 desc
->des2
= dma_map_single(priv
->device
, skb
->data
,
1030 nopaged_len
, DMA_TO_DEVICE
);
1031 priv
->hw
->desc
->prepare_tx_desc(desc
, 1, nopaged_len
,
1035 for (i
= 0; i
< nfrags
; i
++) {
1036 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1037 int len
= frag
->size
;
1039 entry
= (++priv
->cur_tx
) % txsize
;
1040 desc
= priv
->dma_tx
+ entry
;
1042 TX_DBG("\t[entry %d] segment len: %d\n", entry
, len
);
1043 desc
->des2
= dma_map_page(priv
->device
, frag
->page
,
1045 len
, DMA_TO_DEVICE
);
1046 priv
->tx_skbuff
[entry
] = NULL
;
1047 priv
->hw
->desc
->prepare_tx_desc(desc
, 0, len
, csum_insertion
);
1049 priv
->hw
->desc
->set_tx_owner(desc
);
1052 /* Interrupt on completition only for the latest segment */
1053 priv
->hw
->desc
->close_tx_desc(desc
);
1055 #ifdef CONFIG_STMMAC_TIMER
1056 /* Clean IC while using timer */
1057 if (likely(priv
->tm
->enable
))
1058 priv
->hw
->desc
->clear_tx_ic(desc
);
1063 /* To avoid raise condition */
1064 priv
->hw
->desc
->set_tx_owner(first
);
1068 #ifdef STMMAC_XMIT_DEBUG
1069 if (netif_msg_pktdata(priv
)) {
1070 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1071 "first=%p, nfrags=%d\n",
1072 (priv
->cur_tx
% txsize
), (priv
->dirty_tx
% txsize
),
1073 entry
, first
, nfrags
);
1074 display_ring(priv
->dma_tx
, txsize
);
1075 pr_info(">>> frame to be transmitted: ");
1076 print_pkt(skb
->data
, skb
->len
);
1079 if (unlikely(stmmac_tx_avail(priv
) <= (MAX_SKB_FRAGS
+ 1))) {
1080 TX_DBG("%s: stop transmitted packets\n", __func__
);
1081 netif_stop_queue(dev
);
1084 dev
->stats
.tx_bytes
+= skb
->len
;
1086 skb_tx_timestamp(skb
);
1088 priv
->hw
->dma
->enable_dma_transmission(priv
->ioaddr
);
1090 return NETDEV_TX_OK
;
1093 static inline void stmmac_rx_refill(struct stmmac_priv
*priv
)
1095 unsigned int rxsize
= priv
->dma_rx_size
;
1096 int bfsize
= priv
->dma_buf_sz
;
1097 struct dma_desc
*p
= priv
->dma_rx
;
1099 for (; priv
->cur_rx
- priv
->dirty_rx
> 0; priv
->dirty_rx
++) {
1100 unsigned int entry
= priv
->dirty_rx
% rxsize
;
1101 if (likely(priv
->rx_skbuff
[entry
] == NULL
)) {
1102 struct sk_buff
*skb
;
1104 skb
= __skb_dequeue(&priv
->rx_recycle
);
1106 skb
= netdev_alloc_skb_ip_align(priv
->dev
,
1109 if (unlikely(skb
== NULL
))
1112 priv
->rx_skbuff
[entry
] = skb
;
1113 priv
->rx_skbuff_dma
[entry
] =
1114 dma_map_single(priv
->device
, skb
->data
, bfsize
,
1117 (p
+ entry
)->des2
= priv
->rx_skbuff_dma
[entry
];
1118 if (unlikely(priv
->plat
->has_gmac
)) {
1119 if (bfsize
>= BUF_SIZE_8KiB
)
1121 (p
+ entry
)->des2
+ BUF_SIZE_8KiB
;
1123 RX_DBG(KERN_INFO
"\trefill entry #%d\n", entry
);
1126 priv
->hw
->desc
->set_rx_owner(p
+ entry
);
1130 static int stmmac_rx(struct stmmac_priv
*priv
, int limit
)
1132 unsigned int rxsize
= priv
->dma_rx_size
;
1133 unsigned int entry
= priv
->cur_rx
% rxsize
;
1134 unsigned int next_entry
;
1135 unsigned int count
= 0;
1136 struct dma_desc
*p
= priv
->dma_rx
+ entry
;
1137 struct dma_desc
*p_next
;
1139 #ifdef STMMAC_RX_DEBUG
1140 if (netif_msg_hw(priv
)) {
1141 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1142 display_ring(priv
->dma_rx
, rxsize
);
1146 while (!priv
->hw
->desc
->get_rx_owner(p
)) {
1154 next_entry
= (++priv
->cur_rx
) % rxsize
;
1155 p_next
= priv
->dma_rx
+ next_entry
;
1158 /* read the status of the incoming frame */
1159 status
= (priv
->hw
->desc
->rx_status(&priv
->dev
->stats
,
1161 if (unlikely(status
== discard_frame
))
1162 priv
->dev
->stats
.rx_errors
++;
1164 struct sk_buff
*skb
;
1167 frame_len
= priv
->hw
->desc
->get_rx_frame_len(p
);
1168 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1169 * Type frames (LLC/LLC-SNAP) */
1170 if (unlikely(status
!= llc_snap
))
1171 frame_len
-= ETH_FCS_LEN
;
1172 #ifdef STMMAC_RX_DEBUG
1173 if (frame_len
> ETH_FRAME_LEN
)
1174 pr_debug("\tRX frame size %d, COE status: %d\n",
1177 if (netif_msg_hw(priv
))
1178 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1181 skb
= priv
->rx_skbuff
[entry
];
1182 if (unlikely(!skb
)) {
1183 pr_err("%s: Inconsistent Rx descriptor chain\n",
1185 priv
->dev
->stats
.rx_dropped
++;
1188 prefetch(skb
->data
- NET_IP_ALIGN
);
1189 priv
->rx_skbuff
[entry
] = NULL
;
1191 skb_put(skb
, frame_len
);
1192 dma_unmap_single(priv
->device
,
1193 priv
->rx_skbuff_dma
[entry
],
1194 priv
->dma_buf_sz
, DMA_FROM_DEVICE
);
1195 #ifdef STMMAC_RX_DEBUG
1196 if (netif_msg_pktdata(priv
)) {
1197 pr_info(" frame received (%dbytes)", frame_len
);
1198 print_pkt(skb
->data
, frame_len
);
1201 skb
->protocol
= eth_type_trans(skb
, priv
->dev
);
1203 if (unlikely(status
== csum_none
)) {
1204 /* always for the old mac 10/100 */
1205 skb_checksum_none_assert(skb
);
1206 netif_receive_skb(skb
);
1208 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1209 napi_gro_receive(&priv
->napi
, skb
);
1212 priv
->dev
->stats
.rx_packets
++;
1213 priv
->dev
->stats
.rx_bytes
+= frame_len
;
1216 p
= p_next
; /* use prefetched values */
1219 stmmac_rx_refill(priv
);
1221 priv
->xstats
.rx_pkt_n
+= count
;
1227 * stmmac_poll - stmmac poll method (NAPI)
1228 * @napi : pointer to the napi structure.
1229 * @budget : maximum number of packets that the current CPU can receive from
1232 * This function implements the the reception process.
1233 * Also it runs the TX completion thread
1235 static int stmmac_poll(struct napi_struct
*napi
, int budget
)
1237 struct stmmac_priv
*priv
= container_of(napi
, struct stmmac_priv
, napi
);
1240 priv
->xstats
.poll_n
++;
1242 work_done
= stmmac_rx(priv
, budget
);
1244 if (work_done
< budget
) {
1245 napi_complete(napi
);
1246 stmmac_enable_irq(priv
);
1253 * @dev : Pointer to net device structure
1254 * Description: this function is called when a packet transmission fails to
1255 * complete within a reasonable tmrate. The driver will mark the error in the
1256 * netdev structure and arrange for the device to be reset to a sane state
1257 * in order to transmit a new packet.
1259 static void stmmac_tx_timeout(struct net_device
*dev
)
1261 struct stmmac_priv
*priv
= netdev_priv(dev
);
1263 /* Clear Tx resources and restart transmitting again */
1264 stmmac_tx_err(priv
);
1267 /* Configuration changes (passed on by ifconfig) */
1268 static int stmmac_config(struct net_device
*dev
, struct ifmap
*map
)
1270 if (dev
->flags
& IFF_UP
) /* can't act on a running interface */
1273 /* Don't allow changing the I/O address */
1274 if (map
->base_addr
!= dev
->base_addr
) {
1275 pr_warning("%s: can't change I/O address\n", dev
->name
);
1279 /* Don't allow changing the IRQ */
1280 if (map
->irq
!= dev
->irq
) {
1281 pr_warning("%s: can't change IRQ number %d\n",
1282 dev
->name
, dev
->irq
);
1286 /* ignore other fields */
1291 * stmmac_multicast_list - entry point for multicast addressing
1292 * @dev : pointer to the device structure
1294 * This function is a driver entry point which gets called by the kernel
1295 * whenever multicast addresses must be enabled/disabled.
1299 static void stmmac_multicast_list(struct net_device
*dev
)
1301 struct stmmac_priv
*priv
= netdev_priv(dev
);
1303 spin_lock(&priv
->lock
);
1304 priv
->hw
->mac
->set_filter(dev
);
1305 spin_unlock(&priv
->lock
);
1309 * stmmac_change_mtu - entry point to change MTU size for the device.
1310 * @dev : device pointer.
1311 * @new_mtu : the new MTU size for the device.
1312 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1313 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1314 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1316 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1319 static int stmmac_change_mtu(struct net_device
*dev
, int new_mtu
)
1321 struct stmmac_priv
*priv
= netdev_priv(dev
);
1324 if (netif_running(dev
)) {
1325 pr_err("%s: must be stopped to change its MTU\n", dev
->name
);
1329 if (priv
->plat
->has_gmac
)
1330 max_mtu
= JUMBO_LEN
;
1332 max_mtu
= ETH_DATA_LEN
;
1334 if ((new_mtu
< 46) || (new_mtu
> max_mtu
)) {
1335 pr_err("%s: invalid MTU, max MTU is: %d\n", dev
->name
, max_mtu
);
1340 netdev_update_features(dev
);
1345 static u32
stmmac_fix_features(struct net_device
*dev
, u32 features
)
1347 struct stmmac_priv
*priv
= netdev_priv(dev
);
1350 features
&= ~NETIF_F_RXCSUM
;
1351 if (!priv
->plat
->tx_coe
)
1352 features
&= ~NETIF_F_ALL_CSUM
;
1354 /* Some GMAC devices have a bugged Jumbo frame support that
1355 * needs to have the Tx COE disabled for oversized frames
1356 * (due to limited buffer sizes). In this case we disable
1357 * the TX csum insertionin the TDES and not use SF. */
1358 if (priv
->plat
->bugged_jumbo
&& (dev
->mtu
> ETH_DATA_LEN
))
1359 features
&= ~NETIF_F_ALL_CSUM
;
1364 static irqreturn_t
stmmac_interrupt(int irq
, void *dev_id
)
1366 struct net_device
*dev
= (struct net_device
*)dev_id
;
1367 struct stmmac_priv
*priv
= netdev_priv(dev
);
1369 if (unlikely(!dev
)) {
1370 pr_err("%s: invalid dev pointer\n", __func__
);
1374 if (priv
->plat
->has_gmac
)
1375 /* To handle GMAC own interrupts */
1376 priv
->hw
->mac
->host_irq_status((void __iomem
*) dev
->base_addr
);
1378 stmmac_dma_interrupt(priv
);
1383 #ifdef CONFIG_NET_POLL_CONTROLLER
1384 /* Polling receive - used by NETCONSOLE and other diagnostic tools
1385 * to allow network I/O with interrupts disabled. */
1386 static void stmmac_poll_controller(struct net_device
*dev
)
1388 disable_irq(dev
->irq
);
1389 stmmac_interrupt(dev
->irq
, dev
);
1390 enable_irq(dev
->irq
);
1395 * stmmac_ioctl - Entry point for the Ioctl
1396 * @dev: Device pointer.
1397 * @rq: An IOCTL specefic structure, that can contain a pointer to
1398 * a proprietary structure used to pass information to the driver.
1399 * @cmd: IOCTL command
1401 * Currently there are no special functionality supported in IOCTL, just the
1402 * phy_mii_ioctl(...) can be invoked.
1404 static int stmmac_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
1406 struct stmmac_priv
*priv
= netdev_priv(dev
);
1409 if (!netif_running(dev
))
1415 spin_lock(&priv
->lock
);
1416 ret
= phy_mii_ioctl(priv
->phydev
, rq
, cmd
);
1417 spin_unlock(&priv
->lock
);
1422 #ifdef STMMAC_VLAN_TAG_USED
1423 static void stmmac_vlan_rx_register(struct net_device
*dev
,
1424 struct vlan_group
*grp
)
1426 struct stmmac_priv
*priv
= netdev_priv(dev
);
1428 DBG(probe
, INFO
, "%s: Setting vlgrp to %p\n", dev
->name
, grp
);
1430 spin_lock(&priv
->lock
);
1432 spin_unlock(&priv
->lock
);
1436 static const struct net_device_ops stmmac_netdev_ops
= {
1437 .ndo_open
= stmmac_open
,
1438 .ndo_start_xmit
= stmmac_xmit
,
1439 .ndo_stop
= stmmac_release
,
1440 .ndo_change_mtu
= stmmac_change_mtu
,
1441 .ndo_fix_features
= stmmac_fix_features
,
1442 .ndo_set_multicast_list
= stmmac_multicast_list
,
1443 .ndo_tx_timeout
= stmmac_tx_timeout
,
1444 .ndo_do_ioctl
= stmmac_ioctl
,
1445 .ndo_set_config
= stmmac_config
,
1446 #ifdef STMMAC_VLAN_TAG_USED
1447 .ndo_vlan_rx_register
= stmmac_vlan_rx_register
,
1449 #ifdef CONFIG_NET_POLL_CONTROLLER
1450 .ndo_poll_controller
= stmmac_poll_controller
,
1452 .ndo_set_mac_address
= eth_mac_addr
,
1456 * stmmac_probe - Initialization of the adapter .
1457 * @dev : device pointer
1458 * Description: The function initializes the network device structure for
1459 * the STMMAC driver. It also calls the low level routines
1460 * in order to init the HW (i.e. the DMA engine)
1462 static int stmmac_probe(struct net_device
*dev
)
1465 struct stmmac_priv
*priv
= netdev_priv(dev
);
1469 dev
->netdev_ops
= &stmmac_netdev_ops
;
1470 stmmac_set_ethtool_ops(dev
);
1472 dev
->hw_features
= NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_IPV6_CSUM
;
1473 dev
->features
|= dev
->hw_features
| NETIF_F_HIGHDMA
;
1474 dev
->watchdog_timeo
= msecs_to_jiffies(watchdog
);
1475 #ifdef STMMAC_VLAN_TAG_USED
1476 /* Both mac100 and gmac support receive VLAN tag detection */
1477 dev
->features
|= NETIF_F_HW_VLAN_RX
;
1479 priv
->msg_enable
= netif_msg_init(debug
, default_msg_level
);
1482 priv
->flow_ctrl
= FLOW_AUTO
; /* RX/TX pause on */
1484 priv
->pause
= pause
;
1485 netif_napi_add(dev
, &priv
->napi
, stmmac_poll
, 64);
1487 /* Get the MAC address */
1488 priv
->hw
->mac
->get_umac_addr((void __iomem
*) dev
->base_addr
,
1491 if (!is_valid_ether_addr(dev
->dev_addr
))
1492 pr_warning("\tno valid MAC address;"
1493 "please, use ifconfig or nwhwconfig!\n");
1495 spin_lock_init(&priv
->lock
);
1497 ret
= register_netdev(dev
);
1499 pr_err("%s: ERROR %i registering the device\n",
1504 DBG(probe
, DEBUG
, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1505 dev
->name
, (dev
->features
& NETIF_F_SG
) ? "on" : "off",
1506 (dev
->features
& NETIF_F_IP_CSUM
) ? "on" : "off");
1512 * stmmac_mac_device_setup
1513 * @dev : device pointer
1514 * Description: select and initialise the mac device (mac100 or Gmac).
1516 static int stmmac_mac_device_setup(struct net_device
*dev
)
1518 struct stmmac_priv
*priv
= netdev_priv(dev
);
1520 struct mac_device_info
*device
;
1522 if (priv
->plat
->has_gmac
)
1523 device
= dwmac1000_setup(priv
->ioaddr
);
1525 device
= dwmac100_setup(priv
->ioaddr
);
1530 if (priv
->plat
->enh_desc
) {
1531 device
->desc
= &enh_desc_ops
;
1532 pr_info("\tEnhanced descriptor structure\n");
1534 device
->desc
= &ndesc_ops
;
1538 if (device_can_wakeup(priv
->device
)) {
1539 priv
->wolopts
= WAKE_MAGIC
; /* Magic Frame as default */
1540 enable_irq_wake(dev
->irq
);
1546 static int stmmacphy_dvr_probe(struct platform_device
*pdev
)
1548 struct plat_stmmacphy_data
*plat_dat
= pdev
->dev
.platform_data
;
1550 pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
1556 static int stmmacphy_dvr_remove(struct platform_device
*pdev
)
1561 static struct platform_driver stmmacphy_driver
= {
1563 .name
= PHY_RESOURCE_NAME
,
1565 .probe
= stmmacphy_dvr_probe
,
1566 .remove
= stmmacphy_dvr_remove
,
1570 * stmmac_associate_phy
1571 * @dev: pointer to device structure
1572 * @data: points to the private structure.
1573 * Description: Scans through all the PHYs we have registered and checks if
1574 * any are associated with our MAC. If so, then just fill in
1575 * the blanks in our local context structure
1577 static int stmmac_associate_phy(struct device
*dev
, void *data
)
1579 struct stmmac_priv
*priv
= (struct stmmac_priv
*)data
;
1580 struct plat_stmmacphy_data
*plat_dat
= dev
->platform_data
;
1582 DBG(probe
, DEBUG
, "%s: checking phy for bus %d\n", __func__
,
1585 /* Check that this phy is for the MAC being initialised */
1586 if (priv
->plat
->bus_id
!= plat_dat
->bus_id
)
1589 /* OK, this PHY is connected to the MAC.
1590 Go ahead and get the parameters */
1591 DBG(probe
, DEBUG
, "%s: OK. Found PHY config\n", __func__
);
1593 platform_get_irq_byname(to_platform_device(dev
), "phyirq");
1594 DBG(probe
, DEBUG
, "%s: PHY irq on bus %d is %d\n", __func__
,
1595 plat_dat
->bus_id
, priv
->phy_irq
);
1597 /* Override with kernel parameters if supplied XXX CRS XXX
1598 * this needs to have multiple instances */
1599 if ((phyaddr
>= 0) && (phyaddr
<= 31))
1600 plat_dat
->phy_addr
= phyaddr
;
1602 priv
->phy_addr
= plat_dat
->phy_addr
;
1603 priv
->phy_mask
= plat_dat
->phy_mask
;
1604 priv
->phy_interface
= plat_dat
->interface
;
1605 priv
->phy_reset
= plat_dat
->phy_reset
;
1607 DBG(probe
, DEBUG
, "%s: exiting\n", __func__
);
1608 return 1; /* forces exit of driver_for_each_device() */
1613 * @pdev: platform device pointer
1614 * Description: the driver is initialized through platform_device.
1616 static int stmmac_dvr_probe(struct platform_device
*pdev
)
1619 struct resource
*res
;
1620 void __iomem
*addr
= NULL
;
1621 struct net_device
*ndev
= NULL
;
1622 struct stmmac_priv
*priv
= NULL
;
1623 struct plat_stmmacenet_data
*plat_dat
;
1625 pr_info("STMMAC driver:\n\tplatform registration... ");
1626 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1629 pr_info("\tdone!\n");
1631 if (!request_mem_region(res
->start
, resource_size(res
),
1633 pr_err("%s: ERROR: memory allocation failed"
1634 "cannot get the I/O addr 0x%x\n",
1635 __func__
, (unsigned int)res
->start
);
1639 addr
= ioremap(res
->start
, resource_size(res
));
1641 pr_err("%s: ERROR: memory mapping failed\n", __func__
);
1643 goto out_release_region
;
1646 ndev
= alloc_etherdev(sizeof(struct stmmac_priv
));
1648 pr_err("%s: ERROR: allocating the device\n", __func__
);
1653 SET_NETDEV_DEV(ndev
, &pdev
->dev
);
1655 /* Get the MAC information */
1656 ndev
->irq
= platform_get_irq_byname(pdev
, "macirq");
1657 if (ndev
->irq
== -ENXIO
) {
1658 pr_err("%s: ERROR: MAC IRQ configuration "
1659 "information not found\n", __func__
);
1664 priv
= netdev_priv(ndev
);
1665 priv
->device
= &(pdev
->dev
);
1667 plat_dat
= pdev
->dev
.platform_data
;
1669 priv
->plat
= plat_dat
;
1671 priv
->ioaddr
= addr
;
1673 /* PMT module is not integrated in all the MAC devices. */
1674 if (plat_dat
->pmt
) {
1675 pr_info("\tPMT module supported\n");
1676 device_set_wakeup_capable(&pdev
->dev
, 1);
1679 platform_set_drvdata(pdev
, ndev
);
1681 /* Set the I/O base addr */
1682 ndev
->base_addr
= (unsigned long)addr
;
1684 /* Custom initialisation */
1685 if (priv
->plat
->init
) {
1686 ret
= priv
->plat
->init(pdev
);
1691 /* MAC HW revice detection */
1692 ret
= stmmac_mac_device_setup(ndev
);
1696 /* Network Device Registration */
1697 ret
= stmmac_probe(ndev
);
1701 /* associate a PHY - it is provided by another platform bus */
1702 if (!driver_for_each_device
1703 (&(stmmacphy_driver
.driver
), NULL
, (void *)priv
,
1704 stmmac_associate_phy
)) {
1705 pr_err("No PHY device is associated with this MAC!\n");
1707 goto out_unregister
;
1710 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
1711 "\tIO base addr: 0x%p)\n", ndev
->name
, pdev
->name
,
1712 pdev
->id
, ndev
->irq
, addr
);
1714 /* MDIO bus Registration */
1715 pr_debug("\tMDIO bus (id: %d)...", priv
->plat
->bus_id
);
1716 ret
= stmmac_mdio_register(ndev
);
1718 goto out_unregister
;
1719 pr_debug("registered!\n");
1723 unregister_netdev(ndev
);
1725 if (priv
->plat
->exit
)
1726 priv
->plat
->exit(pdev
);
1729 platform_set_drvdata(pdev
, NULL
);
1733 release_mem_region(res
->start
, resource_size(res
));
1740 * @pdev: platform device pointer
1741 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1742 * changes the link status, releases the DMA descriptor rings,
1743 * unregisters the MDIO bus and unmaps the allocated memory.
1745 static int stmmac_dvr_remove(struct platform_device
*pdev
)
1747 struct net_device
*ndev
= platform_get_drvdata(pdev
);
1748 struct stmmac_priv
*priv
= netdev_priv(ndev
);
1749 struct resource
*res
;
1751 pr_info("%s:\n\tremoving driver", __func__
);
1753 priv
->hw
->dma
->stop_rx(priv
->ioaddr
);
1754 priv
->hw
->dma
->stop_tx(priv
->ioaddr
);
1756 stmmac_disable_mac(priv
->ioaddr
);
1758 netif_carrier_off(ndev
);
1760 stmmac_mdio_unregister(ndev
);
1762 if (priv
->plat
->exit
)
1763 priv
->plat
->exit(pdev
);
1765 platform_set_drvdata(pdev
, NULL
);
1766 unregister_netdev(ndev
);
1768 iounmap((void *)priv
->ioaddr
);
1769 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1770 release_mem_region(res
->start
, resource_size(res
));
1778 static int stmmac_suspend(struct device
*dev
)
1780 struct net_device
*ndev
= dev_get_drvdata(dev
);
1781 struct stmmac_priv
*priv
= netdev_priv(ndev
);
1784 if (!ndev
|| !netif_running(ndev
))
1787 spin_lock(&priv
->lock
);
1789 netif_device_detach(ndev
);
1790 netif_stop_queue(ndev
);
1792 phy_stop(priv
->phydev
);
1794 #ifdef CONFIG_STMMAC_TIMER
1795 priv
->tm
->timer_stop();
1796 if (likely(priv
->tm
->enable
))
1799 napi_disable(&priv
->napi
);
1801 /* Stop TX/RX DMA */
1802 priv
->hw
->dma
->stop_tx(priv
->ioaddr
);
1803 priv
->hw
->dma
->stop_rx(priv
->ioaddr
);
1804 /* Clear the Rx/Tx descriptors */
1805 priv
->hw
->desc
->init_rx_desc(priv
->dma_rx
, priv
->dma_rx_size
,
1807 priv
->hw
->desc
->init_tx_desc(priv
->dma_tx
, priv
->dma_tx_size
);
1809 /* Enable Power down mode by programming the PMT regs */
1810 if (device_may_wakeup(priv
->device
))
1811 priv
->hw
->mac
->pmt(priv
->ioaddr
, priv
->wolopts
);
1813 stmmac_disable_mac(priv
->ioaddr
);
1815 spin_unlock(&priv
->lock
);
1819 static int stmmac_resume(struct device
*dev
)
1821 struct net_device
*ndev
= dev_get_drvdata(dev
);
1822 struct stmmac_priv
*priv
= netdev_priv(ndev
);
1824 if (!netif_running(ndev
))
1827 spin_lock(&priv
->lock
);
1829 /* Power Down bit, into the PM register, is cleared
1830 * automatically as soon as a magic packet or a Wake-up frame
1831 * is received. Anyway, it's better to manually clear
1832 * this bit because it can generate problems while resuming
1833 * from another devices (e.g. serial console). */
1834 if (device_may_wakeup(priv
->device
))
1835 priv
->hw
->mac
->pmt(priv
->ioaddr
, 0);
1837 netif_device_attach(ndev
);
1839 /* Enable the MAC and DMA */
1840 stmmac_enable_mac(priv
->ioaddr
);
1841 priv
->hw
->dma
->start_tx(priv
->ioaddr
);
1842 priv
->hw
->dma
->start_rx(priv
->ioaddr
);
1844 #ifdef CONFIG_STMMAC_TIMER
1845 if (likely(priv
->tm
->enable
))
1846 priv
->tm
->timer_start(tmrate
);
1848 napi_enable(&priv
->napi
);
1851 phy_start(priv
->phydev
);
1853 netif_start_queue(ndev
);
1855 spin_unlock(&priv
->lock
);
1859 static int stmmac_freeze(struct device
*dev
)
1861 struct net_device
*ndev
= dev_get_drvdata(dev
);
1863 if (!ndev
|| !netif_running(ndev
))
1866 return stmmac_release(ndev
);
1869 static int stmmac_restore(struct device
*dev
)
1871 struct net_device
*ndev
= dev_get_drvdata(dev
);
1873 if (!ndev
|| !netif_running(ndev
))
1876 return stmmac_open(ndev
);
1879 static const struct dev_pm_ops stmmac_pm_ops
= {
1880 .suspend
= stmmac_suspend
,
1881 .resume
= stmmac_resume
,
1882 .freeze
= stmmac_freeze
,
1883 .thaw
= stmmac_restore
,
1884 .restore
= stmmac_restore
,
1887 static const struct dev_pm_ops stmmac_pm_ops
;
1888 #endif /* CONFIG_PM */
1890 static struct platform_driver stmmac_driver
= {
1891 .probe
= stmmac_dvr_probe
,
1892 .remove
= stmmac_dvr_remove
,
1894 .name
= STMMAC_RESOURCE_NAME
,
1895 .owner
= THIS_MODULE
,
1896 .pm
= &stmmac_pm_ops
,
1901 * stmmac_init_module - Entry point for the driver
1902 * Description: This function is the entry point for the driver.
1904 static int __init
stmmac_init_module(void)
1908 if (platform_driver_register(&stmmacphy_driver
)) {
1909 pr_err("No PHY devices registered!\n");
1913 ret
= platform_driver_register(&stmmac_driver
);
1918 * stmmac_cleanup_module - Cleanup routine for the driver
1919 * Description: This function is the cleanup routine for the driver.
1921 static void __exit
stmmac_cleanup_module(void)
1923 platform_driver_unregister(&stmmacphy_driver
);
1924 platform_driver_unregister(&stmmac_driver
);
1928 static int __init
stmmac_cmdline_opt(char *str
)
1934 while ((opt
= strsep(&str
, ",")) != NULL
) {
1935 if (!strncmp(opt
, "debug:", 6))
1936 strict_strtoul(opt
+ 6, 0, (unsigned long *)&debug
);
1937 else if (!strncmp(opt
, "phyaddr:", 8))
1938 strict_strtoul(opt
+ 8, 0, (unsigned long *)&phyaddr
);
1939 else if (!strncmp(opt
, "dma_txsize:", 11))
1940 strict_strtoul(opt
+ 11, 0,
1941 (unsigned long *)&dma_txsize
);
1942 else if (!strncmp(opt
, "dma_rxsize:", 11))
1943 strict_strtoul(opt
+ 11, 0,
1944 (unsigned long *)&dma_rxsize
);
1945 else if (!strncmp(opt
, "buf_sz:", 7))
1946 strict_strtoul(opt
+ 7, 0, (unsigned long *)&buf_sz
);
1947 else if (!strncmp(opt
, "tc:", 3))
1948 strict_strtoul(opt
+ 3, 0, (unsigned long *)&tc
);
1949 else if (!strncmp(opt
, "watchdog:", 9))
1950 strict_strtoul(opt
+ 9, 0, (unsigned long *)&watchdog
);
1951 else if (!strncmp(opt
, "flow_ctrl:", 10))
1952 strict_strtoul(opt
+ 10, 0,
1953 (unsigned long *)&flow_ctrl
);
1954 else if (!strncmp(opt
, "pause:", 6))
1955 strict_strtoul(opt
+ 6, 0, (unsigned long *)&pause
);
1956 #ifdef CONFIG_STMMAC_TIMER
1957 else if (!strncmp(opt
, "tmrate:", 7))
1958 strict_strtoul(opt
+ 7, 0, (unsigned long *)&tmrate
);
1964 __setup("stmmaceth=", stmmac_cmdline_opt
);
1967 module_init(stmmac_init_module
);
1968 module_exit(stmmac_cleanup_module
);
1970 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
1971 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
1972 MODULE_LICENSE("GPL");