blob: ea3bc0963bd70461b688b947d6c6466f9791bc3b [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
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000136#define STMMAC_DEFAULT_LPI_TIMER 1000
137static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
138module_param(eee_timer, int, S_IRUGO | S_IWUSR);
139MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
140#define STMMAC_LPI_TIMER(x) (jiffies + msecs_to_jiffies(x))
141
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700142static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700143
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000144#ifdef CONFIG_STMMAC_DEBUG_FS
145static int stmmac_init_fs(struct net_device *dev);
146static void stmmac_exit_fs(void);
147#endif
148
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700149/**
150 * stmmac_verify_args - verify the driver parameters.
151 * Description: it verifies if some wrong parameter is passed to the driver.
152 * Note that wrong parameters are replaced with the default values.
153 */
154static void stmmac_verify_args(void)
155{
156 if (unlikely(watchdog < 0))
157 watchdog = TX_TIMEO;
158 if (unlikely(dma_rxsize < 0))
159 dma_rxsize = DMA_RX_SIZE;
160 if (unlikely(dma_txsize < 0))
161 dma_txsize = DMA_TX_SIZE;
162 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
163 buf_sz = DMA_BUFFER_SIZE;
164 if (unlikely(flow_ctrl > 1))
165 flow_ctrl = FLOW_AUTO;
166 else if (likely(flow_ctrl < 0))
167 flow_ctrl = FLOW_OFF;
168 if (unlikely((pause < 0) || (pause > 0xffff)))
169 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000170 if (eee_timer < 0)
171 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700172}
173
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000174static void stmmac_clk_csr_set(struct stmmac_priv *priv)
175{
176#ifdef CONFIG_HAVE_CLK
177 u32 clk_rate;
178
Giuseppe CAVALLARO31ea38e2012-04-18 19:48:22 +0000179 if (IS_ERR(priv->stmmac_clk))
180 return;
181
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000182 clk_rate = clk_get_rate(priv->stmmac_clk);
183
184 /* Platform provided default clk_csr would be assumed valid
185 * for all other cases except for the below mentioned ones. */
186 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
187 if (clk_rate < CSR_F_35M)
188 priv->clk_csr = STMMAC_CSR_20_35M;
189 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
190 priv->clk_csr = STMMAC_CSR_35_60M;
191 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
192 priv->clk_csr = STMMAC_CSR_60_100M;
193 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
194 priv->clk_csr = STMMAC_CSR_100_150M;
195 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
196 priv->clk_csr = STMMAC_CSR_150_250M;
197 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
198 priv->clk_csr = STMMAC_CSR_250_300M;
199 } /* For values higher than the IEEE 802.3 specified frequency
200 * we can not estimate the proper divider as it is not known
201 * the frequency of clk_csr_i. So we do not change the default
202 * divider. */
203#endif
204}
205
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700206#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
207static void print_pkt(unsigned char *buf, int len)
208{
209 int j;
210 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
211 for (j = 0; j < len; j++) {
212 if ((j % 16) == 0)
213 pr_info("\n %03x:", j);
214 pr_info(" %02x", buf[j]);
215 }
216 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700217}
218#endif
219
220/* minimum number of free TX descriptors required to wake up TX process */
221#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
222
223static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
224{
225 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
226}
227
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000228/* On some ST platforms, some HW system configuraton registers have to be
229 * set according to the link speed negotiated.
230 */
231static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
232{
233 struct phy_device *phydev = priv->phydev;
234
235 if (likely(priv->plat->fix_mac_speed))
236 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
237 phydev->speed);
238}
239
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000240static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
241{
242 /* Check and enter in LPI mode */
243 if ((priv->dirty_tx == priv->cur_tx) &&
244 (priv->tx_path_in_lpi_mode == false))
245 priv->hw->mac->set_eee_mode(priv->ioaddr);
246}
247
248void stmmac_disable_eee_mode(struct stmmac_priv *priv)
249{
250 /* Exit and disable EEE in case of we are are in LPI state. */
251 priv->hw->mac->reset_eee_mode(priv->ioaddr);
252 del_timer_sync(&priv->eee_ctrl_timer);
253 priv->tx_path_in_lpi_mode = false;
254}
255
256/**
257 * stmmac_eee_ctrl_timer
258 * @arg : data hook
259 * Description:
260 * If there is no data transfer and if we are not in LPI state,
261 * then MAC Transmitter can be moved to LPI state.
262 */
263static void stmmac_eee_ctrl_timer(unsigned long arg)
264{
265 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
266
267 stmmac_enable_eee_mode(priv);
268 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
269}
270
271/**
272 * stmmac_eee_init
273 * @priv: private device pointer
274 * Description:
275 * If the EEE support has been enabled while configuring the driver,
276 * if the GMAC actually supports the EEE (from the HW cap reg) and the
277 * phy can also manage EEE, so enable the LPI state and start the timer
278 * to verify if the tx path can enter in LPI state.
279 */
280bool stmmac_eee_init(struct stmmac_priv *priv)
281{
282 bool ret = false;
283
284 /* MAC core supports the EEE feature. */
285 if (priv->dma_cap.eee) {
286 /* Check if the PHY supports EEE */
287 if (phy_init_eee(priv->phydev, 1))
288 goto out;
289
290 priv->eee_active = 1;
291 init_timer(&priv->eee_ctrl_timer);
292 priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
293 priv->eee_ctrl_timer.data = (unsigned long)priv;
294 priv->eee_ctrl_timer.expires = STMMAC_LPI_TIMER(eee_timer);
295 add_timer(&priv->eee_ctrl_timer);
296
297 priv->hw->mac->set_eee_timer(priv->ioaddr,
298 STMMAC_DEFAULT_LIT_LS_TIMER,
299 priv->tx_lpi_timer);
300
301 pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
302
303 ret = true;
304 }
305out:
306 return ret;
307}
308
309static void stmmac_eee_adjust(struct stmmac_priv *priv)
310{
311 /* When the EEE has been already initialised we have to
312 * modify the PLS bit in the LPI ctrl & status reg according
313 * to the PHY link status. For this reason.
314 */
315 if (priv->eee_enabled)
316 priv->hw->mac->set_eee_pls(priv->ioaddr, priv->phydev->link);
317}
318
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700319/**
320 * stmmac_adjust_link
321 * @dev: net device structure
322 * Description: it adjusts the link parameters.
323 */
324static void stmmac_adjust_link(struct net_device *dev)
325{
326 struct stmmac_priv *priv = netdev_priv(dev);
327 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700328 unsigned long flags;
329 int new_state = 0;
330 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
331
332 if (phydev == NULL)
333 return;
334
335 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
336 phydev->addr, phydev->link);
337
338 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000339
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700340 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000341 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700342
343 /* Now we make sure that we can be in full duplex mode.
344 * If not, we operate in half-duplex mode. */
345 if (phydev->duplex != priv->oldduplex) {
346 new_state = 1;
347 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000348 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700349 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000350 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700351 priv->oldduplex = phydev->duplex;
352 }
353 /* Flow Control operation */
354 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000355 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000356 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700357
358 if (phydev->speed != priv->speed) {
359 new_state = 1;
360 switch (phydev->speed) {
361 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000362 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000363 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000364 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700365 break;
366 case 100:
367 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000368 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000369 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700370 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000371 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700372 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000373 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700374 }
375 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000376 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700377 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000378 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700379 break;
380 default:
381 if (netif_msg_link(priv))
382 pr_warning("%s: Speed (%d) is not 10"
383 " or 100!\n", dev->name, phydev->speed);
384 break;
385 }
386
387 priv->speed = phydev->speed;
388 }
389
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000390 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700391
392 if (!priv->oldlink) {
393 new_state = 1;
394 priv->oldlink = 1;
395 }
396 } else if (priv->oldlink) {
397 new_state = 1;
398 priv->oldlink = 0;
399 priv->speed = 0;
400 priv->oldduplex = -1;
401 }
402
403 if (new_state && netif_msg_link(priv))
404 phy_print_status(phydev);
405
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000406 stmmac_eee_adjust(priv);
407
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700408 spin_unlock_irqrestore(&priv->lock, flags);
409
410 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
411}
412
413/**
414 * stmmac_init_phy - PHY initialization
415 * @dev: net device structure
416 * Description: it initializes the driver's PHY state, and attaches the PHY
417 * to the mac driver.
418 * Return value:
419 * 0 on success
420 */
421static int stmmac_init_phy(struct net_device *dev)
422{
423 struct stmmac_priv *priv = netdev_priv(dev);
424 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000425 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000426 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000427 int interface = priv->plat->interface;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700428 priv->oldlink = 0;
429 priv->speed = 0;
430 priv->oldduplex = -1;
431
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000432 if (priv->plat->phy_bus_name)
433 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
434 priv->plat->phy_bus_name, priv->plat->bus_id);
435 else
436 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
437 priv->plat->bus_id);
438
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000439 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000440 priv->plat->phy_addr);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000441 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700442
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000443 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, 0,
444 interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700445
446 if (IS_ERR(phydev)) {
447 pr_err("%s: Could not attach to PHY\n", dev->name);
448 return PTR_ERR(phydev);
449 }
450
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000451 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000452 if ((interface == PHY_INTERFACE_MODE_MII) ||
453 (interface == PHY_INTERFACE_MODE_RMII))
454 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
455 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000456
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700457 /*
458 * Broken HW is sometimes missing the pull-up resistor on the
459 * MDIO line, which results in reads to non-existent devices returning
460 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
461 * device as well.
462 * Note: phydev->phy_id is the result of reading the UID PHY registers.
463 */
464 if (phydev->phy_id == 0) {
465 phy_disconnect(phydev);
466 return -ENODEV;
467 }
468 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000469 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700470
471 priv->phydev = phydev;
472
473 return 0;
474}
475
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700476/**
477 * display_ring
478 * @p: pointer to the ring.
479 * @size: size of the ring.
480 * Description: display all the descriptors within the ring.
481 */
482static void display_ring(struct dma_desc *p, int size)
483{
484 struct tmp_s {
485 u64 a;
486 unsigned int b;
487 unsigned int c;
488 };
489 int i;
490 for (i = 0; i < size; i++) {
491 struct tmp_s *x = (struct tmp_s *)(p + i);
492 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
493 i, (unsigned int)virt_to_phys(&p[i]),
494 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
495 x->b, x->c);
496 pr_info("\n");
497 }
498}
499
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000500static int stmmac_set_bfsize(int mtu, int bufsize)
501{
502 int ret = bufsize;
503
504 if (mtu >= BUF_SIZE_4KiB)
505 ret = BUF_SIZE_8KiB;
506 else if (mtu >= BUF_SIZE_2KiB)
507 ret = BUF_SIZE_4KiB;
508 else if (mtu >= DMA_BUFFER_SIZE)
509 ret = BUF_SIZE_2KiB;
510 else
511 ret = DMA_BUFFER_SIZE;
512
513 return ret;
514}
515
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700516/**
517 * init_dma_desc_rings - init the RX/TX descriptor rings
518 * @dev: net device structure
519 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000520 * and allocates the socket buffers. It suppors the chained and ring
521 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700522 */
523static void init_dma_desc_rings(struct net_device *dev)
524{
525 int i;
526 struct stmmac_priv *priv = netdev_priv(dev);
527 struct sk_buff *skb;
528 unsigned int txsize = priv->dma_tx_size;
529 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000530 unsigned int bfsize;
531 int dis_ic = 0;
532 int des3_as_data_buf = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700533
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000534 /* Set the max buffer size according to the DESC mode
535 * and the MTU. Note that RING mode allows 16KiB bsize. */
536 bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
537
538 if (bfsize == BUF_SIZE_16KiB)
539 des3_as_data_buf = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700540 else
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000541 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700542
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000543#ifdef CONFIG_STMMAC_TIMER
544 /* Disable interrupts on completion for the reception if timer is on */
545 if (likely(priv->tm->enable))
546 dis_ic = 1;
547#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700548
549 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
550 txsize, rxsize, bfsize);
551
552 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
553 priv->rx_skbuff =
554 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
555 priv->dma_rx =
556 (struct dma_desc *)dma_alloc_coherent(priv->device,
557 rxsize *
558 sizeof(struct dma_desc),
559 &priv->dma_rx_phy,
560 GFP_KERNEL);
561 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
562 GFP_KERNEL);
563 priv->dma_tx =
564 (struct dma_desc *)dma_alloc_coherent(priv->device,
565 txsize *
566 sizeof(struct dma_desc),
567 &priv->dma_tx_phy,
568 GFP_KERNEL);
569
570 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
571 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
572 return;
573 }
574
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000575 DBG(probe, INFO, "stmmac (%s) DMA desc: virt addr (Rx %p, "
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700576 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
577 dev->name, priv->dma_rx, priv->dma_tx,
578 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
579
580 /* RX INITIALIZATION */
581 DBG(probe, INFO, "stmmac: SKB addresses:\n"
582 "skb\t\tskb data\tdma data\n");
583
584 for (i = 0; i < rxsize; i++) {
585 struct dma_desc *p = priv->dma_rx + i;
586
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +0000587 skb = __netdev_alloc_skb(dev, bfsize + NET_IP_ALIGN,
588 GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700589 if (unlikely(skb == NULL)) {
590 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
591 break;
592 }
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +0000593 skb_reserve(skb, NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700594 priv->rx_skbuff[i] = skb;
595 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
596 bfsize, DMA_FROM_DEVICE);
597
598 p->des2 = priv->rx_skbuff_dma[i];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000599
600 priv->hw->ring->init_desc3(des3_as_data_buf, p);
601
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700602 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
603 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
604 }
605 priv->cur_rx = 0;
606 priv->dirty_rx = (unsigned int)(i - rxsize);
607 priv->dma_buf_sz = bfsize;
608 buf_sz = bfsize;
609
610 /* TX INITIALIZATION */
611 for (i = 0; i < txsize; i++) {
612 priv->tx_skbuff[i] = NULL;
613 priv->dma_tx[i].des2 = 0;
614 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000615
616 /* In case of Chained mode this sets the des3 to the next
617 * element in the chain */
618 priv->hw->ring->init_dma_chain(priv->dma_rx, priv->dma_rx_phy, rxsize);
619 priv->hw->ring->init_dma_chain(priv->dma_tx, priv->dma_tx_phy, txsize);
620
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700621 priv->dirty_tx = 0;
622 priv->cur_tx = 0;
623
624 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000625 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
626 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700627
628 if (netif_msg_hw(priv)) {
629 pr_info("RX descriptor ring:\n");
630 display_ring(priv->dma_rx, rxsize);
631 pr_info("TX descriptor ring:\n");
632 display_ring(priv->dma_tx, txsize);
633 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700634}
635
636static void dma_free_rx_skbufs(struct stmmac_priv *priv)
637{
638 int i;
639
640 for (i = 0; i < priv->dma_rx_size; i++) {
641 if (priv->rx_skbuff[i]) {
642 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
643 priv->dma_buf_sz, DMA_FROM_DEVICE);
644 dev_kfree_skb_any(priv->rx_skbuff[i]);
645 }
646 priv->rx_skbuff[i] = NULL;
647 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700648}
649
650static void dma_free_tx_skbufs(struct stmmac_priv *priv)
651{
652 int i;
653
654 for (i = 0; i < priv->dma_tx_size; i++) {
655 if (priv->tx_skbuff[i] != NULL) {
656 struct dma_desc *p = priv->dma_tx + i;
657 if (p->des2)
658 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000659 priv->hw->desc->get_tx_len(p),
660 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700661 dev_kfree_skb_any(priv->tx_skbuff[i]);
662 priv->tx_skbuff[i] = NULL;
663 }
664 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700665}
666
667static void free_dma_desc_resources(struct stmmac_priv *priv)
668{
669 /* Release the DMA TX/RX socket buffers */
670 dma_free_rx_skbufs(priv);
671 dma_free_tx_skbufs(priv);
672
673 /* Free the region of consistent memory previously allocated for
674 * the DMA */
675 dma_free_coherent(priv->device,
676 priv->dma_tx_size * sizeof(struct dma_desc),
677 priv->dma_tx, priv->dma_tx_phy);
678 dma_free_coherent(priv->device,
679 priv->dma_rx_size * sizeof(struct dma_desc),
680 priv->dma_rx, priv->dma_rx_phy);
681 kfree(priv->rx_skbuff_dma);
682 kfree(priv->rx_skbuff);
683 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700684}
685
686/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700687 * stmmac_dma_operation_mode - HW DMA operation mode
688 * @priv : pointer to the private device structure.
689 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000690 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700691 */
692static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
693{
Srinivas Kandagatla61b80132011-07-17 20:54:09 +0000694 if (likely(priv->plat->force_sf_dma_mode ||
695 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
696 /*
697 * In case of GMAC, SF mode can be enabled
698 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000699 * 1) TX COE if actually supported
700 * 2) There is no bugged Jumbo frame support
701 * that needs to not insert csum in the TDES.
702 */
703 priv->hw->dma->dma_mode(priv->ioaddr,
704 SF_DMA_MODE, SF_DMA_MODE);
705 tc = SF_DMA_MODE;
706 } else
707 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700708}
709
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700710/**
711 * stmmac_tx:
712 * @priv: private driver structure
713 * Description: it reclaims resources after transmission completes.
714 */
715static void stmmac_tx(struct stmmac_priv *priv)
716{
717 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700718
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000719 spin_lock(&priv->tx_lock);
720
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700721 while (priv->dirty_tx != priv->cur_tx) {
722 int last;
723 unsigned int entry = priv->dirty_tx % txsize;
724 struct sk_buff *skb = priv->tx_skbuff[entry];
725 struct dma_desc *p = priv->dma_tx + entry;
726
727 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000728 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700729 break;
730
731 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000732 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700733 if (likely(last)) {
734 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000735 priv->hw->desc->tx_status(&priv->dev->stats,
736 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000737 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700738 if (likely(tx_error == 0)) {
739 priv->dev->stats.tx_packets++;
740 priv->xstats.tx_pkt_n++;
741 } else
742 priv->dev->stats.tx_errors++;
743 }
744 TX_DBG("%s: curr %d, dirty %d\n", __func__,
745 priv->cur_tx, priv->dirty_tx);
746
747 if (likely(p->des2))
748 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000749 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700750 DMA_TO_DEVICE);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000751 priv->hw->ring->clean_desc3(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700752
753 if (likely(skb != NULL)) {
754 /*
755 * If there's room in the queue (limit it to size)
756 * we add this skb back into the pool,
757 * if it's the right size.
758 */
759 if ((skb_queue_len(&priv->rx_recycle) <
760 priv->dma_rx_size) &&
761 skb_recycle_check(skb, priv->dma_buf_sz))
762 __skb_queue_head(&priv->rx_recycle, skb);
763 else
764 dev_kfree_skb(skb);
765
766 priv->tx_skbuff[entry] = NULL;
767 }
768
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000769 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700770
Giuseppe CAVALLARO13497f52012-06-04 06:36:22 +0000771 priv->dirty_tx++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700772 }
773 if (unlikely(netif_queue_stopped(priv->dev) &&
774 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
775 netif_tx_lock(priv->dev);
776 if (netif_queue_stopped(priv->dev) &&
777 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
778 TX_DBG("%s: restart transmit\n", __func__);
779 netif_wake_queue(priv->dev);
780 }
781 netif_tx_unlock(priv->dev);
782 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000783
784 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
785 stmmac_enable_eee_mode(priv);
786 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
787 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000788 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700789}
790
791static inline void stmmac_enable_irq(struct stmmac_priv *priv)
792{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000793#ifdef CONFIG_STMMAC_TIMER
794 if (likely(priv->tm->enable))
795 priv->tm->timer_start(tmrate);
796 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700797#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000798 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700799}
800
801static inline void stmmac_disable_irq(struct stmmac_priv *priv)
802{
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +0000803#ifdef CONFIG_STMMAC_TIMER
804 if (likely(priv->tm->enable))
805 priv->tm->timer_stop();
806 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700807#endif
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000808 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700809}
810
811static int stmmac_has_work(struct stmmac_priv *priv)
812{
813 unsigned int has_work = 0;
814 int rxret, tx_work = 0;
815
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000816 rxret = priv->hw->desc->get_rx_owner(priv->dma_rx +
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700817 (priv->cur_rx % priv->dma_rx_size));
818
819 if (priv->dirty_tx != priv->cur_tx)
820 tx_work = 1;
821
822 if (likely(!rxret || tx_work))
823 has_work = 1;
824
825 return has_work;
826}
827
828static inline void _stmmac_schedule(struct stmmac_priv *priv)
829{
830 if (likely(stmmac_has_work(priv))) {
831 stmmac_disable_irq(priv);
832 napi_schedule(&priv->napi);
833 }
834}
835
836#ifdef CONFIG_STMMAC_TIMER
837void stmmac_schedule(struct net_device *dev)
838{
839 struct stmmac_priv *priv = netdev_priv(dev);
840
841 priv->xstats.sched_timer_n++;
842
843 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700844}
845
846static void stmmac_no_timer_started(unsigned int x)
847{;
848};
849
850static void stmmac_no_timer_stopped(void)
851{;
852};
853#endif
854
855/**
856 * stmmac_tx_err:
857 * @priv: pointer to the private device structure
858 * Description: it cleans the descriptors and restarts the transmission
859 * in case of errors.
860 */
861static void stmmac_tx_err(struct stmmac_priv *priv)
862{
863 netif_stop_queue(priv->dev);
864
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000865 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700866 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000867 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700868 priv->dirty_tx = 0;
869 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000870 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700871
872 priv->dev->stats.tx_errors++;
873 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700874}
875
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000876
877static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700878{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000879 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700880
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000881 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000882 if (likely(status == handle_tx_rx))
883 _stmmac_schedule(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700884
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000885 else if (unlikely(status == tx_hard_error_bump_tc)) {
886 /* Try to bump up the dma threshold on this failure */
887 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
888 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000889 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000890 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700891 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000892 } else if (unlikely(status == tx_hard_error))
893 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700894}
895
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000896static void stmmac_mmc_setup(struct stmmac_priv *priv)
897{
898 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
899 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
900
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000901 /* Mask MMC irq, counters are managed in SW and registers
902 * are cleared on each READ eventually. */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000903 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000904
905 if (priv->dma_cap.rmon) {
906 dwmac_mmc_ctrl(priv->ioaddr, mode);
907 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
908 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +0000909 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000910}
911
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000912static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
913{
914 u32 hwid = priv->hw->synopsys_uid;
915
916 /* Only check valid Synopsys Id because old MAC chips
917 * have no HW registers where get the ID */
918 if (likely(hwid)) {
919 u32 uid = ((hwid & 0x0000ff00) >> 8);
920 u32 synid = (hwid & 0x000000ff);
921
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000922 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000923 uid, synid);
924
925 return synid;
926 }
927 return 0;
928}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000929
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000930/**
931 * stmmac_selec_desc_mode
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +0000932 * @priv : private structure
933 * Description: select the Enhanced/Alternate or Normal descriptors
934 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000935static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
936{
937 if (priv->plat->enh_desc) {
938 pr_info(" Enhanced/Alternate descriptors\n");
939 priv->hw->desc = &enh_desc_ops;
940 } else {
941 pr_info(" Normal descriptors\n");
942 priv->hw->desc = &ndesc_ops;
943 }
944}
945
946/**
947 * stmmac_get_hw_features
948 * @priv : private device pointer
949 * Description:
950 * new GMAC chip generations have a new register to indicate the
951 * presence of the optional feature/functions.
952 * This can be also used to override the value passed through the
953 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000954 */
955static int stmmac_get_hw_features(struct stmmac_priv *priv)
956{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +0000957 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +0000958
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +0000959 if (priv->hw->dma->get_hw_feature) {
960 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000961
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000962 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
963 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
964 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
965 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
966 priv->dma_cap.multi_addr =
967 (hw_cap & DMA_HW_FEAT_ADDMACADRSEL) >> 5;
968 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
969 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
970 priv->dma_cap.pmt_remote_wake_up =
971 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
972 priv->dma_cap.pmt_magic_frame =
973 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000974 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000975 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000976 /* IEEE 1588-2002*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000977 priv->dma_cap.time_stamp =
978 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000979 /* IEEE 1588-2008*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000980 priv->dma_cap.atime_stamp =
981 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000982 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000983 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
984 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000985 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000986 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
987 priv->dma_cap.rx_coe_type1 =
988 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
989 priv->dma_cap.rx_coe_type2 =
990 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
991 priv->dma_cap.rxfifo_over_2048 =
992 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000993 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000994 priv->dma_cap.number_rx_channel =
995 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
996 priv->dma_cap.number_tx_channel =
997 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000998 /* Alternate (enhanced) DESC mode*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000999 priv->dma_cap.enh_desc =
1000 (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001001
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001002 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001003
1004 return hw_cap;
1005}
1006
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001007static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1008{
1009 /* verify if the MAC address is valid, in case of failures it
1010 * generates a random MAC address */
1011 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
1012 priv->hw->mac->get_umac_addr((void __iomem *)
1013 priv->dev->base_addr,
1014 priv->dev->dev_addr, 0);
1015 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001016 eth_hw_addr_random(priv->dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001017 }
1018 pr_warning("%s: device MAC address %pM\n", priv->dev->name,
1019 priv->dev->dev_addr);
1020}
1021
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001022static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1023{
1024 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001025 int mixed_burst = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001026
1027 /* Some DMA parameters can be passed from the platform;
1028 * in case of these are not passed we keep a default
1029 * (good for all the chips) and init the DMA! */
1030 if (priv->plat->dma_cfg) {
1031 pbl = priv->plat->dma_cfg->pbl;
1032 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001033 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001034 burst_len = priv->plat->dma_cfg->burst_len;
1035 }
1036
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001037 return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001038 burst_len, priv->dma_tx_phy,
1039 priv->dma_rx_phy);
1040}
1041
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001042/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001043 * stmmac_open - open entry point of the driver
1044 * @dev : pointer to the device structure.
1045 * Description:
1046 * This function is the open entry point of the driver.
1047 * Return value:
1048 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1049 * file on failure.
1050 */
1051static int stmmac_open(struct net_device *dev)
1052{
1053 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001054 int ret;
1055
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001056#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001057 priv->tm = kzalloc(sizeof(struct stmmac_timer *), GFP_KERNEL);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001058 if (unlikely(priv->tm == NULL))
1059 return -ENOMEM;
Joe Perchese404dec2012-01-29 12:56:23 +00001060
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001061 priv->tm->freq = tmrate;
1062
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001063 /* Test if the external timer can be actually used.
1064 * In case of failure continue without timer. */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001065 if (unlikely((stmmac_open_ext_timer(dev, priv->tm)) < 0)) {
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001066 pr_warning("stmmaceth: cannot attach the external timer.\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001067 priv->tm->freq = 0;
1068 priv->tm->timer_start = stmmac_no_timer_started;
1069 priv->tm->timer_stop = stmmac_no_timer_stopped;
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001070 } else
1071 priv->tm->enable = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001072#endif
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001073 stmmac_clk_enable(priv);
1074
1075 stmmac_check_ether_addr(priv);
1076
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001077 ret = stmmac_init_phy(dev);
1078 if (unlikely(ret)) {
1079 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
1080 goto open_error;
1081 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001082
1083 /* Create and initialize the TX/RX descriptors chains. */
1084 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1085 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1086 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
1087 init_dma_desc_rings(dev);
1088
1089 /* DMA initialization and SW reset */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001090 ret = stmmac_init_dma_engine(priv);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001091 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001092 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001093 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001094 }
1095
1096 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001097 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001098
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +00001099 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001100 if (priv->plat->bus_setup)
1101 priv->plat->bus_setup(priv->ioaddr);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001102
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001103 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001104 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001105
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001106 /* Request the IRQ lines */
1107 ret = request_irq(dev->irq, stmmac_interrupt,
1108 IRQF_SHARED, dev->name, dev);
1109 if (unlikely(ret < 0)) {
1110 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1111 __func__, dev->irq, ret);
1112 goto open_error;
1113 }
1114
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001115 /* Request the Wake IRQ in case of another line is used for WoL */
1116 if (priv->wol_irq != dev->irq) {
1117 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1118 IRQF_SHARED, dev->name, dev);
1119 if (unlikely(ret < 0)) {
1120 pr_err("%s: ERROR: allocating the ext WoL IRQ %d "
1121 "(error: %d)\n", __func__, priv->wol_irq, ret);
1122 goto open_error_wolirq;
1123 }
1124 }
1125
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001126 /* Request the IRQ lines */
1127 if (priv->lpi_irq != -ENXIO) {
1128 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1129 dev->name, dev);
1130 if (unlikely(ret < 0)) {
1131 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1132 __func__, priv->lpi_irq, ret);
1133 goto open_error_lpiirq;
1134 }
1135 }
1136
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001137 /* Enable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001138 stmmac_set_mac(priv->ioaddr, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001139
1140 /* Set the HW DMA mode and the COE */
1141 stmmac_dma_operation_mode(priv);
1142
1143 /* Extra statistics */
1144 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1145 priv->xstats.threshold = tc;
1146
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001147 stmmac_mmc_setup(priv);
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001148
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001149#ifdef CONFIG_STMMAC_DEBUG_FS
1150 ret = stmmac_init_fs(dev);
1151 if (ret < 0)
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001152 pr_warning("%s: failed debugFS registration\n", __func__);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001153#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001154 /* Start the ball rolling... */
1155 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001156 priv->hw->dma->start_tx(priv->ioaddr);
1157 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001158
1159#ifdef CONFIG_STMMAC_TIMER
1160 priv->tm->timer_start(tmrate);
1161#endif
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001162
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001163 /* Dump DMA/MAC registers */
1164 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001165 priv->hw->mac->dump_regs(priv->ioaddr);
1166 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001167 }
1168
1169 if (priv->phydev)
1170 phy_start(priv->phydev);
1171
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001172 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS_TIMER;
1173 priv->eee_enabled = stmmac_eee_init(priv);
1174
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001175 napi_enable(&priv->napi);
1176 skb_queue_head_init(&priv->rx_recycle);
1177 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001178
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001179 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001180
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001181open_error_lpiirq:
1182 if (priv->wol_irq != dev->irq)
1183 free_irq(priv->wol_irq, dev);
1184
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001185open_error_wolirq:
1186 free_irq(dev->irq, dev);
1187
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001188open_error:
1189#ifdef CONFIG_STMMAC_TIMER
1190 kfree(priv->tm);
1191#endif
1192 if (priv->phydev)
1193 phy_disconnect(priv->phydev);
1194
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00001195 stmmac_clk_disable(priv);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001196
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001197 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001198}
1199
1200/**
1201 * stmmac_release - close entry point of the driver
1202 * @dev : device pointer.
1203 * Description:
1204 * This is the stop entry point of the driver.
1205 */
1206static int stmmac_release(struct net_device *dev)
1207{
1208 struct stmmac_priv *priv = netdev_priv(dev);
1209
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001210 if (priv->eee_enabled)
1211 del_timer_sync(&priv->eee_ctrl_timer);
1212
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001213 /* Stop and disconnect the PHY */
1214 if (priv->phydev) {
1215 phy_stop(priv->phydev);
1216 phy_disconnect(priv->phydev);
1217 priv->phydev = NULL;
1218 }
1219
1220 netif_stop_queue(dev);
1221
1222#ifdef CONFIG_STMMAC_TIMER
1223 /* Stop and release the timer */
1224 stmmac_close_ext_timer();
1225 if (priv->tm != NULL)
1226 kfree(priv->tm);
1227#endif
1228 napi_disable(&priv->napi);
1229 skb_queue_purge(&priv->rx_recycle);
1230
1231 /* Free the IRQ lines */
1232 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001233 if (priv->wol_irq != dev->irq)
1234 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001235 if (priv->lpi_irq != -ENXIO)
1236 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001237
1238 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001239 priv->hw->dma->stop_tx(priv->ioaddr);
1240 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001241
1242 /* Release and free the Rx/Tx resources */
1243 free_dma_desc_resources(priv);
1244
avisconti19449bf2010-10-25 18:58:14 +00001245 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001246 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001247
1248 netif_carrier_off(dev);
1249
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001250#ifdef CONFIG_STMMAC_DEBUG_FS
1251 stmmac_exit_fs();
1252#endif
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00001253 stmmac_clk_disable(priv);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001254
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001255 return 0;
1256}
1257
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001258/**
1259 * stmmac_xmit:
1260 * @skb : the socket buffer
1261 * @dev : device pointer
1262 * Description : Tx entry point of the driver.
1263 */
1264static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1265{
1266 struct stmmac_priv *priv = netdev_priv(dev);
1267 unsigned int txsize = priv->dma_tx_size;
1268 unsigned int entry;
1269 int i, csum_insertion = 0;
1270 int nfrags = skb_shinfo(skb)->nr_frags;
1271 struct dma_desc *desc, *first;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001272 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001273
1274 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1275 if (!netif_queue_stopped(dev)) {
1276 netif_stop_queue(dev);
1277 /* This is a hard error, log it. */
1278 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1279 __func__);
1280 }
1281 return NETDEV_TX_BUSY;
1282 }
1283
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001284 spin_lock(&priv->tx_lock);
1285
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001286 if (priv->tx_path_in_lpi_mode)
1287 stmmac_disable_eee_mode(priv);
1288
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001289 entry = priv->cur_tx % txsize;
1290
1291#ifdef STMMAC_XMIT_DEBUG
1292 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1293 pr_info("stmmac xmit:\n"
1294 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1295 "\tn_frags: %d - ip_summed: %d - %s gso\n",
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001296 skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001297 !skb_is_gso(skb) ? "isn't" : "is");
1298#endif
1299
Michał Mirosław5e982f32011-04-09 02:46:55 +00001300 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001301
1302 desc = priv->dma_tx + entry;
1303 first = desc;
1304
1305#ifdef STMMAC_XMIT_DEBUG
1306 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1307 pr_debug("stmmac xmit: skb len: %d, nopaged_len: %d,\n"
1308 "\t\tn_frags: %d, ip_summed: %d\n",
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001309 skb->len, nopaged_len, nfrags, skb->ip_summed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001310#endif
1311 priv->tx_skbuff[entry] = skb;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001312
1313 if (priv->hw->ring->is_jumbo_frm(skb->len, priv->plat->enh_desc)) {
1314 entry = priv->hw->ring->jumbo_frm(priv, skb, csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001315 desc = priv->dma_tx + entry;
1316 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001317 desc->des2 = dma_map_single(priv->device, skb->data,
1318 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001319 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1320 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001321 }
1322
1323 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001324 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1325 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001326
1327 entry = (++priv->cur_tx) % txsize;
1328 desc = priv->dma_tx + entry;
1329
1330 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
Ian Campbellf7223802011-09-21 21:53:20 +00001331 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1332 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001333 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001334 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001335 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001336 priv->hw->desc->set_tx_owner(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001337 }
1338
1339 /* Interrupt on completition only for the latest segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001340 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001341
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001342#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001343 /* Clean IC while using timer */
1344 if (likely(priv->tm->enable))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001345 priv->hw->desc->clear_tx_ic(desc);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001346#endif
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001347
1348 wmb();
1349
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001350 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001351 priv->hw->desc->set_tx_owner(first);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001352
1353 priv->cur_tx++;
1354
1355#ifdef STMMAC_XMIT_DEBUG
1356 if (netif_msg_pktdata(priv)) {
1357 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1358 "first=%p, nfrags=%d\n",
1359 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1360 entry, first, nfrags);
1361 display_ring(priv->dma_tx, txsize);
1362 pr_info(">>> frame to be transmitted: ");
1363 print_pkt(skb->data, skb->len);
1364 }
1365#endif
1366 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1367 TX_DBG("%s: stop transmitted packets\n", __func__);
1368 netif_stop_queue(dev);
1369 }
1370
1371 dev->stats.tx_bytes += skb->len;
1372
Richard Cochran3e82ce12011-06-12 02:19:06 +00001373 skb_tx_timestamp(skb);
1374
Richard Cochran52f64fa2011-06-19 03:31:43 +00001375 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1376
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001377 spin_unlock(&priv->tx_lock);
1378
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001379 return NETDEV_TX_OK;
1380}
1381
1382static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1383{
1384 unsigned int rxsize = priv->dma_rx_size;
1385 int bfsize = priv->dma_buf_sz;
1386 struct dma_desc *p = priv->dma_rx;
1387
1388 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1389 unsigned int entry = priv->dirty_rx % rxsize;
1390 if (likely(priv->rx_skbuff[entry] == NULL)) {
1391 struct sk_buff *skb;
1392
1393 skb = __skb_dequeue(&priv->rx_recycle);
1394 if (skb == NULL)
1395 skb = netdev_alloc_skb_ip_align(priv->dev,
1396 bfsize);
1397
1398 if (unlikely(skb == NULL))
1399 break;
1400
1401 priv->rx_skbuff[entry] = skb;
1402 priv->rx_skbuff_dma[entry] =
1403 dma_map_single(priv->device, skb->data, bfsize,
1404 DMA_FROM_DEVICE);
1405
1406 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001407
1408 if (unlikely(priv->plat->has_gmac))
1409 priv->hw->ring->refill_desc3(bfsize, p + entry);
1410
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001411 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1412 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001413 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001414 priv->hw->desc->set_rx_owner(p + entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001415 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001416}
1417
1418static int stmmac_rx(struct stmmac_priv *priv, int limit)
1419{
1420 unsigned int rxsize = priv->dma_rx_size;
1421 unsigned int entry = priv->cur_rx % rxsize;
1422 unsigned int next_entry;
1423 unsigned int count = 0;
1424 struct dma_desc *p = priv->dma_rx + entry;
1425 struct dma_desc *p_next;
1426
1427#ifdef STMMAC_RX_DEBUG
1428 if (netif_msg_hw(priv)) {
1429 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1430 display_ring(priv->dma_rx, rxsize);
1431 }
1432#endif
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001433 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001434 int status;
1435
1436 if (count >= limit)
1437 break;
1438
1439 count++;
1440
1441 next_entry = (++priv->cur_rx) % rxsize;
1442 p_next = priv->dma_rx + next_entry;
1443 prefetch(p_next);
1444
1445 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001446 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1447 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001448 if (unlikely(status == discard_frame))
1449 priv->dev->stats.rx_errors++;
1450 else {
1451 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001452 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001453
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001454 frame_len = priv->hw->desc->get_rx_frame_len(p,
1455 priv->plat->rx_coe);
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001456 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1457 * Type frames (LLC/LLC-SNAP) */
1458 if (unlikely(status != llc_snap))
1459 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001460#ifdef STMMAC_RX_DEBUG
1461 if (frame_len > ETH_FRAME_LEN)
1462 pr_debug("\tRX frame size %d, COE status: %d\n",
1463 frame_len, status);
1464
1465 if (netif_msg_hw(priv))
1466 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1467 p, entry, p->des2);
1468#endif
1469 skb = priv->rx_skbuff[entry];
1470 if (unlikely(!skb)) {
1471 pr_err("%s: Inconsistent Rx descriptor chain\n",
1472 priv->dev->name);
1473 priv->dev->stats.rx_dropped++;
1474 break;
1475 }
1476 prefetch(skb->data - NET_IP_ALIGN);
1477 priv->rx_skbuff[entry] = NULL;
1478
1479 skb_put(skb, frame_len);
1480 dma_unmap_single(priv->device,
1481 priv->rx_skbuff_dma[entry],
1482 priv->dma_buf_sz, DMA_FROM_DEVICE);
1483#ifdef STMMAC_RX_DEBUG
1484 if (netif_msg_pktdata(priv)) {
1485 pr_info(" frame received (%dbytes)", frame_len);
1486 print_pkt(skb->data, frame_len);
1487 }
1488#endif
1489 skb->protocol = eth_type_trans(skb, priv->dev);
1490
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001491 if (unlikely(!priv->plat->rx_coe)) {
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001492 /* No RX COE for old mac10/100 devices */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001493 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001494 netif_receive_skb(skb);
1495 } else {
1496 skb->ip_summed = CHECKSUM_UNNECESSARY;
1497 napi_gro_receive(&priv->napi, skb);
1498 }
1499
1500 priv->dev->stats.rx_packets++;
1501 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001502 }
1503 entry = next_entry;
1504 p = p_next; /* use prefetched values */
1505 }
1506
1507 stmmac_rx_refill(priv);
1508
1509 priv->xstats.rx_pkt_n += count;
1510
1511 return count;
1512}
1513
1514/**
1515 * stmmac_poll - stmmac poll method (NAPI)
1516 * @napi : pointer to the napi structure.
1517 * @budget : maximum number of packets that the current CPU can receive from
1518 * all interfaces.
1519 * Description :
1520 * This function implements the the reception process.
1521 * Also it runs the TX completion thread
1522 */
1523static int stmmac_poll(struct napi_struct *napi, int budget)
1524{
1525 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1526 int work_done = 0;
1527
1528 priv->xstats.poll_n++;
1529 stmmac_tx(priv);
1530 work_done = stmmac_rx(priv, budget);
1531
1532 if (work_done < budget) {
1533 napi_complete(napi);
1534 stmmac_enable_irq(priv);
1535 }
1536 return work_done;
1537}
1538
1539/**
1540 * stmmac_tx_timeout
1541 * @dev : Pointer to net device structure
1542 * Description: this function is called when a packet transmission fails to
1543 * complete within a reasonable tmrate. The driver will mark the error in the
1544 * netdev structure and arrange for the device to be reset to a sane state
1545 * in order to transmit a new packet.
1546 */
1547static void stmmac_tx_timeout(struct net_device *dev)
1548{
1549 struct stmmac_priv *priv = netdev_priv(dev);
1550
1551 /* Clear Tx resources and restart transmitting again */
1552 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001553}
1554
1555/* Configuration changes (passed on by ifconfig) */
1556static int stmmac_config(struct net_device *dev, struct ifmap *map)
1557{
1558 if (dev->flags & IFF_UP) /* can't act on a running interface */
1559 return -EBUSY;
1560
1561 /* Don't allow changing the I/O address */
1562 if (map->base_addr != dev->base_addr) {
1563 pr_warning("%s: can't change I/O address\n", dev->name);
1564 return -EOPNOTSUPP;
1565 }
1566
1567 /* Don't allow changing the IRQ */
1568 if (map->irq != dev->irq) {
1569 pr_warning("%s: can't change IRQ number %d\n",
1570 dev->name, dev->irq);
1571 return -EOPNOTSUPP;
1572 }
1573
1574 /* ignore other fields */
1575 return 0;
1576}
1577
1578/**
Jiri Pirko01789342011-08-16 06:29:00 +00001579 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001580 * @dev : pointer to the device structure
1581 * Description:
1582 * This function is a driver entry point which gets called by the kernel
1583 * whenever multicast addresses must be enabled/disabled.
1584 * Return value:
1585 * void.
1586 */
Jiri Pirko01789342011-08-16 06:29:00 +00001587static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001588{
1589 struct stmmac_priv *priv = netdev_priv(dev);
1590
1591 spin_lock(&priv->lock);
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00001592 priv->hw->mac->set_filter(dev, priv->synopsys_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001593 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001594}
1595
1596/**
1597 * stmmac_change_mtu - entry point to change MTU size for the device.
1598 * @dev : device pointer.
1599 * @new_mtu : the new MTU size for the device.
1600 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1601 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1602 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1603 * Return value:
1604 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1605 * file on failure.
1606 */
1607static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1608{
1609 struct stmmac_priv *priv = netdev_priv(dev);
1610 int max_mtu;
1611
1612 if (netif_running(dev)) {
1613 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1614 return -EBUSY;
1615 }
1616
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00001617 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001618 max_mtu = JUMBO_LEN;
1619 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00001620 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001621
1622 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1623 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1624 return -EINVAL;
1625 }
1626
Michał Mirosław5e982f32011-04-09 02:46:55 +00001627 dev->mtu = new_mtu;
1628 netdev_update_features(dev);
1629
1630 return 0;
1631}
1632
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001633static netdev_features_t stmmac_fix_features(struct net_device *dev,
1634 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001635{
1636 struct stmmac_priv *priv = netdev_priv(dev);
1637
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001638 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001639 features &= ~NETIF_F_RXCSUM;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001640 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
1641 features &= ~NETIF_F_IPV6_CSUM;
Michał Mirosław5e982f32011-04-09 02:46:55 +00001642 if (!priv->plat->tx_coe)
1643 features &= ~NETIF_F_ALL_CSUM;
1644
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001645 /* Some GMAC devices have a bugged Jumbo frame support that
1646 * needs to have the Tx COE disabled for oversized frames
1647 * (due to limited buffer sizes). In this case we disable
1648 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001649 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1650 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001651
Michał Mirosław5e982f32011-04-09 02:46:55 +00001652 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001653}
1654
1655static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1656{
1657 struct net_device *dev = (struct net_device *)dev_id;
1658 struct stmmac_priv *priv = netdev_priv(dev);
1659
1660 if (unlikely(!dev)) {
1661 pr_err("%s: invalid dev pointer\n", __func__);
1662 return IRQ_NONE;
1663 }
1664
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001665 /* To handle GMAC own interrupts */
1666 if (priv->plat->has_gmac) {
1667 int status = priv->hw->mac->host_irq_status((void __iomem *)
1668 dev->base_addr);
1669 if (unlikely(status)) {
1670 if (status & core_mmc_tx_irq)
1671 priv->xstats.mmc_tx_irq_n++;
1672 if (status & core_mmc_rx_irq)
1673 priv->xstats.mmc_rx_irq_n++;
1674 if (status & core_mmc_rx_csum_offload_irq)
1675 priv->xstats.mmc_rx_csum_offload_irq_n++;
1676 if (status & core_irq_receive_pmt_irq)
1677 priv->xstats.irq_receive_pmt_irq_n++;
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001678
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001679 /* For LPI we need to save the tx status */
1680 if (status & core_irq_tx_path_in_lpi_mode) {
1681 priv->xstats.irq_tx_path_in_lpi_mode_n++;
1682 priv->tx_path_in_lpi_mode = true;
1683 }
1684 if (status & core_irq_tx_path_exit_lpi_mode) {
1685 priv->xstats.irq_tx_path_exit_lpi_mode_n++;
1686 priv->tx_path_in_lpi_mode = false;
1687 }
1688 if (status & core_irq_rx_path_in_lpi_mode)
1689 priv->xstats.irq_rx_path_in_lpi_mode_n++;
1690 if (status & core_irq_rx_path_exit_lpi_mode)
1691 priv->xstats.irq_rx_path_exit_lpi_mode_n++;
1692 }
1693 }
1694
1695 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001696 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001697
1698 return IRQ_HANDLED;
1699}
1700
1701#ifdef CONFIG_NET_POLL_CONTROLLER
1702/* Polling receive - used by NETCONSOLE and other diagnostic tools
1703 * to allow network I/O with interrupts disabled. */
1704static void stmmac_poll_controller(struct net_device *dev)
1705{
1706 disable_irq(dev->irq);
1707 stmmac_interrupt(dev->irq, dev);
1708 enable_irq(dev->irq);
1709}
1710#endif
1711
1712/**
1713 * stmmac_ioctl - Entry point for the Ioctl
1714 * @dev: Device pointer.
1715 * @rq: An IOCTL specefic structure, that can contain a pointer to
1716 * a proprietary structure used to pass information to the driver.
1717 * @cmd: IOCTL command
1718 * Description:
1719 * Currently there are no special functionality supported in IOCTL, just the
1720 * phy_mii_ioctl(...) can be invoked.
1721 */
1722static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1723{
1724 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001725 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001726
1727 if (!netif_running(dev))
1728 return -EINVAL;
1729
Richard Cochran28b04112010-07-17 08:48:55 +00001730 if (!priv->phydev)
1731 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001732
Richard Cochran28b04112010-07-17 08:48:55 +00001733 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
Richard Cochran28b04112010-07-17 08:48:55 +00001734
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001735 return ret;
1736}
1737
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001738#ifdef CONFIG_STMMAC_DEBUG_FS
1739static struct dentry *stmmac_fs_dir;
1740static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001741static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001742
1743static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
1744{
1745 struct tmp_s {
1746 u64 a;
1747 unsigned int b;
1748 unsigned int c;
1749 };
1750 int i;
1751 struct net_device *dev = seq->private;
1752 struct stmmac_priv *priv = netdev_priv(dev);
1753
1754 seq_printf(seq, "=======================\n");
1755 seq_printf(seq, " RX descriptor ring\n");
1756 seq_printf(seq, "=======================\n");
1757
1758 for (i = 0; i < priv->dma_rx_size; i++) {
1759 struct tmp_s *x = (struct tmp_s *)(priv->dma_rx + i);
1760 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1761 i, (unsigned int)(x->a),
1762 (unsigned int)((x->a) >> 32), x->b, x->c);
1763 seq_printf(seq, "\n");
1764 }
1765
1766 seq_printf(seq, "\n");
1767 seq_printf(seq, "=======================\n");
1768 seq_printf(seq, " TX descriptor ring\n");
1769 seq_printf(seq, "=======================\n");
1770
1771 for (i = 0; i < priv->dma_tx_size; i++) {
1772 struct tmp_s *x = (struct tmp_s *)(priv->dma_tx + i);
1773 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1774 i, (unsigned int)(x->a),
1775 (unsigned int)((x->a) >> 32), x->b, x->c);
1776 seq_printf(seq, "\n");
1777 }
1778
1779 return 0;
1780}
1781
1782static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
1783{
1784 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
1785}
1786
1787static const struct file_operations stmmac_rings_status_fops = {
1788 .owner = THIS_MODULE,
1789 .open = stmmac_sysfs_ring_open,
1790 .read = seq_read,
1791 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00001792 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001793};
1794
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001795static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
1796{
1797 struct net_device *dev = seq->private;
1798 struct stmmac_priv *priv = netdev_priv(dev);
1799
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001800 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001801 seq_printf(seq, "DMA HW features not supported\n");
1802 return 0;
1803 }
1804
1805 seq_printf(seq, "==============================\n");
1806 seq_printf(seq, "\tDMA HW features\n");
1807 seq_printf(seq, "==============================\n");
1808
1809 seq_printf(seq, "\t10/100 Mbps %s\n",
1810 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
1811 seq_printf(seq, "\t1000 Mbps %s\n",
1812 (priv->dma_cap.mbps_1000) ? "Y" : "N");
1813 seq_printf(seq, "\tHalf duple %s\n",
1814 (priv->dma_cap.half_duplex) ? "Y" : "N");
1815 seq_printf(seq, "\tHash Filter: %s\n",
1816 (priv->dma_cap.hash_filter) ? "Y" : "N");
1817 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
1818 (priv->dma_cap.multi_addr) ? "Y" : "N");
1819 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
1820 (priv->dma_cap.pcs) ? "Y" : "N");
1821 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
1822 (priv->dma_cap.sma_mdio) ? "Y" : "N");
1823 seq_printf(seq, "\tPMT Remote wake up: %s\n",
1824 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
1825 seq_printf(seq, "\tPMT Magic Frame: %s\n",
1826 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
1827 seq_printf(seq, "\tRMON module: %s\n",
1828 (priv->dma_cap.rmon) ? "Y" : "N");
1829 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
1830 (priv->dma_cap.time_stamp) ? "Y" : "N");
1831 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
1832 (priv->dma_cap.atime_stamp) ? "Y" : "N");
1833 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
1834 (priv->dma_cap.eee) ? "Y" : "N");
1835 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
1836 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
1837 (priv->dma_cap.tx_coe) ? "Y" : "N");
1838 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
1839 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
1840 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
1841 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
1842 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
1843 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
1844 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
1845 priv->dma_cap.number_rx_channel);
1846 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
1847 priv->dma_cap.number_tx_channel);
1848 seq_printf(seq, "\tEnhanced descriptors: %s\n",
1849 (priv->dma_cap.enh_desc) ? "Y" : "N");
1850
1851 return 0;
1852}
1853
1854static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
1855{
1856 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
1857}
1858
1859static const struct file_operations stmmac_dma_cap_fops = {
1860 .owner = THIS_MODULE,
1861 .open = stmmac_sysfs_dma_cap_open,
1862 .read = seq_read,
1863 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00001864 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001865};
1866
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001867static int stmmac_init_fs(struct net_device *dev)
1868{
1869 /* Create debugfs entries */
1870 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
1871
1872 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
1873 pr_err("ERROR %s, debugfs create directory failed\n",
1874 STMMAC_RESOURCE_NAME);
1875
1876 return -ENOMEM;
1877 }
1878
1879 /* Entry to report DMA RX/TX rings */
1880 stmmac_rings_status = debugfs_create_file("descriptors_status",
1881 S_IRUGO, stmmac_fs_dir, dev,
1882 &stmmac_rings_status_fops);
1883
1884 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
1885 pr_info("ERROR creating stmmac ring debugfs file\n");
1886 debugfs_remove(stmmac_fs_dir);
1887
1888 return -ENOMEM;
1889 }
1890
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001891 /* Entry to report the DMA HW features */
1892 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
1893 dev, &stmmac_dma_cap_fops);
1894
1895 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
1896 pr_info("ERROR creating stmmac MMC debugfs file\n");
1897 debugfs_remove(stmmac_rings_status);
1898 debugfs_remove(stmmac_fs_dir);
1899
1900 return -ENOMEM;
1901 }
1902
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001903 return 0;
1904}
1905
1906static void stmmac_exit_fs(void)
1907{
1908 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001909 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001910 debugfs_remove(stmmac_fs_dir);
1911}
1912#endif /* CONFIG_STMMAC_DEBUG_FS */
1913
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001914static const struct net_device_ops stmmac_netdev_ops = {
1915 .ndo_open = stmmac_open,
1916 .ndo_start_xmit = stmmac_xmit,
1917 .ndo_stop = stmmac_release,
1918 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001919 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00001920 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001921 .ndo_tx_timeout = stmmac_tx_timeout,
1922 .ndo_do_ioctl = stmmac_ioctl,
1923 .ndo_set_config = stmmac_config,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001924#ifdef CONFIG_NET_POLL_CONTROLLER
1925 .ndo_poll_controller = stmmac_poll_controller,
1926#endif
1927 .ndo_set_mac_address = eth_mac_addr,
1928};
1929
1930/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001931 * stmmac_hw_init - Init the MAC device
1932 * @priv : pointer to the private device structure.
1933 * Description: this function detects which MAC device
1934 * (GMAC/MAC10-100) has to attached, checks the HW capability
1935 * (if supported) and sets the driver's features (for example
1936 * to use the ring or chaine mode or support the normal/enh
1937 * descriptor structure).
1938 */
1939static int stmmac_hw_init(struct stmmac_priv *priv)
1940{
1941 int ret = 0;
1942 struct mac_device_info *mac;
1943
1944 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00001945 if (priv->plat->has_gmac) {
1946 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001947 mac = dwmac1000_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00001948 } else {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001949 mac = dwmac100_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00001950 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001951 if (!mac)
1952 return -ENOMEM;
1953
1954 priv->hw = mac;
1955
1956 /* To use the chained or ring mode */
1957 priv->hw->ring = &ring_mode_ops;
1958
1959 /* Get and dump the chip ID */
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00001960 priv->synopsys_id = stmmac_get_synopsys_id(priv);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001961
1962 /* Get the HW capability (new GMAC newer than 3.50a) */
1963 priv->hw_cap_support = stmmac_get_hw_features(priv);
1964 if (priv->hw_cap_support) {
1965 pr_info(" DMA HW capability register supported");
1966
1967 /* We can override some gmac/dma configuration fields: e.g.
1968 * enh_desc, tx_coe (e.g. that are passed through the
1969 * platform) with the values from the HW capability
1970 * register (if supported).
1971 */
1972 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001973 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001974
1975 priv->plat->tx_coe = priv->dma_cap.tx_coe;
1976
1977 if (priv->dma_cap.rx_coe_type2)
1978 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
1979 else if (priv->dma_cap.rx_coe_type1)
1980 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
1981
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001982 } else
1983 pr_info(" No HW DMA feature register supported");
1984
1985 /* Select the enhnaced/normal descriptor structures */
1986 stmmac_selec_desc_mode(priv);
1987
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001988 /* Enable the IPC (Checksum Offload) and check if the feature has been
1989 * enabled during the core configuration. */
1990 ret = priv->hw->mac->rx_ipc(priv->ioaddr);
1991 if (!ret) {
1992 pr_warning(" RX IPC Checksum Offload not configured.\n");
1993 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
1994 }
1995
1996 if (priv->plat->rx_coe)
1997 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
1998 priv->plat->rx_coe);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001999 if (priv->plat->tx_coe)
2000 pr_info(" TX Checksum insertion supported\n");
2001
2002 if (priv->plat->pmt) {
2003 pr_info(" Wake-Up On Lan supported\n");
2004 device_set_wakeup_capable(priv->device, 1);
2005 }
2006
2007 return ret;
2008}
2009
2010/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002011 * stmmac_dvr_probe
2012 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002013 * @plat_dat: platform data pointer
2014 * @addr: iobase memory address
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002015 * Description: this is the main probe function used to
2016 * call the alloc_etherdev, allocate the priv structure.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002017 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002018struct stmmac_priv *stmmac_dvr_probe(struct device *device,
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002019 struct plat_stmmacenet_data *plat_dat,
2020 void __iomem *addr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002021{
2022 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002023 struct net_device *ndev = NULL;
2024 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002025
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002026 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002027 if (!ndev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002028 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002029
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002030 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002031
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002032 priv = netdev_priv(ndev);
2033 priv->device = device;
2034 priv->dev = ndev;
2035
2036 ether_setup(ndev);
2037
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002038 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002039 priv->pause = pause;
2040 priv->plat = plat_dat;
2041 priv->ioaddr = addr;
2042 priv->dev->base_addr = (unsigned long)addr;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002043
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002044 /* Verify driver arguments */
2045 stmmac_verify_args();
2046
2047 /* Override with kernel parameters if supplied XXX CRS XXX
2048 * this needs to have multiple instances */
2049 if ((phyaddr >= 0) && (phyaddr <= 31))
2050 priv->plat->phy_addr = phyaddr;
2051
2052 /* Init MAC and get the capabilities */
2053 stmmac_hw_init(priv);
2054
2055 ndev->netdev_ops = &stmmac_netdev_ops;
2056
2057 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2058 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002059 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2060 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002061#ifdef STMMAC_VLAN_TAG_USED
2062 /* Both mac100 and gmac support receive VLAN tag detection */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002063 ndev->features |= NETIF_F_HW_VLAN_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002064#endif
2065 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2066
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002067 if (flow_ctrl)
2068 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2069
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002070 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002071
Vlad Lunguf8e96162010-11-29 22:52:52 +00002072 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002073 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002074
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002075 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002076 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002077 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002078 goto error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002079 }
2080
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002081 if (stmmac_clk_get(priv))
Giuseppe CAVALLARO31ea38e2012-04-18 19:48:22 +00002082 pr_warning("%s: warning: cannot get CSR clock\n", __func__);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002083
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002084 /* If a specific clk_csr value is passed from the platform
2085 * this means that the CSR Clock Range selection cannot be
2086 * changed at run-time and it is fixed. Viceversa the driver'll try to
2087 * set the MDC clock dynamically according to the csr actual
2088 * clock input.
2089 */
2090 if (!priv->plat->clk_csr)
2091 stmmac_clk_csr_set(priv);
2092 else
2093 priv->clk_csr = priv->plat->clk_csr;
2094
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002095 /* MDIO bus Registration */
2096 ret = stmmac_mdio_register(ndev);
2097 if (ret < 0) {
2098 pr_debug("%s: MDIO bus (id: %d) registration failed",
2099 __func__, priv->plat->bus_id);
2100 goto error;
2101 }
2102
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002103 return priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002104
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002105error:
2106 netif_napi_del(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002107
Dan Carpenter34a52f32010-12-20 21:34:56 +00002108 unregister_netdev(ndev);
Dan Carpenter34a52f32010-12-20 21:34:56 +00002109 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002110
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002111 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002112}
2113
2114/**
2115 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002116 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002117 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002118 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002119 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002120int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002121{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002122 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002123
2124 pr_info("%s:\n\tremoving driver", __func__);
2125
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002126 priv->hw->dma->stop_rx(priv->ioaddr);
2127 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002128
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002129 stmmac_set_mac(priv->ioaddr, false);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002130 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002131 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002132 unregister_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002133 free_netdev(ndev);
2134
2135 return 0;
2136}
2137
2138#ifdef CONFIG_PM
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002139int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002140{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002141 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002142 int dis_ic = 0;
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002143 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002144
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002145 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002146 return 0;
2147
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002148 if (priv->phydev)
2149 phy_stop(priv->phydev);
2150
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002151 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002152
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002153 netif_device_detach(ndev);
2154 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002155
2156#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002157 priv->tm->timer_stop();
2158 if (likely(priv->tm->enable))
2159 dis_ic = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002160#endif
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002161 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002162
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002163 /* Stop TX/RX DMA */
2164 priv->hw->dma->stop_tx(priv->ioaddr);
2165 priv->hw->dma->stop_rx(priv->ioaddr);
2166 /* Clear the Rx/Tx descriptors */
2167 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
2168 dis_ic);
2169 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002170
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002171 /* Enable Power down mode by programming the PMT regs */
2172 if (device_may_wakeup(priv->device))
2173 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002174 else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002175 stmmac_set_mac(priv->ioaddr, false);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002176 /* Disable clock in case of PWM is off */
2177 stmmac_clk_disable(priv);
2178 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002179 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002180 return 0;
2181}
2182
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002183int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002184{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002185 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002186 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002187
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002188 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002189 return 0;
2190
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002191 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02002192
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002193 /* Power Down bit, into the PM register, is cleared
2194 * automatically as soon as a magic packet or a Wake-up frame
2195 * is received. Anyway, it's better to manually clear
2196 * this bit because it can generate problems while resuming
2197 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002198 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07002199 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002200 else
2201 /* enable the clk prevously disabled */
2202 stmmac_clk_enable(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002203
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002204 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002205
2206 /* Enable the MAC and DMA */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002207 stmmac_set_mac(priv->ioaddr, true);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002208 priv->hw->dma->start_tx(priv->ioaddr);
2209 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002210
2211#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002212 if (likely(priv->tm->enable))
2213 priv->tm->timer_start(tmrate);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002214#endif
2215 napi_enable(&priv->napi);
2216
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002217 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002218
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002219 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002220
2221 if (priv->phydev)
2222 phy_start(priv->phydev);
2223
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002224 return 0;
2225}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002226
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002227int stmmac_freeze(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002228{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002229 if (!ndev || !netif_running(ndev))
2230 return 0;
2231
2232 return stmmac_release(ndev);
2233}
2234
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002235int stmmac_restore(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002236{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002237 if (!ndev || !netif_running(ndev))
2238 return 0;
2239
2240 return stmmac_open(ndev);
2241}
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002242#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002243
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002244/* Driver can be configured w/ and w/ both PCI and Platf drivers
2245 * depending on the configuration selected.
2246 */
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002247static int __init stmmac_init(void)
2248{
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002249 int err_plt = 0;
2250 int err_pci = 0;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002251
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002252 err_plt = stmmac_register_platform();
2253 err_pci = stmmac_register_pci();
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002254
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002255 if ((err_pci) && (err_plt)) {
2256 pr_err("stmmac: driver registration failed\n");
2257 return -EINVAL;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002258 }
2259
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002260 return 0;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002261}
2262
2263static void __exit stmmac_exit(void)
2264{
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002265 stmmac_unregister_platform();
2266 stmmac_unregister_pci();
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002267}
2268
2269module_init(stmmac_init);
2270module_exit(stmmac_exit);
2271
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002272#ifndef MODULE
2273static int __init stmmac_cmdline_opt(char *str)
2274{
2275 char *opt;
2276
2277 if (!str || !*str)
2278 return -EINVAL;
2279 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002280 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002281 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002282 goto err;
2283 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002284 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002285 goto err;
2286 } else if (!strncmp(opt, "dma_txsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002287 if (kstrtoint(opt + 11, 0, &dma_txsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002288 goto err;
2289 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002290 if (kstrtoint(opt + 11, 0, &dma_rxsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002291 goto err;
2292 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002293 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002294 goto err;
2295 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002296 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002297 goto err;
2298 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002299 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002300 goto err;
2301 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002302 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002303 goto err;
2304 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002305 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002306 goto err;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002307 } else if (!strncmp(opt, "eee_timer:", 6)) {
2308 if (kstrtoint(opt + 10, 0, &eee_timer))
2309 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002310#ifdef CONFIG_STMMAC_TIMER
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002311 } else if (!strncmp(opt, "tmrate:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002312 if (kstrtoint(opt + 7, 0, &tmrate))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002313 goto err;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002314#endif
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002315 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002316 }
2317 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002318
2319err:
2320 pr_err("%s: ERROR broken module parameter conversion", __func__);
2321 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002322}
2323
2324__setup("stmmaceth=", stmmac_cmdline_opt);
2325#endif
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05002326
2327MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
2328MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2329MODULE_LICENSE("GPL");