blob: 89c26268822e8742a51853d7f4563c16e33b33c5 [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 CAVALLARO50fb4f742014-11-04 15:49:33 +010047#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000048#include <linux/debugfs.h>
49#include <linux/seq_file.h>
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +010050#endif /* CONFIG_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>
Mathieu Olivari5790cf32015-05-27 11:02:47 -070055#include <linux/of_mdio.h>
Phil Reid19d857c2015-12-14 11:32:01 +080056#include "dwmac1000.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070057
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070058#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070059
60/* Module parameters */
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000061#define TX_TIMEO 5000
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070062static int watchdog = TX_TIMEO;
63module_param(watchdog, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000064MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070065
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000066static int debug = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070067module_param(debug, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000068MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070069
stephen hemminger47d1f712013-12-30 10:38:57 -080070static int phyaddr = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070071module_param(phyaddr, int, S_IRUGO);
72MODULE_PARM_DESC(phyaddr, "Physical device address");
73
74#define DMA_TX_SIZE 256
75static int dma_txsize = DMA_TX_SIZE;
76module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
77MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
78
79#define DMA_RX_SIZE 256
80static int dma_rxsize = DMA_RX_SIZE;
81module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
82MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
83
84static int flow_ctrl = FLOW_OFF;
85module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
86MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
87
88static int pause = PAUSE_TIME;
89module_param(pause, int, S_IRUGO | S_IWUSR);
90MODULE_PARM_DESC(pause, "Flow Control Pause Time");
91
92#define TC_DEFAULT 64
93static int tc = TC_DEFAULT;
94module_param(tc, int, S_IRUGO | S_IWUSR);
95MODULE_PARM_DESC(tc, "DMA threshold control value");
96
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +010097#define DEFAULT_BUFSIZE 1536
98static int buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070099module_param(buf_sz, int, S_IRUGO | S_IWUSR);
100MODULE_PARM_DESC(buf_sz, "DMA buffer size");
101
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700102static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
103 NETIF_MSG_LINK | NETIF_MSG_IFUP |
104 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
105
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000106#define STMMAC_DEFAULT_LPI_TIMER 1000
107static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
108module_param(eee_timer, int, S_IRUGO | S_IWUSR);
109MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200110#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000111
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000112/* By default the driver will use the ring mode to manage tx and rx descriptors
113 * but passing this value so user can force to use the chain instead of the ring
114 */
115static unsigned int chain_mode;
116module_param(chain_mode, int, S_IRUGO);
117MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
118
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700119static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700120
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +0100121#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000122static int stmmac_init_fs(struct net_device *dev);
Mathieu Olivari466c5ac2015-05-22 19:03:29 -0700123static void stmmac_exit_fs(struct net_device *dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000124#endif
125
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000126#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
127
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700128/**
129 * stmmac_verify_args - verify the driver parameters.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100130 * Description: it checks the driver parameters and set a default in case of
131 * errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700132 */
133static void stmmac_verify_args(void)
134{
135 if (unlikely(watchdog < 0))
136 watchdog = TX_TIMEO;
137 if (unlikely(dma_rxsize < 0))
138 dma_rxsize = DMA_RX_SIZE;
139 if (unlikely(dma_txsize < 0))
140 dma_txsize = DMA_TX_SIZE;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100141 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
142 buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700143 if (unlikely(flow_ctrl > 1))
144 flow_ctrl = FLOW_AUTO;
145 else if (likely(flow_ctrl < 0))
146 flow_ctrl = FLOW_OFF;
147 if (unlikely((pause < 0) || (pause > 0xffff)))
148 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000149 if (eee_timer < 0)
150 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700151}
152
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000153/**
154 * stmmac_clk_csr_set - dynamically set the MDC clock
155 * @priv: driver private structure
156 * Description: this is to dynamically set the MDC clock according to the csr
157 * clock input.
158 * Note:
159 * If a specific clk_csr value is passed from the platform
160 * this means that the CSR Clock Range selection cannot be
161 * changed at run-time and it is fixed (as reported in the driver
162 * documentation). Viceversa the driver will try to set the MDC
163 * clock dynamically according to the actual clock input.
164 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000165static void stmmac_clk_csr_set(struct stmmac_priv *priv)
166{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000167 u32 clk_rate;
168
169 clk_rate = clk_get_rate(priv->stmmac_clk);
170
171 /* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000172 * for all other cases except for the below mentioned ones.
173 * For values higher than the IEEE 802.3 specified frequency
174 * we can not estimate the proper divider as it is not known
175 * the frequency of clk_csr_i. So we do not change the default
176 * divider.
177 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000178 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
179 if (clk_rate < CSR_F_35M)
180 priv->clk_csr = STMMAC_CSR_20_35M;
181 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
182 priv->clk_csr = STMMAC_CSR_35_60M;
183 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
184 priv->clk_csr = STMMAC_CSR_60_100M;
185 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
186 priv->clk_csr = STMMAC_CSR_100_150M;
187 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
188 priv->clk_csr = STMMAC_CSR_150_250M;
Phil Reid19d857c2015-12-14 11:32:01 +0800189 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000190 priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000191 }
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000192}
193
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700194static void print_pkt(unsigned char *buf, int len)
195{
Andy Shevchenko424c4f72014-11-07 16:53:12 +0200196 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
197 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700198}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700199
200/* minimum number of free TX descriptors required to wake up TX process */
201#define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
202
203static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
204{
205 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
206}
207
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000208/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100209 * stmmac_hw_fix_mac_speed - callback for speed selection
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000210 * @priv: driver private structure
211 * Description: on some platforms (e.g. ST), some HW system configuraton
212 * registers have to be set according to the link speed negotiated.
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000213 */
214static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
215{
216 struct phy_device *phydev = priv->phydev;
217
218 if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000219 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000220}
221
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000222/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100223 * stmmac_enable_eee_mode - check and enter in LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000224 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100225 * Description: this function is to verify and enter in LPI mode in case of
226 * EEE.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000227 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000228static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
229{
230 /* Check and enter in LPI mode */
231 if ((priv->dirty_tx == priv->cur_tx) &&
232 (priv->tx_path_in_lpi_mode == false))
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500233 priv->hw->mac->set_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000234}
235
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000236/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100237 * stmmac_disable_eee_mode - disable and exit from LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000238 * @priv: driver private structure
239 * Description: this function is to exit and disable EEE in case of
240 * LPI state is true. This is called by the xmit.
241 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000242void stmmac_disable_eee_mode(struct stmmac_priv *priv)
243{
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500244 priv->hw->mac->reset_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000245 del_timer_sync(&priv->eee_ctrl_timer);
246 priv->tx_path_in_lpi_mode = false;
247}
248
249/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100250 * stmmac_eee_ctrl_timer - EEE TX SW timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000251 * @arg : data hook
252 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000253 * if there is no data transfer and if we are not in LPI state,
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000254 * then MAC Transmitter can be moved to LPI state.
255 */
256static void stmmac_eee_ctrl_timer(unsigned long arg)
257{
258 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
259
260 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200261 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000262}
263
264/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100265 * stmmac_eee_init - init EEE
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000266 * @priv: driver private structure
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000267 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100268 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
269 * can also manage EEE, this function enable the LPI state and start related
270 * timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000271 */
272bool stmmac_eee_init(struct stmmac_priv *priv)
273{
Giuseppe CAVALLARO56b88c22014-08-28 08:11:43 +0200274 char *phy_bus_name = priv->plat->phy_bus_name;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100275 unsigned long flags;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000276 bool ret = false;
277
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200278 /* Using PCS we cannot dial with the phy registers at this stage
279 * so we do not support extra feature like EEE.
280 */
281 if ((priv->pcs == STMMAC_PCS_RGMII) || (priv->pcs == STMMAC_PCS_TBI) ||
282 (priv->pcs == STMMAC_PCS_RTBI))
283 goto out;
284
Giuseppe CAVALLARO56b88c22014-08-28 08:11:43 +0200285 /* Never init EEE in case of a switch is attached */
286 if (phy_bus_name && (!strcmp(phy_bus_name, "fixed")))
287 goto out;
288
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000289 /* MAC core supports the EEE feature. */
290 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100291 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000292
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100293 /* Check if the PHY supports EEE */
294 if (phy_init_eee(priv->phydev, 1)) {
295 /* To manage at run-time if the EEE cannot be supported
296 * anymore (for example because the lp caps have been
297 * changed).
298 * In that case the driver disable own timers.
299 */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100300 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100301 if (priv->eee_active) {
302 pr_debug("stmmac: disable EEE\n");
303 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500304 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100305 tx_lpi_timer);
306 }
307 priv->eee_active = 0;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100308 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100309 goto out;
310 }
311 /* Activate the EEE and start timers */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100312 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200313 if (!priv->eee_active) {
314 priv->eee_active = 1;
Vaishali Thakkarccb36da2015-02-28 00:12:34 +0530315 setup_timer(&priv->eee_ctrl_timer,
316 stmmac_eee_ctrl_timer,
317 (unsigned long)priv);
318 mod_timer(&priv->eee_ctrl_timer,
319 STMMAC_LPI_T(eee_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 CAVALLARO71965352014-08-28 08:11:44 +0200324 }
325 /* Set HW EEE according to the speed */
326 priv->hw->mac->set_eee_pls(priv->hw, priv->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000327
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000328 ret = true;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100329 spin_unlock_irqrestore(&priv->lock, flags);
330
331 pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000332 }
333out:
334 return ret;
335}
336
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100337/* stmmac_get_tx_hwtstamp - get HW TX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000338 * @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 CAVALLARO732fdf02014-11-18 09:47:01 +0100379/* stmmac_get_rx_hwtstamp - get HW RX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000380 * @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;
Arnd Bergmann0a624152015-09-30 13:26:32 +0200428 struct timespec64 now;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000429 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;
Phil Reid19d857c2015-12-14 11:32:01 +0800439 u32 sec_inc;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000440
441 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
442 netdev_alert(priv->dev, "No support for HW time stamping\n");
443 priv->hwts_tx_en = 0;
444 priv->hwts_rx_en = 0;
445
446 return -EOPNOTSUPP;
447 }
448
449 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000450 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000451 return -EFAULT;
452
453 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
454 __func__, config.flags, config.tx_type, config.rx_filter);
455
456 /* reserved for future extensions */
457 if (config.flags)
458 return -EINVAL;
459
Ben Hutchings5f3da322013-11-14 00:43:41 +0000460 if (config.tx_type != HWTSTAMP_TX_OFF &&
461 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000462 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000463
464 if (priv->adv_ts) {
465 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000466 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000467 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000468 config.rx_filter = HWTSTAMP_FILTER_NONE;
469 break;
470
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000471 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000472 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000473 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
474 /* take time stamp for all event messages */
475 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
476
477 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
478 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
479 break;
480
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000481 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000482 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000483 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
484 /* take time stamp for SYNC messages only */
485 ts_event_en = PTP_TCR_TSEVNTENA;
486
487 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
488 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
489 break;
490
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000491 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000492 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000493 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
494 /* take time stamp for Delay_Req messages only */
495 ts_master_en = PTP_TCR_TSMSTRENA;
496 ts_event_en = PTP_TCR_TSEVNTENA;
497
498 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
499 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
500 break;
501
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000502 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000503 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000504 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
505 ptp_v2 = PTP_TCR_TSVER2ENA;
506 /* take time stamp for all event messages */
507 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
508
509 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
510 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
511 break;
512
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000513 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000514 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000515 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
516 ptp_v2 = PTP_TCR_TSVER2ENA;
517 /* take time stamp for SYNC messages only */
518 ts_event_en = PTP_TCR_TSEVNTENA;
519
520 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
521 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
522 break;
523
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000524 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000525 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000526 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
527 ptp_v2 = PTP_TCR_TSVER2ENA;
528 /* take time stamp for Delay_Req messages only */
529 ts_master_en = PTP_TCR_TSMSTRENA;
530 ts_event_en = PTP_TCR_TSEVNTENA;
531
532 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
533 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
534 break;
535
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000536 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000537 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000538 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
539 ptp_v2 = PTP_TCR_TSVER2ENA;
540 /* take time stamp for all event messages */
541 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
542
543 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
544 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
545 ptp_over_ethernet = PTP_TCR_TSIPENA;
546 break;
547
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000548 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000549 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000550 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
551 ptp_v2 = PTP_TCR_TSVER2ENA;
552 /* take time stamp for SYNC messages only */
553 ts_event_en = PTP_TCR_TSEVNTENA;
554
555 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
556 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
557 ptp_over_ethernet = PTP_TCR_TSIPENA;
558 break;
559
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000560 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000561 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000562 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
563 ptp_v2 = PTP_TCR_TSVER2ENA;
564 /* take time stamp for Delay_Req messages only */
565 ts_master_en = PTP_TCR_TSMSTRENA;
566 ts_event_en = PTP_TCR_TSEVNTENA;
567
568 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
569 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
570 ptp_over_ethernet = PTP_TCR_TSIPENA;
571 break;
572
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000573 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000574 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000575 config.rx_filter = HWTSTAMP_FILTER_ALL;
576 tstamp_all = PTP_TCR_TSENALL;
577 break;
578
579 default:
580 return -ERANGE;
581 }
582 } else {
583 switch (config.rx_filter) {
584 case HWTSTAMP_FILTER_NONE:
585 config.rx_filter = HWTSTAMP_FILTER_NONE;
586 break;
587 default:
588 /* PTP v1, UDP, any kind of event packet */
589 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
590 break;
591 }
592 }
593 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000594 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000595
596 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
597 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
598 else {
599 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000600 tstamp_all | ptp_v2 | ptp_over_ethernet |
601 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
602 ts_master_en | snap_type_sel);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000603 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
604
605 /* program Sub Second Increment reg */
Phil Reid19d857c2015-12-14 11:32:01 +0800606 sec_inc = priv->hw->ptp->config_sub_second_increment(
607 priv->ioaddr, priv->clk_ptp_rate);
608 temp = div_u64(1000000000ULL, sec_inc);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000609
610 /* calculate default added value:
611 * formula is :
612 * addend = (2^32)/freq_div_ratio;
Phil Reid19d857c2015-12-14 11:32:01 +0800613 * where, freq_div_ratio = 1e9ns/sec_inc
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000614 */
Phil Reid19d857c2015-12-14 11:32:01 +0800615 temp = (u64)(temp << 32);
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200616 priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000617 priv->hw->ptp->config_addend(priv->ioaddr,
618 priv->default_addend);
619
620 /* initialize system time */
Arnd Bergmann0a624152015-09-30 13:26:32 +0200621 ktime_get_real_ts64(&now);
622
623 /* lower 32 bits of tv_sec are safe until y2106 */
624 priv->hw->ptp->init_systime(priv->ioaddr, (u32)now.tv_sec,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000625 now.tv_nsec);
626 }
627
628 return copy_to_user(ifr->ifr_data, &config,
629 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
630}
631
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000632/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100633 * stmmac_init_ptp - init PTP
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000634 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100635 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000636 * This is done by looking at the HW cap. register.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100637 * This function also registers the ptp driver.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000638 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000639static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000640{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000641 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
642 return -EOPNOTSUPP;
643
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200644 /* Fall-back to main clock in case of no PTP ref is passed */
645 priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
646 if (IS_ERR(priv->clk_ptp_ref)) {
647 priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
648 priv->clk_ptp_ref = NULL;
649 } else {
650 clk_prepare_enable(priv->clk_ptp_ref);
651 priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
652 }
653
Vince Bridgers7cd01392013-12-20 11:19:34 -0600654 priv->adv_ts = 0;
655 if (priv->dma_cap.atime_stamp && priv->extend_desc)
656 priv->adv_ts = 1;
657
658 if (netif_msg_hw(priv) && priv->dma_cap.time_stamp)
659 pr_debug("IEEE 1588-2002 Time Stamp supported\n");
660
661 if (netif_msg_hw(priv) && priv->adv_ts)
662 pr_debug("IEEE 1588-2008 Advanced Time Stamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000663
664 priv->hw->ptp = &stmmac_ptp;
665 priv->hwts_tx_en = 0;
666 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000667
668 return stmmac_ptp_register(priv);
669}
670
671static void stmmac_release_ptp(struct stmmac_priv *priv)
672{
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200673 if (priv->clk_ptp_ref)
674 clk_disable_unprepare(priv->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000675 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000676}
677
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700678/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100679 * stmmac_adjust_link - adjusts the link parameters
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700680 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100681 * Description: this is the helper called by the physical abstraction layer
682 * drivers to communicate the phy link status. According the speed and duplex
683 * this driver can invoke registered glue-logic as well.
684 * It also invoke the eee initialization because it could happen when switch
685 * on different networks (that are eee capable).
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700686 */
687static void stmmac_adjust_link(struct net_device *dev)
688{
689 struct stmmac_priv *priv = netdev_priv(dev);
690 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700691 unsigned long flags;
692 int new_state = 0;
693 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
694
695 if (phydev == NULL)
696 return;
697
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700698 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000699
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700700 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000701 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700702
703 /* Now we make sure that we can be in full duplex mode.
704 * If not, we operate in half-duplex mode. */
705 if (phydev->duplex != priv->oldduplex) {
706 new_state = 1;
707 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000708 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700709 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000710 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700711 priv->oldduplex = phydev->duplex;
712 }
713 /* Flow Control operation */
714 if (phydev->pause)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500715 priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000716 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700717
718 if (phydev->speed != priv->speed) {
719 new_state = 1;
720 switch (phydev->speed) {
721 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000722 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000723 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000724 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700725 break;
726 case 100:
727 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000728 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000729 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700730 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000731 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700732 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000733 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700734 }
735 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000736 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700737 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000738 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700739 break;
740 default:
741 if (netif_msg_link(priv))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000742 pr_warn("%s: Speed (%d) not 10/100\n",
743 dev->name, phydev->speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700744 break;
745 }
746
747 priv->speed = phydev->speed;
748 }
749
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000750 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700751
752 if (!priv->oldlink) {
753 new_state = 1;
754 priv->oldlink = 1;
755 }
756 } else if (priv->oldlink) {
757 new_state = 1;
758 priv->oldlink = 0;
759 priv->speed = 0;
760 priv->oldduplex = -1;
761 }
762
763 if (new_state && netif_msg_link(priv))
764 phy_print_status(phydev);
765
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100766 spin_unlock_irqrestore(&priv->lock, flags);
767
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200768 /* At this stage, it could be needed to setup the EEE or adjust some
769 * MAC related HW registers.
770 */
771 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700772}
773
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000774/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100775 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000776 * @priv: driver private structure
777 * Description: this is to verify if the HW supports the PCS.
778 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
779 * configured for the TBI, RTBI, or SGMII PHY interface.
780 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000781static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
782{
783 int interface = priv->plat->interface;
784
785 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900786 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
787 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
788 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
789 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000790 pr_debug("STMMAC: PCS RGMII support enable\n");
791 priv->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900792 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000793 pr_debug("STMMAC: PCS SGMII support enable\n");
794 priv->pcs = STMMAC_PCS_SGMII;
795 }
796 }
797}
798
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700799/**
800 * stmmac_init_phy - PHY initialization
801 * @dev: net device structure
802 * Description: it initializes the driver's PHY state, and attaches the PHY
803 * to the mac driver.
804 * Return value:
805 * 0 on success
806 */
807static int stmmac_init_phy(struct net_device *dev)
808{
809 struct stmmac_priv *priv = netdev_priv(dev);
810 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000811 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000812 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000813 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000814 int max_speed = priv->plat->max_speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700815 priv->oldlink = 0;
816 priv->speed = 0;
817 priv->oldduplex = -1;
818
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700819 if (priv->plat->phy_node) {
820 phydev = of_phy_connect(dev, priv->plat->phy_node,
821 &stmmac_adjust_link, 0, interface);
822 } else {
823 if (priv->plat->phy_bus_name)
824 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
825 priv->plat->phy_bus_name, priv->plat->bus_id);
826 else
827 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
828 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000829
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700830 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
831 priv->plat->phy_addr);
832 pr_debug("stmmac_init_phy: trying to attach to %s\n",
833 phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700834
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700835 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
836 interface);
837 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700838
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300839 if (IS_ERR_OR_NULL(phydev)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700840 pr_err("%s: Could not attach to PHY\n", dev->name);
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300841 if (!phydev)
842 return -ENODEV;
843
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700844 return PTR_ERR(phydev);
845 }
846
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000847 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000848 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000849 (interface == PHY_INTERFACE_MODE_RMII) ||
Pavel Macheka77e4ac2014-08-25 13:31:16 +0200850 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000851 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
852 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000853
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700854 /*
855 * Broken HW is sometimes missing the pull-up resistor on the
856 * MDIO line, which results in reads to non-existent devices returning
857 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
858 * device as well.
859 * Note: phydev->phy_id is the result of reading the UID PHY registers.
860 */
Mathieu Olivari27732382015-05-27 11:02:48 -0700861 if (!priv->plat->phy_node && phydev->phy_id == 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700862 phy_disconnect(phydev);
863 return -ENODEV;
864 }
865 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000866 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700867
868 priv->phydev = phydev;
869
870 return 0;
871}
872
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700873/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100874 * stmmac_display_ring - display ring
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000875 * @head: pointer to the head of the ring passed.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700876 * @size: size of the ring.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000877 * @extend_desc: to verify if extended descriptors are used.
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000878 * Description: display the control/status and buffer descriptors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700879 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000880static void stmmac_display_ring(void *head, int size, int extend_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700881{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700882 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000883 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
884 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000885
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700886 for (i = 0; i < size; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000887 u64 x;
888 if (extend_desc) {
889 x = *(u64 *) ep;
890 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000891 i, (unsigned int)virt_to_phys(ep),
892 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000893 ep->basic.des2, ep->basic.des3);
894 ep++;
895 } else {
896 x = *(u64 *) p;
897 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000898 i, (unsigned int)virt_to_phys(p),
899 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000900 p->des2, p->des3);
901 p++;
902 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700903 pr_info("\n");
904 }
905}
906
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000907static void stmmac_display_rings(struct stmmac_priv *priv)
908{
909 unsigned int txsize = priv->dma_tx_size;
910 unsigned int rxsize = priv->dma_rx_size;
911
912 if (priv->extend_desc) {
913 pr_info("Extended RX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000914 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000915 pr_info("Extended TX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000916 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000917 } else {
918 pr_info("RX descriptor ring:\n");
919 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
920 pr_info("TX descriptor ring:\n");
921 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
922 }
923}
924
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000925static int stmmac_set_bfsize(int mtu, int bufsize)
926{
927 int ret = bufsize;
928
929 if (mtu >= BUF_SIZE_4KiB)
930 ret = BUF_SIZE_8KiB;
931 else if (mtu >= BUF_SIZE_2KiB)
932 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100933 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000934 ret = BUF_SIZE_2KiB;
935 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100936 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000937
938 return ret;
939}
940
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000941/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100942 * stmmac_clear_descriptors - clear descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000943 * @priv: driver private structure
944 * Description: this function is called to clear the tx and rx descriptors
945 * in case of both basic and extended descriptors are used.
946 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000947static void stmmac_clear_descriptors(struct stmmac_priv *priv)
948{
949 int i;
950 unsigned int txsize = priv->dma_tx_size;
951 unsigned int rxsize = priv->dma_rx_size;
952
953 /* Clear the Rx/Tx descriptors */
954 for (i = 0; i < rxsize; i++)
955 if (priv->extend_desc)
956 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
957 priv->use_riwt, priv->mode,
958 (i == rxsize - 1));
959 else
960 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
961 priv->use_riwt, priv->mode,
962 (i == rxsize - 1));
963 for (i = 0; i < txsize; i++)
964 if (priv->extend_desc)
965 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
966 priv->mode,
967 (i == txsize - 1));
968 else
969 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
970 priv->mode,
971 (i == txsize - 1));
972}
973
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100974/**
975 * stmmac_init_rx_buffers - init the RX descriptor buffer.
976 * @priv: driver private structure
977 * @p: descriptor pointer
978 * @i: descriptor index
979 * @flags: gfp flag.
980 * Description: this function is called to allocate a receive buffer, perform
981 * the DMA mapping and init the descriptor.
982 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000983static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +0100984 int i, gfp_t flags)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000985{
986 struct sk_buff *skb;
987
Vineet Gupta4ec49a32015-05-20 12:04:40 +0530988 skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200989 if (!skb) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000990 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200991 return -ENOMEM;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000992 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000993 priv->rx_skbuff[i] = skb;
994 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
995 priv->dma_buf_sz,
996 DMA_FROM_DEVICE);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200997 if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
998 pr_err("%s: DMA mapping error\n", __func__);
999 dev_kfree_skb_any(skb);
1000 return -EINVAL;
1001 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001002
1003 p->des2 = priv->rx_skbuff_dma[i];
1004
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001005 if ((priv->hw->mode->init_desc3) &&
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001006 (priv->dma_buf_sz == BUF_SIZE_16KiB))
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001007 priv->hw->mode->init_desc3(p);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001008
1009 return 0;
1010}
1011
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001012static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
1013{
1014 if (priv->rx_skbuff[i]) {
1015 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
1016 priv->dma_buf_sz, DMA_FROM_DEVICE);
1017 dev_kfree_skb_any(priv->rx_skbuff[i]);
1018 }
1019 priv->rx_skbuff[i] = NULL;
1020}
1021
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001022/**
1023 * init_dma_desc_rings - init the RX/TX descriptor rings
1024 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001025 * @flags: gfp flag.
1026 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001027 * and allocates the socket buffers. It suppors the chained and ring
1028 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001029 */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001030static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001031{
1032 int i;
1033 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001034 unsigned int txsize = priv->dma_tx_size;
1035 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001036 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001037 int ret = -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001038
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001039 if (priv->hw->mode->set_16kib_bfsize)
1040 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001041
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001042 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001043 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001044
Vince Bridgers2618abb2014-01-20 05:39:01 -06001045 priv->dma_buf_sz = bfsize;
1046
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001047 if (netif_msg_probe(priv))
1048 pr_debug("%s: txsize %d, rxsize %d, bfsize %d\n", __func__,
1049 txsize, rxsize, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001050
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001051 if (netif_msg_probe(priv)) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001052 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1053 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001054
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001055 /* RX INITIALIZATION */
1056 pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
1057 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001058 for (i = 0; i < rxsize; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001059 struct dma_desc *p;
1060 if (priv->extend_desc)
1061 p = &((priv->dma_erx + i)->basic);
1062 else
1063 p = priv->dma_rx + i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001064
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001065 ret = stmmac_init_rx_buffers(priv, p, i, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001066 if (ret)
1067 goto err_init_rx_buffers;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001068
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001069 if (netif_msg_probe(priv))
1070 pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1071 priv->rx_skbuff[i]->data,
1072 (unsigned int)priv->rx_skbuff_dma[i]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001073 }
1074 priv->cur_rx = 0;
1075 priv->dirty_rx = (unsigned int)(i - rxsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001076 buf_sz = bfsize;
1077
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001078 /* Setup the chained descriptor addresses */
1079 if (priv->mode == STMMAC_CHAIN_MODE) {
1080 if (priv->extend_desc) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001081 priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
1082 rxsize, 1);
1083 priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
1084 txsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001085 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001086 priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
1087 rxsize, 0);
1088 priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
1089 txsize, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001090 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001091 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001092
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001093 /* TX INITIALIZATION */
1094 for (i = 0; i < txsize; i++) {
1095 struct dma_desc *p;
1096 if (priv->extend_desc)
1097 p = &((priv->dma_etx + i)->basic);
1098 else
1099 p = priv->dma_tx + i;
1100 p->des2 = 0;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001101 priv->tx_skbuff_dma[i].buf = 0;
1102 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001103 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001104 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001105
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001106 priv->dirty_tx = 0;
1107 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001108 netdev_reset_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001109
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001110 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001111
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001112 if (netif_msg_hw(priv))
1113 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001114
1115 return 0;
1116err_init_rx_buffers:
1117 while (--i >= 0)
1118 stmmac_free_rx_buffers(priv, i);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001119 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001120}
1121
1122static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1123{
1124 int i;
1125
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001126 for (i = 0; i < priv->dma_rx_size; i++)
1127 stmmac_free_rx_buffers(priv, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001128}
1129
1130static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1131{
1132 int i;
1133
1134 for (i = 0; i < priv->dma_tx_size; i++) {
damuzi00075e43642014-01-17 23:47:59 +08001135 struct dma_desc *p;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001136
damuzi00075e43642014-01-17 23:47:59 +08001137 if (priv->extend_desc)
1138 p = &((priv->dma_etx + i)->basic);
1139 else
1140 p = priv->dma_tx + i;
1141
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001142 if (priv->tx_skbuff_dma[i].buf) {
1143 if (priv->tx_skbuff_dma[i].map_as_page)
1144 dma_unmap_page(priv->device,
1145 priv->tx_skbuff_dma[i].buf,
1146 priv->hw->desc->get_tx_len(p),
1147 DMA_TO_DEVICE);
1148 else
1149 dma_unmap_single(priv->device,
1150 priv->tx_skbuff_dma[i].buf,
1151 priv->hw->desc->get_tx_len(p),
1152 DMA_TO_DEVICE);
damuzi00075e43642014-01-17 23:47:59 +08001153 }
1154
1155 if (priv->tx_skbuff[i] != NULL) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001156 dev_kfree_skb_any(priv->tx_skbuff[i]);
1157 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001158 priv->tx_skbuff_dma[i].buf = 0;
1159 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001160 }
1161 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001162}
1163
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001164/**
1165 * alloc_dma_desc_resources - alloc TX/RX resources.
1166 * @priv: private structure
1167 * Description: according to which descriptor can be used (extend or basic)
1168 * this function allocates the resources for TX and RX paths. In case of
1169 * reception, for example, it pre-allocated the RX socket buffer in order to
1170 * allow zero-copy mechanism.
1171 */
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001172static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1173{
1174 unsigned int txsize = priv->dma_tx_size;
1175 unsigned int rxsize = priv->dma_rx_size;
1176 int ret = -ENOMEM;
1177
1178 priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
1179 GFP_KERNEL);
1180 if (!priv->rx_skbuff_dma)
1181 return -ENOMEM;
1182
1183 priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
1184 GFP_KERNEL);
1185 if (!priv->rx_skbuff)
1186 goto err_rx_skbuff;
1187
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001188 priv->tx_skbuff_dma = kmalloc_array(txsize,
1189 sizeof(*priv->tx_skbuff_dma),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001190 GFP_KERNEL);
1191 if (!priv->tx_skbuff_dma)
1192 goto err_tx_skbuff_dma;
1193
1194 priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
1195 GFP_KERNEL);
1196 if (!priv->tx_skbuff)
1197 goto err_tx_skbuff;
1198
1199 if (priv->extend_desc) {
Alexey Brodkinf1590672015-06-24 11:47:41 +03001200 priv->dma_erx = dma_zalloc_coherent(priv->device, rxsize *
1201 sizeof(struct
1202 dma_extended_desc),
1203 &priv->dma_rx_phy,
1204 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001205 if (!priv->dma_erx)
1206 goto err_dma;
1207
Alexey Brodkinf1590672015-06-24 11:47:41 +03001208 priv->dma_etx = dma_zalloc_coherent(priv->device, txsize *
1209 sizeof(struct
1210 dma_extended_desc),
1211 &priv->dma_tx_phy,
1212 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001213 if (!priv->dma_etx) {
1214 dma_free_coherent(priv->device, priv->dma_rx_size *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001215 sizeof(struct dma_extended_desc),
1216 priv->dma_erx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001217 goto err_dma;
1218 }
1219 } else {
Alexey Brodkinf1590672015-06-24 11:47:41 +03001220 priv->dma_rx = dma_zalloc_coherent(priv->device, rxsize *
1221 sizeof(struct dma_desc),
1222 &priv->dma_rx_phy,
1223 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001224 if (!priv->dma_rx)
1225 goto err_dma;
1226
Alexey Brodkinf1590672015-06-24 11:47:41 +03001227 priv->dma_tx = dma_zalloc_coherent(priv->device, txsize *
1228 sizeof(struct dma_desc),
1229 &priv->dma_tx_phy,
1230 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001231 if (!priv->dma_tx) {
1232 dma_free_coherent(priv->device, priv->dma_rx_size *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001233 sizeof(struct dma_desc),
1234 priv->dma_rx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001235 goto err_dma;
1236 }
1237 }
1238
1239 return 0;
1240
1241err_dma:
1242 kfree(priv->tx_skbuff);
1243err_tx_skbuff:
1244 kfree(priv->tx_skbuff_dma);
1245err_tx_skbuff_dma:
1246 kfree(priv->rx_skbuff);
1247err_rx_skbuff:
1248 kfree(priv->rx_skbuff_dma);
1249 return ret;
1250}
1251
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001252static void free_dma_desc_resources(struct stmmac_priv *priv)
1253{
1254 /* Release the DMA TX/RX socket buffers */
1255 dma_free_rx_skbufs(priv);
1256 dma_free_tx_skbufs(priv);
1257
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001258 /* Free DMA regions of consistent memory previously allocated */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001259 if (!priv->extend_desc) {
1260 dma_free_coherent(priv->device,
1261 priv->dma_tx_size * sizeof(struct dma_desc),
1262 priv->dma_tx, priv->dma_tx_phy);
1263 dma_free_coherent(priv->device,
1264 priv->dma_rx_size * sizeof(struct dma_desc),
1265 priv->dma_rx, priv->dma_rx_phy);
1266 } else {
1267 dma_free_coherent(priv->device, priv->dma_tx_size *
1268 sizeof(struct dma_extended_desc),
1269 priv->dma_etx, priv->dma_tx_phy);
1270 dma_free_coherent(priv->device, priv->dma_rx_size *
1271 sizeof(struct dma_extended_desc),
1272 priv->dma_erx, priv->dma_rx_phy);
1273 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001274 kfree(priv->rx_skbuff_dma);
1275 kfree(priv->rx_skbuff);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001276 kfree(priv->tx_skbuff_dma);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001277 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001278}
1279
1280/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001281 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001282 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001283 * Description: it is used for configuring the DMA operation mode register in
1284 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001285 */
1286static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1287{
Vince Bridgersf88203a2015-04-15 11:17:42 -05001288 int rxfifosz = priv->plat->rx_fifo_size;
1289
Sonic Zhange2a240c2013-08-28 18:55:39 +08001290 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001291 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc, rxfifosz);
Sonic Zhange2a240c2013-08-28 18:55:39 +08001292 else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001293 /*
1294 * In case of GMAC, SF mode can be enabled
1295 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001296 * 1) TX COE if actually supported
1297 * 2) There is no bugged Jumbo frame support
1298 * that needs to not insert csum in the TDES.
1299 */
Vince Bridgersf88203a2015-04-15 11:17:42 -05001300 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE,
1301 rxfifosz);
Sonic Zhangb2dec112015-01-30 13:49:32 +08001302 priv->xstats.threshold = SF_DMA_MODE;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001303 } else
Vince Bridgersf88203a2015-04-15 11:17:42 -05001304 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE,
1305 rxfifosz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001306}
1307
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001308/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001309 * stmmac_tx_clean - to manage the transmission completion
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001310 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001311 * Description: it reclaims the transmit resources after transmission completes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001312 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001313static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001314{
1315 unsigned int txsize = priv->dma_tx_size;
Beniamino Galvani38979572015-01-21 19:07:27 +01001316 unsigned int bytes_compl = 0, pkts_compl = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001317
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001318 spin_lock(&priv->tx_lock);
1319
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001320 priv->xstats.tx_clean++;
1321
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001322 while (priv->dirty_tx != priv->cur_tx) {
1323 int last;
1324 unsigned int entry = priv->dirty_tx % txsize;
1325 struct sk_buff *skb = priv->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001326 struct dma_desc *p;
1327
1328 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001329 p = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001330 else
1331 p = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001332
1333 /* Check if the descriptor is owned by the DMA. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001334 if (priv->hw->desc->get_tx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001335 break;
1336
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001337 /* Verify tx error by looking at the last segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00001338 last = priv->hw->desc->get_tx_ls(p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001339 if (likely(last)) {
1340 int tx_error =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001341 priv->hw->desc->tx_status(&priv->dev->stats,
1342 &priv->xstats, p,
1343 priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001344 if (likely(tx_error == 0)) {
1345 priv->dev->stats.tx_packets++;
1346 priv->xstats.tx_pkt_n++;
1347 } else
1348 priv->dev->stats.tx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001349
1350 stmmac_get_tx_hwtstamp(priv, entry, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001351 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001352 if (netif_msg_tx_done(priv))
1353 pr_debug("%s: curr %d, dirty %d\n", __func__,
1354 priv->cur_tx, priv->dirty_tx);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001355
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001356 if (likely(priv->tx_skbuff_dma[entry].buf)) {
1357 if (priv->tx_skbuff_dma[entry].map_as_page)
1358 dma_unmap_page(priv->device,
1359 priv->tx_skbuff_dma[entry].buf,
1360 priv->hw->desc->get_tx_len(p),
1361 DMA_TO_DEVICE);
1362 else
1363 dma_unmap_single(priv->device,
1364 priv->tx_skbuff_dma[entry].buf,
1365 priv->hw->desc->get_tx_len(p),
1366 DMA_TO_DEVICE);
1367 priv->tx_skbuff_dma[entry].buf = 0;
1368 priv->tx_skbuff_dma[entry].map_as_page = false;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001369 }
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001370 priv->hw->mode->clean_desc3(priv, p);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001371
1372 if (likely(skb != NULL)) {
Beniamino Galvani38979572015-01-21 19:07:27 +01001373 pkts_compl++;
1374 bytes_compl += skb->len;
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001375 dev_consume_skb_any(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001376 priv->tx_skbuff[entry] = NULL;
1377 }
1378
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001379 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001380
Giuseppe CAVALLARO13497f52012-06-04 06:36:22 +00001381 priv->dirty_tx++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001382 }
Beniamino Galvani38979572015-01-21 19:07:27 +01001383
1384 netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
1385
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001386 if (unlikely(netif_queue_stopped(priv->dev) &&
1387 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
1388 netif_tx_lock(priv->dev);
1389 if (netif_queue_stopped(priv->dev) &&
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001390 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001391 if (netif_msg_tx_done(priv))
1392 pr_debug("%s: restart transmit\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001393 netif_wake_queue(priv->dev);
1394 }
1395 netif_tx_unlock(priv->dev);
1396 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001397
1398 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1399 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001400 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001401 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001402 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001403}
1404
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001405static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001406{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001407 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001408}
1409
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001410static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001411{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001412 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001413}
1414
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001415/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001416 * stmmac_tx_err - to manage the tx error
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001417 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001418 * Description: it cleans the descriptors and restarts the transmission
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001419 * in case of transmission errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001420 */
1421static void stmmac_tx_err(struct stmmac_priv *priv)
1422{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001423 int i;
1424 int txsize = priv->dma_tx_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001425 netif_stop_queue(priv->dev);
1426
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001427 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001428 dma_free_tx_skbufs(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001429 for (i = 0; i < txsize; i++)
1430 if (priv->extend_desc)
1431 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1432 priv->mode,
1433 (i == txsize - 1));
1434 else
1435 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1436 priv->mode,
1437 (i == txsize - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001438 priv->dirty_tx = 0;
1439 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001440 netdev_reset_queue(priv->dev);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001441 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001442
1443 priv->dev->stats.tx_errors++;
1444 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001445}
1446
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001447/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001448 * stmmac_dma_interrupt - DMA ISR
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001449 * @priv: driver private structure
1450 * Description: this is the DMA ISR. It is called by the main ISR.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001451 * It calls the dwmac dma routine and schedule poll method in case of some
1452 * work can be done.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001453 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001454static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001455{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001456 int status;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001457 int rxfifosz = priv->plat->rx_fifo_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001458
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001459 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001460 if (likely((status & handle_rx)) || (status & handle_tx)) {
1461 if (likely(napi_schedule_prep(&priv->napi))) {
1462 stmmac_disable_dma_irq(priv);
1463 __napi_schedule(&priv->napi);
1464 }
1465 }
1466 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001467 /* Try to bump up the dma threshold on this failure */
Sonic Zhangb2dec112015-01-30 13:49:32 +08001468 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
1469 (tc <= 256)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001470 tc += 64;
Sonic Zhangc405abe2015-01-22 14:55:56 +08001471 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001472 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc,
1473 rxfifosz);
Sonic Zhangc405abe2015-01-22 14:55:56 +08001474 else
1475 priv->hw->dma->dma_mode(priv->ioaddr, tc,
Vince Bridgersf88203a2015-04-15 11:17:42 -05001476 SF_DMA_MODE, rxfifosz);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001477 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001478 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001479 } else if (unlikely(status == tx_hard_error))
1480 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001481}
1482
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001483/**
1484 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1485 * @priv: driver private structure
1486 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1487 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001488static void stmmac_mmc_setup(struct stmmac_priv *priv)
1489{
1490 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001491 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001492
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001493 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001494
1495 if (priv->dma_cap.rmon) {
1496 dwmac_mmc_ctrl(priv->ioaddr, mode);
1497 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1498 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +00001499 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001500}
1501
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001502/**
1503 * stmmac_get_synopsys_id - return the SYINID.
1504 * @priv: driver private structure
1505 * Description: this simple function is to decode and return the SYINID
1506 * starting from the HW core register.
1507 */
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001508static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
1509{
1510 u32 hwid = priv->hw->synopsys_uid;
1511
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001512 /* Check Synopsys Id (not available on old chips) */
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001513 if (likely(hwid)) {
1514 u32 uid = ((hwid & 0x0000ff00) >> 8);
1515 u32 synid = (hwid & 0x000000ff);
1516
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001517 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001518 uid, synid);
1519
1520 return synid;
1521 }
1522 return 0;
1523}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001524
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001525/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001526 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001527 * @priv: driver private structure
1528 * Description: select the Enhanced/Alternate or Normal descriptors.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001529 * In case of Enhanced/Alternate, it checks if the extended descriptors are
1530 * supported by the HW capability register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001531 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001532static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1533{
1534 if (priv->plat->enh_desc) {
1535 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001536
1537 /* GMAC older than 3.50 has no extended descriptors */
1538 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1539 pr_info("\tEnabled extended descriptors\n");
1540 priv->extend_desc = 1;
1541 } else
1542 pr_warn("Extended descriptors not supported\n");
1543
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001544 priv->hw->desc = &enh_desc_ops;
1545 } else {
1546 pr_info(" Normal descriptors\n");
1547 priv->hw->desc = &ndesc_ops;
1548 }
1549}
1550
1551/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001552 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001553 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001554 * Description:
1555 * new GMAC chip generations have a new register to indicate the
1556 * presence of the optional feature/functions.
1557 * This can be also used to override the value passed through the
1558 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001559 */
1560static int stmmac_get_hw_features(struct stmmac_priv *priv)
1561{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001562 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001563
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001564 if (priv->hw->dma->get_hw_feature) {
1565 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001566
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001567 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
1568 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
1569 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
1570 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001571 priv->dma_cap.multi_addr = (hw_cap & DMA_HW_FEAT_ADDMAC) >> 5;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001572 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
1573 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
1574 priv->dma_cap.pmt_remote_wake_up =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001575 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001576 priv->dma_cap.pmt_magic_frame =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001577 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001578 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001579 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001580 /* IEEE 1588-2002 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001581 priv->dma_cap.time_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001582 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
1583 /* IEEE 1588-2008 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001584 priv->dma_cap.atime_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001585 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001586 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001587 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
1588 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001589 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001590 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
1591 priv->dma_cap.rx_coe_type1 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001592 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001593 priv->dma_cap.rx_coe_type2 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001594 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001595 priv->dma_cap.rxfifo_over_2048 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001596 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001597 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001598 priv->dma_cap.number_rx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001599 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001600 priv->dma_cap.number_tx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001601 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
1602 /* Alternate (enhanced) DESC mode */
1603 priv->dma_cap.enh_desc = (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001604 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001605
1606 return hw_cap;
1607}
1608
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001609/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001610 * stmmac_check_ether_addr - check if the MAC addr is valid
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001611 * @priv: driver private structure
1612 * Description:
1613 * it is to verify if the MAC address is valid, in case of failures it
1614 * generates a random MAC address
1615 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001616static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1617{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001618 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001619 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001620 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001621 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001622 eth_hw_addr_random(priv->dev);
Hans de Goedec88460b2014-01-26 15:50:44 +01001623 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1624 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001625 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001626}
1627
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001628/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001629 * stmmac_init_dma_engine - DMA init.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001630 * @priv: driver private structure
1631 * Description:
1632 * It inits the DMA invoking the specific MAC/GMAC callback.
1633 * Some DMA parameters can be passed from the platform;
1634 * in case of these are not passed a default is kept for the MAC or GMAC.
1635 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001636static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1637{
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001638 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, aal = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001639 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001640 int atds = 0;
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001641 int ret = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001642
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001643 if (priv->plat->dma_cfg) {
1644 pbl = priv->plat->dma_cfg->pbl;
1645 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001646 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001647 aal = priv->plat->dma_cfg->aal;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001648 }
1649
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001650 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1651 atds = 1;
1652
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001653 ret = priv->hw->dma->reset(priv->ioaddr);
1654 if (ret) {
1655 dev_err(priv->device, "Failed to reset the dma\n");
1656 return ret;
1657 }
1658
1659 priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001660 aal, priv->dma_tx_phy, priv->dma_rx_phy, atds);
1661
1662 if ((priv->synopsys_id >= DWMAC_CORE_3_50) &&
1663 (priv->plat->axi && priv->hw->dma->axi))
1664 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
1665
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001666 return ret;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001667}
1668
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001669/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001670 * stmmac_tx_timer - mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001671 * @data: data pointer
1672 * Description:
1673 * This is the timer handler to directly invoke the stmmac_tx_clean.
1674 */
1675static void stmmac_tx_timer(unsigned long data)
1676{
1677 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1678
1679 stmmac_tx_clean(priv);
1680}
1681
1682/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001683 * stmmac_init_tx_coalesce - init tx mitigation options.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001684 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001685 * Description:
1686 * This inits the transmit coalesce parameters: i.e. timer rate,
1687 * timer handler and default threshold used for enabling the
1688 * interrupt on completion bit.
1689 */
1690static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1691{
1692 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1693 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1694 init_timer(&priv->txtimer);
1695 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1696 priv->txtimer.data = (unsigned long)priv;
1697 priv->txtimer.function = stmmac_tx_timer;
1698 add_timer(&priv->txtimer);
1699}
1700
1701/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001702 * stmmac_hw_setup - setup mac in a usable state.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001703 * @dev : pointer to the device structure.
1704 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001705 * this is the main function to setup the HW in a usable state because the
1706 * dma engine is reset, the core registers are configured (e.g. AXI,
1707 * Checksum features, timers). The DMA is ready to start receiving and
1708 * transmitting.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001709 * Return value:
1710 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1711 * file on failure.
1712 */
Huacai Chenfe1319292014-12-19 22:38:18 +08001713static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001714{
1715 struct stmmac_priv *priv = netdev_priv(dev);
1716 int ret;
1717
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001718 /* DMA initialization and SW reset */
1719 ret = stmmac_init_dma_engine(priv);
1720 if (ret < 0) {
1721 pr_err("%s: DMA engine initialization failed\n", __func__);
1722 return ret;
1723 }
1724
1725 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001726 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001727
1728 /* If required, perform hw setup of the bus. */
1729 if (priv->plat->bus_setup)
1730 priv->plat->bus_setup(priv->ioaddr);
1731
1732 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001733 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001734
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001735 ret = priv->hw->mac->rx_ipc(priv->hw);
1736 if (!ret) {
1737 pr_warn(" RX IPC Checksum Offload disabled\n");
1738 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02001739 priv->hw->rx_csum = 0;
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001740 }
1741
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001742 /* Enable the MAC Rx/Tx */
1743 stmmac_set_mac(priv->ioaddr, true);
1744
1745 /* Set the HW DMA mode and the COE */
1746 stmmac_dma_operation_mode(priv);
1747
1748 stmmac_mmc_setup(priv);
1749
Huacai Chenfe1319292014-12-19 22:38:18 +08001750 if (init_ptp) {
1751 ret = stmmac_init_ptp(priv);
1752 if (ret && ret != -EOPNOTSUPP)
1753 pr_warn("%s: failed PTP initialisation\n", __func__);
1754 }
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001755
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001756#ifdef CONFIG_DEBUG_FS
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001757 ret = stmmac_init_fs(dev);
1758 if (ret < 0)
1759 pr_warn("%s: failed debugFS registration\n", __func__);
1760#endif
1761 /* Start the ball rolling... */
1762 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1763 priv->hw->dma->start_tx(priv->ioaddr);
1764 priv->hw->dma->start_rx(priv->ioaddr);
1765
1766 /* Dump DMA/MAC registers */
1767 if (netif_msg_hw(priv)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001768 priv->hw->mac->dump_regs(priv->hw);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001769 priv->hw->dma->dump_regs(priv->ioaddr);
1770 }
1771 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1772
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001773 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1774 priv->rx_riwt = MAX_DMA_RIWT;
1775 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1776 }
1777
1778 if (priv->pcs && priv->hw->mac->ctrl_ane)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001779 priv->hw->mac->ctrl_ane(priv->hw, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001780
1781 return 0;
1782}
1783
1784/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001785 * stmmac_open - open entry point of the driver
1786 * @dev : pointer to the device structure.
1787 * Description:
1788 * This function is the open entry point of the driver.
1789 * Return value:
1790 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1791 * file on failure.
1792 */
1793static int stmmac_open(struct net_device *dev)
1794{
1795 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001796 int ret;
1797
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001798 stmmac_check_ether_addr(priv);
1799
Byungho An4d8f0822013-04-07 17:56:16 +00001800 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1801 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001802 ret = stmmac_init_phy(dev);
1803 if (ret) {
1804 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1805 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02001806 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001807 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001808 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001809
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001810 /* Extra statistics */
1811 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1812 priv->xstats.threshold = tc;
1813
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001814 /* Create and initialize the TX/RX descriptors chains. */
1815 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1816 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1817 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001818
Tobias Klauser7262b7b2014-02-22 13:09:03 +01001819 ret = alloc_dma_desc_resources(priv);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001820 if (ret < 0) {
1821 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1822 goto dma_desc_error;
1823 }
1824
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001825 ret = init_dma_desc_rings(dev, GFP_KERNEL);
1826 if (ret < 0) {
1827 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1828 goto init_error;
1829 }
1830
Huacai Chenfe1319292014-12-19 22:38:18 +08001831 ret = stmmac_hw_setup(dev, true);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001832 if (ret < 0) {
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001833 pr_err("%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001834 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001835 }
1836
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001837 stmmac_init_tx_coalesce(priv);
1838
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001839 if (priv->phydev)
1840 phy_start(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001841
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001842 /* Request the IRQ lines */
1843 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001844 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001845 if (unlikely(ret < 0)) {
1846 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1847 __func__, dev->irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001848 goto init_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001849 }
1850
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001851 /* Request the Wake IRQ in case of another line is used for WoL */
1852 if (priv->wol_irq != dev->irq) {
1853 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1854 IRQF_SHARED, dev->name, dev);
1855 if (unlikely(ret < 0)) {
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001856 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1857 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001858 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001859 }
1860 }
1861
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001862 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001863 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001864 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1865 dev->name, dev);
1866 if (unlikely(ret < 0)) {
1867 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1868 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001869 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001870 }
1871 }
1872
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001873 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001874 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001875
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001876 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001877
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001878lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001879 if (priv->wol_irq != dev->irq)
1880 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001881wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001882 free_irq(dev->irq, dev);
1883
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001884init_error:
1885 free_dma_desc_resources(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001886dma_desc_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001887 if (priv->phydev)
1888 phy_disconnect(priv->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001889
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001890 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001891}
1892
1893/**
1894 * stmmac_release - close entry point of the driver
1895 * @dev : device pointer.
1896 * Description:
1897 * This is the stop entry point of the driver.
1898 */
1899static int stmmac_release(struct net_device *dev)
1900{
1901 struct stmmac_priv *priv = netdev_priv(dev);
1902
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001903 if (priv->eee_enabled)
1904 del_timer_sync(&priv->eee_ctrl_timer);
1905
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001906 /* Stop and disconnect the PHY */
1907 if (priv->phydev) {
1908 phy_stop(priv->phydev);
1909 phy_disconnect(priv->phydev);
1910 priv->phydev = NULL;
1911 }
1912
1913 netif_stop_queue(dev);
1914
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001915 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001916
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001917 del_timer_sync(&priv->txtimer);
1918
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001919 /* Free the IRQ lines */
1920 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001921 if (priv->wol_irq != dev->irq)
1922 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001923 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001924 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001925
1926 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001927 priv->hw->dma->stop_tx(priv->ioaddr);
1928 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001929
1930 /* Release and free the Rx/Tx resources */
1931 free_dma_desc_resources(priv);
1932
avisconti19449bf2010-10-25 18:58:14 +00001933 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001934 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001935
1936 netif_carrier_off(dev);
1937
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001938#ifdef CONFIG_DEBUG_FS
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07001939 stmmac_exit_fs(dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001940#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001941
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00001942 stmmac_release_ptp(priv);
1943
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001944 return 0;
1945}
1946
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001947/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001948 * stmmac_xmit - Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001949 * @skb : the socket buffer
1950 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001951 * Description : this is the tx entry point of the driver.
1952 * It programs the chain or the ring and supports oversized frames
1953 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001954 */
1955static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1956{
1957 struct stmmac_priv *priv = netdev_priv(dev);
1958 unsigned int txsize = priv->dma_tx_size;
Andrzej Hajda23c24122015-09-21 15:33:51 +02001959 int entry;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001960 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001961 int nfrags = skb_shinfo(skb)->nr_frags;
1962 struct dma_desc *desc, *first;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001963 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001964 unsigned int enh_desc = priv->plat->enh_desc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001965
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001966 spin_lock(&priv->tx_lock);
1967
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001968 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001969 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001970 if (!netif_queue_stopped(dev)) {
1971 netif_stop_queue(dev);
1972 /* This is a hard error, log it. */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001973 pr_err("%s: Tx Ring full when queue awake\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001974 }
1975 return NETDEV_TX_BUSY;
1976 }
1977
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001978 if (priv->tx_path_in_lpi_mode)
1979 stmmac_disable_eee_mode(priv);
1980
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001981 entry = priv->cur_tx % txsize;
1982
Michał Mirosław5e982f32011-04-09 02:46:55 +00001983 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001984
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001985 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001986 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001987 else
1988 desc = priv->dma_tx + entry;
1989
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001990 first = desc;
1991
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001992 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001993 if (enh_desc)
1994 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
1995
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001996 if (likely(!is_jumbo)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001997 desc->des2 = dma_map_single(priv->device, skb->data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001998 nopaged_len, DMA_TO_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001999 if (dma_mapping_error(priv->device, desc->des2))
2000 goto dma_map_err;
2001 priv->tx_skbuff_dma[entry].buf = desc->des2;
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00002002 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002003 csum_insertion, priv->mode);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002004 } else {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002005 desc = first;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002006 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002007 if (unlikely(entry < 0))
2008 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002009 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002010
2011 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002012 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2013 int len = skb_frag_size(frag);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002014
damuzi00075e43642014-01-17 23:47:59 +08002015 priv->tx_skbuff[entry] = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002016 entry = (++priv->cur_tx) % txsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002017 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002018 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002019 else
2020 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002021
Ian Campbellf7223802011-09-21 21:53:20 +00002022 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
2023 DMA_TO_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002024 if (dma_mapping_error(priv->device, desc->des2))
2025 goto dma_map_err; /* should reuse desc w/o issues */
2026
2027 priv->tx_skbuff_dma[entry].buf = desc->des2;
2028 priv->tx_skbuff_dma[entry].map_as_page = true;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002029 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
2030 priv->mode);
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002031 wmb();
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00002032 priv->hw->desc->set_tx_owner(desc);
Deepak Sikri8e839892012-07-08 21:14:45 +00002033 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002034 }
2035
damuzi00075e43642014-01-17 23:47:59 +08002036 priv->tx_skbuff[entry] = skb;
2037
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002038 /* Finalize the latest segment. */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00002039 priv->hw->desc->close_tx_desc(desc);
Giuseppe CAVALLARO73cfe262009-11-22 22:59:56 +00002040
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002041 wmb();
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002042 /* According to the coalesce parameter the IC bit for the latest
2043 * segment could be reset and the timer re-started to invoke the
2044 * stmmac_tx function. This approach takes care about the fragments.
2045 */
2046 priv->tx_count_frames += nfrags + 1;
2047 if (priv->tx_coal_frames > priv->tx_count_frames) {
2048 priv->hw->desc->clear_tx_ic(desc);
2049 priv->xstats.tx_reset_ic_bit++;
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002050 mod_timer(&priv->txtimer,
2051 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2052 } else
2053 priv->tx_count_frames = 0;
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002054
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002055 /* To avoid raise condition */
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +00002056 priv->hw->desc->set_tx_owner(first);
Deepak Sikri8e839892012-07-08 21:14:45 +00002057 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002058
2059 priv->cur_tx++;
2060
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002061 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002062 pr_debug("%s: curr %d dirty=%d entry=%d, first=%p, nfrags=%d",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002063 __func__, (priv->cur_tx % txsize),
2064 (priv->dirty_tx % txsize), entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002065
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002066 if (priv->extend_desc)
2067 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
2068 else
2069 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
2070
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002071 pr_debug(">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002072 print_pkt(skb->data, skb->len);
2073 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002074 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002075 if (netif_msg_hw(priv))
2076 pr_debug("%s: stop transmitted packets\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002077 netif_stop_queue(dev);
2078 }
2079
2080 dev->stats.tx_bytes += skb->len;
2081
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002082 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2083 priv->hwts_tx_en)) {
2084 /* declare that device is doing timestamping */
2085 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2086 priv->hw->desc->enable_tx_timestamp(first);
2087 }
2088
2089 if (!priv->hwts_tx_en)
2090 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00002091
Beniamino Galvani38979572015-01-21 19:07:27 +01002092 netdev_sent_queue(dev, skb->len);
Richard Cochran52f64fa2011-06-19 03:31:43 +00002093 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2094
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002095 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002096 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002097
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002098dma_map_err:
Fabrice Gasnier758a0ab2014-11-04 17:08:06 +01002099 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002100 dev_err(priv->device, "Tx dma map failed\n");
2101 dev_kfree_skb(skb);
2102 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002103 return NETDEV_TX_OK;
2104}
2105
Vince Bridgersb9381982014-01-14 13:42:05 -06002106static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2107{
2108 struct ethhdr *ehdr;
2109 u16 vlanid;
2110
2111 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2112 NETIF_F_HW_VLAN_CTAG_RX &&
2113 !__vlan_get_tag(skb, &vlanid)) {
2114 /* pop the vlan tag */
2115 ehdr = (struct ethhdr *)skb->data;
2116 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2117 skb_pull(skb, VLAN_HLEN);
2118 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2119 }
2120}
2121
2122
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002123/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002124 * stmmac_rx_refill - refill used skb preallocated buffers
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002125 * @priv: driver private structure
2126 * Description : this is to reallocate the skb for the reception process
2127 * that is based on zero-copy.
2128 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002129static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2130{
2131 unsigned int rxsize = priv->dma_rx_size;
2132 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002133
2134 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
2135 unsigned int entry = priv->dirty_rx % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002136 struct dma_desc *p;
2137
2138 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002139 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002140 else
2141 p = priv->dma_rx + entry;
2142
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002143 if (likely(priv->rx_skbuff[entry] == NULL)) {
2144 struct sk_buff *skb;
2145
Eric Dumazetacb600d2012-10-05 06:23:55 +00002146 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002147
2148 if (unlikely(skb == NULL))
2149 break;
2150
2151 priv->rx_skbuff[entry] = skb;
2152 priv->rx_skbuff_dma[entry] =
2153 dma_map_single(priv->device, skb->data, bfsize,
2154 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002155 if (dma_mapping_error(priv->device,
2156 priv->rx_skbuff_dma[entry])) {
2157 dev_err(priv->device, "Rx dma map failed\n");
2158 dev_kfree_skb(skb);
2159 break;
2160 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002161 p->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002162
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002163 priv->hw->mode->refill_desc3(priv, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002164
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002165 if (netif_msg_rx_status(priv))
2166 pr_debug("\trefill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002167 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002168 wmb();
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002169 priv->hw->desc->set_rx_owner(p);
Deepak Sikri8e839892012-07-08 21:14:45 +00002170 wmb();
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002171 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002172}
2173
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002174/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002175 * stmmac_rx - manage the receive process
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002176 * @priv: driver private structure
2177 * @limit: napi bugget.
2178 * Description : this the function called by the napi poll method.
2179 * It gets all the frames inside the ring.
2180 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002181static int stmmac_rx(struct stmmac_priv *priv, int limit)
2182{
2183 unsigned int rxsize = priv->dma_rx_size;
2184 unsigned int entry = priv->cur_rx % rxsize;
2185 unsigned int next_entry;
2186 unsigned int count = 0;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002187 int coe = priv->hw->rx_csum;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002188
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002189 if (netif_msg_rx_status(priv)) {
2190 pr_debug("%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002191 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002192 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002193 else
2194 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002195 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002196 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002197 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002198 struct dma_desc *p;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002199
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002200 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002201 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002202 else
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002203 p = priv->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002204
2205 if (priv->hw->desc->get_rx_owner(p))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002206 break;
2207
2208 count++;
2209
2210 next_entry = (++priv->cur_rx) % rxsize;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002211 if (priv->extend_desc)
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002212 prefetch(priv->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002213 else
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002214 prefetch(priv->dma_rx + next_entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002215
2216 /* read the status of the incoming frame */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002217 status = priv->hw->desc->rx_status(&priv->dev->stats,
2218 &priv->xstats, p);
2219 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2220 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2221 &priv->xstats,
2222 priv->dma_erx +
2223 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002224 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002225 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002226 if (priv->hwts_rx_en && !priv->extend_desc) {
2227 /* DESC2 & DESC3 will be overwitten by device
2228 * with timestamp value, hence reinitialize
2229 * them in stmmac_rx_refill() function so that
2230 * device can reuse it.
2231 */
2232 priv->rx_skbuff[entry] = NULL;
2233 dma_unmap_single(priv->device,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002234 priv->rx_skbuff_dma[entry],
2235 priv->dma_buf_sz,
2236 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002237 }
2238 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002239 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002240 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002241
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002242 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2243
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01002244 /* check if frame_len fits the preallocated memory */
2245 if (frame_len > priv->dma_buf_sz) {
2246 priv->dev->stats.rx_length_errors++;
2247 break;
2248 }
2249
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002250 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002251 * Type frames (LLC/LLC-SNAP)
2252 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002253 if (unlikely(status != llc_snap))
2254 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002255
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002256 if (netif_msg_rx_status(priv)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002257 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002258 p, entry, p->des2);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002259 if (frame_len > ETH_FRAME_LEN)
2260 pr_debug("\tframe size %d, COE: %d\n",
2261 frame_len, status);
2262 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002263 skb = priv->rx_skbuff[entry];
2264 if (unlikely(!skb)) {
2265 pr_err("%s: Inconsistent Rx descriptor chain\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002266 priv->dev->name);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002267 priv->dev->stats.rx_dropped++;
2268 break;
2269 }
2270 prefetch(skb->data - NET_IP_ALIGN);
2271 priv->rx_skbuff[entry] = NULL;
2272
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002273 stmmac_get_rx_hwtstamp(priv, entry, skb);
2274
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002275 skb_put(skb, frame_len);
2276 dma_unmap_single(priv->device,
2277 priv->rx_skbuff_dma[entry],
2278 priv->dma_buf_sz, DMA_FROM_DEVICE);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002279
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002280 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002281 pr_debug("frame received (%dbytes)", frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002282 print_pkt(skb->data, frame_len);
2283 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002284
Vince Bridgersb9381982014-01-14 13:42:05 -06002285 stmmac_rx_vlan(priv->dev, skb);
2286
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002287 skb->protocol = eth_type_trans(skb, priv->dev);
2288
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002289 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002290 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002291 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002292 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002293
2294 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002295
2296 priv->dev->stats.rx_packets++;
2297 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002298 }
2299 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002300 }
2301
2302 stmmac_rx_refill(priv);
2303
2304 priv->xstats.rx_pkt_n += count;
2305
2306 return count;
2307}
2308
2309/**
2310 * stmmac_poll - stmmac poll method (NAPI)
2311 * @napi : pointer to the napi structure.
2312 * @budget : maximum number of packets that the current CPU can receive from
2313 * all interfaces.
2314 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002315 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002316 */
2317static int stmmac_poll(struct napi_struct *napi, int budget)
2318{
2319 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2320 int work_done = 0;
2321
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002322 priv->xstats.napi_poll++;
2323 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002324
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002325 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002326 if (work_done < budget) {
2327 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002328 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002329 }
2330 return work_done;
2331}
2332
2333/**
2334 * stmmac_tx_timeout
2335 * @dev : Pointer to net device structure
2336 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00002337 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002338 * netdev structure and arrange for the device to be reset to a sane state
2339 * in order to transmit a new packet.
2340 */
2341static void stmmac_tx_timeout(struct net_device *dev)
2342{
2343 struct stmmac_priv *priv = netdev_priv(dev);
2344
2345 /* Clear Tx resources and restart transmitting again */
2346 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002347}
2348
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002349/**
Jiri Pirko01789342011-08-16 06:29:00 +00002350 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002351 * @dev : pointer to the device structure
2352 * Description:
2353 * This function is a driver entry point which gets called by the kernel
2354 * whenever multicast addresses must be enabled/disabled.
2355 * Return value:
2356 * void.
2357 */
Jiri Pirko01789342011-08-16 06:29:00 +00002358static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002359{
2360 struct stmmac_priv *priv = netdev_priv(dev);
2361
Vince Bridgers3b57de92014-07-31 15:49:17 -05002362 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002363}
2364
2365/**
2366 * stmmac_change_mtu - entry point to change MTU size for the device.
2367 * @dev : device pointer.
2368 * @new_mtu : the new MTU size for the device.
2369 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2370 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2371 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2372 * Return value:
2373 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2374 * file on failure.
2375 */
2376static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2377{
2378 struct stmmac_priv *priv = netdev_priv(dev);
2379 int max_mtu;
2380
2381 if (netif_running(dev)) {
2382 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2383 return -EBUSY;
2384 }
2385
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00002386 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002387 max_mtu = JUMBO_LEN;
2388 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00002389 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002390
Vince Bridgers2618abb2014-01-20 05:39:01 -06002391 if (priv->plat->maxmtu < max_mtu)
2392 max_mtu = priv->plat->maxmtu;
2393
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002394 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2395 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2396 return -EINVAL;
2397 }
2398
Michał Mirosław5e982f32011-04-09 02:46:55 +00002399 dev->mtu = new_mtu;
2400 netdev_update_features(dev);
2401
2402 return 0;
2403}
2404
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002405static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002406 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002407{
2408 struct stmmac_priv *priv = netdev_priv(dev);
2409
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002410 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002411 features &= ~NETIF_F_RXCSUM;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002412
Michał Mirosław5e982f32011-04-09 02:46:55 +00002413 if (!priv->plat->tx_coe)
Tom Herberta1882222015-12-14 11:19:43 -08002414 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosław5e982f32011-04-09 02:46:55 +00002415
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002416 /* Some GMAC devices have a bugged Jumbo frame support that
2417 * needs to have the Tx COE disabled for oversized frames
2418 * (due to limited buffer sizes). In this case we disable
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002419 * the TX csum insertionin the TDES and not use SF.
2420 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00002421 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08002422 features &= ~NETIF_F_CSUM_MASK;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002423
Michał Mirosław5e982f32011-04-09 02:46:55 +00002424 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002425}
2426
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002427static int stmmac_set_features(struct net_device *netdev,
2428 netdev_features_t features)
2429{
2430 struct stmmac_priv *priv = netdev_priv(netdev);
2431
2432 /* Keep the COE Type in case of csum is supporting */
2433 if (features & NETIF_F_RXCSUM)
2434 priv->hw->rx_csum = priv->plat->rx_coe;
2435 else
2436 priv->hw->rx_csum = 0;
2437 /* No check needed because rx_coe has been set before and it will be
2438 * fixed in case of issue.
2439 */
2440 priv->hw->mac->rx_ipc(priv->hw);
2441
2442 return 0;
2443}
2444
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002445/**
2446 * stmmac_interrupt - main ISR
2447 * @irq: interrupt number.
2448 * @dev_id: to pass the net device pointer.
2449 * Description: this is the main driver interrupt service routine.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002450 * It can call:
2451 * o DMA service routine (to manage incoming frame reception and transmission
2452 * status)
2453 * o Core interrupts to manage: remote wake-up, management counter, LPI
2454 * interrupts.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002455 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002456static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2457{
2458 struct net_device *dev = (struct net_device *)dev_id;
2459 struct stmmac_priv *priv = netdev_priv(dev);
2460
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002461 if (priv->irq_wake)
2462 pm_wakeup_event(priv->device, 0);
2463
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002464 if (unlikely(!dev)) {
2465 pr_err("%s: invalid dev pointer\n", __func__);
2466 return IRQ_NONE;
2467 }
2468
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002469 /* To handle GMAC own interrupts */
2470 if (priv->plat->has_gmac) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002471 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002472 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002473 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002474 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002475 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002476 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002477 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002478 priv->tx_path_in_lpi_mode = false;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002479 }
2480 }
2481
2482 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002483 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002484
2485 return IRQ_HANDLED;
2486}
2487
2488#ifdef CONFIG_NET_POLL_CONTROLLER
2489/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002490 * to allow network I/O with interrupts disabled.
2491 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002492static void stmmac_poll_controller(struct net_device *dev)
2493{
2494 disable_irq(dev->irq);
2495 stmmac_interrupt(dev->irq, dev);
2496 enable_irq(dev->irq);
2497}
2498#endif
2499
2500/**
2501 * stmmac_ioctl - Entry point for the Ioctl
2502 * @dev: Device pointer.
2503 * @rq: An IOCTL specefic structure, that can contain a pointer to
2504 * a proprietary structure used to pass information to the driver.
2505 * @cmd: IOCTL command
2506 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002507 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002508 */
2509static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2510{
2511 struct stmmac_priv *priv = netdev_priv(dev);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002512 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002513
2514 if (!netif_running(dev))
2515 return -EINVAL;
2516
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002517 switch (cmd) {
2518 case SIOCGMIIPHY:
2519 case SIOCGMIIREG:
2520 case SIOCSMIIREG:
2521 if (!priv->phydev)
2522 return -EINVAL;
2523 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2524 break;
2525 case SIOCSHWTSTAMP:
2526 ret = stmmac_hwtstamp_ioctl(dev, rq);
2527 break;
2528 default:
2529 break;
2530 }
Richard Cochran28b04112010-07-17 08:48:55 +00002531
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002532 return ret;
2533}
2534
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002535#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002536static struct dentry *stmmac_fs_dir;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002537
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002538static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002539 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002540{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002541 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002542 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2543 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002544
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002545 for (i = 0; i < size; i++) {
2546 u64 x;
2547 if (extend_desc) {
2548 x = *(u64 *) ep;
2549 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002550 i, (unsigned int)virt_to_phys(ep),
2551 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002552 ep->basic.des2, ep->basic.des3);
2553 ep++;
2554 } else {
2555 x = *(u64 *) p;
2556 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002557 i, (unsigned int)virt_to_phys(ep),
2558 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002559 p->des2, p->des3);
2560 p++;
2561 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002562 seq_printf(seq, "\n");
2563 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002564}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002565
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002566static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2567{
2568 struct net_device *dev = seq->private;
2569 struct stmmac_priv *priv = netdev_priv(dev);
2570 unsigned int txsize = priv->dma_tx_size;
2571 unsigned int rxsize = priv->dma_rx_size;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002572
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002573 if (priv->extend_desc) {
2574 seq_printf(seq, "Extended RX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002575 sysfs_display_ring((void *)priv->dma_erx, rxsize, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002576 seq_printf(seq, "Extended TX descriptor ring:\n");
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002577 sysfs_display_ring((void *)priv->dma_etx, txsize, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002578 } else {
2579 seq_printf(seq, "RX descriptor ring:\n");
2580 sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
2581 seq_printf(seq, "TX descriptor ring:\n");
2582 sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002583 }
2584
2585 return 0;
2586}
2587
2588static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2589{
2590 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2591}
2592
2593static const struct file_operations stmmac_rings_status_fops = {
2594 .owner = THIS_MODULE,
2595 .open = stmmac_sysfs_ring_open,
2596 .read = seq_read,
2597 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002598 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002599};
2600
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002601static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2602{
2603 struct net_device *dev = seq->private;
2604 struct stmmac_priv *priv = netdev_priv(dev);
2605
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002606 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002607 seq_printf(seq, "DMA HW features not supported\n");
2608 return 0;
2609 }
2610
2611 seq_printf(seq, "==============================\n");
2612 seq_printf(seq, "\tDMA HW features\n");
2613 seq_printf(seq, "==============================\n");
2614
2615 seq_printf(seq, "\t10/100 Mbps %s\n",
2616 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2617 seq_printf(seq, "\t1000 Mbps %s\n",
2618 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2619 seq_printf(seq, "\tHalf duple %s\n",
2620 (priv->dma_cap.half_duplex) ? "Y" : "N");
2621 seq_printf(seq, "\tHash Filter: %s\n",
2622 (priv->dma_cap.hash_filter) ? "Y" : "N");
2623 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2624 (priv->dma_cap.multi_addr) ? "Y" : "N");
2625 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2626 (priv->dma_cap.pcs) ? "Y" : "N");
2627 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2628 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2629 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2630 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2631 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2632 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2633 seq_printf(seq, "\tRMON module: %s\n",
2634 (priv->dma_cap.rmon) ? "Y" : "N");
2635 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2636 (priv->dma_cap.time_stamp) ? "Y" : "N");
2637 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2638 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2639 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2640 (priv->dma_cap.eee) ? "Y" : "N");
2641 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2642 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2643 (priv->dma_cap.tx_coe) ? "Y" : "N");
2644 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2645 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2646 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2647 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2648 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2649 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2650 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2651 priv->dma_cap.number_rx_channel);
2652 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2653 priv->dma_cap.number_tx_channel);
2654 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2655 (priv->dma_cap.enh_desc) ? "Y" : "N");
2656
2657 return 0;
2658}
2659
2660static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2661{
2662 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2663}
2664
2665static const struct file_operations stmmac_dma_cap_fops = {
2666 .owner = THIS_MODULE,
2667 .open = stmmac_sysfs_dma_cap_open,
2668 .read = seq_read,
2669 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002670 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002671};
2672
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002673static int stmmac_init_fs(struct net_device *dev)
2674{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002675 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002676
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002677 /* Create per netdev entries */
2678 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
2679
2680 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
2681 pr_err("ERROR %s/%s, debugfs create directory failed\n",
2682 STMMAC_RESOURCE_NAME, dev->name);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002683
2684 return -ENOMEM;
2685 }
2686
2687 /* Entry to report DMA RX/TX rings */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002688 priv->dbgfs_rings_status =
2689 debugfs_create_file("descriptors_status", S_IRUGO,
2690 priv->dbgfs_dir, dev,
2691 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002692
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002693 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002694 pr_info("ERROR creating stmmac ring debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002695 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002696
2697 return -ENOMEM;
2698 }
2699
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002700 /* Entry to report the DMA HW features */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002701 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
2702 priv->dbgfs_dir,
2703 dev, &stmmac_dma_cap_fops);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002704
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002705 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002706 pr_info("ERROR creating stmmac MMC debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002707 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002708
2709 return -ENOMEM;
2710 }
2711
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002712 return 0;
2713}
2714
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002715static void stmmac_exit_fs(struct net_device *dev)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002716{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002717 struct stmmac_priv *priv = netdev_priv(dev);
2718
2719 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002720}
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002721#endif /* CONFIG_DEBUG_FS */
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002722
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002723static const struct net_device_ops stmmac_netdev_ops = {
2724 .ndo_open = stmmac_open,
2725 .ndo_start_xmit = stmmac_xmit,
2726 .ndo_stop = stmmac_release,
2727 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00002728 .ndo_fix_features = stmmac_fix_features,
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002729 .ndo_set_features = stmmac_set_features,
Jiri Pirko01789342011-08-16 06:29:00 +00002730 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002731 .ndo_tx_timeout = stmmac_tx_timeout,
2732 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002733#ifdef CONFIG_NET_POLL_CONTROLLER
2734 .ndo_poll_controller = stmmac_poll_controller,
2735#endif
2736 .ndo_set_mac_address = eth_mac_addr,
2737};
2738
2739/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002740 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002741 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002742 * Description: this function is to configure the MAC device according to
2743 * some platform parameters or the HW capability register. It prepares the
2744 * driver to use either ring or chain modes and to setup either enhanced or
2745 * normal descriptors.
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002746 */
2747static int stmmac_hw_init(struct stmmac_priv *priv)
2748{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002749 struct mac_device_info *mac;
2750
2751 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002752 if (priv->plat->has_gmac) {
2753 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05002754 mac = dwmac1000_setup(priv->ioaddr,
2755 priv->plat->multicast_filter_bins,
2756 priv->plat->unicast_filter_entries);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002757 } else {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002758 mac = dwmac100_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002759 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002760 if (!mac)
2761 return -ENOMEM;
2762
2763 priv->hw = mac;
2764
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002765 /* Get and dump the chip ID */
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00002766 priv->synopsys_id = stmmac_get_synopsys_id(priv);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002767
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002768 /* To use the chained or ring mode */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002769 if (chain_mode) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002770 priv->hw->mode = &chain_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002771 pr_info(" Chain mode enabled\n");
2772 priv->mode = STMMAC_CHAIN_MODE;
2773 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002774 priv->hw->mode = &ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002775 pr_info(" Ring mode enabled\n");
2776 priv->mode = STMMAC_RING_MODE;
2777 }
2778
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002779 /* Get the HW capability (new GMAC newer than 3.50a) */
2780 priv->hw_cap_support = stmmac_get_hw_features(priv);
2781 if (priv->hw_cap_support) {
2782 pr_info(" DMA HW capability register supported");
2783
2784 /* We can override some gmac/dma configuration fields: e.g.
2785 * enh_desc, tx_coe (e.g. that are passed through the
2786 * platform) with the values from the HW capability
2787 * register (if supported).
2788 */
2789 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002790 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002791
Sonic Zhangdec21652015-01-22 14:55:57 +08002792 /* TXCOE doesn't work in thresh DMA mode */
2793 if (priv->plat->force_thresh_dma_mode)
2794 priv->plat->tx_coe = 0;
2795 else
2796 priv->plat->tx_coe = priv->dma_cap.tx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002797
2798 if (priv->dma_cap.rx_coe_type2)
2799 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2800 else if (priv->dma_cap.rx_coe_type1)
2801 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2802
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002803 } else
2804 pr_info(" No HW DMA feature register supported");
2805
Byungho An61369d02013-06-28 16:35:32 +09002806 /* To use alternate (extended) or normal descriptor structures */
2807 stmmac_selec_desc_mode(priv);
2808
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002809 if (priv->plat->rx_coe) {
2810 priv->hw->rx_csum = priv->plat->rx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002811 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2812 priv->plat->rx_coe);
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002813 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002814 if (priv->plat->tx_coe)
2815 pr_info(" TX Checksum insertion supported\n");
2816
2817 if (priv->plat->pmt) {
2818 pr_info(" Wake-Up On Lan supported\n");
2819 device_set_wakeup_capable(priv->device, 1);
2820 }
2821
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002822 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002823}
2824
2825/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002826 * stmmac_dvr_probe
2827 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002828 * @plat_dat: platform data pointer
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002829 * @res: stmmac resource pointer
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002830 * Description: this is the main probe function used to
2831 * call the alloc_etherdev, allocate the priv structure.
Andy Shevchenko9afec6e2015-01-27 18:38:03 +02002832 * Return:
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002833 * returns 0 on success, otherwise errno.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002834 */
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002835int stmmac_dvr_probe(struct device *device,
2836 struct plat_stmmacenet_data *plat_dat,
2837 struct stmmac_resources *res)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002838{
2839 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002840 struct net_device *ndev = NULL;
2841 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002842
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002843 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002844 if (!ndev)
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002845 return -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002846
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002847 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002848
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002849 priv = netdev_priv(ndev);
2850 priv->device = device;
2851 priv->dev = ndev;
2852
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002853 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002854 priv->pause = pause;
2855 priv->plat = plat_dat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002856 priv->ioaddr = res->addr;
2857 priv->dev->base_addr = (unsigned long)res->addr;
2858
2859 priv->dev->irq = res->irq;
2860 priv->wol_irq = res->wol_irq;
2861 priv->lpi_irq = res->lpi_irq;
2862
2863 if (res->mac)
2864 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002865
Joachim Eastwooda7a62682015-07-17 23:48:17 +02002866 dev_set_drvdata(device, priv->dev);
Joachim Eastwood803f8fc2015-05-20 20:03:06 +02002867
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002868 /* Verify driver arguments */
2869 stmmac_verify_args();
2870
2871 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002872 * this needs to have multiple instances
2873 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002874 if ((phyaddr >= 0) && (phyaddr <= 31))
2875 priv->plat->phy_addr = phyaddr;
2876
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002877 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
2878 if (IS_ERR(priv->stmmac_clk)) {
2879 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
2880 __func__);
Kweh, Hock Leongc5bb86c2014-09-26 21:42:55 +08002881 /* If failed to obtain stmmac_clk and specific clk_csr value
2882 * is NOT passed from the platform, probe fail.
2883 */
2884 if (!priv->plat->clk_csr) {
2885 ret = PTR_ERR(priv->stmmac_clk);
2886 goto error_clk_get;
2887 } else {
2888 priv->stmmac_clk = NULL;
2889 }
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002890 }
2891 clk_prepare_enable(priv->stmmac_clk);
2892
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002893 priv->pclk = devm_clk_get(priv->device, "pclk");
2894 if (IS_ERR(priv->pclk)) {
2895 if (PTR_ERR(priv->pclk) == -EPROBE_DEFER) {
2896 ret = -EPROBE_DEFER;
2897 goto error_pclk_get;
2898 }
2899 priv->pclk = NULL;
2900 }
2901 clk_prepare_enable(priv->pclk);
2902
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002903 priv->stmmac_rst = devm_reset_control_get(priv->device,
2904 STMMAC_RESOURCE_NAME);
2905 if (IS_ERR(priv->stmmac_rst)) {
2906 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
2907 ret = -EPROBE_DEFER;
2908 goto error_hw_init;
2909 }
2910 dev_info(priv->device, "no reset control found\n");
2911 priv->stmmac_rst = NULL;
2912 }
2913 if (priv->stmmac_rst)
2914 reset_control_deassert(priv->stmmac_rst);
2915
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002916 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002917 ret = stmmac_hw_init(priv);
2918 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002919 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002920
2921 ndev->netdev_ops = &stmmac_netdev_ops;
2922
2923 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2924 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002925 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2926 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002927#ifdef STMMAC_VLAN_TAG_USED
2928 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00002929 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002930#endif
2931 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2932
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002933 if (flow_ctrl)
2934 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2935
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002936 /* Rx Watchdog is available in the COREs newer than the 3.40.
2937 * In some case, for example on bugged HW this feature
2938 * has to be disable and this can be done by passing the
2939 * riwt_off field from the platform.
2940 */
2941 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2942 priv->use_riwt = 1;
2943 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2944 }
2945
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002946 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002947
Vlad Lunguf8e96162010-11-29 22:52:52 +00002948 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002949 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002950
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002951 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002952 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002953 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002954 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002955 }
2956
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002957 /* If a specific clk_csr value is passed from the platform
2958 * this means that the CSR Clock Range selection cannot be
2959 * changed at run-time and it is fixed. Viceversa the driver'll try to
2960 * set the MDC clock dynamically according to the csr actual
2961 * clock input.
2962 */
2963 if (!priv->plat->clk_csr)
2964 stmmac_clk_csr_set(priv);
2965 else
2966 priv->clk_csr = priv->plat->clk_csr;
2967
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002968 stmmac_check_pcs_mode(priv);
2969
Byungho An4d8f0822013-04-07 17:56:16 +00002970 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2971 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002972 /* MDIO bus Registration */
2973 ret = stmmac_mdio_register(ndev);
2974 if (ret < 0) {
2975 pr_debug("%s: MDIO bus (id: %d) registration failed",
2976 __func__, priv->plat->bus_id);
2977 goto error_mdio_register;
2978 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002979 }
2980
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002981 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002982
Viresh Kumar6a81c262012-07-30 14:39:41 -07002983error_mdio_register:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002984 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002985error_netdev_register:
2986 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002987error_hw_init:
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002988 clk_disable_unprepare(priv->pclk);
2989error_pclk_get:
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002990 clk_disable_unprepare(priv->stmmac_clk);
2991error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002992 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002993
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002994 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002995}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02002996EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002997
2998/**
2999 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003000 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003001 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003002 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003003 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003004int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003005{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00003006 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003007
3008 pr_info("%s:\n\tremoving driver", __func__);
3009
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00003010 priv->hw->dma->stop_rx(priv->ioaddr);
3011 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003012
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003013 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003014 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003015 unregister_netdev(ndev);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08003016 if (priv->stmmac_rst)
3017 reset_control_assert(priv->stmmac_rst);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003018 clk_disable_unprepare(priv->pclk);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003019 clk_disable_unprepare(priv->stmmac_clk);
Bryan O'Donoghuee7434712015-04-16 17:56:03 +01003020 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
3021 priv->pcs != STMMAC_PCS_RTBI)
3022 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003023 free_netdev(ndev);
3024
3025 return 0;
3026}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003027EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003028
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003029/**
3030 * stmmac_suspend - suspend callback
3031 * @ndev: net device pointer
3032 * Description: this is the function to suspend the device and it is called
3033 * by the platform driver to stop the network queue, release the resources,
3034 * program the PMT register (for WoL), clean and release driver resources.
3035 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003036int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003037{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003038 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003039 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003040
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003041 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003042 return 0;
3043
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003044 if (priv->phydev)
3045 phy_stop(priv->phydev);
3046
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003047 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003048
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003049 netif_device_detach(ndev);
3050 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003051
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003052 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003053
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003054 /* Stop TX/RX DMA */
3055 priv->hw->dma->stop_tx(priv->ioaddr);
3056 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003057
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003058 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003059 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003060 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003061 priv->irq_wake = 1;
3062 } else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003063 stmmac_set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003064 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003065 /* Disable clock in case of PWM is off */
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003066 clk_disable(priv->pclk);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003067 clk_disable(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003068 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003069 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05003070
3071 priv->oldlink = 0;
3072 priv->speed = 0;
3073 priv->oldduplex = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003074 return 0;
3075}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003076EXPORT_SYMBOL_GPL(stmmac_suspend);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003077
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003078/**
3079 * stmmac_resume - resume callback
3080 * @ndev: net device pointer
3081 * Description: when resume this function is invoked to setup the DMA and CORE
3082 * in a usable state.
3083 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003084int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003085{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003086 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003087 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003088
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003089 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003090 return 0;
3091
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003092 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02003093
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003094 /* Power Down bit, into the PM register, is cleared
3095 * automatically as soon as a magic packet or a Wake-up frame
3096 * is received. Anyway, it's better to manually clear
3097 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003098 * from another devices (e.g. serial console).
3099 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003100 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003101 priv->hw->mac->pmt(priv->hw, 0);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003102 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003103 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003104 pinctrl_pm_select_default_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003105 /* enable the clk prevously disabled */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003106 clk_enable(priv->stmmac_clk);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003107 clk_enable(priv->pclk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003108 /* reset the phy so that it's ready */
3109 if (priv->mii)
3110 stmmac_mdio_reset(priv->mii);
3111 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003112
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003113 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003114
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01003115 priv->cur_rx = 0;
3116 priv->dirty_rx = 0;
3117 priv->dirty_tx = 0;
3118 priv->cur_tx = 0;
3119 stmmac_clear_descriptors(priv);
3120
Huacai Chenfe1319292014-12-19 22:38:18 +08003121 stmmac_hw_setup(ndev, false);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003122 stmmac_init_tx_coalesce(priv);
Giuseppe CAVALLAROac316c72015-11-26 08:35:41 +01003123 stmmac_set_rx_mode(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003124
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003125 napi_enable(&priv->napi);
3126
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003127 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003128
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003129 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003130
3131 if (priv->phydev)
3132 phy_start(priv->phydev);
3133
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003134 return 0;
3135}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003136EXPORT_SYMBOL_GPL(stmmac_resume);
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00003137
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003138#ifndef MODULE
3139static int __init stmmac_cmdline_opt(char *str)
3140{
3141 char *opt;
3142
3143 if (!str || !*str)
3144 return -EINVAL;
3145 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003146 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003147 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003148 goto err;
3149 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003150 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003151 goto err;
3152 } else if (!strncmp(opt, "dma_txsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003153 if (kstrtoint(opt + 11, 0, &dma_txsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003154 goto err;
3155 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003156 if (kstrtoint(opt + 11, 0, &dma_rxsize))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003157 goto err;
3158 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003159 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003160 goto err;
3161 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003162 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003163 goto err;
3164 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003165 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003166 goto err;
3167 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003168 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003169 goto err;
3170 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003171 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003172 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00003173 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003174 if (kstrtoint(opt + 10, 0, &eee_timer))
3175 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003176 } else if (!strncmp(opt, "chain_mode:", 11)) {
3177 if (kstrtoint(opt + 11, 0, &chain_mode))
3178 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003179 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003180 }
3181 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003182
3183err:
3184 pr_err("%s: ERROR broken module parameter conversion", __func__);
3185 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003186}
3187
3188__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003189#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003190
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003191static int __init stmmac_init(void)
3192{
3193#ifdef CONFIG_DEBUG_FS
3194 /* Create debugfs main directory if it doesn't exist yet */
3195 if (!stmmac_fs_dir) {
3196 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
3197
3198 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
3199 pr_err("ERROR %s, debugfs create directory failed\n",
3200 STMMAC_RESOURCE_NAME);
3201
3202 return -ENOMEM;
3203 }
3204 }
3205#endif
3206
3207 return 0;
3208}
3209
3210static void __exit stmmac_exit(void)
3211{
3212#ifdef CONFIG_DEBUG_FS
3213 debugfs_remove_recursive(stmmac_fs_dir);
3214#endif
3215}
3216
3217module_init(stmmac_init)
3218module_exit(stmmac_exit)
3219
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003220MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3221MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3222MODULE_LICENSE("GPL");