blob: 3b8b57e3bd208d0a8edc00f0e867688a8b38fcd5 [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{
Giuseppe CAVALLARO56b88c22014-08-28 08:11:43 +0200278 char *phy_bus_name = priv->plat->phy_bus_name;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000279 bool ret = false;
280
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200281 /* Using PCS we cannot dial with the phy registers at this stage
282 * so we do not support extra feature like EEE.
283 */
284 if ((priv->pcs == STMMAC_PCS_RGMII) || (priv->pcs == STMMAC_PCS_TBI) ||
285 (priv->pcs == STMMAC_PCS_RTBI))
286 goto out;
287
Giuseppe CAVALLARO56b88c22014-08-28 08:11:43 +0200288 /* Never init EEE in case of a switch is attached */
289 if (phy_bus_name && (!strcmp(phy_bus_name, "fixed")))
290 goto out;
291
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000292 /* MAC core supports the EEE feature. */
293 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100294 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000295
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100296 /* Check if the PHY supports EEE */
297 if (phy_init_eee(priv->phydev, 1)) {
298 /* To manage at run-time if the EEE cannot be supported
299 * anymore (for example because the lp caps have been
300 * changed).
301 * In that case the driver disable own timers.
302 */
303 if (priv->eee_active) {
304 pr_debug("stmmac: disable EEE\n");
305 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500306 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100307 tx_lpi_timer);
308 }
309 priv->eee_active = 0;
310 goto out;
311 }
312 /* Activate the EEE and start timers */
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200313 if (!priv->eee_active) {
314 priv->eee_active = 1;
315 init_timer(&priv->eee_ctrl_timer);
316 priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
317 priv->eee_ctrl_timer.data = (unsigned long)priv;
318 priv->eee_ctrl_timer.expires = STMMAC_LPI_T(eee_timer);
319 add_timer(&priv->eee_ctrl_timer);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000320
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500321 priv->hw->mac->set_eee_timer(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200322 STMMAC_DEFAULT_LIT_LS,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100323 tx_lpi_timer);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200324 } else
325 /* Set HW EEE according to the speed */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500326 priv->hw->mac->set_eee_pls(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200327 priv->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000328
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100329 pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000330
331 ret = true;
332 }
333out:
334 return ret;
335}
336
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000337/* stmmac_get_tx_hwtstamp: get HW TX timestamps
338 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000339 * @entry : descriptor index to be used.
340 * @skb : the socket buffer
341 * Description :
342 * This function will read timestamp from the descriptor & pass it to stack.
343 * and also perform some sanity checks.
344 */
345static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000346 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000347{
348 struct skb_shared_hwtstamps shhwtstamp;
349 u64 ns;
350 void *desc = NULL;
351
352 if (!priv->hwts_tx_en)
353 return;
354
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000355 /* exit if skb doesn't support hw tstamp */
damuzi00075e43642014-01-17 23:47:59 +0800356 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000357 return;
358
359 if (priv->adv_ts)
360 desc = (priv->dma_etx + entry);
361 else
362 desc = (priv->dma_tx + entry);
363
364 /* check tx tstamp status */
365 if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
366 return;
367
368 /* get the valid tstamp */
369 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
370
371 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
372 shhwtstamp.hwtstamp = ns_to_ktime(ns);
373 /* pass tstamp to stack */
374 skb_tstamp_tx(skb, &shhwtstamp);
375
376 return;
377}
378
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000379/* stmmac_get_rx_hwtstamp: get HW RX timestamps
380 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000381 * @entry : descriptor index to be used.
382 * @skb : the socket buffer
383 * Description :
384 * This function will read received packet's timestamp from the descriptor
385 * and pass it to stack. It also perform some sanity checks.
386 */
387static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000388 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000389{
390 struct skb_shared_hwtstamps *shhwtstamp = NULL;
391 u64 ns;
392 void *desc = NULL;
393
394 if (!priv->hwts_rx_en)
395 return;
396
397 if (priv->adv_ts)
398 desc = (priv->dma_erx + entry);
399 else
400 desc = (priv->dma_rx + entry);
401
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000402 /* exit if rx tstamp is not valid */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000403 if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
404 return;
405
406 /* get valid tstamp */
407 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
408 shhwtstamp = skb_hwtstamps(skb);
409 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
410 shhwtstamp->hwtstamp = ns_to_ktime(ns);
411}
412
413/**
414 * stmmac_hwtstamp_ioctl - control hardware timestamping.
415 * @dev: device pointer.
416 * @ifr: An IOCTL specefic structure, that can contain a pointer to
417 * a proprietary structure used to pass information to the driver.
418 * Description:
419 * This function configures the MAC to enable/disable both outgoing(TX)
420 * and incoming(RX) packets time stamping based on user input.
421 * Return Value:
422 * 0 on success and an appropriate -ve integer on failure.
423 */
424static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
425{
426 struct stmmac_priv *priv = netdev_priv(dev);
427 struct hwtstamp_config config;
428 struct timespec now;
429 u64 temp = 0;
430 u32 ptp_v2 = 0;
431 u32 tstamp_all = 0;
432 u32 ptp_over_ipv4_udp = 0;
433 u32 ptp_over_ipv6_udp = 0;
434 u32 ptp_over_ethernet = 0;
435 u32 snap_type_sel = 0;
436 u32 ts_master_en = 0;
437 u32 ts_event_en = 0;
438 u32 value = 0;
439
440 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
441 netdev_alert(priv->dev, "No support for HW time stamping\n");
442 priv->hwts_tx_en = 0;
443 priv->hwts_rx_en = 0;
444
445 return -EOPNOTSUPP;
446 }
447
448 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000449 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000450 return -EFAULT;
451
452 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
453 __func__, config.flags, config.tx_type, config.rx_filter);
454
455 /* reserved for future extensions */
456 if (config.flags)
457 return -EINVAL;
458
Ben Hutchings5f3da322013-11-14 00:43:41 +0000459 if (config.tx_type != HWTSTAMP_TX_OFF &&
460 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000461 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000462
463 if (priv->adv_ts) {
464 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000465 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000466 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000467 config.rx_filter = HWTSTAMP_FILTER_NONE;
468 break;
469
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000470 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000471 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000472 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
473 /* take time stamp for all event messages */
474 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
475
476 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
477 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
478 break;
479
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000480 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000481 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000482 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
483 /* take time stamp for SYNC messages only */
484 ts_event_en = PTP_TCR_TSEVNTENA;
485
486 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
487 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
488 break;
489
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000490 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000491 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000492 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
493 /* take time stamp for Delay_Req messages only */
494 ts_master_en = PTP_TCR_TSMSTRENA;
495 ts_event_en = PTP_TCR_TSEVNTENA;
496
497 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
498 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
499 break;
500
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000501 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000502 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000503 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
504 ptp_v2 = PTP_TCR_TSVER2ENA;
505 /* take time stamp for all event messages */
506 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
507
508 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
509 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
510 break;
511
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000512 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000513 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000514 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
515 ptp_v2 = PTP_TCR_TSVER2ENA;
516 /* take time stamp for SYNC messages only */
517 ts_event_en = PTP_TCR_TSEVNTENA;
518
519 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
520 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
521 break;
522
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000523 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000524 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000525 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
526 ptp_v2 = PTP_TCR_TSVER2ENA;
527 /* take time stamp for Delay_Req messages only */
528 ts_master_en = PTP_TCR_TSMSTRENA;
529 ts_event_en = PTP_TCR_TSEVNTENA;
530
531 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
532 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
533 break;
534
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000535 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000536 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000537 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
538 ptp_v2 = PTP_TCR_TSVER2ENA;
539 /* take time stamp for all event messages */
540 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
541
542 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
543 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
544 ptp_over_ethernet = PTP_TCR_TSIPENA;
545 break;
546
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000547 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000548 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000549 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
550 ptp_v2 = PTP_TCR_TSVER2ENA;
551 /* take time stamp for SYNC messages only */
552 ts_event_en = PTP_TCR_TSEVNTENA;
553
554 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
555 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
556 ptp_over_ethernet = PTP_TCR_TSIPENA;
557 break;
558
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000559 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000560 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000561 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
562 ptp_v2 = PTP_TCR_TSVER2ENA;
563 /* take time stamp for Delay_Req messages only */
564 ts_master_en = PTP_TCR_TSMSTRENA;
565 ts_event_en = PTP_TCR_TSEVNTENA;
566
567 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
568 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
569 ptp_over_ethernet = PTP_TCR_TSIPENA;
570 break;
571
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000572 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000573 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000574 config.rx_filter = HWTSTAMP_FILTER_ALL;
575 tstamp_all = PTP_TCR_TSENALL;
576 break;
577
578 default:
579 return -ERANGE;
580 }
581 } else {
582 switch (config.rx_filter) {
583 case HWTSTAMP_FILTER_NONE:
584 config.rx_filter = HWTSTAMP_FILTER_NONE;
585 break;
586 default:
587 /* PTP v1, UDP, any kind of event packet */
588 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
589 break;
590 }
591 }
592 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000593 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000594
595 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
596 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
597 else {
598 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000599 tstamp_all | ptp_v2 | ptp_over_ethernet |
600 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
601 ts_master_en | snap_type_sel);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000602
603 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
604
605 /* program Sub Second Increment reg */
606 priv->hw->ptp->config_sub_second_increment(priv->ioaddr);
607
608 /* calculate default added value:
609 * formula is :
610 * addend = (2^32)/freq_div_ratio;
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200611 * where, freq_div_ratio = clk_ptp_ref_i/50MHz
612 * hence, addend = ((2^32) * 50MHz)/clk_ptp_ref_i;
613 * NOTE: clk_ptp_ref_i should be >= 50MHz to
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000614 * achive 20ns accuracy.
615 *
616 * 2^x * y == (y << x), hence
617 * 2^32 * 50000000 ==> (50000000 << 32)
618 */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000619 temp = (u64) (50000000ULL << 32);
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200620 priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000621 priv->hw->ptp->config_addend(priv->ioaddr,
622 priv->default_addend);
623
624 /* initialize system time */
625 getnstimeofday(&now);
626 priv->hw->ptp->init_systime(priv->ioaddr, now.tv_sec,
627 now.tv_nsec);
628 }
629
630 return copy_to_user(ifr->ifr_data, &config,
631 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
632}
633
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000634/**
635 * stmmac_init_ptp: init PTP
636 * @priv: driver private structure
637 * Description: this is to verify if the HW supports the PTPv1 or v2.
638 * This is done by looking at the HW cap. register.
639 * Also it registers the ptp driver.
640 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000641static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000642{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000643 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
644 return -EOPNOTSUPP;
645
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200646 /* Fall-back to main clock in case of no PTP ref is passed */
647 priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
648 if (IS_ERR(priv->clk_ptp_ref)) {
649 priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
650 priv->clk_ptp_ref = NULL;
651 } else {
652 clk_prepare_enable(priv->clk_ptp_ref);
653 priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
654 }
655
Vince Bridgers7cd01392013-12-20 11:19:34 -0600656 priv->adv_ts = 0;
657 if (priv->dma_cap.atime_stamp && priv->extend_desc)
658 priv->adv_ts = 1;
659
660 if (netif_msg_hw(priv) && priv->dma_cap.time_stamp)
661 pr_debug("IEEE 1588-2002 Time Stamp supported\n");
662
663 if (netif_msg_hw(priv) && priv->adv_ts)
664 pr_debug("IEEE 1588-2008 Advanced Time Stamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000665
666 priv->hw->ptp = &stmmac_ptp;
667 priv->hwts_tx_en = 0;
668 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000669
670 return stmmac_ptp_register(priv);
671}
672
673static void stmmac_release_ptp(struct stmmac_priv *priv)
674{
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200675 if (priv->clk_ptp_ref)
676 clk_disable_unprepare(priv->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000677 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000678}
679
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700680/**
681 * stmmac_adjust_link
682 * @dev: net device structure
683 * Description: it adjusts the link parameters.
684 */
685static void stmmac_adjust_link(struct net_device *dev)
686{
687 struct stmmac_priv *priv = netdev_priv(dev);
688 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700689 unsigned long flags;
690 int new_state = 0;
691 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
692
693 if (phydev == NULL)
694 return;
695
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700696 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000697
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700698 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000699 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700700
701 /* Now we make sure that we can be in full duplex mode.
702 * If not, we operate in half-duplex mode. */
703 if (phydev->duplex != priv->oldduplex) {
704 new_state = 1;
705 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000706 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700707 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000708 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700709 priv->oldduplex = phydev->duplex;
710 }
711 /* Flow Control operation */
712 if (phydev->pause)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500713 priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000714 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700715
716 if (phydev->speed != priv->speed) {
717 new_state = 1;
718 switch (phydev->speed) {
719 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000720 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000721 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000722 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700723 break;
724 case 100:
725 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000726 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000727 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700728 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000729 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700730 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000731 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700732 }
733 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000734 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700735 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000736 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700737 break;
738 default:
739 if (netif_msg_link(priv))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000740 pr_warn("%s: Speed (%d) not 10/100\n",
741 dev->name, phydev->speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700742 break;
743 }
744
745 priv->speed = phydev->speed;
746 }
747
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000748 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700749
750 if (!priv->oldlink) {
751 new_state = 1;
752 priv->oldlink = 1;
753 }
754 } else if (priv->oldlink) {
755 new_state = 1;
756 priv->oldlink = 0;
757 priv->speed = 0;
758 priv->oldduplex = -1;
759 }
760
761 if (new_state && netif_msg_link(priv))
762 phy_print_status(phydev);
763
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200764 /* At this stage, it could be needed to setup the EEE or adjust some
765 * MAC related HW registers.
766 */
767 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000768
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700769 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700770}
771
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000772/**
773 * stmmac_check_pcs_mode: verify if RGMII/SGMII is supported
774 * @priv: driver private structure
775 * Description: this is to verify if the HW supports the PCS.
776 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
777 * configured for the TBI, RTBI, or SGMII PHY interface.
778 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000779static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
780{
781 int interface = priv->plat->interface;
782
783 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900784 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
785 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
786 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
787 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000788 pr_debug("STMMAC: PCS RGMII support enable\n");
789 priv->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900790 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000791 pr_debug("STMMAC: PCS SGMII support enable\n");
792 priv->pcs = STMMAC_PCS_SGMII;
793 }
794 }
795}
796
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700797/**
798 * stmmac_init_phy - PHY initialization
799 * @dev: net device structure
800 * Description: it initializes the driver's PHY state, and attaches the PHY
801 * to the mac driver.
802 * Return value:
803 * 0 on success
804 */
805static int stmmac_init_phy(struct net_device *dev)
806{
807 struct stmmac_priv *priv = netdev_priv(dev);
808 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000809 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000810 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000811 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000812 int max_speed = priv->plat->max_speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700813 priv->oldlink = 0;
814 priv->speed = 0;
815 priv->oldduplex = -1;
816
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000817 if (priv->plat->phy_bus_name)
818 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000819 priv->plat->phy_bus_name, priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000820 else
821 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000822 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000823
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000824 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000825 priv->plat->phy_addr);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000826 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700827
Florian Fainellif9a8f832013-01-14 00:52:52 +0000828 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700829
830 if (IS_ERR(phydev)) {
831 pr_err("%s: Could not attach to PHY\n", dev->name);
832 return PTR_ERR(phydev);
833 }
834
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000835 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000836 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000837 (interface == PHY_INTERFACE_MODE_RMII) ||
838 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000839 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
840 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000841
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700842 /*
843 * Broken HW is sometimes missing the pull-up resistor on the
844 * MDIO line, which results in reads to non-existent devices returning
845 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
846 * device as well.
847 * Note: phydev->phy_id is the result of reading the UID PHY registers.
848 */
849 if (phydev->phy_id == 0) {
850 phy_disconnect(phydev);
851 return -ENODEV;
852 }
853 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000854 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700855
856 priv->phydev = phydev;
857
858 return 0;
859}
860
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700861/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000862 * stmmac_display_ring: display ring
863 * @head: pointer to the head of the ring passed.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700864 * @size: size of the ring.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000865 * @extend_desc: to verify if extended descriptors are used.
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000866 * Description: display the control/status and buffer descriptors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700867 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000868static void stmmac_display_ring(void *head, int size, int extend_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700869{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700870 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000871 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
872 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000873
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700874 for (i = 0; i < size; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000875 u64 x;
876 if (extend_desc) {
877 x = *(u64 *) ep;
878 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000879 i, (unsigned int)virt_to_phys(ep),
880 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000881 ep->basic.des2, ep->basic.des3);
882 ep++;
883 } else {
884 x = *(u64 *) p;
885 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000886 i, (unsigned int)virt_to_phys(p),
887 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000888 p->des2, p->des3);
889 p++;
890 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700891 pr_info("\n");
892 }
893}
894
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000895static void stmmac_display_rings(struct stmmac_priv *priv)
896{
897 unsigned int txsize = priv->dma_tx_size;
898 unsigned int rxsize = priv->dma_rx_size;
899
900 if (priv->extend_desc) {
901 pr_info("Extended RX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000902 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000903 pr_info("Extended TX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000904 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000905 } else {
906 pr_info("RX descriptor ring:\n");
907 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
908 pr_info("TX descriptor ring:\n");
909 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
910 }
911}
912
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000913static int stmmac_set_bfsize(int mtu, int bufsize)
914{
915 int ret = bufsize;
916
917 if (mtu >= BUF_SIZE_4KiB)
918 ret = BUF_SIZE_8KiB;
919 else if (mtu >= BUF_SIZE_2KiB)
920 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100921 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000922 ret = BUF_SIZE_2KiB;
923 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100924 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000925
926 return ret;
927}
928
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000929/**
930 * stmmac_clear_descriptors: clear descriptors
931 * @priv: driver private structure
932 * Description: this function is called to clear the tx and rx descriptors
933 * in case of both basic and extended descriptors are used.
934 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000935static void stmmac_clear_descriptors(struct stmmac_priv *priv)
936{
937 int i;
938 unsigned int txsize = priv->dma_tx_size;
939 unsigned int rxsize = priv->dma_rx_size;
940
941 /* Clear the Rx/Tx descriptors */
942 for (i = 0; i < rxsize; i++)
943 if (priv->extend_desc)
944 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
945 priv->use_riwt, priv->mode,
946 (i == rxsize - 1));
947 else
948 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
949 priv->use_riwt, priv->mode,
950 (i == rxsize - 1));
951 for (i = 0; i < txsize; i++)
952 if (priv->extend_desc)
953 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
954 priv->mode,
955 (i == txsize - 1));
956 else
957 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
958 priv->mode,
959 (i == txsize - 1));
960}
961
962static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
963 int i)
964{
965 struct sk_buff *skb;
966
967 skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
968 GFP_KERNEL);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200969 if (!skb) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000970 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200971 return -ENOMEM;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000972 }
973 skb_reserve(skb, NET_IP_ALIGN);
974 priv->rx_skbuff[i] = skb;
975 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
976 priv->dma_buf_sz,
977 DMA_FROM_DEVICE);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200978 if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
979 pr_err("%s: DMA mapping error\n", __func__);
980 dev_kfree_skb_any(skb);
981 return -EINVAL;
982 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000983
984 p->des2 = priv->rx_skbuff_dma[i];
985
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +0100986 if ((priv->hw->mode->init_desc3) &&
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000987 (priv->dma_buf_sz == BUF_SIZE_16KiB))
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +0100988 priv->hw->mode->init_desc3(p);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000989
990 return 0;
991}
992
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200993static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
994{
995 if (priv->rx_skbuff[i]) {
996 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
997 priv->dma_buf_sz, DMA_FROM_DEVICE);
998 dev_kfree_skb_any(priv->rx_skbuff[i]);
999 }
1000 priv->rx_skbuff[i] = NULL;
1001}
1002
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001003/**
1004 * init_dma_desc_rings - init the RX/TX descriptor rings
1005 * @dev: net device structure
1006 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001007 * and allocates the socket buffers. It suppors the chained and ring
1008 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001009 */
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001010static int init_dma_desc_rings(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001011{
1012 int i;
1013 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001014 unsigned int txsize = priv->dma_tx_size;
1015 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001016 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001017 int ret = -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001018
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001019 if (priv->hw->mode->set_16kib_bfsize)
1020 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001021
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001022 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001023 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001024
Vince Bridgers2618abb2014-01-20 05:39:01 -06001025 priv->dma_buf_sz = bfsize;
1026
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001027 if (netif_msg_probe(priv))
1028 pr_debug("%s: txsize %d, rxsize %d, bfsize %d\n", __func__,
1029 txsize, rxsize, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001030
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001031 if (netif_msg_probe(priv)) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001032 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1033 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001034
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001035 /* RX INITIALIZATION */
1036 pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
1037 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001038 for (i = 0; i < rxsize; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001039 struct dma_desc *p;
1040 if (priv->extend_desc)
1041 p = &((priv->dma_erx + i)->basic);
1042 else
1043 p = priv->dma_rx + i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001044
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001045 ret = stmmac_init_rx_buffers(priv, p, i);
1046 if (ret)
1047 goto err_init_rx_buffers;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001048
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001049 if (netif_msg_probe(priv))
1050 pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1051 priv->rx_skbuff[i]->data,
1052 (unsigned int)priv->rx_skbuff_dma[i]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001053 }
1054 priv->cur_rx = 0;
1055 priv->dirty_rx = (unsigned int)(i - rxsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001056 buf_sz = bfsize;
1057
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001058 /* Setup the chained descriptor addresses */
1059 if (priv->mode == STMMAC_CHAIN_MODE) {
1060 if (priv->extend_desc) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001061 priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
1062 rxsize, 1);
1063 priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
1064 txsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001065 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001066 priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
1067 rxsize, 0);
1068 priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
1069 txsize, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001070 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001071 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001072
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001073 /* TX INITIALIZATION */
1074 for (i = 0; i < txsize; i++) {
1075 struct dma_desc *p;
1076 if (priv->extend_desc)
1077 p = &((priv->dma_etx + i)->basic);
1078 else
1079 p = priv->dma_tx + i;
1080 p->des2 = 0;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001081 priv->tx_skbuff_dma[i].buf = 0;
1082 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001083 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001084 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001085
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001086 priv->dirty_tx = 0;
1087 priv->cur_tx = 0;
1088
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001089 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001090
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001091 if (netif_msg_hw(priv))
1092 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001093
1094 return 0;
1095err_init_rx_buffers:
1096 while (--i >= 0)
1097 stmmac_free_rx_buffers(priv, i);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001098 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001099}
1100
1101static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1102{
1103 int i;
1104
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001105 for (i = 0; i < priv->dma_rx_size; i++)
1106 stmmac_free_rx_buffers(priv, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001107}
1108
1109static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1110{
1111 int i;
1112
1113 for (i = 0; i < priv->dma_tx_size; i++) {
damuzi00075e43642014-01-17 23:47:59 +08001114 struct dma_desc *p;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001115
damuzi00075e43642014-01-17 23:47:59 +08001116 if (priv->extend_desc)
1117 p = &((priv->dma_etx + i)->basic);
1118 else
1119 p = priv->dma_tx + i;
1120
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001121 if (priv->tx_skbuff_dma[i].buf) {
1122 if (priv->tx_skbuff_dma[i].map_as_page)
1123 dma_unmap_page(priv->device,
1124 priv->tx_skbuff_dma[i].buf,
1125 priv->hw->desc->get_tx_len(p),
1126 DMA_TO_DEVICE);
1127 else
1128 dma_unmap_single(priv->device,
1129 priv->tx_skbuff_dma[i].buf,
1130 priv->hw->desc->get_tx_len(p),
1131 DMA_TO_DEVICE);
damuzi00075e43642014-01-17 23:47:59 +08001132 }
1133
1134 if (priv->tx_skbuff[i] != NULL) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001135 dev_kfree_skb_any(priv->tx_skbuff[i]);
1136 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001137 priv->tx_skbuff_dma[i].buf = 0;
1138 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001139 }
1140 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001141}
1142
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001143static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1144{
1145 unsigned int txsize = priv->dma_tx_size;
1146 unsigned int rxsize = priv->dma_rx_size;
1147 int ret = -ENOMEM;
1148
1149 priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
1150 GFP_KERNEL);
1151 if (!priv->rx_skbuff_dma)
1152 return -ENOMEM;
1153
1154 priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
1155 GFP_KERNEL);
1156 if (!priv->rx_skbuff)
1157 goto err_rx_skbuff;
1158
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001159 priv->tx_skbuff_dma = kmalloc_array(txsize,
1160 sizeof(*priv->tx_skbuff_dma),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001161 GFP_KERNEL);
1162 if (!priv->tx_skbuff_dma)
1163 goto err_tx_skbuff_dma;
1164
1165 priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
1166 GFP_KERNEL);
1167 if (!priv->tx_skbuff)
1168 goto err_tx_skbuff;
1169
1170 if (priv->extend_desc) {
1171 priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
1172 sizeof(struct
1173 dma_extended_desc),
1174 &priv->dma_rx_phy,
1175 GFP_KERNEL);
1176 if (!priv->dma_erx)
1177 goto err_dma;
1178
1179 priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
1180 sizeof(struct
1181 dma_extended_desc),
1182 &priv->dma_tx_phy,
1183 GFP_KERNEL);
1184 if (!priv->dma_etx) {
1185 dma_free_coherent(priv->device, priv->dma_rx_size *
1186 sizeof(struct dma_extended_desc),
1187 priv->dma_erx, priv->dma_rx_phy);
1188 goto err_dma;
1189 }
1190 } else {
1191 priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
1192 sizeof(struct dma_desc),
1193 &priv->dma_rx_phy,
1194 GFP_KERNEL);
1195 if (!priv->dma_rx)
1196 goto err_dma;
1197
1198 priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
1199 sizeof(struct dma_desc),
1200 &priv->dma_tx_phy,
1201 GFP_KERNEL);
1202 if (!priv->dma_tx) {
1203 dma_free_coherent(priv->device, priv->dma_rx_size *
1204 sizeof(struct dma_desc),
1205 priv->dma_rx, priv->dma_rx_phy);
1206 goto err_dma;
1207 }
1208 }
1209
1210 return 0;
1211
1212err_dma:
1213 kfree(priv->tx_skbuff);
1214err_tx_skbuff:
1215 kfree(priv->tx_skbuff_dma);
1216err_tx_skbuff_dma:
1217 kfree(priv->rx_skbuff);
1218err_rx_skbuff:
1219 kfree(priv->rx_skbuff_dma);
1220 return ret;
1221}
1222
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001223static void free_dma_desc_resources(struct stmmac_priv *priv)
1224{
1225 /* Release the DMA TX/RX socket buffers */
1226 dma_free_rx_skbufs(priv);
1227 dma_free_tx_skbufs(priv);
1228
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001229 /* Free DMA regions of consistent memory previously allocated */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001230 if (!priv->extend_desc) {
1231 dma_free_coherent(priv->device,
1232 priv->dma_tx_size * sizeof(struct dma_desc),
1233 priv->dma_tx, priv->dma_tx_phy);
1234 dma_free_coherent(priv->device,
1235 priv->dma_rx_size * sizeof(struct dma_desc),
1236 priv->dma_rx, priv->dma_rx_phy);
1237 } else {
1238 dma_free_coherent(priv->device, priv->dma_tx_size *
1239 sizeof(struct dma_extended_desc),
1240 priv->dma_etx, priv->dma_tx_phy);
1241 dma_free_coherent(priv->device, priv->dma_rx_size *
1242 sizeof(struct dma_extended_desc),
1243 priv->dma_erx, priv->dma_rx_phy);
1244 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001245 kfree(priv->rx_skbuff_dma);
1246 kfree(priv->rx_skbuff);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001247 kfree(priv->tx_skbuff_dma);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001248 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001249}
1250
1251/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001252 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001253 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001254 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001255 * or Store-And-Forward capability.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001256 */
1257static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1258{
Sonic Zhange2a240c2013-08-28 18:55:39 +08001259 if (priv->plat->force_thresh_dma_mode)
1260 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc);
1261 else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001262 /*
1263 * In case of GMAC, SF mode can be enabled
1264 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001265 * 1) TX COE if actually supported
1266 * 2) There is no bugged Jumbo frame support
1267 * that needs to not insert csum in the TDES.
1268 */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001269 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE);
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001270 tc = SF_DMA_MODE;
1271 } else
1272 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001273}
1274
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001275/**
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001276 * stmmac_tx_clean:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001277 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001278 * Description: it reclaims resources after transmission completes.
1279 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001280static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001281{
1282 unsigned int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001283
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001284 spin_lock(&priv->tx_lock);
1285
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001286 priv->xstats.tx_clean++;
1287
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001288 while (priv->dirty_tx != priv->cur_tx) {
1289 int last;
1290 unsigned int entry = priv->dirty_tx % txsize;
1291 struct sk_buff *skb = priv->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001292 struct dma_desc *p;
1293
1294 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001295 p = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001296 else
1297 p = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001298
1299 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001300 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001301 break;
1302
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001303 /* Verify tx error by looking at the last segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001304 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001305 if (likely(last)) {
1306 int tx_error =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001307 priv->hw->desc->tx_status(&priv->dev->stats,
1308 &priv->xstats, p,
1309 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001310 if (likely(tx_error == 0)) {
1311 priv->dev->stats.tx_packets++;
1312 priv->xstats.tx_pkt_n++;
1313 } else
1314 priv->dev->stats.tx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001315
1316 stmmac_get_tx_hwtstamp(priv, entry, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001317 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001318 if (netif_msg_tx_done(priv))
1319 pr_debug("%s: curr %d, dirty %d\n", __func__,
1320 priv->cur_tx, priv->dirty_tx);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001321
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001322 if (likely(priv->tx_skbuff_dma[entry].buf)) {
1323 if (priv->tx_skbuff_dma[entry].map_as_page)
1324 dma_unmap_page(priv->device,
1325 priv->tx_skbuff_dma[entry].buf,
1326 priv->hw->desc->get_tx_len(p),
1327 DMA_TO_DEVICE);
1328 else
1329 dma_unmap_single(priv->device,
1330 priv->tx_skbuff_dma[entry].buf,
1331 priv->hw->desc->get_tx_len(p),
1332 DMA_TO_DEVICE);
1333 priv->tx_skbuff_dma[entry].buf = 0;
1334 priv->tx_skbuff_dma[entry].map_as_page = false;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001335 }
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001336 priv->hw->mode->clean_desc3(priv, p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001337
1338 if (likely(skb != NULL)) {
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001339 dev_consume_skb_any(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001340 priv->tx_skbuff[entry] = NULL;
1341 }
1342
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001343 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001344
Giuseppe CAVALLARO13497f52012-06-04 06:36:22 +00001345 priv->dirty_tx++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001346 }
1347 if (unlikely(netif_queue_stopped(priv->dev) &&
1348 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
1349 netif_tx_lock(priv->dev);
1350 if (netif_queue_stopped(priv->dev) &&
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001351 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001352 if (netif_msg_tx_done(priv))
1353 pr_debug("%s: restart transmit\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001354 netif_wake_queue(priv->dev);
1355 }
1356 netif_tx_unlock(priv->dev);
1357 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001358
1359 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1360 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001361 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001362 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001363 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001364}
1365
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001366static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001367{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001368 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001369}
1370
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001371static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001372{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001373 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001374}
1375
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001376/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001377 * stmmac_tx_err: irq tx error mng function
1378 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001379 * Description: it cleans the descriptors and restarts the transmission
1380 * in case of errors.
1381 */
1382static void stmmac_tx_err(struct stmmac_priv *priv)
1383{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001384 int i;
1385 int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001386 netif_stop_queue(priv->dev);
1387
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001388 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001389 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001390 for (i = 0; i < txsize; i++)
1391 if (priv->extend_desc)
1392 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1393 priv->mode,
1394 (i == txsize - 1));
1395 else
1396 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1397 priv->mode,
1398 (i == txsize - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001399 priv->dirty_tx = 0;
1400 priv->cur_tx = 0;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001401 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001402
1403 priv->dev->stats.tx_errors++;
1404 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001405}
1406
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001407/**
1408 * stmmac_dma_interrupt: DMA ISR
1409 * @priv: driver private structure
1410 * Description: this is the DMA ISR. It is called by the main ISR.
1411 * It calls the dwmac dma routine to understand which type of interrupt
1412 * happened. In case of there is a Normal interrupt and either TX or RX
1413 * interrupt happened so the NAPI is scheduled.
1414 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001415static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001416{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001417 int status;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001418
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001419 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001420 if (likely((status & handle_rx)) || (status & handle_tx)) {
1421 if (likely(napi_schedule_prep(&priv->napi))) {
1422 stmmac_disable_dma_irq(priv);
1423 __napi_schedule(&priv->napi);
1424 }
1425 }
1426 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001427 /* Try to bump up the dma threshold on this failure */
1428 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
1429 tc += 64;
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001430 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001431 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001432 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001433 } else if (unlikely(status == tx_hard_error))
1434 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001435}
1436
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001437/**
1438 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1439 * @priv: driver private structure
1440 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1441 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001442static void stmmac_mmc_setup(struct stmmac_priv *priv)
1443{
1444 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001445 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001446
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001447 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001448
1449 if (priv->dma_cap.rmon) {
1450 dwmac_mmc_ctrl(priv->ioaddr, mode);
1451 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1452 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +00001453 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001454}
1455
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001456static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
1457{
1458 u32 hwid = priv->hw->synopsys_uid;
1459
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001460 /* Check Synopsys Id (not available on old chips) */
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001461 if (likely(hwid)) {
1462 u32 uid = ((hwid & 0x0000ff00) >> 8);
1463 u32 synid = (hwid & 0x000000ff);
1464
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001465 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001466 uid, synid);
1467
1468 return synid;
1469 }
1470 return 0;
1471}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001472
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001473/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001474 * stmmac_selec_desc_mode: to select among: normal/alternate/extend descriptors
1475 * @priv: driver private structure
1476 * Description: select the Enhanced/Alternate or Normal descriptors.
1477 * In case of Enhanced/Alternate, it looks at the extended descriptors are
1478 * supported by the HW cap. register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001479 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001480static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1481{
1482 if (priv->plat->enh_desc) {
1483 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001484
1485 /* GMAC older than 3.50 has no extended descriptors */
1486 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1487 pr_info("\tEnabled extended descriptors\n");
1488 priv->extend_desc = 1;
1489 } else
1490 pr_warn("Extended descriptors not supported\n");
1491
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001492 priv->hw->desc = &enh_desc_ops;
1493 } else {
1494 pr_info(" Normal descriptors\n");
1495 priv->hw->desc = &ndesc_ops;
1496 }
1497}
1498
1499/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001500 * stmmac_get_hw_features: get MAC capabilities from the HW cap. register.
1501 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001502 * Description:
1503 * new GMAC chip generations have a new register to indicate the
1504 * presence of the optional feature/functions.
1505 * This can be also used to override the value passed through the
1506 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001507 */
1508static int stmmac_get_hw_features(struct stmmac_priv *priv)
1509{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001510 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001511
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001512 if (priv->hw->dma->get_hw_feature) {
1513 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001514
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001515 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
1516 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
1517 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
1518 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001519 priv->dma_cap.multi_addr = (hw_cap & DMA_HW_FEAT_ADDMAC) >> 5;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001520 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
1521 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
1522 priv->dma_cap.pmt_remote_wake_up =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001523 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001524 priv->dma_cap.pmt_magic_frame =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001525 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001526 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001527 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001528 /* IEEE 1588-2002 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001529 priv->dma_cap.time_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001530 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
1531 /* IEEE 1588-2008 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001532 priv->dma_cap.atime_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001533 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001534 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001535 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
1536 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001537 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001538 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
1539 priv->dma_cap.rx_coe_type1 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001540 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001541 priv->dma_cap.rx_coe_type2 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001542 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001543 priv->dma_cap.rxfifo_over_2048 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001544 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001545 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001546 priv->dma_cap.number_rx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001547 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001548 priv->dma_cap.number_tx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001549 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
1550 /* Alternate (enhanced) DESC mode */
1551 priv->dma_cap.enh_desc = (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001552 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001553
1554 return hw_cap;
1555}
1556
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001557/**
1558 * stmmac_check_ether_addr: check if the MAC addr is valid
1559 * @priv: driver private structure
1560 * Description:
1561 * it is to verify if the MAC address is valid, in case of failures it
1562 * generates a random MAC address
1563 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001564static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1565{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001566 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001567 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001568 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001569 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001570 eth_hw_addr_random(priv->dev);
Hans de Goedec88460b2014-01-26 15:50:44 +01001571 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1572 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001573 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001574}
1575
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001576/**
1577 * stmmac_init_dma_engine: DMA init.
1578 * @priv: driver private structure
1579 * Description:
1580 * It inits the DMA invoking the specific MAC/GMAC callback.
1581 * Some DMA parameters can be passed from the platform;
1582 * in case of these are not passed a default is kept for the MAC or GMAC.
1583 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001584static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1585{
1586 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001587 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001588 int atds = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001589
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001590 if (priv->plat->dma_cfg) {
1591 pbl = priv->plat->dma_cfg->pbl;
1592 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001593 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001594 burst_len = priv->plat->dma_cfg->burst_len;
1595 }
1596
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001597 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1598 atds = 1;
1599
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001600 return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001601 burst_len, priv->dma_tx_phy,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001602 priv->dma_rx_phy, atds);
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001603}
1604
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001605/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001606 * stmmac_tx_timer: mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001607 * @data: data pointer
1608 * Description:
1609 * This is the timer handler to directly invoke the stmmac_tx_clean.
1610 */
1611static void stmmac_tx_timer(unsigned long data)
1612{
1613 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1614
1615 stmmac_tx_clean(priv);
1616}
1617
1618/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001619 * stmmac_init_tx_coalesce: init tx mitigation options.
1620 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001621 * Description:
1622 * This inits the transmit coalesce parameters: i.e. timer rate,
1623 * timer handler and default threshold used for enabling the
1624 * interrupt on completion bit.
1625 */
1626static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1627{
1628 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1629 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1630 init_timer(&priv->txtimer);
1631 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1632 priv->txtimer.data = (unsigned long)priv;
1633 priv->txtimer.function = stmmac_tx_timer;
1634 add_timer(&priv->txtimer);
1635}
1636
1637/**
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001638 * stmmac_hw_setup: setup mac in a usable state.
1639 * @dev : pointer to the device structure.
1640 * Description:
1641 * This function sets up the ip in a usable state.
1642 * Return value:
1643 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1644 * file on failure.
1645 */
1646static int stmmac_hw_setup(struct net_device *dev)
1647{
1648 struct stmmac_priv *priv = netdev_priv(dev);
1649 int ret;
1650
1651 ret = init_dma_desc_rings(dev);
1652 if (ret < 0) {
1653 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1654 return ret;
1655 }
1656 /* DMA initialization and SW reset */
1657 ret = stmmac_init_dma_engine(priv);
1658 if (ret < 0) {
1659 pr_err("%s: DMA engine initialization failed\n", __func__);
1660 return ret;
1661 }
1662
1663 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001664 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001665
1666 /* If required, perform hw setup of the bus. */
1667 if (priv->plat->bus_setup)
1668 priv->plat->bus_setup(priv->ioaddr);
1669
1670 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001671 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001672
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001673 ret = priv->hw->mac->rx_ipc(priv->hw);
1674 if (!ret) {
1675 pr_warn(" RX IPC Checksum Offload disabled\n");
1676 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
1677 }
1678
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001679 /* Enable the MAC Rx/Tx */
1680 stmmac_set_mac(priv->ioaddr, true);
1681
1682 /* Set the HW DMA mode and the COE */
1683 stmmac_dma_operation_mode(priv);
1684
1685 stmmac_mmc_setup(priv);
1686
1687 ret = stmmac_init_ptp(priv);
Hans de Goede7509edd2014-01-26 15:50:43 +01001688 if (ret && ret != -EOPNOTSUPP)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001689 pr_warn("%s: failed PTP initialisation\n", __func__);
1690
1691#ifdef CONFIG_STMMAC_DEBUG_FS
1692 ret = stmmac_init_fs(dev);
1693 if (ret < 0)
1694 pr_warn("%s: failed debugFS registration\n", __func__);
1695#endif
1696 /* Start the ball rolling... */
1697 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1698 priv->hw->dma->start_tx(priv->ioaddr);
1699 priv->hw->dma->start_rx(priv->ioaddr);
1700
1701 /* Dump DMA/MAC registers */
1702 if (netif_msg_hw(priv)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001703 priv->hw->mac->dump_regs(priv->hw);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001704 priv->hw->dma->dump_regs(priv->ioaddr);
1705 }
1706 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1707
1708 priv->eee_enabled = stmmac_eee_init(priv);
1709
1710 stmmac_init_tx_coalesce(priv);
1711
1712 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1713 priv->rx_riwt = MAX_DMA_RIWT;
1714 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1715 }
1716
1717 if (priv->pcs && priv->hw->mac->ctrl_ane)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001718 priv->hw->mac->ctrl_ane(priv->hw, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001719
1720 return 0;
1721}
1722
1723/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001724 * stmmac_open - open entry point of the driver
1725 * @dev : pointer to the device structure.
1726 * Description:
1727 * This function is the open entry point of the driver.
1728 * Return value:
1729 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1730 * file on failure.
1731 */
1732static int stmmac_open(struct net_device *dev)
1733{
1734 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001735 int ret;
1736
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001737 stmmac_check_ether_addr(priv);
1738
Byungho An4d8f0822013-04-07 17:56:16 +00001739 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1740 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001741 ret = stmmac_init_phy(dev);
1742 if (ret) {
1743 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1744 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02001745 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001746 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001747 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001748
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001749 /* Extra statistics */
1750 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1751 priv->xstats.threshold = tc;
1752
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001753 /* Create and initialize the TX/RX descriptors chains. */
1754 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1755 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1756 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001757
Tobias Klauser7262b7b2014-02-22 13:09:03 +01001758 ret = alloc_dma_desc_resources(priv);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001759 if (ret < 0) {
1760 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1761 goto dma_desc_error;
1762 }
1763
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001764 ret = stmmac_hw_setup(dev);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001765 if (ret < 0) {
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001766 pr_err("%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001767 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001768 }
1769
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001770 if (priv->phydev)
1771 phy_start(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001772
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001773 /* Request the IRQ lines */
1774 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001775 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001776 if (unlikely(ret < 0)) {
1777 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1778 __func__, dev->irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001779 goto init_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001780 }
1781
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001782 /* Request the Wake IRQ in case of another line is used for WoL */
1783 if (priv->wol_irq != dev->irq) {
1784 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1785 IRQF_SHARED, dev->name, dev);
1786 if (unlikely(ret < 0)) {
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001787 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1788 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001789 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001790 }
1791 }
1792
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001793 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001794 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001795 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1796 dev->name, dev);
1797 if (unlikely(ret < 0)) {
1798 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1799 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001800 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001801 }
1802 }
1803
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001804 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001805 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001806
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001807 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001808
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001809lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001810 if (priv->wol_irq != dev->irq)
1811 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001812wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001813 free_irq(dev->irq, dev);
1814
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001815init_error:
1816 free_dma_desc_resources(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001817dma_desc_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001818 if (priv->phydev)
1819 phy_disconnect(priv->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001820
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001821 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001822}
1823
1824/**
1825 * stmmac_release - close entry point of the driver
1826 * @dev : device pointer.
1827 * Description:
1828 * This is the stop entry point of the driver.
1829 */
1830static int stmmac_release(struct net_device *dev)
1831{
1832 struct stmmac_priv *priv = netdev_priv(dev);
1833
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001834 if (priv->eee_enabled)
1835 del_timer_sync(&priv->eee_ctrl_timer);
1836
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001837 /* Stop and disconnect the PHY */
1838 if (priv->phydev) {
1839 phy_stop(priv->phydev);
1840 phy_disconnect(priv->phydev);
1841 priv->phydev = NULL;
1842 }
1843
1844 netif_stop_queue(dev);
1845
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001846 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001847
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001848 del_timer_sync(&priv->txtimer);
1849
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001850 /* Free the IRQ lines */
1851 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001852 if (priv->wol_irq != dev->irq)
1853 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001854 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001855 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001856
1857 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001858 priv->hw->dma->stop_tx(priv->ioaddr);
1859 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001860
1861 /* Release and free the Rx/Tx resources */
1862 free_dma_desc_resources(priv);
1863
avisconti19449bf2010-10-25 18:58:14 +00001864 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001865 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001866
1867 netif_carrier_off(dev);
1868
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001869#ifdef CONFIG_STMMAC_DEBUG_FS
1870 stmmac_exit_fs();
1871#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001872
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00001873 stmmac_release_ptp(priv);
1874
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001875 return 0;
1876}
1877
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001878/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001879 * stmmac_xmit: Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001880 * @skb : the socket buffer
1881 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001882 * Description : this is the tx entry point of the driver.
1883 * It programs the chain or the ring and supports oversized frames
1884 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001885 */
1886static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1887{
1888 struct stmmac_priv *priv = netdev_priv(dev);
1889 unsigned int txsize = priv->dma_tx_size;
1890 unsigned int entry;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001891 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001892 int nfrags = skb_shinfo(skb)->nr_frags;
1893 struct dma_desc *desc, *first;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001894 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001895 unsigned int enh_desc = priv->plat->enh_desc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001896
1897 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1898 if (!netif_queue_stopped(dev)) {
1899 netif_stop_queue(dev);
1900 /* This is a hard error, log it. */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001901 pr_err("%s: Tx Ring full when queue awake\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001902 }
1903 return NETDEV_TX_BUSY;
1904 }
1905
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001906 spin_lock(&priv->tx_lock);
1907
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001908 if (priv->tx_path_in_lpi_mode)
1909 stmmac_disable_eee_mode(priv);
1910
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001911 entry = priv->cur_tx % txsize;
1912
Michał Mirosław5e982f32011-04-09 02:46:55 +00001913 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001914
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001915 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001916 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001917 else
1918 desc = priv->dma_tx + entry;
1919
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001920 first = desc;
1921
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001922 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001923 if (enh_desc)
1924 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
1925
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001926 if (likely(!is_jumbo)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001927 desc->des2 = dma_map_single(priv->device, skb->data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001928 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001929 if (dma_mapping_error(priv->device, desc->des2))
1930 goto dma_map_err;
1931 priv->tx_skbuff_dma[entry].buf = desc->des2;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001932 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001933 csum_insertion, priv->mode);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001934 } else {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001935 desc = first;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001936 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001937 if (unlikely(entry < 0))
1938 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001939 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001940
1941 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001942 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1943 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001944
damuzi00075e43642014-01-17 23:47:59 +08001945 priv->tx_skbuff[entry] = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001946 entry = (++priv->cur_tx) % txsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001947 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001948 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001949 else
1950 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001951
Ian Campbellf7223802011-09-21 21:53:20 +00001952 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1953 DMA_TO_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001954 if (dma_mapping_error(priv->device, desc->des2))
1955 goto dma_map_err; /* should reuse desc w/o issues */
1956
1957 priv->tx_skbuff_dma[entry].buf = desc->des2;
1958 priv->tx_skbuff_dma[entry].map_as_page = true;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001959 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
1960 priv->mode);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001961 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001962 priv->hw->desc->set_tx_owner(desc);
Deepak Sikri8e839892012-07-08 21:14:45 +00001963 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001964 }
1965
damuzi00075e43642014-01-17 23:47:59 +08001966 priv->tx_skbuff[entry] = skb;
1967
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001968 /* Finalize the latest segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001969 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00001970
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001971 wmb();
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001972 /* According to the coalesce parameter the IC bit for the latest
1973 * segment could be reset and the timer re-started to invoke the
1974 * stmmac_tx function. This approach takes care about the fragments.
1975 */
1976 priv->tx_count_frames += nfrags + 1;
1977 if (priv->tx_coal_frames > priv->tx_count_frames) {
1978 priv->hw->desc->clear_tx_ic(desc);
1979 priv->xstats.tx_reset_ic_bit++;
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001980 mod_timer(&priv->txtimer,
1981 STMMAC_COAL_TIMER(priv->tx_coal_timer));
1982 } else
1983 priv->tx_count_frames = 0;
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00001984
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001985 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001986 priv->hw->desc->set_tx_owner(first);
Deepak Sikri8e839892012-07-08 21:14:45 +00001987 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001988
1989 priv->cur_tx++;
1990
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001991 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001992 pr_debug("%s: curr %d dirty=%d entry=%d, first=%p, nfrags=%d",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001993 __func__, (priv->cur_tx % txsize),
1994 (priv->dirty_tx % txsize), entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001995
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001996 if (priv->extend_desc)
1997 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
1998 else
1999 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
2000
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002001 pr_debug(">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002002 print_pkt(skb->data, skb->len);
2003 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002004 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002005 if (netif_msg_hw(priv))
2006 pr_debug("%s: stop transmitted packets\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002007 netif_stop_queue(dev);
2008 }
2009
2010 dev->stats.tx_bytes += skb->len;
2011
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002012 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2013 priv->hwts_tx_en)) {
2014 /* declare that device is doing timestamping */
2015 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2016 priv->hw->desc->enable_tx_timestamp(first);
2017 }
2018
2019 if (!priv->hwts_tx_en)
2020 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00002021
Richard Cochran52f64fa2011-06-19 03:31:43 +00002022 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2023
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002024 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002025 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002026
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002027dma_map_err:
2028 dev_err(priv->device, "Tx dma map failed\n");
2029 dev_kfree_skb(skb);
2030 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002031 return NETDEV_TX_OK;
2032}
2033
Vince Bridgersb9381982014-01-14 13:42:05 -06002034static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2035{
2036 struct ethhdr *ehdr;
2037 u16 vlanid;
2038
2039 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2040 NETIF_F_HW_VLAN_CTAG_RX &&
2041 !__vlan_get_tag(skb, &vlanid)) {
2042 /* pop the vlan tag */
2043 ehdr = (struct ethhdr *)skb->data;
2044 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2045 skb_pull(skb, VLAN_HLEN);
2046 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2047 }
2048}
2049
2050
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002051/**
2052 * stmmac_rx_refill: refill used skb preallocated buffers
2053 * @priv: driver private structure
2054 * Description : this is to reallocate the skb for the reception process
2055 * that is based on zero-copy.
2056 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002057static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2058{
2059 unsigned int rxsize = priv->dma_rx_size;
2060 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002061
2062 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
2063 unsigned int entry = priv->dirty_rx % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002064 struct dma_desc *p;
2065
2066 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002067 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002068 else
2069 p = priv->dma_rx + entry;
2070
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002071 if (likely(priv->rx_skbuff[entry] == NULL)) {
2072 struct sk_buff *skb;
2073
Eric Dumazetacb600d2012-10-05 06:23:55 +00002074 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002075
2076 if (unlikely(skb == NULL))
2077 break;
2078
2079 priv->rx_skbuff[entry] = skb;
2080 priv->rx_skbuff_dma[entry] =
2081 dma_map_single(priv->device, skb->data, bfsize,
2082 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002083 if (dma_mapping_error(priv->device,
2084 priv->rx_skbuff_dma[entry])) {
2085 dev_err(priv->device, "Rx dma map failed\n");
2086 dev_kfree_skb(skb);
2087 break;
2088 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002089 p->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002090
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002091 priv->hw->mode->refill_desc3(priv, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002092
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002093 if (netif_msg_rx_status(priv))
2094 pr_debug("\trefill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002095 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002096 wmb();
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002097 priv->hw->desc->set_rx_owner(p);
Deepak Sikri8e839892012-07-08 21:14:45 +00002098 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002099 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002100}
2101
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002102/**
2103 * stmmac_rx_refill: refill used skb preallocated buffers
2104 * @priv: driver private structure
2105 * @limit: napi bugget.
2106 * Description : this the function called by the napi poll method.
2107 * It gets all the frames inside the ring.
2108 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002109static int stmmac_rx(struct stmmac_priv *priv, int limit)
2110{
2111 unsigned int rxsize = priv->dma_rx_size;
2112 unsigned int entry = priv->cur_rx % rxsize;
2113 unsigned int next_entry;
2114 unsigned int count = 0;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002115 int coe = priv->plat->rx_coe;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002116
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002117 if (netif_msg_rx_status(priv)) {
2118 pr_debug("%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002119 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002120 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002121 else
2122 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002123 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002124 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002125 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002126 struct dma_desc *p;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002127
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002128 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002129 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002130 else
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002131 p = priv->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002132
2133 if (priv->hw->desc->get_rx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002134 break;
2135
2136 count++;
2137
2138 next_entry = (++priv->cur_rx) % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002139 if (priv->extend_desc)
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002140 prefetch(priv->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002141 else
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002142 prefetch(priv->dma_rx + next_entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002143
2144 /* read the status of the incoming frame */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002145 status = priv->hw->desc->rx_status(&priv->dev->stats,
2146 &priv->xstats, p);
2147 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2148 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2149 &priv->xstats,
2150 priv->dma_erx +
2151 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002152 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002153 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002154 if (priv->hwts_rx_en && !priv->extend_desc) {
2155 /* DESC2 & DESC3 will be overwitten by device
2156 * with timestamp value, hence reinitialize
2157 * them in stmmac_rx_refill() function so that
2158 * device can reuse it.
2159 */
2160 priv->rx_skbuff[entry] = NULL;
2161 dma_unmap_single(priv->device,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002162 priv->rx_skbuff_dma[entry],
2163 priv->dma_buf_sz,
2164 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002165 }
2166 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002167 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002168 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002169
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002170 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2171
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002172 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002173 * Type frames (LLC/LLC-SNAP)
2174 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002175 if (unlikely(status != llc_snap))
2176 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002177
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002178 if (netif_msg_rx_status(priv)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002179 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002180 p, entry, p->des2);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002181 if (frame_len > ETH_FRAME_LEN)
2182 pr_debug("\tframe size %d, COE: %d\n",
2183 frame_len, status);
2184 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002185 skb = priv->rx_skbuff[entry];
2186 if (unlikely(!skb)) {
2187 pr_err("%s: Inconsistent Rx descriptor chain\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002188 priv->dev->name);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002189 priv->dev->stats.rx_dropped++;
2190 break;
2191 }
2192 prefetch(skb->data - NET_IP_ALIGN);
2193 priv->rx_skbuff[entry] = NULL;
2194
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002195 stmmac_get_rx_hwtstamp(priv, entry, skb);
2196
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002197 skb_put(skb, frame_len);
2198 dma_unmap_single(priv->device,
2199 priv->rx_skbuff_dma[entry],
2200 priv->dma_buf_sz, DMA_FROM_DEVICE);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002201
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002202 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002203 pr_debug("frame received (%dbytes)", frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002204 print_pkt(skb->data, frame_len);
2205 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002206
Vince Bridgersb9381982014-01-14 13:42:05 -06002207 stmmac_rx_vlan(priv->dev, skb);
2208
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002209 skb->protocol = eth_type_trans(skb, priv->dev);
2210
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002211 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002212 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002213 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002214 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002215
2216 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002217
2218 priv->dev->stats.rx_packets++;
2219 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002220 }
2221 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002222 }
2223
2224 stmmac_rx_refill(priv);
2225
2226 priv->xstats.rx_pkt_n += count;
2227
2228 return count;
2229}
2230
2231/**
2232 * stmmac_poll - stmmac poll method (NAPI)
2233 * @napi : pointer to the napi structure.
2234 * @budget : maximum number of packets that the current CPU can receive from
2235 * all interfaces.
2236 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002237 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002238 */
2239static int stmmac_poll(struct napi_struct *napi, int budget)
2240{
2241 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2242 int work_done = 0;
2243
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002244 priv->xstats.napi_poll++;
2245 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002246
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002247 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002248 if (work_done < budget) {
2249 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002250 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002251 }
2252 return work_done;
2253}
2254
2255/**
2256 * stmmac_tx_timeout
2257 * @dev : Pointer to net device structure
2258 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00002259 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002260 * netdev structure and arrange for the device to be reset to a sane state
2261 * in order to transmit a new packet.
2262 */
2263static void stmmac_tx_timeout(struct net_device *dev)
2264{
2265 struct stmmac_priv *priv = netdev_priv(dev);
2266
2267 /* Clear Tx resources and restart transmitting again */
2268 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002269}
2270
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002271/**
Jiri Pirko01789342011-08-16 06:29:00 +00002272 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002273 * @dev : pointer to the device structure
2274 * Description:
2275 * This function is a driver entry point which gets called by the kernel
2276 * whenever multicast addresses must be enabled/disabled.
2277 * Return value:
2278 * void.
2279 */
Jiri Pirko01789342011-08-16 06:29:00 +00002280static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002281{
2282 struct stmmac_priv *priv = netdev_priv(dev);
2283
2284 spin_lock(&priv->lock);
Vince Bridgers3b57de92014-07-31 15:49:17 -05002285 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002286 spin_unlock(&priv->lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002287}
2288
2289/**
2290 * stmmac_change_mtu - entry point to change MTU size for the device.
2291 * @dev : device pointer.
2292 * @new_mtu : the new MTU size for the device.
2293 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2294 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2295 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2296 * Return value:
2297 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2298 * file on failure.
2299 */
2300static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2301{
2302 struct stmmac_priv *priv = netdev_priv(dev);
2303 int max_mtu;
2304
2305 if (netif_running(dev)) {
2306 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2307 return -EBUSY;
2308 }
2309
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00002310 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002311 max_mtu = JUMBO_LEN;
2312 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00002313 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002314
Vince Bridgers2618abb2014-01-20 05:39:01 -06002315 if (priv->plat->maxmtu < max_mtu)
2316 max_mtu = priv->plat->maxmtu;
2317
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002318 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2319 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2320 return -EINVAL;
2321 }
2322
Michał Mirosław5e982f32011-04-09 02:46:55 +00002323 dev->mtu = new_mtu;
2324 netdev_update_features(dev);
2325
2326 return 0;
2327}
2328
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002329static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002330 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002331{
2332 struct stmmac_priv *priv = netdev_priv(dev);
2333
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002334 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002335 features &= ~NETIF_F_RXCSUM;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002336 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
2337 features &= ~NETIF_F_IPV6_CSUM;
Michał Mirosław5e982f32011-04-09 02:46:55 +00002338 if (!priv->plat->tx_coe)
2339 features &= ~NETIF_F_ALL_CSUM;
2340
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002341 /* Some GMAC devices have a bugged Jumbo frame support that
2342 * needs to have the Tx COE disabled for oversized frames
2343 * (due to limited buffer sizes). In this case we disable
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002344 * the TX csum insertionin the TDES and not use SF.
2345 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00002346 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
2347 features &= ~NETIF_F_ALL_CSUM;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002348
Michał Mirosław5e982f32011-04-09 02:46:55 +00002349 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002350}
2351
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002352/**
2353 * stmmac_interrupt - main ISR
2354 * @irq: interrupt number.
2355 * @dev_id: to pass the net device pointer.
2356 * Description: this is the main driver interrupt service routine.
2357 * It calls the DMA ISR and also the core ISR to manage PMT, MMC, LPI
2358 * interrupts.
2359 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002360static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2361{
2362 struct net_device *dev = (struct net_device *)dev_id;
2363 struct stmmac_priv *priv = netdev_priv(dev);
2364
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002365 if (priv->irq_wake)
2366 pm_wakeup_event(priv->device, 0);
2367
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002368 if (unlikely(!dev)) {
2369 pr_err("%s: invalid dev pointer\n", __func__);
2370 return IRQ_NONE;
2371 }
2372
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002373 /* To handle GMAC own interrupts */
2374 if (priv->plat->has_gmac) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002375 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002376 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002377 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002378 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002379 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002380 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002381 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002382 priv->tx_path_in_lpi_mode = false;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002383 }
2384 }
2385
2386 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002387 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002388
2389 return IRQ_HANDLED;
2390}
2391
2392#ifdef CONFIG_NET_POLL_CONTROLLER
2393/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002394 * to allow network I/O with interrupts disabled.
2395 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002396static void stmmac_poll_controller(struct net_device *dev)
2397{
2398 disable_irq(dev->irq);
2399 stmmac_interrupt(dev->irq, dev);
2400 enable_irq(dev->irq);
2401}
2402#endif
2403
2404/**
2405 * stmmac_ioctl - Entry point for the Ioctl
2406 * @dev: Device pointer.
2407 * @rq: An IOCTL specefic structure, that can contain a pointer to
2408 * a proprietary structure used to pass information to the driver.
2409 * @cmd: IOCTL command
2410 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002411 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002412 */
2413static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2414{
2415 struct stmmac_priv *priv = netdev_priv(dev);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002416 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002417
2418 if (!netif_running(dev))
2419 return -EINVAL;
2420
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002421 switch (cmd) {
2422 case SIOCGMIIPHY:
2423 case SIOCGMIIREG:
2424 case SIOCSMIIREG:
2425 if (!priv->phydev)
2426 return -EINVAL;
2427 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2428 break;
2429 case SIOCSHWTSTAMP:
2430 ret = stmmac_hwtstamp_ioctl(dev, rq);
2431 break;
2432 default:
2433 break;
2434 }
Richard Cochran28b04112010-07-17 08:48:55 +00002435
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002436 return ret;
2437}
2438
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002439#ifdef CONFIG_STMMAC_DEBUG_FS
2440static struct dentry *stmmac_fs_dir;
2441static struct dentry *stmmac_rings_status;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002442static struct dentry *stmmac_dma_cap;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002443
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002444static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002445 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002446{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002447 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002448 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2449 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002450
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002451 for (i = 0; i < size; i++) {
2452 u64 x;
2453 if (extend_desc) {
2454 x = *(u64 *) ep;
2455 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002456 i, (unsigned int)virt_to_phys(ep),
2457 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002458 ep->basic.des2, ep->basic.des3);
2459 ep++;
2460 } else {
2461 x = *(u64 *) p;
2462 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002463 i, (unsigned int)virt_to_phys(ep),
2464 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002465 p->des2, p->des3);
2466 p++;
2467 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002468 seq_printf(seq, "\n");
2469 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002470}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002471
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002472static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2473{
2474 struct net_device *dev = seq->private;
2475 struct stmmac_priv *priv = netdev_priv(dev);
2476 unsigned int txsize = priv->dma_tx_size;
2477 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002478
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002479 if (priv->extend_desc) {
2480 seq_printf(seq, "Extended RX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002481 sysfs_display_ring((void *)priv->dma_erx, rxsize, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002482 seq_printf(seq, "Extended TX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002483 sysfs_display_ring((void *)priv->dma_etx, txsize, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002484 } else {
2485 seq_printf(seq, "RX descriptor ring:\n");
2486 sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
2487 seq_printf(seq, "TX descriptor ring:\n");
2488 sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002489 }
2490
2491 return 0;
2492}
2493
2494static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2495{
2496 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2497}
2498
2499static const struct file_operations stmmac_rings_status_fops = {
2500 .owner = THIS_MODULE,
2501 .open = stmmac_sysfs_ring_open,
2502 .read = seq_read,
2503 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002504 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002505};
2506
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002507static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2508{
2509 struct net_device *dev = seq->private;
2510 struct stmmac_priv *priv = netdev_priv(dev);
2511
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002512 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002513 seq_printf(seq, "DMA HW features not supported\n");
2514 return 0;
2515 }
2516
2517 seq_printf(seq, "==============================\n");
2518 seq_printf(seq, "\tDMA HW features\n");
2519 seq_printf(seq, "==============================\n");
2520
2521 seq_printf(seq, "\t10/100 Mbps %s\n",
2522 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2523 seq_printf(seq, "\t1000 Mbps %s\n",
2524 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2525 seq_printf(seq, "\tHalf duple %s\n",
2526 (priv->dma_cap.half_duplex) ? "Y" : "N");
2527 seq_printf(seq, "\tHash Filter: %s\n",
2528 (priv->dma_cap.hash_filter) ? "Y" : "N");
2529 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2530 (priv->dma_cap.multi_addr) ? "Y" : "N");
2531 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2532 (priv->dma_cap.pcs) ? "Y" : "N");
2533 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2534 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2535 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2536 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2537 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2538 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2539 seq_printf(seq, "\tRMON module: %s\n",
2540 (priv->dma_cap.rmon) ? "Y" : "N");
2541 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2542 (priv->dma_cap.time_stamp) ? "Y" : "N");
2543 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2544 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2545 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2546 (priv->dma_cap.eee) ? "Y" : "N");
2547 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2548 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2549 (priv->dma_cap.tx_coe) ? "Y" : "N");
2550 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2551 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2552 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2553 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2554 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2555 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2556 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2557 priv->dma_cap.number_rx_channel);
2558 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2559 priv->dma_cap.number_tx_channel);
2560 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2561 (priv->dma_cap.enh_desc) ? "Y" : "N");
2562
2563 return 0;
2564}
2565
2566static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2567{
2568 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2569}
2570
2571static const struct file_operations stmmac_dma_cap_fops = {
2572 .owner = THIS_MODULE,
2573 .open = stmmac_sysfs_dma_cap_open,
2574 .read = seq_read,
2575 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002576 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002577};
2578
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002579static int stmmac_init_fs(struct net_device *dev)
2580{
2581 /* Create debugfs entries */
2582 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
2583
2584 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
2585 pr_err("ERROR %s, debugfs create directory failed\n",
2586 STMMAC_RESOURCE_NAME);
2587
2588 return -ENOMEM;
2589 }
2590
2591 /* Entry to report DMA RX/TX rings */
2592 stmmac_rings_status = debugfs_create_file("descriptors_status",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002593 S_IRUGO, stmmac_fs_dir, dev,
2594 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002595
2596 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
2597 pr_info("ERROR creating stmmac ring debugfs file\n");
2598 debugfs_remove(stmmac_fs_dir);
2599
2600 return -ENOMEM;
2601 }
2602
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002603 /* Entry to report the DMA HW features */
2604 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
2605 dev, &stmmac_dma_cap_fops);
2606
2607 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
2608 pr_info("ERROR creating stmmac MMC debugfs file\n");
2609 debugfs_remove(stmmac_rings_status);
2610 debugfs_remove(stmmac_fs_dir);
2611
2612 return -ENOMEM;
2613 }
2614
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002615 return 0;
2616}
2617
2618static void stmmac_exit_fs(void)
2619{
2620 debugfs_remove(stmmac_rings_status);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002621 debugfs_remove(stmmac_dma_cap);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002622 debugfs_remove(stmmac_fs_dir);
2623}
2624#endif /* CONFIG_STMMAC_DEBUG_FS */
2625
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002626static const struct net_device_ops stmmac_netdev_ops = {
2627 .ndo_open = stmmac_open,
2628 .ndo_start_xmit = stmmac_xmit,
2629 .ndo_stop = stmmac_release,
2630 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00002631 .ndo_fix_features = stmmac_fix_features,
Jiri Pirko01789342011-08-16 06:29:00 +00002632 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002633 .ndo_tx_timeout = stmmac_tx_timeout,
2634 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002635#ifdef CONFIG_NET_POLL_CONTROLLER
2636 .ndo_poll_controller = stmmac_poll_controller,
2637#endif
2638 .ndo_set_mac_address = eth_mac_addr,
2639};
2640
2641/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002642 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002643 * @priv: driver private structure
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002644 * Description: this function detects which MAC device
2645 * (GMAC/MAC10-100) has to attached, checks the HW capability
2646 * (if supported) and sets the driver's features (for example
2647 * to use the ring or chaine mode or support the normal/enh
2648 * descriptor structure).
2649 */
2650static int stmmac_hw_init(struct stmmac_priv *priv)
2651{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002652 struct mac_device_info *mac;
2653
2654 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002655 if (priv->plat->has_gmac) {
2656 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05002657 mac = dwmac1000_setup(priv->ioaddr,
2658 priv->plat->multicast_filter_bins,
2659 priv->plat->unicast_filter_entries);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002660 } else {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002661 mac = dwmac100_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002662 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002663 if (!mac)
2664 return -ENOMEM;
2665
2666 priv->hw = mac;
2667
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002668 /* Get and dump the chip ID */
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00002669 priv->synopsys_id = stmmac_get_synopsys_id(priv);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002670
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002671 /* To use the chained or ring mode */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002672 if (chain_mode) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002673 priv->hw->mode = &chain_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002674 pr_info(" Chain mode enabled\n");
2675 priv->mode = STMMAC_CHAIN_MODE;
2676 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002677 priv->hw->mode = &ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002678 pr_info(" Ring mode enabled\n");
2679 priv->mode = STMMAC_RING_MODE;
2680 }
2681
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002682 /* Get the HW capability (new GMAC newer than 3.50a) */
2683 priv->hw_cap_support = stmmac_get_hw_features(priv);
2684 if (priv->hw_cap_support) {
2685 pr_info(" DMA HW capability register supported");
2686
2687 /* We can override some gmac/dma configuration fields: e.g.
2688 * enh_desc, tx_coe (e.g. that are passed through the
2689 * platform) with the values from the HW capability
2690 * register (if supported).
2691 */
2692 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002693 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002694
2695 priv->plat->tx_coe = priv->dma_cap.tx_coe;
2696
2697 if (priv->dma_cap.rx_coe_type2)
2698 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2699 else if (priv->dma_cap.rx_coe_type1)
2700 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2701
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002702 } else
2703 pr_info(" No HW DMA feature register supported");
2704
Byungho An61369d02013-06-28 16:35:32 +09002705 /* To use alternate (extended) or normal descriptor structures */
2706 stmmac_selec_desc_mode(priv);
2707
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002708 if (priv->plat->rx_coe)
2709 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2710 priv->plat->rx_coe);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002711 if (priv->plat->tx_coe)
2712 pr_info(" TX Checksum insertion supported\n");
2713
2714 if (priv->plat->pmt) {
2715 pr_info(" Wake-Up On Lan supported\n");
2716 device_set_wakeup_capable(priv->device, 1);
2717 }
2718
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002719 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002720}
2721
2722/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002723 * stmmac_dvr_probe
2724 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002725 * @plat_dat: platform data pointer
2726 * @addr: iobase memory address
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002727 * Description: this is the main probe function used to
2728 * call the alloc_etherdev, allocate the priv structure.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002729 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002730struct stmmac_priv *stmmac_dvr_probe(struct device *device,
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002731 struct plat_stmmacenet_data *plat_dat,
2732 void __iomem *addr)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002733{
2734 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002735 struct net_device *ndev = NULL;
2736 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002737
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002738 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002739 if (!ndev)
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002740 return NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002741
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002742 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002743
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002744 priv = netdev_priv(ndev);
2745 priv->device = device;
2746 priv->dev = ndev;
2747
2748 ether_setup(ndev);
2749
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002750 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002751 priv->pause = pause;
2752 priv->plat = plat_dat;
2753 priv->ioaddr = addr;
2754 priv->dev->base_addr = (unsigned long)addr;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002755
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002756 /* Verify driver arguments */
2757 stmmac_verify_args();
2758
2759 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002760 * this needs to have multiple instances
2761 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002762 if ((phyaddr >= 0) && (phyaddr <= 31))
2763 priv->plat->phy_addr = phyaddr;
2764
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002765 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
2766 if (IS_ERR(priv->stmmac_clk)) {
2767 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
2768 __func__);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002769 ret = PTR_ERR(priv->stmmac_clk);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002770 goto error_clk_get;
2771 }
2772 clk_prepare_enable(priv->stmmac_clk);
2773
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002774 priv->stmmac_rst = devm_reset_control_get(priv->device,
2775 STMMAC_RESOURCE_NAME);
2776 if (IS_ERR(priv->stmmac_rst)) {
2777 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
2778 ret = -EPROBE_DEFER;
2779 goto error_hw_init;
2780 }
2781 dev_info(priv->device, "no reset control found\n");
2782 priv->stmmac_rst = NULL;
2783 }
2784 if (priv->stmmac_rst)
2785 reset_control_deassert(priv->stmmac_rst);
2786
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002787 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002788 ret = stmmac_hw_init(priv);
2789 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002790 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002791
2792 ndev->netdev_ops = &stmmac_netdev_ops;
2793
2794 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2795 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002796 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2797 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002798#ifdef STMMAC_VLAN_TAG_USED
2799 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00002800 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002801#endif
2802 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2803
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002804 if (flow_ctrl)
2805 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2806
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002807 /* Rx Watchdog is available in the COREs newer than the 3.40.
2808 * In some case, for example on bugged HW this feature
2809 * has to be disable and this can be done by passing the
2810 * riwt_off field from the platform.
2811 */
2812 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2813 priv->use_riwt = 1;
2814 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2815 }
2816
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002817 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002818
Vlad Lunguf8e96162010-11-29 22:52:52 +00002819 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002820 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002821
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002822 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002823 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002824 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002825 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002826 }
2827
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002828 /* If a specific clk_csr value is passed from the platform
2829 * this means that the CSR Clock Range selection cannot be
2830 * changed at run-time and it is fixed. Viceversa the driver'll try to
2831 * set the MDC clock dynamically according to the csr actual
2832 * clock input.
2833 */
2834 if (!priv->plat->clk_csr)
2835 stmmac_clk_csr_set(priv);
2836 else
2837 priv->clk_csr = priv->plat->clk_csr;
2838
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002839 stmmac_check_pcs_mode(priv);
2840
Byungho An4d8f0822013-04-07 17:56:16 +00002841 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2842 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002843 /* MDIO bus Registration */
2844 ret = stmmac_mdio_register(ndev);
2845 if (ret < 0) {
2846 pr_debug("%s: MDIO bus (id: %d) registration failed",
2847 __func__, priv->plat->bus_id);
2848 goto error_mdio_register;
2849 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002850 }
2851
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002852 return priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002853
Viresh Kumar6a81c262012-07-30 14:39:41 -07002854error_mdio_register:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002855 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002856error_netdev_register:
2857 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002858error_hw_init:
2859 clk_disable_unprepare(priv->stmmac_clk);
2860error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002861 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002862
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002863 return ERR_PTR(ret);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002864}
2865
2866/**
2867 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002868 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002869 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002870 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002871 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002872int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002873{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002874 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002875
2876 pr_info("%s:\n\tremoving driver", __func__);
2877
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002878 priv->hw->dma->stop_rx(priv->ioaddr);
2879 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002880
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002881 stmmac_set_mac(priv->ioaddr, false);
Byungho An4d8f0822013-04-07 17:56:16 +00002882 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2883 priv->pcs != STMMAC_PCS_RTBI)
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002884 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002885 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002886 unregister_netdev(ndev);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002887 if (priv->stmmac_rst)
2888 reset_control_assert(priv->stmmac_rst);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002889 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002890 free_netdev(ndev);
2891
2892 return 0;
2893}
2894
2895#ifdef CONFIG_PM
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002896int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002897{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002898 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002899 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002900
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002901 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002902 return 0;
2903
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002904 if (priv->phydev)
2905 phy_stop(priv->phydev);
2906
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002907 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002908
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002909 netif_device_detach(ndev);
2910 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002911
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002912 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002913
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002914 /* Stop TX/RX DMA */
2915 priv->hw->dma->stop_tx(priv->ioaddr);
2916 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002917
2918 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002919
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002920 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002921 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002922 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002923 priv->irq_wake = 1;
2924 } else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002925 stmmac_set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00002926 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002927 /* Disable clock in case of PWM is off */
Stefan Roesea6308442012-09-21 01:06:29 +00002928 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002929 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002930 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05002931
2932 priv->oldlink = 0;
2933 priv->speed = 0;
2934 priv->oldduplex = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002935 return 0;
2936}
2937
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002938int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002939{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002940 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002941 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002942
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002943 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002944 return 0;
2945
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002946 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02002947
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002948 /* Power Down bit, into the PM register, is cleared
2949 * automatically as soon as a magic packet or a Wake-up frame
2950 * is received. Anyway, it's better to manually clear
2951 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002952 * from another devices (e.g. serial console).
2953 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002954 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002955 priv->hw->mac->pmt(priv->hw, 0);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002956 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002957 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00002958 pinctrl_pm_select_default_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00002959 /* enable the clk prevously disabled */
Stefan Roesea6308442012-09-21 01:06:29 +00002960 clk_prepare_enable(priv->stmmac_clk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002961 /* reset the phy so that it's ready */
2962 if (priv->mii)
2963 stmmac_mdio_reset(priv->mii);
2964 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002965
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002966 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002967
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00002968 stmmac_hw_setup(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002969
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002970 napi_enable(&priv->napi);
2971
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002972 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002973
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00002974 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00002975
2976 if (priv->phydev)
2977 phy_start(priv->phydev);
2978
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002979 return 0;
2980}
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00002981#endif /* CONFIG_PM */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002982
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002983/* Driver can be configured w/ and w/ both PCI and Platf drivers
2984 * depending on the configuration selected.
2985 */
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002986static int __init stmmac_init(void)
2987{
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002988 int ret;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00002989
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002990 ret = stmmac_register_platform();
2991 if (ret)
2992 goto err;
2993 ret = stmmac_register_pci();
2994 if (ret)
2995 goto err_pci;
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00002996 return 0;
Konstantin Khlebnikov493682b2012-12-14 01:02:51 +00002997err_pci:
2998 stmmac_unregister_platform();
2999err:
3000 pr_err("stmmac: driver registration failed\n");
3001 return ret;
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00003002}
3003
3004static void __exit stmmac_exit(void)
3005{
Giuseppe CAVALLARO33d5e332012-06-07 19:25:07 +00003006 stmmac_unregister_platform();
3007 stmmac_unregister_pci();
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00003008}
3009
3010module_init(stmmac_init);
3011module_exit(stmmac_exit);
3012
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003013#ifndef MODULE
3014static int __init stmmac_cmdline_opt(char *str)
3015{
3016 char *opt;
3017
3018 if (!str || !*str)
3019 return -EINVAL;
3020 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003021 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003022 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003023 goto err;
3024 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003025 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003026 goto err;
3027 } else if (!strncmp(opt, "dma_txsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003028 if (kstrtoint(opt + 11, 0, &dma_txsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003029 goto err;
3030 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003031 if (kstrtoint(opt + 11, 0, &dma_rxsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003032 goto err;
3033 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003034 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003035 goto err;
3036 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003037 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003038 goto err;
3039 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003040 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003041 goto err;
3042 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003043 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003044 goto err;
3045 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003046 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003047 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00003048 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003049 if (kstrtoint(opt + 10, 0, &eee_timer))
3050 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003051 } else if (!strncmp(opt, "chain_mode:", 11)) {
3052 if (kstrtoint(opt + 11, 0, &chain_mode))
3053 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003054 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003055 }
3056 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003057
3058err:
3059 pr_err("%s: ERROR broken module parameter conversion", __func__);
3060 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003061}
3062
3063__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003064#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003065
3066MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3067MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3068MODULE_LICENSE("GPL");