blob: ac166be325ede6e1f3946aeebe19d4d8d5b7dcc8 [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*/
Giuseppe CAVALLAROde53d552013-02-06 20:47:51 +000072#ifdef STMMAC_XMIT_DEBUG
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070073#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 CAVALLARO4a7d6662013-03-26 04:43:05 +0000133/* By default the driver will use the ring mode to manage tx and rx descriptors
134 * but passing this value so user can force to use the chain instead of the ring
135 */
136static unsigned int chain_mode;
137module_param(chain_mode, int, S_IRUGO);
138MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
139
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700140static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700141
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000142#ifdef CONFIG_STMMAC_DEBUG_FS
143static int stmmac_init_fs(struct net_device *dev);
144static void stmmac_exit_fs(void);
145#endif
146
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000147#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
148
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700149/**
150 * stmmac_verify_args - verify the driver parameters.
151 * Description: it verifies if some wrong parameter is passed to the driver.
152 * Note that wrong parameters are replaced with the default values.
153 */
154static void stmmac_verify_args(void)
155{
156 if (unlikely(watchdog < 0))
157 watchdog = TX_TIMEO;
158 if (unlikely(dma_rxsize < 0))
159 dma_rxsize = DMA_RX_SIZE;
160 if (unlikely(dma_txsize < 0))
161 dma_txsize = DMA_TX_SIZE;
162 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
163 buf_sz = DMA_BUFFER_SIZE;
164 if (unlikely(flow_ctrl > 1))
165 flow_ctrl = FLOW_AUTO;
166 else if (likely(flow_ctrl < 0))
167 flow_ctrl = FLOW_OFF;
168 if (unlikely((pause < 0) || (pause > 0xffff)))
169 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000170 if (eee_timer < 0)
171 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700172}
173
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000174static void stmmac_clk_csr_set(struct stmmac_priv *priv)
175{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000176 u32 clk_rate;
177
178 clk_rate = clk_get_rate(priv->stmmac_clk);
179
180 /* Platform provided default clk_csr would be assumed valid
181 * for all other cases except for the below mentioned ones. */
182 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
183 if (clk_rate < CSR_F_35M)
184 priv->clk_csr = STMMAC_CSR_20_35M;
185 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
186 priv->clk_csr = STMMAC_CSR_35_60M;
187 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
188 priv->clk_csr = STMMAC_CSR_60_100M;
189 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
190 priv->clk_csr = STMMAC_CSR_100_150M;
191 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
192 priv->clk_csr = STMMAC_CSR_150_250M;
193 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
194 priv->clk_csr = STMMAC_CSR_250_300M;
195 } /* For values higher than the IEEE 802.3 specified frequency
196 * we can not estimate the proper divider as it is not known
197 * the frequency of clk_csr_i. So we do not change the default
198 * divider. */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000199}
200
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700201#if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
202static void print_pkt(unsigned char *buf, int len)
203{
204 int j;
205 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
206 for (j = 0; j < len; j++) {
207 if ((j % 16) == 0)
208 pr_info("\n %03x:", j);
209 pr_info(" %02x", buf[j]);
210 }
211 pr_info("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700212}
213#endif
214
215/* minimum number of free TX descriptors required to wake up TX process */
216#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
217
218static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
219{
220 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
221}
222
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000223/* On some ST platforms, some HW system configuraton registers have to be
224 * set according to the link speed negotiated.
225 */
226static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
227{
228 struct phy_device *phydev = priv->phydev;
229
230 if (likely(priv->plat->fix_mac_speed))
231 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
232 phydev->speed);
233}
234
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000235static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
236{
237 /* Check and enter in LPI mode */
238 if ((priv->dirty_tx == priv->cur_tx) &&
239 (priv->tx_path_in_lpi_mode == false))
240 priv->hw->mac->set_eee_mode(priv->ioaddr);
241}
242
243void stmmac_disable_eee_mode(struct stmmac_priv *priv)
244{
245 /* Exit and disable EEE in case of we are are in LPI state. */
246 priv->hw->mac->reset_eee_mode(priv->ioaddr);
247 del_timer_sync(&priv->eee_ctrl_timer);
248 priv->tx_path_in_lpi_mode = false;
249}
250
251/**
252 * stmmac_eee_ctrl_timer
253 * @arg : data hook
254 * Description:
255 * If there is no data transfer and if we are not in LPI state,
256 * then MAC Transmitter can be moved to LPI state.
257 */
258static void stmmac_eee_ctrl_timer(unsigned long arg)
259{
260 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
261
262 stmmac_enable_eee_mode(priv);
263 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
264}
265
266/**
267 * stmmac_eee_init
268 * @priv: private device pointer
269 * Description:
270 * If the EEE support has been enabled while configuring the driver,
271 * if the GMAC actually supports the EEE (from the HW cap reg) and the
272 * phy can also manage EEE, so enable the LPI state and start the timer
273 * to verify if the tx path can enter in LPI state.
274 */
275bool stmmac_eee_init(struct stmmac_priv *priv)
276{
277 bool ret = false;
278
279 /* MAC core supports the EEE feature. */
280 if (priv->dma_cap.eee) {
281 /* Check if the PHY supports EEE */
282 if (phy_init_eee(priv->phydev, 1))
283 goto out;
284
285 priv->eee_active = 1;
286 init_timer(&priv->eee_ctrl_timer);
287 priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
288 priv->eee_ctrl_timer.data = (unsigned long)priv;
289 priv->eee_ctrl_timer.expires = STMMAC_LPI_TIMER(eee_timer);
290 add_timer(&priv->eee_ctrl_timer);
291
292 priv->hw->mac->set_eee_timer(priv->ioaddr,
293 STMMAC_DEFAULT_LIT_LS_TIMER,
294 priv->tx_lpi_timer);
295
296 pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
297
298 ret = true;
299 }
300out:
301 return ret;
302}
303
304static void stmmac_eee_adjust(struct stmmac_priv *priv)
305{
306 /* When the EEE has been already initialised we have to
307 * modify the PLS bit in the LPI ctrl & status reg according
308 * to the PHY link status. For this reason.
309 */
310 if (priv->eee_enabled)
311 priv->hw->mac->set_eee_pls(priv->ioaddr, priv->phydev->link);
312}
313
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700314/**
315 * stmmac_adjust_link
316 * @dev: net device structure
317 * Description: it adjusts the link parameters.
318 */
319static void stmmac_adjust_link(struct net_device *dev)
320{
321 struct stmmac_priv *priv = netdev_priv(dev);
322 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700323 unsigned long flags;
324 int new_state = 0;
325 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
326
327 if (phydev == NULL)
328 return;
329
330 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
331 phydev->addr, phydev->link);
332
333 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000334
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700335 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000336 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700337
338 /* Now we make sure that we can be in full duplex mode.
339 * If not, we operate in half-duplex mode. */
340 if (phydev->duplex != priv->oldduplex) {
341 new_state = 1;
342 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000343 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700344 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000345 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700346 priv->oldduplex = phydev->duplex;
347 }
348 /* Flow Control operation */
349 if (phydev->pause)
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000350 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000351 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700352
353 if (phydev->speed != priv->speed) {
354 new_state = 1;
355 switch (phydev->speed) {
356 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000357 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000358 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000359 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700360 break;
361 case 100:
362 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000363 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000364 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700365 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000366 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700367 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000368 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700369 }
370 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000371 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700372 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000373 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700374 break;
375 default:
376 if (netif_msg_link(priv))
377 pr_warning("%s: Speed (%d) is not 10"
378 " or 100!\n", dev->name, phydev->speed);
379 break;
380 }
381
382 priv->speed = phydev->speed;
383 }
384
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000385 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700386
387 if (!priv->oldlink) {
388 new_state = 1;
389 priv->oldlink = 1;
390 }
391 } else if (priv->oldlink) {
392 new_state = 1;
393 priv->oldlink = 0;
394 priv->speed = 0;
395 priv->oldduplex = -1;
396 }
397
398 if (new_state && netif_msg_link(priv))
399 phy_print_status(phydev);
400
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000401 stmmac_eee_adjust(priv);
402
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700403 spin_unlock_irqrestore(&priv->lock, flags);
404
405 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
406}
407
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000408static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
409{
410 int interface = priv->plat->interface;
411
412 if (priv->dma_cap.pcs) {
413 if ((interface & PHY_INTERFACE_MODE_RGMII) ||
414 (interface & PHY_INTERFACE_MODE_RGMII_ID) ||
415 (interface & PHY_INTERFACE_MODE_RGMII_RXID) ||
416 (interface & PHY_INTERFACE_MODE_RGMII_TXID)) {
417 pr_debug("STMMAC: PCS RGMII support enable\n");
418 priv->pcs = STMMAC_PCS_RGMII;
419 } else if (interface & PHY_INTERFACE_MODE_SGMII) {
420 pr_debug("STMMAC: PCS SGMII support enable\n");
421 priv->pcs = STMMAC_PCS_SGMII;
422 }
423 }
424}
425
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700426/**
427 * stmmac_init_phy - PHY initialization
428 * @dev: net device structure
429 * Description: it initializes the driver's PHY state, and attaches the PHY
430 * to the mac driver.
431 * Return value:
432 * 0 on success
433 */
434static int stmmac_init_phy(struct net_device *dev)
435{
436 struct stmmac_priv *priv = netdev_priv(dev);
437 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000438 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000439 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000440 int interface = priv->plat->interface;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700441 priv->oldlink = 0;
442 priv->speed = 0;
443 priv->oldduplex = -1;
444
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000445 if (priv->plat->phy_bus_name)
446 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
447 priv->plat->phy_bus_name, priv->plat->bus_id);
448 else
449 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
450 priv->plat->bus_id);
451
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000452 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000453 priv->plat->phy_addr);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000454 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700455
Florian Fainellif9a8f832013-01-14 00:52:52 +0000456 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700457
458 if (IS_ERR(phydev)) {
459 pr_err("%s: Could not attach to PHY\n", dev->name);
460 return PTR_ERR(phydev);
461 }
462
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000463 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000464 if ((interface == PHY_INTERFACE_MODE_MII) ||
465 (interface == PHY_INTERFACE_MODE_RMII))
466 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
467 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000468
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700469 /*
470 * Broken HW is sometimes missing the pull-up resistor on the
471 * MDIO line, which results in reads to non-existent devices returning
472 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
473 * device as well.
474 * Note: phydev->phy_id is the result of reading the UID PHY registers.
475 */
476 if (phydev->phy_id == 0) {
477 phy_disconnect(phydev);
478 return -ENODEV;
479 }
480 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000481 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700482
483 priv->phydev = phydev;
484
485 return 0;
486}
487
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700488/**
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000489 * stmmac_display_ring
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700490 * @p: pointer to the ring.
491 * @size: size of the ring.
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000492 * Description: display the control/status and buffer descriptors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700493 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000494static void stmmac_display_ring(void *head, int size, int extend_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700495{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700496 int i;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000497 struct dma_extended_desc *ep = (struct dma_extended_desc *) head;
498 struct dma_desc *p = (struct dma_desc *) head;
499
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700500 for (i = 0; i < size; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000501 u64 x;
502 if (extend_desc) {
503 x = *(u64 *) ep;
504 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
505 i, (unsigned int) virt_to_phys(ep),
506 (unsigned int) x, (unsigned int) (x >> 32),
507 ep->basic.des2, ep->basic.des3);
508 ep++;
509 } else {
510 x = *(u64 *) p;
511 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
512 i, (unsigned int) virt_to_phys(p),
513 (unsigned int) x, (unsigned int) (x >> 32),
514 p->des2, p->des3);
515 p++;
516 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700517 pr_info("\n");
518 }
519}
520
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000521static void stmmac_display_rings(struct stmmac_priv *priv)
522{
523 unsigned int txsize = priv->dma_tx_size;
524 unsigned int rxsize = priv->dma_rx_size;
525
526 if (priv->extend_desc) {
527 pr_info("Extended RX descriptor ring:\n");
528 stmmac_display_ring((void *) priv->dma_erx, rxsize, 1);
529 pr_info("Extended TX descriptor ring:\n");
530 stmmac_display_ring((void *) priv->dma_etx, txsize, 1);
531 } else {
532 pr_info("RX descriptor ring:\n");
533 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
534 pr_info("TX descriptor ring:\n");
535 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
536 }
537}
538
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000539static int stmmac_set_bfsize(int mtu, int bufsize)
540{
541 int ret = bufsize;
542
543 if (mtu >= BUF_SIZE_4KiB)
544 ret = BUF_SIZE_8KiB;
545 else if (mtu >= BUF_SIZE_2KiB)
546 ret = BUF_SIZE_4KiB;
547 else if (mtu >= DMA_BUFFER_SIZE)
548 ret = BUF_SIZE_2KiB;
549 else
550 ret = DMA_BUFFER_SIZE;
551
552 return ret;
553}
554
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000555static void stmmac_clear_descriptors(struct stmmac_priv *priv)
556{
557 int i;
558 unsigned int txsize = priv->dma_tx_size;
559 unsigned int rxsize = priv->dma_rx_size;
560
561 /* Clear the Rx/Tx descriptors */
562 for (i = 0; i < rxsize; i++)
563 if (priv->extend_desc)
564 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
565 priv->use_riwt, priv->mode,
566 (i == rxsize - 1));
567 else
568 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
569 priv->use_riwt, priv->mode,
570 (i == rxsize - 1));
571 for (i = 0; i < txsize; i++)
572 if (priv->extend_desc)
573 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
574 priv->mode,
575 (i == txsize - 1));
576 else
577 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
578 priv->mode,
579 (i == txsize - 1));
580}
581
582static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
583 int i)
584{
585 struct sk_buff *skb;
586
587 skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
588 GFP_KERNEL);
589 if (unlikely(skb == NULL)) {
590 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
591 return 1;
592 }
593 skb_reserve(skb, NET_IP_ALIGN);
594 priv->rx_skbuff[i] = skb;
595 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
596 priv->dma_buf_sz,
597 DMA_FROM_DEVICE);
598
599 p->des2 = priv->rx_skbuff_dma[i];
600
601 if ((priv->mode == STMMAC_RING_MODE) &&
602 (priv->dma_buf_sz == BUF_SIZE_16KiB))
603 priv->hw->ring->init_desc3(p);
604
605 return 0;
606}
607
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700608/**
609 * init_dma_desc_rings - init the RX/TX descriptor rings
610 * @dev: net device structure
611 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000612 * and allocates the socket buffers. It suppors the chained and ring
613 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700614 */
615static void init_dma_desc_rings(struct net_device *dev)
616{
617 int i;
618 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700619 unsigned int txsize = priv->dma_tx_size;
620 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000621 unsigned int bfsize = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700622
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000623 /* Set the max buffer size according to the DESC mode
624 * and the MTU. Note that RING mode allows 16KiB bsize. */
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000625 if (priv->mode == STMMAC_RING_MODE)
626 bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000627
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000628 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000629 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700630
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700631 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
632 txsize, rxsize, bfsize);
633
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000634 if (priv->extend_desc) {
635 priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
636 sizeof(struct
637 dma_extended_desc),
638 &priv->dma_rx_phy,
639 GFP_KERNEL);
640 priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
641 sizeof(struct
642 dma_extended_desc),
643 &priv->dma_tx_phy,
644 GFP_KERNEL);
645 if ((!priv->dma_erx) || (!priv->dma_etx))
646 return;
647 } else {
648 priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
649 sizeof(struct dma_desc),
650 &priv->dma_rx_phy,
651 GFP_KERNEL);
652 priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
653 sizeof(struct dma_desc),
654 &priv->dma_tx_phy,
655 GFP_KERNEL);
656 if ((!priv->dma_rx) || (!priv->dma_tx))
657 return;
658 }
659
Joe Perchesb2adaca2013-02-03 17:43:58 +0000660 priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
661 GFP_KERNEL);
662 priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
663 GFP_KERNEL);
Joe Perchesb2adaca2013-02-03 17:43:58 +0000664 priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
665 GFP_KERNEL);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000666 if (netif_msg_drv(priv))
667 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
668 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700669
670 /* RX INITIALIZATION */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000671 DBG(probe, INFO, "stmmac: SKB addresses:\nskb\t\tskb data\tdma data\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700672 for (i = 0; i < rxsize; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000673 struct dma_desc *p;
674 if (priv->extend_desc)
675 p = &((priv->dma_erx + i)->basic);
676 else
677 p = priv->dma_rx + i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700678
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000679 if (stmmac_init_rx_buffers(priv, p, i))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700680 break;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000681
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700682 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
683 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
684 }
685 priv->cur_rx = 0;
686 priv->dirty_rx = (unsigned int)(i - rxsize);
687 priv->dma_buf_sz = bfsize;
688 buf_sz = bfsize;
689
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000690 /* Setup the chained descriptor addresses */
691 if (priv->mode == STMMAC_CHAIN_MODE) {
692 if (priv->extend_desc) {
693 priv->hw->chain->init(priv->dma_erx, priv->dma_rx_phy,
694 rxsize, 1);
695 priv->hw->chain->init(priv->dma_etx, priv->dma_tx_phy,
696 txsize, 1);
697 } else {
698 priv->hw->chain->init(priv->dma_rx, priv->dma_rx_phy,
699 rxsize, 0);
700 priv->hw->chain->init(priv->dma_tx, priv->dma_tx_phy,
701 txsize, 0);
702 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700703 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000704
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000705 /* TX INITIALIZATION */
706 for (i = 0; i < txsize; i++) {
707 struct dma_desc *p;
708 if (priv->extend_desc)
709 p = &((priv->dma_etx + i)->basic);
710 else
711 p = priv->dma_tx + i;
712 p->des2 = 0;
713 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000714 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000715
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700716 priv->dirty_tx = 0;
717 priv->cur_tx = 0;
718
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000719 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700720
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000721 if (netif_msg_hw(priv))
722 stmmac_display_rings(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700723}
724
725static void dma_free_rx_skbufs(struct stmmac_priv *priv)
726{
727 int i;
728
729 for (i = 0; i < priv->dma_rx_size; i++) {
730 if (priv->rx_skbuff[i]) {
731 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
732 priv->dma_buf_sz, DMA_FROM_DEVICE);
733 dev_kfree_skb_any(priv->rx_skbuff[i]);
734 }
735 priv->rx_skbuff[i] = NULL;
736 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700737}
738
739static void dma_free_tx_skbufs(struct stmmac_priv *priv)
740{
741 int i;
742
743 for (i = 0; i < priv->dma_tx_size; i++) {
744 if (priv->tx_skbuff[i] != NULL) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000745 struct dma_desc *p;
746 if (priv->extend_desc)
747 p = &((priv->dma_etx + i)->basic);
748 else
749 p = priv->dma_tx + i;
750
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700751 if (p->des2)
752 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000753 priv->hw->desc->get_tx_len(p),
754 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700755 dev_kfree_skb_any(priv->tx_skbuff[i]);
756 priv->tx_skbuff[i] = NULL;
757 }
758 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700759}
760
761static void free_dma_desc_resources(struct stmmac_priv *priv)
762{
763 /* Release the DMA TX/RX socket buffers */
764 dma_free_rx_skbufs(priv);
765 dma_free_tx_skbufs(priv);
766
767 /* Free the region of consistent memory previously allocated for
768 * the DMA */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000769 if (!priv->extend_desc) {
770 dma_free_coherent(priv->device,
771 priv->dma_tx_size * sizeof(struct dma_desc),
772 priv->dma_tx, priv->dma_tx_phy);
773 dma_free_coherent(priv->device,
774 priv->dma_rx_size * sizeof(struct dma_desc),
775 priv->dma_rx, priv->dma_rx_phy);
776 } else {
777 dma_free_coherent(priv->device, priv->dma_tx_size *
778 sizeof(struct dma_extended_desc),
779 priv->dma_etx, priv->dma_tx_phy);
780 dma_free_coherent(priv->device, priv->dma_rx_size *
781 sizeof(struct dma_extended_desc),
782 priv->dma_erx, priv->dma_rx_phy);
783 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700784 kfree(priv->rx_skbuff_dma);
785 kfree(priv->rx_skbuff);
786 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700787}
788
789/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700790 * stmmac_dma_operation_mode - HW DMA operation mode
791 * @priv : pointer to the private device structure.
792 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000793 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700794 */
795static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
796{
Srinivas Kandagatla61b80132011-07-17 20:54:09 +0000797 if (likely(priv->plat->force_sf_dma_mode ||
798 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
799 /*
800 * In case of GMAC, SF mode can be enabled
801 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +0000802 * 1) TX COE if actually supported
803 * 2) There is no bugged Jumbo frame support
804 * that needs to not insert csum in the TDES.
805 */
806 priv->hw->dma->dma_mode(priv->ioaddr,
807 SF_DMA_MODE, SF_DMA_MODE);
808 tc = SF_DMA_MODE;
809 } else
810 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700811}
812
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700813/**
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000814 * stmmac_tx_clean:
815 * @priv: private data pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700816 * Description: it reclaims resources after transmission completes.
817 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000818static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700819{
820 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700821
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000822 spin_lock(&priv->tx_lock);
823
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000824 priv->xstats.tx_clean++;
825
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700826 while (priv->dirty_tx != priv->cur_tx) {
827 int last;
828 unsigned int entry = priv->dirty_tx % txsize;
829 struct sk_buff *skb = priv->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000830 struct dma_desc *p;
831
832 if (priv->extend_desc)
833 p = (struct dma_desc *) (priv->dma_etx + entry);
834 else
835 p = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700836
837 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000838 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700839 break;
840
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000841 /* Verify tx error by looking at the last segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000842 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700843 if (likely(last)) {
844 int tx_error =
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000845 priv->hw->desc->tx_status(&priv->dev->stats,
846 &priv->xstats, p,
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000847 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700848 if (likely(tx_error == 0)) {
849 priv->dev->stats.tx_packets++;
850 priv->xstats.tx_pkt_n++;
851 } else
852 priv->dev->stats.tx_errors++;
853 }
854 TX_DBG("%s: curr %d, dirty %d\n", __func__,
855 priv->cur_tx, priv->dirty_tx);
856
857 if (likely(p->des2))
858 dma_unmap_single(priv->device, p->des2,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000859 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700860 DMA_TO_DEVICE);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000861 if (priv->mode == STMMAC_RING_MODE)
862 priv->hw->ring->clean_desc3(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700863
864 if (likely(skb != NULL)) {
Eric Dumazetacb600d2012-10-05 06:23:55 +0000865 dev_kfree_skb(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700866 priv->tx_skbuff[entry] = NULL;
867 }
868
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000869 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700870
Giuseppe CAVALLARO13497f52012-06-04 06:36:22 +0000871 priv->dirty_tx++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700872 }
873 if (unlikely(netif_queue_stopped(priv->dev) &&
874 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
875 netif_tx_lock(priv->dev);
876 if (netif_queue_stopped(priv->dev) &&
877 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
878 TX_DBG("%s: restart transmit\n", __func__);
879 netif_wake_queue(priv->dev);
880 }
881 netif_tx_unlock(priv->dev);
882 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000883
884 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
885 stmmac_enable_eee_mode(priv);
886 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
887 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +0000888 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700889}
890
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000891static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700892{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +0000893 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700894}
895
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000896static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700897{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +0000898 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700899}
900
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700901
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700902/**
903 * stmmac_tx_err:
904 * @priv: pointer to the private device structure
905 * Description: it cleans the descriptors and restarts the transmission
906 * in case of errors.
907 */
908static void stmmac_tx_err(struct stmmac_priv *priv)
909{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000910 int i;
911 int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700912 netif_stop_queue(priv->dev);
913
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000914 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700915 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000916 for (i = 0; i < txsize; i++)
917 if (priv->extend_desc)
918 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
919 priv->mode,
920 (i == txsize - 1));
921 else
922 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
923 priv->mode,
924 (i == txsize - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700925 priv->dirty_tx = 0;
926 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000927 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700928
929 priv->dev->stats.tx_errors++;
930 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700931}
932
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000933static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700934{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000935 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700936
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000937 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000938 if (likely((status & handle_rx)) || (status & handle_tx)) {
939 if (likely(napi_schedule_prep(&priv->napi))) {
940 stmmac_disable_dma_irq(priv);
941 __napi_schedule(&priv->napi);
942 }
943 }
944 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000945 /* Try to bump up the dma threshold on this failure */
946 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
947 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000948 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000949 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700950 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +0000951 } else if (unlikely(status == tx_hard_error))
952 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700953}
954
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000955static void stmmac_mmc_setup(struct stmmac_priv *priv)
956{
957 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
958 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
959
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000960 /* Mask MMC irq, counters are managed in SW and registers
961 * are cleared on each READ eventually. */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000962 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +0000963
964 if (priv->dma_cap.rmon) {
965 dwmac_mmc_ctrl(priv->ioaddr, mode);
966 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
967 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +0000968 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +0000969}
970
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000971static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
972{
973 u32 hwid = priv->hw->synopsys_uid;
974
975 /* Only check valid Synopsys Id because old MAC chips
976 * have no HW registers where get the ID */
977 if (likely(hwid)) {
978 u32 uid = ((hwid & 0x0000ff00) >> 8);
979 u32 synid = (hwid & 0x000000ff);
980
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +0000981 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +0000982 uid, synid);
983
984 return synid;
985 }
986 return 0;
987}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +0000988
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000989/**
990 * stmmac_selec_desc_mode
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +0000991 * @priv : private structure
992 * Description: select the Enhanced/Alternate or Normal descriptors
993 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +0000994static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
995{
996 if (priv->plat->enh_desc) {
997 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000998
999 /* GMAC older than 3.50 has no extended descriptors */
1000 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1001 pr_info("\tEnabled extended descriptors\n");
1002 priv->extend_desc = 1;
1003 } else
1004 pr_warn("Extended descriptors not supported\n");
1005
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001006 priv->hw->desc = &enh_desc_ops;
1007 } else {
1008 pr_info(" Normal descriptors\n");
1009 priv->hw->desc = &ndesc_ops;
1010 }
1011}
1012
1013/**
1014 * stmmac_get_hw_features
1015 * @priv : private device pointer
1016 * Description:
1017 * new GMAC chip generations have a new register to indicate the
1018 * presence of the optional feature/functions.
1019 * This can be also used to override the value passed through the
1020 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001021 */
1022static int stmmac_get_hw_features(struct stmmac_priv *priv)
1023{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001024 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001025
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001026 if (priv->hw->dma->get_hw_feature) {
1027 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001028
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001029 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
1030 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
1031 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
1032 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
1033 priv->dma_cap.multi_addr =
1034 (hw_cap & DMA_HW_FEAT_ADDMACADRSEL) >> 5;
1035 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
1036 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
1037 priv->dma_cap.pmt_remote_wake_up =
1038 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
1039 priv->dma_cap.pmt_magic_frame =
1040 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001041 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001042 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001043 /* IEEE 1588-2002*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001044 priv->dma_cap.time_stamp =
1045 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001046 /* IEEE 1588-2008*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001047 priv->dma_cap.atime_stamp =
1048 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001049 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001050 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
1051 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001052 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001053 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
1054 priv->dma_cap.rx_coe_type1 =
1055 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
1056 priv->dma_cap.rx_coe_type2 =
1057 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
1058 priv->dma_cap.rxfifo_over_2048 =
1059 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001060 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001061 priv->dma_cap.number_rx_channel =
1062 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
1063 priv->dma_cap.number_tx_channel =
1064 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001065 /* Alternate (enhanced) DESC mode*/
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001066 priv->dma_cap.enh_desc =
1067 (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001068 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001069
1070 return hw_cap;
1071}
1072
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001073static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1074{
1075 /* verify if the MAC address is valid, in case of failures it
1076 * generates a random MAC address */
1077 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
1078 priv->hw->mac->get_umac_addr((void __iomem *)
1079 priv->dev->base_addr,
1080 priv->dev->dev_addr, 0);
1081 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001082 eth_hw_addr_random(priv->dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001083 }
1084 pr_warning("%s: device MAC address %pM\n", priv->dev->name,
1085 priv->dev->dev_addr);
1086}
1087
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001088static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1089{
1090 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001091 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001092 int atds = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001093
1094 /* Some DMA parameters can be passed from the platform;
1095 * in case of these are not passed we keep a default
1096 * (good for all the chips) and init the DMA! */
1097 if (priv->plat->dma_cfg) {
1098 pbl = priv->plat->dma_cfg->pbl;
1099 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001100 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001101 burst_len = priv->plat->dma_cfg->burst_len;
1102 }
1103
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001104 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1105 atds = 1;
1106
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001107 return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001108 burst_len, priv->dma_tx_phy,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001109 priv->dma_rx_phy, atds);
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001110}
1111
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001112/**
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001113 * stmmac_tx_timer:
1114 * @data: data pointer
1115 * Description:
1116 * This is the timer handler to directly invoke the stmmac_tx_clean.
1117 */
1118static void stmmac_tx_timer(unsigned long data)
1119{
1120 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1121
1122 stmmac_tx_clean(priv);
1123}
1124
1125/**
1126 * stmmac_tx_timer:
1127 * @priv: private data structure
1128 * Description:
1129 * This inits the transmit coalesce parameters: i.e. timer rate,
1130 * timer handler and default threshold used for enabling the
1131 * interrupt on completion bit.
1132 */
1133static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1134{
1135 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1136 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1137 init_timer(&priv->txtimer);
1138 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1139 priv->txtimer.data = (unsigned long)priv;
1140 priv->txtimer.function = stmmac_tx_timer;
1141 add_timer(&priv->txtimer);
1142}
1143
1144/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001145 * stmmac_open - open entry point of the driver
1146 * @dev : pointer to the device structure.
1147 * Description:
1148 * This function is the open entry point of the driver.
1149 * Return value:
1150 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1151 * file on failure.
1152 */
1153static int stmmac_open(struct net_device *dev)
1154{
1155 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001156 int ret;
1157
Stefan Roesea6308442012-09-21 01:06:29 +00001158 clk_prepare_enable(priv->stmmac_clk);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001159
1160 stmmac_check_ether_addr(priv);
1161
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001162 if (!priv->pcs) {
1163 ret = stmmac_init_phy(dev);
1164 if (ret) {
1165 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1166 __func__, ret);
1167 goto open_error;
1168 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001169 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001170
1171 /* Create and initialize the TX/RX descriptors chains. */
1172 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1173 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1174 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
1175 init_dma_desc_rings(dev);
1176
1177 /* DMA initialization and SW reset */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001178 ret = stmmac_init_dma_engine(priv);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001179 if (ret < 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001180 pr_err("%s: DMA initialization failed\n", __func__);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001181 goto open_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001182 }
1183
1184 /* Copy the MAC addr into the HW */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001185 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001186
Giuseppe CAVALLAROca5f12c2010-01-06 23:07:15 +00001187 /* If required, perform hw setup of the bus. */
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +00001188 if (priv->plat->bus_setup)
1189 priv->plat->bus_setup(priv->ioaddr);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001190
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001191 /* Initialize the MAC Core */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001192 priv->hw->mac->core_init(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001193
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001194 /* Request the IRQ lines */
1195 ret = request_irq(dev->irq, stmmac_interrupt,
1196 IRQF_SHARED, dev->name, dev);
1197 if (unlikely(ret < 0)) {
1198 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1199 __func__, dev->irq, ret);
1200 goto open_error;
1201 }
1202
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001203 /* Request the Wake IRQ in case of another line is used for WoL */
1204 if (priv->wol_irq != dev->irq) {
1205 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1206 IRQF_SHARED, dev->name, dev);
1207 if (unlikely(ret < 0)) {
1208 pr_err("%s: ERROR: allocating the ext WoL IRQ %d "
1209 "(error: %d)\n", __func__, priv->wol_irq, ret);
1210 goto open_error_wolirq;
1211 }
1212 }
1213
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001214 /* Request the IRQ lines */
1215 if (priv->lpi_irq != -ENXIO) {
1216 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1217 dev->name, dev);
1218 if (unlikely(ret < 0)) {
1219 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1220 __func__, priv->lpi_irq, ret);
1221 goto open_error_lpiirq;
1222 }
1223 }
1224
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001225 /* Enable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001226 stmmac_set_mac(priv->ioaddr, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001227
1228 /* Set the HW DMA mode and the COE */
1229 stmmac_dma_operation_mode(priv);
1230
1231 /* Extra statistics */
1232 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1233 priv->xstats.threshold = tc;
1234
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001235 stmmac_mmc_setup(priv);
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001236
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001237#ifdef CONFIG_STMMAC_DEBUG_FS
1238 ret = stmmac_init_fs(dev);
1239 if (ret < 0)
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001240 pr_warning("%s: failed debugFS registration\n", __func__);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001241#endif
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001242 /* Start the ball rolling... */
1243 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001244 priv->hw->dma->start_tx(priv->ioaddr);
1245 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001246
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001247 /* Dump DMA/MAC registers */
1248 if (netif_msg_hw(priv)) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001249 priv->hw->mac->dump_regs(priv->ioaddr);
1250 priv->hw->dma->dump_regs(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001251 }
1252
1253 if (priv->phydev)
1254 phy_start(priv->phydev);
1255
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001256 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS_TIMER;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001257
1258 /* Using PCS we cannot dial with the phy registers at this stage
1259 * so we do not support extra feature like EEE.
1260 */
1261 if (!priv->pcs)
1262 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001263
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001264 stmmac_init_tx_coalesce(priv);
1265
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00001266 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1267 priv->rx_riwt = MAX_DMA_RIWT;
1268 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1269 }
1270
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001271 if (priv->pcs && priv->hw->mac->ctrl_ane)
1272 priv->hw->mac->ctrl_ane(priv->ioaddr, 0);
1273
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001274 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001275 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001276
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001277 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001278
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001279open_error_lpiirq:
1280 if (priv->wol_irq != dev->irq)
1281 free_irq(priv->wol_irq, dev);
1282
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001283open_error_wolirq:
1284 free_irq(dev->irq, dev);
1285
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001286open_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001287 if (priv->phydev)
1288 phy_disconnect(priv->phydev);
1289
Stefan Roesea6308442012-09-21 01:06:29 +00001290 clk_disable_unprepare(priv->stmmac_clk);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001291
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001292 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001293}
1294
1295/**
1296 * stmmac_release - close entry point of the driver
1297 * @dev : device pointer.
1298 * Description:
1299 * This is the stop entry point of the driver.
1300 */
1301static int stmmac_release(struct net_device *dev)
1302{
1303 struct stmmac_priv *priv = netdev_priv(dev);
1304
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001305 if (priv->eee_enabled)
1306 del_timer_sync(&priv->eee_ctrl_timer);
1307
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001308 /* Stop and disconnect the PHY */
1309 if (priv->phydev) {
1310 phy_stop(priv->phydev);
1311 phy_disconnect(priv->phydev);
1312 priv->phydev = NULL;
1313 }
1314
1315 netif_stop_queue(dev);
1316
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001317 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001318
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001319 del_timer_sync(&priv->txtimer);
1320
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001321 /* Free the IRQ lines */
1322 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001323 if (priv->wol_irq != dev->irq)
1324 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001325 if (priv->lpi_irq != -ENXIO)
1326 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001327
1328 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001329 priv->hw->dma->stop_tx(priv->ioaddr);
1330 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001331
1332 /* Release and free the Rx/Tx resources */
1333 free_dma_desc_resources(priv);
1334
avisconti19449bf2010-10-25 18:58:14 +00001335 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001336 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001337
1338 netif_carrier_off(dev);
1339
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001340#ifdef CONFIG_STMMAC_DEBUG_FS
1341 stmmac_exit_fs();
1342#endif
Stefan Roesea6308442012-09-21 01:06:29 +00001343 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001344
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001345 return 0;
1346}
1347
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001348/**
1349 * stmmac_xmit:
1350 * @skb : the socket buffer
1351 * @dev : device pointer
1352 * Description : Tx entry point of the driver.
1353 */
1354static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1355{
1356 struct stmmac_priv *priv = netdev_priv(dev);
1357 unsigned int txsize = priv->dma_tx_size;
1358 unsigned int entry;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001359 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001360 int nfrags = skb_shinfo(skb)->nr_frags;
1361 struct dma_desc *desc, *first;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001362 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001363
1364 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1365 if (!netif_queue_stopped(dev)) {
1366 netif_stop_queue(dev);
1367 /* This is a hard error, log it. */
1368 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1369 __func__);
1370 }
1371 return NETDEV_TX_BUSY;
1372 }
1373
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001374 spin_lock(&priv->tx_lock);
1375
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001376 if (priv->tx_path_in_lpi_mode)
1377 stmmac_disable_eee_mode(priv);
1378
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001379 entry = priv->cur_tx % txsize;
1380
1381#ifdef STMMAC_XMIT_DEBUG
1382 if ((skb->len > ETH_FRAME_LEN) || nfrags)
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001383 pr_debug("stmmac xmit: [entry %d]\n"
1384 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1385 "\tn_frags: %d - ip_summed: %d - %s gso\n"
1386 "\ttx_count_frames %d\n", entry,
1387 skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
1388 !skb_is_gso(skb) ? "isn't" : "is",
1389 priv->tx_count_frames);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001390#endif
1391
Michał Mirosław5e982f32011-04-09 02:46:55 +00001392 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001393
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001394 if (priv->extend_desc)
1395 desc = (struct dma_desc *) (priv->dma_etx + entry);
1396 else
1397 desc = priv->dma_tx + entry;
1398
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001399 first = desc;
1400
1401#ifdef STMMAC_XMIT_DEBUG
1402 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001403 pr_debug("\tskb len: %d, nopaged_len: %d,\n"
1404 "\t\tn_frags: %d, ip_summed: %d\n",
1405 skb->len, nopaged_len, nfrags, skb->ip_summed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001406#endif
1407 priv->tx_skbuff[entry] = skb;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001408
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001409 /* To program the descriptors according to the size of the frame */
1410 if (priv->mode == STMMAC_RING_MODE) {
1411 is_jumbo = priv->hw->ring->is_jumbo_frm(skb->len,
1412 priv->plat->enh_desc);
1413 if (unlikely(is_jumbo))
1414 entry = priv->hw->ring->jumbo_frm(priv, skb,
1415 csum_insertion);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001416 } else {
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001417 is_jumbo = priv->hw->chain->is_jumbo_frm(skb->len,
1418 priv->plat->enh_desc);
1419 if (unlikely(is_jumbo))
1420 entry = priv->hw->chain->jumbo_frm(priv, skb,
1421 csum_insertion);
1422 }
1423 if (likely(!is_jumbo)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001424 desc->des2 = dma_map_single(priv->device, skb->data,
1425 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001426 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001427 csum_insertion, priv->mode);
1428 } else
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001429 desc = first;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001430
1431 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001432 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1433 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001434
1435 entry = (++priv->cur_tx) % txsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001436 if (priv->extend_desc)
1437 desc = (struct dma_desc *) (priv->dma_etx + entry);
1438 else
1439 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001440
1441 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
Ian Campbellf7223802011-09-21 21:53:20 +00001442 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1443 DMA_TO_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001444 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001445 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
1446 priv->mode);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001447 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001448 priv->hw->desc->set_tx_owner(desc);
Deepak Sikri8e839892012-07-08 21:14:45 +00001449 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001450 }
1451
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001452 /* Finalize the latest segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001453 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001454
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001455 wmb();
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001456 /* According to the coalesce parameter the IC bit for the latest
1457 * segment could be reset and the timer re-started to invoke the
1458 * stmmac_tx function. This approach takes care about the fragments.
1459 */
1460 priv->tx_count_frames += nfrags + 1;
1461 if (priv->tx_coal_frames > priv->tx_count_frames) {
1462 priv->hw->desc->clear_tx_ic(desc);
1463 priv->xstats.tx_reset_ic_bit++;
1464 TX_DBG("\t[entry %d]: tx_count_frames %d\n", entry,
1465 priv->tx_count_frames);
1466 mod_timer(&priv->txtimer,
1467 STMMAC_COAL_TIMER(priv->tx_coal_timer));
1468 } else
1469 priv->tx_count_frames = 0;
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001470
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001471 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001472 priv->hw->desc->set_tx_owner(first);
Deepak Sikri8e839892012-07-08 21:14:45 +00001473 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001474
1475 priv->cur_tx++;
1476
1477#ifdef STMMAC_XMIT_DEBUG
1478 if (netif_msg_pktdata(priv)) {
1479 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1480 "first=%p, nfrags=%d\n",
1481 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1482 entry, first, nfrags);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001483 if (priv->extend_desc)
1484 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
1485 else
1486 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
1487
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001488 pr_info(">>> frame to be transmitted: ");
1489 print_pkt(skb->data, skb->len);
1490 }
1491#endif
1492 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1493 TX_DBG("%s: stop transmitted packets\n", __func__);
1494 netif_stop_queue(dev);
1495 }
1496
1497 dev->stats.tx_bytes += skb->len;
1498
Richard Cochran3e82ce12011-06-12 02:19:06 +00001499 skb_tx_timestamp(skb);
1500
Richard Cochran52f64fa2011-06-19 03:31:43 +00001501 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1502
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001503 spin_unlock(&priv->tx_lock);
1504
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001505 return NETDEV_TX_OK;
1506}
1507
1508static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1509{
1510 unsigned int rxsize = priv->dma_rx_size;
1511 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001512
1513 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1514 unsigned int entry = priv->dirty_rx % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001515 struct dma_desc *p;
1516
1517 if (priv->extend_desc)
1518 p = (struct dma_desc *) (priv->dma_erx + entry);
1519 else
1520 p = priv->dma_rx + entry;
1521
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001522 if (likely(priv->rx_skbuff[entry] == NULL)) {
1523 struct sk_buff *skb;
1524
Eric Dumazetacb600d2012-10-05 06:23:55 +00001525 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001526
1527 if (unlikely(skb == NULL))
1528 break;
1529
1530 priv->rx_skbuff[entry] = skb;
1531 priv->rx_skbuff_dma[entry] =
1532 dma_map_single(priv->device, skb->data, bfsize,
1533 DMA_FROM_DEVICE);
1534
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001535 p->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001536
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001537 if (unlikely((priv->mode == STMMAC_RING_MODE) &&
1538 (priv->plat->has_gmac)))
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001539 priv->hw->ring->refill_desc3(bfsize, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001540
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001541 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1542 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001543 wmb();
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001544 priv->hw->desc->set_rx_owner(p);
Deepak Sikri8e839892012-07-08 21:14:45 +00001545 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001546 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001547}
1548
1549static int stmmac_rx(struct stmmac_priv *priv, int limit)
1550{
1551 unsigned int rxsize = priv->dma_rx_size;
1552 unsigned int entry = priv->cur_rx % rxsize;
1553 unsigned int next_entry;
1554 unsigned int count = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001555
1556#ifdef STMMAC_RX_DEBUG
1557 if (netif_msg_hw(priv)) {
1558 pr_debug(">>> stmmac_rx: descriptor ring:\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001559 if (priv->extend_desc)
1560 stmmac_display_ring((void *) priv->dma_erx, rxsize, 1);
1561 else
1562 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001563 }
1564#endif
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001565 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001566 int status;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001567 struct dma_desc *p, *p_next;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001568
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001569 if (priv->extend_desc)
1570 p = (struct dma_desc *) (priv->dma_erx + entry);
1571 else
1572 p = priv->dma_rx + entry ;
1573
1574 if (priv->hw->desc->get_rx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001575 break;
1576
1577 count++;
1578
1579 next_entry = (++priv->cur_rx) % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001580 if (priv->extend_desc)
1581 p_next = (struct dma_desc *) (priv->dma_erx +
1582 next_entry);
1583 else
1584 p_next = priv->dma_rx + next_entry;
1585
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001586 prefetch(p_next);
1587
1588 /* read the status of the incoming frame */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001589 status = priv->hw->desc->rx_status(&priv->dev->stats,
1590 &priv->xstats, p);
1591 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
1592 priv->hw->desc->rx_extended_status(&priv->dev->stats,
1593 &priv->xstats,
1594 priv->dma_erx +
1595 entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001596 if (unlikely(status == discard_frame))
1597 priv->dev->stats.rx_errors++;
1598 else {
1599 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001600 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001601
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001602 frame_len = priv->hw->desc->get_rx_frame_len(p,
1603 priv->plat->rx_coe);
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00001604 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
1605 * Type frames (LLC/LLC-SNAP) */
1606 if (unlikely(status != llc_snap))
1607 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001608#ifdef STMMAC_RX_DEBUG
1609 if (frame_len > ETH_FRAME_LEN)
1610 pr_debug("\tRX frame size %d, COE status: %d\n",
1611 frame_len, status);
1612
1613 if (netif_msg_hw(priv))
1614 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
1615 p, entry, p->des2);
1616#endif
1617 skb = priv->rx_skbuff[entry];
1618 if (unlikely(!skb)) {
1619 pr_err("%s: Inconsistent Rx descriptor chain\n",
1620 priv->dev->name);
1621 priv->dev->stats.rx_dropped++;
1622 break;
1623 }
1624 prefetch(skb->data - NET_IP_ALIGN);
1625 priv->rx_skbuff[entry] = NULL;
1626
1627 skb_put(skb, frame_len);
1628 dma_unmap_single(priv->device,
1629 priv->rx_skbuff_dma[entry],
1630 priv->dma_buf_sz, DMA_FROM_DEVICE);
1631#ifdef STMMAC_RX_DEBUG
1632 if (netif_msg_pktdata(priv)) {
1633 pr_info(" frame received (%dbytes)", frame_len);
1634 print_pkt(skb->data, frame_len);
1635 }
1636#endif
1637 skb->protocol = eth_type_trans(skb, priv->dev);
1638
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00001639 if (unlikely(!priv->plat->rx_coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07001640 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00001641 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001642 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00001643
1644 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001645
1646 priv->dev->stats.rx_packets++;
1647 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001648 }
1649 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001650 }
1651
1652 stmmac_rx_refill(priv);
1653
1654 priv->xstats.rx_pkt_n += count;
1655
1656 return count;
1657}
1658
1659/**
1660 * stmmac_poll - stmmac poll method (NAPI)
1661 * @napi : pointer to the napi structure.
1662 * @budget : maximum number of packets that the current CPU can receive from
1663 * all interfaces.
1664 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001665 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001666 */
1667static int stmmac_poll(struct napi_struct *napi, int budget)
1668{
1669 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
1670 int work_done = 0;
1671
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001672 priv->xstats.napi_poll++;
1673 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001674
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001675 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001676 if (work_done < budget) {
1677 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001678 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001679 }
1680 return work_done;
1681}
1682
1683/**
1684 * stmmac_tx_timeout
1685 * @dev : Pointer to net device structure
1686 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001687 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001688 * netdev structure and arrange for the device to be reset to a sane state
1689 * in order to transmit a new packet.
1690 */
1691static void stmmac_tx_timeout(struct net_device *dev)
1692{
1693 struct stmmac_priv *priv = netdev_priv(dev);
1694
1695 /* Clear Tx resources and restart transmitting again */
1696 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001697}
1698
1699/* Configuration changes (passed on by ifconfig) */
1700static int stmmac_config(struct net_device *dev, struct ifmap *map)
1701{
1702 if (dev->flags & IFF_UP) /* can't act on a running interface */
1703 return -EBUSY;
1704
1705 /* Don't allow changing the I/O address */
1706 if (map->base_addr != dev->base_addr) {
1707 pr_warning("%s: can't change I/O address\n", dev->name);
1708 return -EOPNOTSUPP;
1709 }
1710
1711 /* Don't allow changing the IRQ */
1712 if (map->irq != dev->irq) {
1713 pr_warning("%s: can't change IRQ number %d\n",
1714 dev->name, dev->irq);
1715 return -EOPNOTSUPP;
1716 }
1717
1718 /* ignore other fields */
1719 return 0;
1720}
1721
1722/**
Jiri Pirko01789342011-08-16 06:29:00 +00001723 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001724 * @dev : pointer to the device structure
1725 * Description:
1726 * This function is a driver entry point which gets called by the kernel
1727 * whenever multicast addresses must be enabled/disabled.
1728 * Return value:
1729 * void.
1730 */
Jiri Pirko01789342011-08-16 06:29:00 +00001731static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001732{
1733 struct stmmac_priv *priv = netdev_priv(dev);
1734
1735 spin_lock(&priv->lock);
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00001736 priv->hw->mac->set_filter(dev, priv->synopsys_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001737 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001738}
1739
1740/**
1741 * stmmac_change_mtu - entry point to change MTU size for the device.
1742 * @dev : device pointer.
1743 * @new_mtu : the new MTU size for the device.
1744 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
1745 * to drive packet transmission. Ethernet has an MTU of 1500 octets
1746 * (ETH_DATA_LEN). This value can be changed with ifconfig.
1747 * Return value:
1748 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1749 * file on failure.
1750 */
1751static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
1752{
1753 struct stmmac_priv *priv = netdev_priv(dev);
1754 int max_mtu;
1755
1756 if (netif_running(dev)) {
1757 pr_err("%s: must be stopped to change its MTU\n", dev->name);
1758 return -EBUSY;
1759 }
1760
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00001761 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001762 max_mtu = JUMBO_LEN;
1763 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00001764 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001765
1766 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
1767 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
1768 return -EINVAL;
1769 }
1770
Michał Mirosław5e982f32011-04-09 02:46:55 +00001771 dev->mtu = new_mtu;
1772 netdev_update_features(dev);
1773
1774 return 0;
1775}
1776
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001777static netdev_features_t stmmac_fix_features(struct net_device *dev,
1778 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001779{
1780 struct stmmac_priv *priv = netdev_priv(dev);
1781
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001782 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00001783 features &= ~NETIF_F_RXCSUM;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00001784 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
1785 features &= ~NETIF_F_IPV6_CSUM;
Michał Mirosław5e982f32011-04-09 02:46:55 +00001786 if (!priv->plat->tx_coe)
1787 features &= ~NETIF_F_ALL_CSUM;
1788
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001789 /* Some GMAC devices have a bugged Jumbo frame support that
1790 * needs to have the Tx COE disabled for oversized frames
1791 * (due to limited buffer sizes). In this case we disable
1792 * the TX csum insertionin the TDES and not use SF. */
Michał Mirosław5e982f32011-04-09 02:46:55 +00001793 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
1794 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001795
Michał Mirosław5e982f32011-04-09 02:46:55 +00001796 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001797}
1798
1799static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
1800{
1801 struct net_device *dev = (struct net_device *)dev_id;
1802 struct stmmac_priv *priv = netdev_priv(dev);
1803
1804 if (unlikely(!dev)) {
1805 pr_err("%s: invalid dev pointer\n", __func__);
1806 return IRQ_NONE;
1807 }
1808
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001809 /* To handle GMAC own interrupts */
1810 if (priv->plat->has_gmac) {
1811 int status = priv->hw->mac->host_irq_status((void __iomem *)
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00001812 dev->base_addr,
1813 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001814 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001815 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00001816 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001817 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00001818 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001819 priv->tx_path_in_lpi_mode = false;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001820 }
1821 }
1822
1823 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001824 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001825
1826 return IRQ_HANDLED;
1827}
1828
1829#ifdef CONFIG_NET_POLL_CONTROLLER
1830/* Polling receive - used by NETCONSOLE and other diagnostic tools
1831 * to allow network I/O with interrupts disabled. */
1832static void stmmac_poll_controller(struct net_device *dev)
1833{
1834 disable_irq(dev->irq);
1835 stmmac_interrupt(dev->irq, dev);
1836 enable_irq(dev->irq);
1837}
1838#endif
1839
1840/**
1841 * stmmac_ioctl - Entry point for the Ioctl
1842 * @dev: Device pointer.
1843 * @rq: An IOCTL specefic structure, that can contain a pointer to
1844 * a proprietary structure used to pass information to the driver.
1845 * @cmd: IOCTL command
1846 * Description:
1847 * Currently there are no special functionality supported in IOCTL, just the
1848 * phy_mii_ioctl(...) can be invoked.
1849 */
1850static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1851{
1852 struct stmmac_priv *priv = netdev_priv(dev);
Richard Cochran28b04112010-07-17 08:48:55 +00001853 int ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001854
1855 if (!netif_running(dev))
1856 return -EINVAL;
1857
Richard Cochran28b04112010-07-17 08:48:55 +00001858 if (!priv->phydev)
1859 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001860
Richard Cochran28b04112010-07-17 08:48:55 +00001861 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
Richard Cochran28b04112010-07-17 08:48:55 +00001862
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001863 return ret;
1864}
1865
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001866#ifdef CONFIG_STMMAC_DEBUG_FS
1867static struct dentry *stmmac_fs_dir;
1868static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001869static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001870
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001871static void sysfs_display_ring(void *head, int size, int extend_desc,
1872 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001873{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001874 int i;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001875 struct dma_extended_desc *ep = (struct dma_extended_desc *) head;
1876 struct dma_desc *p = (struct dma_desc *) head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001877
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001878 for (i = 0; i < size; i++) {
1879 u64 x;
1880 if (extend_desc) {
1881 x = *(u64 *) ep;
1882 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
1883 i, (unsigned int) virt_to_phys(ep),
1884 (unsigned int) x, (unsigned int) (x >> 32),
1885 ep->basic.des2, ep->basic.des3);
1886 ep++;
1887 } else {
1888 x = *(u64 *) p;
1889 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
1890 i, (unsigned int) virt_to_phys(ep),
1891 (unsigned int) x, (unsigned int) (x >> 32),
1892 p->des2, p->des3);
1893 p++;
1894 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001895 seq_printf(seq, "\n");
1896 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001897}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001898
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001899static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
1900{
1901 struct net_device *dev = seq->private;
1902 struct stmmac_priv *priv = netdev_priv(dev);
1903 unsigned int txsize = priv->dma_tx_size;
1904 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001905
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001906 if (priv->extend_desc) {
1907 seq_printf(seq, "Extended RX descriptor ring:\n");
1908 sysfs_display_ring((void *) priv->dma_erx, rxsize, 1, seq);
1909 seq_printf(seq, "Extended TX descriptor ring:\n");
1910 sysfs_display_ring((void *) priv->dma_etx, txsize, 1, seq);
1911 } else {
1912 seq_printf(seq, "RX descriptor ring:\n");
1913 sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
1914 seq_printf(seq, "TX descriptor ring:\n");
1915 sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001916 }
1917
1918 return 0;
1919}
1920
1921static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
1922{
1923 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
1924}
1925
1926static const struct file_operations stmmac_rings_status_fops = {
1927 .owner = THIS_MODULE,
1928 .open = stmmac_sysfs_ring_open,
1929 .read = seq_read,
1930 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00001931 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00001932};
1933
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001934static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
1935{
1936 struct net_device *dev = seq->private;
1937 struct stmmac_priv *priv = netdev_priv(dev);
1938
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001939 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001940 seq_printf(seq, "DMA HW features not supported\n");
1941 return 0;
1942 }
1943
1944 seq_printf(seq, "==============================\n");
1945 seq_printf(seq, "\tDMA HW features\n");
1946 seq_printf(seq, "==============================\n");
1947
1948 seq_printf(seq, "\t10/100 Mbps %s\n",
1949 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
1950 seq_printf(seq, "\t1000 Mbps %s\n",
1951 (priv->dma_cap.mbps_1000) ? "Y" : "N");
1952 seq_printf(seq, "\tHalf duple %s\n",
1953 (priv->dma_cap.half_duplex) ? "Y" : "N");
1954 seq_printf(seq, "\tHash Filter: %s\n",
1955 (priv->dma_cap.hash_filter) ? "Y" : "N");
1956 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
1957 (priv->dma_cap.multi_addr) ? "Y" : "N");
1958 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
1959 (priv->dma_cap.pcs) ? "Y" : "N");
1960 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
1961 (priv->dma_cap.sma_mdio) ? "Y" : "N");
1962 seq_printf(seq, "\tPMT Remote wake up: %s\n",
1963 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
1964 seq_printf(seq, "\tPMT Magic Frame: %s\n",
1965 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
1966 seq_printf(seq, "\tRMON module: %s\n",
1967 (priv->dma_cap.rmon) ? "Y" : "N");
1968 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
1969 (priv->dma_cap.time_stamp) ? "Y" : "N");
1970 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
1971 (priv->dma_cap.atime_stamp) ? "Y" : "N");
1972 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
1973 (priv->dma_cap.eee) ? "Y" : "N");
1974 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
1975 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
1976 (priv->dma_cap.tx_coe) ? "Y" : "N");
1977 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
1978 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
1979 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
1980 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
1981 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
1982 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
1983 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
1984 priv->dma_cap.number_rx_channel);
1985 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
1986 priv->dma_cap.number_tx_channel);
1987 seq_printf(seq, "\tEnhanced descriptors: %s\n",
1988 (priv->dma_cap.enh_desc) ? "Y" : "N");
1989
1990 return 0;
1991}
1992
1993static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
1994{
1995 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
1996}
1997
1998static const struct file_operations stmmac_dma_cap_fops = {
1999 .owner = THIS_MODULE,
2000 .open = stmmac_sysfs_dma_cap_open,
2001 .read = seq_read,
2002 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002003 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002004};
2005
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002006static int stmmac_init_fs(struct net_device *dev)
2007{
2008 /* Create debugfs entries */
2009 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
2010
2011 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
2012 pr_err("ERROR %s, debugfs create directory failed\n",
2013 STMMAC_RESOURCE_NAME);
2014
2015 return -ENOMEM;
2016 }
2017
2018 /* Entry to report DMA RX/TX rings */
2019 stmmac_rings_status = debugfs_create_file("descriptors_status",
2020 S_IRUGO, stmmac_fs_dir, dev,
2021 &stmmac_rings_status_fops);
2022
2023 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
2024 pr_info("ERROR creating stmmac ring debugfs file\n");
2025 debugfs_remove(stmmac_fs_dir);
2026
2027 return -ENOMEM;
2028 }
2029
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002030 /* Entry to report the DMA HW features */
2031 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
2032 dev, &stmmac_dma_cap_fops);
2033
2034 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
2035 pr_info("ERROR creating stmmac MMC debugfs file\n");
2036 debugfs_remove(stmmac_rings_status);
2037 debugfs_remove(stmmac_fs_dir);
2038
2039 return -ENOMEM;
2040 }
2041
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002042 return 0;
2043}
2044
2045static void stmmac_exit_fs(void)
2046{
2047 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002048 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002049 debugfs_remove(stmmac_fs_dir);
2050}
2051#endif /* CONFIG_STMMAC_DEBUG_FS */
2052
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002053static const struct net_device_ops stmmac_netdev_ops = {
2054 .ndo_open = stmmac_open,
2055 .ndo_start_xmit = stmmac_xmit,
2056 .ndo_stop = stmmac_release,
2057 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00002058 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00002059 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002060 .ndo_tx_timeout = stmmac_tx_timeout,
2061 .ndo_do_ioctl = stmmac_ioctl,
2062 .ndo_set_config = stmmac_config,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002063#ifdef CONFIG_NET_POLL_CONTROLLER
2064 .ndo_poll_controller = stmmac_poll_controller,
2065#endif
2066 .ndo_set_mac_address = eth_mac_addr,
2067};
2068
2069/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002070 * stmmac_hw_init - Init the MAC device
2071 * @priv : pointer to the private device structure.
2072 * Description: this function detects which MAC device
2073 * (GMAC/MAC10-100) has to attached, checks the HW capability
2074 * (if supported) and sets the driver's features (for example
2075 * to use the ring or chaine mode or support the normal/enh
2076 * descriptor structure).
2077 */
2078static int stmmac_hw_init(struct stmmac_priv *priv)
2079{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002080 int ret;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002081 struct mac_device_info *mac;
2082
2083 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002084 if (priv->plat->has_gmac) {
2085 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002086 mac = dwmac1000_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002087 } else {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002088 mac = dwmac100_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002089 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002090 if (!mac)
2091 return -ENOMEM;
2092
2093 priv->hw = mac;
2094
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002095 /* Get and dump the chip ID */
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00002096 priv->synopsys_id = stmmac_get_synopsys_id(priv);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002097
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002098 /* To use alternate (extended) or normal descriptor structures */
2099 stmmac_selec_desc_mode(priv);
2100
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002101 /* To use the chained or ring mode */
2102 if (chain_mode) {
2103 priv->hw->chain = &chain_mode_ops;
2104 pr_info(" Chain mode enabled\n");
2105 priv->mode = STMMAC_CHAIN_MODE;
2106 } else {
2107 priv->hw->ring = &ring_mode_ops;
2108 pr_info(" Ring mode enabled\n");
2109 priv->mode = STMMAC_RING_MODE;
2110 }
2111
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002112 /* Get the HW capability (new GMAC newer than 3.50a) */
2113 priv->hw_cap_support = stmmac_get_hw_features(priv);
2114 if (priv->hw_cap_support) {
2115 pr_info(" DMA HW capability register supported");
2116
2117 /* We can override some gmac/dma configuration fields: e.g.
2118 * enh_desc, tx_coe (e.g. that are passed through the
2119 * platform) with the values from the HW capability
2120 * register (if supported).
2121 */
2122 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002123 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002124
2125 priv->plat->tx_coe = priv->dma_cap.tx_coe;
2126
2127 if (priv->dma_cap.rx_coe_type2)
2128 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2129 else if (priv->dma_cap.rx_coe_type1)
2130 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2131
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002132 } else
2133 pr_info(" No HW DMA feature register supported");
2134
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002135 /* Enable the IPC (Checksum Offload) and check if the feature has been
2136 * enabled during the core configuration. */
2137 ret = priv->hw->mac->rx_ipc(priv->ioaddr);
2138 if (!ret) {
2139 pr_warning(" RX IPC Checksum Offload not configured.\n");
2140 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
2141 }
2142
2143 if (priv->plat->rx_coe)
2144 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2145 priv->plat->rx_coe);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002146 if (priv->plat->tx_coe)
2147 pr_info(" TX Checksum insertion supported\n");
2148
2149 if (priv->plat->pmt) {
2150 pr_info(" Wake-Up On Lan supported\n");
2151 device_set_wakeup_capable(priv->device, 1);
2152 }
2153
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002154 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002155}
2156
2157/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002158 * stmmac_dvr_probe
2159 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002160 * @plat_dat: platform data pointer
2161 * @addr: iobase memory address
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002162 * Description: this is the main probe function used to
2163 * call the alloc_etherdev, allocate the priv structure.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002164 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002165struct stmmac_priv *stmmac_dvr_probe(struct device *device,
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002166 struct plat_stmmacenet_data *plat_dat,
2167 void __iomem *addr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002168{
2169 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002170 struct net_device *ndev = NULL;
2171 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002172
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002173 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002174 if (!ndev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002175 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002176
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002177 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002178
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002179 priv = netdev_priv(ndev);
2180 priv->device = device;
2181 priv->dev = ndev;
2182
2183 ether_setup(ndev);
2184
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002185 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002186 priv->pause = pause;
2187 priv->plat = plat_dat;
2188 priv->ioaddr = addr;
2189 priv->dev->base_addr = (unsigned long)addr;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002190
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002191 /* Verify driver arguments */
2192 stmmac_verify_args();
2193
2194 /* Override with kernel parameters if supplied XXX CRS XXX
2195 * this needs to have multiple instances */
2196 if ((phyaddr >= 0) && (phyaddr <= 31))
2197 priv->plat->phy_addr = phyaddr;
2198
2199 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002200 ret = stmmac_hw_init(priv);
2201 if (ret)
2202 goto error_free_netdev;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002203
2204 ndev->netdev_ops = &stmmac_netdev_ops;
2205
2206 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2207 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002208 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2209 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002210#ifdef STMMAC_VLAN_TAG_USED
2211 /* Both mac100 and gmac support receive VLAN tag detection */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002212 ndev->features |= NETIF_F_HW_VLAN_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002213#endif
2214 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2215
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002216 if (flow_ctrl)
2217 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2218
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002219 /* Rx Watchdog is available in the COREs newer than the 3.40.
2220 * In some case, for example on bugged HW this feature
2221 * has to be disable and this can be done by passing the
2222 * riwt_off field from the platform.
2223 */
2224 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2225 priv->use_riwt = 1;
2226 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2227 }
2228
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002229 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002230
Vlad Lunguf8e96162010-11-29 22:52:52 +00002231 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002232 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002233
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002234 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002235 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002236 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002237 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002238 }
2239
Kelvin Cheungae4d8cf2012-08-18 00:16:23 +00002240 priv->stmmac_clk = clk_get(priv->device, STMMAC_RESOURCE_NAME);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002241 if (IS_ERR(priv->stmmac_clk)) {
Giuseppe CAVALLARO31ea38e2012-04-18 19:48:22 +00002242 pr_warning("%s: warning: cannot get CSR clock\n", __func__);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002243 goto error_clk_get;
2244 }
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002245
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002246 /* If a specific clk_csr value is passed from the platform
2247 * this means that the CSR Clock Range selection cannot be
2248 * changed at run-time and it is fixed. Viceversa the driver'll try to
2249 * set the MDC clock dynamically according to the csr actual
2250 * clock input.
2251 */
2252 if (!priv->plat->clk_csr)
2253 stmmac_clk_csr_set(priv);
2254 else
2255 priv->clk_csr = priv->plat->clk_csr;
2256
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002257 stmmac_check_pcs_mode(priv);
2258
2259 if (!priv->pcs) {
2260 /* MDIO bus Registration */
2261 ret = stmmac_mdio_register(ndev);
2262 if (ret < 0) {
2263 pr_debug("%s: MDIO bus (id: %d) registration failed",
2264 __func__, priv->plat->bus_id);
2265 goto error_mdio_register;
2266 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002267 }
2268
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002269 return priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002270
Viresh Kumar6a81c262012-07-30 14:39:41 -07002271error_mdio_register:
2272 clk_put(priv->stmmac_clk);
2273error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002274 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002275error_netdev_register:
2276 netif_napi_del(&priv->napi);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002277error_free_netdev:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002278 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002279
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002280 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002281}
2282
2283/**
2284 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002285 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002286 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002287 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002288 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002289int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002290{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002291 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002292
2293 pr_info("%s:\n\tremoving driver", __func__);
2294
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002295 priv->hw->dma->stop_rx(priv->ioaddr);
2296 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002297
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002298 stmmac_set_mac(priv->ioaddr, false);
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002299 if (!priv->pcs)
2300 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002301 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002302 unregister_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002303 free_netdev(ndev);
2304
2305 return 0;
2306}
2307
2308#ifdef CONFIG_PM
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002309int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002310{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002311 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002312 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002313
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002314 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002315 return 0;
2316
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002317 if (priv->phydev)
2318 phy_stop(priv->phydev);
2319
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002320 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002321
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002322 netif_device_detach(ndev);
2323 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002324
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002325 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002326
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002327 /* Stop TX/RX DMA */
2328 priv->hw->dma->stop_tx(priv->ioaddr);
2329 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002330
2331 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002332
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002333 /* Enable Power down mode by programming the PMT regs */
2334 if (device_may_wakeup(priv->device))
2335 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002336 else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002337 stmmac_set_mac(priv->ioaddr, false);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002338 /* Disable clock in case of PWM is off */
Stefan Roesea6308442012-09-21 01:06:29 +00002339 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002340 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002341 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002342 return 0;
2343}
2344
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002345int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002346{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002347 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002348 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002349
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002350 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002351 return 0;
2352
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002353 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02002354
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002355 /* Power Down bit, into the PM register, is cleared
2356 * automatically as soon as a magic packet or a Wake-up frame
2357 * is received. Anyway, it's better to manually clear
2358 * this bit because it can generate problems while resuming
2359 * from another devices (e.g. serial console). */
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002360 if (device_may_wakeup(priv->device))
Giuseppe Cavallaro543876c2010-09-24 21:27:41 -07002361 priv->hw->mac->pmt(priv->ioaddr, 0);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002362 else
2363 /* enable the clk prevously disabled */
Stefan Roesea6308442012-09-21 01:06:29 +00002364 clk_prepare_enable(priv->stmmac_clk);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002365
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002366 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002367
2368 /* Enable the MAC and DMA */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002369 stmmac_set_mac(priv->ioaddr, true);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002370 priv->hw->dma->start_tx(priv->ioaddr);
2371 priv->hw->dma->start_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002372
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002373 napi_enable(&priv->napi);
2374
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002375 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002376
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002377 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002378
2379 if (priv->phydev)
2380 phy_start(priv->phydev);
2381
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002382 return 0;
2383}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002384
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002385int stmmac_freeze(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002386{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002387 if (!ndev || !netif_running(ndev))
2388 return 0;
2389
2390 return stmmac_release(ndev);
2391}
2392
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002393int stmmac_restore(struct net_device *ndev)
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002394{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002395 if (!ndev || !netif_running(ndev))
2396 return 0;
2397
2398 return stmmac_open(ndev);
2399}
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002400#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002401
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002402/* Driver can be configured w/ and w/ both PCI and Platf drivers
2403 * depending on the configuration selected.
2404 */
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002405static int __init stmmac_init(void)
2406{
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002407 int ret;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002408
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002409 ret = stmmac_register_platform();
2410 if (ret)
2411 goto err;
2412 ret = stmmac_register_pci();
2413 if (ret)
2414 goto err_pci;
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002415 return 0;
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002416err_pci:
2417 stmmac_unregister_platform();
2418err:
2419 pr_err("stmmac: driver registration failed\n");
2420 return ret;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002421}
2422
2423static void __exit stmmac_exit(void)
2424{
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002425 stmmac_unregister_platform();
2426 stmmac_unregister_pci();
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002427}
2428
2429module_init(stmmac_init);
2430module_exit(stmmac_exit);
2431
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002432#ifndef MODULE
2433static int __init stmmac_cmdline_opt(char *str)
2434{
2435 char *opt;
2436
2437 if (!str || !*str)
2438 return -EINVAL;
2439 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002440 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002441 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002442 goto err;
2443 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002444 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002445 goto err;
2446 } else if (!strncmp(opt, "dma_txsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002447 if (kstrtoint(opt + 11, 0, &dma_txsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002448 goto err;
2449 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002450 if (kstrtoint(opt + 11, 0, &dma_rxsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002451 goto err;
2452 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002453 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002454 goto err;
2455 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002456 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002457 goto err;
2458 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002459 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002460 goto err;
2461 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002462 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002463 goto err;
2464 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002465 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002466 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00002467 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002468 if (kstrtoint(opt + 10, 0, &eee_timer))
2469 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002470 } else if (!strncmp(opt, "chain_mode:", 11)) {
2471 if (kstrtoint(opt + 11, 0, &chain_mode))
2472 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002473 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002474 }
2475 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002476
2477err:
2478 pr_err("%s: ERROR broken module parameter conversion", __func__);
2479 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002480}
2481
2482__setup("stmmaceth=", stmmac_cmdline_opt);
2483#endif
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05002484
2485MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
2486MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2487MODULE_LICENSE("GPL");