blob: c88dc358f9b0692ed20bb0bd1f77b74a575bb5e7 [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];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000307 int interface = priv->plat->interface;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700308 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
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000317 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0, interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700318
319 if (IS_ERR(phydev)) {
320 pr_err("%s: Could not attach to PHY\n", dev->name);
321 return PTR_ERR(phydev);
322 }
323
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000324 /* Stop Advertising 1000BASE Capability if interface is not GMII */
325 if ((interface) && ((interface == PHY_INTERFACE_MODE_MII) ||
326 (interface == PHY_INTERFACE_MODE_RMII))) {
327 phydev->supported &= (PHY_BASIC_FEATURES | SUPPORTED_Pause |
328 SUPPORTED_Asym_Pause);
329 priv->phydev->advertising = priv->phydev->supported;
330 }
331
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700332 /*
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)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000344 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700345
346 priv->phydev = phydev;
347
348 return 0;
349}
350
avisconti19449bf2010-10-25 18:58:14 +0000351static inline void stmmac_enable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700352{
353 u32 value = readl(ioaddr + MAC_CTRL_REG);
avisconti19449bf2010-10-25 18:58:14 +0000354
355 value |= MAC_RNABLE_RX | MAC_ENABLE_TX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700356 writel(value, ioaddr + MAC_CTRL_REG);
357}
358
avisconti19449bf2010-10-25 18:58:14 +0000359static inline void stmmac_disable_mac(void __iomem *ioaddr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700360{
361 u32 value = readl(ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700362
avisconti19449bf2010-10-25 18:58:14 +0000363 value &= ~(MAC_ENABLE_TX | MAC_RNABLE_RX);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700364 writel(value, ioaddr + MAC_CTRL_REG);
365}
366
367/**
368 * display_ring
369 * @p: pointer to the ring.
370 * @size: size of the ring.
371 * Description: display all the descriptors within the ring.
372 */
373static void display_ring(struct dma_desc *p, int size)
374{
375 struct tmp_s {
376 u64 a;
377 unsigned int b;
378 unsigned int c;
379 };
380 int i;
381 for (i = 0; i < size; i++) {
382 struct tmp_s *x = (struct tmp_s *)(p + i);
383 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
384 i, (unsigned int)virt_to_phys(&p[i]),
385 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
386 x->b, x->c);
387 pr_info("\n");
388 }
389}
390
391/**
392 * init_dma_desc_rings - init the RX/TX descriptor rings
393 * @dev: net device structure
394 * Description: this function initializes the DMA RX/TX descriptors
395 * and allocates the socket buffers.
396 */
397static void init_dma_desc_rings(struct net_device *dev)
398{
399 int i;
400 struct stmmac_priv *priv = netdev_priv(dev);
401 struct sk_buff *skb;
402 unsigned int txsize = priv->dma_tx_size;
403 unsigned int rxsize = priv->dma_rx_size;
404 unsigned int bfsize = priv->dma_buf_sz;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000405 int buff2_needed = 0, dis_ic = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700406
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700407 /* Set the Buffer size according to the MTU;
408 * indeed, in case of jumbo we need to bump-up the buffer sizes.
409 */
410 if (unlikely(dev->mtu >= BUF_SIZE_8KiB))
411 bfsize = BUF_SIZE_16KiB;
412 else if (unlikely(dev->mtu >= BUF_SIZE_4KiB))
413 bfsize = BUF_SIZE_8KiB;
414 else if (unlikely(dev->mtu >= BUF_SIZE_2KiB))
415 bfsize = BUF_SIZE_4KiB;
416 else if (unlikely(dev->mtu >= DMA_BUFFER_SIZE))
417 bfsize = BUF_SIZE_2KiB;
418 else
419 bfsize = DMA_BUFFER_SIZE;
420
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000421#ifdef CONFIG_STMMAC_TIMER
422 /* Disable interrupts on completion for the reception if timer is on */
423 if (likely(priv->tm->enable))
424 dis_ic = 1;
425#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700426 /* If the MTU exceeds 8k so use the second buffer in the chain */
427 if (bfsize >= BUF_SIZE_8KiB)
428 buff2_needed = 1;
429
430 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
431 txsize, rxsize, bfsize);
432
433 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
434 priv->rx_skbuff =
435 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
436 priv->dma_rx =
437 (struct dma_desc *)dma_alloc_coherent(priv->device,
438 rxsize *
439 sizeof(struct dma_desc),
440 &priv->dma_rx_phy,
441 GFP_KERNEL);
442 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
443 GFP_KERNEL);
444 priv->dma_tx =
445 (struct dma_desc *)dma_alloc_coherent(priv->device,
446 txsize *
447 sizeof(struct dma_desc),
448 &priv->dma_tx_phy,
449 GFP_KERNEL);
450
451 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
452 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
453 return;
454 }
455
456 DBG(probe, INFO, "stmmac (%s) DMA desc rings: virt addr (Rx %p, "
457 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
458 dev->name, priv->dma_rx, priv->dma_tx,
459 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
460
461 /* RX INITIALIZATION */
462 DBG(probe, INFO, "stmmac: SKB addresses:\n"
463 "skb\t\tskb data\tdma data\n");
464
465 for (i = 0; i < rxsize; i++) {
466 struct dma_desc *p = priv->dma_rx + i;
467
468 skb = netdev_alloc_skb_ip_align(dev, bfsize);
469 if (unlikely(skb == NULL)) {
470 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
471 break;
472 }
473 priv->rx_skbuff[i] = skb;
474 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
475 bfsize, DMA_FROM_DEVICE);
476
477 p->des2 = priv->rx_skbuff_dma[i];
478 if (unlikely(buff2_needed))
479 p->des3 = p->des2 + BUF_SIZE_8KiB;
480 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
481 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
482 }
483 priv->cur_rx = 0;
484 priv->dirty_rx = (unsigned int)(i - rxsize);
485 priv->dma_buf_sz = bfsize;
486 buf_sz = bfsize;
487
488 /* TX INITIALIZATION */
489 for (i = 0; i < txsize; i++) {
490 priv->tx_skbuff[i] = NULL;
491 priv->dma_tx[i].des2 = 0;
492 }
493 priv->dirty_tx = 0;
494 priv->cur_tx = 0;
495
496 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000497 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
498 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700499
500 if (netif_msg_hw(priv)) {
501 pr_info("RX descriptor ring:\n");
502 display_ring(priv->dma_rx, rxsize);
503 pr_info("TX descriptor ring:\n");
504 display_ring(priv->dma_tx, txsize);
505 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700506}
507
508static void dma_free_rx_skbufs(struct stmmac_priv *priv)
509{
510 int i;
511
512 for (i = 0; i < priv->dma_rx_size; i++) {
513 if (priv->rx_skbuff[i]) {
514 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
515 priv->dma_buf_sz, DMA_FROM_DEVICE);
516 dev_kfree_skb_any(priv->rx_skbuff[i]);
517 }
518 priv->rx_skbuff[i] = NULL;
519 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700520}
521
522static void dma_free_tx_skbufs(struct stmmac_priv *priv)
523{
524 int i;
525
526 for (i = 0; i < priv->dma_tx_size; i++) {
527 if (priv->tx_skbuff[i] != NULL) {
528 struct dma_desc *p = priv->dma_tx + i;
529 if (p->des2)
530 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000531 priv->hw->desc->get_tx_len(p),
532 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700533 dev_kfree_skb_any(priv->tx_skbuff[i]);
534 priv->tx_skbuff[i] = NULL;
535 }
536 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700537}
538
539static void free_dma_desc_resources(struct stmmac_priv *priv)
540{
541 /* Release the DMA TX/RX socket buffers */
542 dma_free_rx_skbufs(priv);
543 dma_free_tx_skbufs(priv);
544
545 /* Free the region of consistent memory previously allocated for
546 * the DMA */
547 dma_free_coherent(priv->device,
548 priv->dma_tx_size * sizeof(struct dma_desc),
549 priv->dma_tx, priv->dma_tx_phy);
550 dma_free_coherent(priv->device,
551 priv->dma_rx_size * sizeof(struct dma_desc),
552 priv->dma_rx, priv->dma_rx_phy);
553 kfree(priv->rx_skbuff_dma);
554 kfree(priv->rx_skbuff);
555 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700556}
557
558/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700559 * stmmac_dma_operation_mode - HW DMA operation mode
560 * @priv : pointer to the private device structure.
561 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000562 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700563 */
564static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
565{
Srinivas Kandagatla61b80132011-07-17 20:54:09 +0000566 if (likely(priv->plat->force_sf_dma_mode ||
567 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
568 /*
569 * In case of GMAC, SF mode can be enabled
570 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000571 * 1) TX COE if actually supported
572 * 2) There is no bugged Jumbo frame support
573 * that needs to not insert csum in the TDES.
574 */
575 priv->hw->dma->dma_mode(priv->ioaddr,
576 SF_DMA_MODE, SF_DMA_MODE);
577 tc = SF_DMA_MODE;
578 } else
579 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700580}
581
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700582/**
583 * stmmac_tx:
584 * @priv: private driver structure
585 * Description: it reclaims resources after transmission completes.
586 */
587static void stmmac_tx(struct stmmac_priv *priv)
588{
589 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700590
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000591 spin_lock(&priv->tx_lock);
592
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700593 while (priv->dirty_tx != priv->cur_tx) {
594 int last;
595 unsigned int entry = priv->dirty_tx % txsize;
596 struct sk_buff *skb = priv->tx_skbuff[entry];
597 struct dma_desc *p = priv->dma_tx + entry;
598
599 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000600 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700601 break;
602
603 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000604 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700605 if (likely(last)) {
606 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000607 priv->hw->desc->tx_status(&priv->dev->stats,
608 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000609 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700610 if (likely(tx_error == 0)) {
611 priv->dev->stats.tx_packets++;
612 priv->xstats.tx_pkt_n++;
613 } else
614 priv->dev->stats.tx_errors++;
615 }
616 TX_DBG("%s: curr %d, dirty %d\n", __func__,
617 priv->cur_tx, priv->dirty_tx);
618
619 if (likely(p->des2))
620 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000621 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700622 DMA_TO_DEVICE);
623 if (unlikely(p->des3))
624 p->des3 = 0;
625
626 if (likely(skb != NULL)) {
627 /*
628 * If there's room in the queue (limit it to size)
629 * we add this skb back into the pool,
630 * if it's the right size.
631 */
632 if ((skb_queue_len(&priv->rx_recycle) <
633 priv->dma_rx_size) &&
634 skb_recycle_check(skb, priv->dma_buf_sz))
635 __skb_queue_head(&priv->rx_recycle, skb);
636 else
637 dev_kfree_skb(skb);
638
639 priv->tx_skbuff[entry] = NULL;
640 }
641
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000642 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700643
644 entry = (++priv->dirty_tx) % txsize;
645 }
646 if (unlikely(netif_queue_stopped(priv->dev) &&
647 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
648 netif_tx_lock(priv->dev);
649 if (netif_queue_stopped(priv->dev) &&
650 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
651 TX_DBG("%s: restart transmit\n", __func__);
652 netif_wake_queue(priv->dev);
653 }
654 netif_tx_unlock(priv->dev);
655 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000656 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700657}
658
659static inline void stmmac_enable_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_start(tmrate);
664 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700665#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000666 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700667}
668
669static inline void stmmac_disable_irq(struct stmmac_priv *priv)
670{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000671#ifdef CONFIG_STMMAC_TIMER
672 if (likely(priv->tm->enable))
673 priv->tm->timer_stop();
674 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700675#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000676 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700677}
678
679static int stmmac_has_work(struct stmmac_priv *priv)
680{
681 unsigned int has_work = 0;
682 int rxret, tx_work = 0;
683
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000684 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700685 (priv->cur_rx % priv->dma_rx_size));
686
687 if (priv->dirty_tx != priv->cur_tx)
688 tx_work = 1;
689
690 if (likely(!rxret || tx_work))
691 has_work = 1;
692
693 return has_work;
694}
695
696static inline void _stmmac_schedule(struct stmmac_priv *priv)
697{
698 if (likely(stmmac_has_work(priv))) {
699 stmmac_disable_irq(priv);
700 napi_schedule(&priv->napi);
701 }
702}
703
704#ifdef CONFIG_STMMAC_TIMER
705void stmmac_schedule(struct net_device *dev)
706{
707 struct stmmac_priv *priv = netdev_priv(dev);
708
709 priv->xstats.sched_timer_n++;
710
711 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700712}
713
714static void stmmac_no_timer_started(unsigned int x)
715{;
716};
717
718static void stmmac_no_timer_stopped(void)
719{;
720};
721#endif
722
723/**
724 * stmmac_tx_err:
725 * @priv: pointer to the private device structure
726 * Description: it cleans the descriptors and restarts the transmission
727 * in case of errors.
728 */
729static void stmmac_tx_err(struct stmmac_priv *priv)
730{
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000731
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700732 netif_stop_queue(priv->dev);
733
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000734 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700735 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000736 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700737 priv->dirty_tx = 0;
738 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000739 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700740
741 priv->dev->stats.tx_errors++;
742 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700743}
744
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000745
746static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700747{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000748 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700749
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000750 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000751 if (likely(status == handle_tx_rx))
752 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700753
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000754 else if (unlikely(status == tx_hard_error_bump_tc)) {
755 /* Try to bump up the dma threshold on this failure */
756 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
757 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000758 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000759 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700760 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000761 } else if (unlikely(status == tx_hard_error))
762 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700763}
764
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000765static void stmmac_mmc_setup(struct stmmac_priv *priv)
766{
767 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
768 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
769
770 /* Do not manage MMC IRQ (FIXME) */
771 dwmac_mmc_intr_all_mask(priv->ioaddr);
772 dwmac_mmc_ctrl(priv->ioaddr, mode);
773 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
774}
775
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000776static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
777{
778 u32 hwid = priv->hw->synopsys_uid;
779
780 /* Only check valid Synopsys Id because old MAC chips
781 * have no HW registers where get the ID */
782 if (likely(hwid)) {
783 u32 uid = ((hwid & 0x0000ff00) >> 8);
784 u32 synid = (hwid & 0x000000ff);
785
786 pr_info("STMMAC - user ID: 0x%x, Synopsys ID: 0x%x\n",
787 uid, synid);
788
789 return synid;
790 }
791 return 0;
792}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000793
794/* New GMAC chips support a new register to indicate the
795 * presence of the optional feature/functions.
796 */
797static int stmmac_get_hw_features(struct stmmac_priv *priv)
798{
799 u32 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
800
801 if (likely(hw_cap)) {
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000802 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
803 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
804 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
805 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
806 priv->dma_cap.multi_addr =
807 (hw_cap & DMA_HW_FEAT_ADDMACADRSEL) >> 5;
808 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
809 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
810 priv->dma_cap.pmt_remote_wake_up =
811 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
812 priv->dma_cap.pmt_magic_frame =
813 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
814 /*MMC*/
815 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000816 /* IEEE 1588-2002*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000817 priv->dma_cap.time_stamp =
818 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000819 /* IEEE 1588-2008*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000820 priv->dma_cap.atime_stamp =
821 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000822 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000823 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
824 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000825 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000826 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
827 priv->dma_cap.rx_coe_type1 =
828 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
829 priv->dma_cap.rx_coe_type2 =
830 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
831 priv->dma_cap.rxfifo_over_2048 =
832 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000833 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000834 priv->dma_cap.number_rx_channel =
835 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
836 priv->dma_cap.number_tx_channel =
837 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000838 /* Alternate (enhanced) DESC mode*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000839 priv->dma_cap.enh_desc =
840 (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000841
842 } else
843 pr_debug("\tNo HW DMA feature register supported");
844
845 return hw_cap;
846}
847
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700848/**
849 * stmmac_open - open entry point of the driver
850 * @dev : pointer to the device structure.
851 * Description:
852 * This function is the open entry point of the driver.
853 * Return value:
854 * 0 on success and an appropriate (-)ve integer as defined in errno.h
855 * file on failure.
856 */
857static int stmmac_open(struct net_device *dev)
858{
859 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700860 int ret;
861
862 /* Check that the MAC address is valid. If its not, refuse
863 * to bring the device up. The user must specify an
864 * address using the following linux command:
865 * ifconfig eth0 hw ether xx:xx:xx:xx:xx:xx */
866 if (!is_valid_ether_addr(dev->dev_addr)) {
867 random_ether_addr(dev->dev_addr);
868 pr_warning("%s: generated random MAC address %pM\n", dev->name,
869 dev->dev_addr);
870 }
871
872 stmmac_verify_args();
873
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700874#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000875 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700876 if (unlikely(priv->tm == NULL)) {
Frans Pop2381a552010-03-24 07:57:36 +0000877 pr_err("%s: ERROR: timer memory alloc failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700878 return -ENOMEM;
879 }
880 priv->tm->freq = tmrate;
881
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000882 /* Test if the external timer can be actually used.
883 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700884 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000885 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700886 priv->tm->freq = 0;
887 priv->tm->timer_start = stmmac_no_timer_started;
888 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000889 } else
890 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700891#endif
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000892 ret = stmmac_init_phy(dev);
893 if (unlikely(ret)) {
894 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
895 goto open_error;
896 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700897
898 /* Create and initialize the TX/RX descriptors chains. */
899 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
900 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
901 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
902 init_dma_desc_rings(dev);
903
904 /* DMA initialization and SW reset */
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000905 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
906 priv->dma_tx_phy, priv->dma_rx_phy);
907 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700908 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000909 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700910 }
911
912 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000913 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +0000914 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000915 if (priv->plat->bus_setup)
916 priv->plat->bus_setup(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700917 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000918 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700919
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000920 stmmac_get_synopsys_id(priv);
921
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000922 stmmac_get_hw_features(priv);
923
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000924 if (priv->rx_coe)
925 pr_info("stmmac: Rx Checksum Offload Engine supported\n");
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000926 if (priv->plat->tx_coe)
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000927 pr_info("\tTX Checksum insertion supported\n");
Michał Mirosław5e982f32011-04-09 02:46:55 +0000928 netdev_update_features(dev);
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000929
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000930 /* Request the IRQ lines */
931 ret = request_irq(dev->irq, stmmac_interrupt,
932 IRQF_SHARED, dev->name, dev);
933 if (unlikely(ret < 0)) {
934 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
935 __func__, dev->irq, ret);
936 goto open_error;
937 }
938
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700939 /* Enable the MAC Rx/Tx */
avisconti19449bf2010-10-25 18:58:14 +0000940 stmmac_enable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700941
942 /* Set the HW DMA mode and the COE */
943 stmmac_dma_operation_mode(priv);
944
945 /* Extra statistics */
946 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
947 priv->xstats.threshold = tc;
948
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000949 stmmac_mmc_setup(priv);
950
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700951 /* Start the ball rolling... */
952 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000953 priv->hw->dma->start_tx(priv->ioaddr);
954 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700955
956#ifdef CONFIG_STMMAC_TIMER
957 priv->tm->timer_start(tmrate);
958#endif
959 /* Dump DMA/MAC registers */
960 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000961 priv->hw->mac->dump_regs(priv->ioaddr);
962 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700963 }
964
965 if (priv->phydev)
966 phy_start(priv->phydev);
967
968 napi_enable(&priv->napi);
969 skb_queue_head_init(&priv->rx_recycle);
970 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000971
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700972 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000973
974open_error:
975#ifdef CONFIG_STMMAC_TIMER
976 kfree(priv->tm);
977#endif
978 if (priv->phydev)
979 phy_disconnect(priv->phydev);
980
981 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700982}
983
984/**
985 * stmmac_release - close entry point of the driver
986 * @dev : device pointer.
987 * Description:
988 * This is the stop entry point of the driver.
989 */
990static int stmmac_release(struct net_device *dev)
991{
992 struct stmmac_priv *priv = netdev_priv(dev);
993
994 /* Stop and disconnect the PHY */
995 if (priv->phydev) {
996 phy_stop(priv->phydev);
997 phy_disconnect(priv->phydev);
998 priv->phydev = NULL;
999 }
1000
1001 netif_stop_queue(dev);
1002
1003#ifdef CONFIG_STMMAC_TIMER
1004 /* Stop and release the timer */
1005 stmmac_close_ext_timer();
1006 if (priv->tm != NULL)
1007 kfree(priv->tm);
1008#endif
1009 napi_disable(&priv->napi);
1010 skb_queue_purge(&priv->rx_recycle);
1011
1012 /* Free the IRQ lines */
1013 free_irq(dev->irq, dev);
1014
1015 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001016 priv->hw->dma->stop_tx(priv->ioaddr);
1017 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001018
1019 /* Release and free the Rx/Tx resources */
1020 free_dma_desc_resources(priv);
1021
avisconti19449bf2010-10-25 18:58:14 +00001022 /* Disable the MAC Rx/Tx */
1023 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001024
1025 netif_carrier_off(dev);
1026
1027 return 0;
1028}
1029
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001030static unsigned int stmmac_handle_jumbo_frames(struct sk_buff *skb,
1031 struct net_device *dev,
1032 int csum_insertion)
1033{
1034 struct stmmac_priv *priv = netdev_priv(dev);
1035 unsigned int nopaged_len = skb_headlen(skb);
1036 unsigned int txsize = priv->dma_tx_size;
1037 unsigned int entry = priv->cur_tx % txsize;
1038 struct dma_desc *desc = priv->dma_tx + entry;
1039
1040 if (nopaged_len > BUF_SIZE_8KiB) {
1041
1042 int buf2_size = nopaged_len - BUF_SIZE_8KiB;
1043
1044 desc->des2 = dma_map_single(priv->device, skb->data,
1045 BUF_SIZE_8KiB, DMA_TO_DEVICE);
1046 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001047 priv->hw->desc->prepare_tx_desc(desc, 1, BUF_SIZE_8KiB,
1048 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001049
1050 entry = (++priv->cur_tx) % txsize;
1051 desc = priv->dma_tx + entry;
1052
1053 desc->des2 = dma_map_single(priv->device,
1054 skb->data + BUF_SIZE_8KiB,
1055 buf2_size, DMA_TO_DEVICE);
1056 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001057 priv->hw->desc->prepare_tx_desc(desc, 0, buf2_size,
1058 csum_insertion);
1059 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001060 priv->tx_skbuff[entry] = NULL;
1061 } else {
1062 desc->des2 = dma_map_single(priv->device, skb->data,
1063 nopaged_len, DMA_TO_DEVICE);
1064 desc->des3 = desc->des2 + BUF_SIZE_4KiB;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001065 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1066 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001067 }
1068 return entry;
1069}
1070
1071/**
1072 * stmmac_xmit:
1073 * @skb : the socket buffer
1074 * @dev : device pointer
1075 * Description : Tx entry point of the driver.
1076 */
1077static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1078{
1079 struct stmmac_priv *priv = netdev_priv(dev);
1080 unsigned int txsize = priv->dma_tx_size;
1081 unsigned int entry;
1082 int i, csum_insertion = 0;
1083 int nfrags = skb_shinfo(skb)->nr_frags;
1084 struct dma_desc *desc, *first;
1085
1086 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1087 if (!netif_queue_stopped(dev)) {
1088 netif_stop_queue(dev);
1089 /* This is a hard error, log it. */
1090 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1091 __func__);
1092 }
1093 return NETDEV_TX_BUSY;
1094 }
1095
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001096 spin_lock(&priv->tx_lock);
1097
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001098 entry = priv->cur_tx % txsize;
1099
1100#ifdef STMMAC_XMIT_DEBUG
1101 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1102 pr_info("stmmac xmit:\n"
1103 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1104 "\tn_frags: %d - ip_summed: %d - %s gso\n",
1105 skb, skb->len, skb_headlen(skb), nfrags, skb->ip_summed,
1106 !skb_is_gso(skb) ? "isn't" : "is");
1107#endif
1108
Michał Mirosław5e982f32011-04-09 02:46:55 +00001109 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001110
1111 desc = priv->dma_tx + entry;
1112 first = desc;
1113
1114#ifdef STMMAC_XMIT_DEBUG
1115 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1116 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1117 "\t\tn_frags: %d, ip_summed: %d\n",
1118 skb->len, skb_headlen(skb), nfrags, skb->ip_summed);
1119#endif
1120 priv->tx_skbuff[entry] = skb;
1121 if (unlikely(skb->len >= BUF_SIZE_4KiB)) {
1122 entry = stmmac_handle_jumbo_frames(skb, dev, csum_insertion);
1123 desc = priv->dma_tx + entry;
1124 } else {
1125 unsigned int nopaged_len = skb_headlen(skb);
1126 desc->des2 = dma_map_single(priv->device, skb->data,
1127 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001128 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1129 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001130 }
1131
1132 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001133 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1134 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001135
1136 entry = (++priv->cur_tx) % txsize;
1137 desc = priv->dma_tx + entry;
1138
1139 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
Ian Campbellf7223802011-09-21 21:53:20 +00001140 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1141 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001142 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001143 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001144 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001145 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001146 }
1147
1148 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001149 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001150
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001151#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001152 /* Clean IC while using timer */
1153 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001154 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001155#endif
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001156
1157 wmb();
1158
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001159 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001160 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001161
1162 priv->cur_tx++;
1163
1164#ifdef STMMAC_XMIT_DEBUG
1165 if (netif_msg_pktdata(priv)) {
1166 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1167 "first=%p, nfrags=%d\n",
1168 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1169 entry, first, nfrags);
1170 display_ring(priv->dma_tx, txsize);
1171 pr_info(">>> frame to be transmitted: ");
1172 print_pkt(skb->data, skb->len);
1173 }
1174#endif
1175 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1176 TX_DBG("%s: stop transmitted packets\n", __func__);
1177 netif_stop_queue(dev);
1178 }
1179
1180 dev->stats.tx_bytes += skb->len;
1181
Richard Cochran3e82ce12011-06-12 02:19:06 +00001182 skb_tx_timestamp(skb);
1183
Richard Cochran52f64fa2011-06-19 03:31:43 +00001184 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1185
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001186 spin_unlock(&priv->tx_lock);
1187
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001188 return NETDEV_TX_OK;
1189}
1190
1191static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1192{
1193 unsigned int rxsize = priv->dma_rx_size;
1194 int bfsize = priv->dma_buf_sz;
1195 struct dma_desc *p = priv->dma_rx;
1196
1197 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1198 unsigned int entry = priv->dirty_rx % rxsize;
1199 if (likely(priv->rx_skbuff[entry] == NULL)) {
1200 struct sk_buff *skb;
1201
1202 skb = __skb_dequeue(&priv->rx_recycle);
1203 if (skb == NULL)
1204 skb = netdev_alloc_skb_ip_align(priv->dev,
1205 bfsize);
1206
1207 if (unlikely(skb == NULL))
1208 break;
1209
1210 priv->rx_skbuff[entry] = skb;
1211 priv->rx_skbuff_dma[entry] =
1212 dma_map_single(priv->device, skb->data, bfsize,
1213 DMA_FROM_DEVICE);
1214
1215 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001216 if (unlikely(priv->plat->has_gmac)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001217 if (bfsize >= BUF_SIZE_8KiB)
1218 (p + entry)->des3 =
1219 (p + entry)->des2 + BUF_SIZE_8KiB;
1220 }
1221 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1222 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001223 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001224 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001225 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001226}
1227
1228static int stmmac_rx(struct stmmac_priv *priv, int limit)
1229{
1230 unsigned int rxsize = priv->dma_rx_size;
1231 unsigned int entry = priv->cur_rx % rxsize;
1232 unsigned int next_entry;
1233 unsigned int count = 0;
1234 struct dma_desc *p = priv->dma_rx + entry;
1235 struct dma_desc *p_next;
1236
1237#ifdef STMMAC_RX_DEBUG
1238 if (netif_msg_hw(priv)) {
1239 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1240 display_ring(priv->dma_rx, rxsize);
1241 }
1242#endif
1243 count = 0;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001244 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001245 int status;
1246
1247 if (count >= limit)
1248 break;
1249
1250 count++;
1251
1252 next_entry = (++priv->cur_rx) % rxsize;
1253 p_next = priv->dma_rx + next_entry;
1254 prefetch(p_next);
1255
1256 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001257 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1258 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001259 if (unlikely(status == discard_frame))
1260 priv->dev->stats.rx_errors++;
1261 else {
1262 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001263 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001264
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001265 frame_len = priv->hw->desc->get_rx_frame_len(p);
1266 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1267 * Type frames (LLC/LLC-SNAP) */
1268 if (unlikely(status != llc_snap))
1269 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001270#ifdef STMMAC_RX_DEBUG
1271 if (frame_len > ETH_FRAME_LEN)
1272 pr_debug("\tRX frame size %d, COE status: %d\n",
1273 frame_len, status);
1274
1275 if (netif_msg_hw(priv))
1276 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1277 p, entry, p->des2);
1278#endif
1279 skb = priv->rx_skbuff[entry];
1280 if (unlikely(!skb)) {
1281 pr_err("%s: Inconsistent Rx descriptor chain\n",
1282 priv->dev->name);
1283 priv->dev->stats.rx_dropped++;
1284 break;
1285 }
1286 prefetch(skb->data - NET_IP_ALIGN);
1287 priv->rx_skbuff[entry] = NULL;
1288
1289 skb_put(skb, frame_len);
1290 dma_unmap_single(priv->device,
1291 priv->rx_skbuff_dma[entry],
1292 priv->dma_buf_sz, DMA_FROM_DEVICE);
1293#ifdef STMMAC_RX_DEBUG
1294 if (netif_msg_pktdata(priv)) {
1295 pr_info(" frame received (%dbytes)", frame_len);
1296 print_pkt(skb->data, frame_len);
1297 }
1298#endif
1299 skb->protocol = eth_type_trans(skb, priv->dev);
1300
1301 if (unlikely(status == csum_none)) {
1302 /* always for the old mac 10/100 */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001303 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001304 netif_receive_skb(skb);
1305 } else {
1306 skb->ip_summed = CHECKSUM_UNNECESSARY;
1307 napi_gro_receive(&priv->napi, skb);
1308 }
1309
1310 priv->dev->stats.rx_packets++;
1311 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001312 }
1313 entry = next_entry;
1314 p = p_next; /* use prefetched values */
1315 }
1316
1317 stmmac_rx_refill(priv);
1318
1319 priv->xstats.rx_pkt_n += count;
1320
1321 return count;
1322}
1323
1324/**
1325 * stmmac_poll - stmmac poll method (NAPI)
1326 * @napi : pointer to the napi structure.
1327 * @budget : maximum number of packets that the current CPU can receive from
1328 * all interfaces.
1329 * Description :
1330 * This function implements the the reception process.
1331 * Also it runs the TX completion thread
1332 */
1333static int stmmac_poll(struct napi_struct *napi, int budget)
1334{
1335 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1336 int work_done = 0;
1337
1338 priv->xstats.poll_n++;
1339 stmmac_tx(priv);
1340 work_done = stmmac_rx(priv, budget);
1341
1342 if (work_done < budget) {
1343 napi_complete(napi);
1344 stmmac_enable_irq(priv);
1345 }
1346 return work_done;
1347}
1348
1349/**
1350 * stmmac_tx_timeout
1351 * @dev : Pointer to net device structure
1352 * Description: this function is called when a packet transmission fails to
1353 * complete within a reasonable tmrate. The driver will mark the error in the
1354 * netdev structure and arrange for the device to be reset to a sane state
1355 * in order to transmit a new packet.
1356 */
1357static void stmmac_tx_timeout(struct net_device *dev)
1358{
1359 struct stmmac_priv *priv = netdev_priv(dev);
1360
1361 /* Clear Tx resources and restart transmitting again */
1362 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001363}
1364
1365/* Configuration changes (passed on by ifconfig) */
1366static int stmmac_config(struct net_device *dev, struct ifmap *map)
1367{
1368 if (dev->flags & IFF_UP) /* can't act on a running interface */
1369 return -EBUSY;
1370
1371 /* Don't allow changing the I/O address */
1372 if (map->base_addr != dev->base_addr) {
1373 pr_warning("%s: can't change I/O address\n", dev->name);
1374 return -EOPNOTSUPP;
1375 }
1376
1377 /* Don't allow changing the IRQ */
1378 if (map->irq != dev->irq) {
1379 pr_warning("%s: can't change IRQ number %d\n",
1380 dev->name, dev->irq);
1381 return -EOPNOTSUPP;
1382 }
1383
1384 /* ignore other fields */
1385 return 0;
1386}
1387
1388/**
Jiri Pirko01789342011-08-16 06:29:00 +00001389 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001390 * @dev : pointer to the device structure
1391 * Description:
1392 * This function is a driver entry point which gets called by the kernel
1393 * whenever multicast addresses must be enabled/disabled.
1394 * Return value:
1395 * void.
1396 */
Jiri Pirko01789342011-08-16 06:29:00 +00001397static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001398{
1399 struct stmmac_priv *priv = netdev_priv(dev);
1400
1401 spin_lock(&priv->lock);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001402 priv->hw->mac->set_filter(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001403 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001404}
1405
1406/**
1407 * stmmac_change_mtu - entry point to change MTU size for the device.
1408 * @dev : device pointer.
1409 * @new_mtu : the new MTU size for the device.
1410 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1411 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1412 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1413 * Return value:
1414 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1415 * file on failure.
1416 */
1417static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1418{
1419 struct stmmac_priv *priv = netdev_priv(dev);
1420 int max_mtu;
1421
1422 if (netif_running(dev)) {
1423 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1424 return -EBUSY;
1425 }
1426
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00001427 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001428 max_mtu = JUMBO_LEN;
1429 else
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00001430 max_mtu = BUF_SIZE_4KiB;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001431
1432 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1433 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1434 return -EINVAL;
1435 }
1436
Michał Mirosław5e982f32011-04-09 02:46:55 +00001437 dev->mtu = new_mtu;
1438 netdev_update_features(dev);
1439
1440 return 0;
1441}
1442
1443static u32 stmmac_fix_features(struct net_device *dev, u32 features)
1444{
1445 struct stmmac_priv *priv = netdev_priv(dev);
1446
1447 if (!priv->rx_coe)
1448 features &= ~NETIF_F_RXCSUM;
1449 if (!priv->plat->tx_coe)
1450 features &= ~NETIF_F_ALL_CSUM;
1451
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001452 /* Some GMAC devices have a bugged Jumbo frame support that
1453 * needs to have the Tx COE disabled for oversized frames
1454 * (due to limited buffer sizes). In this case we disable
1455 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001456 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1457 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001458
Michał Mirosław5e982f32011-04-09 02:46:55 +00001459 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001460}
1461
1462static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1463{
1464 struct net_device *dev = (struct net_device *)dev_id;
1465 struct stmmac_priv *priv = netdev_priv(dev);
1466
1467 if (unlikely(!dev)) {
1468 pr_err("%s: invalid dev pointer\n", __func__);
1469 return IRQ_NONE;
1470 }
1471
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001472 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001473 /* To handle GMAC own interrupts */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001474 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001475
1476 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001477
1478 return IRQ_HANDLED;
1479}
1480
1481#ifdef CONFIG_NET_POLL_CONTROLLER
1482/* Polling receive - used by NETCONSOLE and other diagnostic tools
1483 * to allow network I/O with interrupts disabled. */
1484static void stmmac_poll_controller(struct net_device *dev)
1485{
1486 disable_irq(dev->irq);
1487 stmmac_interrupt(dev->irq, dev);
1488 enable_irq(dev->irq);
1489}
1490#endif
1491
1492/**
1493 * stmmac_ioctl - Entry point for the Ioctl
1494 * @dev: Device pointer.
1495 * @rq: An IOCTL specefic structure, that can contain a pointer to
1496 * a proprietary structure used to pass information to the driver.
1497 * @cmd: IOCTL command
1498 * Description:
1499 * Currently there are no special functionality supported in IOCTL, just the
1500 * phy_mii_ioctl(...) can be invoked.
1501 */
1502static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1503{
1504 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001505 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001506
1507 if (!netif_running(dev))
1508 return -EINVAL;
1509
Richard Cochran28b04112010-07-17 08:48:55 +00001510 if (!priv->phydev)
1511 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001512
Richard Cochran28b04112010-07-17 08:48:55 +00001513 spin_lock(&priv->lock);
1514 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
1515 spin_unlock(&priv->lock);
1516
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001517 return ret;
1518}
1519
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001520#ifdef CONFIG_STMMAC_DEBUG_FS
1521static struct dentry *stmmac_fs_dir;
1522static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001523static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001524
1525static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
1526{
1527 struct tmp_s {
1528 u64 a;
1529 unsigned int b;
1530 unsigned int c;
1531 };
1532 int i;
1533 struct net_device *dev = seq->private;
1534 struct stmmac_priv *priv = netdev_priv(dev);
1535
1536 seq_printf(seq, "=======================\n");
1537 seq_printf(seq, " RX descriptor ring\n");
1538 seq_printf(seq, "=======================\n");
1539
1540 for (i = 0; i < priv->dma_rx_size; i++) {
1541 struct tmp_s *x = (struct tmp_s *)(priv->dma_rx + i);
1542 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1543 i, (unsigned int)(x->a),
1544 (unsigned int)((x->a) >> 32), x->b, x->c);
1545 seq_printf(seq, "\n");
1546 }
1547
1548 seq_printf(seq, "\n");
1549 seq_printf(seq, "=======================\n");
1550 seq_printf(seq, " TX descriptor ring\n");
1551 seq_printf(seq, "=======================\n");
1552
1553 for (i = 0; i < priv->dma_tx_size; i++) {
1554 struct tmp_s *x = (struct tmp_s *)(priv->dma_tx + i);
1555 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1556 i, (unsigned int)(x->a),
1557 (unsigned int)((x->a) >> 32), x->b, x->c);
1558 seq_printf(seq, "\n");
1559 }
1560
1561 return 0;
1562}
1563
1564static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
1565{
1566 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
1567}
1568
1569static const struct file_operations stmmac_rings_status_fops = {
1570 .owner = THIS_MODULE,
1571 .open = stmmac_sysfs_ring_open,
1572 .read = seq_read,
1573 .llseek = seq_lseek,
1574 .release = seq_release,
1575};
1576
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001577static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
1578{
1579 struct net_device *dev = seq->private;
1580 struct stmmac_priv *priv = netdev_priv(dev);
1581
1582 if (!stmmac_get_hw_features(priv)) {
1583 seq_printf(seq, "DMA HW features not supported\n");
1584 return 0;
1585 }
1586
1587 seq_printf(seq, "==============================\n");
1588 seq_printf(seq, "\tDMA HW features\n");
1589 seq_printf(seq, "==============================\n");
1590
1591 seq_printf(seq, "\t10/100 Mbps %s\n",
1592 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
1593 seq_printf(seq, "\t1000 Mbps %s\n",
1594 (priv->dma_cap.mbps_1000) ? "Y" : "N");
1595 seq_printf(seq, "\tHalf duple %s\n",
1596 (priv->dma_cap.half_duplex) ? "Y" : "N");
1597 seq_printf(seq, "\tHash Filter: %s\n",
1598 (priv->dma_cap.hash_filter) ? "Y" : "N");
1599 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
1600 (priv->dma_cap.multi_addr) ? "Y" : "N");
1601 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
1602 (priv->dma_cap.pcs) ? "Y" : "N");
1603 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
1604 (priv->dma_cap.sma_mdio) ? "Y" : "N");
1605 seq_printf(seq, "\tPMT Remote wake up: %s\n",
1606 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
1607 seq_printf(seq, "\tPMT Magic Frame: %s\n",
1608 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
1609 seq_printf(seq, "\tRMON module: %s\n",
1610 (priv->dma_cap.rmon) ? "Y" : "N");
1611 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
1612 (priv->dma_cap.time_stamp) ? "Y" : "N");
1613 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
1614 (priv->dma_cap.atime_stamp) ? "Y" : "N");
1615 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
1616 (priv->dma_cap.eee) ? "Y" : "N");
1617 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
1618 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
1619 (priv->dma_cap.tx_coe) ? "Y" : "N");
1620 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
1621 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
1622 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
1623 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
1624 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
1625 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
1626 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
1627 priv->dma_cap.number_rx_channel);
1628 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
1629 priv->dma_cap.number_tx_channel);
1630 seq_printf(seq, "\tEnhanced descriptors: %s\n",
1631 (priv->dma_cap.enh_desc) ? "Y" : "N");
1632
1633 return 0;
1634}
1635
1636static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
1637{
1638 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
1639}
1640
1641static const struct file_operations stmmac_dma_cap_fops = {
1642 .owner = THIS_MODULE,
1643 .open = stmmac_sysfs_dma_cap_open,
1644 .read = seq_read,
1645 .llseek = seq_lseek,
1646 .release = seq_release,
1647};
1648
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001649static int stmmac_init_fs(struct net_device *dev)
1650{
1651 /* Create debugfs entries */
1652 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
1653
1654 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
1655 pr_err("ERROR %s, debugfs create directory failed\n",
1656 STMMAC_RESOURCE_NAME);
1657
1658 return -ENOMEM;
1659 }
1660
1661 /* Entry to report DMA RX/TX rings */
1662 stmmac_rings_status = debugfs_create_file("descriptors_status",
1663 S_IRUGO, stmmac_fs_dir, dev,
1664 &stmmac_rings_status_fops);
1665
1666 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
1667 pr_info("ERROR creating stmmac ring debugfs file\n");
1668 debugfs_remove(stmmac_fs_dir);
1669
1670 return -ENOMEM;
1671 }
1672
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001673 /* Entry to report the DMA HW features */
1674 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
1675 dev, &stmmac_dma_cap_fops);
1676
1677 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
1678 pr_info("ERROR creating stmmac MMC debugfs file\n");
1679 debugfs_remove(stmmac_rings_status);
1680 debugfs_remove(stmmac_fs_dir);
1681
1682 return -ENOMEM;
1683 }
1684
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001685 return 0;
1686}
1687
1688static void stmmac_exit_fs(void)
1689{
1690 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001691 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001692 debugfs_remove(stmmac_fs_dir);
1693}
1694#endif /* CONFIG_STMMAC_DEBUG_FS */
1695
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001696static const struct net_device_ops stmmac_netdev_ops = {
1697 .ndo_open = stmmac_open,
1698 .ndo_start_xmit = stmmac_xmit,
1699 .ndo_stop = stmmac_release,
1700 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001701 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00001702 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001703 .ndo_tx_timeout = stmmac_tx_timeout,
1704 .ndo_do_ioctl = stmmac_ioctl,
1705 .ndo_set_config = stmmac_config,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001706#ifdef CONFIG_NET_POLL_CONTROLLER
1707 .ndo_poll_controller = stmmac_poll_controller,
1708#endif
1709 .ndo_set_mac_address = eth_mac_addr,
1710};
1711
1712/**
1713 * stmmac_probe - Initialization of the adapter .
1714 * @dev : device pointer
1715 * Description: The function initializes the network device structure for
1716 * the STMMAC driver. It also calls the low level routines
1717 * in order to init the HW (i.e. the DMA engine)
1718 */
1719static int stmmac_probe(struct net_device *dev)
1720{
1721 int ret = 0;
1722 struct stmmac_priv *priv = netdev_priv(dev);
1723
1724 ether_setup(dev);
1725
1726 dev->netdev_ops = &stmmac_netdev_ops;
1727 stmmac_set_ethtool_ops(dev);
1728
Michał Mirosław5e982f32011-04-09 02:46:55 +00001729 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
1730 dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001731 dev->watchdog_timeo = msecs_to_jiffies(watchdog);
1732#ifdef STMMAC_VLAN_TAG_USED
1733 /* Both mac100 and gmac support receive VLAN tag detection */
1734 dev->features |= NETIF_F_HW_VLAN_RX;
1735#endif
1736 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1737
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001738 if (flow_ctrl)
1739 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1740
1741 priv->pause = pause;
1742 netif_napi_add(dev, &priv->napi, stmmac_poll, 64);
1743
1744 /* Get the MAC address */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001745 priv->hw->mac->get_umac_addr((void __iomem *) dev->base_addr,
1746 dev->dev_addr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001747
1748 if (!is_valid_ether_addr(dev->dev_addr))
1749 pr_warning("\tno valid MAC address;"
1750 "please, use ifconfig or nwhwconfig!\n");
1751
Vlad Lunguf8e96162010-11-29 22:52:52 +00001752 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001753 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00001754
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001755 ret = register_netdev(dev);
1756 if (ret) {
1757 pr_err("%s: ERROR %i registering the device\n",
1758 __func__, ret);
1759 return -ENODEV;
1760 }
1761
1762 DBG(probe, DEBUG, "%s: Scatter/Gather: %s - HW checksums: %s\n",
1763 dev->name, (dev->features & NETIF_F_SG) ? "on" : "off",
Michał Mirosław79032642010-11-30 06:38:00 +00001764 (dev->features & NETIF_F_IP_CSUM) ? "on" : "off");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001765
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001766 return ret;
1767}
1768
1769/**
1770 * stmmac_mac_device_setup
1771 * @dev : device pointer
1772 * Description: select and initialise the mac device (mac100 or Gmac).
1773 */
1774static int stmmac_mac_device_setup(struct net_device *dev)
1775{
1776 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001777
1778 struct mac_device_info *device;
1779
Jiri Pirko01789342011-08-16 06:29:00 +00001780 if (priv->plat->has_gmac) {
1781 dev->priv_flags |= IFF_UNICAST_FLT;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001782 device = dwmac1000_setup(priv->ioaddr);
Jiri Pirko01789342011-08-16 06:29:00 +00001783 } else {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001784 device = dwmac100_setup(priv->ioaddr);
Jiri Pirko01789342011-08-16 06:29:00 +00001785 }
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001786
Dan Carpenter1ff21902010-07-22 01:16:48 +00001787 if (!device)
1788 return -ENOMEM;
1789
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001790 if (priv->plat->enh_desc) {
Giuseppe CAVALLARO3d90c502010-04-13 20:21:15 +00001791 device->desc = &enh_desc_ops;
1792 pr_info("\tEnhanced descriptor structure\n");
1793 } else
Giuseppe CAVALLARO56b106a2010-04-13 20:21:12 +00001794 device->desc = &ndesc_ops;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001795
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001796 priv->hw = device;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001797
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001798 if (device_can_wakeup(priv->device)) {
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001799 priv->wolopts = WAKE_MAGIC; /* Magic Frame as default */
Deepak Sikri3172d3a2011-09-01 21:51:37 +00001800 enable_irq_wake(priv->wol_irq);
Giuseppe Cavallaro539c9aa2011-02-13 17:00:05 -08001801 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001802
1803 return 0;
1804}
1805
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001806/**
1807 * stmmac_dvr_probe
1808 * @pdev: platform device pointer
1809 * Description: the driver is initialized through platform_device.
1810 */
1811static int stmmac_dvr_probe(struct platform_device *pdev)
1812{
1813 int ret = 0;
1814 struct resource *res;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001815 void __iomem *addr = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001816 struct net_device *ndev = NULL;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001817 struct stmmac_priv *priv = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001818 struct plat_stmmacenet_data *plat_dat;
1819
1820 pr_info("STMMAC driver:\n\tplatform registration... ");
1821 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001822 if (!res)
1823 return -ENODEV;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001824 pr_info("\tdone!\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001825
Dan Carpenterb6222682010-04-07 21:50:08 -07001826 if (!request_mem_region(res->start, resource_size(res),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001827 pdev->name)) {
1828 pr_err("%s: ERROR: memory allocation failed"
1829 "cannot get the I/O addr 0x%x\n",
1830 __func__, (unsigned int)res->start);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001831 return -EBUSY;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001832 }
1833
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001834 addr = ioremap(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001835 if (!addr) {
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001836 pr_err("%s: ERROR: memory mapping failed\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001837 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001838 goto out_release_region;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001839 }
1840
1841 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
1842 if (!ndev) {
1843 pr_err("%s: ERROR: allocating the device\n", __func__);
1844 ret = -ENOMEM;
Dan Carpenter34a52f32010-12-20 21:34:56 +00001845 goto out_unmap;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001846 }
1847
1848 SET_NETDEV_DEV(ndev, &pdev->dev);
1849
1850 /* Get the MAC information */
1851 ndev->irq = platform_get_irq_byname(pdev, "macirq");
1852 if (ndev->irq == -ENXIO) {
1853 pr_err("%s: ERROR: MAC IRQ configuration "
1854 "information not found\n", __func__);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001855 ret = -ENXIO;
1856 goto out_free_ndev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001857 }
1858
1859 priv = netdev_priv(ndev);
1860 priv->device = &(pdev->dev);
1861 priv->dev = ndev;
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001862 plat_dat = pdev->dev.platform_data;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001863
1864 priv->plat = plat_dat;
1865
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001866 priv->ioaddr = addr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001867
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001868 /* PMT module is not integrated in all the MAC devices. */
1869 if (plat_dat->pmt) {
1870 pr_info("\tPMT module supported\n");
1871 device_set_wakeup_capable(&pdev->dev, 1);
1872 }
Deepak Sikri3172d3a2011-09-01 21:51:37 +00001873 /*
1874 * On some platforms e.g. SPEAr the wake up irq differs from the mac irq
1875 * The external wake up irq can be passed through the platform code
1876 * named as "eth_wake_irq"
1877 *
1878 * In case the wake up interrupt is not passed from the platform
1879 * so the driver will continue to use the mac irq (ndev->irq)
1880 */
1881 priv->wol_irq = platform_get_irq_byname(pdev, "eth_wake_irq");
1882 if (priv->wol_irq == -ENXIO)
1883 priv->wol_irq = ndev->irq;
1884
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001885
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001886 platform_set_drvdata(pdev, ndev);
1887
1888 /* Set the I/O base addr */
1889 ndev->base_addr = (unsigned long)addr;
1890
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001891 /* Custom initialisation */
1892 if (priv->plat->init) {
1893 ret = priv->plat->init(pdev);
1894 if (unlikely(ret))
Dan Carpenter34a52f32010-12-20 21:34:56 +00001895 goto out_free_ndev;
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001896 }
Giuseppe CAVALLAROee7946a2010-01-06 23:07:14 +00001897
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001898 /* MAC HW revice detection */
1899 ret = stmmac_mac_device_setup(ndev);
1900 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001901 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001902
1903 /* Network Device Registration */
1904 ret = stmmac_probe(ndev);
1905 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001906 goto out_plat_exit;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001907
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +00001908 /* Override with kernel parameters if supplied XXX CRS XXX
1909 * this needs to have multiple instances */
1910 if ((phyaddr >= 0) && (phyaddr <= 31))
1911 priv->plat->phy_addr = phyaddr;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001912
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001913 pr_info("\t%s - (dev. name: %s - id: %d, IRQ #%d\n"
David S. Miller1f0f6382010-08-30 21:55:17 -07001914 "\tIO base addr: 0x%p)\n", ndev->name, pdev->name,
1915 pdev->id, ndev->irq, addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001916
1917 /* MDIO bus Registration */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001918 pr_debug("\tMDIO bus (id: %d)...", priv->plat->bus_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001919 ret = stmmac_mdio_register(ndev);
1920 if (ret < 0)
Dan Carpenter34a52f32010-12-20 21:34:56 +00001921 goto out_unregister;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001922 pr_debug("registered!\n");
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001923
1924#ifdef CONFIG_STMMAC_DEBUG_FS
1925 ret = stmmac_init_fs(ndev);
1926 if (ret < 0)
1927 pr_warning("\tFailed debugFS registration");
1928#endif
1929
Dan Carpenter34a52f32010-12-20 21:34:56 +00001930 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001931
Dan Carpenter34a52f32010-12-20 21:34:56 +00001932out_unregister:
1933 unregister_netdev(ndev);
1934out_plat_exit:
1935 if (priv->plat->exit)
1936 priv->plat->exit(pdev);
1937out_free_ndev:
1938 free_netdev(ndev);
1939 platform_set_drvdata(pdev, NULL);
1940out_unmap:
1941 iounmap(addr);
1942out_release_region:
1943 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001944
1945 return ret;
1946}
1947
1948/**
1949 * stmmac_dvr_remove
1950 * @pdev: platform device pointer
1951 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
1952 * changes the link status, releases the DMA descriptor rings,
1953 * unregisters the MDIO bus and unmaps the allocated memory.
1954 */
1955static int stmmac_dvr_remove(struct platform_device *pdev)
1956{
1957 struct net_device *ndev = platform_get_drvdata(pdev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001958 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001959 struct resource *res;
1960
1961 pr_info("%s:\n\tremoving driver", __func__);
1962
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001963 priv->hw->dma->stop_rx(priv->ioaddr);
1964 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001965
avisconti19449bf2010-10-25 18:58:14 +00001966 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001967
1968 netif_carrier_off(ndev);
1969
1970 stmmac_mdio_unregister(ndev);
1971
Giuseppe CAVALLARO293bb1c2010-11-24 02:38:05 +00001972 if (priv->plat->exit)
1973 priv->plat->exit(pdev);
1974
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001975 platform_set_drvdata(pdev, NULL);
1976 unregister_netdev(ndev);
1977
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001978 iounmap((void *)priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001979 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Dan Carpenter7c5365b2010-03-22 02:11:11 +00001980 release_mem_region(res->start, resource_size(res));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001981
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001982#ifdef CONFIG_STMMAC_DEBUG_FS
1983 stmmac_exit_fs();
1984#endif
1985
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001986 free_netdev(ndev);
1987
1988 return 0;
1989}
1990
1991#ifdef CONFIG_PM
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001992static int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001993{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001994 struct net_device *ndev = dev_get_drvdata(dev);
1995 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001996 int dis_ic = 0;
1997
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001998 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001999 return 0;
2000
2001 spin_lock(&priv->lock);
2002
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002003 netif_device_detach(ndev);
2004 netif_stop_queue(ndev);
2005 if (priv->phydev)
2006 phy_stop(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002007
2008#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002009 priv->tm->timer_stop();
2010 if (likely(priv->tm->enable))
2011 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002012#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002013 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002014
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002015 /* Stop TX/RX DMA */
2016 priv->hw->dma->stop_tx(priv->ioaddr);
2017 priv->hw->dma->stop_rx(priv->ioaddr);
2018 /* Clear the Rx/Tx descriptors */
2019 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
2020 dis_ic);
2021 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002022
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002023 /* Enable Power down mode by programming the PMT regs */
2024 if (device_may_wakeup(priv->device))
2025 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
2026 else
2027 stmmac_disable_mac(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002028
2029 spin_unlock(&priv->lock);
2030 return 0;
2031}
2032
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002033static int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002034{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002035 struct net_device *ndev = dev_get_drvdata(dev);
2036 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002037
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002038 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002039 return 0;
2040
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02002041 spin_lock(&priv->lock);
2042
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002043 /* Power Down bit, into the PM register, is cleared
2044 * automatically as soon as a magic packet or a Wake-up frame
2045 * is received. Anyway, it's better to manually clear
2046 * this bit because it can generate problems while resuming
2047 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002048 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07002049 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002050
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002051 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002052
2053 /* Enable the MAC and DMA */
avisconti19449bf2010-10-25 18:58:14 +00002054 stmmac_enable_mac(priv->ioaddr);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002055 priv->hw->dma->start_tx(priv->ioaddr);
2056 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002057
2058#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002059 if (likely(priv->tm->enable))
2060 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002061#endif
2062 napi_enable(&priv->napi);
2063
2064 if (priv->phydev)
2065 phy_start(priv->phydev);
2066
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002067 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002068
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002069 spin_unlock(&priv->lock);
2070 return 0;
2071}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002072
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002073static int stmmac_freeze(struct device *dev)
2074{
2075 struct net_device *ndev = dev_get_drvdata(dev);
2076
2077 if (!ndev || !netif_running(ndev))
2078 return 0;
2079
2080 return stmmac_release(ndev);
2081}
2082
2083static int stmmac_restore(struct device *dev)
2084{
2085 struct net_device *ndev = dev_get_drvdata(dev);
2086
2087 if (!ndev || !netif_running(ndev))
2088 return 0;
2089
2090 return stmmac_open(ndev);
2091}
2092
2093static const struct dev_pm_ops stmmac_pm_ops = {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002094 .suspend = stmmac_suspend,
2095 .resume = stmmac_resume,
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002096 .freeze = stmmac_freeze,
2097 .thaw = stmmac_restore,
2098 .restore = stmmac_restore,
2099};
2100#else
2101static const struct dev_pm_ops stmmac_pm_ops;
2102#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002103
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002104static struct platform_driver stmmac_driver = {
2105 .probe = stmmac_dvr_probe,
2106 .remove = stmmac_dvr_remove,
2107 .driver = {
2108 .name = STMMAC_RESOURCE_NAME,
2109 .owner = THIS_MODULE,
2110 .pm = &stmmac_pm_ops,
2111 },
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002112};
2113
2114/**
2115 * stmmac_init_module - Entry point for the driver
2116 * Description: This function is the entry point for the driver.
2117 */
2118static int __init stmmac_init_module(void)
2119{
2120 int ret;
2121
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002122 ret = platform_driver_register(&stmmac_driver);
2123 return ret;
2124}
2125
2126/**
2127 * stmmac_cleanup_module - Cleanup routine for the driver
2128 * Description: This function is the cleanup routine for the driver.
2129 */
2130static void __exit stmmac_cleanup_module(void)
2131{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002132 platform_driver_unregister(&stmmac_driver);
2133}
2134
2135#ifndef MODULE
2136static int __init stmmac_cmdline_opt(char *str)
2137{
2138 char *opt;
2139
2140 if (!str || !*str)
2141 return -EINVAL;
2142 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002143 if (!strncmp(opt, "debug:", 6)) {
2144 if (strict_strtoul(opt + 6, 0, (unsigned long *)&debug))
2145 goto err;
2146 } else if (!strncmp(opt, "phyaddr:", 8)) {
2147 if (strict_strtoul(opt + 8, 0,
2148 (unsigned long *)&phyaddr))
2149 goto err;
2150 } else if (!strncmp(opt, "dma_txsize:", 11)) {
2151 if (strict_strtoul(opt + 11, 0,
2152 (unsigned long *)&dma_txsize))
2153 goto err;
2154 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
2155 if (strict_strtoul(opt + 11, 0,
2156 (unsigned long *)&dma_rxsize))
2157 goto err;
2158 } else if (!strncmp(opt, "buf_sz:", 7)) {
2159 if (strict_strtoul(opt + 7, 0,
2160 (unsigned long *)&buf_sz))
2161 goto err;
2162 } else if (!strncmp(opt, "tc:", 3)) {
2163 if (strict_strtoul(opt + 3, 0, (unsigned long *)&tc))
2164 goto err;
2165 } else if (!strncmp(opt, "watchdog:", 9)) {
2166 if (strict_strtoul(opt + 9, 0,
2167 (unsigned long *)&watchdog))
2168 goto err;
2169 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
2170 if (strict_strtoul(opt + 10, 0,
2171 (unsigned long *)&flow_ctrl))
2172 goto err;
2173 } else if (!strncmp(opt, "pause:", 6)) {
2174 if (strict_strtoul(opt + 6, 0, (unsigned long *)&pause))
2175 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002176#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002177 } else if (!strncmp(opt, "tmrate:", 7)) {
2178 if (strict_strtoul(opt + 7, 0,
2179 (unsigned long *)&tmrate))
2180 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002181#endif
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002182 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002183 }
2184 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002185
2186err:
2187 pr_err("%s: ERROR broken module parameter conversion", __func__);
2188 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002189}
2190
2191__setup("stmmaceth=", stmmac_cmdline_opt);
2192#endif
2193
2194module_init(stmmac_init_module);
2195module_exit(stmmac_cleanup_module);
2196
2197MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet driver");
2198MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2199MODULE_LICENSE("GPL");