blob: d9d68649bdaa2e78dc503644252945ffee445715 [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
Viresh Kumar6a81c262012-07-30 14:39:41 -070031#include <linux/clk.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070032#include <linux/kernel.h>
33#include <linux/interrupt.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070034#include <linux/ip.h>
35#include <linux/tcp.h>
36#include <linux/skbuff.h>
37#include <linux/ethtool.h>
38#include <linux/if_ether.h>
39#include <linux/crc32.h>
40#include <linux/mii.h>
Jiri Pirko01789342011-08-16 06:29:00 +000041#include <linux/if.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070042#include <linux/if_vlan.h>
43#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040045#include <linux/prefetch.h>
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000046#ifdef CONFIG_STMMAC_DEBUG_FS
47#include <linux/debugfs.h>
48#include <linux/seq_file.h>
49#endif
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +000050#include "stmmac.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070051
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070052#undef STMMAC_DEBUG
53/*#define STMMAC_DEBUG*/
54#ifdef STMMAC_DEBUG
55#define DBG(nlevel, klevel, fmt, args...) \
56 ((void)(netif_msg_##nlevel(priv) && \
57 printk(KERN_##klevel fmt, ## args)))
58#else
59#define DBG(nlevel, klevel, fmt, args...) do { } while (0)
60#endif
61
62#undef STMMAC_RX_DEBUG
63/*#define STMMAC_RX_DEBUG*/
64#ifdef STMMAC_RX_DEBUG
65#define RX_DBG(fmt, args...) printk(fmt, ## args)
66#else
67#define RX_DBG(fmt, args...) do { } while (0)
68#endif
69
70#undef STMMAC_XMIT_DEBUG
71/*#define STMMAC_XMIT_DEBUG*/
72#ifdef STMMAC_TX_DEBUG
73#define TX_DBG(fmt, args...) printk(fmt, ## args)
74#else
75#define TX_DBG(fmt, args...) do { } while (0)
76#endif
77
78#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
79#define JUMBO_LEN 9000
80
81/* Module parameters */
82#define TX_TIMEO 5000 /* default 5 seconds */
83static int watchdog = TX_TIMEO;
84module_param(watchdog, int, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds");
86
87static int debug = -1; /* -1: default, 0: no output, 16: all */
88module_param(debug, int, S_IRUGO | S_IWUSR);
89MODULE_PARM_DESC(debug, "Message Level (0: no output, 16: all)");
90
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +000091int phyaddr = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070092module_param(phyaddr, int, S_IRUGO);
93MODULE_PARM_DESC(phyaddr, "Physical device address");
94
95#define DMA_TX_SIZE 256
96static int dma_txsize = DMA_TX_SIZE;
97module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
98MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
99
100#define DMA_RX_SIZE 256
101static int dma_rxsize = DMA_RX_SIZE;
102module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
103MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
104
105static int flow_ctrl = FLOW_OFF;
106module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
107MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
108
109static int pause = PAUSE_TIME;
110module_param(pause, int, S_IRUGO | S_IWUSR);
111MODULE_PARM_DESC(pause, "Flow Control Pause Time");
112
113#define TC_DEFAULT 64
114static int tc = TC_DEFAULT;
115module_param(tc, int, S_IRUGO | S_IWUSR);
116MODULE_PARM_DESC(tc, "DMA threshold control value");
117
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700118#define DMA_BUFFER_SIZE BUF_SIZE_2KiB
119static int buf_sz = DMA_BUFFER_SIZE;
120module_param(buf_sz, int, S_IRUGO | S_IWUSR);
121MODULE_PARM_DESC(buf_sz, "DMA buffer size");
122
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700123static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
124 NETIF_MSG_LINK | NETIF_MSG_IFUP |
125 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
126
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000127#define STMMAC_DEFAULT_LPI_TIMER 1000
128static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
129module_param(eee_timer, int, S_IRUGO | S_IWUSR);
130MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
131#define STMMAC_LPI_TIMER(x) (jiffies + msecs_to_jiffies(x))
132
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700133static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700134
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000135#ifdef CONFIG_STMMAC_DEBUG_FS
136static int stmmac_init_fs(struct net_device *dev);
137static void stmmac_exit_fs(void);
138#endif
139
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000140#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
141
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700142/**
143 * stmmac_verify_args - verify the driver parameters.
144 * Description: it verifies if some wrong parameter is passed to the driver.
145 * Note that wrong parameters are replaced with the default values.
146 */
147static void stmmac_verify_args(void)
148{
149 if (unlikely(watchdog < 0))
150 watchdog = TX_TIMEO;
151 if (unlikely(dma_rxsize < 0))
152 dma_rxsize = DMA_RX_SIZE;
153 if (unlikely(dma_txsize < 0))
154 dma_txsize = DMA_TX_SIZE;
155 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
156 buf_sz = DMA_BUFFER_SIZE;
157 if (unlikely(flow_ctrl > 1))
158 flow_ctrl = FLOW_AUTO;
159 else if (likely(flow_ctrl < 0))
160 flow_ctrl = FLOW_OFF;
161 if (unlikely((pause < 0) || (pause > 0xffff)))
162 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000163 if (eee_timer < 0)
164 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700165}
166
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000167static void stmmac_clk_csr_set(struct stmmac_priv *priv)
168{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000169 u32 clk_rate;
170
171 clk_rate = clk_get_rate(priv->stmmac_clk);
172
173 /* Platform provided default clk_csr would be assumed valid
174 * for all other cases except for the below mentioned ones. */
175 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
176 if (clk_rate < CSR_F_35M)
177 priv->clk_csr = STMMAC_CSR_20_35M;
178 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
179 priv->clk_csr = STMMAC_CSR_35_60M;
180 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
181 priv->clk_csr = STMMAC_CSR_60_100M;
182 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
183 priv->clk_csr = STMMAC_CSR_100_150M;
184 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
185 priv->clk_csr = STMMAC_CSR_150_250M;
186 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
187 priv->clk_csr = STMMAC_CSR_250_300M;
188 } /* For values higher than the IEEE 802.3 specified frequency
189 * we can not estimate the proper divider as it is not known
190 * the frequency of clk_csr_i. So we do not change the default
191 * divider. */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000192}
193
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700194#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
195static void print_pkt(unsigned char *buf, int len)
196{
197 int j;
198 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
199 for (j = 0; j < len; j++) {
200 if ((j % 16) == 0)
201 pr_info("\n %03x:", j);
202 pr_info(" %02x", buf[j]);
203 }
204 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700205}
206#endif
207
208/* minimum number of free TX descriptors required to wake up TX process */
209#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
210
211static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
212{
213 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
214}
215
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000216/* On some ST platforms, some HW system configuraton registers have to be
217 * set according to the link speed negotiated.
218 */
219static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
220{
221 struct phy_device *phydev = priv->phydev;
222
223 if (likely(priv->plat->fix_mac_speed))
224 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
225 phydev->speed);
226}
227
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000228static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
229{
230 /* Check and enter in LPI mode */
231 if ((priv->dirty_tx == priv->cur_tx) &&
232 (priv->tx_path_in_lpi_mode == false))
233 priv->hw->mac->set_eee_mode(priv->ioaddr);
234}
235
236void stmmac_disable_eee_mode(struct stmmac_priv *priv)
237{
238 /* Exit and disable EEE in case of we are are in LPI state. */
239 priv->hw->mac->reset_eee_mode(priv->ioaddr);
240 del_timer_sync(&priv->eee_ctrl_timer);
241 priv->tx_path_in_lpi_mode = false;
242}
243
244/**
245 * stmmac_eee_ctrl_timer
246 * @arg : data hook
247 * Description:
248 * If there is no data transfer and if we are not in LPI state,
249 * then MAC Transmitter can be moved to LPI state.
250 */
251static void stmmac_eee_ctrl_timer(unsigned long arg)
252{
253 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
254
255 stmmac_enable_eee_mode(priv);
256 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
257}
258
259/**
260 * stmmac_eee_init
261 * @priv: private device pointer
262 * Description:
263 * If the EEE support has been enabled while configuring the driver,
264 * if the GMAC actually supports the EEE (from the HW cap reg) and the
265 * phy can also manage EEE, so enable the LPI state and start the timer
266 * to verify if the tx path can enter in LPI state.
267 */
268bool stmmac_eee_init(struct stmmac_priv *priv)
269{
270 bool ret = false;
271
272 /* MAC core supports the EEE feature. */
273 if (priv->dma_cap.eee) {
274 /* Check if the PHY supports EEE */
275 if (phy_init_eee(priv->phydev, 1))
276 goto out;
277
278 priv->eee_active = 1;
279 init_timer(&priv->eee_ctrl_timer);
280 priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
281 priv->eee_ctrl_timer.data = (unsigned long)priv;
282 priv->eee_ctrl_timer.expires = STMMAC_LPI_TIMER(eee_timer);
283 add_timer(&priv->eee_ctrl_timer);
284
285 priv->hw->mac->set_eee_timer(priv->ioaddr,
286 STMMAC_DEFAULT_LIT_LS_TIMER,
287 priv->tx_lpi_timer);
288
289 pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
290
291 ret = true;
292 }
293out:
294 return ret;
295}
296
297static void stmmac_eee_adjust(struct stmmac_priv *priv)
298{
299 /* When the EEE has been already initialised we have to
300 * modify the PLS bit in the LPI ctrl & status reg according
301 * to the PHY link status. For this reason.
302 */
303 if (priv->eee_enabled)
304 priv->hw->mac->set_eee_pls(priv->ioaddr, priv->phydev->link);
305}
306
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700307/**
308 * stmmac_adjust_link
309 * @dev: net device structure
310 * Description: it adjusts the link parameters.
311 */
312static void stmmac_adjust_link(struct net_device *dev)
313{
314 struct stmmac_priv *priv = netdev_priv(dev);
315 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700316 unsigned long flags;
317 int new_state = 0;
318 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
319
320 if (phydev == NULL)
321 return;
322
323 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
324 phydev->addr, phydev->link);
325
326 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000327
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700328 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000329 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700330
331 /* Now we make sure that we can be in full duplex mode.
332 * If not, we operate in half-duplex mode. */
333 if (phydev->duplex != priv->oldduplex) {
334 new_state = 1;
335 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000336 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700337 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000338 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700339 priv->oldduplex = phydev->duplex;
340 }
341 /* Flow Control operation */
342 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000343 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000344 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700345
346 if (phydev->speed != priv->speed) {
347 new_state = 1;
348 switch (phydev->speed) {
349 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000350 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000351 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000352 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700353 break;
354 case 100:
355 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000356 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000357 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700358 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000359 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700360 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000361 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700362 }
363 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000364 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700365 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000366 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700367 break;
368 default:
369 if (netif_msg_link(priv))
370 pr_warning("%s: Speed (%d) is not 10"
371 " or 100!\n", dev->name, phydev->speed);
372 break;
373 }
374
375 priv->speed = phydev->speed;
376 }
377
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000378 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700379
380 if (!priv->oldlink) {
381 new_state = 1;
382 priv->oldlink = 1;
383 }
384 } else if (priv->oldlink) {
385 new_state = 1;
386 priv->oldlink = 0;
387 priv->speed = 0;
388 priv->oldduplex = -1;
389 }
390
391 if (new_state && netif_msg_link(priv))
392 phy_print_status(phydev);
393
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000394 stmmac_eee_adjust(priv);
395
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700396 spin_unlock_irqrestore(&priv->lock, flags);
397
398 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
399}
400
401/**
402 * stmmac_init_phy - PHY initialization
403 * @dev: net device structure
404 * Description: it initializes the driver's PHY state, and attaches the PHY
405 * to the mac driver.
406 * Return value:
407 * 0 on success
408 */
409static int stmmac_init_phy(struct net_device *dev)
410{
411 struct stmmac_priv *priv = netdev_priv(dev);
412 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000413 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000414 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000415 int interface = priv->plat->interface;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700416 priv->oldlink = 0;
417 priv->speed = 0;
418 priv->oldduplex = -1;
419
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000420 if (priv->plat->phy_bus_name)
421 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
422 priv->plat->phy_bus_name, priv->plat->bus_id);
423 else
424 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
425 priv->plat->bus_id);
426
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000427 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000428 priv->plat->phy_addr);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000429 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700430
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000431 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, 0,
432 interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700433
434 if (IS_ERR(phydev)) {
435 pr_err("%s: Could not attach to PHY\n", dev->name);
436 return PTR_ERR(phydev);
437 }
438
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000439 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000440 if ((interface == PHY_INTERFACE_MODE_MII) ||
441 (interface == PHY_INTERFACE_MODE_RMII))
442 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
443 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000444
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700445 /*
446 * Broken HW is sometimes missing the pull-up resistor on the
447 * MDIO line, which results in reads to non-existent devices returning
448 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
449 * device as well.
450 * Note: phydev->phy_id is the result of reading the UID PHY registers.
451 */
452 if (phydev->phy_id == 0) {
453 phy_disconnect(phydev);
454 return -ENODEV;
455 }
456 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000457 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700458
459 priv->phydev = phydev;
460
461 return 0;
462}
463
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700464/**
465 * display_ring
466 * @p: pointer to the ring.
467 * @size: size of the ring.
468 * Description: display all the descriptors within the ring.
469 */
470static void display_ring(struct dma_desc *p, int size)
471{
472 struct tmp_s {
473 u64 a;
474 unsigned int b;
475 unsigned int c;
476 };
477 int i;
478 for (i = 0; i < size; i++) {
479 struct tmp_s *x = (struct tmp_s *)(p + i);
480 pr_info("\t%d [0x%x]: DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
481 i, (unsigned int)virt_to_phys(&p[i]),
482 (unsigned int)(x->a), (unsigned int)((x->a) >> 32),
483 x->b, x->c);
484 pr_info("\n");
485 }
486}
487
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000488static int stmmac_set_bfsize(int mtu, int bufsize)
489{
490 int ret = bufsize;
491
492 if (mtu >= BUF_SIZE_4KiB)
493 ret = BUF_SIZE_8KiB;
494 else if (mtu >= BUF_SIZE_2KiB)
495 ret = BUF_SIZE_4KiB;
496 else if (mtu >= DMA_BUFFER_SIZE)
497 ret = BUF_SIZE_2KiB;
498 else
499 ret = DMA_BUFFER_SIZE;
500
501 return ret;
502}
503
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700504/**
505 * init_dma_desc_rings - init the RX/TX descriptor rings
506 * @dev: net device structure
507 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000508 * and allocates the socket buffers. It suppors the chained and ring
509 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700510 */
511static void init_dma_desc_rings(struct net_device *dev)
512{
513 int i;
514 struct stmmac_priv *priv = netdev_priv(dev);
515 struct sk_buff *skb;
516 unsigned int txsize = priv->dma_tx_size;
517 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000518 unsigned int bfsize;
519 int dis_ic = 0;
520 int des3_as_data_buf = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700521
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000522 /* Set the max buffer size according to the DESC mode
523 * and the MTU. Note that RING mode allows 16KiB bsize. */
524 bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
525
526 if (bfsize == BUF_SIZE_16KiB)
527 des3_as_data_buf = 1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700528 else
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000529 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700530
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700531 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
532 txsize, rxsize, bfsize);
533
534 priv->rx_skbuff_dma = kmalloc(rxsize * sizeof(dma_addr_t), GFP_KERNEL);
535 priv->rx_skbuff =
536 kmalloc(sizeof(struct sk_buff *) * rxsize, GFP_KERNEL);
537 priv->dma_rx =
538 (struct dma_desc *)dma_alloc_coherent(priv->device,
539 rxsize *
540 sizeof(struct dma_desc),
541 &priv->dma_rx_phy,
542 GFP_KERNEL);
543 priv->tx_skbuff = kmalloc(sizeof(struct sk_buff *) * txsize,
544 GFP_KERNEL);
545 priv->dma_tx =
546 (struct dma_desc *)dma_alloc_coherent(priv->device,
547 txsize *
548 sizeof(struct dma_desc),
549 &priv->dma_tx_phy,
550 GFP_KERNEL);
551
552 if ((priv->dma_rx == NULL) || (priv->dma_tx == NULL)) {
553 pr_err("%s:ERROR allocating the DMA Tx/Rx desc\n", __func__);
554 return;
555 }
556
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000557 DBG(probe, INFO, "stmmac (%s) DMA desc: virt addr (Rx %p, "
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700558 "Tx %p)\n\tDMA phy addr (Rx 0x%08x, Tx 0x%08x)\n",
559 dev->name, priv->dma_rx, priv->dma_tx,
560 (unsigned int)priv->dma_rx_phy, (unsigned int)priv->dma_tx_phy);
561
562 /* RX INITIALIZATION */
563 DBG(probe, INFO, "stmmac: SKB addresses:\n"
564 "skb\t\tskb data\tdma data\n");
565
566 for (i = 0; i < rxsize; i++) {
567 struct dma_desc *p = priv->dma_rx + i;
568
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +0000569 skb = __netdev_alloc_skb(dev, bfsize + NET_IP_ALIGN,
570 GFP_KERNEL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700571 if (unlikely(skb == NULL)) {
572 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
573 break;
574 }
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +0000575 skb_reserve(skb, NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700576 priv->rx_skbuff[i] = skb;
577 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
578 bfsize, DMA_FROM_DEVICE);
579
580 p->des2 = priv->rx_skbuff_dma[i];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000581
582 priv->hw->ring->init_desc3(des3_as_data_buf, p);
583
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700584 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
585 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
586 }
587 priv->cur_rx = 0;
588 priv->dirty_rx = (unsigned int)(i - rxsize);
589 priv->dma_buf_sz = bfsize;
590 buf_sz = bfsize;
591
592 /* TX INITIALIZATION */
593 for (i = 0; i < txsize; i++) {
594 priv->tx_skbuff[i] = NULL;
595 priv->dma_tx[i].des2 = 0;
596 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000597
598 /* In case of Chained mode this sets the des3 to the next
599 * element in the chain */
600 priv->hw->ring->init_dma_chain(priv->dma_rx, priv->dma_rx_phy, rxsize);
601 priv->hw->ring->init_dma_chain(priv->dma_tx, priv->dma_tx_phy, txsize);
602
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700603 priv->dirty_tx = 0;
604 priv->cur_tx = 0;
605
606 /* Clear the Rx/Tx descriptors */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000607 priv->hw->desc->init_rx_desc(priv->dma_rx, rxsize, dis_ic);
608 priv->hw->desc->init_tx_desc(priv->dma_tx, txsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700609
610 if (netif_msg_hw(priv)) {
611 pr_info("RX descriptor ring:\n");
612 display_ring(priv->dma_rx, rxsize);
613 pr_info("TX descriptor ring:\n");
614 display_ring(priv->dma_tx, txsize);
615 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700616}
617
618static void dma_free_rx_skbufs(struct stmmac_priv *priv)
619{
620 int i;
621
622 for (i = 0; i < priv->dma_rx_size; i++) {
623 if (priv->rx_skbuff[i]) {
624 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
625 priv->dma_buf_sz, DMA_FROM_DEVICE);
626 dev_kfree_skb_any(priv->rx_skbuff[i]);
627 }
628 priv->rx_skbuff[i] = NULL;
629 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700630}
631
632static void dma_free_tx_skbufs(struct stmmac_priv *priv)
633{
634 int i;
635
636 for (i = 0; i < priv->dma_tx_size; i++) {
637 if (priv->tx_skbuff[i] != NULL) {
638 struct dma_desc *p = priv->dma_tx + i;
639 if (p->des2)
640 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000641 priv->hw->desc->get_tx_len(p),
642 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700643 dev_kfree_skb_any(priv->tx_skbuff[i]);
644 priv->tx_skbuff[i] = NULL;
645 }
646 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700647}
648
649static void free_dma_desc_resources(struct stmmac_priv *priv)
650{
651 /* Release the DMA TX/RX socket buffers */
652 dma_free_rx_skbufs(priv);
653 dma_free_tx_skbufs(priv);
654
655 /* Free the region of consistent memory previously allocated for
656 * the DMA */
657 dma_free_coherent(priv->device,
658 priv->dma_tx_size * sizeof(struct dma_desc),
659 priv->dma_tx, priv->dma_tx_phy);
660 dma_free_coherent(priv->device,
661 priv->dma_rx_size * sizeof(struct dma_desc),
662 priv->dma_rx, priv->dma_rx_phy);
663 kfree(priv->rx_skbuff_dma);
664 kfree(priv->rx_skbuff);
665 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700666}
667
668/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700669 * stmmac_dma_operation_mode - HW DMA operation mode
670 * @priv : pointer to the private device structure.
671 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000672 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700673 */
674static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
675{
Srinivas Kandagatla61b80132011-07-17 20:54:09 +0000676 if (likely(priv->plat->force_sf_dma_mode ||
677 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
678 /*
679 * In case of GMAC, SF mode can be enabled
680 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000681 * 1) TX COE if actually supported
682 * 2) There is no bugged Jumbo frame support
683 * that needs to not insert csum in the TDES.
684 */
685 priv->hw->dma->dma_mode(priv->ioaddr,
686 SF_DMA_MODE, SF_DMA_MODE);
687 tc = SF_DMA_MODE;
688 } else
689 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700690}
691
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700692/**
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000693 * stmmac_tx_clean:
694 * @priv: private data pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700695 * Description: it reclaims resources after transmission completes.
696 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000697static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700698{
699 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700700
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000701 spin_lock(&priv->tx_lock);
702
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000703 priv->xstats.tx_clean++;
704
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700705 while (priv->dirty_tx != priv->cur_tx) {
706 int last;
707 unsigned int entry = priv->dirty_tx % txsize;
708 struct sk_buff *skb = priv->tx_skbuff[entry];
709 struct dma_desc *p = priv->dma_tx + entry;
710
711 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000712 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700713 break;
714
715 /* Verify tx error by looking at the last segment */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000716 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700717 if (likely(last)) {
718 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000719 priv->hw->desc->tx_status(&priv->dev->stats,
720 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000721 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700722 if (likely(tx_error == 0)) {
723 priv->dev->stats.tx_packets++;
724 priv->xstats.tx_pkt_n++;
725 } else
726 priv->dev->stats.tx_errors++;
727 }
728 TX_DBG("%s: curr %d, dirty %d\n", __func__,
729 priv->cur_tx, priv->dirty_tx);
730
731 if (likely(p->des2))
732 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000733 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700734 DMA_TO_DEVICE);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000735 priv->hw->ring->clean_desc3(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700736
737 if (likely(skb != NULL)) {
Eric Dumazetacb600d2012-10-05 06:23:55 +0000738 dev_kfree_skb(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700739 priv->tx_skbuff[entry] = NULL;
740 }
741
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000742 priv->hw->desc->release_tx_desc(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700743
Giuseppe CAVALLARO13497f52012-06-04 06:36:22 +0000744 priv->dirty_tx++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700745 }
746 if (unlikely(netif_queue_stopped(priv->dev) &&
747 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
748 netif_tx_lock(priv->dev);
749 if (netif_queue_stopped(priv->dev) &&
750 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
751 TX_DBG("%s: restart transmit\n", __func__);
752 netif_wake_queue(priv->dev);
753 }
754 netif_tx_unlock(priv->dev);
755 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000756
757 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
758 stmmac_enable_eee_mode(priv);
759 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
760 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000761 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700762}
763
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000764static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700765{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +0000766 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700767}
768
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000769static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700770{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +0000771 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700772}
773
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700774
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700775/**
776 * stmmac_tx_err:
777 * @priv: pointer to the private device structure
778 * Description: it cleans the descriptors and restarts the transmission
779 * in case of errors.
780 */
781static void stmmac_tx_err(struct stmmac_priv *priv)
782{
783 netif_stop_queue(priv->dev);
784
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000785 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700786 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000787 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700788 priv->dirty_tx = 0;
789 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000790 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700791
792 priv->dev->stats.tx_errors++;
793 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700794}
795
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000796static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700797{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000798 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700799
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000800 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000801 if (likely((status & handle_rx)) || (status & handle_tx)) {
802 if (likely(napi_schedule_prep(&priv->napi))) {
803 stmmac_disable_dma_irq(priv);
804 __napi_schedule(&priv->napi);
805 }
806 }
807 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000808 /* Try to bump up the dma threshold on this failure */
809 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
810 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000811 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000812 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700813 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000814 } else if (unlikely(status == tx_hard_error))
815 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700816}
817
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000818static void stmmac_mmc_setup(struct stmmac_priv *priv)
819{
820 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
821 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
822
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000823 /* Mask MMC irq, counters are managed in SW and registers
824 * are cleared on each READ eventually. */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000825 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000826
827 if (priv->dma_cap.rmon) {
828 dwmac_mmc_ctrl(priv->ioaddr, mode);
829 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
830 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +0000831 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000832}
833
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000834static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
835{
836 u32 hwid = priv->hw->synopsys_uid;
837
838 /* Only check valid Synopsys Id because old MAC chips
839 * have no HW registers where get the ID */
840 if (likely(hwid)) {
841 u32 uid = ((hwid & 0x0000ff00) >> 8);
842 u32 synid = (hwid & 0x000000ff);
843
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000844 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000845 uid, synid);
846
847 return synid;
848 }
849 return 0;
850}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000851
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000852/**
853 * stmmac_selec_desc_mode
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +0000854 * @priv : private structure
855 * Description: select the Enhanced/Alternate or Normal descriptors
856 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000857static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
858{
859 if (priv->plat->enh_desc) {
860 pr_info(" Enhanced/Alternate descriptors\n");
861 priv->hw->desc = &enh_desc_ops;
862 } else {
863 pr_info(" Normal descriptors\n");
864 priv->hw->desc = &ndesc_ops;
865 }
866}
867
868/**
869 * stmmac_get_hw_features
870 * @priv : private device pointer
871 * Description:
872 * new GMAC chip generations have a new register to indicate the
873 * presence of the optional feature/functions.
874 * This can be also used to override the value passed through the
875 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000876 */
877static int stmmac_get_hw_features(struct stmmac_priv *priv)
878{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +0000879 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +0000880
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +0000881 if (priv->hw->dma->get_hw_feature) {
882 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000883
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000884 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
885 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
886 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
887 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
888 priv->dma_cap.multi_addr =
889 (hw_cap & DMA_HW_FEAT_ADDMACADRSEL) >> 5;
890 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
891 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
892 priv->dma_cap.pmt_remote_wake_up =
893 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
894 priv->dma_cap.pmt_magic_frame =
895 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000896 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000897 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000898 /* IEEE 1588-2002*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000899 priv->dma_cap.time_stamp =
900 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000901 /* IEEE 1588-2008*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000902 priv->dma_cap.atime_stamp =
903 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000904 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000905 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
906 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000907 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000908 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
909 priv->dma_cap.rx_coe_type1 =
910 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
911 priv->dma_cap.rx_coe_type2 =
912 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
913 priv->dma_cap.rxfifo_over_2048 =
914 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000915 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000916 priv->dma_cap.number_rx_channel =
917 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
918 priv->dma_cap.number_tx_channel =
919 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000920 /* Alternate (enhanced) DESC mode*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +0000921 priv->dma_cap.enh_desc =
922 (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000923 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000924
925 return hw_cap;
926}
927
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000928static void stmmac_check_ether_addr(struct stmmac_priv *priv)
929{
930 /* verify if the MAC address is valid, in case of failures it
931 * generates a random MAC address */
932 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
933 priv->hw->mac->get_umac_addr((void __iomem *)
934 priv->dev->base_addr,
935 priv->dev->dev_addr, 0);
936 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +0000937 eth_hw_addr_random(priv->dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000938 }
939 pr_warning("%s: device MAC address %pM\n", priv->dev->name,
940 priv->dev->dev_addr);
941}
942
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +0000943static int stmmac_init_dma_engine(struct stmmac_priv *priv)
944{
945 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +0000946 int mixed_burst = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +0000947
948 /* Some DMA parameters can be passed from the platform;
949 * in case of these are not passed we keep a default
950 * (good for all the chips) and init the DMA! */
951 if (priv->plat->dma_cfg) {
952 pbl = priv->plat->dma_cfg->pbl;
953 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +0000954 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +0000955 burst_len = priv->plat->dma_cfg->burst_len;
956 }
957
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +0000958 return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +0000959 burst_len, priv->dma_tx_phy,
960 priv->dma_rx_phy);
961}
962
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000963/**
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000964 * stmmac_tx_timer:
965 * @data: data pointer
966 * Description:
967 * This is the timer handler to directly invoke the stmmac_tx_clean.
968 */
969static void stmmac_tx_timer(unsigned long data)
970{
971 struct stmmac_priv *priv = (struct stmmac_priv *)data;
972
973 stmmac_tx_clean(priv);
974}
975
976/**
977 * stmmac_tx_timer:
978 * @priv: private data structure
979 * Description:
980 * This inits the transmit coalesce parameters: i.e. timer rate,
981 * timer handler and default threshold used for enabling the
982 * interrupt on completion bit.
983 */
984static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
985{
986 priv->tx_coal_frames = STMMAC_TX_FRAMES;
987 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
988 init_timer(&priv->txtimer);
989 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
990 priv->txtimer.data = (unsigned long)priv;
991 priv->txtimer.function = stmmac_tx_timer;
992 add_timer(&priv->txtimer);
993}
994
995/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700996 * stmmac_open - open entry point of the driver
997 * @dev : pointer to the device structure.
998 * Description:
999 * This function is the open entry point of the driver.
1000 * Return value:
1001 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1002 * file on failure.
1003 */
1004static int stmmac_open(struct net_device *dev)
1005{
1006 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001007 int ret;
1008
Stefan Roesea6308442012-09-21 01:06:29 +00001009 clk_prepare_enable(priv->stmmac_clk);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001010
1011 stmmac_check_ether_addr(priv);
1012
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001013 ret = stmmac_init_phy(dev);
1014 if (unlikely(ret)) {
1015 pr_err("%s: Cannot attach to PHY (error: %d)\n", __func__, ret);
1016 goto open_error;
1017 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001018
1019 /* Create and initialize the TX/RX descriptors chains. */
1020 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1021 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1022 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
1023 init_dma_desc_rings(dev);
1024
1025 /* DMA initialization and SW reset */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001026 ret = stmmac_init_dma_engine(priv);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001027 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001028 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001029 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001030 }
1031
1032 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001033 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001034
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +00001035 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001036 if (priv->plat->bus_setup)
1037 priv->plat->bus_setup(priv->ioaddr);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001038
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001039 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001040 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001041
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001042 /* Request the IRQ lines */
1043 ret = request_irq(dev->irq, stmmac_interrupt,
1044 IRQF_SHARED, dev->name, dev);
1045 if (unlikely(ret < 0)) {
1046 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1047 __func__, dev->irq, ret);
1048 goto open_error;
1049 }
1050
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001051 /* Request the Wake IRQ in case of another line is used for WoL */
1052 if (priv->wol_irq != dev->irq) {
1053 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1054 IRQF_SHARED, dev->name, dev);
1055 if (unlikely(ret < 0)) {
1056 pr_err("%s: ERROR: allocating the ext WoL IRQ %d "
1057 "(error: %d)\n", __func__, priv->wol_irq, ret);
1058 goto open_error_wolirq;
1059 }
1060 }
1061
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001062 /* Request the IRQ lines */
1063 if (priv->lpi_irq != -ENXIO) {
1064 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1065 dev->name, dev);
1066 if (unlikely(ret < 0)) {
1067 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1068 __func__, priv->lpi_irq, ret);
1069 goto open_error_lpiirq;
1070 }
1071 }
1072
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001073 /* Enable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001074 stmmac_set_mac(priv->ioaddr, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001075
1076 /* Set the HW DMA mode and the COE */
1077 stmmac_dma_operation_mode(priv);
1078
1079 /* Extra statistics */
1080 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1081 priv->xstats.threshold = tc;
1082
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001083 stmmac_mmc_setup(priv);
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001084
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001085#ifdef CONFIG_STMMAC_DEBUG_FS
1086 ret = stmmac_init_fs(dev);
1087 if (ret < 0)
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001088 pr_warning("%s: failed debugFS registration\n", __func__);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001089#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001090 /* Start the ball rolling... */
1091 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001092 priv->hw->dma->start_tx(priv->ioaddr);
1093 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001094
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001095 /* Dump DMA/MAC registers */
1096 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001097 priv->hw->mac->dump_regs(priv->ioaddr);
1098 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001099 }
1100
1101 if (priv->phydev)
1102 phy_start(priv->phydev);
1103
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001104 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS_TIMER;
1105 priv->eee_enabled = stmmac_eee_init(priv);
1106
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001107 stmmac_init_tx_coalesce(priv);
1108
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001109 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001110 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001111
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001112 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001113
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001114open_error_lpiirq:
1115 if (priv->wol_irq != dev->irq)
1116 free_irq(priv->wol_irq, dev);
1117
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001118open_error_wolirq:
1119 free_irq(dev->irq, dev);
1120
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001121open_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001122 if (priv->phydev)
1123 phy_disconnect(priv->phydev);
1124
Stefan Roesea6308442012-09-21 01:06:29 +00001125 clk_disable_unprepare(priv->stmmac_clk);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001126
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001127 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001128}
1129
1130/**
1131 * stmmac_release - close entry point of the driver
1132 * @dev : device pointer.
1133 * Description:
1134 * This is the stop entry point of the driver.
1135 */
1136static int stmmac_release(struct net_device *dev)
1137{
1138 struct stmmac_priv *priv = netdev_priv(dev);
1139
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001140 if (priv->eee_enabled)
1141 del_timer_sync(&priv->eee_ctrl_timer);
1142
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001143 /* Stop and disconnect the PHY */
1144 if (priv->phydev) {
1145 phy_stop(priv->phydev);
1146 phy_disconnect(priv->phydev);
1147 priv->phydev = NULL;
1148 }
1149
1150 netif_stop_queue(dev);
1151
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001152 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001153
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001154 del_timer_sync(&priv->txtimer);
1155
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001156 /* Free the IRQ lines */
1157 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001158 if (priv->wol_irq != dev->irq)
1159 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001160 if (priv->lpi_irq != -ENXIO)
1161 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001162
1163 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001164 priv->hw->dma->stop_tx(priv->ioaddr);
1165 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001166
1167 /* Release and free the Rx/Tx resources */
1168 free_dma_desc_resources(priv);
1169
avisconti19449bf2010-10-25 18:58:14 +00001170 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001171 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001172
1173 netif_carrier_off(dev);
1174
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001175#ifdef CONFIG_STMMAC_DEBUG_FS
1176 stmmac_exit_fs();
1177#endif
Stefan Roesea6308442012-09-21 01:06:29 +00001178 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001179
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001180 return 0;
1181}
1182
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001183/**
1184 * stmmac_xmit:
1185 * @skb : the socket buffer
1186 * @dev : device pointer
1187 * Description : Tx entry point of the driver.
1188 */
1189static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1190{
1191 struct stmmac_priv *priv = netdev_priv(dev);
1192 unsigned int txsize = priv->dma_tx_size;
1193 unsigned int entry;
1194 int i, csum_insertion = 0;
1195 int nfrags = skb_shinfo(skb)->nr_frags;
1196 struct dma_desc *desc, *first;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001197 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001198
1199 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1200 if (!netif_queue_stopped(dev)) {
1201 netif_stop_queue(dev);
1202 /* This is a hard error, log it. */
1203 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1204 __func__);
1205 }
1206 return NETDEV_TX_BUSY;
1207 }
1208
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001209 spin_lock(&priv->tx_lock);
1210
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001211 if (priv->tx_path_in_lpi_mode)
1212 stmmac_disable_eee_mode(priv);
1213
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001214 entry = priv->cur_tx % txsize;
1215
1216#ifdef STMMAC_XMIT_DEBUG
1217 if ((skb->len > ETH_FRAME_LEN) || nfrags)
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001218 pr_debug("stmmac xmit: [entry %d]\n"
1219 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1220 "\tn_frags: %d - ip_summed: %d - %s gso\n"
1221 "\ttx_count_frames %d\n", entry,
1222 skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
1223 !skb_is_gso(skb) ? "isn't" : "is",
1224 priv->tx_count_frames);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001225#endif
1226
Michał Mirosław5e982f32011-04-09 02:46:55 +00001227 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001228
1229 desc = priv->dma_tx + entry;
1230 first = desc;
1231
1232#ifdef STMMAC_XMIT_DEBUG
1233 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001234 pr_debug("\tskb len: %d, nopaged_len: %d,\n"
1235 "\t\tn_frags: %d, ip_summed: %d\n",
1236 skb->len, nopaged_len, nfrags, skb->ip_summed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001237#endif
1238 priv->tx_skbuff[entry] = skb;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001239
1240 if (priv->hw->ring->is_jumbo_frm(skb->len, priv->plat->enh_desc)) {
1241 entry = priv->hw->ring->jumbo_frm(priv, skb, csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001242 desc = priv->dma_tx + entry;
1243 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001244 desc->des2 = dma_map_single(priv->device, skb->data,
1245 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001246 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1247 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001248 }
1249
1250 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001251 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1252 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001253
1254 entry = (++priv->cur_tx) % txsize;
1255 desc = priv->dma_tx + entry;
1256
1257 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
Ian Campbellf7223802011-09-21 21:53:20 +00001258 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1259 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001260 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001261 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001262 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001263 priv->hw->desc->set_tx_owner(desc);
Deepak Sikri8e839892012-07-08 21:14:45 +00001264 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001265 }
1266
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001267 /* Finalize the latest segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001268 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001269
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001270 wmb();
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001271 /* According to the coalesce parameter the IC bit for the latest
1272 * segment could be reset and the timer re-started to invoke the
1273 * stmmac_tx function. This approach takes care about the fragments.
1274 */
1275 priv->tx_count_frames += nfrags + 1;
1276 if (priv->tx_coal_frames > priv->tx_count_frames) {
1277 priv->hw->desc->clear_tx_ic(desc);
1278 priv->xstats.tx_reset_ic_bit++;
1279 TX_DBG("\t[entry %d]: tx_count_frames %d\n", entry,
1280 priv->tx_count_frames);
1281 mod_timer(&priv->txtimer,
1282 STMMAC_COAL_TIMER(priv->tx_coal_timer));
1283 } else
1284 priv->tx_count_frames = 0;
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001285
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001286 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001287 priv->hw->desc->set_tx_owner(first);
Deepak Sikri8e839892012-07-08 21:14:45 +00001288 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001289
1290 priv->cur_tx++;
1291
1292#ifdef STMMAC_XMIT_DEBUG
1293 if (netif_msg_pktdata(priv)) {
1294 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1295 "first=%p, nfrags=%d\n",
1296 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1297 entry, first, nfrags);
1298 display_ring(priv->dma_tx, txsize);
1299 pr_info(">>> frame to be transmitted: ");
1300 print_pkt(skb->data, skb->len);
1301 }
1302#endif
1303 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1304 TX_DBG("%s: stop transmitted packets\n", __func__);
1305 netif_stop_queue(dev);
1306 }
1307
1308 dev->stats.tx_bytes += skb->len;
1309
Richard Cochran3e82ce12011-06-12 02:19:06 +00001310 skb_tx_timestamp(skb);
1311
Richard Cochran52f64fa2011-06-19 03:31:43 +00001312 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1313
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001314 spin_unlock(&priv->tx_lock);
1315
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001316 return NETDEV_TX_OK;
1317}
1318
1319static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1320{
1321 unsigned int rxsize = priv->dma_rx_size;
1322 int bfsize = priv->dma_buf_sz;
1323 struct dma_desc *p = priv->dma_rx;
1324
1325 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1326 unsigned int entry = priv->dirty_rx % rxsize;
1327 if (likely(priv->rx_skbuff[entry] == NULL)) {
1328 struct sk_buff *skb;
1329
Eric Dumazetacb600d2012-10-05 06:23:55 +00001330 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001331
1332 if (unlikely(skb == NULL))
1333 break;
1334
1335 priv->rx_skbuff[entry] = skb;
1336 priv->rx_skbuff_dma[entry] =
1337 dma_map_single(priv->device, skb->data, bfsize,
1338 DMA_FROM_DEVICE);
1339
1340 (p + entry)->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001341
1342 if (unlikely(priv->plat->has_gmac))
1343 priv->hw->ring->refill_desc3(bfsize, p + entry);
1344
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001345 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1346 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001347 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001348 priv->hw->desc->set_rx_owner(p + entry);
Deepak Sikri8e839892012-07-08 21:14:45 +00001349 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001350 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001351}
1352
1353static int stmmac_rx(struct stmmac_priv *priv, int limit)
1354{
1355 unsigned int rxsize = priv->dma_rx_size;
1356 unsigned int entry = priv->cur_rx % rxsize;
1357 unsigned int next_entry;
1358 unsigned int count = 0;
1359 struct dma_desc *p = priv->dma_rx + entry;
1360 struct dma_desc *p_next;
1361
1362#ifdef STMMAC_RX_DEBUG
1363 if (netif_msg_hw(priv)) {
1364 pr_debug(">>> stmmac_rx: descriptor ring:\n");
1365 display_ring(priv->dma_rx, rxsize);
1366 }
1367#endif
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001368 while (!priv->hw->desc->get_rx_owner(p)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001369 int status;
1370
1371 if (count >= limit)
1372 break;
1373
1374 count++;
1375
1376 next_entry = (++priv->cur_rx) % rxsize;
1377 p_next = priv->dma_rx + next_entry;
1378 prefetch(p_next);
1379
1380 /* read the status of the incoming frame */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001381 status = (priv->hw->desc->rx_status(&priv->dev->stats,
1382 &priv->xstats, p));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001383 if (unlikely(status == discard_frame))
1384 priv->dev->stats.rx_errors++;
1385 else {
1386 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001387 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001388
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001389 frame_len = priv->hw->desc->get_rx_frame_len(p,
1390 priv->plat->rx_coe);
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001391 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1392 * Type frames (LLC/LLC-SNAP) */
1393 if (unlikely(status != llc_snap))
1394 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001395#ifdef STMMAC_RX_DEBUG
1396 if (frame_len > ETH_FRAME_LEN)
1397 pr_debug("\tRX frame size %d, COE status: %d\n",
1398 frame_len, status);
1399
1400 if (netif_msg_hw(priv))
1401 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1402 p, entry, p->des2);
1403#endif
1404 skb = priv->rx_skbuff[entry];
1405 if (unlikely(!skb)) {
1406 pr_err("%s: Inconsistent Rx descriptor chain\n",
1407 priv->dev->name);
1408 priv->dev->stats.rx_dropped++;
1409 break;
1410 }
1411 prefetch(skb->data - NET_IP_ALIGN);
1412 priv->rx_skbuff[entry] = NULL;
1413
1414 skb_put(skb, frame_len);
1415 dma_unmap_single(priv->device,
1416 priv->rx_skbuff_dma[entry],
1417 priv->dma_buf_sz, DMA_FROM_DEVICE);
1418#ifdef STMMAC_RX_DEBUG
1419 if (netif_msg_pktdata(priv)) {
1420 pr_info(" frame received (%dbytes)", frame_len);
1421 print_pkt(skb->data, frame_len);
1422 }
1423#endif
1424 skb->protocol = eth_type_trans(skb, priv->dev);
1425
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001426 if (unlikely(!priv->plat->rx_coe)) {
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001427 /* No RX COE for old mac10/100 devices */
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001428 skb_checksum_none_assert(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001429 netif_receive_skb(skb);
1430 } else {
1431 skb->ip_summed = CHECKSUM_UNNECESSARY;
1432 napi_gro_receive(&priv->napi, skb);
1433 }
1434
1435 priv->dev->stats.rx_packets++;
1436 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001437 }
1438 entry = next_entry;
1439 p = p_next; /* use prefetched values */
1440 }
1441
1442 stmmac_rx_refill(priv);
1443
1444 priv->xstats.rx_pkt_n += count;
1445
1446 return count;
1447}
1448
1449/**
1450 * stmmac_poll - stmmac poll method (NAPI)
1451 * @napi : pointer to the napi structure.
1452 * @budget : maximum number of packets that the current CPU can receive from
1453 * all interfaces.
1454 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001455 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001456 */
1457static int stmmac_poll(struct napi_struct *napi, int budget)
1458{
1459 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1460 int work_done = 0;
1461
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001462 priv->xstats.napi_poll++;
1463 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001464
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001465 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001466 if (work_done < budget) {
1467 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001468 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001469 }
1470 return work_done;
1471}
1472
1473/**
1474 * stmmac_tx_timeout
1475 * @dev : Pointer to net device structure
1476 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001477 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001478 * netdev structure and arrange for the device to be reset to a sane state
1479 * in order to transmit a new packet.
1480 */
1481static void stmmac_tx_timeout(struct net_device *dev)
1482{
1483 struct stmmac_priv *priv = netdev_priv(dev);
1484
1485 /* Clear Tx resources and restart transmitting again */
1486 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001487}
1488
1489/* Configuration changes (passed on by ifconfig) */
1490static int stmmac_config(struct net_device *dev, struct ifmap *map)
1491{
1492 if (dev->flags & IFF_UP) /* can't act on a running interface */
1493 return -EBUSY;
1494
1495 /* Don't allow changing the I/O address */
1496 if (map->base_addr != dev->base_addr) {
1497 pr_warning("%s: can't change I/O address\n", dev->name);
1498 return -EOPNOTSUPP;
1499 }
1500
1501 /* Don't allow changing the IRQ */
1502 if (map->irq != dev->irq) {
1503 pr_warning("%s: can't change IRQ number %d\n",
1504 dev->name, dev->irq);
1505 return -EOPNOTSUPP;
1506 }
1507
1508 /* ignore other fields */
1509 return 0;
1510}
1511
1512/**
Jiri Pirko01789342011-08-16 06:29:00 +00001513 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001514 * @dev : pointer to the device structure
1515 * Description:
1516 * This function is a driver entry point which gets called by the kernel
1517 * whenever multicast addresses must be enabled/disabled.
1518 * Return value:
1519 * void.
1520 */
Jiri Pirko01789342011-08-16 06:29:00 +00001521static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001522{
1523 struct stmmac_priv *priv = netdev_priv(dev);
1524
1525 spin_lock(&priv->lock);
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00001526 priv->hw->mac->set_filter(dev, priv->synopsys_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001527 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001528}
1529
1530/**
1531 * stmmac_change_mtu - entry point to change MTU size for the device.
1532 * @dev : device pointer.
1533 * @new_mtu : the new MTU size for the device.
1534 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1535 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1536 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1537 * Return value:
1538 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1539 * file on failure.
1540 */
1541static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1542{
1543 struct stmmac_priv *priv = netdev_priv(dev);
1544 int max_mtu;
1545
1546 if (netif_running(dev)) {
1547 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1548 return -EBUSY;
1549 }
1550
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00001551 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001552 max_mtu = JUMBO_LEN;
1553 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00001554 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001555
1556 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1557 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1558 return -EINVAL;
1559 }
1560
Michał Mirosław5e982f32011-04-09 02:46:55 +00001561 dev->mtu = new_mtu;
1562 netdev_update_features(dev);
1563
1564 return 0;
1565}
1566
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001567static netdev_features_t stmmac_fix_features(struct net_device *dev,
1568 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001569{
1570 struct stmmac_priv *priv = netdev_priv(dev);
1571
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001572 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001573 features &= ~NETIF_F_RXCSUM;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001574 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
1575 features &= ~NETIF_F_IPV6_CSUM;
Michał Mirosław5e982f32011-04-09 02:46:55 +00001576 if (!priv->plat->tx_coe)
1577 features &= ~NETIF_F_ALL_CSUM;
1578
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001579 /* Some GMAC devices have a bugged Jumbo frame support that
1580 * needs to have the Tx COE disabled for oversized frames
1581 * (due to limited buffer sizes). In this case we disable
1582 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001583 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1584 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001585
Michał Mirosław5e982f32011-04-09 02:46:55 +00001586 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001587}
1588
1589static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1590{
1591 struct net_device *dev = (struct net_device *)dev_id;
1592 struct stmmac_priv *priv = netdev_priv(dev);
1593
1594 if (unlikely(!dev)) {
1595 pr_err("%s: invalid dev pointer\n", __func__);
1596 return IRQ_NONE;
1597 }
1598
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001599 /* To handle GMAC own interrupts */
1600 if (priv->plat->has_gmac) {
1601 int status = priv->hw->mac->host_irq_status((void __iomem *)
1602 dev->base_addr);
1603 if (unlikely(status)) {
1604 if (status & core_mmc_tx_irq)
1605 priv->xstats.mmc_tx_irq_n++;
1606 if (status & core_mmc_rx_irq)
1607 priv->xstats.mmc_rx_irq_n++;
1608 if (status & core_mmc_rx_csum_offload_irq)
1609 priv->xstats.mmc_rx_csum_offload_irq_n++;
1610 if (status & core_irq_receive_pmt_irq)
1611 priv->xstats.irq_receive_pmt_irq_n++;
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001612
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001613 /* For LPI we need to save the tx status */
1614 if (status & core_irq_tx_path_in_lpi_mode) {
1615 priv->xstats.irq_tx_path_in_lpi_mode_n++;
1616 priv->tx_path_in_lpi_mode = true;
1617 }
1618 if (status & core_irq_tx_path_exit_lpi_mode) {
1619 priv->xstats.irq_tx_path_exit_lpi_mode_n++;
1620 priv->tx_path_in_lpi_mode = false;
1621 }
1622 if (status & core_irq_rx_path_in_lpi_mode)
1623 priv->xstats.irq_rx_path_in_lpi_mode_n++;
1624 if (status & core_irq_rx_path_exit_lpi_mode)
1625 priv->xstats.irq_rx_path_exit_lpi_mode_n++;
1626 }
1627 }
1628
1629 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001630 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001631
1632 return IRQ_HANDLED;
1633}
1634
1635#ifdef CONFIG_NET_POLL_CONTROLLER
1636/* Polling receive - used by NETCONSOLE and other diagnostic tools
1637 * to allow network I/O with interrupts disabled. */
1638static void stmmac_poll_controller(struct net_device *dev)
1639{
1640 disable_irq(dev->irq);
1641 stmmac_interrupt(dev->irq, dev);
1642 enable_irq(dev->irq);
1643}
1644#endif
1645
1646/**
1647 * stmmac_ioctl - Entry point for the Ioctl
1648 * @dev: Device pointer.
1649 * @rq: An IOCTL specefic structure, that can contain a pointer to
1650 * a proprietary structure used to pass information to the driver.
1651 * @cmd: IOCTL command
1652 * Description:
1653 * Currently there are no special functionality supported in IOCTL, just the
1654 * phy_mii_ioctl(...) can be invoked.
1655 */
1656static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1657{
1658 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001659 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001660
1661 if (!netif_running(dev))
1662 return -EINVAL;
1663
Richard Cochran28b04112010-07-17 08:48:55 +00001664 if (!priv->phydev)
1665 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001666
Richard Cochran28b04112010-07-17 08:48:55 +00001667 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
Richard Cochran28b04112010-07-17 08:48:55 +00001668
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001669 return ret;
1670}
1671
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001672#ifdef CONFIG_STMMAC_DEBUG_FS
1673static struct dentry *stmmac_fs_dir;
1674static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001675static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001676
1677static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
1678{
1679 struct tmp_s {
1680 u64 a;
1681 unsigned int b;
1682 unsigned int c;
1683 };
1684 int i;
1685 struct net_device *dev = seq->private;
1686 struct stmmac_priv *priv = netdev_priv(dev);
1687
1688 seq_printf(seq, "=======================\n");
1689 seq_printf(seq, " RX descriptor ring\n");
1690 seq_printf(seq, "=======================\n");
1691
1692 for (i = 0; i < priv->dma_rx_size; i++) {
1693 struct tmp_s *x = (struct tmp_s *)(priv->dma_rx + i);
1694 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1695 i, (unsigned int)(x->a),
1696 (unsigned int)((x->a) >> 32), x->b, x->c);
1697 seq_printf(seq, "\n");
1698 }
1699
1700 seq_printf(seq, "\n");
1701 seq_printf(seq, "=======================\n");
1702 seq_printf(seq, " TX descriptor ring\n");
1703 seq_printf(seq, "=======================\n");
1704
1705 for (i = 0; i < priv->dma_tx_size; i++) {
1706 struct tmp_s *x = (struct tmp_s *)(priv->dma_tx + i);
1707 seq_printf(seq, "[%d] DES0=0x%x DES1=0x%x BUF1=0x%x BUF2=0x%x",
1708 i, (unsigned int)(x->a),
1709 (unsigned int)((x->a) >> 32), x->b, x->c);
1710 seq_printf(seq, "\n");
1711 }
1712
1713 return 0;
1714}
1715
1716static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
1717{
1718 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
1719}
1720
1721static const struct file_operations stmmac_rings_status_fops = {
1722 .owner = THIS_MODULE,
1723 .open = stmmac_sysfs_ring_open,
1724 .read = seq_read,
1725 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00001726 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001727};
1728
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001729static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
1730{
1731 struct net_device *dev = seq->private;
1732 struct stmmac_priv *priv = netdev_priv(dev);
1733
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001734 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001735 seq_printf(seq, "DMA HW features not supported\n");
1736 return 0;
1737 }
1738
1739 seq_printf(seq, "==============================\n");
1740 seq_printf(seq, "\tDMA HW features\n");
1741 seq_printf(seq, "==============================\n");
1742
1743 seq_printf(seq, "\t10/100 Mbps %s\n",
1744 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
1745 seq_printf(seq, "\t1000 Mbps %s\n",
1746 (priv->dma_cap.mbps_1000) ? "Y" : "N");
1747 seq_printf(seq, "\tHalf duple %s\n",
1748 (priv->dma_cap.half_duplex) ? "Y" : "N");
1749 seq_printf(seq, "\tHash Filter: %s\n",
1750 (priv->dma_cap.hash_filter) ? "Y" : "N");
1751 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
1752 (priv->dma_cap.multi_addr) ? "Y" : "N");
1753 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
1754 (priv->dma_cap.pcs) ? "Y" : "N");
1755 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
1756 (priv->dma_cap.sma_mdio) ? "Y" : "N");
1757 seq_printf(seq, "\tPMT Remote wake up: %s\n",
1758 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
1759 seq_printf(seq, "\tPMT Magic Frame: %s\n",
1760 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
1761 seq_printf(seq, "\tRMON module: %s\n",
1762 (priv->dma_cap.rmon) ? "Y" : "N");
1763 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
1764 (priv->dma_cap.time_stamp) ? "Y" : "N");
1765 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
1766 (priv->dma_cap.atime_stamp) ? "Y" : "N");
1767 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
1768 (priv->dma_cap.eee) ? "Y" : "N");
1769 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
1770 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
1771 (priv->dma_cap.tx_coe) ? "Y" : "N");
1772 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
1773 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
1774 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
1775 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
1776 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
1777 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
1778 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
1779 priv->dma_cap.number_rx_channel);
1780 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
1781 priv->dma_cap.number_tx_channel);
1782 seq_printf(seq, "\tEnhanced descriptors: %s\n",
1783 (priv->dma_cap.enh_desc) ? "Y" : "N");
1784
1785 return 0;
1786}
1787
1788static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
1789{
1790 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
1791}
1792
1793static const struct file_operations stmmac_dma_cap_fops = {
1794 .owner = THIS_MODULE,
1795 .open = stmmac_sysfs_dma_cap_open,
1796 .read = seq_read,
1797 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00001798 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001799};
1800
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001801static int stmmac_init_fs(struct net_device *dev)
1802{
1803 /* Create debugfs entries */
1804 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
1805
1806 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
1807 pr_err("ERROR %s, debugfs create directory failed\n",
1808 STMMAC_RESOURCE_NAME);
1809
1810 return -ENOMEM;
1811 }
1812
1813 /* Entry to report DMA RX/TX rings */
1814 stmmac_rings_status = debugfs_create_file("descriptors_status",
1815 S_IRUGO, stmmac_fs_dir, dev,
1816 &stmmac_rings_status_fops);
1817
1818 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
1819 pr_info("ERROR creating stmmac ring debugfs file\n");
1820 debugfs_remove(stmmac_fs_dir);
1821
1822 return -ENOMEM;
1823 }
1824
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001825 /* Entry to report the DMA HW features */
1826 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
1827 dev, &stmmac_dma_cap_fops);
1828
1829 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
1830 pr_info("ERROR creating stmmac MMC debugfs file\n");
1831 debugfs_remove(stmmac_rings_status);
1832 debugfs_remove(stmmac_fs_dir);
1833
1834 return -ENOMEM;
1835 }
1836
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001837 return 0;
1838}
1839
1840static void stmmac_exit_fs(void)
1841{
1842 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001843 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001844 debugfs_remove(stmmac_fs_dir);
1845}
1846#endif /* CONFIG_STMMAC_DEBUG_FS */
1847
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001848static const struct net_device_ops stmmac_netdev_ops = {
1849 .ndo_open = stmmac_open,
1850 .ndo_start_xmit = stmmac_xmit,
1851 .ndo_stop = stmmac_release,
1852 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00001853 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00001854 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001855 .ndo_tx_timeout = stmmac_tx_timeout,
1856 .ndo_do_ioctl = stmmac_ioctl,
1857 .ndo_set_config = stmmac_config,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001858#ifdef CONFIG_NET_POLL_CONTROLLER
1859 .ndo_poll_controller = stmmac_poll_controller,
1860#endif
1861 .ndo_set_mac_address = eth_mac_addr,
1862};
1863
1864/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001865 * stmmac_hw_init - Init the MAC device
1866 * @priv : pointer to the private device structure.
1867 * Description: this function detects which MAC device
1868 * (GMAC/MAC10-100) has to attached, checks the HW capability
1869 * (if supported) and sets the driver's features (for example
1870 * to use the ring or chaine mode or support the normal/enh
1871 * descriptor structure).
1872 */
1873static int stmmac_hw_init(struct stmmac_priv *priv)
1874{
1875 int ret = 0;
1876 struct mac_device_info *mac;
1877
1878 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00001879 if (priv->plat->has_gmac) {
1880 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001881 mac = dwmac1000_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00001882 } else {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001883 mac = dwmac100_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00001884 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001885 if (!mac)
1886 return -ENOMEM;
1887
1888 priv->hw = mac;
1889
1890 /* To use the chained or ring mode */
1891 priv->hw->ring = &ring_mode_ops;
1892
1893 /* Get and dump the chip ID */
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00001894 priv->synopsys_id = stmmac_get_synopsys_id(priv);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001895
1896 /* Get the HW capability (new GMAC newer than 3.50a) */
1897 priv->hw_cap_support = stmmac_get_hw_features(priv);
1898 if (priv->hw_cap_support) {
1899 pr_info(" DMA HW capability register supported");
1900
1901 /* We can override some gmac/dma configuration fields: e.g.
1902 * enh_desc, tx_coe (e.g. that are passed through the
1903 * platform) with the values from the HW capability
1904 * register (if supported).
1905 */
1906 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001907 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001908
1909 priv->plat->tx_coe = priv->dma_cap.tx_coe;
1910
1911 if (priv->dma_cap.rx_coe_type2)
1912 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
1913 else if (priv->dma_cap.rx_coe_type1)
1914 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
1915
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001916 } else
1917 pr_info(" No HW DMA feature register supported");
1918
1919 /* Select the enhnaced/normal descriptor structures */
1920 stmmac_selec_desc_mode(priv);
1921
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001922 /* Enable the IPC (Checksum Offload) and check if the feature has been
1923 * enabled during the core configuration. */
1924 ret = priv->hw->mac->rx_ipc(priv->ioaddr);
1925 if (!ret) {
1926 pr_warning(" RX IPC Checksum Offload not configured.\n");
1927 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
1928 }
1929
1930 if (priv->plat->rx_coe)
1931 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
1932 priv->plat->rx_coe);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001933 if (priv->plat->tx_coe)
1934 pr_info(" TX Checksum insertion supported\n");
1935
1936 if (priv->plat->pmt) {
1937 pr_info(" Wake-Up On Lan supported\n");
1938 device_set_wakeup_capable(priv->device, 1);
1939 }
1940
1941 return ret;
1942}
1943
1944/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001945 * stmmac_dvr_probe
1946 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001947 * @plat_dat: platform data pointer
1948 * @addr: iobase memory address
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001949 * Description: this is the main probe function used to
1950 * call the alloc_etherdev, allocate the priv structure.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001951 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001952struct stmmac_priv *stmmac_dvr_probe(struct device *device,
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001953 struct plat_stmmacenet_data *plat_dat,
1954 void __iomem *addr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001955{
1956 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001957 struct net_device *ndev = NULL;
1958 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001959
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001960 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00001961 if (!ndev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001962 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001963
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001964 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001965
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001966 priv = netdev_priv(ndev);
1967 priv->device = device;
1968 priv->dev = ndev;
1969
1970 ether_setup(ndev);
1971
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001972 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001973 priv->pause = pause;
1974 priv->plat = plat_dat;
1975 priv->ioaddr = addr;
1976 priv->dev->base_addr = (unsigned long)addr;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001977
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001978 /* Verify driver arguments */
1979 stmmac_verify_args();
1980
1981 /* Override with kernel parameters if supplied XXX CRS XXX
1982 * this needs to have multiple instances */
1983 if ((phyaddr >= 0) && (phyaddr <= 31))
1984 priv->plat->phy_addr = phyaddr;
1985
1986 /* Init MAC and get the capabilities */
1987 stmmac_hw_init(priv);
1988
1989 ndev->netdev_ops = &stmmac_netdev_ops;
1990
1991 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
1992 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001993 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
1994 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001995#ifdef STMMAC_VLAN_TAG_USED
1996 /* Both mac100 and gmac support receive VLAN tag detection */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001997 ndev->features |= NETIF_F_HW_VLAN_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001998#endif
1999 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2000
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002001 if (flow_ctrl)
2002 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2003
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002004 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002005
Vlad Lunguf8e96162010-11-29 22:52:52 +00002006 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002007 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002008
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002009 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002010 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002011 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002012 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002013 }
2014
Kelvin Cheungae4d8cf2012-08-18 00:16:23 +00002015 priv->stmmac_clk = clk_get(priv->device, STMMAC_RESOURCE_NAME);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002016 if (IS_ERR(priv->stmmac_clk)) {
Giuseppe CAVALLARO31ea38e2012-04-18 19:48:22 +00002017 pr_warning("%s: warning: cannot get CSR clock\n", __func__);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002018 goto error_clk_get;
2019 }
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002020
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002021 /* If a specific clk_csr value is passed from the platform
2022 * this means that the CSR Clock Range selection cannot be
2023 * changed at run-time and it is fixed. Viceversa the driver'll try to
2024 * set the MDC clock dynamically according to the csr actual
2025 * clock input.
2026 */
2027 if (!priv->plat->clk_csr)
2028 stmmac_clk_csr_set(priv);
2029 else
2030 priv->clk_csr = priv->plat->clk_csr;
2031
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002032 /* MDIO bus Registration */
2033 ret = stmmac_mdio_register(ndev);
2034 if (ret < 0) {
2035 pr_debug("%s: MDIO bus (id: %d) registration failed",
2036 __func__, priv->plat->bus_id);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002037 goto error_mdio_register;
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002038 }
2039
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002040 return priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002041
Viresh Kumar6a81c262012-07-30 14:39:41 -07002042error_mdio_register:
2043 clk_put(priv->stmmac_clk);
2044error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002045 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002046error_netdev_register:
2047 netif_napi_del(&priv->napi);
Dan Carpenter34a52f32010-12-20 21:34:56 +00002048 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002049
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002050 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002051}
2052
2053/**
2054 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002055 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002056 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002057 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002058 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002059int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002060{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002061 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002062
2063 pr_info("%s:\n\tremoving driver", __func__);
2064
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002065 priv->hw->dma->stop_rx(priv->ioaddr);
2066 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002067
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002068 stmmac_set_mac(priv->ioaddr, false);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002069 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002070 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002071 unregister_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002072 free_netdev(ndev);
2073
2074 return 0;
2075}
2076
2077#ifdef CONFIG_PM
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002078int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002079{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002080 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002081 int dis_ic = 0;
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002082 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002083
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002084 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002085 return 0;
2086
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002087 if (priv->phydev)
2088 phy_stop(priv->phydev);
2089
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002090 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002091
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002092 netif_device_detach(ndev);
2093 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002094
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002095 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002096
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002097 /* Stop TX/RX DMA */
2098 priv->hw->dma->stop_tx(priv->ioaddr);
2099 priv->hw->dma->stop_rx(priv->ioaddr);
2100 /* Clear the Rx/Tx descriptors */
2101 priv->hw->desc->init_rx_desc(priv->dma_rx, priv->dma_rx_size,
2102 dis_ic);
2103 priv->hw->desc->init_tx_desc(priv->dma_tx, priv->dma_tx_size);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002104
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002105 /* Enable Power down mode by programming the PMT regs */
2106 if (device_may_wakeup(priv->device))
2107 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002108 else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002109 stmmac_set_mac(priv->ioaddr, false);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002110 /* Disable clock in case of PWM is off */
Stefan Roesea6308442012-09-21 01:06:29 +00002111 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002112 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002113 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002114 return 0;
2115}
2116
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002117int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002118{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002119 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002120 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002121
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002122 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002123 return 0;
2124
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002125 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02002126
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002127 /* Power Down bit, into the PM register, is cleared
2128 * automatically as soon as a magic packet or a Wake-up frame
2129 * is received. Anyway, it's better to manually clear
2130 * this bit because it can generate problems while resuming
2131 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002132 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07002133 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002134 else
2135 /* enable the clk prevously disabled */
Stefan Roesea6308442012-09-21 01:06:29 +00002136 clk_prepare_enable(priv->stmmac_clk);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002137
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002138 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002139
2140 /* Enable the MAC and DMA */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002141 stmmac_set_mac(priv->ioaddr, true);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002142 priv->hw->dma->start_tx(priv->ioaddr);
2143 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002144
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002145 napi_enable(&priv->napi);
2146
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002147 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002148
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002149 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002150
2151 if (priv->phydev)
2152 phy_start(priv->phydev);
2153
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002154 return 0;
2155}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002156
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002157int stmmac_freeze(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002158{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002159 if (!ndev || !netif_running(ndev))
2160 return 0;
2161
2162 return stmmac_release(ndev);
2163}
2164
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002165int stmmac_restore(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002166{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002167 if (!ndev || !netif_running(ndev))
2168 return 0;
2169
2170 return stmmac_open(ndev);
2171}
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002172#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002173
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002174/* Driver can be configured w/ and w/ both PCI and Platf drivers
2175 * depending on the configuration selected.
2176 */
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002177static int __init stmmac_init(void)
2178{
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002179 int err_plt = 0;
2180 int err_pci = 0;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002181
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002182 err_plt = stmmac_register_platform();
2183 err_pci = stmmac_register_pci();
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002184
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002185 if ((err_pci) && (err_plt)) {
2186 pr_err("stmmac: driver registration failed\n");
2187 return -EINVAL;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002188 }
2189
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002190 return 0;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002191}
2192
2193static void __exit stmmac_exit(void)
2194{
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002195 stmmac_unregister_platform();
2196 stmmac_unregister_pci();
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002197}
2198
2199module_init(stmmac_init);
2200module_exit(stmmac_exit);
2201
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002202#ifndef MODULE
2203static int __init stmmac_cmdline_opt(char *str)
2204{
2205 char *opt;
2206
2207 if (!str || !*str)
2208 return -EINVAL;
2209 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002210 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002211 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002212 goto err;
2213 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002214 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002215 goto err;
2216 } else if (!strncmp(opt, "dma_txsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002217 if (kstrtoint(opt + 11, 0, &dma_txsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002218 goto err;
2219 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002220 if (kstrtoint(opt + 11, 0, &dma_rxsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002221 goto err;
2222 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002223 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002224 goto err;
2225 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002226 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002227 goto err;
2228 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002229 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002230 goto err;
2231 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002232 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002233 goto err;
2234 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002235 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002236 goto err;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002237 } else if (!strncmp(opt, "eee_timer:", 6)) {
2238 if (kstrtoint(opt + 10, 0, &eee_timer))
2239 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002240 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002241 }
2242 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002243
2244err:
2245 pr_err("%s: ERROR broken module parameter conversion", __func__);
2246 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002247}
2248
2249__setup("stmmaceth=", stmmac_cmdline_opt);
2250#endif
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05002251
2252MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
2253MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2254MODULE_LICENSE("GPL");