blob: a02006d57a598f5373ae691fc34f3c0118cca221 [file] [log] [blame]
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001/*******************************************************************************
2 This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
3 ST Ethernet IPs are built around a Synopsys IP Core.
4
5 Copyright (C) 2007-2009 STMicroelectronics Ltd
6
7 This program is free software; you can redistribute it and/or modify it
8 under the terms and conditions of the GNU General Public License,
9 version 2, as published by the Free Software Foundation.
10
11 This program is distributed in the hope it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19
20 The full GNU General Public License is included in this distribution in
21 the file called "COPYING".
22
23 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
24
25 Documentation available at:
26 http://www.stlinux.com
27 Support available at:
28 https://bugzilla.stlinux.com/
29*******************************************************************************/
30
31#include <linux/module.h>
32#include <linux/init.h>
33#include <linux/kernel.h>
34#include <linux/interrupt.h>
35#include <linux/netdevice.h>
36#include <linux/etherdevice.h>
37#include <linux/platform_device.h>
38#include <linux/ip.h>
39#include <linux/tcp.h>
40#include <linux/skbuff.h>
41#include <linux/ethtool.h>
42#include <linux/if_ether.h>
43#include <linux/crc32.h>
44#include <linux/mii.h>
45#include <linux/phy.h>
46#include <linux/if_vlan.h>
47#include <linux/dma-mapping.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070048#include "stmmac.h"
49
50#define STMMAC_RESOURCE_NAME "stmmaceth"
51#define PHY_RESOURCE_NAME "stmmacphy"
52
53#undef STMMAC_DEBUG
54/*#define STMMAC_DEBUG*/
55#ifdef STMMAC_DEBUG
56#define DBG(nlevel, klevel, fmt, args...) \
57 ((void)(netif_msg_##nlevel(priv) && \
58 printk(KERN_##klevel fmt, ## args)))
59#else
60#define DBG(nlevel, klevel, fmt, args...) do { } while (0)
61#endif
62
63#undef STMMAC_RX_DEBUG
64/*#define STMMAC_RX_DEBUG*/
65#ifdef STMMAC_RX_DEBUG
66#define RX_DBG(fmt, args...) printk(fmt, ## args)
67#else
68#define RX_DBG(fmt, args...) do { } while (0)
69#endif
70
71#undef STMMAC_XMIT_DEBUG
72/*#define STMMAC_XMIT_DEBUG*/
73#ifdef STMMAC_TX_DEBUG
74#define TX_DBG(fmt, args...) printk(fmt, ## args)
75#else
76#define TX_DBG(fmt, args...) do { } while (0)
77#endif
78
79#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
80#define JUMBO_LEN 9000
81
82/* Module parameters */
83#define TX_TIMEO 5000 /* default 5 seconds */
84static int watchdog = TX_TIMEO;
85module_param(watchdog, int, S_IRUGO | S_IWUSR);
86MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
87
88static int debug = -1; /* -1: default, 0: no output, 16: all */
89module_param(debug, int, S_IRUGO | S_IWUSR);
90MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
91
92static int phyaddr = -1;
93module_param(phyaddr, int, S_IRUGO);
94MODULE_PARM_DESC(phyaddr, "Physical device address");
95
96#define DMA_TX_SIZE 256
97static int dma_txsize = DMA_TX_SIZE;
98module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
99MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
100
101#define DMA_RX_SIZE 256
102static int dma_rxsize = DMA_RX_SIZE;
103module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
105
106static int flow_ctrl = FLOW_OFF;
107module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
108MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
109
110static int pause = PAUSE_TIME;
111module_param(pause, int, S_IRUGO | S_IWUSR);
112MODULE_PARM_DESC(pause, "Flow Control Pause Time");
113
114#define TC_DEFAULT 64
115static int tc = TC_DEFAULT;
116module_param(tc, int, S_IRUGO | S_IWUSR);
117MODULE_PARM_DESC(tc, "DMA threshold control value");
118
119#define RX_NO_COALESCE 1 /* Always interrupt on completion */
120#define TX_NO_COALESCE -1 /* No moderation by default */
121
122/* Pay attention to tune this parameter; take care of both
123 * hardware capability and network stabitily/performance impact.
124 * Many tests showed that ~4ms latency seems to be good enough. */
125#ifdef CONFIG_STMMAC_TIMER
126#define DEFAULT_PERIODIC_RATE 256
127static int tmrate = DEFAULT_PERIODIC_RATE;
128module_param(tmrate, int, S_IRUGO | S_IWUSR);
129MODULE_PARM_DESC(tmrate, "External timer freq. (default: 256Hz)");
130#endif
131
132#define DMA_BUFFER_SIZE BUF_SIZE_2KiB
133static int buf_sz = DMA_BUFFER_SIZE;
134module_param(buf_sz, int, S_IRUGO | S_IWUSR);
135MODULE_PARM_DESC(buf_sz, "DMA buffer size");
136
137/* In case of Giga ETH, we can enable/disable the COE for the
138 * transmit HW checksum computation.
139 * Note that, if tx csum is off in HW, SG will be still supported. */
140static int tx_coe = HW_CSUM;
141module_param(tx_coe, int, S_IRUGO | S_IWUSR);
142MODULE_PARM_DESC(tx_coe, "GMAC COE type 2 [on/off]");
143
144static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
145 NETIF_MSG_LINK | NETIF_MSG_IFUP |
146 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
147
148static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
149static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev);
150
151/**
152 * stmmac_verify_args - verify the driver parameters.
153 * Description: it verifies if some wrong parameter is passed to the driver.
154 * Note that wrong parameters are replaced with the default values.
155 */
156static void stmmac_verify_args(void)
157{
158 if (unlikely(watchdog < 0))
159 watchdog = TX_TIMEO;
160 if (unlikely(dma_rxsize < 0))
161 dma_rxsize = DMA_RX_SIZE;
162 if (unlikely(dma_txsize < 0))
163 dma_txsize = DMA_TX_SIZE;
164 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
165 buf_sz = DMA_BUFFER_SIZE;
166 if (unlikely(flow_ctrl > 1))
167 flow_ctrl = FLOW_AUTO;
168 else if (likely(flow_ctrl < 0))
169 flow_ctrl = FLOW_OFF;
170 if (unlikely((pause < 0) || (pause > 0xffff)))
171 pause = PAUSE_TIME;
172
173 return;
174}
175
176#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
177static void print_pkt(unsigned char *buf, int len)
178{
179 int j;
180 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
181 for (j = 0; j < len; j++) {
182 if ((j % 16) == 0)
183 pr_info("\n %03x:", j);
184 pr_info(" %02x", buf[j]);
185 }
186 pr_info("\n");
187 return;
188}
189#endif
190
191/* minimum number of free TX descriptors required to wake up TX process */
192#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
193
194static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
195{
196 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
197}
198
199/**
200 * stmmac_adjust_link
201 * @dev: net device structure
202 * Description: it adjusts the link parameters.
203 */
204static void stmmac_adjust_link(struct net_device *dev)
205{
206 struct stmmac_priv *priv = netdev_priv(dev);
207 struct phy_device *phydev = priv->phydev;
208 unsigned long ioaddr = dev->base_addr;
209 unsigned long flags;
210 int new_state = 0;
211 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
212
213 if (phydev == NULL)
214 return;
215
216 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
217 phydev->addr, phydev->link);
218
219 spin_lock_irqsave(&priv->lock, flags);
220 if (phydev->link) {
221 u32 ctrl = readl(ioaddr + MAC_CTRL_REG);
222
223 /* Now we make sure that we can be in full duplex mode.
224 * If not, we operate in half-duplex mode. */
225 if (phydev->duplex != priv->oldduplex) {
226 new_state = 1;
227 if (!(phydev->duplex))
228 ctrl &= ~priv->mac_type->hw.link.duplex;
229 else
230 ctrl |= priv->mac_type->hw.link.duplex;
231 priv->oldduplex = phydev->duplex;
232 }
233 /* Flow Control operation */
234 if (phydev->pause)
235 priv->mac_type->ops->flow_ctrl(ioaddr, phydev->duplex,
236 fc, pause_time);
237
238 if (phydev->speed != priv->speed) {
239 new_state = 1;
240 switch (phydev->speed) {
241 case 1000:
242 if (likely(priv->is_gmac))
243 ctrl &= ~priv->mac_type->hw.link.port;
244 break;
245 case 100:
246 case 10:
247 if (priv->is_gmac) {
248 ctrl |= priv->mac_type->hw.link.port;
249 if (phydev->speed == SPEED_100) {
250 ctrl |=
251 priv->mac_type->hw.link.
252 speed;
253 } else {
254 ctrl &=
255 ~(priv->mac_type->hw.
256 link.speed);
257 }
258 } else {
259 ctrl &= ~priv->mac_type->hw.link.port;
260 }
261 priv->fix_mac_speed(priv->bsp_priv,
262 phydev->speed);
263 break;
264 default:
265 if (netif_msg_link(priv))
266 pr_warning("%s: Speed (%d) is not 10"
267 " or 100!\n", dev->name, phydev->speed);
268 break;
269 }
270
271 priv->speed = phydev->speed;
272 }
273
274 writel(ctrl, ioaddr + MAC_CTRL_REG);
275
276 if (!priv->oldlink) {
277 new_state = 1;
278 priv->oldlink = 1;
279 }
280 } else if (priv->oldlink) {
281 new_state = 1;
282 priv->oldlink = 0;
283 priv->speed = 0;
284 priv->oldduplex = -1;
285 }
286
287 if (new_state && netif_msg_link(priv))
288 phy_print_status(phydev);
289
290 spin_unlock_irqrestore(&priv->lock, flags);
291
292 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
293}
294
295/**
296 * stmmac_init_phy - PHY initialization
297 * @dev: net device structure
298 * Description: it initializes the driver's PHY state, and attaches the PHY
299 * to the mac driver.
300 * Return value:
301 * 0 on success
302 */
303static int stmmac_init_phy(struct net_device *dev)
304{
305 struct stmmac_priv *priv = netdev_priv(dev);
306 struct phy_device *phydev;
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000307 char phy_id[MII_BUS_ID_SIZE + 3];
308 char bus_id[MII_BUS_ID_SIZE];
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700309
310 priv->oldlink = 0;
311 priv->speed = 0;
312 priv->oldduplex = -1;
313
314 if (priv->phy_addr == -1) {
315 /* We don't have a PHY, so do nothing */
316 return 0;
317 }
318
319 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->bus_id);
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000320 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
321 priv->phy_addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700322 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
323
324 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
325 priv->phy_interface);
326
327 if (IS_ERR(phydev)) {
328 pr_err("%s: Could not attach to PHY\n", dev->name);
329 return PTR_ERR(phydev);
330 }
331
332 /*
333 * Broken HW is sometimes missing the pull-up resistor on the
334 * MDIO line, which results in reads to non-existent devices returning
335 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
336 * device as well.
337 * Note: phydev->phy_id is the result of reading the UID PHY registers.
338 */
339 if (phydev->phy_id == 0) {
340 phy_disconnect(phydev);
341 return -ENODEV;
342 }
343 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
344 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
345
346 priv->phydev = phydev;
347
348 return 0;
349}
350
351static inline void stmmac_mac_enable_rx(unsigned long ioaddr)
352{
353 u32 value = readl(ioaddr + MAC_CTRL_REG);
354 value |= MAC_RNABLE_RX;
355 /* Set the RE (receive enable bit into the MAC CTRL register). */
356 writel(value, ioaddr + MAC_CTRL_REG);
357}
358
359static inline void stmmac_mac_enable_tx(unsigned long ioaddr)
360{
361 u32 value = readl(ioaddr + MAC_CTRL_REG);
362 value |= MAC_ENABLE_TX;
363 /* Set the TE (transmit enable bit into the MAC CTRL register). */
364 writel(value, ioaddr + MAC_CTRL_REG);
365}
366
367static inline void stmmac_mac_disable_rx(unsigned long ioaddr)
368{
369 u32 value = readl(ioaddr + MAC_CTRL_REG);
370 value &= ~MAC_RNABLE_RX;
371 writel(value, ioaddr + MAC_CTRL_REG);
372}
373
374static inline void stmmac_mac_disable_tx(unsigned long ioaddr)
375{
376 u32 value = readl(ioaddr + MAC_CTRL_REG);
377 value &= ~MAC_ENABLE_TX;
378 writel(value, ioaddr + MAC_CTRL_REG);
379}
380
381/**
382 * display_ring
383 * @p: pointer to the ring.
384 * @size: size of the ring.
385 * Description: display all the descriptors within the ring.
386 */
387static void display_ring(struct dma_desc *p, int size)
388{
389 struct tmp_s {
390 u64 a;
391 unsigned int b;
392 unsigned int c;
393 };
394 int i;
395 for (i = 0; i < size; i++) {
396 struct tmp_s *x = (struct tmp_s *)(p + i);
397 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
398 i, (unsigned int)virt_to_phys(&p[i]),
399 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
400 x->b, x->c);
401 pr_info("\n");
402 }
403}
404
405/**
406 * init_dma_desc_rings - init the RX/TX descriptor rings
407 * @dev: net device structure
408 * Description: this function initializes the DMA RX/TX descriptors
409 * and allocates the socket buffers.
410 */
411static void init_dma_desc_rings(struct net_device *dev)
412{
413 int i;
414 struct stmmac_priv *priv = netdev_priv(dev);
415 struct sk_buff *skb;
416 unsigned int txsize = priv->dma_tx_size;
417 unsigned int rxsize = priv->dma_rx_size;
418 unsigned int bfsize = priv->dma_buf_sz;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000419 int buff2_needed = 0, dis_ic = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700420
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700421 /* Set the Buffer size according to the MTU;
422 * indeed, in case of jumbo we need to bump-up the buffer sizes.
423 */
424 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
425 bfsize = BUF_SIZE_16KiB;
426 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
427 bfsize = BUF_SIZE_8KiB;
428 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
429 bfsize = BUF_SIZE_4KiB;
430 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
431 bfsize = BUF_SIZE_2KiB;
432 else
433 bfsize = DMA_BUFFER_SIZE;
434
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000435#ifdef CONFIG_STMMAC_TIMER
436 /* Disable interrupts on completion for the reception if timer is on */
437 if (likely(priv->tm->enable))
438 dis_ic = 1;
439#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700440 /* If the MTU exceeds 8k so use the second buffer in the chain */
441 if (bfsize >= BUF_SIZE_8KiB)
442 buff2_needed = 1;
443
444 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
445 txsize, rxsize, bfsize);
446
447 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
448 priv->rx_skbuff =
449 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
450 priv->dma_rx =
451 (struct dma_desc *)dma_alloc_coherent(priv->device,
452 rxsize *
453 sizeof(struct dma_desc),
454 &priv->dma_rx_phy,
455 GFP_KERNEL);
456 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
457 GFP_KERNEL);
458 priv->dma_tx =
459 (struct dma_desc *)dma_alloc_coherent(priv->device,
460 txsize *
461 sizeof(struct dma_desc),
462 &priv->dma_tx_phy,
463 GFP_KERNEL);
464
465 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
466 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
467 return;
468 }
469
470 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
471 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
472 dev->name, priv->dma_rx, priv->dma_tx,
473 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
474
475 /* RX INITIALIZATION */
476 DBG(probe, INFO, "stmmac: SKB addresses:\n"
477 "skb\t\tskb data\tdma data\n");
478
479 for (i = 0; i < rxsize; i++) {
480 struct dma_desc *p = priv->dma_rx + i;
481
482 skb = netdev_alloc_skb_ip_align(dev, bfsize);
483 if (unlikely(skb == NULL)) {
484 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
485 break;
486 }
487 priv->rx_skbuff[i] = skb;
488 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
489 bfsize, DMA_FROM_DEVICE);
490
491 p->des2 = priv->rx_skbuff_dma[i];
492 if (unlikely(buff2_needed))
493 p->des3 = p->des2 + BUF_SIZE_8KiB;
494 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
495 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
496 }
497 priv->cur_rx = 0;
498 priv->dirty_rx = (unsigned int)(i - rxsize);
499 priv->dma_buf_sz = bfsize;
500 buf_sz = bfsize;
501
502 /* TX INITIALIZATION */
503 for (i = 0; i < txsize; i++) {
504 priv->tx_skbuff[i] = NULL;
505 priv->dma_tx[i].des2 = 0;
506 }
507 priv->dirty_tx = 0;
508 priv->cur_tx = 0;
509
510 /* Clear the Rx/Tx descriptors */
511 priv->mac_type->ops->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
512 priv->mac_type->ops->init_tx_desc(priv->dma_tx, txsize);
513
514 if (netif_msg_hw(priv)) {
515 pr_info("RX descriptor ring:\n");
516 display_ring(priv->dma_rx, rxsize);
517 pr_info("TX descriptor ring:\n");
518 display_ring(priv->dma_tx, txsize);
519 }
520 return;
521}
522
523static void dma_free_rx_skbufs(struct stmmac_priv *priv)
524{
525 int i;
526
527 for (i = 0; i < priv->dma_rx_size; i++) {
528 if (priv->rx_skbuff[i]) {
529 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
530 priv->dma_buf_sz, DMA_FROM_DEVICE);
531 dev_kfree_skb_any(priv->rx_skbuff[i]);
532 }
533 priv->rx_skbuff[i] = NULL;
534 }
535 return;
536}
537
538static void dma_free_tx_skbufs(struct stmmac_priv *priv)
539{
540 int i;
541
542 for (i = 0; i < priv->dma_tx_size; i++) {
543 if (priv->tx_skbuff[i] != NULL) {
544 struct dma_desc *p = priv->dma_tx + i;
545 if (p->des2)
546 dma_unmap_single(priv->device, p->des2,
547 priv->mac_type->ops->get_tx_len(p),
548 DMA_TO_DEVICE);
549 dev_kfree_skb_any(priv->tx_skbuff[i]);
550 priv->tx_skbuff[i] = NULL;
551 }
552 }
553 return;
554}
555
556static void free_dma_desc_resources(struct stmmac_priv *priv)
557{
558 /* Release the DMA TX/RX socket buffers */
559 dma_free_rx_skbufs(priv);
560 dma_free_tx_skbufs(priv);
561
562 /* Free the region of consistent memory previously allocated for
563 * the DMA */
564 dma_free_coherent(priv->device,
565 priv->dma_tx_size * sizeof(struct dma_desc),
566 priv->dma_tx, priv->dma_tx_phy);
567 dma_free_coherent(priv->device,
568 priv->dma_rx_size * sizeof(struct dma_desc),
569 priv->dma_rx, priv->dma_rx_phy);
570 kfree(priv->rx_skbuff_dma);
571 kfree(priv->rx_skbuff);
572 kfree(priv->tx_skbuff);
573
574 return;
575}
576
577/**
578 * stmmac_dma_start_tx
579 * @ioaddr: device I/O address
580 * Description: this function starts the DMA tx process.
581 */
582static void stmmac_dma_start_tx(unsigned long ioaddr)
583{
584 u32 value = readl(ioaddr + DMA_CONTROL);
585 value |= DMA_CONTROL_ST;
586 writel(value, ioaddr + DMA_CONTROL);
587 return;
588}
589
590static void stmmac_dma_stop_tx(unsigned long ioaddr)
591{
592 u32 value = readl(ioaddr + DMA_CONTROL);
593 value &= ~DMA_CONTROL_ST;
594 writel(value, ioaddr + DMA_CONTROL);
595 return;
596}
597
598/**
599 * stmmac_dma_start_rx
600 * @ioaddr: device I/O address
601 * Description: this function starts the DMA rx process.
602 */
603static void stmmac_dma_start_rx(unsigned long ioaddr)
604{
605 u32 value = readl(ioaddr + DMA_CONTROL);
606 value |= DMA_CONTROL_SR;
607 writel(value, ioaddr + DMA_CONTROL);
608
609 return;
610}
611
612static void stmmac_dma_stop_rx(unsigned long ioaddr)
613{
614 u32 value = readl(ioaddr + DMA_CONTROL);
615 value &= ~DMA_CONTROL_SR;
616 writel(value, ioaddr + DMA_CONTROL);
617
618 return;
619}
620
621/**
622 * stmmac_dma_operation_mode - HW DMA operation mode
623 * @priv : pointer to the private device structure.
624 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
625 * or Store-And-Forward capability. It also verifies the COE for the
626 * transmission in case of Giga ETH.
627 */
628static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
629{
630 if (!priv->is_gmac) {
631 /* MAC 10/100 */
632 priv->mac_type->ops->dma_mode(priv->dev->base_addr, tc, 0);
633 priv->tx_coe = NO_HW_CSUM;
634 } else {
635 if ((priv->dev->mtu <= ETH_DATA_LEN) && (tx_coe)) {
636 priv->mac_type->ops->dma_mode(priv->dev->base_addr,
637 SF_DMA_MODE, SF_DMA_MODE);
638 tc = SF_DMA_MODE;
639 priv->tx_coe = HW_CSUM;
640 } else {
641 /* Checksum computation is performed in software. */
642 priv->mac_type->ops->dma_mode(priv->dev->base_addr, tc,
643 SF_DMA_MODE);
644 priv->tx_coe = NO_HW_CSUM;
645 }
646 }
647 tx_coe = priv->tx_coe;
648
649 return;
650}
651
652#ifdef STMMAC_DEBUG
653/**
654 * show_tx_process_state
655 * @status: tx descriptor status field
656 * Description: it shows the Transmit Process State for CSR5[22:20]
657 */
658static void show_tx_process_state(unsigned int status)
659{
660 unsigned int state;
661 state = (status & DMA_STATUS_TS_MASK) >> DMA_STATUS_TS_SHIFT;
662
663 switch (state) {
664 case 0:
665 pr_info("- TX (Stopped): Reset or Stop command\n");
666 break;
667 case 1:
668 pr_info("- TX (Running):Fetching the Tx desc\n");
669 break;
670 case 2:
671 pr_info("- TX (Running): Waiting for end of tx\n");
672 break;
673 case 3:
674 pr_info("- TX (Running): Reading the data "
675 "and queuing the data into the Tx buf\n");
676 break;
677 case 6:
678 pr_info("- TX (Suspended): Tx Buff Underflow "
679 "or an unavailable Transmit descriptor\n");
680 break;
681 case 7:
682 pr_info("- TX (Running): Closing Tx descriptor\n");
683 break;
684 default:
685 break;
686 }
687 return;
688}
689
690/**
691 * show_rx_process_state
692 * @status: rx descriptor status field
693 * Description: it shows the Receive Process State for CSR5[19:17]
694 */
695static void show_rx_process_state(unsigned int status)
696{
697 unsigned int state;
698 state = (status & DMA_STATUS_RS_MASK) >> DMA_STATUS_RS_SHIFT;
699
700 switch (state) {
701 case 0:
702 pr_info("- RX (Stopped): Reset or Stop command\n");
703 break;
704 case 1:
705 pr_info("- RX (Running): Fetching the Rx desc\n");
706 break;
707 case 2:
708 pr_info("- RX (Running):Checking for end of pkt\n");
709 break;
710 case 3:
711 pr_info("- RX (Running): Waiting for Rx pkt\n");
712 break;
713 case 4:
714 pr_info("- RX (Suspended): Unavailable Rx buf\n");
715 break;
716 case 5:
717 pr_info("- RX (Running): Closing Rx descriptor\n");
718 break;
719 case 6:
720 pr_info("- RX(Running): Flushing the current frame"
721 " from the Rx buf\n");
722 break;
723 case 7:
724 pr_info("- RX (Running): Queuing the Rx frame"
725 " from the Rx buf into memory\n");
726 break;
727 default:
728 break;
729 }
730 return;
731}
732#endif
733
734/**
735 * stmmac_tx:
736 * @priv: private driver structure
737 * Description: it reclaims resources after transmission completes.
738 */
739static void stmmac_tx(struct stmmac_priv *priv)
740{
741 unsigned int txsize = priv->dma_tx_size;
742 unsigned long ioaddr = priv->dev->base_addr;
743
744 while (priv->dirty_tx != priv->cur_tx) {
745 int last;
746 unsigned int entry = priv->dirty_tx % txsize;
747 struct sk_buff *skb = priv->tx_skbuff[entry];
748 struct dma_desc *p = priv->dma_tx + entry;
749
750 /* Check if the descriptor is owned by the DMA. */
751 if (priv->mac_type->ops->get_tx_owner(p))
752 break;
753
754 /* Verify tx error by looking at the last segment */
755 last = priv->mac_type->ops->get_tx_ls(p);
756 if (likely(last)) {
757 int tx_error =
758 priv->mac_type->ops->tx_status(&priv->dev->stats,
759 &priv->xstats,
760 p, ioaddr);
761 if (likely(tx_error == 0)) {
762 priv->dev->stats.tx_packets++;
763 priv->xstats.tx_pkt_n++;
764 } else
765 priv->dev->stats.tx_errors++;
766 }
767 TX_DBG("%s: curr %d, dirty %d\n", __func__,
768 priv->cur_tx, priv->dirty_tx);
769
770 if (likely(p->des2))
771 dma_unmap_single(priv->device, p->des2,
772 priv->mac_type->ops->get_tx_len(p),
773 DMA_TO_DEVICE);
774 if (unlikely(p->des3))
775 p->des3 = 0;
776
777 if (likely(skb != NULL)) {
778 /*
779 * If there's room in the queue (limit it to size)
780 * we add this skb back into the pool,
781 * if it's the right size.
782 */
783 if ((skb_queue_len(&priv->rx_recycle) <
784 priv->dma_rx_size) &&
785 skb_recycle_check(skb, priv->dma_buf_sz))
786 __skb_queue_head(&priv->rx_recycle, skb);
787 else
788 dev_kfree_skb(skb);
789
790 priv->tx_skbuff[entry] = NULL;
791 }
792
793 priv->mac_type->ops->release_tx_desc(p);
794
795 entry = (++priv->dirty_tx) % txsize;
796 }
797 if (unlikely(netif_queue_stopped(priv->dev) &&
798 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
799 netif_tx_lock(priv->dev);
800 if (netif_queue_stopped(priv->dev) &&
801 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
802 TX_DBG("%s: restart transmit\n", __func__);
803 netif_wake_queue(priv->dev);
804 }
805 netif_tx_unlock(priv->dev);
806 }
807 return;
808}
809
810static inline void stmmac_enable_irq(struct stmmac_priv *priv)
811{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000812#ifdef CONFIG_STMMAC_TIMER
813 if (likely(priv->tm->enable))
814 priv->tm->timer_start(tmrate);
815 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700816#endif
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000817 writel(DMA_INTR_DEFAULT_MASK, priv->dev->base_addr + DMA_INTR_ENA);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700818}
819
820static inline void stmmac_disable_irq(struct stmmac_priv *priv)
821{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000822#ifdef CONFIG_STMMAC_TIMER
823 if (likely(priv->tm->enable))
824 priv->tm->timer_stop();
825 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700826#endif
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000827 writel(0, priv->dev->base_addr + DMA_INTR_ENA);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700828}
829
830static int stmmac_has_work(struct stmmac_priv *priv)
831{
832 unsigned int has_work = 0;
833 int rxret, tx_work = 0;
834
835 rxret = priv->mac_type->ops->get_rx_owner(priv->dma_rx +
836 (priv->cur_rx % priv->dma_rx_size));
837
838 if (priv->dirty_tx != priv->cur_tx)
839 tx_work = 1;
840
841 if (likely(!rxret || tx_work))
842 has_work = 1;
843
844 return has_work;
845}
846
847static inline void _stmmac_schedule(struct stmmac_priv *priv)
848{
849 if (likely(stmmac_has_work(priv))) {
850 stmmac_disable_irq(priv);
851 napi_schedule(&priv->napi);
852 }
853}
854
855#ifdef CONFIG_STMMAC_TIMER
856void stmmac_schedule(struct net_device *dev)
857{
858 struct stmmac_priv *priv = netdev_priv(dev);
859
860 priv->xstats.sched_timer_n++;
861
862 _stmmac_schedule(priv);
863
864 return;
865}
866
867static void stmmac_no_timer_started(unsigned int x)
868{;
869};
870
871static void stmmac_no_timer_stopped(void)
872{;
873};
874#endif
875
876/**
877 * stmmac_tx_err:
878 * @priv: pointer to the private device structure
879 * Description: it cleans the descriptors and restarts the transmission
880 * in case of errors.
881 */
882static void stmmac_tx_err(struct stmmac_priv *priv)
883{
884 netif_stop_queue(priv->dev);
885
886 stmmac_dma_stop_tx(priv->dev->base_addr);
887 dma_free_tx_skbufs(priv);
888 priv->mac_type->ops->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
889 priv->dirty_tx = 0;
890 priv->cur_tx = 0;
891 stmmac_dma_start_tx(priv->dev->base_addr);
892
893 priv->dev->stats.tx_errors++;
894 netif_wake_queue(priv->dev);
895
896 return;
897}
898
899/**
900 * stmmac_dma_interrupt - Interrupt handler for the driver
901 * @dev: net device structure
902 * Description: Interrupt handler for the driver (DMA).
903 */
904static void stmmac_dma_interrupt(struct net_device *dev)
905{
906 unsigned long ioaddr = dev->base_addr;
907 struct stmmac_priv *priv = netdev_priv(dev);
908 /* read the status register (CSR5) */
909 u32 intr_status = readl(ioaddr + DMA_STATUS);
910
911 DBG(intr, INFO, "%s: [CSR5: 0x%08x]\n", __func__, intr_status);
912
913#ifdef STMMAC_DEBUG
914 /* It displays the DMA transmit process state (CSR5 register) */
915 if (netif_msg_tx_done(priv))
916 show_tx_process_state(intr_status);
917 if (netif_msg_rx_status(priv))
918 show_rx_process_state(intr_status);
919#endif
920 /* ABNORMAL interrupts */
921 if (unlikely(intr_status & DMA_STATUS_AIS)) {
922 DBG(intr, INFO, "CSR5[15] DMA ABNORMAL IRQ: ");
923 if (unlikely(intr_status & DMA_STATUS_UNF)) {
924 DBG(intr, INFO, "transmit underflow\n");
Joe Perches8e95a202009-12-03 07:58:21 +0000925 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700926 /* Try to bump up the threshold */
927 tc += 64;
928 priv->mac_type->ops->dma_mode(ioaddr, tc,
929 SF_DMA_MODE);
930 priv->xstats.threshold = tc;
931 }
932 stmmac_tx_err(priv);
933 priv->xstats.tx_undeflow_irq++;
934 }
935 if (unlikely(intr_status & DMA_STATUS_TJT)) {
936 DBG(intr, INFO, "transmit jabber\n");
937 priv->xstats.tx_jabber_irq++;
938 }
939 if (unlikely(intr_status & DMA_STATUS_OVF)) {
940 DBG(intr, INFO, "recv overflow\n");
941 priv->xstats.rx_overflow_irq++;
942 }
943 if (unlikely(intr_status & DMA_STATUS_RU)) {
944 DBG(intr, INFO, "receive buffer unavailable\n");
945 priv->xstats.rx_buf_unav_irq++;
946 }
947 if (unlikely(intr_status & DMA_STATUS_RPS)) {
948 DBG(intr, INFO, "receive process stopped\n");
949 priv->xstats.rx_process_stopped_irq++;
950 }
951 if (unlikely(intr_status & DMA_STATUS_RWT)) {
952 DBG(intr, INFO, "receive watchdog\n");
953 priv->xstats.rx_watchdog_irq++;
954 }
955 if (unlikely(intr_status & DMA_STATUS_ETI)) {
956 DBG(intr, INFO, "transmit early interrupt\n");
957 priv->xstats.tx_early_irq++;
958 }
959 if (unlikely(intr_status & DMA_STATUS_TPS)) {
960 DBG(intr, INFO, "transmit process stopped\n");
961 priv->xstats.tx_process_stopped_irq++;
962 stmmac_tx_err(priv);
963 }
964 if (unlikely(intr_status & DMA_STATUS_FBI)) {
965 DBG(intr, INFO, "fatal bus error\n");
966 priv->xstats.fatal_bus_error_irq++;
967 stmmac_tx_err(priv);
968 }
969 }
970
971 /* TX/RX NORMAL interrupts */
972 if (intr_status & DMA_STATUS_NIS) {
973 priv->xstats.normal_irq_n++;
974 if (likely((intr_status & DMA_STATUS_RI) ||
975 (intr_status & (DMA_STATUS_TI))))
976 _stmmac_schedule(priv);
977 }
978
979 /* Optional hardware blocks, interrupts should be disabled */
980 if (unlikely(intr_status &
981 (DMA_STATUS_GPI | DMA_STATUS_GMI | DMA_STATUS_GLI)))
982 pr_info("%s: unexpected status %08x\n", __func__, intr_status);
983
984 /* Clear the interrupt by writing a logic 1 to the CSR5[15-0] */
985 writel((intr_status & 0x1ffff), ioaddr + DMA_STATUS);
986
987 DBG(intr, INFO, "\n\n");
988
989 return;
990}
991
992/**
993 * stmmac_open - open entry point of the driver
994 * @dev : pointer to the device structure.
995 * Description:
996 * This function is the open entry point of the driver.
997 * Return value:
998 * 0 on success and an appropriate (-)ve integer as defined in errno.h
999 * file on failure.
1000 */
1001static int stmmac_open(struct net_device *dev)
1002{
1003 struct stmmac_priv *priv = netdev_priv(dev);
1004 unsigned long ioaddr = dev->base_addr;
1005 int ret;
1006
1007 /* Check that the MAC address is valid. If its not, refuse
1008 * to bring the device up. The user must specify an
1009 * address using the following linux command:
1010 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
1011 if (!is_valid_ether_addr(dev->dev_addr)) {
1012 random_ether_addr(dev->dev_addr);
1013 pr_warning("%s: generated random MAC address %pM\n", dev->name,
1014 dev->dev_addr);
1015 }
1016
1017 stmmac_verify_args();
1018
1019 ret = stmmac_init_phy(dev);
1020 if (unlikely(ret)) {
1021 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
1022 return ret;
1023 }
1024
1025 /* Request the IRQ lines */
Joe Perchesa0607fd2009-11-18 23:29:17 -08001026 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001027 IRQF_SHARED, dev->name, dev);
1028 if (unlikely(ret < 0)) {
1029 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1030 __func__, dev->irq, ret);
1031 return ret;
1032 }
1033
1034#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001035 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001036 if (unlikely(priv->tm == NULL)) {
1037 pr_err("%s: ERROR: timer memory alloc failed \n", __func__);
1038 return -ENOMEM;
1039 }
1040 priv->tm->freq = tmrate;
1041
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001042 /* Test if the external timer can be actually used.
1043 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001044 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001045 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001046 tmrate = 0;
1047 priv->tm->freq = 0;
1048 priv->tm->timer_start = stmmac_no_timer_started;
1049 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001050 } else
1051 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001052#endif
1053
1054 /* Create and initialize the TX/RX descriptors chains. */
1055 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1056 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1057 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
1058 init_dma_desc_rings(dev);
1059
1060 /* DMA initialization and SW reset */
1061 if (unlikely(priv->mac_type->ops->dma_init(ioaddr,
1062 priv->pbl, priv->dma_tx_phy, priv->dma_rx_phy) < 0)) {
1063
1064 pr_err("%s: DMA initialization failed\n", __func__);
1065 return -1;
1066 }
1067
1068 /* Copy the MAC addr into the HW */
1069 priv->mac_type->ops->set_umac_addr(ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +00001070 /* If required, perform hw setup of the bus. */
1071 if (priv->bus_setup)
1072 priv->bus_setup(ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001073 /* Initialize the MAC Core */
1074 priv->mac_type->ops->core_init(ioaddr);
1075
1076 priv->shutdown = 0;
1077
1078 /* Initialise the MMC (if present) to disable all interrupts. */
1079 writel(0xffffffff, ioaddr + MMC_HIGH_INTR_MASK);
1080 writel(0xffffffff, ioaddr + MMC_LOW_INTR_MASK);
1081
1082 /* Enable the MAC Rx/Tx */
1083 stmmac_mac_enable_rx(ioaddr);
1084 stmmac_mac_enable_tx(ioaddr);
1085
1086 /* Set the HW DMA mode and the COE */
1087 stmmac_dma_operation_mode(priv);
1088
1089 /* Extra statistics */
1090 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1091 priv->xstats.threshold = tc;
1092
1093 /* Start the ball rolling... */
1094 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
1095 stmmac_dma_start_tx(ioaddr);
1096 stmmac_dma_start_rx(ioaddr);
1097
1098#ifdef CONFIG_STMMAC_TIMER
1099 priv->tm->timer_start(tmrate);
1100#endif
1101 /* Dump DMA/MAC registers */
1102 if (netif_msg_hw(priv)) {
1103 priv->mac_type->ops->dump_mac_regs(ioaddr);
1104 priv->mac_type->ops->dump_dma_regs(ioaddr);
1105 }
1106
1107 if (priv->phydev)
1108 phy_start(priv->phydev);
1109
1110 napi_enable(&priv->napi);
1111 skb_queue_head_init(&priv->rx_recycle);
1112 netif_start_queue(dev);
1113 return 0;
1114}
1115
1116/**
1117 * stmmac_release - close entry point of the driver
1118 * @dev : device pointer.
1119 * Description:
1120 * This is the stop entry point of the driver.
1121 */
1122static int stmmac_release(struct net_device *dev)
1123{
1124 struct stmmac_priv *priv = netdev_priv(dev);
1125
1126 /* Stop and disconnect the PHY */
1127 if (priv->phydev) {
1128 phy_stop(priv->phydev);
1129 phy_disconnect(priv->phydev);
1130 priv->phydev = NULL;
1131 }
1132
1133 netif_stop_queue(dev);
1134
1135#ifdef CONFIG_STMMAC_TIMER
1136 /* Stop and release the timer */
1137 stmmac_close_ext_timer();
1138 if (priv->tm != NULL)
1139 kfree(priv->tm);
1140#endif
1141 napi_disable(&priv->napi);
1142 skb_queue_purge(&priv->rx_recycle);
1143
1144 /* Free the IRQ lines */
1145 free_irq(dev->irq, dev);
1146
1147 /* Stop TX/RX DMA and clear the descriptors */
1148 stmmac_dma_stop_tx(dev->base_addr);
1149 stmmac_dma_stop_rx(dev->base_addr);
1150
1151 /* Release and free the Rx/Tx resources */
1152 free_dma_desc_resources(priv);
1153
1154 /* Disable the MAC core */
1155 stmmac_mac_disable_tx(dev->base_addr);
1156 stmmac_mac_disable_rx(dev->base_addr);
1157
1158 netif_carrier_off(dev);
1159
1160 return 0;
1161}
1162
1163/*
1164 * To perform emulated hardware segmentation on skb.
1165 */
1166static int stmmac_sw_tso(struct stmmac_priv *priv, struct sk_buff *skb)
1167{
1168 struct sk_buff *segs, *curr_skb;
1169 int gso_segs = skb_shinfo(skb)->gso_segs;
1170
1171 /* Estimate the number of fragments in the worst case */
1172 if (unlikely(stmmac_tx_avail(priv) < gso_segs)) {
1173 netif_stop_queue(priv->dev);
1174 TX_DBG(KERN_ERR "%s: TSO BUG! Tx Ring full when queue awake\n",
1175 __func__);
1176 if (stmmac_tx_avail(priv) < gso_segs)
1177 return NETDEV_TX_BUSY;
1178
1179 netif_wake_queue(priv->dev);
1180 }
1181 TX_DBG("\tstmmac_sw_tso: segmenting: skb %p (len %d)\n",
1182 skb, skb->len);
1183
1184 segs = skb_gso_segment(skb, priv->dev->features & ~NETIF_F_TSO);
1185 if (unlikely(IS_ERR(segs)))
1186 goto sw_tso_end;
1187
1188 do {
1189 curr_skb = segs;
1190 segs = segs->next;
1191 TX_DBG("\t\tcurrent skb->len: %d, *curr %p,"
1192 "*next %p\n", curr_skb->len, curr_skb, segs);
1193 curr_skb->next = NULL;
1194 stmmac_xmit(curr_skb, priv->dev);
1195 } while (segs);
1196
1197sw_tso_end:
1198 dev_kfree_skb(skb);
1199
1200 return NETDEV_TX_OK;
1201}
1202
1203static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
1204 struct net_device *dev,
1205 int csum_insertion)
1206{
1207 struct stmmac_priv *priv = netdev_priv(dev);
1208 unsigned int nopaged_len = skb_headlen(skb);
1209 unsigned int txsize = priv->dma_tx_size;
1210 unsigned int entry = priv->cur_tx % txsize;
1211 struct dma_desc *desc = priv->dma_tx + entry;
1212
1213 if (nopaged_len > BUF_SIZE_8KiB) {
1214
1215 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
1216
1217 desc->des2 = dma_map_single(priv->device, skb->data,
1218 BUF_SIZE_8KiB, DMA_TO_DEVICE);
1219 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
1220 priv->mac_type->ops->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
1221 csum_insertion);
1222
1223 entry = (++priv->cur_tx) % txsize;
1224 desc = priv->dma_tx + entry;
1225
1226 desc->des2 = dma_map_single(priv->device,
1227 skb->data + BUF_SIZE_8KiB,
1228 buf2_size, DMA_TO_DEVICE);
1229 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
1230 priv->mac_type->ops->prepare_tx_desc(desc, 0,
1231 buf2_size, csum_insertion);
1232 priv->mac_type->ops->set_tx_owner(desc);
1233 priv->tx_skbuff[entry] = NULL;
1234 } else {
1235 desc->des2 = dma_map_single(priv->device, skb->data,
1236 nopaged_len, DMA_TO_DEVICE);
1237 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
1238 priv->mac_type->ops->prepare_tx_desc(desc, 1, nopaged_len,
1239 csum_insertion);
1240 }
1241 return entry;
1242}
1243
1244/**
1245 * stmmac_xmit:
1246 * @skb : the socket buffer
1247 * @dev : device pointer
1248 * Description : Tx entry point of the driver.
1249 */
1250static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1251{
1252 struct stmmac_priv *priv = netdev_priv(dev);
1253 unsigned int txsize = priv->dma_tx_size;
1254 unsigned int entry;
1255 int i, csum_insertion = 0;
1256 int nfrags = skb_shinfo(skb)->nr_frags;
1257 struct dma_desc *desc, *first;
1258
1259 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1260 if (!netif_queue_stopped(dev)) {
1261 netif_stop_queue(dev);
1262 /* This is a hard error, log it. */
1263 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1264 __func__);
1265 }
1266 return NETDEV_TX_BUSY;
1267 }
1268
1269 entry = priv->cur_tx % txsize;
1270
1271#ifdef STMMAC_XMIT_DEBUG
1272 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1273 pr_info("stmmac xmit:\n"
1274 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1275 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1276 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1277 !skb_is_gso(skb) ? "isn't" : "is");
1278#endif
1279
1280 if (unlikely(skb_is_gso(skb)))
1281 return stmmac_sw_tso(priv, skb);
1282
1283 if (likely((skb->ip_summed == CHECKSUM_PARTIAL))) {
1284 if (likely(priv->tx_coe == NO_HW_CSUM))
1285 skb_checksum_help(skb);
1286 else
1287 csum_insertion = 1;
1288 }
1289
1290 desc = priv->dma_tx + entry;
1291 first = desc;
1292
1293#ifdef STMMAC_XMIT_DEBUG
1294 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1295 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1296 "\t\tn_frags: %d, ip_summed: %d\n",
1297 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1298#endif
1299 priv->tx_skbuff[entry] = skb;
1300 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1301 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1302 desc = priv->dma_tx + entry;
1303 } else {
1304 unsigned int nopaged_len = skb_headlen(skb);
1305 desc->des2 = dma_map_single(priv->device, skb->data,
1306 nopaged_len, DMA_TO_DEVICE);
1307 priv->mac_type->ops->prepare_tx_desc(desc, 1, nopaged_len,
1308 csum_insertion);
1309 }
1310
1311 for (i = 0; i < nfrags; i++) {
1312 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1313 int len = frag->size;
1314
1315 entry = (++priv->cur_tx) % txsize;
1316 desc = priv->dma_tx + entry;
1317
1318 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1319 desc->des2 = dma_map_page(priv->device, frag->page,
1320 frag->page_offset,
1321 len, DMA_TO_DEVICE);
1322 priv->tx_skbuff[entry] = NULL;
1323 priv->mac_type->ops->prepare_tx_desc(desc, 0, len,
1324 csum_insertion);
1325 priv->mac_type->ops->set_tx_owner(desc);
1326 }
1327
1328 /* Interrupt on completition only for the latest segment */
1329 priv->mac_type->ops->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001330
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001331#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001332 /* Clean IC while using timer */
1333 if (likely(priv->tm->enable))
1334 priv->mac_type->ops->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001335#endif
1336 /* To avoid raise condition */
1337 priv->mac_type->ops->set_tx_owner(first);
1338
1339 priv->cur_tx++;
1340
1341#ifdef STMMAC_XMIT_DEBUG
1342 if (netif_msg_pktdata(priv)) {
1343 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1344 "first=%p, nfrags=%d\n",
1345 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1346 entry, first, nfrags);
1347 display_ring(priv->dma_tx, txsize);
1348 pr_info(">>> frame to be transmitted: ");
1349 print_pkt(skb->data, skb->len);
1350 }
1351#endif
1352 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1353 TX_DBG("%s: stop transmitted packets\n", __func__);
1354 netif_stop_queue(dev);
1355 }
1356
1357 dev->stats.tx_bytes += skb->len;
1358
1359 /* CSR1 enables the transmit DMA to check for new descriptor */
1360 writel(1, dev->base_addr + DMA_XMT_POLL_DEMAND);
1361
1362 return NETDEV_TX_OK;
1363}
1364
1365static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1366{
1367 unsigned int rxsize = priv->dma_rx_size;
1368 int bfsize = priv->dma_buf_sz;
1369 struct dma_desc *p = priv->dma_rx;
1370
1371 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1372 unsigned int entry = priv->dirty_rx % rxsize;
1373 if (likely(priv->rx_skbuff[entry] == NULL)) {
1374 struct sk_buff *skb;
1375
1376 skb = __skb_dequeue(&priv->rx_recycle);
1377 if (skb == NULL)
1378 skb = netdev_alloc_skb_ip_align(priv->dev,
1379 bfsize);
1380
1381 if (unlikely(skb == NULL))
1382 break;
1383
1384 priv->rx_skbuff[entry] = skb;
1385 priv->rx_skbuff_dma[entry] =
1386 dma_map_single(priv->device, skb->data, bfsize,
1387 DMA_FROM_DEVICE);
1388
1389 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
1390 if (unlikely(priv->is_gmac)) {
1391 if (bfsize >= BUF_SIZE_8KiB)
1392 (p + entry)->des3 =
1393 (p + entry)->des2 + BUF_SIZE_8KiB;
1394 }
1395 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1396 }
1397 priv->mac_type->ops->set_rx_owner(p + entry);
1398 }
1399 return;
1400}
1401
1402static int stmmac_rx(struct stmmac_priv *priv, int limit)
1403{
1404 unsigned int rxsize = priv->dma_rx_size;
1405 unsigned int entry = priv->cur_rx % rxsize;
1406 unsigned int next_entry;
1407 unsigned int count = 0;
1408 struct dma_desc *p = priv->dma_rx + entry;
1409 struct dma_desc *p_next;
1410
1411#ifdef STMMAC_RX_DEBUG
1412 if (netif_msg_hw(priv)) {
1413 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1414 display_ring(priv->dma_rx, rxsize);
1415 }
1416#endif
1417 count = 0;
1418 while (!priv->mac_type->ops->get_rx_owner(p)) {
1419 int status;
1420
1421 if (count >= limit)
1422 break;
1423
1424 count++;
1425
1426 next_entry = (++priv->cur_rx) % rxsize;
1427 p_next = priv->dma_rx + next_entry;
1428 prefetch(p_next);
1429
1430 /* read the status of the incoming frame */
1431 status = (priv->mac_type->ops->rx_status(&priv->dev->stats,
1432 &priv->xstats, p));
1433 if (unlikely(status == discard_frame))
1434 priv->dev->stats.rx_errors++;
1435 else {
1436 struct sk_buff *skb;
1437 /* Length should omit the CRC */
1438 int frame_len =
1439 priv->mac_type->ops->get_rx_frame_len(p) - 4;
1440
1441#ifdef STMMAC_RX_DEBUG
1442 if (frame_len > ETH_FRAME_LEN)
1443 pr_debug("\tRX frame size %d, COE status: %d\n",
1444 frame_len, status);
1445
1446 if (netif_msg_hw(priv))
1447 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1448 p, entry, p->des2);
1449#endif
1450 skb = priv->rx_skbuff[entry];
1451 if (unlikely(!skb)) {
1452 pr_err("%s: Inconsistent Rx descriptor chain\n",
1453 priv->dev->name);
1454 priv->dev->stats.rx_dropped++;
1455 break;
1456 }
1457 prefetch(skb->data - NET_IP_ALIGN);
1458 priv->rx_skbuff[entry] = NULL;
1459
1460 skb_put(skb, frame_len);
1461 dma_unmap_single(priv->device,
1462 priv->rx_skbuff_dma[entry],
1463 priv->dma_buf_sz, DMA_FROM_DEVICE);
1464#ifdef STMMAC_RX_DEBUG
1465 if (netif_msg_pktdata(priv)) {
1466 pr_info(" frame received (%dbytes)", frame_len);
1467 print_pkt(skb->data, frame_len);
1468 }
1469#endif
1470 skb->protocol = eth_type_trans(skb, priv->dev);
1471
1472 if (unlikely(status == csum_none)) {
1473 /* always for the old mac 10/100 */
1474 skb->ip_summed = CHECKSUM_NONE;
1475 netif_receive_skb(skb);
1476 } else {
1477 skb->ip_summed = CHECKSUM_UNNECESSARY;
1478 napi_gro_receive(&priv->napi, skb);
1479 }
1480
1481 priv->dev->stats.rx_packets++;
1482 priv->dev->stats.rx_bytes += frame_len;
1483 priv->dev->last_rx = jiffies;
1484 }
1485 entry = next_entry;
1486 p = p_next; /* use prefetched values */
1487 }
1488
1489 stmmac_rx_refill(priv);
1490
1491 priv->xstats.rx_pkt_n += count;
1492
1493 return count;
1494}
1495
1496/**
1497 * stmmac_poll - stmmac poll method (NAPI)
1498 * @napi : pointer to the napi structure.
1499 * @budget : maximum number of packets that the current CPU can receive from
1500 * all interfaces.
1501 * Description :
1502 * This function implements the the reception process.
1503 * Also it runs the TX completion thread
1504 */
1505static int stmmac_poll(struct napi_struct *napi, int budget)
1506{
1507 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1508 int work_done = 0;
1509
1510 priv->xstats.poll_n++;
1511 stmmac_tx(priv);
1512 work_done = stmmac_rx(priv, budget);
1513
1514 if (work_done < budget) {
1515 napi_complete(napi);
1516 stmmac_enable_irq(priv);
1517 }
1518 return work_done;
1519}
1520
1521/**
1522 * stmmac_tx_timeout
1523 * @dev : Pointer to net device structure
1524 * Description: this function is called when a packet transmission fails to
1525 * complete within a reasonable tmrate. The driver will mark the error in the
1526 * netdev structure and arrange for the device to be reset to a sane state
1527 * in order to transmit a new packet.
1528 */
1529static void stmmac_tx_timeout(struct net_device *dev)
1530{
1531 struct stmmac_priv *priv = netdev_priv(dev);
1532
1533 /* Clear Tx resources and restart transmitting again */
1534 stmmac_tx_err(priv);
1535 return;
1536}
1537
1538/* Configuration changes (passed on by ifconfig) */
1539static int stmmac_config(struct net_device *dev, struct ifmap *map)
1540{
1541 if (dev->flags & IFF_UP) /* can't act on a running interface */
1542 return -EBUSY;
1543
1544 /* Don't allow changing the I/O address */
1545 if (map->base_addr != dev->base_addr) {
1546 pr_warning("%s: can't change I/O address\n", dev->name);
1547 return -EOPNOTSUPP;
1548 }
1549
1550 /* Don't allow changing the IRQ */
1551 if (map->irq != dev->irq) {
1552 pr_warning("%s: can't change IRQ number %d\n",
1553 dev->name, dev->irq);
1554 return -EOPNOTSUPP;
1555 }
1556
1557 /* ignore other fields */
1558 return 0;
1559}
1560
1561/**
1562 * stmmac_multicast_list - entry point for multicast addressing
1563 * @dev : pointer to the device structure
1564 * Description:
1565 * This function is a driver entry point which gets called by the kernel
1566 * whenever multicast addresses must be enabled/disabled.
1567 * Return value:
1568 * void.
1569 */
1570static void stmmac_multicast_list(struct net_device *dev)
1571{
1572 struct stmmac_priv *priv = netdev_priv(dev);
1573
1574 spin_lock(&priv->lock);
1575 priv->mac_type->ops->set_filter(dev);
1576 spin_unlock(&priv->lock);
1577 return;
1578}
1579
1580/**
1581 * stmmac_change_mtu - entry point to change MTU size for the device.
1582 * @dev : device pointer.
1583 * @new_mtu : the new MTU size for the device.
1584 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1585 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1586 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1587 * Return value:
1588 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1589 * file on failure.
1590 */
1591static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1592{
1593 struct stmmac_priv *priv = netdev_priv(dev);
1594 int max_mtu;
1595
1596 if (netif_running(dev)) {
1597 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1598 return -EBUSY;
1599 }
1600
1601 if (priv->is_gmac)
1602 max_mtu = JUMBO_LEN;
1603 else
1604 max_mtu = ETH_DATA_LEN;
1605
1606 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1607 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1608 return -EINVAL;
1609 }
1610
1611 dev->mtu = new_mtu;
1612
1613 return 0;
1614}
1615
1616static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1617{
1618 struct net_device *dev = (struct net_device *)dev_id;
1619 struct stmmac_priv *priv = netdev_priv(dev);
1620
1621 if (unlikely(!dev)) {
1622 pr_err("%s: invalid dev pointer\n", __func__);
1623 return IRQ_NONE;
1624 }
1625
1626 if (priv->is_gmac) {
1627 unsigned long ioaddr = dev->base_addr;
1628 /* To handle GMAC own interrupts */
1629 priv->mac_type->ops->host_irq_status(ioaddr);
1630 }
1631 stmmac_dma_interrupt(dev);
1632
1633 return IRQ_HANDLED;
1634}
1635
1636#ifdef CONFIG_NET_POLL_CONTROLLER
1637/* Polling receive - used by NETCONSOLE and other diagnostic tools
1638 * to allow network I/O with interrupts disabled. */
1639static void stmmac_poll_controller(struct net_device *dev)
1640{
1641 disable_irq(dev->irq);
1642 stmmac_interrupt(dev->irq, dev);
1643 enable_irq(dev->irq);
1644}
1645#endif
1646
1647/**
1648 * stmmac_ioctl - Entry point for the Ioctl
1649 * @dev: Device pointer.
1650 * @rq: An IOCTL specefic structure, that can contain a pointer to
1651 * a proprietary structure used to pass information to the driver.
1652 * @cmd: IOCTL command
1653 * Description:
1654 * Currently there are no special functionality supported in IOCTL, just the
1655 * phy_mii_ioctl(...) can be invoked.
1656 */
1657static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1658{
1659 struct stmmac_priv *priv = netdev_priv(dev);
1660 int ret = -EOPNOTSUPP;
1661
1662 if (!netif_running(dev))
1663 return -EINVAL;
1664
1665 switch (cmd) {
1666 case SIOCGMIIPHY:
1667 case SIOCGMIIREG:
1668 case SIOCSMIIREG:
1669 if (!priv->phydev)
1670 return -EINVAL;
1671
1672 spin_lock(&priv->lock);
1673 ret = phy_mii_ioctl(priv->phydev, if_mii(rq), cmd);
1674 spin_unlock(&priv->lock);
1675 default:
1676 break;
1677 }
1678 return ret;
1679}
1680
1681#ifdef STMMAC_VLAN_TAG_USED
1682static void stmmac_vlan_rx_register(struct net_device *dev,
1683 struct vlan_group *grp)
1684{
1685 struct stmmac_priv *priv = netdev_priv(dev);
1686
1687 DBG(probe, INFO, "%s: Setting vlgrp to %p\n", dev->name, grp);
1688
1689 spin_lock(&priv->lock);
1690 priv->vlgrp = grp;
1691 spin_unlock(&priv->lock);
1692
1693 return;
1694}
1695#endif
1696
1697static const struct net_device_ops stmmac_netdev_ops = {
1698 .ndo_open = stmmac_open,
1699 .ndo_start_xmit = stmmac_xmit,
1700 .ndo_stop = stmmac_release,
1701 .ndo_change_mtu = stmmac_change_mtu,
1702 .ndo_set_multicast_list = stmmac_multicast_list,
1703 .ndo_tx_timeout = stmmac_tx_timeout,
1704 .ndo_do_ioctl = stmmac_ioctl,
1705 .ndo_set_config = stmmac_config,
1706#ifdef STMMAC_VLAN_TAG_USED
1707 .ndo_vlan_rx_register = stmmac_vlan_rx_register,
1708#endif
1709#ifdef CONFIG_NET_POLL_CONTROLLER
1710 .ndo_poll_controller = stmmac_poll_controller,
1711#endif
1712 .ndo_set_mac_address = eth_mac_addr,
1713};
1714
1715/**
1716 * stmmac_probe - Initialization of the adapter .
1717 * @dev : device pointer
1718 * Description: The function initializes the network device structure for
1719 * the STMMAC driver. It also calls the low level routines
1720 * in order to init the HW (i.e. the DMA engine)
1721 */
1722static int stmmac_probe(struct net_device *dev)
1723{
1724 int ret = 0;
1725 struct stmmac_priv *priv = netdev_priv(dev);
1726
1727 ether_setup(dev);
1728
1729 dev->netdev_ops = &stmmac_netdev_ops;
1730 stmmac_set_ethtool_ops(dev);
1731
1732 dev->features |= (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA);
1733 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1734#ifdef STMMAC_VLAN_TAG_USED
1735 /* Both mac100 and gmac support receive VLAN tag detection */
1736 dev->features |= NETIF_F_HW_VLAN_RX;
1737#endif
1738 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1739
1740 if (priv->is_gmac)
1741 priv->rx_csum = 1;
1742
1743 if (flow_ctrl)
1744 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1745
1746 priv->pause = pause;
1747 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1748
1749 /* Get the MAC address */
1750 priv->mac_type->ops->get_umac_addr(dev->base_addr, dev->dev_addr, 0);
1751
1752 if (!is_valid_ether_addr(dev->dev_addr))
1753 pr_warning("\tno valid MAC address;"
1754 "please, use ifconfig or nwhwconfig!\n");
1755
1756 ret = register_netdev(dev);
1757 if (ret) {
1758 pr_err("%s: ERROR %i registering the device\n",
1759 __func__, ret);
1760 return -ENODEV;
1761 }
1762
1763 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1764 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
1765 (dev->features & NETIF_F_HW_CSUM) ? "on" : "off");
1766
1767 spin_lock_init(&priv->lock);
1768
1769 return ret;
1770}
1771
1772/**
1773 * stmmac_mac_device_setup
1774 * @dev : device pointer
1775 * Description: select and initialise the mac device (mac100 or Gmac).
1776 */
1777static int stmmac_mac_device_setup(struct net_device *dev)
1778{
1779 struct stmmac_priv *priv = netdev_priv(dev);
1780 unsigned long ioaddr = dev->base_addr;
1781
1782 struct mac_device_info *device;
1783
1784 if (priv->is_gmac)
1785 device = gmac_setup(ioaddr);
1786 else
1787 device = mac100_setup(ioaddr);
1788
1789 if (!device)
1790 return -ENOMEM;
1791
1792 priv->mac_type = device;
1793
1794 priv->wolenabled = priv->mac_type->hw.pmt; /* PMT supported */
1795 if (priv->wolenabled == PMT_SUPPORTED)
1796 priv->wolopts = WAKE_MAGIC; /* Magic Frame */
1797
1798 return 0;
1799}
1800
1801static int stmmacphy_dvr_probe(struct platform_device *pdev)
1802{
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001803 struct plat_stmmacphy_data *plat_dat = pdev->dev.platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001804
1805 pr_debug("stmmacphy_dvr_probe: added phy for bus %d\n",
1806 plat_dat->bus_id);
1807
1808 return 0;
1809}
1810
1811static int stmmacphy_dvr_remove(struct platform_device *pdev)
1812{
1813 return 0;
1814}
1815
1816static struct platform_driver stmmacphy_driver = {
1817 .driver = {
1818 .name = PHY_RESOURCE_NAME,
1819 },
1820 .probe = stmmacphy_dvr_probe,
1821 .remove = stmmacphy_dvr_remove,
1822};
1823
1824/**
1825 * stmmac_associate_phy
1826 * @dev: pointer to device structure
1827 * @data: points to the private structure.
1828 * Description: Scans through all the PHYs we have registered and checks if
1829 * any are associated with our MAC. If so, then just fill in
1830 * the blanks in our local context structure
1831 */
1832static int stmmac_associate_phy(struct device *dev, void *data)
1833{
1834 struct stmmac_priv *priv = (struct stmmac_priv *)data;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001835 struct plat_stmmacphy_data *plat_dat = dev->platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001836
1837 DBG(probe, DEBUG, "%s: checking phy for bus %d\n", __func__,
1838 plat_dat->bus_id);
1839
1840 /* Check that this phy is for the MAC being initialised */
1841 if (priv->bus_id != plat_dat->bus_id)
1842 return 0;
1843
1844 /* OK, this PHY is connected to the MAC.
1845 Go ahead and get the parameters */
1846 DBG(probe, DEBUG, "%s: OK. Found PHY config\n", __func__);
1847 priv->phy_irq =
1848 platform_get_irq_byname(to_platform_device(dev), "phyirq");
1849 DBG(probe, DEBUG, "%s: PHY irq on bus %d is %d\n", __func__,
1850 plat_dat->bus_id, priv->phy_irq);
1851
1852 /* Override with kernel parameters if supplied XXX CRS XXX
1853 * this needs to have multiple instances */
1854 if ((phyaddr >= 0) && (phyaddr <= 31))
1855 plat_dat->phy_addr = phyaddr;
1856
1857 priv->phy_addr = plat_dat->phy_addr;
1858 priv->phy_mask = plat_dat->phy_mask;
1859 priv->phy_interface = plat_dat->interface;
1860 priv->phy_reset = plat_dat->phy_reset;
1861
1862 DBG(probe, DEBUG, "%s: exiting\n", __func__);
1863 return 1; /* forces exit of driver_for_each_device() */
1864}
1865
1866/**
1867 * stmmac_dvr_probe
1868 * @pdev: platform device pointer
1869 * Description: the driver is initialized through platform_device.
1870 */
1871static int stmmac_dvr_probe(struct platform_device *pdev)
1872{
1873 int ret = 0;
1874 struct resource *res;
1875 unsigned int *addr = NULL;
1876 struct net_device *ndev = NULL;
1877 struct stmmac_priv *priv;
1878 struct plat_stmmacenet_data *plat_dat;
1879
1880 pr_info("STMMAC driver:\n\tplatform registration... ");
1881 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1882 if (!res) {
1883 ret = -ENODEV;
1884 goto out;
1885 }
1886 pr_info("done!\n");
1887
1888 if (!request_mem_region(res->start, (res->end - res->start),
1889 pdev->name)) {
1890 pr_err("%s: ERROR: memory allocation failed"
1891 "cannot get the I/O addr 0x%x\n",
1892 __func__, (unsigned int)res->start);
1893 ret = -EBUSY;
1894 goto out;
1895 }
1896
1897 addr = ioremap(res->start, (res->end - res->start));
1898 if (!addr) {
1899 pr_err("%s: ERROR: memory mapping failed \n", __func__);
1900 ret = -ENOMEM;
1901 goto out;
1902 }
1903
1904 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1905 if (!ndev) {
1906 pr_err("%s: ERROR: allocating the device\n", __func__);
1907 ret = -ENOMEM;
1908 goto out;
1909 }
1910
1911 SET_NETDEV_DEV(ndev, &pdev->dev);
1912
1913 /* Get the MAC information */
1914 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1915 if (ndev->irq == -ENXIO) {
1916 pr_err("%s: ERROR: MAC IRQ configuration "
1917 "information not found\n", __func__);
1918 ret = -ENODEV;
1919 goto out;
1920 }
1921
1922 priv = netdev_priv(ndev);
1923 priv->device = &(pdev->dev);
1924 priv->dev = ndev;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001925 plat_dat = pdev->dev.platform_data;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001926 priv->bus_id = plat_dat->bus_id;
1927 priv->pbl = plat_dat->pbl; /* TLI */
1928 priv->is_gmac = plat_dat->has_gmac; /* GMAC is on board */
1929
1930 platform_set_drvdata(pdev, ndev);
1931
1932 /* Set the I/O base addr */
1933 ndev->base_addr = (unsigned long)addr;
1934
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001935 /* Verify embedded resource for the platform */
1936 ret = stmmac_claim_resource(pdev);
1937 if (ret < 0)
1938 goto out;
1939
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001940 /* MAC HW revice detection */
1941 ret = stmmac_mac_device_setup(ndev);
1942 if (ret < 0)
1943 goto out;
1944
1945 /* Network Device Registration */
1946 ret = stmmac_probe(ndev);
1947 if (ret < 0)
1948 goto out;
1949
1950 /* associate a PHY - it is provided by another platform bus */
1951 if (!driver_for_each_device
1952 (&(stmmacphy_driver.driver), NULL, (void *)priv,
1953 stmmac_associate_phy)) {
1954 pr_err("No PHY device is associated with this MAC!\n");
1955 ret = -ENODEV;
1956 goto out;
1957 }
1958
1959 priv->fix_mac_speed = plat_dat->fix_mac_speed;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001960 priv->bus_setup = plat_dat->bus_setup;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001961 priv->bsp_priv = plat_dat->bsp_priv;
1962
1963 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
1964 "\tIO base addr: 0x%08x)\n", ndev->name, pdev->name,
1965 pdev->id, ndev->irq, (unsigned int)addr);
1966
1967 /* MDIO bus Registration */
1968 pr_debug("\tMDIO bus (id: %d)...", priv->bus_id);
1969 ret = stmmac_mdio_register(ndev);
1970 if (ret < 0)
1971 goto out;
1972 pr_debug("registered!\n");
1973
1974out:
1975 if (ret < 0) {
1976 platform_set_drvdata(pdev, NULL);
1977 release_mem_region(res->start, (res->end - res->start));
1978 if (addr != NULL)
1979 iounmap(addr);
1980 }
1981
1982 return ret;
1983}
1984
1985/**
1986 * stmmac_dvr_remove
1987 * @pdev: platform device pointer
1988 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1989 * changes the link status, releases the DMA descriptor rings,
1990 * unregisters the MDIO bus and unmaps the allocated memory.
1991 */
1992static int stmmac_dvr_remove(struct platform_device *pdev)
1993{
1994 struct net_device *ndev = platform_get_drvdata(pdev);
1995 struct resource *res;
1996
1997 pr_info("%s:\n\tremoving driver", __func__);
1998
1999 stmmac_dma_stop_rx(ndev->base_addr);
2000 stmmac_dma_stop_tx(ndev->base_addr);
2001
2002 stmmac_mac_disable_rx(ndev->base_addr);
2003 stmmac_mac_disable_tx(ndev->base_addr);
2004
2005 netif_carrier_off(ndev);
2006
2007 stmmac_mdio_unregister(ndev);
2008
2009 platform_set_drvdata(pdev, NULL);
2010 unregister_netdev(ndev);
2011
2012 iounmap((void *)ndev->base_addr);
2013 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2014 release_mem_region(res->start, (res->end - res->start));
2015
2016 free_netdev(ndev);
2017
2018 return 0;
2019}
2020
2021#ifdef CONFIG_PM
2022static int stmmac_suspend(struct platform_device *pdev, pm_message_t state)
2023{
2024 struct net_device *dev = platform_get_drvdata(pdev);
2025 struct stmmac_priv *priv = netdev_priv(dev);
2026 int dis_ic = 0;
2027
2028 if (!dev || !netif_running(dev))
2029 return 0;
2030
2031 spin_lock(&priv->lock);
2032
2033 if (state.event == PM_EVENT_SUSPEND) {
2034 netif_device_detach(dev);
2035 netif_stop_queue(dev);
2036 if (priv->phydev)
2037 phy_stop(priv->phydev);
2038
2039#ifdef CONFIG_STMMAC_TIMER
2040 priv->tm->timer_stop();
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00002041 if (likely(priv->tm->enable))
2042 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002043#endif
2044 napi_disable(&priv->napi);
2045
2046 /* Stop TX/RX DMA */
2047 stmmac_dma_stop_tx(dev->base_addr);
2048 stmmac_dma_stop_rx(dev->base_addr);
2049 /* Clear the Rx/Tx descriptors */
2050 priv->mac_type->ops->init_rx_desc(priv->dma_rx,
2051 priv->dma_rx_size, dis_ic);
2052 priv->mac_type->ops->init_tx_desc(priv->dma_tx,
2053 priv->dma_tx_size);
2054
2055 stmmac_mac_disable_tx(dev->base_addr);
2056
2057 if (device_may_wakeup(&(pdev->dev))) {
2058 /* Enable Power down mode by programming the PMT regs */
2059 if (priv->wolenabled == PMT_SUPPORTED)
2060 priv->mac_type->ops->pmt(dev->base_addr,
2061 priv->wolopts);
2062 } else {
2063 stmmac_mac_disable_rx(dev->base_addr);
2064 }
2065 } else {
2066 priv->shutdown = 1;
2067 /* Although this can appear slightly redundant it actually
2068 * makes fast the standby operation and guarantees the driver
2069 * working if hibernation is on media. */
2070 stmmac_release(dev);
2071 }
2072
2073 spin_unlock(&priv->lock);
2074 return 0;
2075}
2076
2077static int stmmac_resume(struct platform_device *pdev)
2078{
2079 struct net_device *dev = platform_get_drvdata(pdev);
2080 struct stmmac_priv *priv = netdev_priv(dev);
2081 unsigned long ioaddr = dev->base_addr;
2082
2083 if (!netif_running(dev))
2084 return 0;
2085
2086 spin_lock(&priv->lock);
2087
2088 if (priv->shutdown) {
2089 /* Re-open the interface and re-init the MAC/DMA
2090 and the rings. */
2091 stmmac_open(dev);
2092 goto out_resume;
2093 }
2094
2095 /* Power Down bit, into the PM register, is cleared
2096 * automatically as soon as a magic packet or a Wake-up frame
2097 * is received. Anyway, it's better to manually clear
2098 * this bit because it can generate problems while resuming
2099 * from another devices (e.g. serial console). */
2100 if (device_may_wakeup(&(pdev->dev)))
2101 if (priv->wolenabled == PMT_SUPPORTED)
2102 priv->mac_type->ops->pmt(dev->base_addr, 0);
2103
2104 netif_device_attach(dev);
2105
2106 /* Enable the MAC and DMA */
2107 stmmac_mac_enable_rx(ioaddr);
2108 stmmac_mac_enable_tx(ioaddr);
2109 stmmac_dma_start_tx(ioaddr);
2110 stmmac_dma_start_rx(ioaddr);
2111
2112#ifdef CONFIG_STMMAC_TIMER
2113 priv->tm->timer_start(tmrate);
2114#endif
2115 napi_enable(&priv->napi);
2116
2117 if (priv->phydev)
2118 phy_start(priv->phydev);
2119
2120 netif_start_queue(dev);
2121
2122out_resume:
2123 spin_unlock(&priv->lock);
2124 return 0;
2125}
2126#endif
2127
2128static struct platform_driver stmmac_driver = {
2129 .driver = {
2130 .name = STMMAC_RESOURCE_NAME,
2131 },
2132 .probe = stmmac_dvr_probe,
2133 .remove = stmmac_dvr_remove,
2134#ifdef CONFIG_PM
2135 .suspend = stmmac_suspend,
2136 .resume = stmmac_resume,
2137#endif
2138
2139};
2140
2141/**
2142 * stmmac_init_module - Entry point for the driver
2143 * Description: This function is the entry point for the driver.
2144 */
2145static int __init stmmac_init_module(void)
2146{
2147 int ret;
2148
2149 if (platform_driver_register(&stmmacphy_driver)) {
2150 pr_err("No PHY devices registered!\n");
2151 return -ENODEV;
2152 }
2153
2154 ret = platform_driver_register(&stmmac_driver);
2155 return ret;
2156}
2157
2158/**
2159 * stmmac_cleanup_module - Cleanup routine for the driver
2160 * Description: This function is the cleanup routine for the driver.
2161 */
2162static void __exit stmmac_cleanup_module(void)
2163{
2164 platform_driver_unregister(&stmmacphy_driver);
2165 platform_driver_unregister(&stmmac_driver);
2166}
2167
2168#ifndef MODULE
2169static int __init stmmac_cmdline_opt(char *str)
2170{
2171 char *opt;
2172
2173 if (!str || !*str)
2174 return -EINVAL;
2175 while ((opt = strsep(&str, ",")) != NULL) {
2176 if (!strncmp(opt, "debug:", 6))
2177 strict_strtoul(opt + 6, 0, (unsigned long *)&debug);
2178 else if (!strncmp(opt, "phyaddr:", 8))
2179 strict_strtoul(opt + 8, 0, (unsigned long *)&phyaddr);
2180 else if (!strncmp(opt, "dma_txsize:", 11))
2181 strict_strtoul(opt + 11, 0,
2182 (unsigned long *)&dma_txsize);
2183 else if (!strncmp(opt, "dma_rxsize:", 11))
2184 strict_strtoul(opt + 11, 0,
2185 (unsigned long *)&dma_rxsize);
2186 else if (!strncmp(opt, "buf_sz:", 7))
2187 strict_strtoul(opt + 7, 0, (unsigned long *)&buf_sz);
2188 else if (!strncmp(opt, "tc:", 3))
2189 strict_strtoul(opt + 3, 0, (unsigned long *)&tc);
2190 else if (!strncmp(opt, "tx_coe:", 7))
2191 strict_strtoul(opt + 7, 0, (unsigned long *)&tx_coe);
2192 else if (!strncmp(opt, "watchdog:", 9))
2193 strict_strtoul(opt + 9, 0, (unsigned long *)&watchdog);
2194 else if (!strncmp(opt, "flow_ctrl:", 10))
2195 strict_strtoul(opt + 10, 0,
2196 (unsigned long *)&flow_ctrl);
2197 else if (!strncmp(opt, "pause:", 6))
2198 strict_strtoul(opt + 6, 0, (unsigned long *)&pause);
2199#ifdef CONFIG_STMMAC_TIMER
2200 else if (!strncmp(opt, "tmrate:", 7))
2201 strict_strtoul(opt + 7, 0, (unsigned long *)&tmrate);
2202#endif
2203 }
2204 return 0;
2205}
2206
2207__setup("stmmaceth=", stmmac_cmdline_opt);
2208#endif
2209
2210module_init(stmmac_init_module);
2211module_exit(stmmac_cleanup_module);
2212
2213MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
2214MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2215MODULE_LICENSE("GPL");