stmmac: support wake up irq from external sources (v3)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / ethernet / stmicro / stmmac / stmmac_main.c
blob5aea21e587dd3ebe9aaff37fd8d30da413b29572
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
14 more details.
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
27 Support available at:
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>
37 #include <linux/ip.h>
38 #include <linux/tcp.h>
39 #include <linux/skbuff.h>
40 #include <linux/ethtool.h>
41 #include <linux/if_ether.h>
42 #include <linux/crc32.h>
43 #include <linux/mii.h>
44 #include <linux/phy.h>
45 #include <linux/if.h>
46 #include <linux/if_vlan.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/slab.h>
49 #include <linux/prefetch.h>
50 #include "stmmac.h"
52 #define STMMAC_RESOURCE_NAME "stmmaceth"
54 #undef STMMAC_DEBUG
55 /*#define STMMAC_DEBUG*/
56 #ifdef STMMAC_DEBUG
57 #define DBG(nlevel, klevel, fmt, args...) \
58 ((void)(netif_msg_##nlevel(priv) && \
59 printk(KERN_##klevel fmt, ## args)))
60 #else
61 #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
62 #endif
64 #undef STMMAC_RX_DEBUG
65 /*#define STMMAC_RX_DEBUG*/
66 #ifdef STMMAC_RX_DEBUG
67 #define RX_DBG(fmt, args...) printk(fmt, ## args)
68 #else
69 #define RX_DBG(fmt, args...) do { } while (0)
70 #endif
72 #undef STMMAC_XMIT_DEBUG
73 /*#define STMMAC_XMIT_DEBUG*/
74 #ifdef STMMAC_TX_DEBUG
75 #define TX_DBG(fmt, args...) printk(fmt, ## args)
76 #else
77 #define TX_DBG(fmt, args...) do { } while (0)
78 #endif
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)");
128 #endif
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))
149 watchdog = TX_TIMEO;
150 if (unlikely(dma_rxsize < 0))
151 dma_rxsize = DMA_RX_SIZE;
152 if (unlikely(dma_txsize < 0))
153 dma_txsize = DMA_TX_SIZE;
154 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
155 buf_sz = DMA_BUFFER_SIZE;
156 if (unlikely(flow_ctrl > 1))
157 flow_ctrl = FLOW_AUTO;
158 else if (likely(flow_ctrl < 0))
159 flow_ctrl = FLOW_OFF;
160 if (unlikely((pause < 0) || (pause > 0xffff)))
161 pause = PAUSE_TIME;
164 #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
165 static void print_pkt(unsigned char *buf, int len)
167 int j;
168 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
169 for (j = 0; j < len; j++) {
170 if ((j % 16) == 0)
171 pr_info("\n %03x:", j);
172 pr_info(" %02x", buf[j]);
174 pr_info("\n");
176 #endif
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,
195 phydev->speed);
199 * stmmac_adjust_link
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;
207 unsigned long flags;
208 int new_state = 0;
209 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
211 if (phydev == NULL)
212 return;
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);
218 if (phydev->link) {
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) {
224 new_state = 1;
225 if (!(phydev->duplex))
226 ctrl &= ~priv->hw->link.duplex;
227 else
228 ctrl |= priv->hw->link.duplex;
229 priv->oldduplex = phydev->duplex;
231 /* Flow Control operation */
232 if (phydev->pause)
233 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
234 fc, pause_time);
236 if (phydev->speed != priv->speed) {
237 new_state = 1;
238 switch (phydev->speed) {
239 case 1000:
240 if (likely(priv->plat->has_gmac))
241 ctrl &= ~priv->hw->link.port;
242 stmmac_hw_fix_mac_speed(priv);
243 break;
244 case 100:
245 case 10:
246 if (priv->plat->has_gmac) {
247 ctrl |= priv->hw->link.port;
248 if (phydev->speed == SPEED_100) {
249 ctrl |= priv->hw->link.speed;
250 } else {
251 ctrl &= ~(priv->hw->link.speed);
253 } else {
254 ctrl &= ~priv->hw->link.port;
256 stmmac_hw_fix_mac_speed(priv);
257 break;
258 default:
259 if (netif_msg_link(priv))
260 pr_warning("%s: Speed (%d) is not 10"
261 " or 100!\n", dev->name, phydev->speed);
262 break;
265 priv->speed = phydev->speed;
268 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
270 if (!priv->oldlink) {
271 new_state = 1;
272 priv->oldlink = 1;
274 } else if (priv->oldlink) {
275 new_state = 1;
276 priv->oldlink = 0;
277 priv->speed = 0;
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
293 * to the mac driver.
294 * Return value:
295 * 0 on success
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];
304 priv->oldlink = 0;
305 priv->speed = 0;
306 priv->oldduplex = -1;
308 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
309 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
310 priv->plat->phy_addr);
311 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
313 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
314 priv->plat->interface);
316 if (IS_ERR(phydev)) {
317 pr_err("%s: Could not attach to PHY\n", dev->name);
318 return PTR_ERR(phydev);
322 * Broken HW is sometimes missing the pull-up resistor on the
323 * MDIO line, which results in reads to non-existent devices returning
324 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
325 * device as well.
326 * Note: phydev->phy_id is the result of reading the UID PHY registers.
328 if (phydev->phy_id == 0) {
329 phy_disconnect(phydev);
330 return -ENODEV;
332 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
333 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
335 priv->phydev = phydev;
337 return 0;
340 static inline void stmmac_enable_mac(void __iomem *ioaddr)
342 u32 value = readl(ioaddr + MAC_CTRL_REG);
344 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
345 writel(value, ioaddr + MAC_CTRL_REG);
348 static inline void stmmac_disable_mac(void __iomem *ioaddr)
350 u32 value = readl(ioaddr + MAC_CTRL_REG);
352 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
353 writel(value, ioaddr + MAC_CTRL_REG);
357 * display_ring
358 * @p: pointer to the ring.
359 * @size: size of the ring.
360 * Description: display all the descriptors within the ring.
362 static void display_ring(struct dma_desc *p, int size)
364 struct tmp_s {
365 u64 a;
366 unsigned int b;
367 unsigned int c;
369 int i;
370 for (i = 0; i < size; i++) {
371 struct tmp_s *x = (struct tmp_s *)(p + i);
372 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
373 i, (unsigned int)virt_to_phys(&p[i]),
374 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
375 x->b, x->c);
376 pr_info("\n");
381 * init_dma_desc_rings - init the RX/TX descriptor rings
382 * @dev: net device structure
383 * Description: this function initializes the DMA RX/TX descriptors
384 * and allocates the socket buffers.
386 static void init_dma_desc_rings(struct net_device *dev)
388 int i;
389 struct stmmac_priv *priv = netdev_priv(dev);
390 struct sk_buff *skb;
391 unsigned int txsize = priv->dma_tx_size;
392 unsigned int rxsize = priv->dma_rx_size;
393 unsigned int bfsize = priv->dma_buf_sz;
394 int buff2_needed = 0, dis_ic = 0;
396 /* Set the Buffer size according to the MTU;
397 * indeed, in case of jumbo we need to bump-up the buffer sizes.
399 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
400 bfsize = BUF_SIZE_16KiB;
401 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
402 bfsize = BUF_SIZE_8KiB;
403 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
404 bfsize = BUF_SIZE_4KiB;
405 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
406 bfsize = BUF_SIZE_2KiB;
407 else
408 bfsize = DMA_BUFFER_SIZE;
410 #ifdef CONFIG_STMMAC_TIMER
411 /* Disable interrupts on completion for the reception if timer is on */
412 if (likely(priv->tm->enable))
413 dis_ic = 1;
414 #endif
415 /* If the MTU exceeds 8k so use the second buffer in the chain */
416 if (bfsize >= BUF_SIZE_8KiB)
417 buff2_needed = 1;
419 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
420 txsize, rxsize, bfsize);
422 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
423 priv->rx_skbuff =
424 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
425 priv->dma_rx =
426 (struct dma_desc *)dma_alloc_coherent(priv->device,
427 rxsize *
428 sizeof(struct dma_desc),
429 &priv->dma_rx_phy,
430 GFP_KERNEL);
431 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
432 GFP_KERNEL);
433 priv->dma_tx =
434 (struct dma_desc *)dma_alloc_coherent(priv->device,
435 txsize *
436 sizeof(struct dma_desc),
437 &priv->dma_tx_phy,
438 GFP_KERNEL);
440 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
441 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
442 return;
445 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
446 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
447 dev->name, priv->dma_rx, priv->dma_tx,
448 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
450 /* RX INITIALIZATION */
451 DBG(probe, INFO, "stmmac: SKB addresses:\n"
452 "skb\t\tskb data\tdma data\n");
454 for (i = 0; i < rxsize; i++) {
455 struct dma_desc *p = priv->dma_rx + i;
457 skb = netdev_alloc_skb_ip_align(dev, bfsize);
458 if (unlikely(skb == NULL)) {
459 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
460 break;
462 priv->rx_skbuff[i] = skb;
463 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
464 bfsize, DMA_FROM_DEVICE);
466 p->des2 = priv->rx_skbuff_dma[i];
467 if (unlikely(buff2_needed))
468 p->des3 = p->des2 + BUF_SIZE_8KiB;
469 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
470 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
472 priv->cur_rx = 0;
473 priv->dirty_rx = (unsigned int)(i - rxsize);
474 priv->dma_buf_sz = bfsize;
475 buf_sz = bfsize;
477 /* TX INITIALIZATION */
478 for (i = 0; i < txsize; i++) {
479 priv->tx_skbuff[i] = NULL;
480 priv->dma_tx[i].des2 = 0;
482 priv->dirty_tx = 0;
483 priv->cur_tx = 0;
485 /* Clear the Rx/Tx descriptors */
486 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
487 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
489 if (netif_msg_hw(priv)) {
490 pr_info("RX descriptor ring:\n");
491 display_ring(priv->dma_rx, rxsize);
492 pr_info("TX descriptor ring:\n");
493 display_ring(priv->dma_tx, txsize);
497 static void dma_free_rx_skbufs(struct stmmac_priv *priv)
499 int i;
501 for (i = 0; i < priv->dma_rx_size; i++) {
502 if (priv->rx_skbuff[i]) {
503 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
504 priv->dma_buf_sz, DMA_FROM_DEVICE);
505 dev_kfree_skb_any(priv->rx_skbuff[i]);
507 priv->rx_skbuff[i] = NULL;
511 static void dma_free_tx_skbufs(struct stmmac_priv *priv)
513 int i;
515 for (i = 0; i < priv->dma_tx_size; i++) {
516 if (priv->tx_skbuff[i] != NULL) {
517 struct dma_desc *p = priv->dma_tx + i;
518 if (p->des2)
519 dma_unmap_single(priv->device, p->des2,
520 priv->hw->desc->get_tx_len(p),
521 DMA_TO_DEVICE);
522 dev_kfree_skb_any(priv->tx_skbuff[i]);
523 priv->tx_skbuff[i] = NULL;
528 static void free_dma_desc_resources(struct stmmac_priv *priv)
530 /* Release the DMA TX/RX socket buffers */
531 dma_free_rx_skbufs(priv);
532 dma_free_tx_skbufs(priv);
534 /* Free the region of consistent memory previously allocated for
535 * the DMA */
536 dma_free_coherent(priv->device,
537 priv->dma_tx_size * sizeof(struct dma_desc),
538 priv->dma_tx, priv->dma_tx_phy);
539 dma_free_coherent(priv->device,
540 priv->dma_rx_size * sizeof(struct dma_desc),
541 priv->dma_rx, priv->dma_rx_phy);
542 kfree(priv->rx_skbuff_dma);
543 kfree(priv->rx_skbuff);
544 kfree(priv->tx_skbuff);
548 * stmmac_dma_operation_mode - HW DMA operation mode
549 * @priv : pointer to the private device structure.
550 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
551 * or Store-And-Forward capability.
553 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
555 if (likely(priv->plat->force_sf_dma_mode ||
556 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
558 * In case of GMAC, SF mode can be enabled
559 * to perform the TX COE in HW. This depends on:
560 * 1) TX COE if actually supported
561 * 2) There is no bugged Jumbo frame support
562 * that needs to not insert csum in the TDES.
564 priv->hw->dma->dma_mode(priv->ioaddr,
565 SF_DMA_MODE, SF_DMA_MODE);
566 tc = SF_DMA_MODE;
567 } else
568 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
572 * stmmac_tx:
573 * @priv: private driver structure
574 * Description: it reclaims resources after transmission completes.
576 static void stmmac_tx(struct stmmac_priv *priv)
578 unsigned int txsize = priv->dma_tx_size;
580 while (priv->dirty_tx != priv->cur_tx) {
581 int last;
582 unsigned int entry = priv->dirty_tx % txsize;
583 struct sk_buff *skb = priv->tx_skbuff[entry];
584 struct dma_desc *p = priv->dma_tx + entry;
586 /* Check if the descriptor is owned by the DMA. */
587 if (priv->hw->desc->get_tx_owner(p))
588 break;
590 /* Verify tx error by looking at the last segment */
591 last = priv->hw->desc->get_tx_ls(p);
592 if (likely(last)) {
593 int tx_error =
594 priv->hw->desc->tx_status(&priv->dev->stats,
595 &priv->xstats, p,
596 priv->ioaddr);
597 if (likely(tx_error == 0)) {
598 priv->dev->stats.tx_packets++;
599 priv->xstats.tx_pkt_n++;
600 } else
601 priv->dev->stats.tx_errors++;
603 TX_DBG("%s: curr %d, dirty %d\n", __func__,
604 priv->cur_tx, priv->dirty_tx);
606 if (likely(p->des2))
607 dma_unmap_single(priv->device, p->des2,
608 priv->hw->desc->get_tx_len(p),
609 DMA_TO_DEVICE);
610 if (unlikely(p->des3))
611 p->des3 = 0;
613 if (likely(skb != NULL)) {
615 * If there's room in the queue (limit it to size)
616 * we add this skb back into the pool,
617 * if it's the right size.
619 if ((skb_queue_len(&priv->rx_recycle) <
620 priv->dma_rx_size) &&
621 skb_recycle_check(skb, priv->dma_buf_sz))
622 __skb_queue_head(&priv->rx_recycle, skb);
623 else
624 dev_kfree_skb(skb);
626 priv->tx_skbuff[entry] = NULL;
629 priv->hw->desc->release_tx_desc(p);
631 entry = (++priv->dirty_tx) % txsize;
633 if (unlikely(netif_queue_stopped(priv->dev) &&
634 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
635 netif_tx_lock(priv->dev);
636 if (netif_queue_stopped(priv->dev) &&
637 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
638 TX_DBG("%s: restart transmit\n", __func__);
639 netif_wake_queue(priv->dev);
641 netif_tx_unlock(priv->dev);
645 static inline void stmmac_enable_irq(struct stmmac_priv *priv)
647 #ifdef CONFIG_STMMAC_TIMER
648 if (likely(priv->tm->enable))
649 priv->tm->timer_start(tmrate);
650 else
651 #endif
652 priv->hw->dma->enable_dma_irq(priv->ioaddr);
655 static inline void stmmac_disable_irq(struct stmmac_priv *priv)
657 #ifdef CONFIG_STMMAC_TIMER
658 if (likely(priv->tm->enable))
659 priv->tm->timer_stop();
660 else
661 #endif
662 priv->hw->dma->disable_dma_irq(priv->ioaddr);
665 static int stmmac_has_work(struct stmmac_priv *priv)
667 unsigned int has_work = 0;
668 int rxret, tx_work = 0;
670 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
671 (priv->cur_rx % priv->dma_rx_size));
673 if (priv->dirty_tx != priv->cur_tx)
674 tx_work = 1;
676 if (likely(!rxret || tx_work))
677 has_work = 1;
679 return has_work;
682 static inline void _stmmac_schedule(struct stmmac_priv *priv)
684 if (likely(stmmac_has_work(priv))) {
685 stmmac_disable_irq(priv);
686 napi_schedule(&priv->napi);
690 #ifdef CONFIG_STMMAC_TIMER
691 void stmmac_schedule(struct net_device *dev)
693 struct stmmac_priv *priv = netdev_priv(dev);
695 priv->xstats.sched_timer_n++;
697 _stmmac_schedule(priv);
700 static void stmmac_no_timer_started(unsigned int x)
704 static void stmmac_no_timer_stopped(void)
707 #endif
710 * stmmac_tx_err:
711 * @priv: pointer to the private device structure
712 * Description: it cleans the descriptors and restarts the transmission
713 * in case of errors.
715 static void stmmac_tx_err(struct stmmac_priv *priv)
718 netif_stop_queue(priv->dev);
720 priv->hw->dma->stop_tx(priv->ioaddr);
721 dma_free_tx_skbufs(priv);
722 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
723 priv->dirty_tx = 0;
724 priv->cur_tx = 0;
725 priv->hw->dma->start_tx(priv->ioaddr);
727 priv->dev->stats.tx_errors++;
728 netif_wake_queue(priv->dev);
732 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
734 int status;
736 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
737 if (likely(status == handle_tx_rx))
738 _stmmac_schedule(priv);
740 else if (unlikely(status == tx_hard_error_bump_tc)) {
741 /* Try to bump up the dma threshold on this failure */
742 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
743 tc += 64;
744 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
745 priv->xstats.threshold = tc;
747 } else if (unlikely(status == tx_hard_error))
748 stmmac_tx_err(priv);
752 * stmmac_open - open entry point of the driver
753 * @dev : pointer to the device structure.
754 * Description:
755 * This function is the open entry point of the driver.
756 * Return value:
757 * 0 on success and an appropriate (-)ve integer as defined in errno.h
758 * file on failure.
760 static int stmmac_open(struct net_device *dev)
762 struct stmmac_priv *priv = netdev_priv(dev);
763 int ret;
765 /* Check that the MAC address is valid. If its not, refuse
766 * to bring the device up. The user must specify an
767 * address using the following linux command:
768 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
769 if (!is_valid_ether_addr(dev->dev_addr)) {
770 random_ether_addr(dev->dev_addr);
771 pr_warning("%s: generated random MAC address %pM\n", dev->name,
772 dev->dev_addr);
775 stmmac_verify_args();
777 #ifdef CONFIG_STMMAC_TIMER
778 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
779 if (unlikely(priv->tm == NULL)) {
780 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
781 return -ENOMEM;
783 priv->tm->freq = tmrate;
785 /* Test if the external timer can be actually used.
786 * In case of failure continue without timer. */
787 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
788 pr_warning("stmmaceth: cannot attach the external timer.\n");
789 priv->tm->freq = 0;
790 priv->tm->timer_start = stmmac_no_timer_started;
791 priv->tm->timer_stop = stmmac_no_timer_stopped;
792 } else
793 priv->tm->enable = 1;
794 #endif
795 ret = stmmac_init_phy(dev);
796 if (unlikely(ret)) {
797 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
798 goto open_error;
801 /* Create and initialize the TX/RX descriptors chains. */
802 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
803 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
804 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
805 init_dma_desc_rings(dev);
807 /* DMA initialization and SW reset */
808 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
809 priv->dma_tx_phy, priv->dma_rx_phy);
810 if (ret < 0) {
811 pr_err("%s: DMA initialization failed\n", __func__);
812 goto open_error;
815 /* Copy the MAC addr into the HW */
816 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
817 /* If required, perform hw setup of the bus. */
818 if (priv->plat->bus_setup)
819 priv->plat->bus_setup(priv->ioaddr);
820 /* Initialize the MAC Core */
821 priv->hw->mac->core_init(priv->ioaddr);
823 priv->rx_coe = priv->hw->mac->rx_coe(priv->ioaddr);
824 if (priv->rx_coe)
825 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
826 if (priv->plat->tx_coe)
827 pr_info("\tTX Checksum insertion supported\n");
828 netdev_update_features(dev);
830 /* Request the IRQ lines */
831 ret = request_irq(dev->irq, stmmac_interrupt,
832 IRQF_SHARED, dev->name, dev);
833 if (unlikely(ret < 0)) {
834 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
835 __func__, dev->irq, ret);
836 goto open_error;
839 /* Enable the MAC Rx/Tx */
840 stmmac_enable_mac(priv->ioaddr);
842 /* Set the HW DMA mode and the COE */
843 stmmac_dma_operation_mode(priv);
845 /* Extra statistics */
846 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
847 priv->xstats.threshold = tc;
849 /* Start the ball rolling... */
850 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
851 priv->hw->dma->start_tx(priv->ioaddr);
852 priv->hw->dma->start_rx(priv->ioaddr);
854 #ifdef CONFIG_STMMAC_TIMER
855 priv->tm->timer_start(tmrate);
856 #endif
857 /* Dump DMA/MAC registers */
858 if (netif_msg_hw(priv)) {
859 priv->hw->mac->dump_regs(priv->ioaddr);
860 priv->hw->dma->dump_regs(priv->ioaddr);
863 if (priv->phydev)
864 phy_start(priv->phydev);
866 napi_enable(&priv->napi);
867 skb_queue_head_init(&priv->rx_recycle);
868 netif_start_queue(dev);
870 return 0;
872 open_error:
873 #ifdef CONFIG_STMMAC_TIMER
874 kfree(priv->tm);
875 #endif
876 if (priv->phydev)
877 phy_disconnect(priv->phydev);
879 return ret;
883 * stmmac_release - close entry point of the driver
884 * @dev : device pointer.
885 * Description:
886 * This is the stop entry point of the driver.
888 static int stmmac_release(struct net_device *dev)
890 struct stmmac_priv *priv = netdev_priv(dev);
892 /* Stop and disconnect the PHY */
893 if (priv->phydev) {
894 phy_stop(priv->phydev);
895 phy_disconnect(priv->phydev);
896 priv->phydev = NULL;
899 netif_stop_queue(dev);
901 #ifdef CONFIG_STMMAC_TIMER
902 /* Stop and release the timer */
903 stmmac_close_ext_timer();
904 if (priv->tm != NULL)
905 kfree(priv->tm);
906 #endif
907 napi_disable(&priv->napi);
908 skb_queue_purge(&priv->rx_recycle);
910 /* Free the IRQ lines */
911 free_irq(dev->irq, dev);
913 /* Stop TX/RX DMA and clear the descriptors */
914 priv->hw->dma->stop_tx(priv->ioaddr);
915 priv->hw->dma->stop_rx(priv->ioaddr);
917 /* Release and free the Rx/Tx resources */
918 free_dma_desc_resources(priv);
920 /* Disable the MAC Rx/Tx */
921 stmmac_disable_mac(priv->ioaddr);
923 netif_carrier_off(dev);
925 return 0;
928 static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
929 struct net_device *dev,
930 int csum_insertion)
932 struct stmmac_priv *priv = netdev_priv(dev);
933 unsigned int nopaged_len = skb_headlen(skb);
934 unsigned int txsize = priv->dma_tx_size;
935 unsigned int entry = priv->cur_tx % txsize;
936 struct dma_desc *desc = priv->dma_tx + entry;
938 if (nopaged_len > BUF_SIZE_8KiB) {
940 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
942 desc->des2 = dma_map_single(priv->device, skb->data,
943 BUF_SIZE_8KiB, DMA_TO_DEVICE);
944 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
945 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
946 csum_insertion);
948 entry = (++priv->cur_tx) % txsize;
949 desc = priv->dma_tx + entry;
951 desc->des2 = dma_map_single(priv->device,
952 skb->data + BUF_SIZE_8KiB,
953 buf2_size, DMA_TO_DEVICE);
954 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
955 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
956 csum_insertion);
957 priv->hw->desc->set_tx_owner(desc);
958 priv->tx_skbuff[entry] = NULL;
959 } else {
960 desc->des2 = dma_map_single(priv->device, skb->data,
961 nopaged_len, DMA_TO_DEVICE);
962 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
963 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
964 csum_insertion);
966 return entry;
970 * stmmac_xmit:
971 * @skb : the socket buffer
972 * @dev : device pointer
973 * Description : Tx entry point of the driver.
975 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
977 struct stmmac_priv *priv = netdev_priv(dev);
978 unsigned int txsize = priv->dma_tx_size;
979 unsigned int entry;
980 int i, csum_insertion = 0;
981 int nfrags = skb_shinfo(skb)->nr_frags;
982 struct dma_desc *desc, *first;
984 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
985 if (!netif_queue_stopped(dev)) {
986 netif_stop_queue(dev);
987 /* This is a hard error, log it. */
988 pr_err("%s: BUG! Tx Ring full when queue awake\n",
989 __func__);
991 return NETDEV_TX_BUSY;
994 entry = priv->cur_tx % txsize;
996 #ifdef STMMAC_XMIT_DEBUG
997 if ((skb->len > ETH_FRAME_LEN) || nfrags)
998 pr_info("stmmac xmit:\n"
999 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1000 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1001 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1002 !skb_is_gso(skb) ? "isn't" : "is");
1003 #endif
1005 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
1007 desc = priv->dma_tx + entry;
1008 first = desc;
1010 #ifdef STMMAC_XMIT_DEBUG
1011 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1012 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1013 "\t\tn_frags: %d, ip_summed: %d\n",
1014 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1015 #endif
1016 priv->tx_skbuff[entry] = skb;
1017 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1018 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1019 desc = priv->dma_tx + entry;
1020 } else {
1021 unsigned int nopaged_len = skb_headlen(skb);
1022 desc->des2 = dma_map_single(priv->device, skb->data,
1023 nopaged_len, DMA_TO_DEVICE);
1024 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1025 csum_insertion);
1028 for (i = 0; i < nfrags; i++) {
1029 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1030 int len = frag->size;
1032 entry = (++priv->cur_tx) % txsize;
1033 desc = priv->dma_tx + entry;
1035 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1036 desc->des2 = dma_map_page(priv->device, frag->page,
1037 frag->page_offset,
1038 len, DMA_TO_DEVICE);
1039 priv->tx_skbuff[entry] = NULL;
1040 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
1041 wmb();
1042 priv->hw->desc->set_tx_owner(desc);
1045 /* Interrupt on completition only for the latest segment */
1046 priv->hw->desc->close_tx_desc(desc);
1048 #ifdef CONFIG_STMMAC_TIMER
1049 /* Clean IC while using timer */
1050 if (likely(priv->tm->enable))
1051 priv->hw->desc->clear_tx_ic(desc);
1052 #endif
1054 wmb();
1056 /* To avoid raise condition */
1057 priv->hw->desc->set_tx_owner(first);
1059 priv->cur_tx++;
1061 #ifdef STMMAC_XMIT_DEBUG
1062 if (netif_msg_pktdata(priv)) {
1063 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1064 "first=%p, nfrags=%d\n",
1065 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1066 entry, first, nfrags);
1067 display_ring(priv->dma_tx, txsize);
1068 pr_info(">>> frame to be transmitted: ");
1069 print_pkt(skb->data, skb->len);
1071 #endif
1072 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1073 TX_DBG("%s: stop transmitted packets\n", __func__);
1074 netif_stop_queue(dev);
1077 dev->stats.tx_bytes += skb->len;
1079 skb_tx_timestamp(skb);
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);
1098 if (skb == NULL)
1099 skb = netdev_alloc_skb_ip_align(priv->dev,
1100 bfsize);
1102 if (unlikely(skb == NULL))
1103 break;
1105 priv->rx_skbuff[entry] = skb;
1106 priv->rx_skbuff_dma[entry] =
1107 dma_map_single(priv->device, skb->data, bfsize,
1108 DMA_FROM_DEVICE);
1110 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
1111 if (unlikely(priv->plat->has_gmac)) {
1112 if (bfsize >= BUF_SIZE_8KiB)
1113 (p + entry)->des3 =
1114 (p + entry)->des2 + BUF_SIZE_8KiB;
1116 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1118 wmb();
1119 priv->hw->desc->set_rx_owner(p + entry);
1123 static int stmmac_rx(struct stmmac_priv *priv, int limit)
1125 unsigned int rxsize = priv->dma_rx_size;
1126 unsigned int entry = priv->cur_rx % rxsize;
1127 unsigned int next_entry;
1128 unsigned int count = 0;
1129 struct dma_desc *p = priv->dma_rx + entry;
1130 struct dma_desc *p_next;
1132 #ifdef STMMAC_RX_DEBUG
1133 if (netif_msg_hw(priv)) {
1134 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1135 display_ring(priv->dma_rx, rxsize);
1137 #endif
1138 count = 0;
1139 while (!priv->hw->desc->get_rx_owner(p)) {
1140 int status;
1142 if (count >= limit)
1143 break;
1145 count++;
1147 next_entry = (++priv->cur_rx) % rxsize;
1148 p_next = priv->dma_rx + next_entry;
1149 prefetch(p_next);
1151 /* read the status of the incoming frame */
1152 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1153 &priv->xstats, p));
1154 if (unlikely(status == discard_frame))
1155 priv->dev->stats.rx_errors++;
1156 else {
1157 struct sk_buff *skb;
1158 int frame_len;
1160 frame_len = priv->hw->desc->get_rx_frame_len(p);
1161 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1162 * Type frames (LLC/LLC-SNAP) */
1163 if (unlikely(status != llc_snap))
1164 frame_len -= ETH_FCS_LEN;
1165 #ifdef STMMAC_RX_DEBUG
1166 if (frame_len > ETH_FRAME_LEN)
1167 pr_debug("\tRX frame size %d, COE status: %d\n",
1168 frame_len, status);
1170 if (netif_msg_hw(priv))
1171 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1172 p, entry, p->des2);
1173 #endif
1174 skb = priv->rx_skbuff[entry];
1175 if (unlikely(!skb)) {
1176 pr_err("%s: Inconsistent Rx descriptor chain\n",
1177 priv->dev->name);
1178 priv->dev->stats.rx_dropped++;
1179 break;
1181 prefetch(skb->data - NET_IP_ALIGN);
1182 priv->rx_skbuff[entry] = NULL;
1184 skb_put(skb, frame_len);
1185 dma_unmap_single(priv->device,
1186 priv->rx_skbuff_dma[entry],
1187 priv->dma_buf_sz, DMA_FROM_DEVICE);
1188 #ifdef STMMAC_RX_DEBUG
1189 if (netif_msg_pktdata(priv)) {
1190 pr_info(" frame received (%dbytes)", frame_len);
1191 print_pkt(skb->data, frame_len);
1193 #endif
1194 skb->protocol = eth_type_trans(skb, priv->dev);
1196 if (unlikely(status == csum_none)) {
1197 /* always for the old mac 10/100 */
1198 skb_checksum_none_assert(skb);
1199 netif_receive_skb(skb);
1200 } else {
1201 skb->ip_summed = CHECKSUM_UNNECESSARY;
1202 napi_gro_receive(&priv->napi, skb);
1205 priv->dev->stats.rx_packets++;
1206 priv->dev->stats.rx_bytes += frame_len;
1208 entry = next_entry;
1209 p = p_next; /* use prefetched values */
1212 stmmac_rx_refill(priv);
1214 priv->xstats.rx_pkt_n += count;
1216 return count;
1220 * stmmac_poll - stmmac poll method (NAPI)
1221 * @napi : pointer to the napi structure.
1222 * @budget : maximum number of packets that the current CPU can receive from
1223 * all interfaces.
1224 * Description :
1225 * This function implements the the reception process.
1226 * Also it runs the TX completion thread
1228 static int stmmac_poll(struct napi_struct *napi, int budget)
1230 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1231 int work_done = 0;
1233 priv->xstats.poll_n++;
1234 stmmac_tx(priv);
1235 work_done = stmmac_rx(priv, budget);
1237 if (work_done < budget) {
1238 napi_complete(napi);
1239 stmmac_enable_irq(priv);
1241 return work_done;
1245 * stmmac_tx_timeout
1246 * @dev : Pointer to net device structure
1247 * Description: this function is called when a packet transmission fails to
1248 * complete within a reasonable tmrate. The driver will mark the error in the
1249 * netdev structure and arrange for the device to be reset to a sane state
1250 * in order to transmit a new packet.
1252 static void stmmac_tx_timeout(struct net_device *dev)
1254 struct stmmac_priv *priv = netdev_priv(dev);
1256 /* Clear Tx resources and restart transmitting again */
1257 stmmac_tx_err(priv);
1260 /* Configuration changes (passed on by ifconfig) */
1261 static int stmmac_config(struct net_device *dev, struct ifmap *map)
1263 if (dev->flags & IFF_UP) /* can't act on a running interface */
1264 return -EBUSY;
1266 /* Don't allow changing the I/O address */
1267 if (map->base_addr != dev->base_addr) {
1268 pr_warning("%s: can't change I/O address\n", dev->name);
1269 return -EOPNOTSUPP;
1272 /* Don't allow changing the IRQ */
1273 if (map->irq != dev->irq) {
1274 pr_warning("%s: can't change IRQ number %d\n",
1275 dev->name, dev->irq);
1276 return -EOPNOTSUPP;
1279 /* ignore other fields */
1280 return 0;
1284 * stmmac_set_rx_mode - entry point for multicast addressing
1285 * @dev : pointer to the device structure
1286 * Description:
1287 * This function is a driver entry point which gets called by the kernel
1288 * whenever multicast addresses must be enabled/disabled.
1289 * Return value:
1290 * void.
1292 static void stmmac_set_rx_mode(struct net_device *dev)
1294 struct stmmac_priv *priv = netdev_priv(dev);
1296 spin_lock(&priv->lock);
1297 priv->hw->mac->set_filter(dev);
1298 spin_unlock(&priv->lock);
1302 * stmmac_change_mtu - entry point to change MTU size for the device.
1303 * @dev : device pointer.
1304 * @new_mtu : the new MTU size for the device.
1305 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1306 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1307 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1308 * Return value:
1309 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1310 * file on failure.
1312 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1314 struct stmmac_priv *priv = netdev_priv(dev);
1315 int max_mtu;
1317 if (netif_running(dev)) {
1318 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1319 return -EBUSY;
1322 if (priv->plat->has_gmac)
1323 max_mtu = JUMBO_LEN;
1324 else
1325 max_mtu = ETH_DATA_LEN;
1327 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1328 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1329 return -EINVAL;
1332 dev->mtu = new_mtu;
1333 netdev_update_features(dev);
1335 return 0;
1338 static u32 stmmac_fix_features(struct net_device *dev, u32 features)
1340 struct stmmac_priv *priv = netdev_priv(dev);
1342 if (!priv->rx_coe)
1343 features &= ~NETIF_F_RXCSUM;
1344 if (!priv->plat->tx_coe)
1345 features &= ~NETIF_F_ALL_CSUM;
1347 /* Some GMAC devices have a bugged Jumbo frame support that
1348 * needs to have the Tx COE disabled for oversized frames
1349 * (due to limited buffer sizes). In this case we disable
1350 * the TX csum insertionin the TDES and not use SF. */
1351 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1352 features &= ~NETIF_F_ALL_CSUM;
1354 return features;
1357 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1359 struct net_device *dev = (struct net_device *)dev_id;
1360 struct stmmac_priv *priv = netdev_priv(dev);
1362 if (unlikely(!dev)) {
1363 pr_err("%s: invalid dev pointer\n", __func__);
1364 return IRQ_NONE;
1367 if (priv->plat->has_gmac)
1368 /* To handle GMAC own interrupts */
1369 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
1371 stmmac_dma_interrupt(priv);
1373 return IRQ_HANDLED;
1376 #ifdef CONFIG_NET_POLL_CONTROLLER
1377 /* Polling receive - used by NETCONSOLE and other diagnostic tools
1378 * to allow network I/O with interrupts disabled. */
1379 static void stmmac_poll_controller(struct net_device *dev)
1381 disable_irq(dev->irq);
1382 stmmac_interrupt(dev->irq, dev);
1383 enable_irq(dev->irq);
1385 #endif
1388 * stmmac_ioctl - Entry point for the Ioctl
1389 * @dev: Device pointer.
1390 * @rq: An IOCTL specefic structure, that can contain a pointer to
1391 * a proprietary structure used to pass information to the driver.
1392 * @cmd: IOCTL command
1393 * Description:
1394 * Currently there are no special functionality supported in IOCTL, just the
1395 * phy_mii_ioctl(...) can be invoked.
1397 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1399 struct stmmac_priv *priv = netdev_priv(dev);
1400 int ret;
1402 if (!netif_running(dev))
1403 return -EINVAL;
1405 if (!priv->phydev)
1406 return -EINVAL;
1408 spin_lock(&priv->lock);
1409 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1410 spin_unlock(&priv->lock);
1412 return ret;
1415 static const struct net_device_ops stmmac_netdev_ops = {
1416 .ndo_open = stmmac_open,
1417 .ndo_start_xmit = stmmac_xmit,
1418 .ndo_stop = stmmac_release,
1419 .ndo_change_mtu = stmmac_change_mtu,
1420 .ndo_fix_features = stmmac_fix_features,
1421 .ndo_set_rx_mode = stmmac_set_rx_mode,
1422 .ndo_tx_timeout = stmmac_tx_timeout,
1423 .ndo_do_ioctl = stmmac_ioctl,
1424 .ndo_set_config = stmmac_config,
1425 #ifdef CONFIG_NET_POLL_CONTROLLER
1426 .ndo_poll_controller = stmmac_poll_controller,
1427 #endif
1428 .ndo_set_mac_address = eth_mac_addr,
1432 * stmmac_probe - Initialization of the adapter .
1433 * @dev : device pointer
1434 * Description: The function initializes the network device structure for
1435 * the STMMAC driver. It also calls the low level routines
1436 * in order to init the HW (i.e. the DMA engine)
1438 static int stmmac_probe(struct net_device *dev)
1440 int ret = 0;
1441 struct stmmac_priv *priv = netdev_priv(dev);
1443 ether_setup(dev);
1445 dev->netdev_ops = &stmmac_netdev_ops;
1446 stmmac_set_ethtool_ops(dev);
1448 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1449 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
1450 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1451 #ifdef STMMAC_VLAN_TAG_USED
1452 /* Both mac100 and gmac support receive VLAN tag detection */
1453 dev->features |= NETIF_F_HW_VLAN_RX;
1454 #endif
1455 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1457 if (flow_ctrl)
1458 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1460 priv->pause = pause;
1461 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1463 /* Get the MAC address */
1464 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1465 dev->dev_addr, 0);
1467 if (!is_valid_ether_addr(dev->dev_addr))
1468 pr_warning("\tno valid MAC address;"
1469 "please, use ifconfig or nwhwconfig!\n");
1471 spin_lock_init(&priv->lock);
1473 ret = register_netdev(dev);
1474 if (ret) {
1475 pr_err("%s: ERROR %i registering the device\n",
1476 __func__, ret);
1477 return -ENODEV;
1480 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1481 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
1482 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
1484 return ret;
1488 * stmmac_mac_device_setup
1489 * @dev : device pointer
1490 * Description: select and initialise the mac device (mac100 or Gmac).
1492 static int stmmac_mac_device_setup(struct net_device *dev)
1494 struct stmmac_priv *priv = netdev_priv(dev);
1496 struct mac_device_info *device;
1498 if (priv->plat->has_gmac) {
1499 dev->priv_flags |= IFF_UNICAST_FLT;
1500 device = dwmac1000_setup(priv->ioaddr);
1501 } else {
1502 device = dwmac100_setup(priv->ioaddr);
1505 if (!device)
1506 return -ENOMEM;
1508 if (priv->plat->enh_desc) {
1509 device->desc = &enh_desc_ops;
1510 pr_info("\tEnhanced descriptor structure\n");
1511 } else
1512 device->desc = &ndesc_ops;
1514 priv->hw = device;
1516 if (device_can_wakeup(priv->device)) {
1517 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
1518 enable_irq_wake(priv->wol_irq);
1521 return 0;
1525 * stmmac_dvr_probe
1526 * @pdev: platform device pointer
1527 * Description: the driver is initialized through platform_device.
1529 static int stmmac_dvr_probe(struct platform_device *pdev)
1531 int ret = 0;
1532 struct resource *res;
1533 void __iomem *addr = NULL;
1534 struct net_device *ndev = NULL;
1535 struct stmmac_priv *priv = NULL;
1536 struct plat_stmmacenet_data *plat_dat;
1538 pr_info("STMMAC driver:\n\tplatform registration... ");
1539 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1540 if (!res)
1541 return -ENODEV;
1542 pr_info("\tdone!\n");
1544 if (!request_mem_region(res->start, resource_size(res),
1545 pdev->name)) {
1546 pr_err("%s: ERROR: memory allocation failed"
1547 "cannot get the I/O addr 0x%x\n",
1548 __func__, (unsigned int)res->start);
1549 return -EBUSY;
1552 addr = ioremap(res->start, resource_size(res));
1553 if (!addr) {
1554 pr_err("%s: ERROR: memory mapping failed\n", __func__);
1555 ret = -ENOMEM;
1556 goto out_release_region;
1559 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1560 if (!ndev) {
1561 pr_err("%s: ERROR: allocating the device\n", __func__);
1562 ret = -ENOMEM;
1563 goto out_unmap;
1566 SET_NETDEV_DEV(ndev, &pdev->dev);
1568 /* Get the MAC information */
1569 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1570 if (ndev->irq == -ENXIO) {
1571 pr_err("%s: ERROR: MAC IRQ configuration "
1572 "information not found\n", __func__);
1573 ret = -ENXIO;
1574 goto out_free_ndev;
1577 priv = netdev_priv(ndev);
1578 priv->device = &(pdev->dev);
1579 priv->dev = ndev;
1580 plat_dat = pdev->dev.platform_data;
1582 priv->plat = plat_dat;
1584 priv->ioaddr = addr;
1586 /* PMT module is not integrated in all the MAC devices. */
1587 if (plat_dat->pmt) {
1588 pr_info("\tPMT module supported\n");
1589 device_set_wakeup_capable(&pdev->dev, 1);
1592 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
1593 * The external wake up irq can be passed through the platform code
1594 * named as "eth_wake_irq"
1596 * In case the wake up interrupt is not passed from the platform
1597 * so the driver will continue to use the mac irq (ndev->irq)
1599 priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
1600 if (priv->wol_irq == -ENXIO)
1601 priv->wol_irq = ndev->irq;
1604 platform_set_drvdata(pdev, ndev);
1606 /* Set the I/O base addr */
1607 ndev->base_addr = (unsigned long)addr;
1609 /* Custom initialisation */
1610 if (priv->plat->init) {
1611 ret = priv->plat->init(pdev);
1612 if (unlikely(ret))
1613 goto out_free_ndev;
1616 /* MAC HW revice detection */
1617 ret = stmmac_mac_device_setup(ndev);
1618 if (ret < 0)
1619 goto out_plat_exit;
1621 /* Network Device Registration */
1622 ret = stmmac_probe(ndev);
1623 if (ret < 0)
1624 goto out_plat_exit;
1626 /* Override with kernel parameters if supplied XXX CRS XXX
1627 * this needs to have multiple instances */
1628 if ((phyaddr >= 0) && (phyaddr <= 31))
1629 priv->plat->phy_addr = phyaddr;
1631 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
1632 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1633 pdev->id, ndev->irq, addr);
1635 /* MDIO bus Registration */
1636 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
1637 ret = stmmac_mdio_register(ndev);
1638 if (ret < 0)
1639 goto out_unregister;
1640 pr_debug("registered!\n");
1641 return 0;
1643 out_unregister:
1644 unregister_netdev(ndev);
1645 out_plat_exit:
1646 if (priv->plat->exit)
1647 priv->plat->exit(pdev);
1648 out_free_ndev:
1649 free_netdev(ndev);
1650 platform_set_drvdata(pdev, NULL);
1651 out_unmap:
1652 iounmap(addr);
1653 out_release_region:
1654 release_mem_region(res->start, resource_size(res));
1656 return ret;
1660 * stmmac_dvr_remove
1661 * @pdev: platform device pointer
1662 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1663 * changes the link status, releases the DMA descriptor rings,
1664 * unregisters the MDIO bus and unmaps the allocated memory.
1666 static int stmmac_dvr_remove(struct platform_device *pdev)
1668 struct net_device *ndev = platform_get_drvdata(pdev);
1669 struct stmmac_priv *priv = netdev_priv(ndev);
1670 struct resource *res;
1672 pr_info("%s:\n\tremoving driver", __func__);
1674 priv->hw->dma->stop_rx(priv->ioaddr);
1675 priv->hw->dma->stop_tx(priv->ioaddr);
1677 stmmac_disable_mac(priv->ioaddr);
1679 netif_carrier_off(ndev);
1681 stmmac_mdio_unregister(ndev);
1683 if (priv->plat->exit)
1684 priv->plat->exit(pdev);
1686 platform_set_drvdata(pdev, NULL);
1687 unregister_netdev(ndev);
1689 iounmap((void *)priv->ioaddr);
1690 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1691 release_mem_region(res->start, resource_size(res));
1693 free_netdev(ndev);
1695 return 0;
1698 #ifdef CONFIG_PM
1699 static int stmmac_suspend(struct device *dev)
1701 struct net_device *ndev = dev_get_drvdata(dev);
1702 struct stmmac_priv *priv = netdev_priv(ndev);
1703 int dis_ic = 0;
1705 if (!ndev || !netif_running(ndev))
1706 return 0;
1708 spin_lock(&priv->lock);
1710 netif_device_detach(ndev);
1711 netif_stop_queue(ndev);
1712 if (priv->phydev)
1713 phy_stop(priv->phydev);
1715 #ifdef CONFIG_STMMAC_TIMER
1716 priv->tm->timer_stop();
1717 if (likely(priv->tm->enable))
1718 dis_ic = 1;
1719 #endif
1720 napi_disable(&priv->napi);
1722 /* Stop TX/RX DMA */
1723 priv->hw->dma->stop_tx(priv->ioaddr);
1724 priv->hw->dma->stop_rx(priv->ioaddr);
1725 /* Clear the Rx/Tx descriptors */
1726 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1727 dis_ic);
1728 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
1730 /* Enable Power down mode by programming the PMT regs */
1731 if (device_may_wakeup(priv->device))
1732 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
1733 else
1734 stmmac_disable_mac(priv->ioaddr);
1736 spin_unlock(&priv->lock);
1737 return 0;
1740 static int stmmac_resume(struct device *dev)
1742 struct net_device *ndev = dev_get_drvdata(dev);
1743 struct stmmac_priv *priv = netdev_priv(ndev);
1745 if (!netif_running(ndev))
1746 return 0;
1748 spin_lock(&priv->lock);
1750 /* Power Down bit, into the PM register, is cleared
1751 * automatically as soon as a magic packet or a Wake-up frame
1752 * is received. Anyway, it's better to manually clear
1753 * this bit because it can generate problems while resuming
1754 * from another devices (e.g. serial console). */
1755 if (device_may_wakeup(priv->device))
1756 priv->hw->mac->pmt(priv->ioaddr, 0);
1758 netif_device_attach(ndev);
1760 /* Enable the MAC and DMA */
1761 stmmac_enable_mac(priv->ioaddr);
1762 priv->hw->dma->start_tx(priv->ioaddr);
1763 priv->hw->dma->start_rx(priv->ioaddr);
1765 #ifdef CONFIG_STMMAC_TIMER
1766 if (likely(priv->tm->enable))
1767 priv->tm->timer_start(tmrate);
1768 #endif
1769 napi_enable(&priv->napi);
1771 if (priv->phydev)
1772 phy_start(priv->phydev);
1774 netif_start_queue(ndev);
1776 spin_unlock(&priv->lock);
1777 return 0;
1780 static int stmmac_freeze(struct device *dev)
1782 struct net_device *ndev = dev_get_drvdata(dev);
1784 if (!ndev || !netif_running(ndev))
1785 return 0;
1787 return stmmac_release(ndev);
1790 static int stmmac_restore(struct device *dev)
1792 struct net_device *ndev = dev_get_drvdata(dev);
1794 if (!ndev || !netif_running(ndev))
1795 return 0;
1797 return stmmac_open(ndev);
1800 static const struct dev_pm_ops stmmac_pm_ops = {
1801 .suspend = stmmac_suspend,
1802 .resume = stmmac_resume,
1803 .freeze = stmmac_freeze,
1804 .thaw = stmmac_restore,
1805 .restore = stmmac_restore,
1807 #else
1808 static const struct dev_pm_ops stmmac_pm_ops;
1809 #endif /* CONFIG_PM */
1811 static struct platform_driver stmmac_driver = {
1812 .probe = stmmac_dvr_probe,
1813 .remove = stmmac_dvr_remove,
1814 .driver = {
1815 .name = STMMAC_RESOURCE_NAME,
1816 .owner = THIS_MODULE,
1817 .pm = &stmmac_pm_ops,
1822 * stmmac_init_module - Entry point for the driver
1823 * Description: This function is the entry point for the driver.
1825 static int __init stmmac_init_module(void)
1827 int ret;
1829 ret = platform_driver_register(&stmmac_driver);
1830 return ret;
1834 * stmmac_cleanup_module - Cleanup routine for the driver
1835 * Description: This function is the cleanup routine for the driver.
1837 static void __exit stmmac_cleanup_module(void)
1839 platform_driver_unregister(&stmmac_driver);
1842 #ifndef MODULE
1843 static int __init stmmac_cmdline_opt(char *str)
1845 char *opt;
1847 if (!str || !*str)
1848 return -EINVAL;
1849 while ((opt = strsep(&str, ",")) != NULL) {
1850 if (!strncmp(opt, "debug:", 6)) {
1851 if (strict_strtoul(opt + 6, 0, (unsigned long *)&debug))
1852 goto err;
1853 } else if (!strncmp(opt, "phyaddr:", 8)) {
1854 if (strict_strtoul(opt + 8, 0,
1855 (unsigned long *)&phyaddr))
1856 goto err;
1857 } else if (!strncmp(opt, "dma_txsize:", 11)) {
1858 if (strict_strtoul(opt + 11, 0,
1859 (unsigned long *)&dma_txsize))
1860 goto err;
1861 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
1862 if (strict_strtoul(opt + 11, 0,
1863 (unsigned long *)&dma_rxsize))
1864 goto err;
1865 } else if (!strncmp(opt, "buf_sz:", 7)) {
1866 if (strict_strtoul(opt + 7, 0,
1867 (unsigned long *)&buf_sz))
1868 goto err;
1869 } else if (!strncmp(opt, "tc:", 3)) {
1870 if (strict_strtoul(opt + 3, 0, (unsigned long *)&tc))
1871 goto err;
1872 } else if (!strncmp(opt, "watchdog:", 9)) {
1873 if (strict_strtoul(opt + 9, 0,
1874 (unsigned long *)&watchdog))
1875 goto err;
1876 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
1877 if (strict_strtoul(opt + 10, 0,
1878 (unsigned long *)&flow_ctrl))
1879 goto err;
1880 } else if (!strncmp(opt, "pause:", 6)) {
1881 if (strict_strtoul(opt + 6, 0, (unsigned long *)&pause))
1882 goto err;
1883 #ifdef CONFIG_STMMAC_TIMER
1884 } else if (!strncmp(opt, "tmrate:", 7)) {
1885 if (strict_strtoul(opt + 7, 0,
1886 (unsigned long *)&tmrate))
1887 goto err;
1888 #endif
1891 return 0;
1893 err:
1894 pr_err("%s: ERROR broken module parameter conversion", __func__);
1895 return -EINVAL;
1898 __setup("stmmaceth=", stmmac_cmdline_opt);
1899 #endif
1901 module_init(stmmac_init_module);
1902 module_exit(stmmac_cleanup_module);
1904 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
1905 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
1906 MODULE_LICENSE("GPL");