blob: fcbd4be562e2b9ef7e873482d7f00c27c1d3c3d9 [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
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +010074#define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +010075#define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070076
77static int flow_ctrl = FLOW_OFF;
78module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
79MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
80
81static int pause = PAUSE_TIME;
82module_param(pause, int, S_IRUGO | S_IWUSR);
83MODULE_PARM_DESC(pause, "Flow Control Pause Time");
84
85#define TC_DEFAULT 64
86static int tc = TC_DEFAULT;
87module_param(tc, int, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(tc, "DMA threshold control value");
89
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +010090#define DEFAULT_BUFSIZE 1536
91static int buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070092module_param(buf_sz, int, S_IRUGO | S_IWUSR);
93MODULE_PARM_DESC(buf_sz, "DMA buffer size");
94
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +010095#define STMMAC_RX_COPYBREAK 256
96
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070097static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
98 NETIF_MSG_LINK | NETIF_MSG_IFUP |
99 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
100
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000101#define STMMAC_DEFAULT_LPI_TIMER 1000
102static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
103module_param(eee_timer, int, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200105#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000106
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000107/* By default the driver will use the ring mode to manage tx and rx descriptors
108 * but passing this value so user can force to use the chain instead of the ring
109 */
110static unsigned int chain_mode;
111module_param(chain_mode, int, S_IRUGO);
112MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
113
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700114static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700115
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +0100116#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000117static int stmmac_init_fs(struct net_device *dev);
Mathieu Olivari466c5ac2015-05-22 19:03:29 -0700118static void stmmac_exit_fs(struct net_device *dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000119#endif
120
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000121#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
122
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700123/**
124 * stmmac_verify_args - verify the driver parameters.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100125 * Description: it checks the driver parameters and set a default in case of
126 * errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700127 */
128static void stmmac_verify_args(void)
129{
130 if (unlikely(watchdog < 0))
131 watchdog = TX_TIMEO;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100132 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
133 buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700134 if (unlikely(flow_ctrl > 1))
135 flow_ctrl = FLOW_AUTO;
136 else if (likely(flow_ctrl < 0))
137 flow_ctrl = FLOW_OFF;
138 if (unlikely((pause < 0) || (pause > 0xffff)))
139 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000140 if (eee_timer < 0)
141 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700142}
143
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000144/**
145 * stmmac_clk_csr_set - dynamically set the MDC clock
146 * @priv: driver private structure
147 * Description: this is to dynamically set the MDC clock according to the csr
148 * clock input.
149 * Note:
150 * If a specific clk_csr value is passed from the platform
151 * this means that the CSR Clock Range selection cannot be
152 * changed at run-time and it is fixed (as reported in the driver
153 * documentation). Viceversa the driver will try to set the MDC
154 * clock dynamically according to the actual clock input.
155 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000156static void stmmac_clk_csr_set(struct stmmac_priv *priv)
157{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000158 u32 clk_rate;
159
160 clk_rate = clk_get_rate(priv->stmmac_clk);
161
162 /* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000163 * for all other cases except for the below mentioned ones.
164 * For values higher than the IEEE 802.3 specified frequency
165 * we can not estimate the proper divider as it is not known
166 * the frequency of clk_csr_i. So we do not change the default
167 * divider.
168 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000169 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
170 if (clk_rate < CSR_F_35M)
171 priv->clk_csr = STMMAC_CSR_20_35M;
172 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
173 priv->clk_csr = STMMAC_CSR_35_60M;
174 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
175 priv->clk_csr = STMMAC_CSR_60_100M;
176 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
177 priv->clk_csr = STMMAC_CSR_100_150M;
178 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
179 priv->clk_csr = STMMAC_CSR_150_250M;
Phil Reid19d857c2015-12-14 11:32:01 +0800180 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000181 priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000182 }
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000183}
184
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700185static void print_pkt(unsigned char *buf, int len)
186{
Andy Shevchenko424c4f72014-11-07 16:53:12 +0200187 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
188 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700189}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700190
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700191static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
192{
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100193 unsigned avail;
194
195 if (priv->dirty_tx > priv->cur_tx)
196 avail = priv->dirty_tx - priv->cur_tx - 1;
197 else
198 avail = DMA_TX_SIZE - priv->cur_tx + priv->dirty_tx - 1;
199
200 return avail;
201}
202
203static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv)
204{
205 unsigned dirty;
206
207 if (priv->dirty_rx <= priv->cur_rx)
208 dirty = priv->cur_rx - priv->dirty_rx;
209 else
210 dirty = DMA_RX_SIZE - priv->dirty_rx + priv->cur_rx;
211
212 return dirty;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700213}
214
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000215/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100216 * stmmac_hw_fix_mac_speed - callback for speed selection
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000217 * @priv: driver private structure
218 * Description: on some platforms (e.g. ST), some HW system configuraton
219 * registers have to be set according to the link speed negotiated.
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000220 */
221static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
222{
223 struct phy_device *phydev = priv->phydev;
224
225 if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000226 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000227}
228
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000229/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100230 * stmmac_enable_eee_mode - check and enter in LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000231 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100232 * Description: this function is to verify and enter in LPI mode in case of
233 * EEE.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000234 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000235static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
236{
237 /* Check and enter in LPI mode */
238 if ((priv->dirty_tx == priv->cur_tx) &&
239 (priv->tx_path_in_lpi_mode == false))
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500240 priv->hw->mac->set_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000241}
242
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000243/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100244 * stmmac_disable_eee_mode - disable and exit from LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000245 * @priv: driver private structure
246 * Description: this function is to exit and disable EEE in case of
247 * LPI state is true. This is called by the xmit.
248 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000249void stmmac_disable_eee_mode(struct stmmac_priv *priv)
250{
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500251 priv->hw->mac->reset_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000252 del_timer_sync(&priv->eee_ctrl_timer);
253 priv->tx_path_in_lpi_mode = false;
254}
255
256/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100257 * stmmac_eee_ctrl_timer - EEE TX SW timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000258 * @arg : data hook
259 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000260 * if there is no data transfer and if we are not in LPI state,
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000261 * then MAC Transmitter can be moved to LPI state.
262 */
263static void stmmac_eee_ctrl_timer(unsigned long arg)
264{
265 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
266
267 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200268 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000269}
270
271/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100272 * stmmac_eee_init - init EEE
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000273 * @priv: driver private structure
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000274 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100275 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
276 * can also manage EEE, this function enable the LPI state and start related
277 * timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000278 */
279bool stmmac_eee_init(struct stmmac_priv *priv)
280{
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100281 unsigned long flags;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000282 bool ret = false;
283
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200284 /* Using PCS we cannot dial with the phy registers at this stage
285 * so we do not support extra feature like EEE.
286 */
287 if ((priv->pcs == STMMAC_PCS_RGMII) || (priv->pcs == STMMAC_PCS_TBI) ||
288 (priv->pcs == STMMAC_PCS_RTBI))
289 goto out;
290
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000291 /* MAC core supports the EEE feature. */
292 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100293 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000294
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100295 /* Check if the PHY supports EEE */
296 if (phy_init_eee(priv->phydev, 1)) {
297 /* To manage at run-time if the EEE cannot be supported
298 * anymore (for example because the lp caps have been
299 * changed).
300 * In that case the driver disable own timers.
301 */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100302 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100303 if (priv->eee_active) {
304 pr_debug("stmmac: disable EEE\n");
305 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500306 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100307 tx_lpi_timer);
308 }
309 priv->eee_active = 0;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100310 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100311 goto out;
312 }
313 /* Activate the EEE and start timers */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100314 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200315 if (!priv->eee_active) {
316 priv->eee_active = 1;
Vaishali Thakkarccb36da2015-02-28 00:12:34 +0530317 setup_timer(&priv->eee_ctrl_timer,
318 stmmac_eee_ctrl_timer,
319 (unsigned long)priv);
320 mod_timer(&priv->eee_ctrl_timer,
321 STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000322
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500323 priv->hw->mac->set_eee_timer(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200324 STMMAC_DEFAULT_LIT_LS,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100325 tx_lpi_timer);
Giuseppe CAVALLARO71965352014-08-28 08:11:44 +0200326 }
327 /* Set HW EEE according to the speed */
328 priv->hw->mac->set_eee_pls(priv->hw, priv->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000329
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000330 ret = true;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100331 spin_unlock_irqrestore(&priv->lock, flags);
332
333 pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000334 }
335out:
336 return ret;
337}
338
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100339/* stmmac_get_tx_hwtstamp - get HW TX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000340 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000341 * @entry : descriptor index to be used.
342 * @skb : the socket buffer
343 * Description :
344 * This function will read timestamp from the descriptor & pass it to stack.
345 * and also perform some sanity checks.
346 */
347static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000348 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000349{
350 struct skb_shared_hwtstamps shhwtstamp;
351 u64 ns;
352 void *desc = NULL;
353
354 if (!priv->hwts_tx_en)
355 return;
356
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000357 /* exit if skb doesn't support hw tstamp */
damuzi00075e43642014-01-17 23:47:59 +0800358 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000359 return;
360
361 if (priv->adv_ts)
362 desc = (priv->dma_etx + entry);
363 else
364 desc = (priv->dma_tx + entry);
365
366 /* check tx tstamp status */
367 if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
368 return;
369
370 /* get the valid tstamp */
371 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
372
373 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
374 shhwtstamp.hwtstamp = ns_to_ktime(ns);
375 /* pass tstamp to stack */
376 skb_tstamp_tx(skb, &shhwtstamp);
377
378 return;
379}
380
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100381/* stmmac_get_rx_hwtstamp - get HW RX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000382 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000383 * @entry : descriptor index to be used.
384 * @skb : the socket buffer
385 * Description :
386 * This function will read received packet's timestamp from the descriptor
387 * and pass it to stack. It also perform some sanity checks.
388 */
389static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000390 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000391{
392 struct skb_shared_hwtstamps *shhwtstamp = NULL;
393 u64 ns;
394 void *desc = NULL;
395
396 if (!priv->hwts_rx_en)
397 return;
398
399 if (priv->adv_ts)
400 desc = (priv->dma_erx + entry);
401 else
402 desc = (priv->dma_rx + entry);
403
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000404 /* exit if rx tstamp is not valid */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000405 if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
406 return;
407
408 /* get valid tstamp */
409 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
410 shhwtstamp = skb_hwtstamps(skb);
411 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
412 shhwtstamp->hwtstamp = ns_to_ktime(ns);
413}
414
415/**
416 * stmmac_hwtstamp_ioctl - control hardware timestamping.
417 * @dev: device pointer.
418 * @ifr: An IOCTL specefic structure, that can contain a pointer to
419 * a proprietary structure used to pass information to the driver.
420 * Description:
421 * This function configures the MAC to enable/disable both outgoing(TX)
422 * and incoming(RX) packets time stamping based on user input.
423 * Return Value:
424 * 0 on success and an appropriate -ve integer on failure.
425 */
426static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
427{
428 struct stmmac_priv *priv = netdev_priv(dev);
429 struct hwtstamp_config config;
Arnd Bergmann0a624152015-09-30 13:26:32 +0200430 struct timespec64 now;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000431 u64 temp = 0;
432 u32 ptp_v2 = 0;
433 u32 tstamp_all = 0;
434 u32 ptp_over_ipv4_udp = 0;
435 u32 ptp_over_ipv6_udp = 0;
436 u32 ptp_over_ethernet = 0;
437 u32 snap_type_sel = 0;
438 u32 ts_master_en = 0;
439 u32 ts_event_en = 0;
440 u32 value = 0;
Phil Reid19d857c2015-12-14 11:32:01 +0800441 u32 sec_inc;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000442
443 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
444 netdev_alert(priv->dev, "No support for HW time stamping\n");
445 priv->hwts_tx_en = 0;
446 priv->hwts_rx_en = 0;
447
448 return -EOPNOTSUPP;
449 }
450
451 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000452 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000453 return -EFAULT;
454
455 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
456 __func__, config.flags, config.tx_type, config.rx_filter);
457
458 /* reserved for future extensions */
459 if (config.flags)
460 return -EINVAL;
461
Ben Hutchings5f3da322013-11-14 00:43:41 +0000462 if (config.tx_type != HWTSTAMP_TX_OFF &&
463 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000464 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000465
466 if (priv->adv_ts) {
467 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000468 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000469 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000470 config.rx_filter = HWTSTAMP_FILTER_NONE;
471 break;
472
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000473 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000474 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000475 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
476 /* take time stamp for all event messages */
477 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
478
479 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
480 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
481 break;
482
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000483 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000484 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000485 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
486 /* take time stamp for SYNC messages only */
487 ts_event_en = PTP_TCR_TSEVNTENA;
488
489 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
490 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
491 break;
492
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000493 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000494 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000495 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
496 /* take time stamp for Delay_Req messages only */
497 ts_master_en = PTP_TCR_TSMSTRENA;
498 ts_event_en = PTP_TCR_TSEVNTENA;
499
500 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
501 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
502 break;
503
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000504 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000505 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000506 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
507 ptp_v2 = PTP_TCR_TSVER2ENA;
508 /* take time stamp for all event messages */
509 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
510
511 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
512 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
513 break;
514
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000515 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000516 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000517 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
518 ptp_v2 = PTP_TCR_TSVER2ENA;
519 /* take time stamp for SYNC messages only */
520 ts_event_en = PTP_TCR_TSEVNTENA;
521
522 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
523 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
524 break;
525
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000526 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000527 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000528 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
529 ptp_v2 = PTP_TCR_TSVER2ENA;
530 /* take time stamp for Delay_Req messages only */
531 ts_master_en = PTP_TCR_TSMSTRENA;
532 ts_event_en = PTP_TCR_TSEVNTENA;
533
534 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
535 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
536 break;
537
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000538 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000539 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000540 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
541 ptp_v2 = PTP_TCR_TSVER2ENA;
542 /* take time stamp for all event messages */
543 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
544
545 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
546 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
547 ptp_over_ethernet = PTP_TCR_TSIPENA;
548 break;
549
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000550 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000551 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000552 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
553 ptp_v2 = PTP_TCR_TSVER2ENA;
554 /* take time stamp for SYNC messages only */
555 ts_event_en = PTP_TCR_TSEVNTENA;
556
557 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
558 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
559 ptp_over_ethernet = PTP_TCR_TSIPENA;
560 break;
561
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000562 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000563 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000564 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
565 ptp_v2 = PTP_TCR_TSVER2ENA;
566 /* take time stamp for Delay_Req messages only */
567 ts_master_en = PTP_TCR_TSMSTRENA;
568 ts_event_en = PTP_TCR_TSEVNTENA;
569
570 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
571 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
572 ptp_over_ethernet = PTP_TCR_TSIPENA;
573 break;
574
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000575 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000576 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000577 config.rx_filter = HWTSTAMP_FILTER_ALL;
578 tstamp_all = PTP_TCR_TSENALL;
579 break;
580
581 default:
582 return -ERANGE;
583 }
584 } else {
585 switch (config.rx_filter) {
586 case HWTSTAMP_FILTER_NONE:
587 config.rx_filter = HWTSTAMP_FILTER_NONE;
588 break;
589 default:
590 /* PTP v1, UDP, any kind of event packet */
591 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
592 break;
593 }
594 }
595 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000596 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000597
598 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
599 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
600 else {
601 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000602 tstamp_all | ptp_v2 | ptp_over_ethernet |
603 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
604 ts_master_en | snap_type_sel);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000605 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
606
607 /* program Sub Second Increment reg */
Phil Reid19d857c2015-12-14 11:32:01 +0800608 sec_inc = priv->hw->ptp->config_sub_second_increment(
609 priv->ioaddr, priv->clk_ptp_rate);
610 temp = div_u64(1000000000ULL, sec_inc);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000611
612 /* calculate default added value:
613 * formula is :
614 * addend = (2^32)/freq_div_ratio;
Phil Reid19d857c2015-12-14 11:32:01 +0800615 * where, freq_div_ratio = 1e9ns/sec_inc
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000616 */
Phil Reid19d857c2015-12-14 11:32:01 +0800617 temp = (u64)(temp << 32);
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200618 priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000619 priv->hw->ptp->config_addend(priv->ioaddr,
620 priv->default_addend);
621
622 /* initialize system time */
Arnd Bergmann0a624152015-09-30 13:26:32 +0200623 ktime_get_real_ts64(&now);
624
625 /* lower 32 bits of tv_sec are safe until y2106 */
626 priv->hw->ptp->init_systime(priv->ioaddr, (u32)now.tv_sec,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000627 now.tv_nsec);
628 }
629
630 return copy_to_user(ifr->ifr_data, &config,
631 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
632}
633
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000634/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100635 * stmmac_init_ptp - init PTP
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000636 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100637 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000638 * This is done by looking at the HW cap. register.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100639 * This function also registers the ptp driver.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000640 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000641static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000642{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000643 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
644 return -EOPNOTSUPP;
645
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200646 /* Fall-back to main clock in case of no PTP ref is passed */
647 priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
648 if (IS_ERR(priv->clk_ptp_ref)) {
649 priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
650 priv->clk_ptp_ref = NULL;
651 } else {
652 clk_prepare_enable(priv->clk_ptp_ref);
653 priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
654 }
655
Vince Bridgers7cd01392013-12-20 11:19:34 -0600656 priv->adv_ts = 0;
657 if (priv->dma_cap.atime_stamp && priv->extend_desc)
658 priv->adv_ts = 1;
659
660 if (netif_msg_hw(priv) && priv->dma_cap.time_stamp)
661 pr_debug("IEEE 1588-2002 Time Stamp supported\n");
662
663 if (netif_msg_hw(priv) && priv->adv_ts)
664 pr_debug("IEEE 1588-2008 Advanced Time Stamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000665
666 priv->hw->ptp = &stmmac_ptp;
667 priv->hwts_tx_en = 0;
668 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000669
670 return stmmac_ptp_register(priv);
671}
672
673static void stmmac_release_ptp(struct stmmac_priv *priv)
674{
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200675 if (priv->clk_ptp_ref)
676 clk_disable_unprepare(priv->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000677 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000678}
679
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700680/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100681 * stmmac_adjust_link - adjusts the link parameters
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700682 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100683 * Description: this is the helper called by the physical abstraction layer
684 * drivers to communicate the phy link status. According the speed and duplex
685 * this driver can invoke registered glue-logic as well.
686 * It also invoke the eee initialization because it could happen when switch
687 * on different networks (that are eee capable).
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700688 */
689static void stmmac_adjust_link(struct net_device *dev)
690{
691 struct stmmac_priv *priv = netdev_priv(dev);
692 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700693 unsigned long flags;
694 int new_state = 0;
695 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
696
697 if (phydev == NULL)
698 return;
699
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700700 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000701
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700702 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000703 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700704
705 /* Now we make sure that we can be in full duplex mode.
706 * If not, we operate in half-duplex mode. */
707 if (phydev->duplex != priv->oldduplex) {
708 new_state = 1;
709 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000710 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700711 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000712 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700713 priv->oldduplex = phydev->duplex;
714 }
715 /* Flow Control operation */
716 if (phydev->pause)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500717 priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000718 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700719
720 if (phydev->speed != priv->speed) {
721 new_state = 1;
722 switch (phydev->speed) {
723 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000724 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000725 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000726 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700727 break;
728 case 100:
729 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000730 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000731 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700732 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000733 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700734 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000735 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700736 }
737 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000738 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700739 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000740 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700741 break;
742 default:
743 if (netif_msg_link(priv))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000744 pr_warn("%s: Speed (%d) not 10/100\n",
745 dev->name, phydev->speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700746 break;
747 }
748
749 priv->speed = phydev->speed;
750 }
751
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000752 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700753
754 if (!priv->oldlink) {
755 new_state = 1;
756 priv->oldlink = 1;
757 }
758 } else if (priv->oldlink) {
759 new_state = 1;
760 priv->oldlink = 0;
761 priv->speed = 0;
762 priv->oldduplex = -1;
763 }
764
765 if (new_state && netif_msg_link(priv))
766 phy_print_status(phydev);
767
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100768 spin_unlock_irqrestore(&priv->lock, flags);
769
Giuseppe CAVALLARO52f95bb2016-04-05 08:46:57 +0200770 if (phydev->is_pseudo_fixed_link)
771 /* Stop PHY layer to call the hook to adjust the link in case
772 * of a switch is attached to the stmmac driver.
773 */
774 phydev->irq = PHY_IGNORE_INTERRUPT;
775 else
776 /* At this stage, init the EEE if supported.
777 * Never called in case of fixed_link.
778 */
779 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700780}
781
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000782/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100783 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000784 * @priv: driver private structure
785 * Description: this is to verify if the HW supports the PCS.
786 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
787 * configured for the TBI, RTBI, or SGMII PHY interface.
788 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000789static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
790{
791 int interface = priv->plat->interface;
792
793 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900794 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
795 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
796 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
797 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000798 pr_debug("STMMAC: PCS RGMII support enable\n");
799 priv->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900800 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000801 pr_debug("STMMAC: PCS SGMII support enable\n");
802 priv->pcs = STMMAC_PCS_SGMII;
803 }
804 }
805}
806
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700807/**
808 * stmmac_init_phy - PHY initialization
809 * @dev: net device structure
810 * Description: it initializes the driver's PHY state, and attaches the PHY
811 * to the mac driver.
812 * Return value:
813 * 0 on success
814 */
815static int stmmac_init_phy(struct net_device *dev)
816{
817 struct stmmac_priv *priv = netdev_priv(dev);
818 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000819 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000820 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000821 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000822 int max_speed = priv->plat->max_speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700823 priv->oldlink = 0;
824 priv->speed = 0;
825 priv->oldduplex = -1;
826
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700827 if (priv->plat->phy_node) {
828 phydev = of_phy_connect(dev, priv->plat->phy_node,
829 &stmmac_adjust_link, 0, interface);
830 } else {
Giuseppe CAVALLAROa7657f12016-04-01 09:07:16 +0200831 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
832 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000833
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700834 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
835 priv->plat->phy_addr);
836 pr_debug("stmmac_init_phy: trying to attach to %s\n",
837 phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700838
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700839 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
840 interface);
841 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700842
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300843 if (IS_ERR_OR_NULL(phydev)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700844 pr_err("%s: Could not attach to PHY\n", dev->name);
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300845 if (!phydev)
846 return -ENODEV;
847
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700848 return PTR_ERR(phydev);
849 }
850
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000851 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000852 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000853 (interface == PHY_INTERFACE_MODE_RMII) ||
Pavel Macheka77e4ac2014-08-25 13:31:16 +0200854 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000855 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
856 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000857
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700858 /*
859 * Broken HW is sometimes missing the pull-up resistor on the
860 * MDIO line, which results in reads to non-existent devices returning
861 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
862 * device as well.
863 * Note: phydev->phy_id is the result of reading the UID PHY registers.
864 */
Mathieu Olivari27732382015-05-27 11:02:48 -0700865 if (!priv->plat->phy_node && phydev->phy_id == 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700866 phy_disconnect(phydev);
867 return -ENODEV;
868 }
Giuseppe Cavallaro8e99fc52016-02-29 14:27:39 +0100869
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700870 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000871 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700872
873 priv->phydev = phydev;
874
875 return 0;
876}
877
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700878/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100879 * stmmac_display_ring - display ring
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000880 * @head: pointer to the head of the ring passed.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700881 * @size: size of the ring.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000882 * @extend_desc: to verify if extended descriptors are used.
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000883 * Description: display the control/status and buffer descriptors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700884 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000885static void stmmac_display_ring(void *head, int size, int extend_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700886{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700887 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000888 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
889 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000890
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700891 for (i = 0; i < size; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000892 u64 x;
893 if (extend_desc) {
894 x = *(u64 *) ep;
895 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000896 i, (unsigned int)virt_to_phys(ep),
897 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000898 ep->basic.des2, ep->basic.des3);
899 ep++;
900 } else {
901 x = *(u64 *) p;
902 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000903 i, (unsigned int)virt_to_phys(p),
904 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000905 p->des2, p->des3);
906 p++;
907 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700908 pr_info("\n");
909 }
910}
911
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000912static void stmmac_display_rings(struct stmmac_priv *priv)
913{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000914 if (priv->extend_desc) {
915 pr_info("Extended RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100916 stmmac_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000917 pr_info("Extended TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100918 stmmac_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000919 } else {
920 pr_info("RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100921 stmmac_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000922 pr_info("TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100923 stmmac_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000924 }
925}
926
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000927static int stmmac_set_bfsize(int mtu, int bufsize)
928{
929 int ret = bufsize;
930
931 if (mtu >= BUF_SIZE_4KiB)
932 ret = BUF_SIZE_8KiB;
933 else if (mtu >= BUF_SIZE_2KiB)
934 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100935 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000936 ret = BUF_SIZE_2KiB;
937 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100938 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000939
940 return ret;
941}
942
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000943/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100944 * stmmac_clear_descriptors - clear descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000945 * @priv: driver private structure
946 * Description: this function is called to clear the tx and rx descriptors
947 * in case of both basic and extended descriptors are used.
948 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000949static void stmmac_clear_descriptors(struct stmmac_priv *priv)
950{
951 int i;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000952
953 /* Clear the Rx/Tx descriptors */
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100954 for (i = 0; i < DMA_RX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000955 if (priv->extend_desc)
956 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
957 priv->use_riwt, priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100958 (i == DMA_RX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000959 else
960 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
961 priv->use_riwt, priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100962 (i == DMA_RX_SIZE - 1));
963 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000964 if (priv->extend_desc)
965 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
966 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100967 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000968 else
969 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
970 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100971 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000972}
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 CAVALLARO4a7d6662013-03-26 04:43:05 +00001034 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001035 int ret = -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001036
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001037 if (priv->hw->mode->set_16kib_bfsize)
1038 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001039
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001040 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001041 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001042
Vince Bridgers2618abb2014-01-20 05:39:01 -06001043 priv->dma_buf_sz = bfsize;
1044
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001045 if (netif_msg_probe(priv)) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001046 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1047 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001048
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001049 /* RX INITIALIZATION */
1050 pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
1051 }
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001052 for (i = 0; i < DMA_RX_SIZE; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001053 struct dma_desc *p;
1054 if (priv->extend_desc)
1055 p = &((priv->dma_erx + i)->basic);
1056 else
1057 p = priv->dma_rx + i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001058
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001059 ret = stmmac_init_rx_buffers(priv, p, i, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001060 if (ret)
1061 goto err_init_rx_buffers;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001062
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001063 if (netif_msg_probe(priv))
1064 pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1065 priv->rx_skbuff[i]->data,
1066 (unsigned int)priv->rx_skbuff_dma[i]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001067 }
1068 priv->cur_rx = 0;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001069 priv->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001070 buf_sz = bfsize;
1071
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001072 /* Setup the chained descriptor addresses */
1073 if (priv->mode == STMMAC_CHAIN_MODE) {
1074 if (priv->extend_desc) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001075 priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001076 DMA_RX_SIZE, 1);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001077 priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001078 DMA_TX_SIZE, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001079 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001080 priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001081 DMA_RX_SIZE, 0);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001082 priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001083 DMA_TX_SIZE, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001084 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001085 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001086
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001087 /* TX INITIALIZATION */
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001088 for (i = 0; i < DMA_TX_SIZE; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001089 struct dma_desc *p;
1090 if (priv->extend_desc)
1091 p = &((priv->dma_etx + i)->basic);
1092 else
1093 p = priv->dma_tx + i;
1094 p->des2 = 0;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001095 priv->tx_skbuff_dma[i].buf = 0;
1096 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001097 priv->tx_skbuff_dma[i].len = 0;
Giuseppe Cavallaro2a6d8e12016-02-29 14:27:32 +01001098 priv->tx_skbuff_dma[i].last_segment = false;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001099 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001100 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001101
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001102 priv->dirty_tx = 0;
1103 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001104 netdev_reset_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001105
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001106 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001107
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001108 if (netif_msg_hw(priv))
1109 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001110
1111 return 0;
1112err_init_rx_buffers:
1113 while (--i >= 0)
1114 stmmac_free_rx_buffers(priv, i);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001115 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001116}
1117
1118static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1119{
1120 int i;
1121
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001122 for (i = 0; i < DMA_RX_SIZE; i++)
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001123 stmmac_free_rx_buffers(priv, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001124}
1125
1126static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1127{
1128 int i;
1129
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001130 for (i = 0; i < DMA_TX_SIZE; i++) {
damuzi00075e43642014-01-17 23:47:59 +08001131 struct dma_desc *p;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001132
damuzi00075e43642014-01-17 23:47:59 +08001133 if (priv->extend_desc)
1134 p = &((priv->dma_etx + i)->basic);
1135 else
1136 p = priv->dma_tx + i;
1137
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001138 if (priv->tx_skbuff_dma[i].buf) {
1139 if (priv->tx_skbuff_dma[i].map_as_page)
1140 dma_unmap_page(priv->device,
1141 priv->tx_skbuff_dma[i].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001142 priv->tx_skbuff_dma[i].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001143 DMA_TO_DEVICE);
1144 else
1145 dma_unmap_single(priv->device,
1146 priv->tx_skbuff_dma[i].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001147 priv->tx_skbuff_dma[i].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001148 DMA_TO_DEVICE);
damuzi00075e43642014-01-17 23:47:59 +08001149 }
1150
1151 if (priv->tx_skbuff[i] != NULL) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001152 dev_kfree_skb_any(priv->tx_skbuff[i]);
1153 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001154 priv->tx_skbuff_dma[i].buf = 0;
1155 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001156 }
1157 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001158}
1159
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001160/**
1161 * alloc_dma_desc_resources - alloc TX/RX resources.
1162 * @priv: private structure
1163 * Description: according to which descriptor can be used (extend or basic)
1164 * this function allocates the resources for TX and RX paths. In case of
1165 * reception, for example, it pre-allocated the RX socket buffer in order to
1166 * allow zero-copy mechanism.
1167 */
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001168static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1169{
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001170 int ret = -ENOMEM;
1171
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001172 priv->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE, sizeof(dma_addr_t),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001173 GFP_KERNEL);
1174 if (!priv->rx_skbuff_dma)
1175 return -ENOMEM;
1176
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001177 priv->rx_skbuff = kmalloc_array(DMA_RX_SIZE, sizeof(struct sk_buff *),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001178 GFP_KERNEL);
1179 if (!priv->rx_skbuff)
1180 goto err_rx_skbuff;
1181
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001182 priv->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001183 sizeof(*priv->tx_skbuff_dma),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001184 GFP_KERNEL);
1185 if (!priv->tx_skbuff_dma)
1186 goto err_tx_skbuff_dma;
1187
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001188 priv->tx_skbuff = kmalloc_array(DMA_TX_SIZE, sizeof(struct sk_buff *),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001189 GFP_KERNEL);
1190 if (!priv->tx_skbuff)
1191 goto err_tx_skbuff;
1192
1193 if (priv->extend_desc) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001194 priv->dma_erx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001195 sizeof(struct
1196 dma_extended_desc),
1197 &priv->dma_rx_phy,
1198 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001199 if (!priv->dma_erx)
1200 goto err_dma;
1201
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001202 priv->dma_etx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001203 sizeof(struct
1204 dma_extended_desc),
1205 &priv->dma_tx_phy,
1206 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001207 if (!priv->dma_etx) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001208 dma_free_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001209 sizeof(struct dma_extended_desc),
1210 priv->dma_erx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001211 goto err_dma;
1212 }
1213 } else {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001214 priv->dma_rx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001215 sizeof(struct dma_desc),
1216 &priv->dma_rx_phy,
1217 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001218 if (!priv->dma_rx)
1219 goto err_dma;
1220
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001221 priv->dma_tx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001222 sizeof(struct dma_desc),
1223 &priv->dma_tx_phy,
1224 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001225 if (!priv->dma_tx) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001226 dma_free_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001227 sizeof(struct dma_desc),
1228 priv->dma_rx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001229 goto err_dma;
1230 }
1231 }
1232
1233 return 0;
1234
1235err_dma:
1236 kfree(priv->tx_skbuff);
1237err_tx_skbuff:
1238 kfree(priv->tx_skbuff_dma);
1239err_tx_skbuff_dma:
1240 kfree(priv->rx_skbuff);
1241err_rx_skbuff:
1242 kfree(priv->rx_skbuff_dma);
1243 return ret;
1244}
1245
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001246static void free_dma_desc_resources(struct stmmac_priv *priv)
1247{
1248 /* Release the DMA TX/RX socket buffers */
1249 dma_free_rx_skbufs(priv);
1250 dma_free_tx_skbufs(priv);
1251
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001252 /* Free DMA regions of consistent memory previously allocated */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001253 if (!priv->extend_desc) {
1254 dma_free_coherent(priv->device,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001255 DMA_TX_SIZE * sizeof(struct dma_desc),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001256 priv->dma_tx, priv->dma_tx_phy);
1257 dma_free_coherent(priv->device,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001258 DMA_RX_SIZE * sizeof(struct dma_desc),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001259 priv->dma_rx, priv->dma_rx_phy);
1260 } else {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001261 dma_free_coherent(priv->device, DMA_TX_SIZE *
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001262 sizeof(struct dma_extended_desc),
1263 priv->dma_etx, priv->dma_tx_phy);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001264 dma_free_coherent(priv->device, DMA_RX_SIZE *
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001265 sizeof(struct dma_extended_desc),
1266 priv->dma_erx, priv->dma_rx_phy);
1267 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001268 kfree(priv->rx_skbuff_dma);
1269 kfree(priv->rx_skbuff);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001270 kfree(priv->tx_skbuff_dma);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001271 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001272}
1273
1274/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001275 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001276 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001277 * Description: it is used for configuring the DMA operation mode register in
1278 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001279 */
1280static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1281{
Vince Bridgersf88203a2015-04-15 11:17:42 -05001282 int rxfifosz = priv->plat->rx_fifo_size;
1283
Sonic Zhange2a240c2013-08-28 18:55:39 +08001284 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001285 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc, rxfifosz);
Sonic Zhange2a240c2013-08-28 18:55:39 +08001286 else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001287 /*
1288 * In case of GMAC, SF mode can be enabled
1289 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001290 * 1) TX COE if actually supported
1291 * 2) There is no bugged Jumbo frame support
1292 * that needs to not insert csum in the TDES.
1293 */
Vince Bridgersf88203a2015-04-15 11:17:42 -05001294 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE,
1295 rxfifosz);
Sonic Zhangb2dec112015-01-30 13:49:32 +08001296 priv->xstats.threshold = SF_DMA_MODE;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001297 } else
Vince Bridgersf88203a2015-04-15 11:17:42 -05001298 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE,
1299 rxfifosz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001300}
1301
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001302/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001303 * stmmac_tx_clean - to manage the transmission completion
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001304 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001305 * Description: it reclaims the transmit resources after transmission completes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001306 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001307static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001308{
Beniamino Galvani38979572015-01-21 19:07:27 +01001309 unsigned int bytes_compl = 0, pkts_compl = 0;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001310 unsigned int entry = priv->dirty_tx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001311
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001312 spin_lock(&priv->tx_lock);
1313
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001314 priv->xstats.tx_clean++;
1315
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001316 while (entry != priv->cur_tx) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001317 struct sk_buff *skb = priv->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001318 struct dma_desc *p;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001319 int status;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001320
1321 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001322 p = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001323 else
1324 p = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001325
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001326 status = priv->hw->desc->tx_status(&priv->dev->stats,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001327 &priv->xstats, p,
1328 priv->ioaddr);
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001329 /* Check if the descriptor is owned by the DMA */
1330 if (unlikely(status & tx_dma_own))
1331 break;
1332
1333 /* Just consider the last segment and ...*/
1334 if (likely(!(status & tx_not_ls))) {
1335 /* ... verify the status error condition */
1336 if (unlikely(status & tx_err)) {
1337 priv->dev->stats.tx_errors++;
1338 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001339 priv->dev->stats.tx_packets++;
1340 priv->xstats.tx_pkt_n++;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001341 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001342 stmmac_get_tx_hwtstamp(priv, entry, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001343 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001344
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001345 if (likely(priv->tx_skbuff_dma[entry].buf)) {
1346 if (priv->tx_skbuff_dma[entry].map_as_page)
1347 dma_unmap_page(priv->device,
1348 priv->tx_skbuff_dma[entry].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001349 priv->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001350 DMA_TO_DEVICE);
1351 else
1352 dma_unmap_single(priv->device,
1353 priv->tx_skbuff_dma[entry].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001354 priv->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001355 DMA_TO_DEVICE);
1356 priv->tx_skbuff_dma[entry].buf = 0;
1357 priv->tx_skbuff_dma[entry].map_as_page = false;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001358 }
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001359 priv->hw->mode->clean_desc3(priv, p);
Giuseppe Cavallaro2a6d8e12016-02-29 14:27:32 +01001360 priv->tx_skbuff_dma[entry].last_segment = false;
Giuseppe Cavallaro96951362016-02-29 14:27:33 +01001361 priv->tx_skbuff_dma[entry].is_jumbo = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001362
1363 if (likely(skb != NULL)) {
Beniamino Galvani38979572015-01-21 19:07:27 +01001364 pkts_compl++;
1365 bytes_compl += skb->len;
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001366 dev_consume_skb_any(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001367 priv->tx_skbuff[entry] = NULL;
1368 }
1369
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001370 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001371
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001372 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001373 }
Giuseppe Cavallarofbc80822016-02-29 14:27:37 +01001374 priv->dirty_tx = entry;
Beniamino Galvani38979572015-01-21 19:07:27 +01001375
1376 netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
1377
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001378 if (unlikely(netif_queue_stopped(priv->dev) &&
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001379 stmmac_tx_avail(priv) > STMMAC_TX_THRESH)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001380 netif_tx_lock(priv->dev);
1381 if (netif_queue_stopped(priv->dev) &&
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001382 stmmac_tx_avail(priv) > STMMAC_TX_THRESH) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001383 if (netif_msg_tx_done(priv))
1384 pr_debug("%s: restart transmit\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001385 netif_wake_queue(priv->dev);
1386 }
1387 netif_tx_unlock(priv->dev);
1388 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001389
1390 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1391 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001392 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001393 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001394 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001395}
1396
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001397static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001398{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001399 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001400}
1401
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001402static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001403{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001404 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001405}
1406
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001407/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001408 * stmmac_tx_err - to manage the tx error
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001409 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001410 * Description: it cleans the descriptors and restarts the transmission
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001411 * in case of transmission errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001412 */
1413static void stmmac_tx_err(struct stmmac_priv *priv)
1414{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001415 int i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001416 netif_stop_queue(priv->dev);
1417
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001418 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001419 dma_free_tx_skbufs(priv);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001420 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001421 if (priv->extend_desc)
1422 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1423 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001424 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001425 else
1426 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1427 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001428 (i == DMA_TX_SIZE - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001429 priv->dirty_tx = 0;
1430 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001431 netdev_reset_queue(priv->dev);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001432 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001433
1434 priv->dev->stats.tx_errors++;
1435 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001436}
1437
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001438/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001439 * stmmac_dma_interrupt - DMA ISR
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001440 * @priv: driver private structure
1441 * Description: this is the DMA ISR. It is called by the main ISR.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001442 * It calls the dwmac dma routine and schedule poll method in case of some
1443 * work can be done.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001444 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001445static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001446{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001447 int status;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001448 int rxfifosz = priv->plat->rx_fifo_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001449
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001450 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001451 if (likely((status & handle_rx)) || (status & handle_tx)) {
1452 if (likely(napi_schedule_prep(&priv->napi))) {
1453 stmmac_disable_dma_irq(priv);
1454 __napi_schedule(&priv->napi);
1455 }
1456 }
1457 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001458 /* Try to bump up the dma threshold on this failure */
Sonic Zhangb2dec112015-01-30 13:49:32 +08001459 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
1460 (tc <= 256)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001461 tc += 64;
Sonic Zhangc405abe2015-01-22 14:55:56 +08001462 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001463 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc,
1464 rxfifosz);
Sonic Zhangc405abe2015-01-22 14:55:56 +08001465 else
1466 priv->hw->dma->dma_mode(priv->ioaddr, tc,
Vince Bridgersf88203a2015-04-15 11:17:42 -05001467 SF_DMA_MODE, rxfifosz);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001468 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001469 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001470 } else if (unlikely(status == tx_hard_error))
1471 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001472}
1473
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001474/**
1475 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1476 * @priv: driver private structure
1477 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1478 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001479static void stmmac_mmc_setup(struct stmmac_priv *priv)
1480{
1481 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001482 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001483
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001484 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001485
1486 if (priv->dma_cap.rmon) {
1487 dwmac_mmc_ctrl(priv->ioaddr, mode);
1488 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1489 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +00001490 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001491}
1492
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001493/**
1494 * stmmac_get_synopsys_id - return the SYINID.
1495 * @priv: driver private structure
1496 * Description: this simple function is to decode and return the SYINID
1497 * starting from the HW core register.
1498 */
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001499static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
1500{
1501 u32 hwid = priv->hw->synopsys_uid;
1502
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001503 /* Check Synopsys Id (not available on old chips) */
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001504 if (likely(hwid)) {
1505 u32 uid = ((hwid & 0x0000ff00) >> 8);
1506 u32 synid = (hwid & 0x000000ff);
1507
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00001508 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
Giuseppe CAVALLAROf0b9d782011-09-01 21:51:40 +00001509 uid, synid);
1510
1511 return synid;
1512 }
1513 return 0;
1514}
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001515
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001516/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001517 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001518 * @priv: driver private structure
1519 * Description: select the Enhanced/Alternate or Normal descriptors.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001520 * In case of Enhanced/Alternate, it checks if the extended descriptors are
1521 * supported by the HW capability register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001522 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001523static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1524{
1525 if (priv->plat->enh_desc) {
1526 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001527
1528 /* GMAC older than 3.50 has no extended descriptors */
1529 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1530 pr_info("\tEnabled extended descriptors\n");
1531 priv->extend_desc = 1;
1532 } else
1533 pr_warn("Extended descriptors not supported\n");
1534
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001535 priv->hw->desc = &enh_desc_ops;
1536 } else {
1537 pr_info(" Normal descriptors\n");
1538 priv->hw->desc = &ndesc_ops;
1539 }
1540}
1541
1542/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001543 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001544 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001545 * Description:
1546 * new GMAC chip generations have a new register to indicate the
1547 * presence of the optional feature/functions.
1548 * This can be also used to override the value passed through the
1549 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001550 */
1551static int stmmac_get_hw_features(struct stmmac_priv *priv)
1552{
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001553 u32 hw_cap = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001554
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001555 if (priv->hw->dma->get_hw_feature) {
1556 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001557
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001558 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
1559 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
1560 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
1561 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001562 priv->dma_cap.multi_addr = (hw_cap & DMA_HW_FEAT_ADDMAC) >> 5;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001563 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
1564 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
1565 priv->dma_cap.pmt_remote_wake_up =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001566 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001567 priv->dma_cap.pmt_magic_frame =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001568 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001569 /* MMC */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001570 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001571 /* IEEE 1588-2002 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001572 priv->dma_cap.time_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001573 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
1574 /* IEEE 1588-2008 */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001575 priv->dma_cap.atime_stamp =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001576 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001577 /* 802.3az - Energy-Efficient Ethernet (EEE) */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001578 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
1579 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001580 /* TX and RX csum */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001581 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
1582 priv->dma_cap.rx_coe_type1 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001583 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001584 priv->dma_cap.rx_coe_type2 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001585 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001586 priv->dma_cap.rxfifo_over_2048 =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001587 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001588 /* TX and RX number of channels */
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001589 priv->dma_cap.number_rx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001590 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
Rayagond Kokatanur1db123f2011-10-18 00:01:22 +00001591 priv->dma_cap.number_tx_channel =
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001592 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
1593 /* Alternate (enhanced) DESC mode */
1594 priv->dma_cap.enh_desc = (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001595 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001596
1597 return hw_cap;
1598}
1599
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001600/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001601 * stmmac_check_ether_addr - check if the MAC addr is valid
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001602 * @priv: driver private structure
1603 * Description:
1604 * it is to verify if the MAC address is valid, in case of failures it
1605 * generates a random MAC address
1606 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001607static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1608{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001609 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001610 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001611 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001612 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001613 eth_hw_addr_random(priv->dev);
Hans de Goedec88460b2014-01-26 15:50:44 +01001614 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1615 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001616 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001617}
1618
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001619/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001620 * stmmac_init_dma_engine - DMA init.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001621 * @priv: driver private structure
1622 * Description:
1623 * It inits the DMA invoking the specific MAC/GMAC callback.
1624 * Some DMA parameters can be passed from the platform;
1625 * in case of these are not passed a default is kept for the MAC or GMAC.
1626 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001627static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1628{
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001629 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, aal = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001630 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001631 int atds = 0;
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001632 int ret = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001633
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001634 if (priv->plat->dma_cfg) {
1635 pbl = priv->plat->dma_cfg->pbl;
1636 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001637 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001638 aal = priv->plat->dma_cfg->aal;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001639 }
1640
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001641 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1642 atds = 1;
1643
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001644 ret = priv->hw->dma->reset(priv->ioaddr);
1645 if (ret) {
1646 dev_err(priv->device, "Failed to reset the dma\n");
1647 return ret;
1648 }
1649
1650 priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001651 aal, priv->dma_tx_phy, priv->dma_rx_phy, atds);
1652
1653 if ((priv->synopsys_id >= DWMAC_CORE_3_50) &&
1654 (priv->plat->axi && priv->hw->dma->axi))
1655 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
1656
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001657 return ret;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001658}
1659
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001660/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001661 * stmmac_tx_timer - mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001662 * @data: data pointer
1663 * Description:
1664 * This is the timer handler to directly invoke the stmmac_tx_clean.
1665 */
1666static void stmmac_tx_timer(unsigned long data)
1667{
1668 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1669
1670 stmmac_tx_clean(priv);
1671}
1672
1673/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001674 * stmmac_init_tx_coalesce - init tx mitigation options.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001675 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001676 * Description:
1677 * This inits the transmit coalesce parameters: i.e. timer rate,
1678 * timer handler and default threshold used for enabling the
1679 * interrupt on completion bit.
1680 */
1681static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1682{
1683 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1684 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1685 init_timer(&priv->txtimer);
1686 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1687 priv->txtimer.data = (unsigned long)priv;
1688 priv->txtimer.function = stmmac_tx_timer;
1689 add_timer(&priv->txtimer);
1690}
1691
1692/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001693 * stmmac_hw_setup - setup mac in a usable state.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001694 * @dev : pointer to the device structure.
1695 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001696 * this is the main function to setup the HW in a usable state because the
1697 * dma engine is reset, the core registers are configured (e.g. AXI,
1698 * Checksum features, timers). The DMA is ready to start receiving and
1699 * transmitting.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001700 * Return value:
1701 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1702 * file on failure.
1703 */
Huacai Chenfe1319292014-12-19 22:38:18 +08001704static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001705{
1706 struct stmmac_priv *priv = netdev_priv(dev);
1707 int ret;
1708
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001709 /* DMA initialization and SW reset */
1710 ret = stmmac_init_dma_engine(priv);
1711 if (ret < 0) {
1712 pr_err("%s: DMA engine initialization failed\n", __func__);
1713 return ret;
1714 }
1715
1716 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001717 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001718
1719 /* If required, perform hw setup of the bus. */
1720 if (priv->plat->bus_setup)
1721 priv->plat->bus_setup(priv->ioaddr);
1722
1723 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001724 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001725
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001726 ret = priv->hw->mac->rx_ipc(priv->hw);
1727 if (!ret) {
1728 pr_warn(" RX IPC Checksum Offload disabled\n");
1729 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02001730 priv->hw->rx_csum = 0;
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001731 }
1732
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001733 /* Enable the MAC Rx/Tx */
1734 stmmac_set_mac(priv->ioaddr, true);
1735
1736 /* Set the HW DMA mode and the COE */
1737 stmmac_dma_operation_mode(priv);
1738
1739 stmmac_mmc_setup(priv);
1740
Huacai Chenfe1319292014-12-19 22:38:18 +08001741 if (init_ptp) {
1742 ret = stmmac_init_ptp(priv);
1743 if (ret && ret != -EOPNOTSUPP)
1744 pr_warn("%s: failed PTP initialisation\n", __func__);
1745 }
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001746
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001747#ifdef CONFIG_DEBUG_FS
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001748 ret = stmmac_init_fs(dev);
1749 if (ret < 0)
1750 pr_warn("%s: failed debugFS registration\n", __func__);
1751#endif
1752 /* Start the ball rolling... */
1753 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1754 priv->hw->dma->start_tx(priv->ioaddr);
1755 priv->hw->dma->start_rx(priv->ioaddr);
1756
1757 /* Dump DMA/MAC registers */
1758 if (netif_msg_hw(priv)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001759 priv->hw->mac->dump_regs(priv->hw);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001760 priv->hw->dma->dump_regs(priv->ioaddr);
1761 }
1762 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1763
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001764 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1765 priv->rx_riwt = MAX_DMA_RIWT;
1766 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1767 }
1768
1769 if (priv->pcs && priv->hw->mac->ctrl_ane)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001770 priv->hw->mac->ctrl_ane(priv->hw, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001771
1772 return 0;
1773}
1774
1775/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001776 * stmmac_open - open entry point of the driver
1777 * @dev : pointer to the device structure.
1778 * Description:
1779 * This function is the open entry point of the driver.
1780 * Return value:
1781 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1782 * file on failure.
1783 */
1784static int stmmac_open(struct net_device *dev)
1785{
1786 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001787 int ret;
1788
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001789 stmmac_check_ether_addr(priv);
1790
Byungho An4d8f0822013-04-07 17:56:16 +00001791 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1792 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001793 ret = stmmac_init_phy(dev);
1794 if (ret) {
1795 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1796 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02001797 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001798 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001799 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001800
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001801 /* Extra statistics */
1802 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1803 priv->xstats.threshold = tc;
1804
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001805 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01001806 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001807
Tobias Klauser7262b7b2014-02-22 13:09:03 +01001808 ret = alloc_dma_desc_resources(priv);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001809 if (ret < 0) {
1810 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1811 goto dma_desc_error;
1812 }
1813
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001814 ret = init_dma_desc_rings(dev, GFP_KERNEL);
1815 if (ret < 0) {
1816 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1817 goto init_error;
1818 }
1819
Huacai Chenfe1319292014-12-19 22:38:18 +08001820 ret = stmmac_hw_setup(dev, true);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001821 if (ret < 0) {
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001822 pr_err("%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001823 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001824 }
1825
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001826 stmmac_init_tx_coalesce(priv);
1827
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001828 if (priv->phydev)
1829 phy_start(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001830
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001831 /* Request the IRQ lines */
1832 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001833 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001834 if (unlikely(ret < 0)) {
1835 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1836 __func__, dev->irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001837 goto init_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001838 }
1839
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001840 /* Request the Wake IRQ in case of another line is used for WoL */
1841 if (priv->wol_irq != dev->irq) {
1842 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1843 IRQF_SHARED, dev->name, dev);
1844 if (unlikely(ret < 0)) {
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001845 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1846 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001847 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001848 }
1849 }
1850
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001851 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001852 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001853 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1854 dev->name, dev);
1855 if (unlikely(ret < 0)) {
1856 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1857 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001858 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001859 }
1860 }
1861
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001862 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001863 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001864
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001865 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001866
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001867lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001868 if (priv->wol_irq != dev->irq)
1869 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001870wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001871 free_irq(dev->irq, dev);
1872
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001873init_error:
1874 free_dma_desc_resources(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001875dma_desc_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001876 if (priv->phydev)
1877 phy_disconnect(priv->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001878
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001879 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001880}
1881
1882/**
1883 * stmmac_release - close entry point of the driver
1884 * @dev : device pointer.
1885 * Description:
1886 * This is the stop entry point of the driver.
1887 */
1888static int stmmac_release(struct net_device *dev)
1889{
1890 struct stmmac_priv *priv = netdev_priv(dev);
1891
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001892 if (priv->eee_enabled)
1893 del_timer_sync(&priv->eee_ctrl_timer);
1894
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001895 /* Stop and disconnect the PHY */
1896 if (priv->phydev) {
1897 phy_stop(priv->phydev);
1898 phy_disconnect(priv->phydev);
1899 priv->phydev = NULL;
1900 }
1901
1902 netif_stop_queue(dev);
1903
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001904 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001905
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001906 del_timer_sync(&priv->txtimer);
1907
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001908 /* Free the IRQ lines */
1909 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001910 if (priv->wol_irq != dev->irq)
1911 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001912 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001913 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001914
1915 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001916 priv->hw->dma->stop_tx(priv->ioaddr);
1917 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001918
1919 /* Release and free the Rx/Tx resources */
1920 free_dma_desc_resources(priv);
1921
avisconti19449bf2010-10-25 18:58:14 +00001922 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001923 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001924
1925 netif_carrier_off(dev);
1926
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001927#ifdef CONFIG_DEBUG_FS
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07001928 stmmac_exit_fs(dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001929#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001930
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00001931 stmmac_release_ptp(priv);
1932
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001933 return 0;
1934}
1935
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001936/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001937 * stmmac_xmit - Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001938 * @skb : the socket buffer
1939 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001940 * Description : this is the tx entry point of the driver.
1941 * It programs the chain or the ring and supports oversized frames
1942 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001943 */
1944static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1945{
1946 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001947 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001948 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001949 int nfrags = skb_shinfo(skb)->nr_frags;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001950 unsigned int entry, first_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001951 struct dma_desc *desc, *first;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001952 unsigned int enh_desc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001953
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001954 spin_lock(&priv->tx_lock);
1955
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001956 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001957 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001958 if (!netif_queue_stopped(dev)) {
1959 netif_stop_queue(dev);
1960 /* This is a hard error, log it. */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001961 pr_err("%s: Tx Ring full when queue awake\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001962 }
1963 return NETDEV_TX_BUSY;
1964 }
1965
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001966 if (priv->tx_path_in_lpi_mode)
1967 stmmac_disable_eee_mode(priv);
1968
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001969 entry = priv->cur_tx;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001970 first_entry = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001971
Michał Mirosław5e982f32011-04-09 02:46:55 +00001972 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001973
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001974 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001975 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001976 else
1977 desc = priv->dma_tx + entry;
1978
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001979 first = desc;
1980
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001981 priv->tx_skbuff[first_entry] = skb;
1982
1983 enh_desc = priv->plat->enh_desc;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001984 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001985 if (enh_desc)
1986 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
1987
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001988 if (unlikely(is_jumbo)) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001989 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001990 if (unlikely(entry < 0))
1991 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001992 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001993
1994 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001995 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1996 int len = skb_frag_size(frag);
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01001997 bool last_segment = (i == (nfrags - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001998
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001999 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2000
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002001 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002002 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002003 else
2004 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002005
Ian Campbellf7223802011-09-21 21:53:20 +00002006 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
2007 DMA_TO_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002008 if (dma_mapping_error(priv->device, desc->des2))
2009 goto dma_map_err; /* should reuse desc w/o issues */
2010
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002011 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002012 priv->tx_skbuff_dma[entry].buf = desc->des2;
2013 priv->tx_skbuff_dma[entry].map_as_page = true;
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01002014 priv->tx_skbuff_dma[entry].len = len;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002015 priv->tx_skbuff_dma[entry].last_segment = last_segment;
2016
2017 /* Prepare the descriptor and set the own bit too */
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002018 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01002019 priv->mode, 1, last_segment);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002020 }
2021
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002022 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2023
2024 priv->cur_tx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002025
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002026 if (netif_msg_pktdata(priv)) {
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002027 pr_debug("%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
2028 __func__, priv->cur_tx, priv->dirty_tx, first_entry,
2029 entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002030
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002031 if (priv->extend_desc)
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002032 stmmac_display_ring((void *)priv->dma_etx,
2033 DMA_TX_SIZE, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002034 else
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002035 stmmac_display_ring((void *)priv->dma_tx,
2036 DMA_TX_SIZE, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002037
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002038 pr_debug(">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002039 print_pkt(skb->data, skb->len);
2040 }
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002041
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002042 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002043 if (netif_msg_hw(priv))
2044 pr_debug("%s: stop transmitted packets\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002045 netif_stop_queue(dev);
2046 }
2047
2048 dev->stats.tx_bytes += skb->len;
2049
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002050 /* According to the coalesce parameter the IC bit for the latest
2051 * segment is reset and the timer re-started to clean the tx status.
2052 * This approach takes care about the fragments: desc is the first
2053 * element in case of no SG.
2054 */
2055 priv->tx_count_frames += nfrags + 1;
2056 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
2057 mod_timer(&priv->txtimer,
2058 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2059 } else {
2060 priv->tx_count_frames = 0;
2061 priv->hw->desc->set_tx_ic(desc);
2062 priv->xstats.tx_set_ic_bit++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002063 }
2064
2065 if (!priv->hwts_tx_en)
2066 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00002067
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002068 /* Ready to fill the first descriptor and set the OWN bit w/o any
2069 * problems because all the descriptors are actually ready to be
2070 * passed to the DMA engine.
2071 */
2072 if (likely(!is_jumbo)) {
2073 bool last_segment = (nfrags == 0);
2074
2075 first->des2 = dma_map_single(priv->device, skb->data,
2076 nopaged_len, DMA_TO_DEVICE);
2077 if (dma_mapping_error(priv->device, first->des2))
2078 goto dma_map_err;
2079
2080 priv->tx_skbuff_dma[first_entry].buf = first->des2;
2081 priv->tx_skbuff_dma[first_entry].len = nopaged_len;
2082 priv->tx_skbuff_dma[first_entry].last_segment = last_segment;
2083
2084 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2085 priv->hwts_tx_en)) {
2086 /* declare that device is doing timestamping */
2087 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2088 priv->hw->desc->enable_tx_timestamp(first);
2089 }
2090
2091 /* Prepare the first descriptor setting the OWN bit too */
2092 priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
2093 csum_insertion, priv->mode, 1,
2094 last_segment);
2095
2096 /* The own bit must be the latest setting done when prepare the
2097 * descriptor and then barrier is needed to make sure that
2098 * all is coherent before granting the DMA engine.
2099 */
2100 smp_wmb();
2101 }
2102
Beniamino Galvani38979572015-01-21 19:07:27 +01002103 netdev_sent_queue(dev, skb->len);
Richard Cochran52f64fa2011-06-19 03:31:43 +00002104 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2105
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002106 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002107 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002108
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002109dma_map_err:
Fabrice Gasnier758a0ab2014-11-04 17:08:06 +01002110 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002111 dev_err(priv->device, "Tx dma map failed\n");
2112 dev_kfree_skb(skb);
2113 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002114 return NETDEV_TX_OK;
2115}
2116
Vince Bridgersb9381982014-01-14 13:42:05 -06002117static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2118{
2119 struct ethhdr *ehdr;
2120 u16 vlanid;
2121
2122 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2123 NETIF_F_HW_VLAN_CTAG_RX &&
2124 !__vlan_get_tag(skb, &vlanid)) {
2125 /* pop the vlan tag */
2126 ehdr = (struct ethhdr *)skb->data;
2127 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2128 skb_pull(skb, VLAN_HLEN);
2129 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2130 }
2131}
2132
2133
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002134static inline int stmmac_rx_threshold_count(struct stmmac_priv *priv)
2135{
2136 if (priv->rx_zeroc_thresh < STMMAC_RX_THRESH)
2137 return 0;
2138
2139 return 1;
2140}
2141
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002142/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002143 * stmmac_rx_refill - refill used skb preallocated buffers
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002144 * @priv: driver private structure
2145 * Description : this is to reallocate the skb for the reception process
2146 * that is based on zero-copy.
2147 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002148static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2149{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002150 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002151 unsigned int entry = priv->dirty_rx;
2152 int dirty = stmmac_rx_dirty(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002153
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002154 while (dirty-- > 0) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002155 struct dma_desc *p;
2156
2157 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002158 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002159 else
2160 p = priv->dma_rx + entry;
2161
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002162 if (likely(priv->rx_skbuff[entry] == NULL)) {
2163 struct sk_buff *skb;
2164
Eric Dumazetacb600d2012-10-05 06:23:55 +00002165 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002166 if (unlikely(!skb)) {
2167 /* so for a while no zero-copy! */
2168 priv->rx_zeroc_thresh = STMMAC_RX_THRESH;
2169 if (unlikely(net_ratelimit()))
2170 dev_err(priv->device,
2171 "fail to alloc skb entry %d\n",
2172 entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002173 break;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002174 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002175
2176 priv->rx_skbuff[entry] = skb;
2177 priv->rx_skbuff_dma[entry] =
2178 dma_map_single(priv->device, skb->data, bfsize,
2179 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002180 if (dma_mapping_error(priv->device,
2181 priv->rx_skbuff_dma[entry])) {
2182 dev_err(priv->device, "Rx dma map failed\n");
2183 dev_kfree_skb(skb);
2184 break;
2185 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002186 p->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002187
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002188 priv->hw->mode->refill_desc3(priv, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002189
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002190 if (priv->rx_zeroc_thresh > 0)
2191 priv->rx_zeroc_thresh--;
2192
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002193 if (netif_msg_rx_status(priv))
2194 pr_debug("\trefill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002195 }
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002196
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002197 wmb();
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002198 priv->hw->desc->set_rx_owner(p);
Deepak Sikri8e839892012-07-08 21:14:45 +00002199 wmb();
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002200
2201 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002202 }
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002203 priv->dirty_rx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002204}
2205
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002206/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002207 * stmmac_rx - manage the receive process
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002208 * @priv: driver private structure
2209 * @limit: napi bugget.
2210 * Description : this the function called by the napi poll method.
2211 * It gets all the frames inside the ring.
2212 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002213static int stmmac_rx(struct stmmac_priv *priv, int limit)
2214{
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002215 unsigned int entry = priv->cur_rx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002216 unsigned int next_entry;
2217 unsigned int count = 0;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002218 int coe = priv->hw->rx_csum;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002219
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002220 if (netif_msg_rx_status(priv)) {
2221 pr_debug("%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002222 if (priv->extend_desc)
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002223 stmmac_display_ring((void *)priv->dma_erx,
2224 DMA_RX_SIZE, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002225 else
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002226 stmmac_display_ring((void *)priv->dma_rx,
2227 DMA_RX_SIZE, 0);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002228 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002229 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002230 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002231 struct dma_desc *p;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002232
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002233 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002234 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002235 else
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002236 p = priv->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002237
Fabrice Gasnierc1fa3212016-02-29 14:27:34 +01002238 /* read the status of the incoming frame */
2239 status = priv->hw->desc->rx_status(&priv->dev->stats,
2240 &priv->xstats, p);
2241 /* check if managed by the DMA otherwise go ahead */
2242 if (unlikely(status & dma_own))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002243 break;
2244
2245 count++;
2246
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002247 priv->cur_rx = STMMAC_GET_ENTRY(priv->cur_rx, DMA_RX_SIZE);
2248 next_entry = priv->cur_rx;
2249
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002250 if (priv->extend_desc)
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002251 prefetch(priv->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002252 else
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002253 prefetch(priv->dma_rx + next_entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002254
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002255 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2256 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2257 &priv->xstats,
2258 priv->dma_erx +
2259 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002260 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002261 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002262 if (priv->hwts_rx_en && !priv->extend_desc) {
2263 /* DESC2 & DESC3 will be overwitten by device
2264 * with timestamp value, hence reinitialize
2265 * them in stmmac_rx_refill() function so that
2266 * device can reuse it.
2267 */
2268 priv->rx_skbuff[entry] = NULL;
2269 dma_unmap_single(priv->device,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002270 priv->rx_skbuff_dma[entry],
2271 priv->dma_buf_sz,
2272 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002273 }
2274 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002275 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002276 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002277
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002278 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2279
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01002280 /* check if frame_len fits the preallocated memory */
2281 if (frame_len > priv->dma_buf_sz) {
2282 priv->dev->stats.rx_length_errors++;
2283 break;
2284 }
2285
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002286 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002287 * Type frames (LLC/LLC-SNAP)
2288 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002289 if (unlikely(status != llc_snap))
2290 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002291
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002292 if (netif_msg_rx_status(priv)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002293 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002294 p, entry, p->des2);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002295 if (frame_len > ETH_FRAME_LEN)
2296 pr_debug("\tframe size %d, COE: %d\n",
2297 frame_len, status);
2298 }
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002299
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002300 if (unlikely((frame_len < priv->rx_copybreak) ||
2301 stmmac_rx_threshold_count(priv))) {
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002302 skb = netdev_alloc_skb_ip_align(priv->dev,
2303 frame_len);
2304 if (unlikely(!skb)) {
2305 if (net_ratelimit())
2306 dev_warn(priv->device,
2307 "packet dropped\n");
2308 priv->dev->stats.rx_dropped++;
2309 break;
2310 }
2311
2312 dma_sync_single_for_cpu(priv->device,
2313 priv->rx_skbuff_dma
2314 [entry], frame_len,
2315 DMA_FROM_DEVICE);
2316 skb_copy_to_linear_data(skb,
2317 priv->
2318 rx_skbuff[entry]->data,
2319 frame_len);
2320
2321 skb_put(skb, frame_len);
2322 dma_sync_single_for_device(priv->device,
2323 priv->rx_skbuff_dma
2324 [entry], frame_len,
2325 DMA_FROM_DEVICE);
2326 } else {
2327 skb = priv->rx_skbuff[entry];
2328 if (unlikely(!skb)) {
2329 pr_err("%s: Inconsistent Rx chain\n",
2330 priv->dev->name);
2331 priv->dev->stats.rx_dropped++;
2332 break;
2333 }
2334 prefetch(skb->data - NET_IP_ALIGN);
2335 priv->rx_skbuff[entry] = NULL;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002336 priv->rx_zeroc_thresh++;
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002337
2338 skb_put(skb, frame_len);
2339 dma_unmap_single(priv->device,
2340 priv->rx_skbuff_dma[entry],
2341 priv->dma_buf_sz,
2342 DMA_FROM_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002343 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002344
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002345 stmmac_get_rx_hwtstamp(priv, entry, skb);
2346
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002347 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002348 pr_debug("frame received (%dbytes)", frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002349 print_pkt(skb->data, frame_len);
2350 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002351
Vince Bridgersb9381982014-01-14 13:42:05 -06002352 stmmac_rx_vlan(priv->dev, skb);
2353
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002354 skb->protocol = eth_type_trans(skb, priv->dev);
2355
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002356 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002357 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002358 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002359 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002360
2361 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002362
2363 priv->dev->stats.rx_packets++;
2364 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002365 }
2366 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002367 }
2368
2369 stmmac_rx_refill(priv);
2370
2371 priv->xstats.rx_pkt_n += count;
2372
2373 return count;
2374}
2375
2376/**
2377 * stmmac_poll - stmmac poll method (NAPI)
2378 * @napi : pointer to the napi structure.
2379 * @budget : maximum number of packets that the current CPU can receive from
2380 * all interfaces.
2381 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002382 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002383 */
2384static int stmmac_poll(struct napi_struct *napi, int budget)
2385{
2386 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2387 int work_done = 0;
2388
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002389 priv->xstats.napi_poll++;
2390 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002391
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002392 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002393 if (work_done < budget) {
2394 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002395 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002396 }
2397 return work_done;
2398}
2399
2400/**
2401 * stmmac_tx_timeout
2402 * @dev : Pointer to net device structure
2403 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00002404 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002405 * netdev structure and arrange for the device to be reset to a sane state
2406 * in order to transmit a new packet.
2407 */
2408static void stmmac_tx_timeout(struct net_device *dev)
2409{
2410 struct stmmac_priv *priv = netdev_priv(dev);
2411
2412 /* Clear Tx resources and restart transmitting again */
2413 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002414}
2415
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002416/**
Jiri Pirko01789342011-08-16 06:29:00 +00002417 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002418 * @dev : pointer to the device structure
2419 * Description:
2420 * This function is a driver entry point which gets called by the kernel
2421 * whenever multicast addresses must be enabled/disabled.
2422 * Return value:
2423 * void.
2424 */
Jiri Pirko01789342011-08-16 06:29:00 +00002425static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002426{
2427 struct stmmac_priv *priv = netdev_priv(dev);
2428
Vince Bridgers3b57de92014-07-31 15:49:17 -05002429 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002430}
2431
2432/**
2433 * stmmac_change_mtu - entry point to change MTU size for the device.
2434 * @dev : device pointer.
2435 * @new_mtu : the new MTU size for the device.
2436 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2437 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2438 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2439 * Return value:
2440 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2441 * file on failure.
2442 */
2443static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2444{
2445 struct stmmac_priv *priv = netdev_priv(dev);
2446 int max_mtu;
2447
2448 if (netif_running(dev)) {
2449 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2450 return -EBUSY;
2451 }
2452
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00002453 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002454 max_mtu = JUMBO_LEN;
2455 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00002456 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002457
Vince Bridgers2618abb2014-01-20 05:39:01 -06002458 if (priv->plat->maxmtu < max_mtu)
2459 max_mtu = priv->plat->maxmtu;
2460
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002461 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2462 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2463 return -EINVAL;
2464 }
2465
Michał Mirosław5e982f32011-04-09 02:46:55 +00002466 dev->mtu = new_mtu;
2467 netdev_update_features(dev);
2468
2469 return 0;
2470}
2471
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002472static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002473 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002474{
2475 struct stmmac_priv *priv = netdev_priv(dev);
2476
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002477 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002478 features &= ~NETIF_F_RXCSUM;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002479
Michał Mirosław5e982f32011-04-09 02:46:55 +00002480 if (!priv->plat->tx_coe)
Tom Herberta1882222015-12-14 11:19:43 -08002481 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosław5e982f32011-04-09 02:46:55 +00002482
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002483 /* Some GMAC devices have a bugged Jumbo frame support that
2484 * needs to have the Tx COE disabled for oversized frames
2485 * (due to limited buffer sizes). In this case we disable
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002486 * the TX csum insertionin the TDES and not use SF.
2487 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00002488 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08002489 features &= ~NETIF_F_CSUM_MASK;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002490
Michał Mirosław5e982f32011-04-09 02:46:55 +00002491 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002492}
2493
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002494static int stmmac_set_features(struct net_device *netdev,
2495 netdev_features_t features)
2496{
2497 struct stmmac_priv *priv = netdev_priv(netdev);
2498
2499 /* Keep the COE Type in case of csum is supporting */
2500 if (features & NETIF_F_RXCSUM)
2501 priv->hw->rx_csum = priv->plat->rx_coe;
2502 else
2503 priv->hw->rx_csum = 0;
2504 /* No check needed because rx_coe has been set before and it will be
2505 * fixed in case of issue.
2506 */
2507 priv->hw->mac->rx_ipc(priv->hw);
2508
2509 return 0;
2510}
2511
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002512/**
2513 * stmmac_interrupt - main ISR
2514 * @irq: interrupt number.
2515 * @dev_id: to pass the net device pointer.
2516 * Description: this is the main driver interrupt service routine.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002517 * It can call:
2518 * o DMA service routine (to manage incoming frame reception and transmission
2519 * status)
2520 * o Core interrupts to manage: remote wake-up, management counter, LPI
2521 * interrupts.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002522 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002523static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2524{
2525 struct net_device *dev = (struct net_device *)dev_id;
2526 struct stmmac_priv *priv = netdev_priv(dev);
2527
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002528 if (priv->irq_wake)
2529 pm_wakeup_event(priv->device, 0);
2530
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002531 if (unlikely(!dev)) {
2532 pr_err("%s: invalid dev pointer\n", __func__);
2533 return IRQ_NONE;
2534 }
2535
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002536 /* To handle GMAC own interrupts */
2537 if (priv->plat->has_gmac) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002538 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002539 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002540 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002541 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002542 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002543 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002544 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002545 priv->tx_path_in_lpi_mode = false;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002546 }
2547 }
2548
2549 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002550 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002551
2552 return IRQ_HANDLED;
2553}
2554
2555#ifdef CONFIG_NET_POLL_CONTROLLER
2556/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002557 * to allow network I/O with interrupts disabled.
2558 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002559static void stmmac_poll_controller(struct net_device *dev)
2560{
2561 disable_irq(dev->irq);
2562 stmmac_interrupt(dev->irq, dev);
2563 enable_irq(dev->irq);
2564}
2565#endif
2566
2567/**
2568 * stmmac_ioctl - Entry point for the Ioctl
2569 * @dev: Device pointer.
2570 * @rq: An IOCTL specefic structure, that can contain a pointer to
2571 * a proprietary structure used to pass information to the driver.
2572 * @cmd: IOCTL command
2573 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002574 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002575 */
2576static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2577{
2578 struct stmmac_priv *priv = netdev_priv(dev);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002579 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002580
2581 if (!netif_running(dev))
2582 return -EINVAL;
2583
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002584 switch (cmd) {
2585 case SIOCGMIIPHY:
2586 case SIOCGMIIREG:
2587 case SIOCSMIIREG:
2588 if (!priv->phydev)
2589 return -EINVAL;
2590 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2591 break;
2592 case SIOCSHWTSTAMP:
2593 ret = stmmac_hwtstamp_ioctl(dev, rq);
2594 break;
2595 default:
2596 break;
2597 }
Richard Cochran28b04112010-07-17 08:48:55 +00002598
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002599 return ret;
2600}
2601
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002602#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002603static struct dentry *stmmac_fs_dir;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002604
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002605static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002606 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002607{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002608 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002609 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2610 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002611
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002612 for (i = 0; i < size; i++) {
2613 u64 x;
2614 if (extend_desc) {
2615 x = *(u64 *) ep;
2616 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002617 i, (unsigned int)virt_to_phys(ep),
2618 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002619 ep->basic.des2, ep->basic.des3);
2620 ep++;
2621 } else {
2622 x = *(u64 *) p;
2623 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002624 i, (unsigned int)virt_to_phys(ep),
2625 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002626 p->des2, p->des3);
2627 p++;
2628 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002629 seq_printf(seq, "\n");
2630 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002631}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002632
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002633static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2634{
2635 struct net_device *dev = seq->private;
2636 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002637
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002638 if (priv->extend_desc) {
2639 seq_printf(seq, "Extended RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002640 sysfs_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002641 seq_printf(seq, "Extended TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002642 sysfs_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002643 } else {
2644 seq_printf(seq, "RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002645 sysfs_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002646 seq_printf(seq, "TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002647 sysfs_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002648 }
2649
2650 return 0;
2651}
2652
2653static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2654{
2655 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2656}
2657
2658static const struct file_operations stmmac_rings_status_fops = {
2659 .owner = THIS_MODULE,
2660 .open = stmmac_sysfs_ring_open,
2661 .read = seq_read,
2662 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002663 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002664};
2665
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002666static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2667{
2668 struct net_device *dev = seq->private;
2669 struct stmmac_priv *priv = netdev_priv(dev);
2670
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002671 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002672 seq_printf(seq, "DMA HW features not supported\n");
2673 return 0;
2674 }
2675
2676 seq_printf(seq, "==============================\n");
2677 seq_printf(seq, "\tDMA HW features\n");
2678 seq_printf(seq, "==============================\n");
2679
2680 seq_printf(seq, "\t10/100 Mbps %s\n",
2681 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2682 seq_printf(seq, "\t1000 Mbps %s\n",
2683 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2684 seq_printf(seq, "\tHalf duple %s\n",
2685 (priv->dma_cap.half_duplex) ? "Y" : "N");
2686 seq_printf(seq, "\tHash Filter: %s\n",
2687 (priv->dma_cap.hash_filter) ? "Y" : "N");
2688 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2689 (priv->dma_cap.multi_addr) ? "Y" : "N");
2690 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2691 (priv->dma_cap.pcs) ? "Y" : "N");
2692 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2693 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2694 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2695 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2696 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2697 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2698 seq_printf(seq, "\tRMON module: %s\n",
2699 (priv->dma_cap.rmon) ? "Y" : "N");
2700 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2701 (priv->dma_cap.time_stamp) ? "Y" : "N");
2702 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2703 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2704 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2705 (priv->dma_cap.eee) ? "Y" : "N");
2706 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2707 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2708 (priv->dma_cap.tx_coe) ? "Y" : "N");
2709 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2710 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2711 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2712 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2713 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2714 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2715 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2716 priv->dma_cap.number_rx_channel);
2717 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2718 priv->dma_cap.number_tx_channel);
2719 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2720 (priv->dma_cap.enh_desc) ? "Y" : "N");
2721
2722 return 0;
2723}
2724
2725static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2726{
2727 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2728}
2729
2730static const struct file_operations stmmac_dma_cap_fops = {
2731 .owner = THIS_MODULE,
2732 .open = stmmac_sysfs_dma_cap_open,
2733 .read = seq_read,
2734 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002735 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002736};
2737
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002738static int stmmac_init_fs(struct net_device *dev)
2739{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002740 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002741
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002742 /* Create per netdev entries */
2743 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
2744
2745 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
2746 pr_err("ERROR %s/%s, debugfs create directory failed\n",
2747 STMMAC_RESOURCE_NAME, dev->name);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002748
2749 return -ENOMEM;
2750 }
2751
2752 /* Entry to report DMA RX/TX rings */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002753 priv->dbgfs_rings_status =
2754 debugfs_create_file("descriptors_status", S_IRUGO,
2755 priv->dbgfs_dir, dev,
2756 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002757
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002758 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002759 pr_info("ERROR creating stmmac ring debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002760 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002761
2762 return -ENOMEM;
2763 }
2764
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002765 /* Entry to report the DMA HW features */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002766 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
2767 priv->dbgfs_dir,
2768 dev, &stmmac_dma_cap_fops);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002769
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002770 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002771 pr_info("ERROR creating stmmac MMC debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002772 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002773
2774 return -ENOMEM;
2775 }
2776
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002777 return 0;
2778}
2779
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002780static void stmmac_exit_fs(struct net_device *dev)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002781{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002782 struct stmmac_priv *priv = netdev_priv(dev);
2783
2784 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002785}
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002786#endif /* CONFIG_DEBUG_FS */
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002787
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002788static const struct net_device_ops stmmac_netdev_ops = {
2789 .ndo_open = stmmac_open,
2790 .ndo_start_xmit = stmmac_xmit,
2791 .ndo_stop = stmmac_release,
2792 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00002793 .ndo_fix_features = stmmac_fix_features,
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002794 .ndo_set_features = stmmac_set_features,
Jiri Pirko01789342011-08-16 06:29:00 +00002795 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002796 .ndo_tx_timeout = stmmac_tx_timeout,
2797 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002798#ifdef CONFIG_NET_POLL_CONTROLLER
2799 .ndo_poll_controller = stmmac_poll_controller,
2800#endif
2801 .ndo_set_mac_address = eth_mac_addr,
2802};
2803
2804/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002805 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002806 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002807 * Description: this function is to configure the MAC device according to
2808 * some platform parameters or the HW capability register. It prepares the
2809 * driver to use either ring or chain modes and to setup either enhanced or
2810 * normal descriptors.
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002811 */
2812static int stmmac_hw_init(struct stmmac_priv *priv)
2813{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002814 struct mac_device_info *mac;
2815
2816 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002817 if (priv->plat->has_gmac) {
2818 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05002819 mac = dwmac1000_setup(priv->ioaddr,
2820 priv->plat->multicast_filter_bins,
2821 priv->plat->unicast_filter_entries);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002822 } else {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002823 mac = dwmac100_setup(priv->ioaddr);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002824 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002825 if (!mac)
2826 return -ENOMEM;
2827
2828 priv->hw = mac;
2829
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002830 /* Get and dump the chip ID */
Giuseppe CAVALLAROcffb13f2012-05-13 22:18:41 +00002831 priv->synopsys_id = stmmac_get_synopsys_id(priv);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002832
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002833 /* To use the chained or ring mode */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002834 if (chain_mode) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002835 priv->hw->mode = &chain_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002836 pr_info(" Chain mode enabled\n");
2837 priv->mode = STMMAC_CHAIN_MODE;
2838 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002839 priv->hw->mode = &ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002840 pr_info(" Ring mode enabled\n");
2841 priv->mode = STMMAC_RING_MODE;
2842 }
2843
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002844 /* Get the HW capability (new GMAC newer than 3.50a) */
2845 priv->hw_cap_support = stmmac_get_hw_features(priv);
2846 if (priv->hw_cap_support) {
2847 pr_info(" DMA HW capability register supported");
2848
2849 /* We can override some gmac/dma configuration fields: e.g.
2850 * enh_desc, tx_coe (e.g. that are passed through the
2851 * platform) with the values from the HW capability
2852 * register (if supported).
2853 */
2854 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002855 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002856
Sonic Zhangdec21652015-01-22 14:55:57 +08002857 /* TXCOE doesn't work in thresh DMA mode */
2858 if (priv->plat->force_thresh_dma_mode)
2859 priv->plat->tx_coe = 0;
2860 else
2861 priv->plat->tx_coe = priv->dma_cap.tx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002862
2863 if (priv->dma_cap.rx_coe_type2)
2864 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2865 else if (priv->dma_cap.rx_coe_type1)
2866 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2867
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002868 } else
2869 pr_info(" No HW DMA feature register supported");
2870
Byungho An61369d02013-06-28 16:35:32 +09002871 /* To use alternate (extended) or normal descriptor structures */
2872 stmmac_selec_desc_mode(priv);
2873
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002874 if (priv->plat->rx_coe) {
2875 priv->hw->rx_csum = priv->plat->rx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002876 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2877 priv->plat->rx_coe);
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002878 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002879 if (priv->plat->tx_coe)
2880 pr_info(" TX Checksum insertion supported\n");
2881
2882 if (priv->plat->pmt) {
2883 pr_info(" Wake-Up On Lan supported\n");
2884 device_set_wakeup_capable(priv->device, 1);
2885 }
2886
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002887 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002888}
2889
2890/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002891 * stmmac_dvr_probe
2892 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002893 * @plat_dat: platform data pointer
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002894 * @res: stmmac resource pointer
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002895 * Description: this is the main probe function used to
2896 * call the alloc_etherdev, allocate the priv structure.
Andy Shevchenko9afec6e2015-01-27 18:38:03 +02002897 * Return:
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002898 * returns 0 on success, otherwise errno.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002899 */
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002900int stmmac_dvr_probe(struct device *device,
2901 struct plat_stmmacenet_data *plat_dat,
2902 struct stmmac_resources *res)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002903{
2904 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002905 struct net_device *ndev = NULL;
2906 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002907
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002908 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002909 if (!ndev)
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002910 return -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002911
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002912 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002913
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002914 priv = netdev_priv(ndev);
2915 priv->device = device;
2916 priv->dev = ndev;
2917
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002918 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002919 priv->pause = pause;
2920 priv->plat = plat_dat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002921 priv->ioaddr = res->addr;
2922 priv->dev->base_addr = (unsigned long)res->addr;
2923
2924 priv->dev->irq = res->irq;
2925 priv->wol_irq = res->wol_irq;
2926 priv->lpi_irq = res->lpi_irq;
2927
2928 if (res->mac)
2929 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002930
Joachim Eastwooda7a62682015-07-17 23:48:17 +02002931 dev_set_drvdata(device, priv->dev);
Joachim Eastwood803f8fc2015-05-20 20:03:06 +02002932
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002933 /* Verify driver arguments */
2934 stmmac_verify_args();
2935
2936 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002937 * this needs to have multiple instances
2938 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002939 if ((phyaddr >= 0) && (phyaddr <= 31))
2940 priv->plat->phy_addr = phyaddr;
2941
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002942 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
2943 if (IS_ERR(priv->stmmac_clk)) {
2944 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
2945 __func__);
Kweh, Hock Leongc5bb86c2014-09-26 21:42:55 +08002946 /* If failed to obtain stmmac_clk and specific clk_csr value
2947 * is NOT passed from the platform, probe fail.
2948 */
2949 if (!priv->plat->clk_csr) {
2950 ret = PTR_ERR(priv->stmmac_clk);
2951 goto error_clk_get;
2952 } else {
2953 priv->stmmac_clk = NULL;
2954 }
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002955 }
2956 clk_prepare_enable(priv->stmmac_clk);
2957
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002958 priv->pclk = devm_clk_get(priv->device, "pclk");
2959 if (IS_ERR(priv->pclk)) {
2960 if (PTR_ERR(priv->pclk) == -EPROBE_DEFER) {
2961 ret = -EPROBE_DEFER;
2962 goto error_pclk_get;
2963 }
2964 priv->pclk = NULL;
2965 }
2966 clk_prepare_enable(priv->pclk);
2967
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002968 priv->stmmac_rst = devm_reset_control_get(priv->device,
2969 STMMAC_RESOURCE_NAME);
2970 if (IS_ERR(priv->stmmac_rst)) {
2971 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
2972 ret = -EPROBE_DEFER;
2973 goto error_hw_init;
2974 }
2975 dev_info(priv->device, "no reset control found\n");
2976 priv->stmmac_rst = NULL;
2977 }
2978 if (priv->stmmac_rst)
2979 reset_control_deassert(priv->stmmac_rst);
2980
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002981 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002982 ret = stmmac_hw_init(priv);
2983 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002984 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002985
2986 ndev->netdev_ops = &stmmac_netdev_ops;
2987
2988 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2989 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002990 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2991 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002992#ifdef STMMAC_VLAN_TAG_USED
2993 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00002994 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002995#endif
2996 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2997
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002998 if (flow_ctrl)
2999 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
3000
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00003001 /* Rx Watchdog is available in the COREs newer than the 3.40.
3002 * In some case, for example on bugged HW this feature
3003 * has to be disable and this can be done by passing the
3004 * riwt_off field from the platform.
3005 */
3006 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
3007 priv->use_riwt = 1;
3008 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
3009 }
3010
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003011 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003012
Vlad Lunguf8e96162010-11-29 22:52:52 +00003013 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00003014 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00003015
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003016 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003017 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003018 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07003019 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003020 }
3021
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00003022 /* If a specific clk_csr value is passed from the platform
3023 * this means that the CSR Clock Range selection cannot be
3024 * changed at run-time and it is fixed. Viceversa the driver'll try to
3025 * set the MDC clock dynamically according to the csr actual
3026 * clock input.
3027 */
3028 if (!priv->plat->clk_csr)
3029 stmmac_clk_csr_set(priv);
3030 else
3031 priv->clk_csr = priv->plat->clk_csr;
3032
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00003033 stmmac_check_pcs_mode(priv);
3034
Byungho An4d8f0822013-04-07 17:56:16 +00003035 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
3036 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00003037 /* MDIO bus Registration */
3038 ret = stmmac_mdio_register(ndev);
3039 if (ret < 0) {
3040 pr_debug("%s: MDIO bus (id: %d) registration failed",
3041 __func__, priv->plat->bus_id);
3042 goto error_mdio_register;
3043 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00003044 }
3045
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003046 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003047
Viresh Kumar6a81c262012-07-30 14:39:41 -07003048error_mdio_register:
Dan Carpenter34a52f32010-12-20 21:34:56 +00003049 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07003050error_netdev_register:
3051 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003052error_hw_init:
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003053 clk_disable_unprepare(priv->pclk);
3054error_pclk_get:
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003055 clk_disable_unprepare(priv->stmmac_clk);
3056error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00003057 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003058
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003059 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003060}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003061EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003062
3063/**
3064 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003065 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003066 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003067 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003068 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003069int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003070{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00003071 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003072
3073 pr_info("%s:\n\tremoving driver", __func__);
3074
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00003075 priv->hw->dma->stop_rx(priv->ioaddr);
3076 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003077
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003078 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003079 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003080 unregister_netdev(ndev);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08003081 if (priv->stmmac_rst)
3082 reset_control_assert(priv->stmmac_rst);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003083 clk_disable_unprepare(priv->pclk);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003084 clk_disable_unprepare(priv->stmmac_clk);
Bryan O'Donoghuee7434712015-04-16 17:56:03 +01003085 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
3086 priv->pcs != STMMAC_PCS_RTBI)
3087 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003088 free_netdev(ndev);
3089
3090 return 0;
3091}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003092EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003093
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003094/**
3095 * stmmac_suspend - suspend callback
3096 * @ndev: net device pointer
3097 * Description: this is the function to suspend the device and it is called
3098 * by the platform driver to stop the network queue, release the resources,
3099 * program the PMT register (for WoL), clean and release driver resources.
3100 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003101int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003102{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003103 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003104 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003105
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003106 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003107 return 0;
3108
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003109 if (priv->phydev)
3110 phy_stop(priv->phydev);
3111
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003112 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003113
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003114 netif_device_detach(ndev);
3115 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003116
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003117 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003118
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003119 /* Stop TX/RX DMA */
3120 priv->hw->dma->stop_tx(priv->ioaddr);
3121 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003122
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003123 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003124 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003125 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003126 priv->irq_wake = 1;
3127 } else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003128 stmmac_set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003129 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003130 /* Disable clock in case of PWM is off */
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003131 clk_disable(priv->pclk);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003132 clk_disable(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003133 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003134 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05003135
3136 priv->oldlink = 0;
3137 priv->speed = 0;
3138 priv->oldduplex = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003139 return 0;
3140}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003141EXPORT_SYMBOL_GPL(stmmac_suspend);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003142
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003143/**
3144 * stmmac_resume - resume callback
3145 * @ndev: net device pointer
3146 * Description: when resume this function is invoked to setup the DMA and CORE
3147 * in a usable state.
3148 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003149int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003150{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003151 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003152 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003153
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003154 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003155 return 0;
3156
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003157 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02003158
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003159 /* Power Down bit, into the PM register, is cleared
3160 * automatically as soon as a magic packet or a Wake-up frame
3161 * is received. Anyway, it's better to manually clear
3162 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003163 * from another devices (e.g. serial console).
3164 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003165 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003166 priv->hw->mac->pmt(priv->hw, 0);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003167 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003168 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003169 pinctrl_pm_select_default_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003170 /* enable the clk prevously disabled */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003171 clk_enable(priv->stmmac_clk);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003172 clk_enable(priv->pclk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003173 /* reset the phy so that it's ready */
3174 if (priv->mii)
3175 stmmac_mdio_reset(priv->mii);
3176 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003177
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003178 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003179
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01003180 priv->cur_rx = 0;
3181 priv->dirty_rx = 0;
3182 priv->dirty_tx = 0;
3183 priv->cur_tx = 0;
3184 stmmac_clear_descriptors(priv);
3185
Huacai Chenfe1319292014-12-19 22:38:18 +08003186 stmmac_hw_setup(ndev, false);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003187 stmmac_init_tx_coalesce(priv);
Giuseppe CAVALLAROac316c72015-11-26 08:35:41 +01003188 stmmac_set_rx_mode(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003189
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003190 napi_enable(&priv->napi);
3191
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003192 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003193
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003194 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003195
3196 if (priv->phydev)
3197 phy_start(priv->phydev);
3198
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003199 return 0;
3200}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003201EXPORT_SYMBOL_GPL(stmmac_resume);
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00003202
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003203#ifndef MODULE
3204static int __init stmmac_cmdline_opt(char *str)
3205{
3206 char *opt;
3207
3208 if (!str || !*str)
3209 return -EINVAL;
3210 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003211 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003212 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003213 goto err;
3214 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003215 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003216 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003217 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003218 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003219 goto err;
3220 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003221 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003222 goto err;
3223 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003224 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003225 goto err;
3226 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003227 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003228 goto err;
3229 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003230 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003231 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00003232 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003233 if (kstrtoint(opt + 10, 0, &eee_timer))
3234 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003235 } else if (!strncmp(opt, "chain_mode:", 11)) {
3236 if (kstrtoint(opt + 11, 0, &chain_mode))
3237 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003238 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003239 }
3240 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003241
3242err:
3243 pr_err("%s: ERROR broken module parameter conversion", __func__);
3244 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003245}
3246
3247__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003248#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003249
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003250static int __init stmmac_init(void)
3251{
3252#ifdef CONFIG_DEBUG_FS
3253 /* Create debugfs main directory if it doesn't exist yet */
3254 if (!stmmac_fs_dir) {
3255 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
3256
3257 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
3258 pr_err("ERROR %s, debugfs create directory failed\n",
3259 STMMAC_RESOURCE_NAME);
3260
3261 return -ENOMEM;
3262 }
3263 }
3264#endif
3265
3266 return 0;
3267}
3268
3269static void __exit stmmac_exit(void)
3270{
3271#ifdef CONFIG_DEBUG_FS
3272 debugfs_remove_recursive(stmmac_fs_dir);
3273#endif
3274}
3275
3276module_init(stmmac_init)
3277module_exit(stmmac_exit)
3278
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003279MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3280MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3281MODULE_LICENSE("GPL");