blob: 84f6b348ec7057c847b780b95582c47a4e40d304 [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
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00005 Copyright(C) 2007-2011 STMicroelectronics Ltd
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07006
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
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070031#include <linux/kernel.h>
32#include <linux/interrupt.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070033#include <linux/ip.h>
34#include <linux/tcp.h>
35#include <linux/skbuff.h>
36#include <linux/ethtool.h>
37#include <linux/if_ether.h>
38#include <linux/crc32.h>
39#include <linux/mii.h>
Jiri Pirko01789342011-08-16 06:29:00 +000040#include <linux/if.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070041#include <linux/if_vlan.h>
42#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040044#include <linux/prefetch.h>
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000045#ifdef CONFIG_STMMAC_DEBUG_FS
46#include <linux/debugfs.h>
47#include <linux/seq_file.h>
48#endif
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +000049#include "stmmac.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070050
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070051#undef STMMAC_DEBUG
52/*#define STMMAC_DEBUG*/
53#ifdef STMMAC_DEBUG
54#define DBG(nlevel, klevel, fmt, args...) \
55 ((void)(netif_msg_##nlevel(priv) && \
56 printk(KERN_##klevel fmt, ## args)))
57#else
58#define DBG(nlevel, klevel, fmt, args...) do { } while (0)
59#endif
60
61#undef STMMAC_RX_DEBUG
62/*#define STMMAC_RX_DEBUG*/
63#ifdef STMMAC_RX_DEBUG
64#define RX_DBG(fmt, args...) printk(fmt, ## args)
65#else
66#define RX_DBG(fmt, args...) do { } while (0)
67#endif
68
69#undef STMMAC_XMIT_DEBUG
70/*#define STMMAC_XMIT_DEBUG*/
71#ifdef STMMAC_TX_DEBUG
72#define TX_DBG(fmt, args...) printk(fmt, ## args)
73#else
74#define TX_DBG(fmt, args...) do { } while (0)
75#endif
76
77#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
78#define JUMBO_LEN 9000
79
80/* Module parameters */
81#define TX_TIMEO 5000 /* default 5 seconds */
82static int watchdog = TX_TIMEO;
83module_param(watchdog, int, S_IRUGO | S_IWUSR);
84MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
85
86static int debug = -1; /* -1: default, 0: no output, 16: all */
87module_param(debug, int, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
89
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000090int phyaddr = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070091module_param(phyaddr, int, S_IRUGO);
92MODULE_PARM_DESC(phyaddr, "Physical device address");
93
94#define DMA_TX_SIZE 256
95static int dma_txsize = DMA_TX_SIZE;
96module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
97MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
98
99#define DMA_RX_SIZE 256
100static int dma_rxsize = DMA_RX_SIZE;
101module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
102MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
103
104static int flow_ctrl = FLOW_OFF;
105module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
106MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
107
108static int pause = PAUSE_TIME;
109module_param(pause, int, S_IRUGO | S_IWUSR);
110MODULE_PARM_DESC(pause, "Flow Control Pause Time");
111
112#define TC_DEFAULT 64
113static int tc = TC_DEFAULT;
114module_param(tc, int, S_IRUGO | S_IWUSR);
115MODULE_PARM_DESC(tc, "DMA threshold control value");
116
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700117/* Pay attention to tune this parameter; take care of both
118 * hardware capability and network stabitily/performance impact.
119 * Many tests showed that ~4ms latency seems to be good enough. */
120#ifdef CONFIG_STMMAC_TIMER
121#define DEFAULT_PERIODIC_RATE 256
122static int tmrate = DEFAULT_PERIODIC_RATE;
123module_param(tmrate, int, S_IRUGO | S_IWUSR);
124MODULE_PARM_DESC(tmrate, "External timer freq. (default: 256Hz)");
125#endif
126
127#define DMA_BUFFER_SIZE BUF_SIZE_2KiB
128static int buf_sz = DMA_BUFFER_SIZE;
129module_param(buf_sz, int, S_IRUGO | S_IWUSR);
130MODULE_PARM_DESC(buf_sz, "DMA buffer size");
131
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700132static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
133 NETIF_MSG_LINK | NETIF_MSG_IFUP |
134 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
135
136static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700137
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000138#ifdef CONFIG_STMMAC_DEBUG_FS
139static int stmmac_init_fs(struct net_device *dev);
140static void stmmac_exit_fs(void);
141#endif
142
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700143/**
144 * stmmac_verify_args - verify the driver parameters.
145 * Description: it verifies if some wrong parameter is passed to the driver.
146 * Note that wrong parameters are replaced with the default values.
147 */
148static void stmmac_verify_args(void)
149{
150 if (unlikely(watchdog < 0))
151 watchdog = TX_TIMEO;
152 if (unlikely(dma_rxsize < 0))
153 dma_rxsize = DMA_RX_SIZE;
154 if (unlikely(dma_txsize < 0))
155 dma_txsize = DMA_TX_SIZE;
156 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
157 buf_sz = DMA_BUFFER_SIZE;
158 if (unlikely(flow_ctrl > 1))
159 flow_ctrl = FLOW_AUTO;
160 else if (likely(flow_ctrl < 0))
161 flow_ctrl = FLOW_OFF;
162 if (unlikely((pause < 0) || (pause > 0xffff)))
163 pause = PAUSE_TIME;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700164}
165
166#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
167static void print_pkt(unsigned char *buf, int len)
168{
169 int j;
170 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
171 for (j = 0; j < len; j++) {
172 if ((j % 16) == 0)
173 pr_info("\n %03x:", j);
174 pr_info(" %02x", buf[j]);
175 }
176 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700177}
178#endif
179
180/* minimum number of free TX descriptors required to wake up TX process */
181#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
182
183static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
184{
185 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
186}
187
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000188/* On some ST platforms, some HW system configuraton registers have to be
189 * set according to the link speed negotiated.
190 */
191static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
192{
193 struct phy_device *phydev = priv->phydev;
194
195 if (likely(priv->plat->fix_mac_speed))
196 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
197 phydev->speed);
198}
199
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700200/**
201 * stmmac_adjust_link
202 * @dev: net device structure
203 * Description: it adjusts the link parameters.
204 */
205static void stmmac_adjust_link(struct net_device *dev)
206{
207 struct stmmac_priv *priv = netdev_priv(dev);
208 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700209 unsigned long flags;
210 int new_state = 0;
211 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
212
213 if (phydev == NULL)
214 return;
215
216 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
217 phydev->addr, phydev->link);
218
219 spin_lock_irqsave(&priv->lock, flags);
220 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000221 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700222
223 /* Now we make sure that we can be in full duplex mode.
224 * If not, we operate in half-duplex mode. */
225 if (phydev->duplex != priv->oldduplex) {
226 new_state = 1;
227 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000228 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700229 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000230 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700231 priv->oldduplex = phydev->duplex;
232 }
233 /* Flow Control operation */
234 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000235 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000236 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700237
238 if (phydev->speed != priv->speed) {
239 new_state = 1;
240 switch (phydev->speed) {
241 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000242 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000243 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000244 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700245 break;
246 case 100:
247 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000248 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000249 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700250 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000251 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700252 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000253 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700254 }
255 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000256 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700257 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000258 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700259 break;
260 default:
261 if (netif_msg_link(priv))
262 pr_warning("%s: Speed (%d) is not 10"
263 " or 100!\n", dev->name, phydev->speed);
264 break;
265 }
266
267 priv->speed = phydev->speed;
268 }
269
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000270 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700271
272 if (!priv->oldlink) {
273 new_state = 1;
274 priv->oldlink = 1;
275 }
276 } else if (priv->oldlink) {
277 new_state = 1;
278 priv->oldlink = 0;
279 priv->speed = 0;
280 priv->oldduplex = -1;
281 }
282
283 if (new_state && netif_msg_link(priv))
284 phy_print_status(phydev);
285
286 spin_unlock_irqrestore(&priv->lock, flags);
287
288 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
289}
290
291/**
292 * stmmac_init_phy - PHY initialization
293 * @dev: net device structure
294 * Description: it initializes the driver's PHY state, and attaches the PHY
295 * to the mac driver.
296 * Return value:
297 * 0 on success
298 */
299static int stmmac_init_phy(struct net_device *dev)
300{
301 struct stmmac_priv *priv = netdev_priv(dev);
302 struct phy_device *phydev;
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000303 char phy_id[MII_BUS_ID_SIZE + 3];
304 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000305 int interface = priv->plat->interface;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700306 priv->oldlink = 0;
307 priv->speed = 0;
308 priv->oldduplex = -1;
309
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000310 if (priv->plat->phy_bus_name)
311 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
312 priv->plat->phy_bus_name, priv->plat->bus_id);
313 else
314 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
315 priv->plat->bus_id);
316
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000317 snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000318 priv->plat->phy_addr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700319 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id);
320
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000321 phydev = phy_connect(dev, phy_id, &stmmac_adjust_link, 0, interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700322
323 if (IS_ERR(phydev)) {
324 pr_err("%s: Could not attach to PHY\n", dev->name);
325 return PTR_ERR(phydev);
326 }
327
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000328 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000329 if ((interface == PHY_INTERFACE_MODE_MII) ||
330 (interface == PHY_INTERFACE_MODE_RMII))
331 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
332 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000333
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700334 /*
335 * Broken HW is sometimes missing the pull-up resistor on the
336 * MDIO line, which results in reads to non-existent devices returning
337 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
338 * device as well.
339 * Note: phydev->phy_id is the result of reading the UID PHY registers.
340 */
341 if (phydev->phy_id == 0) {
342 phy_disconnect(phydev);
343 return -ENODEV;
344 }
345 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000346 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700347
348 priv->phydev = phydev;
349
350 return 0;
351}
352
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700353/**
354 * display_ring
355 * @p: pointer to the ring.
356 * @size: size of the ring.
357 * Description: display all the descriptors within the ring.
358 */
359static void display_ring(struct dma_desc *p, int size)
360{
361 struct tmp_s {
362 u64 a;
363 unsigned int b;
364 unsigned int c;
365 };
366 int i;
367 for (i = 0; i < size; i++) {
368 struct tmp_s *x = (struct tmp_s *)(p + i);
369 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
370 i, (unsigned int)virt_to_phys(&p[i]),
371 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
372 x->b, x->c);
373 pr_info("\n");
374 }
375}
376
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000377static int stmmac_set_bfsize(int mtu, int bufsize)
378{
379 int ret = bufsize;
380
381 if (mtu >= BUF_SIZE_4KiB)
382 ret = BUF_SIZE_8KiB;
383 else if (mtu >= BUF_SIZE_2KiB)
384 ret = BUF_SIZE_4KiB;
385 else if (mtu >= DMA_BUFFER_SIZE)
386 ret = BUF_SIZE_2KiB;
387 else
388 ret = DMA_BUFFER_SIZE;
389
390 return ret;
391}
392
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700393/**
394 * init_dma_desc_rings - init the RX/TX descriptor rings
395 * @dev: net device structure
396 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000397 * and allocates the socket buffers. It suppors the chained and ring
398 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700399 */
400static void init_dma_desc_rings(struct net_device *dev)
401{
402 int i;
403 struct stmmac_priv *priv = netdev_priv(dev);
404 struct sk_buff *skb;
405 unsigned int txsize = priv->dma_tx_size;
406 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000407 unsigned int bfsize;
408 int dis_ic = 0;
409 int des3_as_data_buf = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700410
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000411 /* Set the max buffer size according to the DESC mode
412 * and the MTU. Note that RING mode allows 16KiB bsize. */
413 bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
414
415 if (bfsize == BUF_SIZE_16KiB)
416 des3_as_data_buf = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700417 else
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000418 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700419
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000420#ifdef CONFIG_STMMAC_TIMER
421 /* Disable interrupts on completion for the reception if timer is on */
422 if (likely(priv->tm->enable))
423 dis_ic = 1;
424#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700425
426 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
427 txsize, rxsize, bfsize);
428
429 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
430 priv->rx_skbuff =
431 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
432 priv->dma_rx =
433 (struct dma_desc *)dma_alloc_coherent(priv->device,
434 rxsize *
435 sizeof(struct dma_desc),
436 &priv->dma_rx_phy,
437 GFP_KERNEL);
438 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
439 GFP_KERNEL);
440 priv->dma_tx =
441 (struct dma_desc *)dma_alloc_coherent(priv->device,
442 txsize *
443 sizeof(struct dma_desc),
444 &priv->dma_tx_phy,
445 GFP_KERNEL);
446
447 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
448 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
449 return;
450 }
451
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000452 DBG(probe, INFO, "stmmac (%s) DMA desc: virt addr (Rx %p, "
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700453 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
454 dev->name, priv->dma_rx, priv->dma_tx,
455 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
456
457 /* RX INITIALIZATION */
458 DBG(probe, INFO, "stmmac: SKB addresses:\n"
459 "skb\t\tskb data\tdma data\n");
460
461 for (i = 0; i < rxsize; i++) {
462 struct dma_desc *p = priv->dma_rx + i;
463
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +0000464 skb = __netdev_alloc_skb(dev, bfsize + NET_IP_ALIGN,
465 GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700466 if (unlikely(skb == NULL)) {
467 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
468 break;
469 }
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +0000470 skb_reserve(skb, NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700471 priv->rx_skbuff[i] = skb;
472 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
473 bfsize, DMA_FROM_DEVICE);
474
475 p->des2 = priv->rx_skbuff_dma[i];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000476
477 priv->hw->ring->init_desc3(des3_as_data_buf, p);
478
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700479 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
480 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
481 }
482 priv->cur_rx = 0;
483 priv->dirty_rx = (unsigned int)(i - rxsize);
484 priv->dma_buf_sz = bfsize;
485 buf_sz = bfsize;
486
487 /* TX INITIALIZATION */
488 for (i = 0; i < txsize; i++) {
489 priv->tx_skbuff[i] = NULL;
490 priv->dma_tx[i].des2 = 0;
491 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000492
493 /* In case of Chained mode this sets the des3 to the next
494 * element in the chain */
495 priv->hw->ring->init_dma_chain(priv->dma_rx, priv->dma_rx_phy, rxsize);
496 priv->hw->ring->init_dma_chain(priv->dma_tx, priv->dma_tx_phy, txsize);
497
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700498 priv->dirty_tx = 0;
499 priv->cur_tx = 0;
500
501 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000502 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
503 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700504
505 if (netif_msg_hw(priv)) {
506 pr_info("RX descriptor ring:\n");
507 display_ring(priv->dma_rx, rxsize);
508 pr_info("TX descriptor ring:\n");
509 display_ring(priv->dma_tx, txsize);
510 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700511}
512
513static void dma_free_rx_skbufs(struct stmmac_priv *priv)
514{
515 int i;
516
517 for (i = 0; i < priv->dma_rx_size; i++) {
518 if (priv->rx_skbuff[i]) {
519 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
520 priv->dma_buf_sz, DMA_FROM_DEVICE);
521 dev_kfree_skb_any(priv->rx_skbuff[i]);
522 }
523 priv->rx_skbuff[i] = NULL;
524 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700525}
526
527static void dma_free_tx_skbufs(struct stmmac_priv *priv)
528{
529 int i;
530
531 for (i = 0; i < priv->dma_tx_size; i++) {
532 if (priv->tx_skbuff[i] != NULL) {
533 struct dma_desc *p = priv->dma_tx + i;
534 if (p->des2)
535 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000536 priv->hw->desc->get_tx_len(p),
537 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700538 dev_kfree_skb_any(priv->tx_skbuff[i]);
539 priv->tx_skbuff[i] = NULL;
540 }
541 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700542}
543
544static void free_dma_desc_resources(struct stmmac_priv *priv)
545{
546 /* Release the DMA TX/RX socket buffers */
547 dma_free_rx_skbufs(priv);
548 dma_free_tx_skbufs(priv);
549
550 /* Free the region of consistent memory previously allocated for
551 * the DMA */
552 dma_free_coherent(priv->device,
553 priv->dma_tx_size * sizeof(struct dma_desc),
554 priv->dma_tx, priv->dma_tx_phy);
555 dma_free_coherent(priv->device,
556 priv->dma_rx_size * sizeof(struct dma_desc),
557 priv->dma_rx, priv->dma_rx_phy);
558 kfree(priv->rx_skbuff_dma);
559 kfree(priv->rx_skbuff);
560 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700561}
562
563/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700564 * stmmac_dma_operation_mode - HW DMA operation mode
565 * @priv : pointer to the private device structure.
566 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000567 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700568 */
569static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
570{
Srinivas Kandagatla61b80132011-07-17 20:54:09 +0000571 if (likely(priv->plat->force_sf_dma_mode ||
572 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
573 /*
574 * In case of GMAC, SF mode can be enabled
575 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000576 * 1) TX COE if actually supported
577 * 2) There is no bugged Jumbo frame support
578 * that needs to not insert csum in the TDES.
579 */
580 priv->hw->dma->dma_mode(priv->ioaddr,
581 SF_DMA_MODE, SF_DMA_MODE);
582 tc = SF_DMA_MODE;
583 } else
584 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700585}
586
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700587/**
588 * stmmac_tx:
589 * @priv: private driver structure
590 * Description: it reclaims resources after transmission completes.
591 */
592static void stmmac_tx(struct stmmac_priv *priv)
593{
594 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700595
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000596 spin_lock(&priv->tx_lock);
597
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700598 while (priv->dirty_tx != priv->cur_tx) {
599 int last;
600 unsigned int entry = priv->dirty_tx % txsize;
601 struct sk_buff *skb = priv->tx_skbuff[entry];
602 struct dma_desc *p = priv->dma_tx + entry;
603
604 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000605 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700606 break;
607
608 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000609 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700610 if (likely(last)) {
611 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000612 priv->hw->desc->tx_status(&priv->dev->stats,
613 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000614 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700615 if (likely(tx_error == 0)) {
616 priv->dev->stats.tx_packets++;
617 priv->xstats.tx_pkt_n++;
618 } else
619 priv->dev->stats.tx_errors++;
620 }
621 TX_DBG("%s: curr %d, dirty %d\n", __func__,
622 priv->cur_tx, priv->dirty_tx);
623
624 if (likely(p->des2))
625 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000626 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700627 DMA_TO_DEVICE);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000628 priv->hw->ring->clean_desc3(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700629
630 if (likely(skb != NULL)) {
631 /*
632 * If there's room in the queue (limit it to size)
633 * we add this skb back into the pool,
634 * if it's the right size.
635 */
636 if ((skb_queue_len(&priv->rx_recycle) <
637 priv->dma_rx_size) &&
638 skb_recycle_check(skb, priv->dma_buf_sz))
639 __skb_queue_head(&priv->rx_recycle, skb);
640 else
641 dev_kfree_skb(skb);
642
643 priv->tx_skbuff[entry] = NULL;
644 }
645
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000646 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700647
648 entry = (++priv->dirty_tx) % txsize;
649 }
650 if (unlikely(netif_queue_stopped(priv->dev) &&
651 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
652 netif_tx_lock(priv->dev);
653 if (netif_queue_stopped(priv->dev) &&
654 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
655 TX_DBG("%s: restart transmit\n", __func__);
656 netif_wake_queue(priv->dev);
657 }
658 netif_tx_unlock(priv->dev);
659 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000660 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700661}
662
663static inline void stmmac_enable_irq(struct stmmac_priv *priv)
664{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000665#ifdef CONFIG_STMMAC_TIMER
666 if (likely(priv->tm->enable))
667 priv->tm->timer_start(tmrate);
668 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700669#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000670 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700671}
672
673static inline void stmmac_disable_irq(struct stmmac_priv *priv)
674{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000675#ifdef CONFIG_STMMAC_TIMER
676 if (likely(priv->tm->enable))
677 priv->tm->timer_stop();
678 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700679#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000680 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700681}
682
683static int stmmac_has_work(struct stmmac_priv *priv)
684{
685 unsigned int has_work = 0;
686 int rxret, tx_work = 0;
687
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000688 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700689 (priv->cur_rx % priv->dma_rx_size));
690
691 if (priv->dirty_tx != priv->cur_tx)
692 tx_work = 1;
693
694 if (likely(!rxret || tx_work))
695 has_work = 1;
696
697 return has_work;
698}
699
700static inline void _stmmac_schedule(struct stmmac_priv *priv)
701{
702 if (likely(stmmac_has_work(priv))) {
703 stmmac_disable_irq(priv);
704 napi_schedule(&priv->napi);
705 }
706}
707
708#ifdef CONFIG_STMMAC_TIMER
709void stmmac_schedule(struct net_device *dev)
710{
711 struct stmmac_priv *priv = netdev_priv(dev);
712
713 priv->xstats.sched_timer_n++;
714
715 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700716}
717
718static void stmmac_no_timer_started(unsigned int x)
719{;
720};
721
722static void stmmac_no_timer_stopped(void)
723{;
724};
725#endif
726
727/**
728 * stmmac_tx_err:
729 * @priv: pointer to the private device structure
730 * Description: it cleans the descriptors and restarts the transmission
731 * in case of errors.
732 */
733static void stmmac_tx_err(struct stmmac_priv *priv)
734{
735 netif_stop_queue(priv->dev);
736
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000737 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700738 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000739 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700740 priv->dirty_tx = 0;
741 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000742 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700743
744 priv->dev->stats.tx_errors++;
745 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700746}
747
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000748
749static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700750{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000751 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700752
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000753 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000754 if (likely(status == handle_tx_rx))
755 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700756
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000757 else if (unlikely(status == tx_hard_error_bump_tc)) {
758 /* Try to bump up the dma threshold on this failure */
759 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
760 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000761 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000762 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700763 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000764 } else if (unlikely(status == tx_hard_error))
765 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700766}
767
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000768static void stmmac_mmc_setup(struct stmmac_priv *priv)
769{
770 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
771 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
772
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000773 /* Mask MMC irq, counters are managed in SW and registers
774 * are cleared on each READ eventually. */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000775 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000776
777 if (priv->dma_cap.rmon) {
778 dwmac_mmc_ctrl(priv->ioaddr, mode);
779 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
780 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +0000781 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000782}
783
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000784static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
785{
786 u32 hwid = priv->hw->synopsys_uid;
787
788 /* Only check valid Synopsys Id because old MAC chips
789 * have no HW registers where get the ID */
790 if (likely(hwid)) {
791 u32 uid = ((hwid & 0x0000ff00) >> 8);
792 u32 synid = (hwid & 0x000000ff);
793
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000794 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000795 uid, synid);
796
797 return synid;
798 }
799 return 0;
800}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000801
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000802/**
803 * stmmac_selec_desc_mode
804 * @dev : device pointer
805 * Description: select the Enhanced/Alternate or Normal descriptors */
806static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
807{
808 if (priv->plat->enh_desc) {
809 pr_info(" Enhanced/Alternate descriptors\n");
810 priv->hw->desc = &enh_desc_ops;
811 } else {
812 pr_info(" Normal descriptors\n");
813 priv->hw->desc = &ndesc_ops;
814 }
815}
816
817/**
818 * stmmac_get_hw_features
819 * @priv : private device pointer
820 * Description:
821 * new GMAC chip generations have a new register to indicate the
822 * presence of the optional feature/functions.
823 * This can be also used to override the value passed through the
824 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000825 */
826static int stmmac_get_hw_features(struct stmmac_priv *priv)
827{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +0000828 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +0000829
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +0000830 if (priv->hw->dma->get_hw_feature) {
831 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000832
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000833 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
834 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
835 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
836 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
837 priv->dma_cap.multi_addr =
838 (hw_cap & DMA_HW_FEAT_ADDMACADRSEL) >> 5;
839 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
840 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
841 priv->dma_cap.pmt_remote_wake_up =
842 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
843 priv->dma_cap.pmt_magic_frame =
844 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000845 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000846 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000847 /* IEEE 1588-2002*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000848 priv->dma_cap.time_stamp =
849 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000850 /* IEEE 1588-2008*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000851 priv->dma_cap.atime_stamp =
852 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000853 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000854 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
855 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000856 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000857 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
858 priv->dma_cap.rx_coe_type1 =
859 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
860 priv->dma_cap.rx_coe_type2 =
861 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
862 priv->dma_cap.rxfifo_over_2048 =
863 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000864 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000865 priv->dma_cap.number_rx_channel =
866 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
867 priv->dma_cap.number_tx_channel =
868 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000869 /* Alternate (enhanced) DESC mode*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000870 priv->dma_cap.enh_desc =
871 (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000872
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000873 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000874
875 return hw_cap;
876}
877
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000878static void stmmac_check_ether_addr(struct stmmac_priv *priv)
879{
880 /* verify if the MAC address is valid, in case of failures it
881 * generates a random MAC address */
882 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
883 priv->hw->mac->get_umac_addr((void __iomem *)
884 priv->dev->base_addr,
885 priv->dev->dev_addr, 0);
886 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000887 eth_hw_addr_random(priv->dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000888 }
889 pr_warning("%s: device MAC address %pM\n", priv->dev->name,
890 priv->dev->dev_addr);
891}
892
893/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700894 * stmmac_open - open entry point of the driver
895 * @dev : pointer to the device structure.
896 * Description:
897 * This function is the open entry point of the driver.
898 * Return value:
899 * 0 on success and an appropriate (-)ve integer as defined in errno.h
900 * file on failure.
901 */
902static int stmmac_open(struct net_device *dev)
903{
904 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700905 int ret;
906
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000907 stmmac_check_ether_addr(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700908
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000909 /* MDIO bus Registration */
910 ret = stmmac_mdio_register(dev);
911 if (ret < 0) {
912 pr_debug("%s: MDIO bus (id: %d) registration failed",
913 __func__, priv->plat->bus_id);
914 return ret;
915 }
916
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700917#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000918 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Joe Perchese404dec2012-01-29 12:56:23 +0000919 if (unlikely(priv->tm == NULL))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700920 return -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +0000921
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700922 priv->tm->freq = tmrate;
923
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000924 /* Test if the external timer can be actually used.
925 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700926 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000927 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700928 priv->tm->freq = 0;
929 priv->tm->timer_start = stmmac_no_timer_started;
930 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000931 } else
932 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700933#endif
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000934 ret = stmmac_init_phy(dev);
935 if (unlikely(ret)) {
936 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
937 goto open_error;
938 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700939
940 /* Create and initialize the TX/RX descriptors chains. */
941 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
942 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
943 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
944 init_dma_desc_rings(dev);
945
946 /* DMA initialization and SW reset */
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000947 ret = priv->hw->dma->init(priv->ioaddr, priv->plat->pbl,
948 priv->dma_tx_phy, priv->dma_rx_phy);
949 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700950 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000951 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700952 }
953
954 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000955 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000956
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +0000957 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000958 if (priv->plat->bus_setup)
959 priv->plat->bus_setup(priv->ioaddr);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000960
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700961 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000962 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700963
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +0000964 /* Request the IRQ lines */
965 ret = request_irq(dev->irq, stmmac_interrupt,
966 IRQF_SHARED, dev->name, dev);
967 if (unlikely(ret < 0)) {
968 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
969 __func__, dev->irq, ret);
970 goto open_error;
971 }
972
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +0000973 /* Request the Wake IRQ in case of another line is used for WoL */
974 if (priv->wol_irq != dev->irq) {
975 ret = request_irq(priv->wol_irq, stmmac_interrupt,
976 IRQF_SHARED, dev->name, dev);
977 if (unlikely(ret < 0)) {
978 pr_err("%s: ERROR: allocating the ext WoL IRQ %d "
979 "(error: %d)\n", __func__, priv->wol_irq, ret);
980 goto open_error_wolirq;
981 }
982 }
983
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700984 /* Enable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000985 stmmac_set_mac(priv->ioaddr, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700986
987 /* Set the HW DMA mode and the COE */
988 stmmac_dma_operation_mode(priv);
989
990 /* Extra statistics */
991 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
992 priv->xstats.threshold = tc;
993
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000994 stmmac_mmc_setup(priv);
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000995
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000996#ifdef CONFIG_STMMAC_DEBUG_FS
997 ret = stmmac_init_fs(dev);
998 if (ret < 0)
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000999 pr_warning("%s: failed debugFS registration\n", __func__);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001000#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001001 /* Start the ball rolling... */
1002 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001003 priv->hw->dma->start_tx(priv->ioaddr);
1004 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001005
1006#ifdef CONFIG_STMMAC_TIMER
1007 priv->tm->timer_start(tmrate);
1008#endif
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001009
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001010 /* Dump DMA/MAC registers */
1011 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001012 priv->hw->mac->dump_regs(priv->ioaddr);
1013 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001014 }
1015
1016 if (priv->phydev)
1017 phy_start(priv->phydev);
1018
1019 napi_enable(&priv->napi);
1020 skb_queue_head_init(&priv->rx_recycle);
1021 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001022
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001023 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001024
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001025open_error_wolirq:
1026 free_irq(dev->irq, dev);
1027
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001028open_error:
1029#ifdef CONFIG_STMMAC_TIMER
1030 kfree(priv->tm);
1031#endif
1032 if (priv->phydev)
1033 phy_disconnect(priv->phydev);
1034
1035 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001036}
1037
1038/**
1039 * stmmac_release - close entry point of the driver
1040 * @dev : device pointer.
1041 * Description:
1042 * This is the stop entry point of the driver.
1043 */
1044static int stmmac_release(struct net_device *dev)
1045{
1046 struct stmmac_priv *priv = netdev_priv(dev);
1047
1048 /* Stop and disconnect the PHY */
1049 if (priv->phydev) {
1050 phy_stop(priv->phydev);
1051 phy_disconnect(priv->phydev);
1052 priv->phydev = NULL;
1053 }
1054
1055 netif_stop_queue(dev);
1056
1057#ifdef CONFIG_STMMAC_TIMER
1058 /* Stop and release the timer */
1059 stmmac_close_ext_timer();
1060 if (priv->tm != NULL)
1061 kfree(priv->tm);
1062#endif
1063 napi_disable(&priv->napi);
1064 skb_queue_purge(&priv->rx_recycle);
1065
1066 /* Free the IRQ lines */
1067 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001068 if (priv->wol_irq != dev->irq)
1069 free_irq(priv->wol_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001070
1071 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001072 priv->hw->dma->stop_tx(priv->ioaddr);
1073 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001074
1075 /* Release and free the Rx/Tx resources */
1076 free_dma_desc_resources(priv);
1077
avisconti19449bf2010-10-25 18:58:14 +00001078 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001079 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001080
1081 netif_carrier_off(dev);
1082
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001083#ifdef CONFIG_STMMAC_DEBUG_FS
1084 stmmac_exit_fs();
1085#endif
1086 stmmac_mdio_unregister(dev);
1087
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001088 return 0;
1089}
1090
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001091/**
1092 * stmmac_xmit:
1093 * @skb : the socket buffer
1094 * @dev : device pointer
1095 * Description : Tx entry point of the driver.
1096 */
1097static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1098{
1099 struct stmmac_priv *priv = netdev_priv(dev);
1100 unsigned int txsize = priv->dma_tx_size;
1101 unsigned int entry;
1102 int i, csum_insertion = 0;
1103 int nfrags = skb_shinfo(skb)->nr_frags;
1104 struct dma_desc *desc, *first;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001105 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001106
1107 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1108 if (!netif_queue_stopped(dev)) {
1109 netif_stop_queue(dev);
1110 /* This is a hard error, log it. */
1111 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1112 __func__);
1113 }
1114 return NETDEV_TX_BUSY;
1115 }
1116
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001117 spin_lock(&priv->tx_lock);
1118
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001119 entry = priv->cur_tx % txsize;
1120
1121#ifdef STMMAC_XMIT_DEBUG
1122 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1123 pr_info("stmmac xmit:\n"
1124 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1125 "\tn_frags: %d - ip_summed: %d - %s gso\n",
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001126 skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001127 !skb_is_gso(skb) ? "isn't" : "is");
1128#endif
1129
Michał Mirosław5e982f32011-04-09 02:46:55 +00001130 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001131
1132 desc = priv->dma_tx + entry;
1133 first = desc;
1134
1135#ifdef STMMAC_XMIT_DEBUG
1136 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1137 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1138 "\t\tn_frags: %d, ip_summed: %d\n",
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001139 skb->len, nopaged_len, nfrags, skb->ip_summed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001140#endif
1141 priv->tx_skbuff[entry] = skb;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001142
1143 if (priv->hw->ring->is_jumbo_frm(skb->len, priv->plat->enh_desc)) {
1144 entry = priv->hw->ring->jumbo_frm(priv, skb, csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001145 desc = priv->dma_tx + entry;
1146 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001147 desc->des2 = dma_map_single(priv->device, skb->data,
1148 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001149 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1150 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001151 }
1152
1153 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001154 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1155 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001156
1157 entry = (++priv->cur_tx) % txsize;
1158 desc = priv->dma_tx + entry;
1159
1160 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
Ian Campbellf7223802011-09-21 21:53:20 +00001161 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1162 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001163 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001164 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001165 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001166 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001167 }
1168
1169 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001170 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001171
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001172#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001173 /* Clean IC while using timer */
1174 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001175 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001176#endif
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001177
1178 wmb();
1179
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001180 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001181 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001182
1183 priv->cur_tx++;
1184
1185#ifdef STMMAC_XMIT_DEBUG
1186 if (netif_msg_pktdata(priv)) {
1187 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1188 "first=%p, nfrags=%d\n",
1189 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1190 entry, first, nfrags);
1191 display_ring(priv->dma_tx, txsize);
1192 pr_info(">>> frame to be transmitted: ");
1193 print_pkt(skb->data, skb->len);
1194 }
1195#endif
1196 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1197 TX_DBG("%s: stop transmitted packets\n", __func__);
1198 netif_stop_queue(dev);
1199 }
1200
1201 dev->stats.tx_bytes += skb->len;
1202
Richard Cochran3e82ce12011-06-12 02:19:06 +00001203 skb_tx_timestamp(skb);
1204
Richard Cochran52f64fa2011-06-19 03:31:43 +00001205 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1206
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001207 spin_unlock(&priv->tx_lock);
1208
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001209 return NETDEV_TX_OK;
1210}
1211
1212static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1213{
1214 unsigned int rxsize = priv->dma_rx_size;
1215 int bfsize = priv->dma_buf_sz;
1216 struct dma_desc *p = priv->dma_rx;
1217
1218 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1219 unsigned int entry = priv->dirty_rx % rxsize;
1220 if (likely(priv->rx_skbuff[entry] == NULL)) {
1221 struct sk_buff *skb;
1222
1223 skb = __skb_dequeue(&priv->rx_recycle);
1224 if (skb == NULL)
1225 skb = netdev_alloc_skb_ip_align(priv->dev,
1226 bfsize);
1227
1228 if (unlikely(skb == NULL))
1229 break;
1230
1231 priv->rx_skbuff[entry] = skb;
1232 priv->rx_skbuff_dma[entry] =
1233 dma_map_single(priv->device, skb->data, bfsize,
1234 DMA_FROM_DEVICE);
1235
1236 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001237
1238 if (unlikely(priv->plat->has_gmac))
1239 priv->hw->ring->refill_desc3(bfsize, p + entry);
1240
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001241 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1242 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001243 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001244 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001245 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001246}
1247
1248static int stmmac_rx(struct stmmac_priv *priv, int limit)
1249{
1250 unsigned int rxsize = priv->dma_rx_size;
1251 unsigned int entry = priv->cur_rx % rxsize;
1252 unsigned int next_entry;
1253 unsigned int count = 0;
1254 struct dma_desc *p = priv->dma_rx + entry;
1255 struct dma_desc *p_next;
1256
1257#ifdef STMMAC_RX_DEBUG
1258 if (netif_msg_hw(priv)) {
1259 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1260 display_ring(priv->dma_rx, rxsize);
1261 }
1262#endif
1263 count = 0;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001264 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001265 int status;
1266
1267 if (count >= limit)
1268 break;
1269
1270 count++;
1271
1272 next_entry = (++priv->cur_rx) % rxsize;
1273 p_next = priv->dma_rx + next_entry;
1274 prefetch(p_next);
1275
1276 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001277 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1278 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001279 if (unlikely(status == discard_frame))
1280 priv->dev->stats.rx_errors++;
1281 else {
1282 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001283 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001284
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001285 frame_len = priv->hw->desc->get_rx_frame_len(p,
1286 priv->plat->rx_coe);
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001287 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1288 * Type frames (LLC/LLC-SNAP) */
1289 if (unlikely(status != llc_snap))
1290 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001291#ifdef STMMAC_RX_DEBUG
1292 if (frame_len > ETH_FRAME_LEN)
1293 pr_debug("\tRX frame size %d, COE status: %d\n",
1294 frame_len, status);
1295
1296 if (netif_msg_hw(priv))
1297 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1298 p, entry, p->des2);
1299#endif
1300 skb = priv->rx_skbuff[entry];
1301 if (unlikely(!skb)) {
1302 pr_err("%s: Inconsistent Rx descriptor chain\n",
1303 priv->dev->name);
1304 priv->dev->stats.rx_dropped++;
1305 break;
1306 }
1307 prefetch(skb->data - NET_IP_ALIGN);
1308 priv->rx_skbuff[entry] = NULL;
1309
1310 skb_put(skb, frame_len);
1311 dma_unmap_single(priv->device,
1312 priv->rx_skbuff_dma[entry],
1313 priv->dma_buf_sz, DMA_FROM_DEVICE);
1314#ifdef STMMAC_RX_DEBUG
1315 if (netif_msg_pktdata(priv)) {
1316 pr_info(" frame received (%dbytes)", frame_len);
1317 print_pkt(skb->data, frame_len);
1318 }
1319#endif
1320 skb->protocol = eth_type_trans(skb, priv->dev);
1321
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001322 if (unlikely(!priv->plat->rx_coe)) {
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001323 /* No RX COE for old mac10/100 devices */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001324 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001325 netif_receive_skb(skb);
1326 } else {
1327 skb->ip_summed = CHECKSUM_UNNECESSARY;
1328 napi_gro_receive(&priv->napi, skb);
1329 }
1330
1331 priv->dev->stats.rx_packets++;
1332 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001333 }
1334 entry = next_entry;
1335 p = p_next; /* use prefetched values */
1336 }
1337
1338 stmmac_rx_refill(priv);
1339
1340 priv->xstats.rx_pkt_n += count;
1341
1342 return count;
1343}
1344
1345/**
1346 * stmmac_poll - stmmac poll method (NAPI)
1347 * @napi : pointer to the napi structure.
1348 * @budget : maximum number of packets that the current CPU can receive from
1349 * all interfaces.
1350 * Description :
1351 * This function implements the the reception process.
1352 * Also it runs the TX completion thread
1353 */
1354static int stmmac_poll(struct napi_struct *napi, int budget)
1355{
1356 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1357 int work_done = 0;
1358
1359 priv->xstats.poll_n++;
1360 stmmac_tx(priv);
1361 work_done = stmmac_rx(priv, budget);
1362
1363 if (work_done < budget) {
1364 napi_complete(napi);
1365 stmmac_enable_irq(priv);
1366 }
1367 return work_done;
1368}
1369
1370/**
1371 * stmmac_tx_timeout
1372 * @dev : Pointer to net device structure
1373 * Description: this function is called when a packet transmission fails to
1374 * complete within a reasonable tmrate. The driver will mark the error in the
1375 * netdev structure and arrange for the device to be reset to a sane state
1376 * in order to transmit a new packet.
1377 */
1378static void stmmac_tx_timeout(struct net_device *dev)
1379{
1380 struct stmmac_priv *priv = netdev_priv(dev);
1381
1382 /* Clear Tx resources and restart transmitting again */
1383 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001384}
1385
1386/* Configuration changes (passed on by ifconfig) */
1387static int stmmac_config(struct net_device *dev, struct ifmap *map)
1388{
1389 if (dev->flags & IFF_UP) /* can't act on a running interface */
1390 return -EBUSY;
1391
1392 /* Don't allow changing the I/O address */
1393 if (map->base_addr != dev->base_addr) {
1394 pr_warning("%s: can't change I/O address\n", dev->name);
1395 return -EOPNOTSUPP;
1396 }
1397
1398 /* Don't allow changing the IRQ */
1399 if (map->irq != dev->irq) {
1400 pr_warning("%s: can't change IRQ number %d\n",
1401 dev->name, dev->irq);
1402 return -EOPNOTSUPP;
1403 }
1404
1405 /* ignore other fields */
1406 return 0;
1407}
1408
1409/**
Jiri Pirko01789342011-08-16 06:29:00 +00001410 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001411 * @dev : pointer to the device structure
1412 * Description:
1413 * This function is a driver entry point which gets called by the kernel
1414 * whenever multicast addresses must be enabled/disabled.
1415 * Return value:
1416 * void.
1417 */
Jiri Pirko01789342011-08-16 06:29:00 +00001418static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001419{
1420 struct stmmac_priv *priv = netdev_priv(dev);
1421
1422 spin_lock(&priv->lock);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001423 priv->hw->mac->set_filter(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001424 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001425}
1426
1427/**
1428 * stmmac_change_mtu - entry point to change MTU size for the device.
1429 * @dev : device pointer.
1430 * @new_mtu : the new MTU size for the device.
1431 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1432 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1433 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1434 * Return value:
1435 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1436 * file on failure.
1437 */
1438static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1439{
1440 struct stmmac_priv *priv = netdev_priv(dev);
1441 int max_mtu;
1442
1443 if (netif_running(dev)) {
1444 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1445 return -EBUSY;
1446 }
1447
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00001448 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001449 max_mtu = JUMBO_LEN;
1450 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00001451 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001452
1453 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1454 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1455 return -EINVAL;
1456 }
1457
Michał Mirosław5e982f32011-04-09 02:46:55 +00001458 dev->mtu = new_mtu;
1459 netdev_update_features(dev);
1460
1461 return 0;
1462}
1463
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001464static netdev_features_t stmmac_fix_features(struct net_device *dev,
1465 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001466{
1467 struct stmmac_priv *priv = netdev_priv(dev);
1468
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001469 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001470 features &= ~NETIF_F_RXCSUM;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001471 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
1472 features &= ~NETIF_F_IPV6_CSUM;
Michał Mirosław5e982f32011-04-09 02:46:55 +00001473 if (!priv->plat->tx_coe)
1474 features &= ~NETIF_F_ALL_CSUM;
1475
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001476 /* Some GMAC devices have a bugged Jumbo frame support that
1477 * needs to have the Tx COE disabled for oversized frames
1478 * (due to limited buffer sizes). In this case we disable
1479 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001480 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1481 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001482
Michał Mirosław5e982f32011-04-09 02:46:55 +00001483 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001484}
1485
1486static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1487{
1488 struct net_device *dev = (struct net_device *)dev_id;
1489 struct stmmac_priv *priv = netdev_priv(dev);
1490
1491 if (unlikely(!dev)) {
1492 pr_err("%s: invalid dev pointer\n", __func__);
1493 return IRQ_NONE;
1494 }
1495
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001496 if (priv->plat->has_gmac)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001497 /* To handle GMAC own interrupts */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001498 priv->hw->mac->host_irq_status((void __iomem *) dev->base_addr);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001499
1500 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001501
1502 return IRQ_HANDLED;
1503}
1504
1505#ifdef CONFIG_NET_POLL_CONTROLLER
1506/* Polling receive - used by NETCONSOLE and other diagnostic tools
1507 * to allow network I/O with interrupts disabled. */
1508static void stmmac_poll_controller(struct net_device *dev)
1509{
1510 disable_irq(dev->irq);
1511 stmmac_interrupt(dev->irq, dev);
1512 enable_irq(dev->irq);
1513}
1514#endif
1515
1516/**
1517 * stmmac_ioctl - Entry point for the Ioctl
1518 * @dev: Device pointer.
1519 * @rq: An IOCTL specefic structure, that can contain a pointer to
1520 * a proprietary structure used to pass information to the driver.
1521 * @cmd: IOCTL command
1522 * Description:
1523 * Currently there are no special functionality supported in IOCTL, just the
1524 * phy_mii_ioctl(...) can be invoked.
1525 */
1526static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1527{
1528 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001529 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001530
1531 if (!netif_running(dev))
1532 return -EINVAL;
1533
Richard Cochran28b04112010-07-17 08:48:55 +00001534 if (!priv->phydev)
1535 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001536
Richard Cochran28b04112010-07-17 08:48:55 +00001537 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
Richard Cochran28b04112010-07-17 08:48:55 +00001538
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001539 return ret;
1540}
1541
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001542#ifdef CONFIG_STMMAC_DEBUG_FS
1543static struct dentry *stmmac_fs_dir;
1544static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001545static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001546
1547static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
1548{
1549 struct tmp_s {
1550 u64 a;
1551 unsigned int b;
1552 unsigned int c;
1553 };
1554 int i;
1555 struct net_device *dev = seq->private;
1556 struct stmmac_priv *priv = netdev_priv(dev);
1557
1558 seq_printf(seq, "=======================\n");
1559 seq_printf(seq, " RX descriptor ring\n");
1560 seq_printf(seq, "=======================\n");
1561
1562 for (i = 0; i < priv->dma_rx_size; i++) {
1563 struct tmp_s *x = (struct tmp_s *)(priv->dma_rx + i);
1564 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1565 i, (unsigned int)(x->a),
1566 (unsigned int)((x->a) >> 32), x->b, x->c);
1567 seq_printf(seq, "\n");
1568 }
1569
1570 seq_printf(seq, "\n");
1571 seq_printf(seq, "=======================\n");
1572 seq_printf(seq, " TX descriptor ring\n");
1573 seq_printf(seq, "=======================\n");
1574
1575 for (i = 0; i < priv->dma_tx_size; i++) {
1576 struct tmp_s *x = (struct tmp_s *)(priv->dma_tx + i);
1577 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1578 i, (unsigned int)(x->a),
1579 (unsigned int)((x->a) >> 32), x->b, x->c);
1580 seq_printf(seq, "\n");
1581 }
1582
1583 return 0;
1584}
1585
1586static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
1587{
1588 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
1589}
1590
1591static const struct file_operations stmmac_rings_status_fops = {
1592 .owner = THIS_MODULE,
1593 .open = stmmac_sysfs_ring_open,
1594 .read = seq_read,
1595 .llseek = seq_lseek,
1596 .release = seq_release,
1597};
1598
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001599static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
1600{
1601 struct net_device *dev = seq->private;
1602 struct stmmac_priv *priv = netdev_priv(dev);
1603
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001604 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001605 seq_printf(seq, "DMA HW features not supported\n");
1606 return 0;
1607 }
1608
1609 seq_printf(seq, "==============================\n");
1610 seq_printf(seq, "\tDMA HW features\n");
1611 seq_printf(seq, "==============================\n");
1612
1613 seq_printf(seq, "\t10/100 Mbps %s\n",
1614 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
1615 seq_printf(seq, "\t1000 Mbps %s\n",
1616 (priv->dma_cap.mbps_1000) ? "Y" : "N");
1617 seq_printf(seq, "\tHalf duple %s\n",
1618 (priv->dma_cap.half_duplex) ? "Y" : "N");
1619 seq_printf(seq, "\tHash Filter: %s\n",
1620 (priv->dma_cap.hash_filter) ? "Y" : "N");
1621 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
1622 (priv->dma_cap.multi_addr) ? "Y" : "N");
1623 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
1624 (priv->dma_cap.pcs) ? "Y" : "N");
1625 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
1626 (priv->dma_cap.sma_mdio) ? "Y" : "N");
1627 seq_printf(seq, "\tPMT Remote wake up: %s\n",
1628 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
1629 seq_printf(seq, "\tPMT Magic Frame: %s\n",
1630 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
1631 seq_printf(seq, "\tRMON module: %s\n",
1632 (priv->dma_cap.rmon) ? "Y" : "N");
1633 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
1634 (priv->dma_cap.time_stamp) ? "Y" : "N");
1635 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
1636 (priv->dma_cap.atime_stamp) ? "Y" : "N");
1637 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
1638 (priv->dma_cap.eee) ? "Y" : "N");
1639 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
1640 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
1641 (priv->dma_cap.tx_coe) ? "Y" : "N");
1642 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
1643 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
1644 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
1645 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
1646 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
1647 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
1648 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
1649 priv->dma_cap.number_rx_channel);
1650 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
1651 priv->dma_cap.number_tx_channel);
1652 seq_printf(seq, "\tEnhanced descriptors: %s\n",
1653 (priv->dma_cap.enh_desc) ? "Y" : "N");
1654
1655 return 0;
1656}
1657
1658static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
1659{
1660 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
1661}
1662
1663static const struct file_operations stmmac_dma_cap_fops = {
1664 .owner = THIS_MODULE,
1665 .open = stmmac_sysfs_dma_cap_open,
1666 .read = seq_read,
1667 .llseek = seq_lseek,
1668 .release = seq_release,
1669};
1670
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001671static int stmmac_init_fs(struct net_device *dev)
1672{
1673 /* Create debugfs entries */
1674 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
1675
1676 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
1677 pr_err("ERROR %s, debugfs create directory failed\n",
1678 STMMAC_RESOURCE_NAME);
1679
1680 return -ENOMEM;
1681 }
1682
1683 /* Entry to report DMA RX/TX rings */
1684 stmmac_rings_status = debugfs_create_file("descriptors_status",
1685 S_IRUGO, stmmac_fs_dir, dev,
1686 &stmmac_rings_status_fops);
1687
1688 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
1689 pr_info("ERROR creating stmmac ring debugfs file\n");
1690 debugfs_remove(stmmac_fs_dir);
1691
1692 return -ENOMEM;
1693 }
1694
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001695 /* Entry to report the DMA HW features */
1696 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
1697 dev, &stmmac_dma_cap_fops);
1698
1699 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
1700 pr_info("ERROR creating stmmac MMC debugfs file\n");
1701 debugfs_remove(stmmac_rings_status);
1702 debugfs_remove(stmmac_fs_dir);
1703
1704 return -ENOMEM;
1705 }
1706
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001707 return 0;
1708}
1709
1710static void stmmac_exit_fs(void)
1711{
1712 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001713 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001714 debugfs_remove(stmmac_fs_dir);
1715}
1716#endif /* CONFIG_STMMAC_DEBUG_FS */
1717
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001718static const struct net_device_ops stmmac_netdev_ops = {
1719 .ndo_open = stmmac_open,
1720 .ndo_start_xmit = stmmac_xmit,
1721 .ndo_stop = stmmac_release,
1722 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001723 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00001724 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001725 .ndo_tx_timeout = stmmac_tx_timeout,
1726 .ndo_do_ioctl = stmmac_ioctl,
1727 .ndo_set_config = stmmac_config,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001728#ifdef CONFIG_NET_POLL_CONTROLLER
1729 .ndo_poll_controller = stmmac_poll_controller,
1730#endif
1731 .ndo_set_mac_address = eth_mac_addr,
1732};
1733
1734/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001735 * stmmac_hw_init - Init the MAC device
1736 * @priv : pointer to the private device structure.
1737 * Description: this function detects which MAC device
1738 * (GMAC/MAC10-100) has to attached, checks the HW capability
1739 * (if supported) and sets the driver's features (for example
1740 * to use the ring or chaine mode or support the normal/enh
1741 * descriptor structure).
1742 */
1743static int stmmac_hw_init(struct stmmac_priv *priv)
1744{
1745 int ret = 0;
1746 struct mac_device_info *mac;
1747
1748 /* Identify the MAC HW device */
1749 if (priv->plat->has_gmac)
1750 mac = dwmac1000_setup(priv->ioaddr);
1751 else
1752 mac = dwmac100_setup(priv->ioaddr);
1753 if (!mac)
1754 return -ENOMEM;
1755
1756 priv->hw = mac;
1757
1758 /* To use the chained or ring mode */
1759 priv->hw->ring = &ring_mode_ops;
1760
1761 /* Get and dump the chip ID */
1762 stmmac_get_synopsys_id(priv);
1763
1764 /* Get the HW capability (new GMAC newer than 3.50a) */
1765 priv->hw_cap_support = stmmac_get_hw_features(priv);
1766 if (priv->hw_cap_support) {
1767 pr_info(" DMA HW capability register supported");
1768
1769 /* We can override some gmac/dma configuration fields: e.g.
1770 * enh_desc, tx_coe (e.g. that are passed through the
1771 * platform) with the values from the HW capability
1772 * register (if supported).
1773 */
1774 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001775 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001776
1777 priv->plat->tx_coe = priv->dma_cap.tx_coe;
1778
1779 if (priv->dma_cap.rx_coe_type2)
1780 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
1781 else if (priv->dma_cap.rx_coe_type1)
1782 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
1783
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001784 } else
1785 pr_info(" No HW DMA feature register supported");
1786
1787 /* Select the enhnaced/normal descriptor structures */
1788 stmmac_selec_desc_mode(priv);
1789
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001790 /* Enable the IPC (Checksum Offload) and check if the feature has been
1791 * enabled during the core configuration. */
1792 ret = priv->hw->mac->rx_ipc(priv->ioaddr);
1793 if (!ret) {
1794 pr_warning(" RX IPC Checksum Offload not configured.\n");
1795 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
1796 }
1797
1798 if (priv->plat->rx_coe)
1799 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
1800 priv->plat->rx_coe);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001801 if (priv->plat->tx_coe)
1802 pr_info(" TX Checksum insertion supported\n");
1803
1804 if (priv->plat->pmt) {
1805 pr_info(" Wake-Up On Lan supported\n");
1806 device_set_wakeup_capable(priv->device, 1);
1807 }
1808
1809 return ret;
1810}
1811
1812/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001813 * stmmac_dvr_probe
1814 * @device: device pointer
1815 * Description: this is the main probe function used to
1816 * call the alloc_etherdev, allocate the priv structure.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001817 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001818struct stmmac_priv *stmmac_dvr_probe(struct device *device,
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001819 struct plat_stmmacenet_data *plat_dat,
1820 void __iomem *addr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001821{
1822 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001823 struct net_device *ndev = NULL;
1824 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001825
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001826 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00001827 if (!ndev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001828 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001829
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001830 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001831
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001832 priv = netdev_priv(ndev);
1833 priv->device = device;
1834 priv->dev = ndev;
1835
1836 ether_setup(ndev);
1837
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001838 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001839 priv->pause = pause;
1840 priv->plat = plat_dat;
1841 priv->ioaddr = addr;
1842 priv->dev->base_addr = (unsigned long)addr;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001843
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001844 /* Verify driver arguments */
1845 stmmac_verify_args();
1846
1847 /* Override with kernel parameters if supplied XXX CRS XXX
1848 * this needs to have multiple instances */
1849 if ((phyaddr >= 0) && (phyaddr <= 31))
1850 priv->plat->phy_addr = phyaddr;
1851
1852 /* Init MAC and get the capabilities */
1853 stmmac_hw_init(priv);
1854
1855 ndev->netdev_ops = &stmmac_netdev_ops;
1856
1857 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1858 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001859 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
1860 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001861#ifdef STMMAC_VLAN_TAG_USED
1862 /* Both mac100 and gmac support receive VLAN tag detection */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001863 ndev->features |= NETIF_F_HW_VLAN_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001864#endif
1865 priv->msg_enable = netif_msg_init(debug, default_msg_level);
1866
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001867 if (flow_ctrl)
1868 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
1869
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001870 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001871
Vlad Lunguf8e96162010-11-29 22:52:52 +00001872 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001873 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00001874
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001875 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001876 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001877 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001878 goto error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001879 }
1880
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001881 return priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001882
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001883error:
1884 netif_napi_del(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001885
Dan Carpenter34a52f32010-12-20 21:34:56 +00001886 unregister_netdev(ndev);
Dan Carpenter34a52f32010-12-20 21:34:56 +00001887 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001888
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001889 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001890}
1891
1892/**
1893 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001894 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001895 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001896 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001897 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001898int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001899{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001900 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001901
1902 pr_info("%s:\n\tremoving driver", __func__);
1903
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001904 priv->hw->dma->stop_rx(priv->ioaddr);
1905 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001906
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001907 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001908 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001909 unregister_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001910 free_netdev(ndev);
1911
1912 return 0;
1913}
1914
1915#ifdef CONFIG_PM
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001916int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001917{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001918 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001919 int dis_ic = 0;
1920
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001921 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001922 return 0;
1923
Francesco Virlinzi102463b2011-11-16 21:58:02 +00001924 if (priv->phydev)
1925 phy_stop(priv->phydev);
1926
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001927 spin_lock(&priv->lock);
1928
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001929 netif_device_detach(ndev);
1930 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001931
1932#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001933 priv->tm->timer_stop();
1934 if (likely(priv->tm->enable))
1935 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001936#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001937 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001938
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001939 /* Stop TX/RX DMA */
1940 priv->hw->dma->stop_tx(priv->ioaddr);
1941 priv->hw->dma->stop_rx(priv->ioaddr);
1942 /* Clear the Rx/Tx descriptors */
1943 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
1944 dis_ic);
1945 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001946
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001947 /* Enable Power down mode by programming the PMT regs */
1948 if (device_may_wakeup(priv->device))
1949 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
1950 else
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001951 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001952
1953 spin_unlock(&priv->lock);
1954 return 0;
1955}
1956
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001957int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001958{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001959 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001960
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001961 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001962 return 0;
1963
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02001964 spin_lock(&priv->lock);
1965
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001966 /* Power Down bit, into the PM register, is cleared
1967 * automatically as soon as a magic packet or a Wake-up frame
1968 * is received. Anyway, it's better to manually clear
1969 * this bit because it can generate problems while resuming
1970 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001971 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07001972 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001973
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001974 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001975
1976 /* Enable the MAC and DMA */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001977 stmmac_set_mac(priv->ioaddr, true);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001978 priv->hw->dma->start_tx(priv->ioaddr);
1979 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001980
1981#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001982 if (likely(priv->tm->enable))
1983 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001984#endif
1985 napi_enable(&priv->napi);
1986
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001987 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001988
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001989 spin_unlock(&priv->lock);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00001990
1991 if (priv->phydev)
1992 phy_start(priv->phydev);
1993
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001994 return 0;
1995}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001996
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001997int stmmac_freeze(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001998{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00001999 if (!ndev || !netif_running(ndev))
2000 return 0;
2001
2002 return stmmac_release(ndev);
2003}
2004
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002005int stmmac_restore(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002006{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002007 if (!ndev || !netif_running(ndev))
2008 return 0;
2009
2010 return stmmac_open(ndev);
2011}
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002012#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002013
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002014#ifndef MODULE
2015static int __init stmmac_cmdline_opt(char *str)
2016{
2017 char *opt;
2018
2019 if (!str || !*str)
2020 return -EINVAL;
2021 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002022 if (!strncmp(opt, "debug:", 6)) {
2023 if (strict_strtoul(opt + 6, 0, (unsigned long *)&debug))
2024 goto err;
2025 } else if (!strncmp(opt, "phyaddr:", 8)) {
2026 if (strict_strtoul(opt + 8, 0,
2027 (unsigned long *)&phyaddr))
2028 goto err;
2029 } else if (!strncmp(opt, "dma_txsize:", 11)) {
2030 if (strict_strtoul(opt + 11, 0,
2031 (unsigned long *)&dma_txsize))
2032 goto err;
2033 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
2034 if (strict_strtoul(opt + 11, 0,
2035 (unsigned long *)&dma_rxsize))
2036 goto err;
2037 } else if (!strncmp(opt, "buf_sz:", 7)) {
2038 if (strict_strtoul(opt + 7, 0,
2039 (unsigned long *)&buf_sz))
2040 goto err;
2041 } else if (!strncmp(opt, "tc:", 3)) {
2042 if (strict_strtoul(opt + 3, 0, (unsigned long *)&tc))
2043 goto err;
2044 } else if (!strncmp(opt, "watchdog:", 9)) {
2045 if (strict_strtoul(opt + 9, 0,
2046 (unsigned long *)&watchdog))
2047 goto err;
2048 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
2049 if (strict_strtoul(opt + 10, 0,
2050 (unsigned long *)&flow_ctrl))
2051 goto err;
2052 } else if (!strncmp(opt, "pause:", 6)) {
2053 if (strict_strtoul(opt + 6, 0, (unsigned long *)&pause))
2054 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002055#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002056 } else if (!strncmp(opt, "tmrate:", 7)) {
2057 if (strict_strtoul(opt + 7, 0,
2058 (unsigned long *)&tmrate))
2059 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002060#endif
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002061 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002062 }
2063 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002064
2065err:
2066 pr_err("%s: ERROR broken module parameter conversion", __func__);
2067 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002068}
2069
2070__setup("stmmaceth=", stmmac_cmdline_opt);
2071#endif
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05002072
2073MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
2074MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2075MODULE_LICENSE("GPL");