blob: 03652891fcbf602b9b13f649095fd96302ccd18b [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>
Srinivas Kandagatladb88f102014-01-16 10:52:52 +000046#include <linux/pinctrl/consumer.h>
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000047#ifdef CONFIG_STMMAC_DEBUG_FS
48#include <linux/debugfs.h>
49#include <linux/seq_file.h>
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +000050#endif /* CONFIG_STMMAC_DEBUG_FS */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +000051#include <linux/net_tstamp.h>
52#include "stmmac_ptp.h"
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +000053#include "stmmac.h"
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +080054#include <linux/reset.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070055
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070056#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070057
58/* Module parameters */
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000059#define TX_TIMEO 5000
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070060static int watchdog = TX_TIMEO;
61module_param(watchdog, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000062MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070063
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000064static int debug = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070065module_param(debug, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000066MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070067
stephen hemminger47d1f712013-12-30 10:38:57 -080068static int phyaddr = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070069module_param(phyaddr, int, S_IRUGO);
70MODULE_PARM_DESC(phyaddr, "Physical device address");
71
72#define DMA_TX_SIZE 256
73static int dma_txsize = DMA_TX_SIZE;
74module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
75MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
76
77#define DMA_RX_SIZE 256
78static int dma_rxsize = DMA_RX_SIZE;
79module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
80MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
81
82static int flow_ctrl = FLOW_OFF;
83module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
84MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
85
86static int pause = PAUSE_TIME;
87module_param(pause, int, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(pause, "Flow Control Pause Time");
89
90#define TC_DEFAULT 64
91static int tc = TC_DEFAULT;
92module_param(tc, int, S_IRUGO | S_IWUSR);
93MODULE_PARM_DESC(tc, "DMA threshold control value");
94
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +010095#define DEFAULT_BUFSIZE 1536
96static int buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070097module_param(buf_sz, int, S_IRUGO | S_IWUSR);
98MODULE_PARM_DESC(buf_sz, "DMA buffer size");
99
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700100static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
101 NETIF_MSG_LINK | NETIF_MSG_IFUP |
102 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
103
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000104#define STMMAC_DEFAULT_LPI_TIMER 1000
105static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
106module_param(eee_timer, int, S_IRUGO | S_IWUSR);
107MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200108#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000109
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000110/* By default the driver will use the ring mode to manage tx and rx descriptors
111 * but passing this value so user can force to use the chain instead of the ring
112 */
113static unsigned int chain_mode;
114module_param(chain_mode, int, S_IRUGO);
115MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
116
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700117static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700118
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000119#ifdef CONFIG_STMMAC_DEBUG_FS
120static int stmmac_init_fs(struct net_device *dev);
121static void stmmac_exit_fs(void);
122#endif
123
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000124#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
125
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700126/**
127 * stmmac_verify_args - verify the driver parameters.
128 * Description: it verifies if some wrong parameter is passed to the driver.
129 * Note that wrong parameters are replaced with the default values.
130 */
131static void stmmac_verify_args(void)
132{
133 if (unlikely(watchdog < 0))
134 watchdog = TX_TIMEO;
135 if (unlikely(dma_rxsize < 0))
136 dma_rxsize = DMA_RX_SIZE;
137 if (unlikely(dma_txsize < 0))
138 dma_txsize = DMA_TX_SIZE;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100139 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
140 buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700141 if (unlikely(flow_ctrl > 1))
142 flow_ctrl = FLOW_AUTO;
143 else if (likely(flow_ctrl < 0))
144 flow_ctrl = FLOW_OFF;
145 if (unlikely((pause < 0) || (pause > 0xffff)))
146 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000147 if (eee_timer < 0)
148 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700149}
150
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000151/**
152 * stmmac_clk_csr_set - dynamically set the MDC clock
153 * @priv: driver private structure
154 * Description: this is to dynamically set the MDC clock according to the csr
155 * clock input.
156 * Note:
157 * If a specific clk_csr value is passed from the platform
158 * this means that the CSR Clock Range selection cannot be
159 * changed at run-time and it is fixed (as reported in the driver
160 * documentation). Viceversa the driver will try to set the MDC
161 * clock dynamically according to the actual clock input.
162 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000163static void stmmac_clk_csr_set(struct stmmac_priv *priv)
164{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000165 u32 clk_rate;
166
167 clk_rate = clk_get_rate(priv->stmmac_clk);
168
169 /* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000170 * for all other cases except for the below mentioned ones.
171 * For values higher than the IEEE 802.3 specified frequency
172 * we can not estimate the proper divider as it is not known
173 * the frequency of clk_csr_i. So we do not change the default
174 * divider.
175 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000176 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
177 if (clk_rate < CSR_F_35M)
178 priv->clk_csr = STMMAC_CSR_20_35M;
179 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
180 priv->clk_csr = STMMAC_CSR_35_60M;
181 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
182 priv->clk_csr = STMMAC_CSR_60_100M;
183 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
184 priv->clk_csr = STMMAC_CSR_100_150M;
185 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
186 priv->clk_csr = STMMAC_CSR_150_250M;
187 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
188 priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000189 }
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000190}
191
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700192static void print_pkt(unsigned char *buf, int len)
193{
194 int j;
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200195 pr_debug("len = %d byte, buf addr: 0x%p", len, buf);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700196 for (j = 0; j < len; j++) {
197 if ((j % 16) == 0)
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200198 pr_debug("\n %03x:", j);
199 pr_debug(" %02x", buf[j]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700200 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +0200201 pr_debug("\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700202}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700203
204/* minimum number of free TX descriptors required to wake up TX process */
205#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
206
207static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
208{
209 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
210}
211
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000212/**
213 * stmmac_hw_fix_mac_speed: callback for speed selection
214 * @priv: driver private structure
215 * Description: on some platforms (e.g. ST), some HW system configuraton
216 * registers have to be set according to the link speed negotiated.
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000217 */
218static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
219{
220 struct phy_device *phydev = priv->phydev;
221
222 if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000223 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000224}
225
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000226/**
227 * stmmac_enable_eee_mode: Check and enter in LPI mode
228 * @priv: driver private structure
229 * Description: this function is to verify and enter in LPI mode for EEE.
230 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000231static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
232{
233 /* Check and enter in LPI mode */
234 if ((priv->dirty_tx == priv->cur_tx) &&
235 (priv->tx_path_in_lpi_mode == false))
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500236 priv->hw->mac->set_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000237}
238
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000239/**
240 * stmmac_disable_eee_mode: disable/exit from EEE
241 * @priv: driver private structure
242 * Description: this function is to exit and disable EEE in case of
243 * LPI state is true. This is called by the xmit.
244 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000245void stmmac_disable_eee_mode(struct stmmac_priv *priv)
246{
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500247 priv->hw->mac->reset_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000248 del_timer_sync(&priv->eee_ctrl_timer);
249 priv->tx_path_in_lpi_mode = false;
250}
251
252/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000253 * stmmac_eee_ctrl_timer: EEE TX SW timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000254 * @arg : data hook
255 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000256 * if there is no data transfer and if we are not in LPI state,
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000257 * then MAC Transmitter can be moved to LPI state.
258 */
259static void stmmac_eee_ctrl_timer(unsigned long arg)
260{
261 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
262
263 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200264 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000265}
266
267/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000268 * stmmac_eee_init: init EEE
269 * @priv: driver private structure
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000270 * Description:
271 * If the EEE support has been enabled while configuring the driver,
272 * if the GMAC actually supports the EEE (from the HW cap reg) and the
273 * phy can also manage EEE, so enable the LPI state and start the timer
274 * to verify if the tx path can enter in LPI state.
275 */
276bool stmmac_eee_init(struct stmmac_priv *priv)
277{
278 bool ret = false;
279
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200280 /* Using PCS we cannot dial with the phy registers at this stage
281 * so we do not support extra feature like EEE.
282 */
283 if ((priv->pcs == STMMAC_PCS_RGMII) || (priv->pcs == STMMAC_PCS_TBI) ||
284 (priv->pcs == STMMAC_PCS_RTBI))
285 goto out;
286
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000287 /* MAC core supports the EEE feature. */
288 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100289 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000290
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100291 /* Check if the PHY supports EEE */
292 if (phy_init_eee(priv->phydev, 1)) {
293 /* To manage at run-time if the EEE cannot be supported
294 * anymore (for example because the lp caps have been
295 * changed).
296 * In that case the driver disable own timers.
297 */
298 if (priv->eee_active) {
299 pr_debug("stmmac: disable EEE\n");
300 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500301 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100302 tx_lpi_timer);
303 }
304 priv->eee_active = 0;
305 goto out;
306 }
307 /* Activate the EEE and start timers */
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200308 if (!priv->eee_active) {
309 priv->eee_active = 1;
310 init_timer(&priv->eee_ctrl_timer);
311 priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
312 priv->eee_ctrl_timer.data = (unsigned long)priv;
313 priv->eee_ctrl_timer.expires = STMMAC_LPI_T(eee_timer);
314 add_timer(&priv->eee_ctrl_timer);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000315
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500316 priv->hw->mac->set_eee_timer(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200317 STMMAC_DEFAULT_LIT_LS,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100318 tx_lpi_timer);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200319 } else
320 /* Set HW EEE according to the speed */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500321 priv->hw->mac->set_eee_pls(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200322 priv->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000323
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100324 pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000325
326 ret = true;
327 }
328out:
329 return ret;
330}
331
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000332/* stmmac_get_tx_hwtstamp: get HW TX timestamps
333 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000334 * @entry : descriptor index to be used.
335 * @skb : the socket buffer
336 * Description :
337 * This function will read timestamp from the descriptor & pass it to stack.
338 * and also perform some sanity checks.
339 */
340static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000341 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000342{
343 struct skb_shared_hwtstamps shhwtstamp;
344 u64 ns;
345 void *desc = NULL;
346
347 if (!priv->hwts_tx_en)
348 return;
349
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000350 /* exit if skb doesn't support hw tstamp */
damuzi00075e43642014-01-17 23:47:59 +0800351 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000352 return;
353
354 if (priv->adv_ts)
355 desc = (priv->dma_etx + entry);
356 else
357 desc = (priv->dma_tx + entry);
358
359 /* check tx tstamp status */
360 if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
361 return;
362
363 /* get the valid tstamp */
364 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
365
366 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
367 shhwtstamp.hwtstamp = ns_to_ktime(ns);
368 /* pass tstamp to stack */
369 skb_tstamp_tx(skb, &shhwtstamp);
370
371 return;
372}
373
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000374/* stmmac_get_rx_hwtstamp: get HW RX timestamps
375 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000376 * @entry : descriptor index to be used.
377 * @skb : the socket buffer
378 * Description :
379 * This function will read received packet's timestamp from the descriptor
380 * and pass it to stack. It also perform some sanity checks.
381 */
382static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000383 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000384{
385 struct skb_shared_hwtstamps *shhwtstamp = NULL;
386 u64 ns;
387 void *desc = NULL;
388
389 if (!priv->hwts_rx_en)
390 return;
391
392 if (priv->adv_ts)
393 desc = (priv->dma_erx + entry);
394 else
395 desc = (priv->dma_rx + entry);
396
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000397 /* exit if rx tstamp is not valid */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000398 if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
399 return;
400
401 /* get valid tstamp */
402 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
403 shhwtstamp = skb_hwtstamps(skb);
404 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
405 shhwtstamp->hwtstamp = ns_to_ktime(ns);
406}
407
408/**
409 * stmmac_hwtstamp_ioctl - control hardware timestamping.
410 * @dev: device pointer.
411 * @ifr: An IOCTL specefic structure, that can contain a pointer to
412 * a proprietary structure used to pass information to the driver.
413 * Description:
414 * This function configures the MAC to enable/disable both outgoing(TX)
415 * and incoming(RX) packets time stamping based on user input.
416 * Return Value:
417 * 0 on success and an appropriate -ve integer on failure.
418 */
419static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
420{
421 struct stmmac_priv *priv = netdev_priv(dev);
422 struct hwtstamp_config config;
423 struct timespec now;
424 u64 temp = 0;
425 u32 ptp_v2 = 0;
426 u32 tstamp_all = 0;
427 u32 ptp_over_ipv4_udp = 0;
428 u32 ptp_over_ipv6_udp = 0;
429 u32 ptp_over_ethernet = 0;
430 u32 snap_type_sel = 0;
431 u32 ts_master_en = 0;
432 u32 ts_event_en = 0;
433 u32 value = 0;
434
435 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
436 netdev_alert(priv->dev, "No support for HW time stamping\n");
437 priv->hwts_tx_en = 0;
438 priv->hwts_rx_en = 0;
439
440 return -EOPNOTSUPP;
441 }
442
443 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000444 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000445 return -EFAULT;
446
447 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
448 __func__, config.flags, config.tx_type, config.rx_filter);
449
450 /* reserved for future extensions */
451 if (config.flags)
452 return -EINVAL;
453
Ben Hutchings5f3da322013-11-14 00:43:41 +0000454 if (config.tx_type != HWTSTAMP_TX_OFF &&
455 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000456 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000457
458 if (priv->adv_ts) {
459 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000460 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000461 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000462 config.rx_filter = HWTSTAMP_FILTER_NONE;
463 break;
464
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000465 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000466 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000467 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
468 /* take time stamp for all event messages */
469 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
470
471 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
472 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
473 break;
474
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000475 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000476 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000477 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
478 /* take time stamp for SYNC messages only */
479 ts_event_en = PTP_TCR_TSEVNTENA;
480
481 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
482 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
483 break;
484
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000485 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000486 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000487 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
488 /* take time stamp for Delay_Req messages only */
489 ts_master_en = PTP_TCR_TSMSTRENA;
490 ts_event_en = PTP_TCR_TSEVNTENA;
491
492 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
493 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
494 break;
495
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000496 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000497 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000498 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
499 ptp_v2 = PTP_TCR_TSVER2ENA;
500 /* take time stamp for all event messages */
501 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
502
503 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
504 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
505 break;
506
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000507 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000508 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000509 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
510 ptp_v2 = PTP_TCR_TSVER2ENA;
511 /* take time stamp for SYNC messages only */
512 ts_event_en = PTP_TCR_TSEVNTENA;
513
514 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
515 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
516 break;
517
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000518 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000519 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000520 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
521 ptp_v2 = PTP_TCR_TSVER2ENA;
522 /* take time stamp for Delay_Req messages only */
523 ts_master_en = PTP_TCR_TSMSTRENA;
524 ts_event_en = PTP_TCR_TSEVNTENA;
525
526 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
527 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
528 break;
529
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000530 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000531 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000532 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
533 ptp_v2 = PTP_TCR_TSVER2ENA;
534 /* take time stamp for all event messages */
535 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
536
537 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
538 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
539 ptp_over_ethernet = PTP_TCR_TSIPENA;
540 break;
541
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000542 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000543 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000544 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
545 ptp_v2 = PTP_TCR_TSVER2ENA;
546 /* take time stamp for SYNC messages only */
547 ts_event_en = PTP_TCR_TSEVNTENA;
548
549 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
550 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
551 ptp_over_ethernet = PTP_TCR_TSIPENA;
552 break;
553
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000554 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000555 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000556 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
557 ptp_v2 = PTP_TCR_TSVER2ENA;
558 /* take time stamp for Delay_Req messages only */
559 ts_master_en = PTP_TCR_TSMSTRENA;
560 ts_event_en = PTP_TCR_TSEVNTENA;
561
562 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
563 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
564 ptp_over_ethernet = PTP_TCR_TSIPENA;
565 break;
566
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000567 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000568 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000569 config.rx_filter = HWTSTAMP_FILTER_ALL;
570 tstamp_all = PTP_TCR_TSENALL;
571 break;
572
573 default:
574 return -ERANGE;
575 }
576 } else {
577 switch (config.rx_filter) {
578 case HWTSTAMP_FILTER_NONE:
579 config.rx_filter = HWTSTAMP_FILTER_NONE;
580 break;
581 default:
582 /* PTP v1, UDP, any kind of event packet */
583 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
584 break;
585 }
586 }
587 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000588 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000589
590 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
591 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
592 else {
593 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000594 tstamp_all | ptp_v2 | ptp_over_ethernet |
595 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
596 ts_master_en | snap_type_sel);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000597
598 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
599
600 /* program Sub Second Increment reg */
601 priv->hw->ptp->config_sub_second_increment(priv->ioaddr);
602
603 /* calculate default added value:
604 * formula is :
605 * addend = (2^32)/freq_div_ratio;
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200606 * where, freq_div_ratio = clk_ptp_ref_i/50MHz
607 * hence, addend = ((2^32) * 50MHz)/clk_ptp_ref_i;
608 * NOTE: clk_ptp_ref_i should be >= 50MHz to
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000609 * achive 20ns accuracy.
610 *
611 * 2^x * y == (y << x), hence
612 * 2^32 * 50000000 ==> (50000000 << 32)
613 */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000614 temp = (u64) (50000000ULL << 32);
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200615 priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000616 priv->hw->ptp->config_addend(priv->ioaddr,
617 priv->default_addend);
618
619 /* initialize system time */
620 getnstimeofday(&now);
621 priv->hw->ptp->init_systime(priv->ioaddr, now.tv_sec,
622 now.tv_nsec);
623 }
624
625 return copy_to_user(ifr->ifr_data, &config,
626 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
627}
628
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000629/**
630 * stmmac_init_ptp: init PTP
631 * @priv: driver private structure
632 * Description: this is to verify if the HW supports the PTPv1 or v2.
633 * This is done by looking at the HW cap. register.
634 * Also it registers the ptp driver.
635 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000636static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000637{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000638 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
639 return -EOPNOTSUPP;
640
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200641 /* Fall-back to main clock in case of no PTP ref is passed */
642 priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
643 if (IS_ERR(priv->clk_ptp_ref)) {
644 priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
645 priv->clk_ptp_ref = NULL;
646 } else {
647 clk_prepare_enable(priv->clk_ptp_ref);
648 priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
649 }
650
Vince Bridgers7cd01392013-12-20 11:19:34 -0600651 priv->adv_ts = 0;
652 if (priv->dma_cap.atime_stamp && priv->extend_desc)
653 priv->adv_ts = 1;
654
655 if (netif_msg_hw(priv) && priv->dma_cap.time_stamp)
656 pr_debug("IEEE 1588-2002 Time Stamp supported\n");
657
658 if (netif_msg_hw(priv) && priv->adv_ts)
659 pr_debug("IEEE 1588-2008 Advanced Time Stamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000660
661 priv->hw->ptp = &stmmac_ptp;
662 priv->hwts_tx_en = 0;
663 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000664
665 return stmmac_ptp_register(priv);
666}
667
668static void stmmac_release_ptp(struct stmmac_priv *priv)
669{
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200670 if (priv->clk_ptp_ref)
671 clk_disable_unprepare(priv->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000672 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000673}
674
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700675/**
676 * stmmac_adjust_link
677 * @dev: net device structure
678 * Description: it adjusts the link parameters.
679 */
680static void stmmac_adjust_link(struct net_device *dev)
681{
682 struct stmmac_priv *priv = netdev_priv(dev);
683 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700684 unsigned long flags;
685 int new_state = 0;
686 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
687
688 if (phydev == NULL)
689 return;
690
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700691 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000692
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700693 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000694 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700695
696 /* Now we make sure that we can be in full duplex mode.
697 * If not, we operate in half-duplex mode. */
698 if (phydev->duplex != priv->oldduplex) {
699 new_state = 1;
700 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000701 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700702 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000703 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700704 priv->oldduplex = phydev->duplex;
705 }
706 /* Flow Control operation */
707 if (phydev->pause)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500708 priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000709 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700710
711 if (phydev->speed != priv->speed) {
712 new_state = 1;
713 switch (phydev->speed) {
714 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000715 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000716 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000717 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700718 break;
719 case 100:
720 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000721 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000722 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700723 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000724 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700725 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000726 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700727 }
728 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000729 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700730 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000731 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700732 break;
733 default:
734 if (netif_msg_link(priv))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000735 pr_warn("%s: Speed (%d) not 10/100\n",
736 dev->name, phydev->speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700737 break;
738 }
739
740 priv->speed = phydev->speed;
741 }
742
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000743 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700744
745 if (!priv->oldlink) {
746 new_state = 1;
747 priv->oldlink = 1;
748 }
749 } else if (priv->oldlink) {
750 new_state = 1;
751 priv->oldlink = 0;
752 priv->speed = 0;
753 priv->oldduplex = -1;
754 }
755
756 if (new_state && netif_msg_link(priv))
757 phy_print_status(phydev);
758
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200759 /* At this stage, it could be needed to setup the EEE or adjust some
760 * MAC related HW registers.
761 */
762 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000763
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700764 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700765}
766
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000767/**
768 * stmmac_check_pcs_mode: verify if RGMII/SGMII is supported
769 * @priv: driver private structure
770 * Description: this is to verify if the HW supports the PCS.
771 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
772 * configured for the TBI, RTBI, or SGMII PHY interface.
773 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000774static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
775{
776 int interface = priv->plat->interface;
777
778 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900779 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
780 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
781 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
782 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000783 pr_debug("STMMAC: PCS RGMII support enable\n");
784 priv->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900785 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000786 pr_debug("STMMAC: PCS SGMII support enable\n");
787 priv->pcs = STMMAC_PCS_SGMII;
788 }
789 }
790}
791
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700792/**
793 * stmmac_init_phy - PHY initialization
794 * @dev: net device structure
795 * Description: it initializes the driver's PHY state, and attaches the PHY
796 * to the mac driver.
797 * Return value:
798 * 0 on success
799 */
800static int stmmac_init_phy(struct net_device *dev)
801{
802 struct stmmac_priv *priv = netdev_priv(dev);
803 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000804 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000805 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000806 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000807 int max_speed = priv->plat->max_speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700808 priv->oldlink = 0;
809 priv->speed = 0;
810 priv->oldduplex = -1;
811
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000812 if (priv->plat->phy_bus_name)
813 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000814 priv->plat->phy_bus_name, priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000815 else
816 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000817 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000818
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000819 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000820 priv->plat->phy_addr);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000821 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700822
Florian Fainellif9a8f832013-01-14 00:52:52 +0000823 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700824
825 if (IS_ERR(phydev)) {
826 pr_err("%s: Could not attach to PHY\n", dev->name);
827 return PTR_ERR(phydev);
828 }
829
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000830 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000831 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000832 (interface == PHY_INTERFACE_MODE_RMII) ||
833 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000834 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
835 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000836
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700837 /*
838 * Broken HW is sometimes missing the pull-up resistor on the
839 * MDIO line, which results in reads to non-existent devices returning
840 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
841 * device as well.
842 * Note: phydev->phy_id is the result of reading the UID PHY registers.
843 */
844 if (phydev->phy_id == 0) {
845 phy_disconnect(phydev);
846 return -ENODEV;
847 }
848 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000849 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700850
851 priv->phydev = phydev;
852
853 return 0;
854}
855
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700856/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000857 * stmmac_display_ring: display ring
858 * @head: pointer to the head of the ring passed.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700859 * @size: size of the ring.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000860 * @extend_desc: to verify if extended descriptors are used.
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000861 * Description: display the control/status and buffer descriptors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700862 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000863static void stmmac_display_ring(void *head, int size, int extend_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700864{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700865 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000866 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
867 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000868
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700869 for (i = 0; i < size; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000870 u64 x;
871 if (extend_desc) {
872 x = *(u64 *) ep;
873 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000874 i, (unsigned int)virt_to_phys(ep),
875 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000876 ep->basic.des2, ep->basic.des3);
877 ep++;
878 } else {
879 x = *(u64 *) p;
880 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000881 i, (unsigned int)virt_to_phys(p),
882 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000883 p->des2, p->des3);
884 p++;
885 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700886 pr_info("\n");
887 }
888}
889
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000890static void stmmac_display_rings(struct stmmac_priv *priv)
891{
892 unsigned int txsize = priv->dma_tx_size;
893 unsigned int rxsize = priv->dma_rx_size;
894
895 if (priv->extend_desc) {
896 pr_info("Extended RX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000897 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000898 pr_info("Extended TX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000899 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000900 } else {
901 pr_info("RX descriptor ring:\n");
902 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
903 pr_info("TX descriptor ring:\n");
904 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
905 }
906}
907
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000908static int stmmac_set_bfsize(int mtu, int bufsize)
909{
910 int ret = bufsize;
911
912 if (mtu >= BUF_SIZE_4KiB)
913 ret = BUF_SIZE_8KiB;
914 else if (mtu >= BUF_SIZE_2KiB)
915 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100916 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000917 ret = BUF_SIZE_2KiB;
918 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100919 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000920
921 return ret;
922}
923
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000924/**
925 * stmmac_clear_descriptors: clear descriptors
926 * @priv: driver private structure
927 * Description: this function is called to clear the tx and rx descriptors
928 * in case of both basic and extended descriptors are used.
929 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000930static void stmmac_clear_descriptors(struct stmmac_priv *priv)
931{
932 int i;
933 unsigned int txsize = priv->dma_tx_size;
934 unsigned int rxsize = priv->dma_rx_size;
935
936 /* Clear the Rx/Tx descriptors */
937 for (i = 0; i < rxsize; i++)
938 if (priv->extend_desc)
939 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
940 priv->use_riwt, priv->mode,
941 (i == rxsize - 1));
942 else
943 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
944 priv->use_riwt, priv->mode,
945 (i == rxsize - 1));
946 for (i = 0; i < txsize; i++)
947 if (priv->extend_desc)
948 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
949 priv->mode,
950 (i == txsize - 1));
951 else
952 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
953 priv->mode,
954 (i == txsize - 1));
955}
956
957static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
958 int i)
959{
960 struct sk_buff *skb;
961
962 skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
963 GFP_KERNEL);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200964 if (!skb) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000965 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200966 return -ENOMEM;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000967 }
968 skb_reserve(skb, NET_IP_ALIGN);
969 priv->rx_skbuff[i] = skb;
970 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
971 priv->dma_buf_sz,
972 DMA_FROM_DEVICE);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200973 if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
974 pr_err("%s: DMA mapping error\n", __func__);
975 dev_kfree_skb_any(skb);
976 return -EINVAL;
977 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000978
979 p->des2 = priv->rx_skbuff_dma[i];
980
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +0100981 if ((priv->hw->mode->init_desc3) &&
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000982 (priv->dma_buf_sz == BUF_SIZE_16KiB))
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +0100983 priv->hw->mode->init_desc3(p);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000984
985 return 0;
986}
987
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200988static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
989{
990 if (priv->rx_skbuff[i]) {
991 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
992 priv->dma_buf_sz, DMA_FROM_DEVICE);
993 dev_kfree_skb_any(priv->rx_skbuff[i]);
994 }
995 priv->rx_skbuff[i] = NULL;
996}
997
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700998/**
999 * init_dma_desc_rings - init the RX/TX descriptor rings
1000 * @dev: net device structure
1001 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001002 * and allocates the socket buffers. It suppors the chained and ring
1003 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001004 */
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001005static int init_dma_desc_rings(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001006{
1007 int i;
1008 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001009 unsigned int txsize = priv->dma_tx_size;
1010 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001011 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001012 int ret = -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001013
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001014 if (priv->hw->mode->set_16kib_bfsize)
1015 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001016
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001017 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001018 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001019
Vince Bridgers2618abb2014-01-20 05:39:01 -06001020 priv->dma_buf_sz = bfsize;
1021
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001022 if (netif_msg_probe(priv))
1023 pr_debug("%s: txsize %d, rxsize %d, bfsize %d\n", __func__,
1024 txsize, rxsize, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001025
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001026 if (netif_msg_probe(priv)) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001027 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1028 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001029
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001030 /* RX INITIALIZATION */
1031 pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
1032 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001033 for (i = 0; i < rxsize; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001034 struct dma_desc *p;
1035 if (priv->extend_desc)
1036 p = &((priv->dma_erx + i)->basic);
1037 else
1038 p = priv->dma_rx + i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001039
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001040 ret = stmmac_init_rx_buffers(priv, p, i);
1041 if (ret)
1042 goto err_init_rx_buffers;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001043
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001044 if (netif_msg_probe(priv))
1045 pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1046 priv->rx_skbuff[i]->data,
1047 (unsigned int)priv->rx_skbuff_dma[i]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001048 }
1049 priv->cur_rx = 0;
1050 priv->dirty_rx = (unsigned int)(i - rxsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001051 buf_sz = bfsize;
1052
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001053 /* Setup the chained descriptor addresses */
1054 if (priv->mode == STMMAC_CHAIN_MODE) {
1055 if (priv->extend_desc) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001056 priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
1057 rxsize, 1);
1058 priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
1059 txsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001060 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001061 priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
1062 rxsize, 0);
1063 priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
1064 txsize, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001065 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001066 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001067
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001068 /* TX INITIALIZATION */
1069 for (i = 0; i < txsize; i++) {
1070 struct dma_desc *p;
1071 if (priv->extend_desc)
1072 p = &((priv->dma_etx + i)->basic);
1073 else
1074 p = priv->dma_tx + i;
1075 p->des2 = 0;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001076 priv->tx_skbuff_dma[i] = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001077 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001078 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001079
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001080 priv->dirty_tx = 0;
1081 priv->cur_tx = 0;
1082
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001083 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001084
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001085 if (netif_msg_hw(priv))
1086 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001087
1088 return 0;
1089err_init_rx_buffers:
1090 while (--i >= 0)
1091 stmmac_free_rx_buffers(priv, i);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001092 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001093}
1094
1095static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1096{
1097 int i;
1098
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001099 for (i = 0; i < priv->dma_rx_size; i++)
1100 stmmac_free_rx_buffers(priv, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001101}
1102
1103static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1104{
1105 int i;
1106
1107 for (i = 0; i < priv->dma_tx_size; i++) {
damuzi00075e43642014-01-17 23:47:59 +08001108 struct dma_desc *p;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001109
damuzi00075e43642014-01-17 23:47:59 +08001110 if (priv->extend_desc)
1111 p = &((priv->dma_etx + i)->basic);
1112 else
1113 p = priv->dma_tx + i;
1114
1115 if (priv->tx_skbuff_dma[i]) {
1116 dma_unmap_single(priv->device,
1117 priv->tx_skbuff_dma[i],
1118 priv->hw->desc->get_tx_len(p),
1119 DMA_TO_DEVICE);
1120 priv->tx_skbuff_dma[i] = 0;
1121 }
1122
1123 if (priv->tx_skbuff[i] != NULL) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001124 dev_kfree_skb_any(priv->tx_skbuff[i]);
1125 priv->tx_skbuff[i] = NULL;
1126 }
1127 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001128}
1129
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001130static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1131{
1132 unsigned int txsize = priv->dma_tx_size;
1133 unsigned int rxsize = priv->dma_rx_size;
1134 int ret = -ENOMEM;
1135
1136 priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
1137 GFP_KERNEL);
1138 if (!priv->rx_skbuff_dma)
1139 return -ENOMEM;
1140
1141 priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
1142 GFP_KERNEL);
1143 if (!priv->rx_skbuff)
1144 goto err_rx_skbuff;
1145
1146 priv->tx_skbuff_dma = kmalloc_array(txsize, sizeof(dma_addr_t),
1147 GFP_KERNEL);
1148 if (!priv->tx_skbuff_dma)
1149 goto err_tx_skbuff_dma;
1150
1151 priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
1152 GFP_KERNEL);
1153 if (!priv->tx_skbuff)
1154 goto err_tx_skbuff;
1155
1156 if (priv->extend_desc) {
1157 priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
1158 sizeof(struct
1159 dma_extended_desc),
1160 &priv->dma_rx_phy,
1161 GFP_KERNEL);
1162 if (!priv->dma_erx)
1163 goto err_dma;
1164
1165 priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
1166 sizeof(struct
1167 dma_extended_desc),
1168 &priv->dma_tx_phy,
1169 GFP_KERNEL);
1170 if (!priv->dma_etx) {
1171 dma_free_coherent(priv->device, priv->dma_rx_size *
1172 sizeof(struct dma_extended_desc),
1173 priv->dma_erx, priv->dma_rx_phy);
1174 goto err_dma;
1175 }
1176 } else {
1177 priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
1178 sizeof(struct dma_desc),
1179 &priv->dma_rx_phy,
1180 GFP_KERNEL);
1181 if (!priv->dma_rx)
1182 goto err_dma;
1183
1184 priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
1185 sizeof(struct dma_desc),
1186 &priv->dma_tx_phy,
1187 GFP_KERNEL);
1188 if (!priv->dma_tx) {
1189 dma_free_coherent(priv->device, priv->dma_rx_size *
1190 sizeof(struct dma_desc),
1191 priv->dma_rx, priv->dma_rx_phy);
1192 goto err_dma;
1193 }
1194 }
1195
1196 return 0;
1197
1198err_dma:
1199 kfree(priv->tx_skbuff);
1200err_tx_skbuff:
1201 kfree(priv->tx_skbuff_dma);
1202err_tx_skbuff_dma:
1203 kfree(priv->rx_skbuff);
1204err_rx_skbuff:
1205 kfree(priv->rx_skbuff_dma);
1206 return ret;
1207}
1208
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001209static void free_dma_desc_resources(struct stmmac_priv *priv)
1210{
1211 /* Release the DMA TX/RX socket buffers */
1212 dma_free_rx_skbufs(priv);
1213 dma_free_tx_skbufs(priv);
1214
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001215 /* Free DMA regions of consistent memory previously allocated */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001216 if (!priv->extend_desc) {
1217 dma_free_coherent(priv->device,
1218 priv->dma_tx_size * sizeof(struct dma_desc),
1219 priv->dma_tx, priv->dma_tx_phy);
1220 dma_free_coherent(priv->device,
1221 priv->dma_rx_size * sizeof(struct dma_desc),
1222 priv->dma_rx, priv->dma_rx_phy);
1223 } else {
1224 dma_free_coherent(priv->device, priv->dma_tx_size *
1225 sizeof(struct dma_extended_desc),
1226 priv->dma_etx, priv->dma_tx_phy);
1227 dma_free_coherent(priv->device, priv->dma_rx_size *
1228 sizeof(struct dma_extended_desc),
1229 priv->dma_erx, priv->dma_rx_phy);
1230 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001231 kfree(priv->rx_skbuff_dma);
1232 kfree(priv->rx_skbuff);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001233 kfree(priv->tx_skbuff_dma);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001234 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001235}
1236
1237/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001238 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001239 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001240 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001241 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001242 */
1243static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1244{
Sonic Zhange2a240c2013-08-28 18:55:39 +08001245 if (priv->plat->force_thresh_dma_mode)
1246 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc);
1247 else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001248 /*
1249 * In case of GMAC, SF mode can be enabled
1250 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001251 * 1) TX COE if actually supported
1252 * 2) There is no bugged Jumbo frame support
1253 * that needs to not insert csum in the TDES.
1254 */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001255 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE);
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001256 tc = SF_DMA_MODE;
1257 } else
1258 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001259}
1260
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001261/**
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001262 * stmmac_tx_clean:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001263 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001264 * Description: it reclaims resources after transmission completes.
1265 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001266static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001267{
1268 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001269
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001270 spin_lock(&priv->tx_lock);
1271
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001272 priv->xstats.tx_clean++;
1273
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001274 while (priv->dirty_tx != priv->cur_tx) {
1275 int last;
1276 unsigned int entry = priv->dirty_tx % txsize;
1277 struct sk_buff *skb = priv->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001278 struct dma_desc *p;
1279
1280 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001281 p = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001282 else
1283 p = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001284
1285 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001286 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001287 break;
1288
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001289 /* Verify tx error by looking at the last segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001290 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001291 if (likely(last)) {
1292 int tx_error =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001293 priv->hw->desc->tx_status(&priv->dev->stats,
1294 &priv->xstats, p,
1295 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001296 if (likely(tx_error == 0)) {
1297 priv->dev->stats.tx_packets++;
1298 priv->xstats.tx_pkt_n++;
1299 } else
1300 priv->dev->stats.tx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001301
1302 stmmac_get_tx_hwtstamp(priv, entry, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001303 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001304 if (netif_msg_tx_done(priv))
1305 pr_debug("%s: curr %d, dirty %d\n", __func__,
1306 priv->cur_tx, priv->dirty_tx);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001307
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001308 if (likely(priv->tx_skbuff_dma[entry])) {
1309 dma_unmap_single(priv->device,
1310 priv->tx_skbuff_dma[entry],
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001311 priv->hw->desc->get_tx_len(p),
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001312 DMA_TO_DEVICE);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001313 priv->tx_skbuff_dma[entry] = 0;
1314 }
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001315 priv->hw->mode->clean_desc3(priv, p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001316
1317 if (likely(skb != NULL)) {
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001318 dev_consume_skb_any(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001319 priv->tx_skbuff[entry] = NULL;
1320 }
1321
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001322 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001323
Giuseppe CAVALLARO13497f52012-06-04 06:36:22 +00001324 priv->dirty_tx++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001325 }
1326 if (unlikely(netif_queue_stopped(priv->dev) &&
1327 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
1328 netif_tx_lock(priv->dev);
1329 if (netif_queue_stopped(priv->dev) &&
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001330 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001331 if (netif_msg_tx_done(priv))
1332 pr_debug("%s: restart transmit\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001333 netif_wake_queue(priv->dev);
1334 }
1335 netif_tx_unlock(priv->dev);
1336 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001337
1338 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1339 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001340 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001341 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001342 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001343}
1344
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001345static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001346{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001347 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001348}
1349
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001350static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001351{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001352 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001353}
1354
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001355/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001356 * stmmac_tx_err: irq tx error mng function
1357 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001358 * Description: it cleans the descriptors and restarts the transmission
1359 * in case of errors.
1360 */
1361static void stmmac_tx_err(struct stmmac_priv *priv)
1362{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001363 int i;
1364 int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001365 netif_stop_queue(priv->dev);
1366
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001367 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001368 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001369 for (i = 0; i < txsize; i++)
1370 if (priv->extend_desc)
1371 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1372 priv->mode,
1373 (i == txsize - 1));
1374 else
1375 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1376 priv->mode,
1377 (i == txsize - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001378 priv->dirty_tx = 0;
1379 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001380 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001381
1382 priv->dev->stats.tx_errors++;
1383 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001384}
1385
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001386/**
1387 * stmmac_dma_interrupt: DMA ISR
1388 * @priv: driver private structure
1389 * Description: this is the DMA ISR. It is called by the main ISR.
1390 * It calls the dwmac dma routine to understand which type of interrupt
1391 * happened. In case of there is a Normal interrupt and either TX or RX
1392 * interrupt happened so the NAPI is scheduled.
1393 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001394static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001395{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001396 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001397
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001398 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001399 if (likely((status & handle_rx)) || (status & handle_tx)) {
1400 if (likely(napi_schedule_prep(&priv->napi))) {
1401 stmmac_disable_dma_irq(priv);
1402 __napi_schedule(&priv->napi);
1403 }
1404 }
1405 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001406 /* Try to bump up the dma threshold on this failure */
1407 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
1408 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001409 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001410 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001411 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001412 } else if (unlikely(status == tx_hard_error))
1413 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001414}
1415
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001416/**
1417 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1418 * @priv: driver private structure
1419 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1420 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001421static void stmmac_mmc_setup(struct stmmac_priv *priv)
1422{
1423 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001424 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001425
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001426 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001427
1428 if (priv->dma_cap.rmon) {
1429 dwmac_mmc_ctrl(priv->ioaddr, mode);
1430 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1431 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +00001432 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001433}
1434
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001435static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
1436{
1437 u32 hwid = priv->hw->synopsys_uid;
1438
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001439 /* Check Synopsys Id (not available on old chips) */
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001440 if (likely(hwid)) {
1441 u32 uid = ((hwid & 0x0000ff00) >> 8);
1442 u32 synid = (hwid & 0x000000ff);
1443
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001444 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001445 uid, synid);
1446
1447 return synid;
1448 }
1449 return 0;
1450}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001451
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001452/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001453 * stmmac_selec_desc_mode: to select among: normal/alternate/extend descriptors
1454 * @priv: driver private structure
1455 * Description: select the Enhanced/Alternate or Normal descriptors.
1456 * In case of Enhanced/Alternate, it looks at the extended descriptors are
1457 * supported by the HW cap. register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001458 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001459static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1460{
1461 if (priv->plat->enh_desc) {
1462 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001463
1464 /* GMAC older than 3.50 has no extended descriptors */
1465 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1466 pr_info("\tEnabled extended descriptors\n");
1467 priv->extend_desc = 1;
1468 } else
1469 pr_warn("Extended descriptors not supported\n");
1470
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001471 priv->hw->desc = &enh_desc_ops;
1472 } else {
1473 pr_info(" Normal descriptors\n");
1474 priv->hw->desc = &ndesc_ops;
1475 }
1476}
1477
1478/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001479 * stmmac_get_hw_features: get MAC capabilities from the HW cap. register.
1480 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001481 * Description:
1482 * new GMAC chip generations have a new register to indicate the
1483 * presence of the optional feature/functions.
1484 * This can be also used to override the value passed through the
1485 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001486 */
1487static int stmmac_get_hw_features(struct stmmac_priv *priv)
1488{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001489 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001490
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001491 if (priv->hw->dma->get_hw_feature) {
1492 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001493
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001494 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
1495 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
1496 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
1497 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001498 priv->dma_cap.multi_addr = (hw_cap & DMA_HW_FEAT_ADDMAC) >> 5;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001499 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
1500 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
1501 priv->dma_cap.pmt_remote_wake_up =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001502 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001503 priv->dma_cap.pmt_magic_frame =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001504 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001505 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001506 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001507 /* IEEE 1588-2002 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001508 priv->dma_cap.time_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001509 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
1510 /* IEEE 1588-2008 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001511 priv->dma_cap.atime_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001512 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001513 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001514 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
1515 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001516 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001517 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
1518 priv->dma_cap.rx_coe_type1 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001519 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001520 priv->dma_cap.rx_coe_type2 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001521 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001522 priv->dma_cap.rxfifo_over_2048 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001523 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001524 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001525 priv->dma_cap.number_rx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001526 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001527 priv->dma_cap.number_tx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001528 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
1529 /* Alternate (enhanced) DESC mode */
1530 priv->dma_cap.enh_desc = (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001531 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001532
1533 return hw_cap;
1534}
1535
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001536/**
1537 * stmmac_check_ether_addr: check if the MAC addr is valid
1538 * @priv: driver private structure
1539 * Description:
1540 * it is to verify if the MAC address is valid, in case of failures it
1541 * generates a random MAC address
1542 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001543static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1544{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001545 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001546 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001547 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001548 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001549 eth_hw_addr_random(priv->dev);
Hans de Goedec88460b2014-01-26 15:50:44 +01001550 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1551 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001552 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001553}
1554
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001555/**
1556 * stmmac_init_dma_engine: DMA init.
1557 * @priv: driver private structure
1558 * Description:
1559 * It inits the DMA invoking the specific MAC/GMAC callback.
1560 * Some DMA parameters can be passed from the platform;
1561 * in case of these are not passed a default is kept for the MAC or GMAC.
1562 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001563static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1564{
1565 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001566 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001567 int atds = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001568
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001569 if (priv->plat->dma_cfg) {
1570 pbl = priv->plat->dma_cfg->pbl;
1571 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001572 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001573 burst_len = priv->plat->dma_cfg->burst_len;
1574 }
1575
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001576 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1577 atds = 1;
1578
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001579 return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001580 burst_len, priv->dma_tx_phy,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001581 priv->dma_rx_phy, atds);
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001582}
1583
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001584/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001585 * stmmac_tx_timer: mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001586 * @data: data pointer
1587 * Description:
1588 * This is the timer handler to directly invoke the stmmac_tx_clean.
1589 */
1590static void stmmac_tx_timer(unsigned long data)
1591{
1592 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1593
1594 stmmac_tx_clean(priv);
1595}
1596
1597/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001598 * stmmac_init_tx_coalesce: init tx mitigation options.
1599 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001600 * Description:
1601 * This inits the transmit coalesce parameters: i.e. timer rate,
1602 * timer handler and default threshold used for enabling the
1603 * interrupt on completion bit.
1604 */
1605static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1606{
1607 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1608 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1609 init_timer(&priv->txtimer);
1610 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1611 priv->txtimer.data = (unsigned long)priv;
1612 priv->txtimer.function = stmmac_tx_timer;
1613 add_timer(&priv->txtimer);
1614}
1615
1616/**
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001617 * stmmac_hw_setup: setup mac in a usable state.
1618 * @dev : pointer to the device structure.
1619 * Description:
1620 * This function sets up the ip in a usable state.
1621 * Return value:
1622 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1623 * file on failure.
1624 */
1625static int stmmac_hw_setup(struct net_device *dev)
1626{
1627 struct stmmac_priv *priv = netdev_priv(dev);
1628 int ret;
1629
1630 ret = init_dma_desc_rings(dev);
1631 if (ret < 0) {
1632 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1633 return ret;
1634 }
1635 /* DMA initialization and SW reset */
1636 ret = stmmac_init_dma_engine(priv);
1637 if (ret < 0) {
1638 pr_err("%s: DMA engine initialization failed\n", __func__);
1639 return ret;
1640 }
1641
1642 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001643 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001644
1645 /* If required, perform hw setup of the bus. */
1646 if (priv->plat->bus_setup)
1647 priv->plat->bus_setup(priv->ioaddr);
1648
1649 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001650 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001651
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001652 ret = priv->hw->mac->rx_ipc(priv->hw);
1653 if (!ret) {
1654 pr_warn(" RX IPC Checksum Offload disabled\n");
1655 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
1656 }
1657
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001658 /* Enable the MAC Rx/Tx */
1659 stmmac_set_mac(priv->ioaddr, true);
1660
1661 /* Set the HW DMA mode and the COE */
1662 stmmac_dma_operation_mode(priv);
1663
1664 stmmac_mmc_setup(priv);
1665
1666 ret = stmmac_init_ptp(priv);
Hans de Goede7509edd2014-01-26 15:50:43 +01001667 if (ret && ret != -EOPNOTSUPP)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001668 pr_warn("%s: failed PTP initialisation\n", __func__);
1669
1670#ifdef CONFIG_STMMAC_DEBUG_FS
1671 ret = stmmac_init_fs(dev);
1672 if (ret < 0)
1673 pr_warn("%s: failed debugFS registration\n", __func__);
1674#endif
1675 /* Start the ball rolling... */
1676 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1677 priv->hw->dma->start_tx(priv->ioaddr);
1678 priv->hw->dma->start_rx(priv->ioaddr);
1679
1680 /* Dump DMA/MAC registers */
1681 if (netif_msg_hw(priv)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001682 priv->hw->mac->dump_regs(priv->hw);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001683 priv->hw->dma->dump_regs(priv->ioaddr);
1684 }
1685 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1686
1687 priv->eee_enabled = stmmac_eee_init(priv);
1688
1689 stmmac_init_tx_coalesce(priv);
1690
1691 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1692 priv->rx_riwt = MAX_DMA_RIWT;
1693 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1694 }
1695
1696 if (priv->pcs && priv->hw->mac->ctrl_ane)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001697 priv->hw->mac->ctrl_ane(priv->hw, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001698
1699 return 0;
1700}
1701
1702/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001703 * stmmac_open - open entry point of the driver
1704 * @dev : pointer to the device structure.
1705 * Description:
1706 * This function is the open entry point of the driver.
1707 * Return value:
1708 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1709 * file on failure.
1710 */
1711static int stmmac_open(struct net_device *dev)
1712{
1713 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001714 int ret;
1715
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001716 stmmac_check_ether_addr(priv);
1717
Byungho An4d8f0822013-04-07 17:56:16 +00001718 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1719 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001720 ret = stmmac_init_phy(dev);
1721 if (ret) {
1722 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1723 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02001724 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001725 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001726 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001727
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001728 /* Extra statistics */
1729 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1730 priv->xstats.threshold = tc;
1731
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001732 /* Create and initialize the TX/RX descriptors chains. */
1733 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1734 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1735 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001736
Tobias Klauser7262b7b2014-02-22 13:09:03 +01001737 ret = alloc_dma_desc_resources(priv);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001738 if (ret < 0) {
1739 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1740 goto dma_desc_error;
1741 }
1742
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001743 ret = stmmac_hw_setup(dev);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001744 if (ret < 0) {
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001745 pr_err("%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001746 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001747 }
1748
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001749 if (priv->phydev)
1750 phy_start(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001751
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001752 /* Request the IRQ lines */
1753 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001754 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001755 if (unlikely(ret < 0)) {
1756 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1757 __func__, dev->irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001758 goto init_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001759 }
1760
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001761 /* Request the Wake IRQ in case of another line is used for WoL */
1762 if (priv->wol_irq != dev->irq) {
1763 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1764 IRQF_SHARED, dev->name, dev);
1765 if (unlikely(ret < 0)) {
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001766 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1767 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001768 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001769 }
1770 }
1771
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001772 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001773 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001774 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1775 dev->name, dev);
1776 if (unlikely(ret < 0)) {
1777 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1778 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001779 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001780 }
1781 }
1782
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001783 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001784 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001785
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001786 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001787
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001788lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001789 if (priv->wol_irq != dev->irq)
1790 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001791wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001792 free_irq(dev->irq, dev);
1793
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001794init_error:
1795 free_dma_desc_resources(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001796dma_desc_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001797 if (priv->phydev)
1798 phy_disconnect(priv->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001799
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001800 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001801}
1802
1803/**
1804 * stmmac_release - close entry point of the driver
1805 * @dev : device pointer.
1806 * Description:
1807 * This is the stop entry point of the driver.
1808 */
1809static int stmmac_release(struct net_device *dev)
1810{
1811 struct stmmac_priv *priv = netdev_priv(dev);
1812
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001813 if (priv->eee_enabled)
1814 del_timer_sync(&priv->eee_ctrl_timer);
1815
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001816 /* Stop and disconnect the PHY */
1817 if (priv->phydev) {
1818 phy_stop(priv->phydev);
1819 phy_disconnect(priv->phydev);
1820 priv->phydev = NULL;
1821 }
1822
1823 netif_stop_queue(dev);
1824
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001825 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001826
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001827 del_timer_sync(&priv->txtimer);
1828
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001829 /* Free the IRQ lines */
1830 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001831 if (priv->wol_irq != dev->irq)
1832 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001833 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001834 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001835
1836 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001837 priv->hw->dma->stop_tx(priv->ioaddr);
1838 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001839
1840 /* Release and free the Rx/Tx resources */
1841 free_dma_desc_resources(priv);
1842
avisconti19449bf2010-10-25 18:58:14 +00001843 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001844 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001845
1846 netif_carrier_off(dev);
1847
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001848#ifdef CONFIG_STMMAC_DEBUG_FS
1849 stmmac_exit_fs();
1850#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001851
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00001852 stmmac_release_ptp(priv);
1853
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001854 return 0;
1855}
1856
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001857/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001858 * stmmac_xmit: Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001859 * @skb : the socket buffer
1860 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001861 * Description : this is the tx entry point of the driver.
1862 * It programs the chain or the ring and supports oversized frames
1863 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001864 */
1865static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1866{
1867 struct stmmac_priv *priv = netdev_priv(dev);
1868 unsigned int txsize = priv->dma_tx_size;
1869 unsigned int entry;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001870 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001871 int nfrags = skb_shinfo(skb)->nr_frags;
1872 struct dma_desc *desc, *first;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001873 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001874 unsigned int enh_desc = priv->plat->enh_desc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001875
1876 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1877 if (!netif_queue_stopped(dev)) {
1878 netif_stop_queue(dev);
1879 /* This is a hard error, log it. */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001880 pr_err("%s: Tx Ring full when queue awake\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001881 }
1882 return NETDEV_TX_BUSY;
1883 }
1884
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001885 spin_lock(&priv->tx_lock);
1886
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001887 if (priv->tx_path_in_lpi_mode)
1888 stmmac_disable_eee_mode(priv);
1889
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001890 entry = priv->cur_tx % txsize;
1891
Michał Mirosław5e982f32011-04-09 02:46:55 +00001892 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001893
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001894 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001895 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001896 else
1897 desc = priv->dma_tx + entry;
1898
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001899 first = desc;
1900
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001901 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001902 if (enh_desc)
1903 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
1904
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001905 if (likely(!is_jumbo)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001906 desc->des2 = dma_map_single(priv->device, skb->data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001907 nopaged_len, DMA_TO_DEVICE);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001908 priv->tx_skbuff_dma[entry] = desc->des2;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001909 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001910 csum_insertion, priv->mode);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001911 } else {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001912 desc = first;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001913 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
1914 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001915
1916 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001917 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1918 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001919
damuzi00075e43642014-01-17 23:47:59 +08001920 priv->tx_skbuff[entry] = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001921 entry = (++priv->cur_tx) % txsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001922 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001923 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001924 else
1925 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001926
Ian Campbellf7223802011-09-21 21:53:20 +00001927 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1928 DMA_TO_DEVICE);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001929 priv->tx_skbuff_dma[entry] = desc->des2;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001930 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
1931 priv->mode);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001932 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001933 priv->hw->desc->set_tx_owner(desc);
Deepak Sikri8e839892012-07-08 21:14:45 +00001934 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001935 }
1936
damuzi00075e43642014-01-17 23:47:59 +08001937 priv->tx_skbuff[entry] = skb;
1938
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001939 /* Finalize the latest segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001940 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001941
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001942 wmb();
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001943 /* According to the coalesce parameter the IC bit for the latest
1944 * segment could be reset and the timer re-started to invoke the
1945 * stmmac_tx function. This approach takes care about the fragments.
1946 */
1947 priv->tx_count_frames += nfrags + 1;
1948 if (priv->tx_coal_frames > priv->tx_count_frames) {
1949 priv->hw->desc->clear_tx_ic(desc);
1950 priv->xstats.tx_reset_ic_bit++;
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001951 mod_timer(&priv->txtimer,
1952 STMMAC_COAL_TIMER(priv->tx_coal_timer));
1953 } else
1954 priv->tx_count_frames = 0;
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001955
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001956 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001957 priv->hw->desc->set_tx_owner(first);
Deepak Sikri8e839892012-07-08 21:14:45 +00001958 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001959
1960 priv->cur_tx++;
1961
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001962 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001963 pr_debug("%s: curr %d dirty=%d entry=%d, first=%p, nfrags=%d",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001964 __func__, (priv->cur_tx % txsize),
1965 (priv->dirty_tx % txsize), entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001966
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001967 if (priv->extend_desc)
1968 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
1969 else
1970 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
1971
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001972 pr_debug(">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001973 print_pkt(skb->data, skb->len);
1974 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001975 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001976 if (netif_msg_hw(priv))
1977 pr_debug("%s: stop transmitted packets\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001978 netif_stop_queue(dev);
1979 }
1980
1981 dev->stats.tx_bytes += skb->len;
1982
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001983 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1984 priv->hwts_tx_en)) {
1985 /* declare that device is doing timestamping */
1986 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1987 priv->hw->desc->enable_tx_timestamp(first);
1988 }
1989
1990 if (!priv->hwts_tx_en)
1991 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00001992
Richard Cochran52f64fa2011-06-19 03:31:43 +00001993 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1994
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001995 spin_unlock(&priv->tx_lock);
1996
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001997 return NETDEV_TX_OK;
1998}
1999
Vince Bridgersb9381982014-01-14 13:42:05 -06002000static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2001{
2002 struct ethhdr *ehdr;
2003 u16 vlanid;
2004
2005 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2006 NETIF_F_HW_VLAN_CTAG_RX &&
2007 !__vlan_get_tag(skb, &vlanid)) {
2008 /* pop the vlan tag */
2009 ehdr = (struct ethhdr *)skb->data;
2010 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2011 skb_pull(skb, VLAN_HLEN);
2012 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2013 }
2014}
2015
2016
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002017/**
2018 * stmmac_rx_refill: refill used skb preallocated buffers
2019 * @priv: driver private structure
2020 * Description : this is to reallocate the skb for the reception process
2021 * that is based on zero-copy.
2022 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002023static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2024{
2025 unsigned int rxsize = priv->dma_rx_size;
2026 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002027
2028 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
2029 unsigned int entry = priv->dirty_rx % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002030 struct dma_desc *p;
2031
2032 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002033 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002034 else
2035 p = priv->dma_rx + entry;
2036
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002037 if (likely(priv->rx_skbuff[entry] == NULL)) {
2038 struct sk_buff *skb;
2039
Eric Dumazetacb600d2012-10-05 06:23:55 +00002040 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002041
2042 if (unlikely(skb == NULL))
2043 break;
2044
2045 priv->rx_skbuff[entry] = skb;
2046 priv->rx_skbuff_dma[entry] =
2047 dma_map_single(priv->device, skb->data, bfsize,
2048 DMA_FROM_DEVICE);
2049
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002050 p->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002051
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002052 priv->hw->mode->refill_desc3(priv, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002053
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002054 if (netif_msg_rx_status(priv))
2055 pr_debug("\trefill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002056 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002057 wmb();
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002058 priv->hw->desc->set_rx_owner(p);
Deepak Sikri8e839892012-07-08 21:14:45 +00002059 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002060 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002061}
2062
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002063/**
2064 * stmmac_rx_refill: refill used skb preallocated buffers
2065 * @priv: driver private structure
2066 * @limit: napi bugget.
2067 * Description : this the function called by the napi poll method.
2068 * It gets all the frames inside the ring.
2069 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002070static int stmmac_rx(struct stmmac_priv *priv, int limit)
2071{
2072 unsigned int rxsize = priv->dma_rx_size;
2073 unsigned int entry = priv->cur_rx % rxsize;
2074 unsigned int next_entry;
2075 unsigned int count = 0;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002076 int coe = priv->plat->rx_coe;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002077
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002078 if (netif_msg_rx_status(priv)) {
2079 pr_debug("%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002080 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002081 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002082 else
2083 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002084 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002085 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002086 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002087 struct dma_desc *p;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002088
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002089 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002090 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002091 else
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002092 p = priv->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002093
2094 if (priv->hw->desc->get_rx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002095 break;
2096
2097 count++;
2098
2099 next_entry = (++priv->cur_rx) % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002100 if (priv->extend_desc)
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002101 prefetch(priv->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002102 else
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002103 prefetch(priv->dma_rx + next_entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002104
2105 /* read the status of the incoming frame */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002106 status = priv->hw->desc->rx_status(&priv->dev->stats,
2107 &priv->xstats, p);
2108 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2109 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2110 &priv->xstats,
2111 priv->dma_erx +
2112 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002113 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002114 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002115 if (priv->hwts_rx_en && !priv->extend_desc) {
2116 /* DESC2 & DESC3 will be overwitten by device
2117 * with timestamp value, hence reinitialize
2118 * them in stmmac_rx_refill() function so that
2119 * device can reuse it.
2120 */
2121 priv->rx_skbuff[entry] = NULL;
2122 dma_unmap_single(priv->device,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002123 priv->rx_skbuff_dma[entry],
2124 priv->dma_buf_sz,
2125 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002126 }
2127 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002128 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002129 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002130
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002131 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2132
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002133 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002134 * Type frames (LLC/LLC-SNAP)
2135 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002136 if (unlikely(status != llc_snap))
2137 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002138
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002139 if (netif_msg_rx_status(priv)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002140 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002141 p, entry, p->des2);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002142 if (frame_len > ETH_FRAME_LEN)
2143 pr_debug("\tframe size %d, COE: %d\n",
2144 frame_len, status);
2145 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002146 skb = priv->rx_skbuff[entry];
2147 if (unlikely(!skb)) {
2148 pr_err("%s: Inconsistent Rx descriptor chain\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002149 priv->dev->name);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002150 priv->dev->stats.rx_dropped++;
2151 break;
2152 }
2153 prefetch(skb->data - NET_IP_ALIGN);
2154 priv->rx_skbuff[entry] = NULL;
2155
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002156 stmmac_get_rx_hwtstamp(priv, entry, skb);
2157
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002158 skb_put(skb, frame_len);
2159 dma_unmap_single(priv->device,
2160 priv->rx_skbuff_dma[entry],
2161 priv->dma_buf_sz, DMA_FROM_DEVICE);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002162
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002163 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002164 pr_debug("frame received (%dbytes)", frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002165 print_pkt(skb->data, frame_len);
2166 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002167
Vince Bridgersb9381982014-01-14 13:42:05 -06002168 stmmac_rx_vlan(priv->dev, skb);
2169
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002170 skb->protocol = eth_type_trans(skb, priv->dev);
2171
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002172 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002173 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002174 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002175 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002176
2177 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002178
2179 priv->dev->stats.rx_packets++;
2180 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002181 }
2182 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002183 }
2184
2185 stmmac_rx_refill(priv);
2186
2187 priv->xstats.rx_pkt_n += count;
2188
2189 return count;
2190}
2191
2192/**
2193 * stmmac_poll - stmmac poll method (NAPI)
2194 * @napi : pointer to the napi structure.
2195 * @budget : maximum number of packets that the current CPU can receive from
2196 * all interfaces.
2197 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002198 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002199 */
2200static int stmmac_poll(struct napi_struct *napi, int budget)
2201{
2202 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2203 int work_done = 0;
2204
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002205 priv->xstats.napi_poll++;
2206 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002207
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002208 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002209 if (work_done < budget) {
2210 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002211 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002212 }
2213 return work_done;
2214}
2215
2216/**
2217 * stmmac_tx_timeout
2218 * @dev : Pointer to net device structure
2219 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00002220 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002221 * netdev structure and arrange for the device to be reset to a sane state
2222 * in order to transmit a new packet.
2223 */
2224static void stmmac_tx_timeout(struct net_device *dev)
2225{
2226 struct stmmac_priv *priv = netdev_priv(dev);
2227
2228 /* Clear Tx resources and restart transmitting again */
2229 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002230}
2231
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002232/**
Jiri Pirko01789342011-08-16 06:29:00 +00002233 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002234 * @dev : pointer to the device structure
2235 * Description:
2236 * This function is a driver entry point which gets called by the kernel
2237 * whenever multicast addresses must be enabled/disabled.
2238 * Return value:
2239 * void.
2240 */
Jiri Pirko01789342011-08-16 06:29:00 +00002241static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002242{
2243 struct stmmac_priv *priv = netdev_priv(dev);
2244
2245 spin_lock(&priv->lock);
Vince Bridgers3b57de92014-07-31 15:49:17 -05002246 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002247 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002248}
2249
2250/**
2251 * stmmac_change_mtu - entry point to change MTU size for the device.
2252 * @dev : device pointer.
2253 * @new_mtu : the new MTU size for the device.
2254 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2255 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2256 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2257 * Return value:
2258 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2259 * file on failure.
2260 */
2261static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2262{
2263 struct stmmac_priv *priv = netdev_priv(dev);
2264 int max_mtu;
2265
2266 if (netif_running(dev)) {
2267 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2268 return -EBUSY;
2269 }
2270
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00002271 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002272 max_mtu = JUMBO_LEN;
2273 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00002274 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002275
Vince Bridgers2618abb2014-01-20 05:39:01 -06002276 if (priv->plat->maxmtu < max_mtu)
2277 max_mtu = priv->plat->maxmtu;
2278
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002279 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2280 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2281 return -EINVAL;
2282 }
2283
Michał Mirosław5e982f32011-04-09 02:46:55 +00002284 dev->mtu = new_mtu;
2285 netdev_update_features(dev);
2286
2287 return 0;
2288}
2289
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002290static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002291 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002292{
2293 struct stmmac_priv *priv = netdev_priv(dev);
2294
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002295 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002296 features &= ~NETIF_F_RXCSUM;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002297 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
2298 features &= ~NETIF_F_IPV6_CSUM;
Michał Mirosław5e982f32011-04-09 02:46:55 +00002299 if (!priv->plat->tx_coe)
2300 features &= ~NETIF_F_ALL_CSUM;
2301
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002302 /* Some GMAC devices have a bugged Jumbo frame support that
2303 * needs to have the Tx COE disabled for oversized frames
2304 * (due to limited buffer sizes). In this case we disable
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002305 * the TX csum insertionin the TDES and not use SF.
2306 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00002307 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
2308 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002309
Michał Mirosław5e982f32011-04-09 02:46:55 +00002310 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002311}
2312
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002313/**
2314 * stmmac_interrupt - main ISR
2315 * @irq: interrupt number.
2316 * @dev_id: to pass the net device pointer.
2317 * Description: this is the main driver interrupt service routine.
2318 * It calls the DMA ISR and also the core ISR to manage PMT, MMC, LPI
2319 * interrupts.
2320 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002321static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2322{
2323 struct net_device *dev = (struct net_device *)dev_id;
2324 struct stmmac_priv *priv = netdev_priv(dev);
2325
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002326 if (priv->irq_wake)
2327 pm_wakeup_event(priv->device, 0);
2328
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002329 if (unlikely(!dev)) {
2330 pr_err("%s: invalid dev pointer\n", __func__);
2331 return IRQ_NONE;
2332 }
2333
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002334 /* To handle GMAC own interrupts */
2335 if (priv->plat->has_gmac) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002336 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002337 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002338 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002339 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002340 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002341 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002342 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002343 priv->tx_path_in_lpi_mode = false;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002344 }
2345 }
2346
2347 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002348 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002349
2350 return IRQ_HANDLED;
2351}
2352
2353#ifdef CONFIG_NET_POLL_CONTROLLER
2354/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002355 * to allow network I/O with interrupts disabled.
2356 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002357static void stmmac_poll_controller(struct net_device *dev)
2358{
2359 disable_irq(dev->irq);
2360 stmmac_interrupt(dev->irq, dev);
2361 enable_irq(dev->irq);
2362}
2363#endif
2364
2365/**
2366 * stmmac_ioctl - Entry point for the Ioctl
2367 * @dev: Device pointer.
2368 * @rq: An IOCTL specefic structure, that can contain a pointer to
2369 * a proprietary structure used to pass information to the driver.
2370 * @cmd: IOCTL command
2371 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002372 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002373 */
2374static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2375{
2376 struct stmmac_priv *priv = netdev_priv(dev);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002377 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002378
2379 if (!netif_running(dev))
2380 return -EINVAL;
2381
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002382 switch (cmd) {
2383 case SIOCGMIIPHY:
2384 case SIOCGMIIREG:
2385 case SIOCSMIIREG:
2386 if (!priv->phydev)
2387 return -EINVAL;
2388 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2389 break;
2390 case SIOCSHWTSTAMP:
2391 ret = stmmac_hwtstamp_ioctl(dev, rq);
2392 break;
2393 default:
2394 break;
2395 }
Richard Cochran28b04112010-07-17 08:48:55 +00002396
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002397 return ret;
2398}
2399
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002400#ifdef CONFIG_STMMAC_DEBUG_FS
2401static struct dentry *stmmac_fs_dir;
2402static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002403static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002404
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002405static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002406 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002407{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002408 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002409 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2410 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002411
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002412 for (i = 0; i < size; i++) {
2413 u64 x;
2414 if (extend_desc) {
2415 x = *(u64 *) ep;
2416 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002417 i, (unsigned int)virt_to_phys(ep),
2418 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002419 ep->basic.des2, ep->basic.des3);
2420 ep++;
2421 } else {
2422 x = *(u64 *) p;
2423 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002424 i, (unsigned int)virt_to_phys(ep),
2425 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002426 p->des2, p->des3);
2427 p++;
2428 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002429 seq_printf(seq, "\n");
2430 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002431}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002432
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002433static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2434{
2435 struct net_device *dev = seq->private;
2436 struct stmmac_priv *priv = netdev_priv(dev);
2437 unsigned int txsize = priv->dma_tx_size;
2438 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002439
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002440 if (priv->extend_desc) {
2441 seq_printf(seq, "Extended RX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002442 sysfs_display_ring((void *)priv->dma_erx, rxsize, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002443 seq_printf(seq, "Extended TX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002444 sysfs_display_ring((void *)priv->dma_etx, txsize, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002445 } else {
2446 seq_printf(seq, "RX descriptor ring:\n");
2447 sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
2448 seq_printf(seq, "TX descriptor ring:\n");
2449 sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002450 }
2451
2452 return 0;
2453}
2454
2455static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2456{
2457 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2458}
2459
2460static const struct file_operations stmmac_rings_status_fops = {
2461 .owner = THIS_MODULE,
2462 .open = stmmac_sysfs_ring_open,
2463 .read = seq_read,
2464 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002465 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002466};
2467
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002468static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2469{
2470 struct net_device *dev = seq->private;
2471 struct stmmac_priv *priv = netdev_priv(dev);
2472
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002473 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002474 seq_printf(seq, "DMA HW features not supported\n");
2475 return 0;
2476 }
2477
2478 seq_printf(seq, "==============================\n");
2479 seq_printf(seq, "\tDMA HW features\n");
2480 seq_printf(seq, "==============================\n");
2481
2482 seq_printf(seq, "\t10/100 Mbps %s\n",
2483 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2484 seq_printf(seq, "\t1000 Mbps %s\n",
2485 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2486 seq_printf(seq, "\tHalf duple %s\n",
2487 (priv->dma_cap.half_duplex) ? "Y" : "N");
2488 seq_printf(seq, "\tHash Filter: %s\n",
2489 (priv->dma_cap.hash_filter) ? "Y" : "N");
2490 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2491 (priv->dma_cap.multi_addr) ? "Y" : "N");
2492 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2493 (priv->dma_cap.pcs) ? "Y" : "N");
2494 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2495 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2496 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2497 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2498 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2499 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2500 seq_printf(seq, "\tRMON module: %s\n",
2501 (priv->dma_cap.rmon) ? "Y" : "N");
2502 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2503 (priv->dma_cap.time_stamp) ? "Y" : "N");
2504 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2505 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2506 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2507 (priv->dma_cap.eee) ? "Y" : "N");
2508 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2509 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2510 (priv->dma_cap.tx_coe) ? "Y" : "N");
2511 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2512 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2513 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2514 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2515 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2516 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2517 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2518 priv->dma_cap.number_rx_channel);
2519 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2520 priv->dma_cap.number_tx_channel);
2521 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2522 (priv->dma_cap.enh_desc) ? "Y" : "N");
2523
2524 return 0;
2525}
2526
2527static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2528{
2529 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2530}
2531
2532static const struct file_operations stmmac_dma_cap_fops = {
2533 .owner = THIS_MODULE,
2534 .open = stmmac_sysfs_dma_cap_open,
2535 .read = seq_read,
2536 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002537 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002538};
2539
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002540static int stmmac_init_fs(struct net_device *dev)
2541{
2542 /* Create debugfs entries */
2543 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
2544
2545 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
2546 pr_err("ERROR %s, debugfs create directory failed\n",
2547 STMMAC_RESOURCE_NAME);
2548
2549 return -ENOMEM;
2550 }
2551
2552 /* Entry to report DMA RX/TX rings */
2553 stmmac_rings_status = debugfs_create_file("descriptors_status",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002554 S_IRUGO, stmmac_fs_dir, dev,
2555 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002556
2557 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
2558 pr_info("ERROR creating stmmac ring debugfs file\n");
2559 debugfs_remove(stmmac_fs_dir);
2560
2561 return -ENOMEM;
2562 }
2563
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002564 /* Entry to report the DMA HW features */
2565 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
2566 dev, &stmmac_dma_cap_fops);
2567
2568 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
2569 pr_info("ERROR creating stmmac MMC debugfs file\n");
2570 debugfs_remove(stmmac_rings_status);
2571 debugfs_remove(stmmac_fs_dir);
2572
2573 return -ENOMEM;
2574 }
2575
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002576 return 0;
2577}
2578
2579static void stmmac_exit_fs(void)
2580{
2581 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002582 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002583 debugfs_remove(stmmac_fs_dir);
2584}
2585#endif /* CONFIG_STMMAC_DEBUG_FS */
2586
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002587static const struct net_device_ops stmmac_netdev_ops = {
2588 .ndo_open = stmmac_open,
2589 .ndo_start_xmit = stmmac_xmit,
2590 .ndo_stop = stmmac_release,
2591 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00002592 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00002593 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002594 .ndo_tx_timeout = stmmac_tx_timeout,
2595 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002596#ifdef CONFIG_NET_POLL_CONTROLLER
2597 .ndo_poll_controller = stmmac_poll_controller,
2598#endif
2599 .ndo_set_mac_address = eth_mac_addr,
2600};
2601
2602/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002603 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002604 * @priv: driver private structure
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002605 * Description: this function detects which MAC device
2606 * (GMAC/MAC10-100) has to attached, checks the HW capability
2607 * (if supported) and sets the driver's features (for example
2608 * to use the ring or chaine mode or support the normal/enh
2609 * descriptor structure).
2610 */
2611static int stmmac_hw_init(struct stmmac_priv *priv)
2612{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002613 struct mac_device_info *mac;
2614
2615 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002616 if (priv->plat->has_gmac) {
2617 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05002618 mac = dwmac1000_setup(priv->ioaddr,
2619 priv->plat->multicast_filter_bins,
2620 priv->plat->unicast_filter_entries);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002621 } else {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002622 mac = dwmac100_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002623 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002624 if (!mac)
2625 return -ENOMEM;
2626
2627 priv->hw = mac;
2628
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002629 /* Get and dump the chip ID */
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00002630 priv->synopsys_id = stmmac_get_synopsys_id(priv);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002631
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002632 /* To use the chained or ring mode */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002633 if (chain_mode) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002634 priv->hw->mode = &chain_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002635 pr_info(" Chain mode enabled\n");
2636 priv->mode = STMMAC_CHAIN_MODE;
2637 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002638 priv->hw->mode = &ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002639 pr_info(" Ring mode enabled\n");
2640 priv->mode = STMMAC_RING_MODE;
2641 }
2642
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002643 /* Get the HW capability (new GMAC newer than 3.50a) */
2644 priv->hw_cap_support = stmmac_get_hw_features(priv);
2645 if (priv->hw_cap_support) {
2646 pr_info(" DMA HW capability register supported");
2647
2648 /* We can override some gmac/dma configuration fields: e.g.
2649 * enh_desc, tx_coe (e.g. that are passed through the
2650 * platform) with the values from the HW capability
2651 * register (if supported).
2652 */
2653 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002654 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002655
2656 priv->plat->tx_coe = priv->dma_cap.tx_coe;
2657
2658 if (priv->dma_cap.rx_coe_type2)
2659 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2660 else if (priv->dma_cap.rx_coe_type1)
2661 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2662
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002663 } else
2664 pr_info(" No HW DMA feature register supported");
2665
Byungho An61369d02013-06-28 16:35:32 +09002666 /* To use alternate (extended) or normal descriptor structures */
2667 stmmac_selec_desc_mode(priv);
2668
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002669 if (priv->plat->rx_coe)
2670 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2671 priv->plat->rx_coe);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002672 if (priv->plat->tx_coe)
2673 pr_info(" TX Checksum insertion supported\n");
2674
2675 if (priv->plat->pmt) {
2676 pr_info(" Wake-Up On Lan supported\n");
2677 device_set_wakeup_capable(priv->device, 1);
2678 }
2679
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002680 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002681}
2682
2683/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002684 * stmmac_dvr_probe
2685 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002686 * @plat_dat: platform data pointer
2687 * @addr: iobase memory address
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002688 * Description: this is the main probe function used to
2689 * call the alloc_etherdev, allocate the priv structure.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002690 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002691struct stmmac_priv *stmmac_dvr_probe(struct device *device,
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002692 struct plat_stmmacenet_data *plat_dat,
2693 void __iomem *addr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002694{
2695 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002696 struct net_device *ndev = NULL;
2697 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002698
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002699 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002700 if (!ndev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002701 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002702
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002703 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002704
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002705 priv = netdev_priv(ndev);
2706 priv->device = device;
2707 priv->dev = ndev;
2708
2709 ether_setup(ndev);
2710
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002711 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002712 priv->pause = pause;
2713 priv->plat = plat_dat;
2714 priv->ioaddr = addr;
2715 priv->dev->base_addr = (unsigned long)addr;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002716
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002717 /* Verify driver arguments */
2718 stmmac_verify_args();
2719
2720 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002721 * this needs to have multiple instances
2722 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002723 if ((phyaddr >= 0) && (phyaddr <= 31))
2724 priv->plat->phy_addr = phyaddr;
2725
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002726 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
2727 if (IS_ERR(priv->stmmac_clk)) {
2728 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
2729 __func__);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002730 ret = PTR_ERR(priv->stmmac_clk);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002731 goto error_clk_get;
2732 }
2733 clk_prepare_enable(priv->stmmac_clk);
2734
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002735 priv->stmmac_rst = devm_reset_control_get(priv->device,
2736 STMMAC_RESOURCE_NAME);
2737 if (IS_ERR(priv->stmmac_rst)) {
2738 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
2739 ret = -EPROBE_DEFER;
2740 goto error_hw_init;
2741 }
2742 dev_info(priv->device, "no reset control found\n");
2743 priv->stmmac_rst = NULL;
2744 }
2745 if (priv->stmmac_rst)
2746 reset_control_deassert(priv->stmmac_rst);
2747
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002748 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002749 ret = stmmac_hw_init(priv);
2750 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002751 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002752
2753 ndev->netdev_ops = &stmmac_netdev_ops;
2754
2755 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2756 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002757 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2758 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002759#ifdef STMMAC_VLAN_TAG_USED
2760 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00002761 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002762#endif
2763 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2764
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002765 if (flow_ctrl)
2766 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2767
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002768 /* Rx Watchdog is available in the COREs newer than the 3.40.
2769 * In some case, for example on bugged HW this feature
2770 * has to be disable and this can be done by passing the
2771 * riwt_off field from the platform.
2772 */
2773 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2774 priv->use_riwt = 1;
2775 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2776 }
2777
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002778 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002779
Vlad Lunguf8e96162010-11-29 22:52:52 +00002780 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002781 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002782
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002783 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002784 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002785 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002786 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002787 }
2788
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002789 /* If a specific clk_csr value is passed from the platform
2790 * this means that the CSR Clock Range selection cannot be
2791 * changed at run-time and it is fixed. Viceversa the driver'll try to
2792 * set the MDC clock dynamically according to the csr actual
2793 * clock input.
2794 */
2795 if (!priv->plat->clk_csr)
2796 stmmac_clk_csr_set(priv);
2797 else
2798 priv->clk_csr = priv->plat->clk_csr;
2799
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002800 stmmac_check_pcs_mode(priv);
2801
Byungho An4d8f0822013-04-07 17:56:16 +00002802 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2803 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002804 /* MDIO bus Registration */
2805 ret = stmmac_mdio_register(ndev);
2806 if (ret < 0) {
2807 pr_debug("%s: MDIO bus (id: %d) registration failed",
2808 __func__, priv->plat->bus_id);
2809 goto error_mdio_register;
2810 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002811 }
2812
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002813 return priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002814
Viresh Kumar6a81c262012-07-30 14:39:41 -07002815error_mdio_register:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002816 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002817error_netdev_register:
2818 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002819error_hw_init:
2820 clk_disable_unprepare(priv->stmmac_clk);
2821error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002822 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002823
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002824 return ERR_PTR(ret);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002825}
2826
2827/**
2828 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002829 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002830 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002831 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002832 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002833int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002834{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002835 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002836
2837 pr_info("%s:\n\tremoving driver", __func__);
2838
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002839 priv->hw->dma->stop_rx(priv->ioaddr);
2840 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002841
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002842 stmmac_set_mac(priv->ioaddr, false);
Byungho An4d8f0822013-04-07 17:56:16 +00002843 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2844 priv->pcs != STMMAC_PCS_RTBI)
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002845 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002846 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002847 unregister_netdev(ndev);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002848 if (priv->stmmac_rst)
2849 reset_control_assert(priv->stmmac_rst);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002850 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002851 free_netdev(ndev);
2852
2853 return 0;
2854}
2855
2856#ifdef CONFIG_PM
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002857int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002858{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002859 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002860 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002861
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002862 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002863 return 0;
2864
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002865 if (priv->phydev)
2866 phy_stop(priv->phydev);
2867
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002868 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002869
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002870 netif_device_detach(ndev);
2871 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002872
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002873 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002874
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002875 /* Stop TX/RX DMA */
2876 priv->hw->dma->stop_tx(priv->ioaddr);
2877 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002878
2879 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002880
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002881 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002882 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002883 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002884 priv->irq_wake = 1;
2885 } else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002886 stmmac_set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00002887 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002888 /* Disable clock in case of PWM is off */
Stefan Roesea6308442012-09-21 01:06:29 +00002889 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002890 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002891 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05002892
2893 priv->oldlink = 0;
2894 priv->speed = 0;
2895 priv->oldduplex = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002896 return 0;
2897}
2898
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002899int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002900{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002901 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002902 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002903
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002904 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002905 return 0;
2906
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002907 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02002908
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002909 /* Power Down bit, into the PM register, is cleared
2910 * automatically as soon as a magic packet or a Wake-up frame
2911 * is received. Anyway, it's better to manually clear
2912 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002913 * from another devices (e.g. serial console).
2914 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002915 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002916 priv->hw->mac->pmt(priv->hw, 0);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002917 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002918 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00002919 pinctrl_pm_select_default_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002920 /* enable the clk prevously disabled */
Stefan Roesea6308442012-09-21 01:06:29 +00002921 clk_prepare_enable(priv->stmmac_clk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002922 /* reset the phy so that it's ready */
2923 if (priv->mii)
2924 stmmac_mdio_reset(priv->mii);
2925 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002926
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002927 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002928
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002929 stmmac_hw_setup(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002930
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002931 napi_enable(&priv->napi);
2932
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002933 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002934
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002935 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002936
2937 if (priv->phydev)
2938 phy_start(priv->phydev);
2939
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002940 return 0;
2941}
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002942#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002943
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002944/* Driver can be configured w/ and w/ both PCI and Platf drivers
2945 * depending on the configuration selected.
2946 */
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002947static int __init stmmac_init(void)
2948{
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002949 int ret;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002950
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002951 ret = stmmac_register_platform();
2952 if (ret)
2953 goto err;
2954 ret = stmmac_register_pci();
2955 if (ret)
2956 goto err_pci;
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002957 return 0;
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002958err_pci:
2959 stmmac_unregister_platform();
2960err:
2961 pr_err("stmmac: driver registration failed\n");
2962 return ret;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002963}
2964
2965static void __exit stmmac_exit(void)
2966{
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002967 stmmac_unregister_platform();
2968 stmmac_unregister_pci();
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002969}
2970
2971module_init(stmmac_init);
2972module_exit(stmmac_exit);
2973
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002974#ifndef MODULE
2975static int __init stmmac_cmdline_opt(char *str)
2976{
2977 char *opt;
2978
2979 if (!str || !*str)
2980 return -EINVAL;
2981 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002982 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002983 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002984 goto err;
2985 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002986 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002987 goto err;
2988 } else if (!strncmp(opt, "dma_txsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002989 if (kstrtoint(opt + 11, 0, &dma_txsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002990 goto err;
2991 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002992 if (kstrtoint(opt + 11, 0, &dma_rxsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002993 goto err;
2994 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002995 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002996 goto err;
2997 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00002998 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00002999 goto err;
3000 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003001 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003002 goto err;
3003 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003004 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003005 goto err;
3006 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003007 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003008 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00003009 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003010 if (kstrtoint(opt + 10, 0, &eee_timer))
3011 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003012 } else if (!strncmp(opt, "chain_mode:", 11)) {
3013 if (kstrtoint(opt + 11, 0, &chain_mode))
3014 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003015 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003016 }
3017 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003018
3019err:
3020 pr_err("%s: ERROR broken module parameter conversion", __func__);
3021 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003022}
3023
3024__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003025#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003026
3027MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3028MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3029MODULE_LICENSE("GPL");