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>
50 #define STMMAC_RESOURCE_NAME "stmmaceth"
51 #define PHY_RESOURCE_NAME "stmmacphy"
54 /*#define STMMAC_DEBUG*/
56 #define DBG(nlevel, klevel, fmt, args...) \
57 ((void)(netif_msg_##nlevel(priv) && \
58 printk(KERN_##klevel fmt, ## args)))
60 #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
63 #undef STMMAC_RX_DEBUG
64 /*#define STMMAC_RX_DEBUG*/
65 #ifdef STMMAC_RX_DEBUG
66 #define RX_DBG(fmt, args...) printk(fmt, ## args)
68 #define RX_DBG(fmt, args...) do { } while (0)
71 #undef STMMAC_XMIT_DEBUG
72 /*#define STMMAC_XMIT_DEBUG*/
73 #ifdef STMMAC_TX_DEBUG
74 #define TX_DBG(fmt, args...) printk(fmt, ## args)
76 #define TX_DBG(fmt, args...) do { } while (0)
79 #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
80 #define JUMBO_LEN 9000
82 /* Module parameters */
83 #define TX_TIMEO 5000 /* default 5 seconds */
84 static int watchdog = TX_TIMEO;
85 module_param(watchdog, int, S_IRUGO | S_IWUSR);
86 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
88 static int debug = -1; /* -1: default, 0: no output, 16: all */
89 module_param(debug, int, S_IRUGO | S_IWUSR);
90 MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
92 static int phyaddr = -1;
93 module_param(phyaddr, int, S_IRUGO);
94 MODULE_PARM_DESC(phyaddr, "Physical device address");
96 #define DMA_TX_SIZE 256
97 static int dma_txsize = DMA_TX_SIZE;
98 module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
99 MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
101 #define DMA_RX_SIZE 256
102 static int dma_rxsize = DMA_RX_SIZE;
103 module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
104 MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
106 static int flow_ctrl = FLOW_OFF;
107 module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
108 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
110 static int pause = PAUSE_TIME;
111 module_param(pause, int, S_IRUGO | S_IWUSR);
112 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
114 #define TC_DEFAULT 64
115 static int tc = TC_DEFAULT;
116 module_param(tc, int, S_IRUGO | S_IWUSR);
117 MODULE_PARM_DESC(tc, "DMA threshold control value");
119 /* Pay attention to tune this parameter; take care of both
120 * hardware capability and network stabitily/performance impact.
121 * Many tests showed that ~4ms latency seems to be good enough. */
122 #ifdef CONFIG_STMMAC_TIMER
123 #define DEFAULT_PERIODIC_RATE 256
124 static int tmrate = DEFAULT_PERIODIC_RATE;
125 module_param(tmrate, int, S_IRUGO | S_IWUSR);
126 MODULE_PARM_DESC(tmrate, "External timer freq. (default: 256Hz)");
129 #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
130 static int buf_sz = DMA_BUFFER_SIZE;
131 module_param(buf_sz, int, S_IRUGO | S_IWUSR);
132 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
134 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
135 NETIF_MSG_LINK | NETIF_MSG_IFUP |
136 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
138 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
141 * stmmac_verify_args - verify the driver parameters.
142 * Description: it verifies if some wrong parameter is passed to the driver.
143 * Note that wrong parameters are replaced with the default values.
145 static void stmmac_verify_args(void)
147 if (unlikely(watchdog < 0))
149 if (unlikely(dma_rxsize < 0))
150 dma_rxsize = DMA_RX_SIZE;
151 if (unlikely(dma_txsize < 0))
152 dma_txsize = DMA_TX_SIZE;
153 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
154 buf_sz = DMA_BUFFER_SIZE;
155 if (unlikely(flow_ctrl > 1))
156 flow_ctrl = FLOW_AUTO;
157 else if (likely(flow_ctrl < 0))
158 flow_ctrl = FLOW_OFF;
159 if (unlikely((pause < 0) || (pause > 0xffff)))
163 #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
164 static void print_pkt(unsigned char *buf, int len)
167 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
168 for (j = 0; j < len; j++) {
170 pr_info("\n %03x:", j);
171 pr_info(" %02x", buf[j]);
177 /* minimum number of free TX descriptors required to wake up TX process */
178 #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
180 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
182 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
185 /* On some ST platforms, some HW system configuraton registers have to be
186 * set according to the link speed negotiated.
188 static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
190 struct phy_device *phydev = priv->phydev;
192 if (likely(priv->plat->fix_mac_speed))
193 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
199 * @dev: net device structure
200 * Description: it adjusts the link parameters.
202 static void stmmac_adjust_link(struct net_device *dev)
204 struct stmmac_priv *priv = netdev_priv(dev);
205 struct phy_device *phydev = priv->phydev;
208 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
213 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
214 phydev->addr, phydev->link);
216 spin_lock_irqsave(&priv->lock, flags);
218 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
220 /* Now we make sure that we can be in full duplex mode.
221 * If not, we operate in half-duplex mode. */
222 if (phydev->duplex != priv->oldduplex) {
224 if (!(phydev->duplex))
225 ctrl &= ~priv->hw->link.duplex;
227 ctrl |= priv->hw->link.duplex;
228 priv->oldduplex = phydev->duplex;
230 /* Flow Control operation */
232 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
235 if (phydev->speed != priv->speed) {
237 switch (phydev->speed) {
239 if (likely(priv->plat->has_gmac))
240 ctrl &= ~priv->hw->link.port;
241 stmmac_hw_fix_mac_speed(priv);
245 if (priv->plat->has_gmac) {
246 ctrl |= priv->hw->link.port;
247 if (phydev->speed == SPEED_100) {
248 ctrl |= priv->hw->link.speed;
250 ctrl &= ~(priv->hw->link.speed);
253 ctrl &= ~priv->hw->link.port;
255 stmmac_hw_fix_mac_speed(priv);
258 if (netif_msg_link(priv))
259 pr_warning("%s: Speed (%d) is not 10"
260 " or 100!\n", dev->name, phydev->speed);
264 priv->speed = phydev->speed;
267 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
269 if (!priv->oldlink) {
273 } else if (priv->oldlink) {
277 priv->oldduplex = -1;
280 if (new_state && netif_msg_link(priv))
281 phy_print_status(phydev);
283 spin_unlock_irqrestore(&priv->lock, flags);
285 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
289 * stmmac_init_phy - PHY initialization
290 * @dev: net device structure
291 * Description: it initializes the driver's PHY state, and attaches the PHY
296 static int stmmac_init_phy(struct net_device *dev)
298 struct stmmac_priv *priv = netdev_priv(dev);
299 struct phy_device *phydev;
300 char phy_id[MII_BUS_ID_SIZE + 3];
301 char bus_id[MII_BUS_ID_SIZE];
305 priv->oldduplex = -1;
307 if (priv->phy_addr == -1) {
308 /* We don't have a PHY, so do nothing */
312 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
313 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
315 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
317 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
318 priv->phy_interface);
320 if (IS_ERR(phydev)) {
321 pr_err("%s: Could not attach to PHY\n", dev->name);
322 return PTR_ERR(phydev);
326 * Broken HW is sometimes missing the pull-up resistor on the
327 * MDIO line, which results in reads to non-existent devices returning
328 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
330 * Note: phydev->phy_id is the result of reading the UID PHY registers.
332 if (phydev->phy_id == 0) {
333 phy_disconnect(phydev);
336 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
337 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
339 priv->phydev = phydev;
344 static inline void stmmac_enable_mac(void __iomem *ioaddr)
346 u32 value = readl(ioaddr + MAC_CTRL_REG);
348 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
349 writel(value, ioaddr + MAC_CTRL_REG);
352 static inline void stmmac_disable_mac(void __iomem *ioaddr)
354 u32 value = readl(ioaddr + MAC_CTRL_REG);
356 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
357 writel(value, ioaddr + MAC_CTRL_REG);
362 * @p: pointer to the ring.
363 * @size: size of the ring.
364 * Description: display all the descriptors within the ring.
366 static void display_ring(struct dma_desc *p, int size)
374 for (i = 0; i < size; i++) {
375 struct tmp_s *x = (struct tmp_s *)(p + i);
376 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
377 i, (unsigned int)virt_to_phys(&p[i]),
378 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
385 * init_dma_desc_rings - init the RX/TX descriptor rings
386 * @dev: net device structure
387 * Description: this function initializes the DMA RX/TX descriptors
388 * and allocates the socket buffers.
390 static void init_dma_desc_rings(struct net_device *dev)
393 struct stmmac_priv *priv = netdev_priv(dev);
395 unsigned int txsize = priv->dma_tx_size;
396 unsigned int rxsize = priv->dma_rx_size;
397 unsigned int bfsize = priv->dma_buf_sz;
398 int buff2_needed = 0, dis_ic = 0;
400 /* Set the Buffer size according to the MTU;
401 * indeed, in case of jumbo we need to bump-up the buffer sizes.
403 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
404 bfsize = BUF_SIZE_16KiB;
405 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
406 bfsize = BUF_SIZE_8KiB;
407 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
408 bfsize = BUF_SIZE_4KiB;
409 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
410 bfsize = BUF_SIZE_2KiB;
412 bfsize = DMA_BUFFER_SIZE;
414 #ifdef CONFIG_STMMAC_TIMER
415 /* Disable interrupts on completion for the reception if timer is on */
416 if (likely(priv->tm->enable))
419 /* If the MTU exceeds 8k so use the second buffer in the chain */
420 if (bfsize >= BUF_SIZE_8KiB)
423 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
424 txsize, rxsize, bfsize);
426 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
428 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
430 (struct dma_desc *)dma_alloc_coherent(priv->device,
432 sizeof(struct dma_desc),
435 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
438 (struct dma_desc *)dma_alloc_coherent(priv->device,
440 sizeof(struct dma_desc),
444 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
445 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
449 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
450 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
451 dev->name, priv->dma_rx, priv->dma_tx,
452 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
454 /* RX INITIALIZATION */
455 DBG(probe, INFO, "stmmac: SKB addresses:\n"
456 "skb\t\tskb data\tdma data\n");
458 for (i = 0; i < rxsize; i++) {
459 struct dma_desc *p = priv->dma_rx + i;
461 skb = netdev_alloc_skb_ip_align(dev, bfsize);
462 if (unlikely(skb == NULL)) {
463 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
466 priv->rx_skbuff[i] = skb;
467 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
468 bfsize, DMA_FROM_DEVICE);
470 p->des2 = priv->rx_skbuff_dma[i];
471 if (unlikely(buff2_needed))
472 p->des3 = p->des2 + BUF_SIZE_8KiB;
473 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
474 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
477 priv->dirty_rx = (unsigned int)(i - rxsize);
478 priv->dma_buf_sz = bfsize;
481 /* TX INITIALIZATION */
482 for (i = 0; i < txsize; i++) {
483 priv->tx_skbuff[i] = NULL;
484 priv->dma_tx[i].des2 = 0;
489 /* Clear the Rx/Tx descriptors */
490 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
491 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
493 if (netif_msg_hw(priv)) {
494 pr_info("RX descriptor ring:\n");
495 display_ring(priv->dma_rx, rxsize);
496 pr_info("TX descriptor ring:\n");
497 display_ring(priv->dma_tx, txsize);
501 static void dma_free_rx_skbufs(struct stmmac_priv *priv)
505 for (i = 0; i < priv->dma_rx_size; i++) {
506 if (priv->rx_skbuff[i]) {
507 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
508 priv->dma_buf_sz, DMA_FROM_DEVICE);
509 dev_kfree_skb_any(priv->rx_skbuff[i]);
511 priv->rx_skbuff[i] = NULL;
515 static void dma_free_tx_skbufs(struct stmmac_priv *priv)
519 for (i = 0; i < priv->dma_tx_size; i++) {
520 if (priv->tx_skbuff[i] != NULL) {
521 struct dma_desc *p = priv->dma_tx + i;
523 dma_unmap_single(priv->device, p->des2,
524 priv->hw->desc->get_tx_len(p),
526 dev_kfree_skb_any(priv->tx_skbuff[i]);
527 priv->tx_skbuff[i] = NULL;
532 static void free_dma_desc_resources(struct stmmac_priv *priv)
534 /* Release the DMA TX/RX socket buffers */
535 dma_free_rx_skbufs(priv);
536 dma_free_tx_skbufs(priv);
538 /* Free the region of consistent memory previously allocated for
540 dma_free_coherent(priv->device,
541 priv->dma_tx_size * sizeof(struct dma_desc),
542 priv->dma_tx, priv->dma_tx_phy);
543 dma_free_coherent(priv->device,
544 priv->dma_rx_size * sizeof(struct dma_desc),
545 priv->dma_rx, priv->dma_rx_phy);
546 kfree(priv->rx_skbuff_dma);
547 kfree(priv->rx_skbuff);
548 kfree(priv->tx_skbuff);
552 * stmmac_dma_operation_mode - HW DMA operation mode
553 * @priv : pointer to the private device structure.
554 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
555 * or Store-And-Forward capability.
557 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
559 if (likely((priv->plat->tx_coe) && (!priv->no_csum_insertion))) {
560 /* In case of GMAC, SF mode has to be enabled
561 * to perform the TX COE. This depends on:
562 * 1) TX COE if actually supported
563 * 2) There is no bugged Jumbo frame support
564 * that needs to not insert csum in the TDES.
566 priv->hw->dma->dma_mode(priv->ioaddr,
567 SF_DMA_MODE, SF_DMA_MODE);
570 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
575 * @priv: private driver structure
576 * Description: it reclaims resources after transmission completes.
578 static void stmmac_tx(struct stmmac_priv *priv)
580 unsigned int txsize = priv->dma_tx_size;
582 while (priv->dirty_tx != priv->cur_tx) {
584 unsigned int entry = priv->dirty_tx % txsize;
585 struct sk_buff *skb = priv->tx_skbuff[entry];
586 struct dma_desc *p = priv->dma_tx + entry;
588 /* Check if the descriptor is owned by the DMA. */
589 if (priv->hw->desc->get_tx_owner(p))
592 /* Verify tx error by looking at the last segment */
593 last = priv->hw->desc->get_tx_ls(p);
596 priv->hw->desc->tx_status(&priv->dev->stats,
599 if (likely(tx_error == 0)) {
600 priv->dev->stats.tx_packets++;
601 priv->xstats.tx_pkt_n++;
603 priv->dev->stats.tx_errors++;
605 TX_DBG("%s: curr %d, dirty %d\n", __func__,
606 priv->cur_tx, priv->dirty_tx);
609 dma_unmap_single(priv->device, p->des2,
610 priv->hw->desc->get_tx_len(p),
612 if (unlikely(p->des3))
615 if (likely(skb != NULL)) {
617 * If there's room in the queue (limit it to size)
618 * we add this skb back into the pool,
619 * if it's the right size.
621 if ((skb_queue_len(&priv->rx_recycle) <
622 priv->dma_rx_size) &&
623 skb_recycle_check(skb, priv->dma_buf_sz))
624 __skb_queue_head(&priv->rx_recycle, skb);
628 priv->tx_skbuff[entry] = NULL;
631 priv->hw->desc->release_tx_desc(p);
633 entry = (++priv->dirty_tx) % txsize;
635 if (unlikely(netif_queue_stopped(priv->dev) &&
636 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
637 netif_tx_lock(priv->dev);
638 if (netif_queue_stopped(priv->dev) &&
639 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
640 TX_DBG("%s: restart transmit\n", __func__);
641 netif_wake_queue(priv->dev);
643 netif_tx_unlock(priv->dev);
647 static inline void stmmac_enable_irq(struct stmmac_priv *priv)
649 #ifdef CONFIG_STMMAC_TIMER
650 if (likely(priv->tm->enable))
651 priv->tm->timer_start(tmrate);
654 priv->hw->dma->enable_dma_irq(priv->ioaddr);
657 static inline void stmmac_disable_irq(struct stmmac_priv *priv)
659 #ifdef CONFIG_STMMAC_TIMER
660 if (likely(priv->tm->enable))
661 priv->tm->timer_stop();
664 priv->hw->dma->disable_dma_irq(priv->ioaddr);
667 static int stmmac_has_work(struct stmmac_priv *priv)
669 unsigned int has_work = 0;
670 int rxret, tx_work = 0;
672 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
673 (priv->cur_rx % priv->dma_rx_size));
675 if (priv->dirty_tx != priv->cur_tx)
678 if (likely(!rxret || tx_work))
684 static inline void _stmmac_schedule(struct stmmac_priv *priv)
686 if (likely(stmmac_has_work(priv))) {
687 stmmac_disable_irq(priv);
688 napi_schedule(&priv->napi);
692 #ifdef CONFIG_STMMAC_TIMER
693 void stmmac_schedule(struct net_device *dev)
695 struct stmmac_priv *priv = netdev_priv(dev);
697 priv->xstats.sched_timer_n++;
699 _stmmac_schedule(priv);
702 static void stmmac_no_timer_started(unsigned int x)
706 static void stmmac_no_timer_stopped(void)
713 * @priv: pointer to the private device structure
714 * Description: it cleans the descriptors and restarts the transmission
717 static void stmmac_tx_err(struct stmmac_priv *priv)
720 netif_stop_queue(priv->dev);
722 priv->hw->dma->stop_tx(priv->ioaddr);
723 dma_free_tx_skbufs(priv);
724 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
727 priv->hw->dma->start_tx(priv->ioaddr);
729 priv->dev->stats.tx_errors++;
730 netif_wake_queue(priv->dev);
734 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
738 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
739 if (likely(status == handle_tx_rx))
740 _stmmac_schedule(priv);
742 else if (unlikely(status == tx_hard_error_bump_tc)) {
743 /* Try to bump up the dma threshold on this failure */
744 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
746 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
747 priv->xstats.threshold = tc;
749 } else if (unlikely(status == tx_hard_error))
754 * stmmac_open - open entry point of the driver
755 * @dev : pointer to the device structure.
757 * This function is the open entry point of the driver.
759 * 0 on success and an appropriate (-)ve integer as defined in errno.h
762 static int stmmac_open(struct net_device *dev)
764 struct stmmac_priv *priv = netdev_priv(dev);
767 /* Check that the MAC address is valid. If its not, refuse
768 * to bring the device up. The user must specify an
769 * address using the following linux command:
770 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
771 if (!is_valid_ether_addr(dev->dev_addr)) {
772 random_ether_addr(dev->dev_addr);
773 pr_warning("%s: generated random MAC address %pM\n", dev->name,
777 stmmac_verify_args();
779 #ifdef CONFIG_STMMAC_TIMER
780 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
781 if (unlikely(priv->tm == NULL)) {
782 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
785 priv->tm->freq = tmrate;
787 /* Test if the external timer can be actually used.
788 * In case of failure continue without timer. */
789 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
790 pr_warning("stmmaceth: cannot attach the external timer.\n");
792 priv->tm->timer_start = stmmac_no_timer_started;
793 priv->tm->timer_stop = stmmac_no_timer_stopped;
795 priv->tm->enable = 1;
797 ret = stmmac_init_phy(dev);
799 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
803 /* Create and initialize the TX/RX descriptors chains. */
804 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
805 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
806 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
807 init_dma_desc_rings(dev);
809 /* DMA initialization and SW reset */
810 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
811 priv->dma_tx_phy, priv->dma_rx_phy);
813 pr_err("%s: DMA initialization failed\n", __func__);
817 /* Copy the MAC addr into the HW */
818 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
819 /* If required, perform hw setup of the bus. */
820 if (priv->plat->bus_setup)
821 priv->plat->bus_setup(priv->ioaddr);
822 /* Initialize the MAC Core */
823 priv->hw->mac->core_init(priv->ioaddr);
825 priv->rx_coe = priv->hw->mac->rx_coe(priv->ioaddr);
827 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
828 if (priv->plat->tx_coe)
829 pr_info("\tTX Checksum insertion supported\n");
830 netdev_update_features(dev);
832 /* Initialise the MMC (if present) to disable all interrupts. */
833 writel(0xffffffff, priv->ioaddr + MMC_HIGH_INTR_MASK);
834 writel(0xffffffff, priv->ioaddr + MMC_LOW_INTR_MASK);
836 /* Request the IRQ lines */
837 ret = request_irq(dev->irq, stmmac_interrupt,
838 IRQF_SHARED, dev->name, dev);
839 if (unlikely(ret < 0)) {
840 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
841 __func__, dev->irq, ret);
845 /* Enable the MAC Rx/Tx */
846 stmmac_enable_mac(priv->ioaddr);
848 /* Set the HW DMA mode and the COE */
849 stmmac_dma_operation_mode(priv);
851 /* Extra statistics */
852 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
853 priv->xstats.threshold = tc;
855 /* Start the ball rolling... */
856 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
857 priv->hw->dma->start_tx(priv->ioaddr);
858 priv->hw->dma->start_rx(priv->ioaddr);
860 #ifdef CONFIG_STMMAC_TIMER
861 priv->tm->timer_start(tmrate);
863 /* Dump DMA/MAC registers */
864 if (netif_msg_hw(priv)) {
865 priv->hw->mac->dump_regs(priv->ioaddr);
866 priv->hw->dma->dump_regs(priv->ioaddr);
870 phy_start(priv->phydev);
872 napi_enable(&priv->napi);
873 skb_queue_head_init(&priv->rx_recycle);
874 netif_start_queue(dev);
879 #ifdef CONFIG_STMMAC_TIMER
883 phy_disconnect(priv->phydev);
889 * stmmac_release - close entry point of the driver
890 * @dev : device pointer.
892 * This is the stop entry point of the driver.
894 static int stmmac_release(struct net_device *dev)
896 struct stmmac_priv *priv = netdev_priv(dev);
898 /* Stop and disconnect the PHY */
900 phy_stop(priv->phydev);
901 phy_disconnect(priv->phydev);
905 netif_stop_queue(dev);
907 #ifdef CONFIG_STMMAC_TIMER
908 /* Stop and release the timer */
909 stmmac_close_ext_timer();
910 if (priv->tm != NULL)
913 napi_disable(&priv->napi);
914 skb_queue_purge(&priv->rx_recycle);
916 /* Free the IRQ lines */
917 free_irq(dev->irq, dev);
919 /* Stop TX/RX DMA and clear the descriptors */
920 priv->hw->dma->stop_tx(priv->ioaddr);
921 priv->hw->dma->stop_rx(priv->ioaddr);
923 /* Release and free the Rx/Tx resources */
924 free_dma_desc_resources(priv);
926 /* Disable the MAC Rx/Tx */
927 stmmac_disable_mac(priv->ioaddr);
929 netif_carrier_off(dev);
934 static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
935 struct net_device *dev,
938 struct stmmac_priv *priv = netdev_priv(dev);
939 unsigned int nopaged_len = skb_headlen(skb);
940 unsigned int txsize = priv->dma_tx_size;
941 unsigned int entry = priv->cur_tx % txsize;
942 struct dma_desc *desc = priv->dma_tx + entry;
944 if (nopaged_len > BUF_SIZE_8KiB) {
946 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
948 desc->des2 = dma_map_single(priv->device, skb->data,
949 BUF_SIZE_8KiB, DMA_TO_DEVICE);
950 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
951 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
954 entry = (++priv->cur_tx) % txsize;
955 desc = priv->dma_tx + entry;
957 desc->des2 = dma_map_single(priv->device,
958 skb->data + BUF_SIZE_8KiB,
959 buf2_size, DMA_TO_DEVICE);
960 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
961 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
963 priv->hw->desc->set_tx_owner(desc);
964 priv->tx_skbuff[entry] = NULL;
966 desc->des2 = dma_map_single(priv->device, skb->data,
967 nopaged_len, DMA_TO_DEVICE);
968 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
969 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
977 * @skb : the socket buffer
978 * @dev : device pointer
979 * Description : Tx entry point of the driver.
981 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
983 struct stmmac_priv *priv = netdev_priv(dev);
984 unsigned int txsize = priv->dma_tx_size;
986 int i, csum_insertion = 0;
987 int nfrags = skb_shinfo(skb)->nr_frags;
988 struct dma_desc *desc, *first;
990 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
991 if (!netif_queue_stopped(dev)) {
992 netif_stop_queue(dev);
993 /* This is a hard error, log it. */
994 pr_err("%s: BUG! Tx Ring full when queue awake\n",
997 return NETDEV_TX_BUSY;
1000 entry = priv->cur_tx % txsize;
1002 #ifdef STMMAC_XMIT_DEBUG
1003 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1004 pr_info("stmmac xmit:\n"
1005 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1006 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1007 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1008 !skb_is_gso(skb) ? "isn't" : "is");
1011 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
1013 desc = priv->dma_tx + entry;
1016 #ifdef STMMAC_XMIT_DEBUG
1017 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1018 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1019 "\t\tn_frags: %d, ip_summed: %d\n",
1020 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1022 priv->tx_skbuff[entry] = skb;
1023 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1024 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1025 desc = priv->dma_tx + entry;
1027 unsigned int nopaged_len = skb_headlen(skb);
1028 desc->des2 = dma_map_single(priv->device, skb->data,
1029 nopaged_len, DMA_TO_DEVICE);
1030 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1034 for (i = 0; i < nfrags; i++) {
1035 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1036 int len = frag->size;
1038 entry = (++priv->cur_tx) % txsize;
1039 desc = priv->dma_tx + entry;
1041 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1042 desc->des2 = dma_map_page(priv->device, frag->page,
1044 len, DMA_TO_DEVICE);
1045 priv->tx_skbuff[entry] = NULL;
1046 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
1047 priv->hw->desc->set_tx_owner(desc);
1050 /* Interrupt on completition only for the latest segment */
1051 priv->hw->desc->close_tx_desc(desc);
1053 #ifdef CONFIG_STMMAC_TIMER
1054 /* Clean IC while using timer */
1055 if (likely(priv->tm->enable))
1056 priv->hw->desc->clear_tx_ic(desc);
1058 /* To avoid raise condition */
1059 priv->hw->desc->set_tx_owner(first);
1063 #ifdef STMMAC_XMIT_DEBUG
1064 if (netif_msg_pktdata(priv)) {
1065 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1066 "first=%p, nfrags=%d\n",
1067 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1068 entry, first, nfrags);
1069 display_ring(priv->dma_tx, txsize);
1070 pr_info(">>> frame to be transmitted: ");
1071 print_pkt(skb->data, skb->len);
1074 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1075 TX_DBG("%s: stop transmitted packets\n", __func__);
1076 netif_stop_queue(dev);
1079 dev->stats.tx_bytes += skb->len;
1081 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1083 return NETDEV_TX_OK;
1086 static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1088 unsigned int rxsize = priv->dma_rx_size;
1089 int bfsize = priv->dma_buf_sz;
1090 struct dma_desc *p = priv->dma_rx;
1092 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1093 unsigned int entry = priv->dirty_rx % rxsize;
1094 if (likely(priv->rx_skbuff[entry] == NULL)) {
1095 struct sk_buff *skb;
1097 skb = __skb_dequeue(&priv->rx_recycle);
1099 skb = netdev_alloc_skb_ip_align(priv->dev,
1102 if (unlikely(skb == NULL))
1105 priv->rx_skbuff[entry] = skb;
1106 priv->rx_skbuff_dma[entry] =
1107 dma_map_single(priv->device, skb->data, bfsize,
1110 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
1111 if (unlikely(priv->plat->has_gmac)) {
1112 if (bfsize >= BUF_SIZE_8KiB)
1114 (p + entry)->des2 + BUF_SIZE_8KiB;
1116 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1118 priv->hw->desc->set_rx_owner(p + entry);
1122 static int stmmac_rx(struct stmmac_priv *priv, int limit)
1124 unsigned int rxsize = priv->dma_rx_size;
1125 unsigned int entry = priv->cur_rx % rxsize;
1126 unsigned int next_entry;
1127 unsigned int count = 0;
1128 struct dma_desc *p = priv->dma_rx + entry;
1129 struct dma_desc *p_next;
1131 #ifdef STMMAC_RX_DEBUG
1132 if (netif_msg_hw(priv)) {
1133 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1134 display_ring(priv->dma_rx, rxsize);
1138 while (!priv->hw->desc->get_rx_owner(p)) {
1146 next_entry = (++priv->cur_rx) % rxsize;
1147 p_next = priv->dma_rx + next_entry;
1150 /* read the status of the incoming frame */
1151 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1153 if (unlikely(status == discard_frame))
1154 priv->dev->stats.rx_errors++;
1156 struct sk_buff *skb;
1159 frame_len = priv->hw->desc->get_rx_frame_len(p);
1160 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1161 * Type frames (LLC/LLC-SNAP) */
1162 if (unlikely(status != llc_snap))
1163 frame_len -= ETH_FCS_LEN;
1164 #ifdef STMMAC_RX_DEBUG
1165 if (frame_len > ETH_FRAME_LEN)
1166 pr_debug("\tRX frame size %d, COE status: %d\n",
1169 if (netif_msg_hw(priv))
1170 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1173 skb = priv->rx_skbuff[entry];
1174 if (unlikely(!skb)) {
1175 pr_err("%s: Inconsistent Rx descriptor chain\n",
1177 priv->dev->stats.rx_dropped++;
1180 prefetch(skb->data - NET_IP_ALIGN);
1181 priv->rx_skbuff[entry] = NULL;
1183 skb_put(skb, frame_len);
1184 dma_unmap_single(priv->device,
1185 priv->rx_skbuff_dma[entry],
1186 priv->dma_buf_sz, DMA_FROM_DEVICE);
1187 #ifdef STMMAC_RX_DEBUG
1188 if (netif_msg_pktdata(priv)) {
1189 pr_info(" frame received (%dbytes)", frame_len);
1190 print_pkt(skb->data, frame_len);
1193 skb->protocol = eth_type_trans(skb, priv->dev);
1195 if (unlikely(status == csum_none)) {
1196 /* always for the old mac 10/100 */
1197 skb_checksum_none_assert(skb);
1198 netif_receive_skb(skb);
1200 skb->ip_summed = CHECKSUM_UNNECESSARY;
1201 napi_gro_receive(&priv->napi, skb);
1204 priv->dev->stats.rx_packets++;
1205 priv->dev->stats.rx_bytes += frame_len;
1208 p = p_next; /* use prefetched values */
1211 stmmac_rx_refill(priv);
1213 priv->xstats.rx_pkt_n += count;
1219 * stmmac_poll - stmmac poll method (NAPI)
1220 * @napi : pointer to the napi structure.
1221 * @budget : maximum number of packets that the current CPU can receive from
1224 * This function implements the the reception process.
1225 * Also it runs the TX completion thread
1227 static int stmmac_poll(struct napi_struct *napi, int budget)
1229 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1232 priv->xstats.poll_n++;
1234 work_done = stmmac_rx(priv, budget);
1236 if (work_done < budget) {
1237 napi_complete(napi);
1238 stmmac_enable_irq(priv);
1245 * @dev : Pointer to net device structure
1246 * Description: this function is called when a packet transmission fails to
1247 * complete within a reasonable tmrate. The driver will mark the error in the
1248 * netdev structure and arrange for the device to be reset to a sane state
1249 * in order to transmit a new packet.
1251 static void stmmac_tx_timeout(struct net_device *dev)
1253 struct stmmac_priv *priv = netdev_priv(dev);
1255 /* Clear Tx resources and restart transmitting again */
1256 stmmac_tx_err(priv);
1259 /* Configuration changes (passed on by ifconfig) */
1260 static int stmmac_config(struct net_device *dev, struct ifmap *map)
1262 if (dev->flags & IFF_UP) /* can't act on a running interface */
1265 /* Don't allow changing the I/O address */
1266 if (map->base_addr != dev->base_addr) {
1267 pr_warning("%s: can't change I/O address\n", dev->name);
1271 /* Don't allow changing the IRQ */
1272 if (map->irq != dev->irq) {
1273 pr_warning("%s: can't change IRQ number %d\n",
1274 dev->name, dev->irq);
1278 /* ignore other fields */
1283 * stmmac_multicast_list - entry point for multicast addressing
1284 * @dev : pointer to the device structure
1286 * This function is a driver entry point which gets called by the kernel
1287 * whenever multicast addresses must be enabled/disabled.
1291 static void stmmac_multicast_list(struct net_device *dev)
1293 struct stmmac_priv *priv = netdev_priv(dev);
1295 spin_lock(&priv->lock);
1296 priv->hw->mac->set_filter(dev);
1297 spin_unlock(&priv->lock);
1301 * stmmac_change_mtu - entry point to change MTU size for the device.
1302 * @dev : device pointer.
1303 * @new_mtu : the new MTU size for the device.
1304 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1305 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1306 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1308 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1311 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1313 struct stmmac_priv *priv = netdev_priv(dev);
1316 if (netif_running(dev)) {
1317 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1321 if (priv->plat->has_gmac)
1322 max_mtu = JUMBO_LEN;
1324 max_mtu = ETH_DATA_LEN;
1326 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1327 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1332 netdev_update_features(dev);
1337 static u32 stmmac_fix_features(struct net_device *dev, u32 features)
1339 struct stmmac_priv *priv = netdev_priv(dev);
1342 features &= ~NETIF_F_RXCSUM;
1343 if (!priv->plat->tx_coe)
1344 features &= ~NETIF_F_ALL_CSUM;
1346 /* Some GMAC devices have a bugged Jumbo frame support that
1347 * needs to have the Tx COE disabled for oversized frames
1348 * (due to limited buffer sizes). In this case we disable
1349 * the TX csum insertionin the TDES and not use SF. */
1350 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1351 features &= ~NETIF_F_ALL_CSUM;
1356 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1358 struct net_device *dev = (struct net_device *)dev_id;
1359 struct stmmac_priv *priv = netdev_priv(dev);
1361 if (unlikely(!dev)) {
1362 pr_err("%s: invalid dev pointer\n", __func__);
1366 if (priv->plat->has_gmac)
1367 /* To handle GMAC own interrupts */
1368 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
1370 stmmac_dma_interrupt(priv);
1375 #ifdef CONFIG_NET_POLL_CONTROLLER
1376 /* Polling receive - used by NETCONSOLE and other diagnostic tools
1377 * to allow network I/O with interrupts disabled. */
1378 static void stmmac_poll_controller(struct net_device *dev)
1380 disable_irq(dev->irq);
1381 stmmac_interrupt(dev->irq, dev);
1382 enable_irq(dev->irq);
1387 * stmmac_ioctl - Entry point for the Ioctl
1388 * @dev: Device pointer.
1389 * @rq: An IOCTL specefic structure, that can contain a pointer to
1390 * a proprietary structure used to pass information to the driver.
1391 * @cmd: IOCTL command
1393 * Currently there are no special functionality supported in IOCTL, just the
1394 * phy_mii_ioctl(...) can be invoked.
1396 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1398 struct stmmac_priv *priv = netdev_priv(dev);
1401 if (!netif_running(dev))
1407 spin_lock(&priv->lock);
1408 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1409 spin_unlock(&priv->lock);
1414 #ifdef STMMAC_VLAN_TAG_USED
1415 static void stmmac_vlan_rx_register(struct net_device *dev,
1416 struct vlan_group *grp)
1418 struct stmmac_priv *priv = netdev_priv(dev);
1420 DBG(probe, INFO, "%s: Setting vlgrp to %p\n", dev->name, grp);
1422 spin_lock(&priv->lock);
1424 spin_unlock(&priv->lock);
1428 static const struct net_device_ops stmmac_netdev_ops = {
1429 .ndo_open = stmmac_open,
1430 .ndo_start_xmit = stmmac_xmit,
1431 .ndo_stop = stmmac_release,
1432 .ndo_change_mtu = stmmac_change_mtu,
1433 .ndo_fix_features = stmmac_fix_features,
1434 .ndo_set_multicast_list = stmmac_multicast_list,
1435 .ndo_tx_timeout = stmmac_tx_timeout,
1436 .ndo_do_ioctl = stmmac_ioctl,
1437 .ndo_set_config = stmmac_config,
1438 #ifdef STMMAC_VLAN_TAG_USED
1439 .ndo_vlan_rx_register = stmmac_vlan_rx_register,
1441 #ifdef CONFIG_NET_POLL_CONTROLLER
1442 .ndo_poll_controller = stmmac_poll_controller,
1444 .ndo_set_mac_address = eth_mac_addr,
1448 * stmmac_probe - Initialization of the adapter .
1449 * @dev : device pointer
1450 * Description: The function initializes the network device structure for
1451 * the STMMAC driver. It also calls the low level routines
1452 * in order to init the HW (i.e. the DMA engine)
1454 static int stmmac_probe(struct net_device *dev)
1457 struct stmmac_priv *priv = netdev_priv(dev);
1461 dev->netdev_ops = &stmmac_netdev_ops;
1462 stmmac_set_ethtool_ops(dev);
1464 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1465 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
1466 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1467 #ifdef STMMAC_VLAN_TAG_USED
1468 /* Both mac100 and gmac support receive VLAN tag detection */
1469 dev->features |= NETIF_F_HW_VLAN_RX;
1471 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1474 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1476 priv->pause = pause;
1477 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1479 /* Get the MAC address */
1480 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1483 if (!is_valid_ether_addr(dev->dev_addr))
1484 pr_warning("\tno valid MAC address;"
1485 "please, use ifconfig or nwhwconfig!\n");
1487 spin_lock_init(&priv->lock);
1489 ret = register_netdev(dev);
1491 pr_err("%s: ERROR %i registering the device\n",
1496 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1497 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
1498 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
1504 * stmmac_mac_device_setup
1505 * @dev : device pointer
1506 * Description: select and initialise the mac device (mac100 or Gmac).
1508 static int stmmac_mac_device_setup(struct net_device *dev)
1510 struct stmmac_priv *priv = netdev_priv(dev);
1512 struct mac_device_info *device;
1514 if (priv->plat->has_gmac)
1515 device = dwmac1000_setup(priv->ioaddr);
1517 device = dwmac100_setup(priv->ioaddr);
1522 if (priv->plat->enh_desc) {
1523 device->desc = &enh_desc_ops;
1524 pr_info("\tEnhanced descriptor structure\n");
1526 device->desc = &ndesc_ops;
1530 if (device_can_wakeup(priv->device)) {
1531 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
1532 enable_irq_wake(dev->irq);
1538 static int stmmacphy_dvr_probe(struct platform_device *pdev)
1540 struct plat_stmmacphy_data *plat_dat = pdev->dev.platform_data;
1542 pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
1548 static int stmmacphy_dvr_remove(struct platform_device *pdev)
1553 static struct platform_driver stmmacphy_driver = {
1555 .name = PHY_RESOURCE_NAME,
1557 .probe = stmmacphy_dvr_probe,
1558 .remove = stmmacphy_dvr_remove,
1562 * stmmac_associate_phy
1563 * @dev: pointer to device structure
1564 * @data: points to the private structure.
1565 * Description: Scans through all the PHYs we have registered and checks if
1566 * any are associated with our MAC. If so, then just fill in
1567 * the blanks in our local context structure
1569 static int stmmac_associate_phy(struct device *dev, void *data)
1571 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1572 struct plat_stmmacphy_data *plat_dat = dev->platform_data;
1574 DBG(probe, DEBUG, "%s: checking phy for bus %d\n", __func__,
1577 /* Check that this phy is for the MAC being initialised */
1578 if (priv->plat->bus_id != plat_dat->bus_id)
1581 /* OK, this PHY is connected to the MAC.
1582 Go ahead and get the parameters */
1583 DBG(probe, DEBUG, "%s: OK. Found PHY config\n", __func__);
1585 platform_get_irq_byname(to_platform_device(dev), "phyirq");
1586 DBG(probe, DEBUG, "%s: PHY irq on bus %d is %d\n", __func__,
1587 plat_dat->bus_id, priv->phy_irq);
1589 /* Override with kernel parameters if supplied XXX CRS XXX
1590 * this needs to have multiple instances */
1591 if ((phyaddr >= 0) && (phyaddr <= 31))
1592 plat_dat->phy_addr = phyaddr;
1594 priv->phy_addr = plat_dat->phy_addr;
1595 priv->phy_mask = plat_dat->phy_mask;
1596 priv->phy_interface = plat_dat->interface;
1597 priv->phy_reset = plat_dat->phy_reset;
1599 DBG(probe, DEBUG, "%s: exiting\n", __func__);
1600 return 1; /* forces exit of driver_for_each_device() */
1605 * @pdev: platform device pointer
1606 * Description: the driver is initialized through platform_device.
1608 static int stmmac_dvr_probe(struct platform_device *pdev)
1611 struct resource *res;
1612 void __iomem *addr = NULL;
1613 struct net_device *ndev = NULL;
1614 struct stmmac_priv *priv = NULL;
1615 struct plat_stmmacenet_data *plat_dat;
1617 pr_info("STMMAC driver:\n\tplatform registration... ");
1618 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1621 pr_info("\tdone!\n");
1623 if (!request_mem_region(res->start, resource_size(res),
1625 pr_err("%s: ERROR: memory allocation failed"
1626 "cannot get the I/O addr 0x%x\n",
1627 __func__, (unsigned int)res->start);
1631 addr = ioremap(res->start, resource_size(res));
1633 pr_err("%s: ERROR: memory mapping failed\n", __func__);
1635 goto out_release_region;
1638 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1640 pr_err("%s: ERROR: allocating the device\n", __func__);
1645 SET_NETDEV_DEV(ndev, &pdev->dev);
1647 /* Get the MAC information */
1648 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1649 if (ndev->irq == -ENXIO) {
1650 pr_err("%s: ERROR: MAC IRQ configuration "
1651 "information not found\n", __func__);
1656 priv = netdev_priv(ndev);
1657 priv->device = &(pdev->dev);
1659 plat_dat = pdev->dev.platform_data;
1661 priv->plat = plat_dat;
1663 priv->ioaddr = addr;
1665 /* PMT module is not integrated in all the MAC devices. */
1666 if (plat_dat->pmt) {
1667 pr_info("\tPMT module supported\n");
1668 device_set_wakeup_capable(&pdev->dev, 1);
1671 platform_set_drvdata(pdev, ndev);
1673 /* Set the I/O base addr */
1674 ndev->base_addr = (unsigned long)addr;
1676 /* Custom initialisation */
1677 if (priv->plat->init) {
1678 ret = priv->plat->init(pdev);
1683 /* MAC HW revice detection */
1684 ret = stmmac_mac_device_setup(ndev);
1688 /* Network Device Registration */
1689 ret = stmmac_probe(ndev);
1693 /* associate a PHY - it is provided by another platform bus */
1694 if (!driver_for_each_device
1695 (&(stmmacphy_driver.driver), NULL, (void *)priv,
1696 stmmac_associate_phy)) {
1697 pr_err("No PHY device is associated with this MAC!\n");
1699 goto out_unregister;
1702 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
1703 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1704 pdev->id, ndev->irq, addr);
1706 /* MDIO bus Registration */
1707 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
1708 ret = stmmac_mdio_register(ndev);
1710 goto out_unregister;
1711 pr_debug("registered!\n");
1715 unregister_netdev(ndev);
1717 if (priv->plat->exit)
1718 priv->plat->exit(pdev);
1721 platform_set_drvdata(pdev, NULL);
1725 release_mem_region(res->start, resource_size(res));
1732 * @pdev: platform device pointer
1733 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1734 * changes the link status, releases the DMA descriptor rings,
1735 * unregisters the MDIO bus and unmaps the allocated memory.
1737 static int stmmac_dvr_remove(struct platform_device *pdev)
1739 struct net_device *ndev = platform_get_drvdata(pdev);
1740 struct stmmac_priv *priv = netdev_priv(ndev);
1741 struct resource *res;
1743 pr_info("%s:\n\tremoving driver", __func__);
1745 priv->hw->dma->stop_rx(priv->ioaddr);
1746 priv->hw->dma->stop_tx(priv->ioaddr);
1748 stmmac_disable_mac(priv->ioaddr);
1750 netif_carrier_off(ndev);
1752 stmmac_mdio_unregister(ndev);
1754 if (priv->plat->exit)
1755 priv->plat->exit(pdev);
1757 platform_set_drvdata(pdev, NULL);
1758 unregister_netdev(ndev);
1760 iounmap((void *)priv->ioaddr);
1761 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1762 release_mem_region(res->start, resource_size(res));
1770 static int stmmac_suspend(struct device *dev)
1772 struct net_device *ndev = dev_get_drvdata(dev);
1773 struct stmmac_priv *priv = netdev_priv(ndev);
1776 if (!ndev || !netif_running(ndev))
1779 spin_lock(&priv->lock);
1781 netif_device_detach(ndev);
1782 netif_stop_queue(ndev);
1784 phy_stop(priv->phydev);
1786 #ifdef CONFIG_STMMAC_TIMER
1787 priv->tm->timer_stop();
1788 if (likely(priv->tm->enable))
1791 napi_disable(&priv->napi);
1793 /* Stop TX/RX DMA */
1794 priv->hw->dma->stop_tx(priv->ioaddr);
1795 priv->hw->dma->stop_rx(priv->ioaddr);
1796 /* Clear the Rx/Tx descriptors */
1797 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1799 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
1801 /* Enable Power down mode by programming the PMT regs */
1802 if (device_may_wakeup(priv->device))
1803 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
1805 stmmac_disable_mac(priv->ioaddr);
1807 spin_unlock(&priv->lock);
1811 static int stmmac_resume(struct device *dev)
1813 struct net_device *ndev = dev_get_drvdata(dev);
1814 struct stmmac_priv *priv = netdev_priv(ndev);
1816 if (!netif_running(ndev))
1819 spin_lock(&priv->lock);
1821 /* Power Down bit, into the PM register, is cleared
1822 * automatically as soon as a magic packet or a Wake-up frame
1823 * is received. Anyway, it's better to manually clear
1824 * this bit because it can generate problems while resuming
1825 * from another devices (e.g. serial console). */
1826 if (device_may_wakeup(priv->device))
1827 priv->hw->mac->pmt(priv->ioaddr, 0);
1829 netif_device_attach(ndev);
1831 /* Enable the MAC and DMA */
1832 stmmac_enable_mac(priv->ioaddr);
1833 priv->hw->dma->start_tx(priv->ioaddr);
1834 priv->hw->dma->start_rx(priv->ioaddr);
1836 #ifdef CONFIG_STMMAC_TIMER
1837 if (likely(priv->tm->enable))
1838 priv->tm->timer_start(tmrate);
1840 napi_enable(&priv->napi);
1843 phy_start(priv->phydev);
1845 netif_start_queue(ndev);
1847 spin_unlock(&priv->lock);
1851 static int stmmac_freeze(struct device *dev)
1853 struct net_device *ndev = dev_get_drvdata(dev);
1855 if (!ndev || !netif_running(ndev))
1858 return stmmac_release(ndev);
1861 static int stmmac_restore(struct device *dev)
1863 struct net_device *ndev = dev_get_drvdata(dev);
1865 if (!ndev || !netif_running(ndev))
1868 return stmmac_open(ndev);
1871 static const struct dev_pm_ops stmmac_pm_ops = {
1872 .suspend = stmmac_suspend,
1873 .resume = stmmac_resume,
1874 .freeze = stmmac_freeze,
1875 .thaw = stmmac_restore,
1876 .restore = stmmac_restore,
1879 static const struct dev_pm_ops stmmac_pm_ops;
1880 #endif /* CONFIG_PM */
1882 static struct platform_driver stmmac_driver = {
1883 .probe = stmmac_dvr_probe,
1884 .remove = stmmac_dvr_remove,
1886 .name = STMMAC_RESOURCE_NAME,
1887 .owner = THIS_MODULE,
1888 .pm = &stmmac_pm_ops,
1893 * stmmac_init_module - Entry point for the driver
1894 * Description: This function is the entry point for the driver.
1896 static int __init stmmac_init_module(void)
1900 if (platform_driver_register(&stmmacphy_driver)) {
1901 pr_err("No PHY devices registered!\n");
1905 ret = platform_driver_register(&stmmac_driver);
1910 * stmmac_cleanup_module - Cleanup routine for the driver
1911 * Description: This function is the cleanup routine for the driver.
1913 static void __exit stmmac_cleanup_module(void)
1915 platform_driver_unregister(&stmmacphy_driver);
1916 platform_driver_unregister(&stmmac_driver);
1920 static int __init stmmac_cmdline_opt(char *str)
1926 while ((opt = strsep(&str, ",")) != NULL) {
1927 if (!strncmp(opt, "debug:", 6))
1928 strict_strtoul(opt + 6, 0, (unsigned long *)&debug);
1929 else if (!strncmp(opt, "phyaddr:", 8))
1930 strict_strtoul(opt + 8, 0, (unsigned long *)&phyaddr);
1931 else if (!strncmp(opt, "dma_txsize:", 11))
1932 strict_strtoul(opt + 11, 0,
1933 (unsigned long *)&dma_txsize);
1934 else if (!strncmp(opt, "dma_rxsize:", 11))
1935 strict_strtoul(opt + 11, 0,
1936 (unsigned long *)&dma_rxsize);
1937 else if (!strncmp(opt, "buf_sz:", 7))
1938 strict_strtoul(opt + 7, 0, (unsigned long *)&buf_sz);
1939 else if (!strncmp(opt, "tc:", 3))
1940 strict_strtoul(opt + 3, 0, (unsigned long *)&tc);
1941 else if (!strncmp(opt, "watchdog:", 9))
1942 strict_strtoul(opt + 9, 0, (unsigned long *)&watchdog);
1943 else if (!strncmp(opt, "flow_ctrl:", 10))
1944 strict_strtoul(opt + 10, 0,
1945 (unsigned long *)&flow_ctrl);
1946 else if (!strncmp(opt, "pause:", 6))
1947 strict_strtoul(opt + 6, 0, (unsigned long *)&pause);
1948 #ifdef CONFIG_STMMAC_TIMER
1949 else if (!strncmp(opt, "tmrate:", 7))
1950 strict_strtoul(opt + 7, 0, (unsigned long *)&tmrate);
1956 __setup("stmmaceth=", stmmac_cmdline_opt);
1959 module_init(stmmac_init_module);
1960 module_exit(stmmac_cleanup_module);
1962 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
1963 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
1964 MODULE_LICENSE("GPL");