blob: d0fbc5477d10e72563e8dc679544fdad2b39c056 [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>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070035#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>
Jiri Pirko01789342011-08-16 06:29:00 +000045#include <linux/if.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070046#include <linux/if_vlan.h>
47#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040049#include <linux/prefetch.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070050#include "stmmac.h"
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000051#ifdef CONFIG_STMMAC_DEBUG_FS
52#include <linux/debugfs.h>
53#include <linux/seq_file.h>
54#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070055
56#define STMMAC_RESOURCE_NAME "stmmaceth"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070057
58#undef STMMAC_DEBUG
59/*#define STMMAC_DEBUG*/
60#ifdef STMMAC_DEBUG
61#define DBG(nlevel, klevel, fmt, args...) \
62 ((void)(netif_msg_##nlevel(priv) && \
63 printk(KERN_##klevel fmt, ## args)))
64#else
65#define DBG(nlevel, klevel, fmt, args...) do { } while (0)
66#endif
67
68#undef STMMAC_RX_DEBUG
69/*#define STMMAC_RX_DEBUG*/
70#ifdef STMMAC_RX_DEBUG
71#define RX_DBG(fmt, args...) printk(fmt, ## args)
72#else
73#define RX_DBG(fmt, args...) do { } while (0)
74#endif
75
76#undef STMMAC_XMIT_DEBUG
77/*#define STMMAC_XMIT_DEBUG*/
78#ifdef STMMAC_TX_DEBUG
79#define TX_DBG(fmt, args...) printk(fmt, ## args)
80#else
81#define TX_DBG(fmt, args...) do { } while (0)
82#endif
83
84#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
85#define JUMBO_LEN 9000
86
87/* Module parameters */
88#define TX_TIMEO 5000 /* default 5 seconds */
89static int watchdog = TX_TIMEO;
90module_param(watchdog, int, S_IRUGO | S_IWUSR);
91MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
92
93static int debug = -1; /* -1: default, 0: no output, 16: all */
94module_param(debug, int, S_IRUGO | S_IWUSR);
95MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
96
97static int phyaddr = -1;
98module_param(phyaddr, int, S_IRUGO);
99MODULE_PARM_DESC(phyaddr, "Physical device address");
100
101#define DMA_TX_SIZE 256
102static int dma_txsize = DMA_TX_SIZE;
103module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
105
106#define DMA_RX_SIZE 256
107static int dma_rxsize = DMA_RX_SIZE;
108module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
109MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
110
111static int flow_ctrl = FLOW_OFF;
112module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
113MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
114
115static int pause = PAUSE_TIME;
116module_param(pause, int, S_IRUGO | S_IWUSR);
117MODULE_PARM_DESC(pause, "Flow Control Pause Time");
118
119#define TC_DEFAULT 64
120static int tc = TC_DEFAULT;
121module_param(tc, int, S_IRUGO | S_IWUSR);
122MODULE_PARM_DESC(tc, "DMA threshold control value");
123
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700124/* Pay attention to tune this parameter; take care of both
125 * hardware capability and network stabitily/performance impact.
126 * Many tests showed that ~4ms latency seems to be good enough. */
127#ifdef CONFIG_STMMAC_TIMER
128#define DEFAULT_PERIODIC_RATE 256
129static int tmrate = DEFAULT_PERIODIC_RATE;
130module_param(tmrate, int, S_IRUGO | S_IWUSR);
131MODULE_PARM_DESC(tmrate, "External timer freq. (default: 256Hz)");
132#endif
133
134#define DMA_BUFFER_SIZE BUF_SIZE_2KiB
135static int buf_sz = DMA_BUFFER_SIZE;
136module_param(buf_sz, int, S_IRUGO | S_IWUSR);
137MODULE_PARM_DESC(buf_sz, "DMA buffer size");
138
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700139static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
140 NETIF_MSG_LINK | NETIF_MSG_IFUP |
141 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
142
143static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700144
145/**
146 * stmmac_verify_args - verify the driver parameters.
147 * Description: it verifies if some wrong parameter is passed to the driver.
148 * Note that wrong parameters are replaced with the default values.
149 */
150static void stmmac_verify_args(void)
151{
152 if (unlikely(watchdog < 0))
153 watchdog = TX_TIMEO;
154 if (unlikely(dma_rxsize < 0))
155 dma_rxsize = DMA_RX_SIZE;
156 if (unlikely(dma_txsize < 0))
157 dma_txsize = DMA_TX_SIZE;
158 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
159 buf_sz = DMA_BUFFER_SIZE;
160 if (unlikely(flow_ctrl > 1))
161 flow_ctrl = FLOW_AUTO;
162 else if (likely(flow_ctrl < 0))
163 flow_ctrl = FLOW_OFF;
164 if (unlikely((pause < 0) || (pause > 0xffff)))
165 pause = PAUSE_TIME;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700166}
167
168#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
169static void print_pkt(unsigned char *buf, int len)
170{
171 int j;
172 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
173 for (j = 0; j < len; j++) {
174 if ((j % 16) == 0)
175 pr_info("\n %03x:", j);
176 pr_info(" %02x", buf[j]);
177 }
178 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700179}
180#endif
181
182/* minimum number of free TX descriptors required to wake up TX process */
183#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
184
185static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
186{
187 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
188}
189
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000190/* On some ST platforms, some HW system configuraton registers have to be
191 * set according to the link speed negotiated.
192 */
193static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
194{
195 struct phy_device *phydev = priv->phydev;
196
197 if (likely(priv->plat->fix_mac_speed))
198 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
199 phydev->speed);
200}
201
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700202/**
203 * stmmac_adjust_link
204 * @dev: net device structure
205 * Description: it adjusts the link parameters.
206 */
207static void stmmac_adjust_link(struct net_device *dev)
208{
209 struct stmmac_priv *priv = netdev_priv(dev);
210 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700211 unsigned long flags;
212 int new_state = 0;
213 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
214
215 if (phydev == NULL)
216 return;
217
218 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
219 phydev->addr, phydev->link);
220
221 spin_lock_irqsave(&priv->lock, flags);
222 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000223 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700224
225 /* Now we make sure that we can be in full duplex mode.
226 * If not, we operate in half-duplex mode. */
227 if (phydev->duplex != priv->oldduplex) {
228 new_state = 1;
229 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000230 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700231 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000232 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700233 priv->oldduplex = phydev->duplex;
234 }
235 /* Flow Control operation */
236 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000237 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000238 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700239
240 if (phydev->speed != priv->speed) {
241 new_state = 1;
242 switch (phydev->speed) {
243 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000244 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000245 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000246 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700247 break;
248 case 100:
249 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000250 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000251 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700252 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000253 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700254 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000255 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700256 }
257 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000258 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700259 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000260 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700261 break;
262 default:
263 if (netif_msg_link(priv))
264 pr_warning("%s: Speed (%d) is not 10"
265 " or 100!\n", dev->name, phydev->speed);
266 break;
267 }
268
269 priv->speed = phydev->speed;
270 }
271
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000272 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700273
274 if (!priv->oldlink) {
275 new_state = 1;
276 priv->oldlink = 1;
277 }
278 } else if (priv->oldlink) {
279 new_state = 1;
280 priv->oldlink = 0;
281 priv->speed = 0;
282 priv->oldduplex = -1;
283 }
284
285 if (new_state && netif_msg_link(priv))
286 phy_print_status(phydev);
287
288 spin_unlock_irqrestore(&priv->lock, flags);
289
290 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
291}
292
293/**
294 * stmmac_init_phy - PHY initialization
295 * @dev: net device structure
296 * Description: it initializes the driver's PHY state, and attaches the PHY
297 * to the mac driver.
298 * Return value:
299 * 0 on success
300 */
301static int stmmac_init_phy(struct net_device *dev)
302{
303 struct stmmac_priv *priv = netdev_priv(dev);
304 struct phy_device *phydev;
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000305 char phy_id[MII_BUS_ID_SIZE + 3];
306 char bus_id[MII_BUS_ID_SIZE];
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700307
308 priv->oldlink = 0;
309 priv->speed = 0;
310 priv->oldduplex = -1;
311
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000312 snprintf(bus_id, MII_BUS_ID_SIZE, "%x", priv->plat->bus_id);
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000313 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000314 priv->plat->phy_addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700315 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
316
317 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000318 priv->plat->interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700319
320 if (IS_ERR(phydev)) {
321 pr_err("%s: Could not attach to PHY\n", dev->name);
322 return PTR_ERR(phydev);
323 }
324
325 /*
326 * Broken HW is sometimes missing the pull-up resistor on the
327 * MDIO line, which results in reads to non-existent devices returning
328 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
329 * device as well.
330 * Note: phydev->phy_id is the result of reading the UID PHY registers.
331 */
332 if (phydev->phy_id == 0) {
333 phy_disconnect(phydev);
334 return -ENODEV;
335 }
336 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000337 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700338
339 priv->phydev = phydev;
340
341 return 0;
342}
343
avisconti19449bf2010-10-25 18:58:14 +0000344static inline void stmmac_enable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700345{
346 u32 value = readl(ioaddr + MAC_CTRL_REG);
avisconti19449bf2010-10-25 18:58:14 +0000347
348 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700349 writel(value, ioaddr + MAC_CTRL_REG);
350}
351
avisconti19449bf2010-10-25 18:58:14 +0000352static inline void stmmac_disable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700353{
354 u32 value = readl(ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700355
avisconti19449bf2010-10-25 18:58:14 +0000356 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700357 writel(value, ioaddr + MAC_CTRL_REG);
358}
359
360/**
361 * display_ring
362 * @p: pointer to the ring.
363 * @size: size of the ring.
364 * Description: display all the descriptors within the ring.
365 */
366static void display_ring(struct dma_desc *p, int size)
367{
368 struct tmp_s {
369 u64 a;
370 unsigned int b;
371 unsigned int c;
372 };
373 int i;
374 for (i = 0; i < size; i++) {
375 struct tmp_s *x = (struct tmp_s *)(p + i);
376 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
377 i, (unsigned int)virt_to_phys(&p[i]),
378 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
379 x->b, x->c);
380 pr_info("\n");
381 }
382}
383
384/**
385 * init_dma_desc_rings - init the RX/TX descriptor rings
386 * @dev: net device structure
387 * Description: this function initializes the DMA RX/TX descriptors
388 * and allocates the socket buffers.
389 */
390static void init_dma_desc_rings(struct net_device *dev)
391{
392 int i;
393 struct stmmac_priv *priv = netdev_priv(dev);
394 struct sk_buff *skb;
395 unsigned int txsize = priv->dma_tx_size;
396 unsigned int rxsize = priv->dma_rx_size;
397 unsigned int bfsize = priv->dma_buf_sz;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000398 int buff2_needed = 0, dis_ic = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700399
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700400 /* Set the Buffer size according to the MTU;
401 * indeed, in case of jumbo we need to bump-up the buffer sizes.
402 */
403 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
404 bfsize = BUF_SIZE_16KiB;
405 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
406 bfsize = BUF_SIZE_8KiB;
407 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
408 bfsize = BUF_SIZE_4KiB;
409 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
410 bfsize = BUF_SIZE_2KiB;
411 else
412 bfsize = DMA_BUFFER_SIZE;
413
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000414#ifdef CONFIG_STMMAC_TIMER
415 /* Disable interrupts on completion for the reception if timer is on */
416 if (likely(priv->tm->enable))
417 dis_ic = 1;
418#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700419 /* If the MTU exceeds 8k so use the second buffer in the chain */
420 if (bfsize >= BUF_SIZE_8KiB)
421 buff2_needed = 1;
422
423 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
424 txsize, rxsize, bfsize);
425
426 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
427 priv->rx_skbuff =
428 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
429 priv->dma_rx =
430 (struct dma_desc *)dma_alloc_coherent(priv->device,
431 rxsize *
432 sizeof(struct dma_desc),
433 &priv->dma_rx_phy,
434 GFP_KERNEL);
435 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
436 GFP_KERNEL);
437 priv->dma_tx =
438 (struct dma_desc *)dma_alloc_coherent(priv->device,
439 txsize *
440 sizeof(struct dma_desc),
441 &priv->dma_tx_phy,
442 GFP_KERNEL);
443
444 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
445 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
446 return;
447 }
448
449 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
450 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
451 dev->name, priv->dma_rx, priv->dma_tx,
452 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
453
454 /* RX INITIALIZATION */
455 DBG(probe, INFO, "stmmac: SKB addresses:\n"
456 "skb\t\tskb data\tdma data\n");
457
458 for (i = 0; i < rxsize; i++) {
459 struct dma_desc *p = priv->dma_rx + i;
460
461 skb = netdev_alloc_skb_ip_align(dev, bfsize);
462 if (unlikely(skb == NULL)) {
463 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
464 break;
465 }
466 priv->rx_skbuff[i] = skb;
467 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
468 bfsize, DMA_FROM_DEVICE);
469
470 p->des2 = priv->rx_skbuff_dma[i];
471 if (unlikely(buff2_needed))
472 p->des3 = p->des2 + BUF_SIZE_8KiB;
473 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
474 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
475 }
476 priv->cur_rx = 0;
477 priv->dirty_rx = (unsigned int)(i - rxsize);
478 priv->dma_buf_sz = bfsize;
479 buf_sz = bfsize;
480
481 /* TX INITIALIZATION */
482 for (i = 0; i < txsize; i++) {
483 priv->tx_skbuff[i] = NULL;
484 priv->dma_tx[i].des2 = 0;
485 }
486 priv->dirty_tx = 0;
487 priv->cur_tx = 0;
488
489 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000490 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
491 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700492
493 if (netif_msg_hw(priv)) {
494 pr_info("RX descriptor ring:\n");
495 display_ring(priv->dma_rx, rxsize);
496 pr_info("TX descriptor ring:\n");
497 display_ring(priv->dma_tx, txsize);
498 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700499}
500
501static void dma_free_rx_skbufs(struct stmmac_priv *priv)
502{
503 int i;
504
505 for (i = 0; i < priv->dma_rx_size; i++) {
506 if (priv->rx_skbuff[i]) {
507 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
508 priv->dma_buf_sz, DMA_FROM_DEVICE);
509 dev_kfree_skb_any(priv->rx_skbuff[i]);
510 }
511 priv->rx_skbuff[i] = NULL;
512 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700513}
514
515static void dma_free_tx_skbufs(struct stmmac_priv *priv)
516{
517 int i;
518
519 for (i = 0; i < priv->dma_tx_size; i++) {
520 if (priv->tx_skbuff[i] != NULL) {
521 struct dma_desc *p = priv->dma_tx + i;
522 if (p->des2)
523 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000524 priv->hw->desc->get_tx_len(p),
525 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700526 dev_kfree_skb_any(priv->tx_skbuff[i]);
527 priv->tx_skbuff[i] = NULL;
528 }
529 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700530}
531
532static void free_dma_desc_resources(struct stmmac_priv *priv)
533{
534 /* Release the DMA TX/RX socket buffers */
535 dma_free_rx_skbufs(priv);
536 dma_free_tx_skbufs(priv);
537
538 /* Free the region of consistent memory previously allocated for
539 * the DMA */
540 dma_free_coherent(priv->device,
541 priv->dma_tx_size * sizeof(struct dma_desc),
542 priv->dma_tx, priv->dma_tx_phy);
543 dma_free_coherent(priv->device,
544 priv->dma_rx_size * sizeof(struct dma_desc),
545 priv->dma_rx, priv->dma_rx_phy);
546 kfree(priv->rx_skbuff_dma);
547 kfree(priv->rx_skbuff);
548 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700549}
550
551/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700552 * stmmac_dma_operation_mode - HW DMA operation mode
553 * @priv : pointer to the private device structure.
554 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000555 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700556 */
557static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
558{
Srinivas Kandagatla61b80132011-07-17 20:54:09 +0000559 if (likely(priv->plat->force_sf_dma_mode ||
560 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
561 /*
562 * In case of GMAC, SF mode can be enabled
563 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000564 * 1) TX COE if actually supported
565 * 2) There is no bugged Jumbo frame support
566 * that needs to not insert csum in the TDES.
567 */
568 priv->hw->dma->dma_mode(priv->ioaddr,
569 SF_DMA_MODE, SF_DMA_MODE);
570 tc = SF_DMA_MODE;
571 } else
572 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700573}
574
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700575/**
576 * stmmac_tx:
577 * @priv: private driver structure
578 * Description: it reclaims resources after transmission completes.
579 */
580static void stmmac_tx(struct stmmac_priv *priv)
581{
582 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700583
584 while (priv->dirty_tx != priv->cur_tx) {
585 int last;
586 unsigned int entry = priv->dirty_tx % txsize;
587 struct sk_buff *skb = priv->tx_skbuff[entry];
588 struct dma_desc *p = priv->dma_tx + entry;
589
590 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000591 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700592 break;
593
594 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000595 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700596 if (likely(last)) {
597 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000598 priv->hw->desc->tx_status(&priv->dev->stats,
599 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000600 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700601 if (likely(tx_error == 0)) {
602 priv->dev->stats.tx_packets++;
603 priv->xstats.tx_pkt_n++;
604 } else
605 priv->dev->stats.tx_errors++;
606 }
607 TX_DBG("%s: curr %d, dirty %d\n", __func__,
608 priv->cur_tx, priv->dirty_tx);
609
610 if (likely(p->des2))
611 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000612 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700613 DMA_TO_DEVICE);
614 if (unlikely(p->des3))
615 p->des3 = 0;
616
617 if (likely(skb != NULL)) {
618 /*
619 * If there's room in the queue (limit it to size)
620 * we add this skb back into the pool,
621 * if it's the right size.
622 */
623 if ((skb_queue_len(&priv->rx_recycle) <
624 priv->dma_rx_size) &&
625 skb_recycle_check(skb, priv->dma_buf_sz))
626 __skb_queue_head(&priv->rx_recycle, skb);
627 else
628 dev_kfree_skb(skb);
629
630 priv->tx_skbuff[entry] = NULL;
631 }
632
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000633 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700634
635 entry = (++priv->dirty_tx) % txsize;
636 }
637 if (unlikely(netif_queue_stopped(priv->dev) &&
638 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
639 netif_tx_lock(priv->dev);
640 if (netif_queue_stopped(priv->dev) &&
641 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
642 TX_DBG("%s: restart transmit\n", __func__);
643 netif_wake_queue(priv->dev);
644 }
645 netif_tx_unlock(priv->dev);
646 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700647}
648
649static inline void stmmac_enable_irq(struct stmmac_priv *priv)
650{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000651#ifdef CONFIG_STMMAC_TIMER
652 if (likely(priv->tm->enable))
653 priv->tm->timer_start(tmrate);
654 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700655#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000656 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700657}
658
659static inline void stmmac_disable_irq(struct stmmac_priv *priv)
660{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000661#ifdef CONFIG_STMMAC_TIMER
662 if (likely(priv->tm->enable))
663 priv->tm->timer_stop();
664 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700665#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000666 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700667}
668
669static int stmmac_has_work(struct stmmac_priv *priv)
670{
671 unsigned int has_work = 0;
672 int rxret, tx_work = 0;
673
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000674 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700675 (priv->cur_rx % priv->dma_rx_size));
676
677 if (priv->dirty_tx != priv->cur_tx)
678 tx_work = 1;
679
680 if (likely(!rxret || tx_work))
681 has_work = 1;
682
683 return has_work;
684}
685
686static inline void _stmmac_schedule(struct stmmac_priv *priv)
687{
688 if (likely(stmmac_has_work(priv))) {
689 stmmac_disable_irq(priv);
690 napi_schedule(&priv->napi);
691 }
692}
693
694#ifdef CONFIG_STMMAC_TIMER
695void stmmac_schedule(struct net_device *dev)
696{
697 struct stmmac_priv *priv = netdev_priv(dev);
698
699 priv->xstats.sched_timer_n++;
700
701 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700702}
703
704static void stmmac_no_timer_started(unsigned int x)
705{;
706};
707
708static void stmmac_no_timer_stopped(void)
709{;
710};
711#endif
712
713/**
714 * stmmac_tx_err:
715 * @priv: pointer to the private device structure
716 * Description: it cleans the descriptors and restarts the transmission
717 * in case of errors.
718 */
719static void stmmac_tx_err(struct stmmac_priv *priv)
720{
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000721
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700722 netif_stop_queue(priv->dev);
723
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000724 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700725 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000726 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700727 priv->dirty_tx = 0;
728 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000729 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700730
731 priv->dev->stats.tx_errors++;
732 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700733}
734
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000735
736static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700737{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000738 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700739
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000740 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000741 if (likely(status == handle_tx_rx))
742 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700743
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000744 else if (unlikely(status == tx_hard_error_bump_tc)) {
745 /* Try to bump up the dma threshold on this failure */
746 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
747 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000748 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000749 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700750 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000751 } else if (unlikely(status == tx_hard_error))
752 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700753}
754
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000755static void stmmac_mmc_setup(struct stmmac_priv *priv)
756{
757 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
758 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
759
760 /* Do not manage MMC IRQ (FIXME) */
761 dwmac_mmc_intr_all_mask(priv->ioaddr);
762 dwmac_mmc_ctrl(priv->ioaddr, mode);
763 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
764}
765
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000766static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
767{
768 u32 hwid = priv->hw->synopsys_uid;
769
770 /* Only check valid Synopsys Id because old MAC chips
771 * have no HW registers where get the ID */
772 if (likely(hwid)) {
773 u32 uid = ((hwid & 0x0000ff00) >> 8);
774 u32 synid = (hwid & 0x000000ff);
775
776 pr_info("STMMAC - user ID: 0x%x, Synopsys ID: 0x%x\n",
777 uid, synid);
778
779 return synid;
780 }
781 return 0;
782}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000783
784/* New GMAC chips support a new register to indicate the
785 * presence of the optional feature/functions.
786 */
787static int stmmac_get_hw_features(struct stmmac_priv *priv)
788{
789 u32 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
790
791 if (likely(hw_cap)) {
792 priv->dma_cap.mbps_10_100 = (hw_cap & 0x1);
793 priv->dma_cap.mbps_1000 = (hw_cap & 0x2) >> 1;
794 priv->dma_cap.half_duplex = (hw_cap & 0x4) >> 2;
795 priv->dma_cap.hash_filter = (hw_cap & 0x10) >> 4;
796 priv->dma_cap.multi_addr = (hw_cap & 0x20) >> 5;
797 priv->dma_cap.pcs = (hw_cap & 0x40) >> 6;
798 priv->dma_cap.sma_mdio = (hw_cap & 0x100) >> 8;
799 priv->dma_cap.pmt_remote_wake_up = (hw_cap & 0x200) >> 9;
800 priv->dma_cap.pmt_magic_frame = (hw_cap & 0x400) >> 10;
801 priv->dma_cap.rmon = (hw_cap & 0x800) >> 11; /* MMC */
802 /* IEEE 1588-2002*/
803 priv->dma_cap.time_stamp = (hw_cap & 0x1000) >> 12;
804 /* IEEE 1588-2008*/
805 priv->dma_cap.atime_stamp = (hw_cap & 0x2000) >> 13;
806 /* 802.3az - Energy-Efficient Ethernet (EEE) */
807 priv->dma_cap.eee = (hw_cap & 0x4000) >> 14;
808 priv->dma_cap.av = (hw_cap & 0x8000) >> 15;
809 /* TX and RX csum */
810 priv->dma_cap.tx_coe = (hw_cap & 0x10000) >> 16;
811 priv->dma_cap.rx_coe_type1 = (hw_cap & 0x20000) >> 17;
812 priv->dma_cap.rx_coe_type2 = (hw_cap & 0x40000) >> 18;
813 priv->dma_cap.rxfifo_over_2048 = (hw_cap & 0x80000) >> 19;
814 /* TX and RX number of channels */
815 priv->dma_cap.number_rx_channel = (hw_cap & 0x300000) >> 20;
816 priv->dma_cap.number_tx_channel = (hw_cap & 0xc00000) >> 22;
817 /* Alternate (enhanced) DESC mode*/
818 priv->dma_cap.enh_desc = (hw_cap & 0x1000000) >> 24;
819
820 } else
821 pr_debug("\tNo HW DMA feature register supported");
822
823 return hw_cap;
824}
825
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700826/**
827 * stmmac_open - open entry point of the driver
828 * @dev : pointer to the device structure.
829 * Description:
830 * This function is the open entry point of the driver.
831 * Return value:
832 * 0 on success and an appropriate (-)ve integer as defined in errno.h
833 * file on failure.
834 */
835static int stmmac_open(struct net_device *dev)
836{
837 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700838 int ret;
839
840 /* Check that the MAC address is valid. If its not, refuse
841 * to bring the device up. The user must specify an
842 * address using the following linux command:
843 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
844 if (!is_valid_ether_addr(dev->dev_addr)) {
845 random_ether_addr(dev->dev_addr);
846 pr_warning("%s: generated random MAC address %pM\n", dev->name,
847 dev->dev_addr);
848 }
849
850 stmmac_verify_args();
851
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700852#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000853 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700854 if (unlikely(priv->tm == NULL)) {
Frans Pop2381a552010-03-24 07:57:36 +0000855 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700856 return -ENOMEM;
857 }
858 priv->tm->freq = tmrate;
859
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000860 /* Test if the external timer can be actually used.
861 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700862 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000863 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700864 priv->tm->freq = 0;
865 priv->tm->timer_start = stmmac_no_timer_started;
866 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000867 } else
868 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700869#endif
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000870 ret = stmmac_init_phy(dev);
871 if (unlikely(ret)) {
872 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
873 goto open_error;
874 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700875
876 /* Create and initialize the TX/RX descriptors chains. */
877 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
878 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
879 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
880 init_dma_desc_rings(dev);
881
882 /* DMA initialization and SW reset */
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000883 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
884 priv->dma_tx_phy, priv->dma_rx_phy);
885 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700886 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000887 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700888 }
889
890 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000891 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +0000892 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000893 if (priv->plat->bus_setup)
894 priv->plat->bus_setup(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700895 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000896 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700897
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000898 stmmac_get_synopsys_id(priv);
899
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000900 stmmac_get_hw_features(priv);
901
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000902 if (priv->rx_coe)
903 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000904 if (priv->plat->tx_coe)
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000905 pr_info("\tTX Checksum insertion supported\n");
Michał Mirosław5e982f32011-04-09 02:46:55 +0000906 netdev_update_features(dev);
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000907
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000908 /* Request the IRQ lines */
909 ret = request_irq(dev->irq, stmmac_interrupt,
910 IRQF_SHARED, dev->name, dev);
911 if (unlikely(ret < 0)) {
912 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
913 __func__, dev->irq, ret);
914 goto open_error;
915 }
916
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700917 /* Enable the MAC Rx/Tx */
avisconti19449bf2010-10-25 18:58:14 +0000918 stmmac_enable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700919
920 /* Set the HW DMA mode and the COE */
921 stmmac_dma_operation_mode(priv);
922
923 /* Extra statistics */
924 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
925 priv->xstats.threshold = tc;
926
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000927 stmmac_mmc_setup(priv);
928
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700929 /* Start the ball rolling... */
930 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000931 priv->hw->dma->start_tx(priv->ioaddr);
932 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700933
934#ifdef CONFIG_STMMAC_TIMER
935 priv->tm->timer_start(tmrate);
936#endif
937 /* Dump DMA/MAC registers */
938 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000939 priv->hw->mac->dump_regs(priv->ioaddr);
940 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700941 }
942
943 if (priv->phydev)
944 phy_start(priv->phydev);
945
946 napi_enable(&priv->napi);
947 skb_queue_head_init(&priv->rx_recycle);
948 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000949
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700950 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000951
952open_error:
953#ifdef CONFIG_STMMAC_TIMER
954 kfree(priv->tm);
955#endif
956 if (priv->phydev)
957 phy_disconnect(priv->phydev);
958
959 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700960}
961
962/**
963 * stmmac_release - close entry point of the driver
964 * @dev : device pointer.
965 * Description:
966 * This is the stop entry point of the driver.
967 */
968static int stmmac_release(struct net_device *dev)
969{
970 struct stmmac_priv *priv = netdev_priv(dev);
971
972 /* Stop and disconnect the PHY */
973 if (priv->phydev) {
974 phy_stop(priv->phydev);
975 phy_disconnect(priv->phydev);
976 priv->phydev = NULL;
977 }
978
979 netif_stop_queue(dev);
980
981#ifdef CONFIG_STMMAC_TIMER
982 /* Stop and release the timer */
983 stmmac_close_ext_timer();
984 if (priv->tm != NULL)
985 kfree(priv->tm);
986#endif
987 napi_disable(&priv->napi);
988 skb_queue_purge(&priv->rx_recycle);
989
990 /* Free the IRQ lines */
991 free_irq(dev->irq, dev);
992
993 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000994 priv->hw->dma->stop_tx(priv->ioaddr);
995 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700996
997 /* Release and free the Rx/Tx resources */
998 free_dma_desc_resources(priv);
999
avisconti19449bf2010-10-25 18:58:14 +00001000 /* Disable the MAC Rx/Tx */
1001 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001002
1003 netif_carrier_off(dev);
1004
1005 return 0;
1006}
1007
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001008static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
1009 struct net_device *dev,
1010 int csum_insertion)
1011{
1012 struct stmmac_priv *priv = netdev_priv(dev);
1013 unsigned int nopaged_len = skb_headlen(skb);
1014 unsigned int txsize = priv->dma_tx_size;
1015 unsigned int entry = priv->cur_tx % txsize;
1016 struct dma_desc *desc = priv->dma_tx + entry;
1017
1018 if (nopaged_len > BUF_SIZE_8KiB) {
1019
1020 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
1021
1022 desc->des2 = dma_map_single(priv->device, skb->data,
1023 BUF_SIZE_8KiB, DMA_TO_DEVICE);
1024 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001025 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
1026 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001027
1028 entry = (++priv->cur_tx) % txsize;
1029 desc = priv->dma_tx + entry;
1030
1031 desc->des2 = dma_map_single(priv->device,
1032 skb->data + BUF_SIZE_8KiB,
1033 buf2_size, DMA_TO_DEVICE);
1034 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001035 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
1036 csum_insertion);
1037 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001038 priv->tx_skbuff[entry] = NULL;
1039 } else {
1040 desc->des2 = dma_map_single(priv->device, skb->data,
1041 nopaged_len, DMA_TO_DEVICE);
1042 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001043 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1044 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001045 }
1046 return entry;
1047}
1048
1049/**
1050 * stmmac_xmit:
1051 * @skb : the socket buffer
1052 * @dev : device pointer
1053 * Description : Tx entry point of the driver.
1054 */
1055static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1056{
1057 struct stmmac_priv *priv = netdev_priv(dev);
1058 unsigned int txsize = priv->dma_tx_size;
1059 unsigned int entry;
1060 int i, csum_insertion = 0;
1061 int nfrags = skb_shinfo(skb)->nr_frags;
1062 struct dma_desc *desc, *first;
1063
1064 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1065 if (!netif_queue_stopped(dev)) {
1066 netif_stop_queue(dev);
1067 /* This is a hard error, log it. */
1068 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1069 __func__);
1070 }
1071 return NETDEV_TX_BUSY;
1072 }
1073
1074 entry = priv->cur_tx % txsize;
1075
1076#ifdef STMMAC_XMIT_DEBUG
1077 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1078 pr_info("stmmac xmit:\n"
1079 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1080 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1081 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1082 !skb_is_gso(skb) ? "isn't" : "is");
1083#endif
1084
Michał Mirosław5e982f32011-04-09 02:46:55 +00001085 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001086
1087 desc = priv->dma_tx + entry;
1088 first = desc;
1089
1090#ifdef STMMAC_XMIT_DEBUG
1091 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1092 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1093 "\t\tn_frags: %d, ip_summed: %d\n",
1094 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1095#endif
1096 priv->tx_skbuff[entry] = skb;
1097 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1098 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1099 desc = priv->dma_tx + entry;
1100 } else {
1101 unsigned int nopaged_len = skb_headlen(skb);
1102 desc->des2 = dma_map_single(priv->device, skb->data,
1103 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001104 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1105 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001106 }
1107
1108 for (i = 0; i < nfrags; i++) {
1109 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1110 int len = frag->size;
1111
1112 entry = (++priv->cur_tx) % txsize;
1113 desc = priv->dma_tx + entry;
1114
1115 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1116 desc->des2 = dma_map_page(priv->device, frag->page,
1117 frag->page_offset,
1118 len, DMA_TO_DEVICE);
1119 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001120 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001121 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001122 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001123 }
1124
1125 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001126 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001127
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001128#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001129 /* Clean IC while using timer */
1130 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001131 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001132#endif
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001133
1134 wmb();
1135
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001136 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001137 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001138
1139 priv->cur_tx++;
1140
1141#ifdef STMMAC_XMIT_DEBUG
1142 if (netif_msg_pktdata(priv)) {
1143 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1144 "first=%p, nfrags=%d\n",
1145 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1146 entry, first, nfrags);
1147 display_ring(priv->dma_tx, txsize);
1148 pr_info(">>> frame to be transmitted: ");
1149 print_pkt(skb->data, skb->len);
1150 }
1151#endif
1152 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1153 TX_DBG("%s: stop transmitted packets\n", __func__);
1154 netif_stop_queue(dev);
1155 }
1156
1157 dev->stats.tx_bytes += skb->len;
1158
Richard Cochran3e82ce12011-06-12 02:19:06 +00001159 skb_tx_timestamp(skb);
1160
Richard Cochran52f64fa2011-06-19 03:31:43 +00001161 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1162
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001163 return NETDEV_TX_OK;
1164}
1165
1166static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1167{
1168 unsigned int rxsize = priv->dma_rx_size;
1169 int bfsize = priv->dma_buf_sz;
1170 struct dma_desc *p = priv->dma_rx;
1171
1172 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1173 unsigned int entry = priv->dirty_rx % rxsize;
1174 if (likely(priv->rx_skbuff[entry] == NULL)) {
1175 struct sk_buff *skb;
1176
1177 skb = __skb_dequeue(&priv->rx_recycle);
1178 if (skb == NULL)
1179 skb = netdev_alloc_skb_ip_align(priv->dev,
1180 bfsize);
1181
1182 if (unlikely(skb == NULL))
1183 break;
1184
1185 priv->rx_skbuff[entry] = skb;
1186 priv->rx_skbuff_dma[entry] =
1187 dma_map_single(priv->device, skb->data, bfsize,
1188 DMA_FROM_DEVICE);
1189
1190 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001191 if (unlikely(priv->plat->has_gmac)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001192 if (bfsize >= BUF_SIZE_8KiB)
1193 (p + entry)->des3 =
1194 (p + entry)->des2 + BUF_SIZE_8KiB;
1195 }
1196 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1197 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001198 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001199 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001200 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001201}
1202
1203static int stmmac_rx(struct stmmac_priv *priv, int limit)
1204{
1205 unsigned int rxsize = priv->dma_rx_size;
1206 unsigned int entry = priv->cur_rx % rxsize;
1207 unsigned int next_entry;
1208 unsigned int count = 0;
1209 struct dma_desc *p = priv->dma_rx + entry;
1210 struct dma_desc *p_next;
1211
1212#ifdef STMMAC_RX_DEBUG
1213 if (netif_msg_hw(priv)) {
1214 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1215 display_ring(priv->dma_rx, rxsize);
1216 }
1217#endif
1218 count = 0;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001219 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001220 int status;
1221
1222 if (count >= limit)
1223 break;
1224
1225 count++;
1226
1227 next_entry = (++priv->cur_rx) % rxsize;
1228 p_next = priv->dma_rx + next_entry;
1229 prefetch(p_next);
1230
1231 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001232 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1233 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001234 if (unlikely(status == discard_frame))
1235 priv->dev->stats.rx_errors++;
1236 else {
1237 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001238 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001239
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001240 frame_len = priv->hw->desc->get_rx_frame_len(p);
1241 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1242 * Type frames (LLC/LLC-SNAP) */
1243 if (unlikely(status != llc_snap))
1244 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001245#ifdef STMMAC_RX_DEBUG
1246 if (frame_len > ETH_FRAME_LEN)
1247 pr_debug("\tRX frame size %d, COE status: %d\n",
1248 frame_len, status);
1249
1250 if (netif_msg_hw(priv))
1251 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1252 p, entry, p->des2);
1253#endif
1254 skb = priv->rx_skbuff[entry];
1255 if (unlikely(!skb)) {
1256 pr_err("%s: Inconsistent Rx descriptor chain\n",
1257 priv->dev->name);
1258 priv->dev->stats.rx_dropped++;
1259 break;
1260 }
1261 prefetch(skb->data - NET_IP_ALIGN);
1262 priv->rx_skbuff[entry] = NULL;
1263
1264 skb_put(skb, frame_len);
1265 dma_unmap_single(priv->device,
1266 priv->rx_skbuff_dma[entry],
1267 priv->dma_buf_sz, DMA_FROM_DEVICE);
1268#ifdef STMMAC_RX_DEBUG
1269 if (netif_msg_pktdata(priv)) {
1270 pr_info(" frame received (%dbytes)", frame_len);
1271 print_pkt(skb->data, frame_len);
1272 }
1273#endif
1274 skb->protocol = eth_type_trans(skb, priv->dev);
1275
1276 if (unlikely(status == csum_none)) {
1277 /* always for the old mac 10/100 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001278 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001279 netif_receive_skb(skb);
1280 } else {
1281 skb->ip_summed = CHECKSUM_UNNECESSARY;
1282 napi_gro_receive(&priv->napi, skb);
1283 }
1284
1285 priv->dev->stats.rx_packets++;
1286 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001287 }
1288 entry = next_entry;
1289 p = p_next; /* use prefetched values */
1290 }
1291
1292 stmmac_rx_refill(priv);
1293
1294 priv->xstats.rx_pkt_n += count;
1295
1296 return count;
1297}
1298
1299/**
1300 * stmmac_poll - stmmac poll method (NAPI)
1301 * @napi : pointer to the napi structure.
1302 * @budget : maximum number of packets that the current CPU can receive from
1303 * all interfaces.
1304 * Description :
1305 * This function implements the the reception process.
1306 * Also it runs the TX completion thread
1307 */
1308static int stmmac_poll(struct napi_struct *napi, int budget)
1309{
1310 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1311 int work_done = 0;
1312
1313 priv->xstats.poll_n++;
1314 stmmac_tx(priv);
1315 work_done = stmmac_rx(priv, budget);
1316
1317 if (work_done < budget) {
1318 napi_complete(napi);
1319 stmmac_enable_irq(priv);
1320 }
1321 return work_done;
1322}
1323
1324/**
1325 * stmmac_tx_timeout
1326 * @dev : Pointer to net device structure
1327 * Description: this function is called when a packet transmission fails to
1328 * complete within a reasonable tmrate. The driver will mark the error in the
1329 * netdev structure and arrange for the device to be reset to a sane state
1330 * in order to transmit a new packet.
1331 */
1332static void stmmac_tx_timeout(struct net_device *dev)
1333{
1334 struct stmmac_priv *priv = netdev_priv(dev);
1335
1336 /* Clear Tx resources and restart transmitting again */
1337 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001338}
1339
1340/* Configuration changes (passed on by ifconfig) */
1341static int stmmac_config(struct net_device *dev, struct ifmap *map)
1342{
1343 if (dev->flags & IFF_UP) /* can't act on a running interface */
1344 return -EBUSY;
1345
1346 /* Don't allow changing the I/O address */
1347 if (map->base_addr != dev->base_addr) {
1348 pr_warning("%s: can't change I/O address\n", dev->name);
1349 return -EOPNOTSUPP;
1350 }
1351
1352 /* Don't allow changing the IRQ */
1353 if (map->irq != dev->irq) {
1354 pr_warning("%s: can't change IRQ number %d\n",
1355 dev->name, dev->irq);
1356 return -EOPNOTSUPP;
1357 }
1358
1359 /* ignore other fields */
1360 return 0;
1361}
1362
1363/**
Jiri Pirko01789342011-08-16 06:29:00 +00001364 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001365 * @dev : pointer to the device structure
1366 * Description:
1367 * This function is a driver entry point which gets called by the kernel
1368 * whenever multicast addresses must be enabled/disabled.
1369 * Return value:
1370 * void.
1371 */
Jiri Pirko01789342011-08-16 06:29:00 +00001372static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001373{
1374 struct stmmac_priv *priv = netdev_priv(dev);
1375
1376 spin_lock(&priv->lock);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001377 priv->hw->mac->set_filter(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001378 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001379}
1380
1381/**
1382 * stmmac_change_mtu - entry point to change MTU size for the device.
1383 * @dev : device pointer.
1384 * @new_mtu : the new MTU size for the device.
1385 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1386 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1387 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1388 * Return value:
1389 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1390 * file on failure.
1391 */
1392static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1393{
1394 struct stmmac_priv *priv = netdev_priv(dev);
1395 int max_mtu;
1396
1397 if (netif_running(dev)) {
1398 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1399 return -EBUSY;
1400 }
1401
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001402 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001403 max_mtu = JUMBO_LEN;
1404 else
1405 max_mtu = ETH_DATA_LEN;
1406
1407 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1408 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1409 return -EINVAL;
1410 }
1411
Michał Mirosław5e982f32011-04-09 02:46:55 +00001412 dev->mtu = new_mtu;
1413 netdev_update_features(dev);
1414
1415 return 0;
1416}
1417
1418static u32 stmmac_fix_features(struct net_device *dev, u32 features)
1419{
1420 struct stmmac_priv *priv = netdev_priv(dev);
1421
1422 if (!priv->rx_coe)
1423 features &= ~NETIF_F_RXCSUM;
1424 if (!priv->plat->tx_coe)
1425 features &= ~NETIF_F_ALL_CSUM;
1426
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001427 /* Some GMAC devices have a bugged Jumbo frame support that
1428 * needs to have the Tx COE disabled for oversized frames
1429 * (due to limited buffer sizes). In this case we disable
1430 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001431 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1432 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001433
Michał Mirosław5e982f32011-04-09 02:46:55 +00001434 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001435}
1436
1437static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1438{
1439 struct net_device *dev = (struct net_device *)dev_id;
1440 struct stmmac_priv *priv = netdev_priv(dev);
1441
1442 if (unlikely(!dev)) {
1443 pr_err("%s: invalid dev pointer\n", __func__);
1444 return IRQ_NONE;
1445 }
1446
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001447 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001448 /* To handle GMAC own interrupts */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001449 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001450
1451 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001452
1453 return IRQ_HANDLED;
1454}
1455
1456#ifdef CONFIG_NET_POLL_CONTROLLER
1457/* Polling receive - used by NETCONSOLE and other diagnostic tools
1458 * to allow network I/O with interrupts disabled. */
1459static void stmmac_poll_controller(struct net_device *dev)
1460{
1461 disable_irq(dev->irq);
1462 stmmac_interrupt(dev->irq, dev);
1463 enable_irq(dev->irq);
1464}
1465#endif
1466
1467/**
1468 * stmmac_ioctl - Entry point for the Ioctl
1469 * @dev: Device pointer.
1470 * @rq: An IOCTL specefic structure, that can contain a pointer to
1471 * a proprietary structure used to pass information to the driver.
1472 * @cmd: IOCTL command
1473 * Description:
1474 * Currently there are no special functionality supported in IOCTL, just the
1475 * phy_mii_ioctl(...) can be invoked.
1476 */
1477static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1478{
1479 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001480 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001481
1482 if (!netif_running(dev))
1483 return -EINVAL;
1484
Richard Cochran28b04112010-07-17 08:48:55 +00001485 if (!priv->phydev)
1486 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001487
Richard Cochran28b04112010-07-17 08:48:55 +00001488 spin_lock(&priv->lock);
1489 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1490 spin_unlock(&priv->lock);
1491
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001492 return ret;
1493}
1494
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001495#ifdef CONFIG_STMMAC_DEBUG_FS
1496static struct dentry *stmmac_fs_dir;
1497static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001498static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001499
1500static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
1501{
1502 struct tmp_s {
1503 u64 a;
1504 unsigned int b;
1505 unsigned int c;
1506 };
1507 int i;
1508 struct net_device *dev = seq->private;
1509 struct stmmac_priv *priv = netdev_priv(dev);
1510
1511 seq_printf(seq, "=======================\n");
1512 seq_printf(seq, " RX descriptor ring\n");
1513 seq_printf(seq, "=======================\n");
1514
1515 for (i = 0; i < priv->dma_rx_size; i++) {
1516 struct tmp_s *x = (struct tmp_s *)(priv->dma_rx + i);
1517 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1518 i, (unsigned int)(x->a),
1519 (unsigned int)((x->a) >> 32), x->b, x->c);
1520 seq_printf(seq, "\n");
1521 }
1522
1523 seq_printf(seq, "\n");
1524 seq_printf(seq, "=======================\n");
1525 seq_printf(seq, " TX descriptor ring\n");
1526 seq_printf(seq, "=======================\n");
1527
1528 for (i = 0; i < priv->dma_tx_size; i++) {
1529 struct tmp_s *x = (struct tmp_s *)(priv->dma_tx + i);
1530 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1531 i, (unsigned int)(x->a),
1532 (unsigned int)((x->a) >> 32), x->b, x->c);
1533 seq_printf(seq, "\n");
1534 }
1535
1536 return 0;
1537}
1538
1539static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
1540{
1541 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
1542}
1543
1544static const struct file_operations stmmac_rings_status_fops = {
1545 .owner = THIS_MODULE,
1546 .open = stmmac_sysfs_ring_open,
1547 .read = seq_read,
1548 .llseek = seq_lseek,
1549 .release = seq_release,
1550};
1551
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001552static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
1553{
1554 struct net_device *dev = seq->private;
1555 struct stmmac_priv *priv = netdev_priv(dev);
1556
1557 if (!stmmac_get_hw_features(priv)) {
1558 seq_printf(seq, "DMA HW features not supported\n");
1559 return 0;
1560 }
1561
1562 seq_printf(seq, "==============================\n");
1563 seq_printf(seq, "\tDMA HW features\n");
1564 seq_printf(seq, "==============================\n");
1565
1566 seq_printf(seq, "\t10/100 Mbps %s\n",
1567 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
1568 seq_printf(seq, "\t1000 Mbps %s\n",
1569 (priv->dma_cap.mbps_1000) ? "Y" : "N");
1570 seq_printf(seq, "\tHalf duple %s\n",
1571 (priv->dma_cap.half_duplex) ? "Y" : "N");
1572 seq_printf(seq, "\tHash Filter: %s\n",
1573 (priv->dma_cap.hash_filter) ? "Y" : "N");
1574 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
1575 (priv->dma_cap.multi_addr) ? "Y" : "N");
1576 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
1577 (priv->dma_cap.pcs) ? "Y" : "N");
1578 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
1579 (priv->dma_cap.sma_mdio) ? "Y" : "N");
1580 seq_printf(seq, "\tPMT Remote wake up: %s\n",
1581 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
1582 seq_printf(seq, "\tPMT Magic Frame: %s\n",
1583 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
1584 seq_printf(seq, "\tRMON module: %s\n",
1585 (priv->dma_cap.rmon) ? "Y" : "N");
1586 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
1587 (priv->dma_cap.time_stamp) ? "Y" : "N");
1588 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
1589 (priv->dma_cap.atime_stamp) ? "Y" : "N");
1590 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
1591 (priv->dma_cap.eee) ? "Y" : "N");
1592 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
1593 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
1594 (priv->dma_cap.tx_coe) ? "Y" : "N");
1595 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
1596 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
1597 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
1598 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
1599 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
1600 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
1601 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
1602 priv->dma_cap.number_rx_channel);
1603 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
1604 priv->dma_cap.number_tx_channel);
1605 seq_printf(seq, "\tEnhanced descriptors: %s\n",
1606 (priv->dma_cap.enh_desc) ? "Y" : "N");
1607
1608 return 0;
1609}
1610
1611static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
1612{
1613 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
1614}
1615
1616static const struct file_operations stmmac_dma_cap_fops = {
1617 .owner = THIS_MODULE,
1618 .open = stmmac_sysfs_dma_cap_open,
1619 .read = seq_read,
1620 .llseek = seq_lseek,
1621 .release = seq_release,
1622};
1623
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001624static int stmmac_init_fs(struct net_device *dev)
1625{
1626 /* Create debugfs entries */
1627 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
1628
1629 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
1630 pr_err("ERROR %s, debugfs create directory failed\n",
1631 STMMAC_RESOURCE_NAME);
1632
1633 return -ENOMEM;
1634 }
1635
1636 /* Entry to report DMA RX/TX rings */
1637 stmmac_rings_status = debugfs_create_file("descriptors_status",
1638 S_IRUGO, stmmac_fs_dir, dev,
1639 &stmmac_rings_status_fops);
1640
1641 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
1642 pr_info("ERROR creating stmmac ring debugfs file\n");
1643 debugfs_remove(stmmac_fs_dir);
1644
1645 return -ENOMEM;
1646 }
1647
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001648 /* Entry to report the DMA HW features */
1649 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
1650 dev, &stmmac_dma_cap_fops);
1651
1652 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
1653 pr_info("ERROR creating stmmac MMC debugfs file\n");
1654 debugfs_remove(stmmac_rings_status);
1655 debugfs_remove(stmmac_fs_dir);
1656
1657 return -ENOMEM;
1658 }
1659
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001660 return 0;
1661}
1662
1663static void stmmac_exit_fs(void)
1664{
1665 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001666 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001667 debugfs_remove(stmmac_fs_dir);
1668}
1669#endif /* CONFIG_STMMAC_DEBUG_FS */
1670
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001671static const struct net_device_ops stmmac_netdev_ops = {
1672 .ndo_open = stmmac_open,
1673 .ndo_start_xmit = stmmac_xmit,
1674 .ndo_stop = stmmac_release,
1675 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001676 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00001677 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001678 .ndo_tx_timeout = stmmac_tx_timeout,
1679 .ndo_do_ioctl = stmmac_ioctl,
1680 .ndo_set_config = stmmac_config,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001681#ifdef CONFIG_NET_POLL_CONTROLLER
1682 .ndo_poll_controller = stmmac_poll_controller,
1683#endif
1684 .ndo_set_mac_address = eth_mac_addr,
1685};
1686
1687/**
1688 * stmmac_probe - Initialization of the adapter .
1689 * @dev : device pointer
1690 * Description: The function initializes the network device structure for
1691 * the STMMAC driver. It also calls the low level routines
1692 * in order to init the HW (i.e. the DMA engine)
1693 */
1694static int stmmac_probe(struct net_device *dev)
1695{
1696 int ret = 0;
1697 struct stmmac_priv *priv = netdev_priv(dev);
1698
1699 ether_setup(dev);
1700
1701 dev->netdev_ops = &stmmac_netdev_ops;
1702 stmmac_set_ethtool_ops(dev);
1703
Michał Mirosław5e982f32011-04-09 02:46:55 +00001704 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1705 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001706 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1707#ifdef STMMAC_VLAN_TAG_USED
1708 /* Both mac100 and gmac support receive VLAN tag detection */
1709 dev->features |= NETIF_F_HW_VLAN_RX;
1710#endif
1711 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1712
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001713 if (flow_ctrl)
1714 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1715
1716 priv->pause = pause;
1717 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1718
1719 /* Get the MAC address */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001720 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1721 dev->dev_addr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001722
1723 if (!is_valid_ether_addr(dev->dev_addr))
1724 pr_warning("\tno valid MAC address;"
1725 "please, use ifconfig or nwhwconfig!\n");
1726
Vlad Lunguf8e96162010-11-29 22:52:52 +00001727 spin_lock_init(&priv->lock);
1728
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001729 ret = register_netdev(dev);
1730 if (ret) {
1731 pr_err("%s: ERROR %i registering the device\n",
1732 __func__, ret);
1733 return -ENODEV;
1734 }
1735
1736 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1737 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
Michał Mirosław79032642010-11-30 06:38:00 +00001738 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001739
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001740 return ret;
1741}
1742
1743/**
1744 * stmmac_mac_device_setup
1745 * @dev : device pointer
1746 * Description: select and initialise the mac device (mac100 or Gmac).
1747 */
1748static int stmmac_mac_device_setup(struct net_device *dev)
1749{
1750 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001751
1752 struct mac_device_info *device;
1753
Jiri Pirko01789342011-08-16 06:29:00 +00001754 if (priv->plat->has_gmac) {
1755 dev->priv_flags |= IFF_UNICAST_FLT;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001756 device = dwmac1000_setup(priv->ioaddr);
Jiri Pirko01789342011-08-16 06:29:00 +00001757 } else {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001758 device = dwmac100_setup(priv->ioaddr);
Jiri Pirko01789342011-08-16 06:29:00 +00001759 }
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001760
Dan Carpenter1ff21902010-07-22 01:16:48 +00001761 if (!device)
1762 return -ENOMEM;
1763
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001764 if (priv->plat->enh_desc) {
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001765 device->desc = &enh_desc_ops;
1766 pr_info("\tEnhanced descriptor structure\n");
1767 } else
Giuseppe CAVALLARO56b106a2010-04-13 20:21:12 +00001768 device->desc = &ndesc_ops;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001769
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001770 priv->hw = device;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001771
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001772 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001773 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
Deepak Sikri3172d3a2011-09-01 21:51:37 +00001774 enable_irq_wake(priv->wol_irq);
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001775 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001776
1777 return 0;
1778}
1779
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001780/**
1781 * stmmac_dvr_probe
1782 * @pdev: platform device pointer
1783 * Description: the driver is initialized through platform_device.
1784 */
1785static int stmmac_dvr_probe(struct platform_device *pdev)
1786{
1787 int ret = 0;
1788 struct resource *res;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001789 void __iomem *addr = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001790 struct net_device *ndev = NULL;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001791 struct stmmac_priv *priv = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001792 struct plat_stmmacenet_data *plat_dat;
1793
1794 pr_info("STMMAC driver:\n\tplatform registration... ");
1795 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001796 if (!res)
1797 return -ENODEV;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001798 pr_info("\tdone!\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001799
Dan Carpenterb6222682010-04-07 21:50:08 -07001800 if (!request_mem_region(res->start, resource_size(res),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001801 pdev->name)) {
1802 pr_err("%s: ERROR: memory allocation failed"
1803 "cannot get the I/O addr 0x%x\n",
1804 __func__, (unsigned int)res->start);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001805 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001806 }
1807
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001808 addr = ioremap(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001809 if (!addr) {
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001810 pr_err("%s: ERROR: memory mapping failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001811 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001812 goto out_release_region;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001813 }
1814
1815 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1816 if (!ndev) {
1817 pr_err("%s: ERROR: allocating the device\n", __func__);
1818 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001819 goto out_unmap;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001820 }
1821
1822 SET_NETDEV_DEV(ndev, &pdev->dev);
1823
1824 /* Get the MAC information */
1825 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1826 if (ndev->irq == -ENXIO) {
1827 pr_err("%s: ERROR: MAC IRQ configuration "
1828 "information not found\n", __func__);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001829 ret = -ENXIO;
1830 goto out_free_ndev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001831 }
1832
1833 priv = netdev_priv(ndev);
1834 priv->device = &(pdev->dev);
1835 priv->dev = ndev;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001836 plat_dat = pdev->dev.platform_data;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001837
1838 priv->plat = plat_dat;
1839
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001840 priv->ioaddr = addr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001841
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001842 /* PMT module is not integrated in all the MAC devices. */
1843 if (plat_dat->pmt) {
1844 pr_info("\tPMT module supported\n");
1845 device_set_wakeup_capable(&pdev->dev, 1);
1846 }
Deepak Sikri3172d3a2011-09-01 21:51:37 +00001847 /*
1848 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
1849 * The external wake up irq can be passed through the platform code
1850 * named as "eth_wake_irq"
1851 *
1852 * In case the wake up interrupt is not passed from the platform
1853 * so the driver will continue to use the mac irq (ndev->irq)
1854 */
1855 priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
1856 if (priv->wol_irq == -ENXIO)
1857 priv->wol_irq = ndev->irq;
1858
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001859
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001860 platform_set_drvdata(pdev, ndev);
1861
1862 /* Set the I/O base addr */
1863 ndev->base_addr = (unsigned long)addr;
1864
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001865 /* Custom initialisation */
1866 if (priv->plat->init) {
1867 ret = priv->plat->init(pdev);
1868 if (unlikely(ret))
Dan Carpenter34a52f32010-12-20 21:34:56 +00001869 goto out_free_ndev;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001870 }
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001871
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001872 /* MAC HW revice detection */
1873 ret = stmmac_mac_device_setup(ndev);
1874 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001875 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001876
1877 /* Network Device Registration */
1878 ret = stmmac_probe(ndev);
1879 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001880 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001881
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +00001882 /* Override with kernel parameters if supplied XXX CRS XXX
1883 * this needs to have multiple instances */
1884 if ((phyaddr >= 0) && (phyaddr <= 31))
1885 priv->plat->phy_addr = phyaddr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001886
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001887 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
David S. Miller1f0f6382010-08-30 21:55:17 -07001888 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1889 pdev->id, ndev->irq, addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001890
1891 /* MDIO bus Registration */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001892 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001893 ret = stmmac_mdio_register(ndev);
1894 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001895 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001896 pr_debug("registered!\n");
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001897
1898#ifdef CONFIG_STMMAC_DEBUG_FS
1899 ret = stmmac_init_fs(ndev);
1900 if (ret < 0)
1901 pr_warning("\tFailed debugFS registration");
1902#endif
1903
Dan Carpenter34a52f32010-12-20 21:34:56 +00001904 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001905
Dan Carpenter34a52f32010-12-20 21:34:56 +00001906out_unregister:
1907 unregister_netdev(ndev);
1908out_plat_exit:
1909 if (priv->plat->exit)
1910 priv->plat->exit(pdev);
1911out_free_ndev:
1912 free_netdev(ndev);
1913 platform_set_drvdata(pdev, NULL);
1914out_unmap:
1915 iounmap(addr);
1916out_release_region:
1917 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001918
1919 return ret;
1920}
1921
1922/**
1923 * stmmac_dvr_remove
1924 * @pdev: platform device pointer
1925 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1926 * changes the link status, releases the DMA descriptor rings,
1927 * unregisters the MDIO bus and unmaps the allocated memory.
1928 */
1929static int stmmac_dvr_remove(struct platform_device *pdev)
1930{
1931 struct net_device *ndev = platform_get_drvdata(pdev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001932 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001933 struct resource *res;
1934
1935 pr_info("%s:\n\tremoving driver", __func__);
1936
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001937 priv->hw->dma->stop_rx(priv->ioaddr);
1938 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001939
avisconti19449bf2010-10-25 18:58:14 +00001940 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001941
1942 netif_carrier_off(ndev);
1943
1944 stmmac_mdio_unregister(ndev);
1945
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001946 if (priv->plat->exit)
1947 priv->plat->exit(pdev);
1948
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001949 platform_set_drvdata(pdev, NULL);
1950 unregister_netdev(ndev);
1951
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001952 iounmap((void *)priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001953 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001954 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001955
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001956#ifdef CONFIG_STMMAC_DEBUG_FS
1957 stmmac_exit_fs();
1958#endif
1959
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001960 free_netdev(ndev);
1961
1962 return 0;
1963}
1964
1965#ifdef CONFIG_PM
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001966static int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001967{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001968 struct net_device *ndev = dev_get_drvdata(dev);
1969 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001970 int dis_ic = 0;
1971
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001972 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001973 return 0;
1974
1975 spin_lock(&priv->lock);
1976
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001977 netif_device_detach(ndev);
1978 netif_stop_queue(ndev);
1979 if (priv->phydev)
1980 phy_stop(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001981
1982#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001983 priv->tm->timer_stop();
1984 if (likely(priv->tm->enable))
1985 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001986#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001987 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001988
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001989 /* Stop TX/RX DMA */
1990 priv->hw->dma->stop_tx(priv->ioaddr);
1991 priv->hw->dma->stop_rx(priv->ioaddr);
1992 /* Clear the Rx/Tx descriptors */
1993 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1994 dis_ic);
1995 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001996
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001997 /* Enable Power down mode by programming the PMT regs */
1998 if (device_may_wakeup(priv->device))
1999 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
2000 else
2001 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002002
2003 spin_unlock(&priv->lock);
2004 return 0;
2005}
2006
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002007static int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002008{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002009 struct net_device *ndev = dev_get_drvdata(dev);
2010 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002011
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002012 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002013 return 0;
2014
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02002015 spin_lock(&priv->lock);
2016
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002017 /* Power Down bit, into the PM register, is cleared
2018 * automatically as soon as a magic packet or a Wake-up frame
2019 * is received. Anyway, it's better to manually clear
2020 * this bit because it can generate problems while resuming
2021 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002022 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07002023 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002024
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002025 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002026
2027 /* Enable the MAC and DMA */
avisconti19449bf2010-10-25 18:58:14 +00002028 stmmac_enable_mac(priv->ioaddr);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002029 priv->hw->dma->start_tx(priv->ioaddr);
2030 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002031
2032#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002033 if (likely(priv->tm->enable))
2034 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002035#endif
2036 napi_enable(&priv->napi);
2037
2038 if (priv->phydev)
2039 phy_start(priv->phydev);
2040
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002041 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002042
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002043 spin_unlock(&priv->lock);
2044 return 0;
2045}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002046
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002047static int stmmac_freeze(struct device *dev)
2048{
2049 struct net_device *ndev = dev_get_drvdata(dev);
2050
2051 if (!ndev || !netif_running(ndev))
2052 return 0;
2053
2054 return stmmac_release(ndev);
2055}
2056
2057static int stmmac_restore(struct device *dev)
2058{
2059 struct net_device *ndev = dev_get_drvdata(dev);
2060
2061 if (!ndev || !netif_running(ndev))
2062 return 0;
2063
2064 return stmmac_open(ndev);
2065}
2066
2067static const struct dev_pm_ops stmmac_pm_ops = {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002068 .suspend = stmmac_suspend,
2069 .resume = stmmac_resume,
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002070 .freeze = stmmac_freeze,
2071 .thaw = stmmac_restore,
2072 .restore = stmmac_restore,
2073};
2074#else
2075static const struct dev_pm_ops stmmac_pm_ops;
2076#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002077
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002078static struct platform_driver stmmac_driver = {
2079 .probe = stmmac_dvr_probe,
2080 .remove = stmmac_dvr_remove,
2081 .driver = {
2082 .name = STMMAC_RESOURCE_NAME,
2083 .owner = THIS_MODULE,
2084 .pm = &stmmac_pm_ops,
2085 },
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002086};
2087
2088/**
2089 * stmmac_init_module - Entry point for the driver
2090 * Description: This function is the entry point for the driver.
2091 */
2092static int __init stmmac_init_module(void)
2093{
2094 int ret;
2095
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002096 ret = platform_driver_register(&stmmac_driver);
2097 return ret;
2098}
2099
2100/**
2101 * stmmac_cleanup_module - Cleanup routine for the driver
2102 * Description: This function is the cleanup routine for the driver.
2103 */
2104static void __exit stmmac_cleanup_module(void)
2105{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002106 platform_driver_unregister(&stmmac_driver);
2107}
2108
2109#ifndef MODULE
2110static int __init stmmac_cmdline_opt(char *str)
2111{
2112 char *opt;
2113
2114 if (!str || !*str)
2115 return -EINVAL;
2116 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002117 if (!strncmp(opt, "debug:", 6)) {
2118 if (strict_strtoul(opt + 6, 0, (unsigned long *)&debug))
2119 goto err;
2120 } else if (!strncmp(opt, "phyaddr:", 8)) {
2121 if (strict_strtoul(opt + 8, 0,
2122 (unsigned long *)&phyaddr))
2123 goto err;
2124 } else if (!strncmp(opt, "dma_txsize:", 11)) {
2125 if (strict_strtoul(opt + 11, 0,
2126 (unsigned long *)&dma_txsize))
2127 goto err;
2128 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
2129 if (strict_strtoul(opt + 11, 0,
2130 (unsigned long *)&dma_rxsize))
2131 goto err;
2132 } else if (!strncmp(opt, "buf_sz:", 7)) {
2133 if (strict_strtoul(opt + 7, 0,
2134 (unsigned long *)&buf_sz))
2135 goto err;
2136 } else if (!strncmp(opt, "tc:", 3)) {
2137 if (strict_strtoul(opt + 3, 0, (unsigned long *)&tc))
2138 goto err;
2139 } else if (!strncmp(opt, "watchdog:", 9)) {
2140 if (strict_strtoul(opt + 9, 0,
2141 (unsigned long *)&watchdog))
2142 goto err;
2143 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
2144 if (strict_strtoul(opt + 10, 0,
2145 (unsigned long *)&flow_ctrl))
2146 goto err;
2147 } else if (!strncmp(opt, "pause:", 6)) {
2148 if (strict_strtoul(opt + 6, 0, (unsigned long *)&pause))
2149 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002150#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002151 } else if (!strncmp(opt, "tmrate:", 7)) {
2152 if (strict_strtoul(opt + 7, 0,
2153 (unsigned long *)&tmrate))
2154 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002155#endif
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002156 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002157 }
2158 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002159
2160err:
2161 pr_err("%s: ERROR broken module parameter conversion", __func__);
2162 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002163}
2164
2165__setup("stmmaceth=", stmmac_cmdline_opt);
2166#endif
2167
2168module_init(stmmac_init_module);
2169module_exit(stmmac_cleanup_module);
2170
2171MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
2172MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2173MODULE_LICENSE("GPL");