blob: b3bc1287b2a7093b113d8feca390e21f28386262 [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)
Alexandre TORGUEf748be52016-04-01 11:37:34 +020059#define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070060
61/* Module parameters */
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000062#define TX_TIMEO 5000
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070063static int watchdog = TX_TIMEO;
64module_param(watchdog, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000065MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070066
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000067static int debug = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070068module_param(debug, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000069MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070070
stephen hemminger47d1f712013-12-30 10:38:57 -080071static int phyaddr = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070072module_param(phyaddr, int, S_IRUGO);
73MODULE_PARM_DESC(phyaddr, "Physical device address");
74
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +010075#define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +010076#define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070077
78static int flow_ctrl = FLOW_OFF;
79module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
80MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
81
82static int pause = PAUSE_TIME;
83module_param(pause, int, S_IRUGO | S_IWUSR);
84MODULE_PARM_DESC(pause, "Flow Control Pause Time");
85
86#define TC_DEFAULT 64
87static int tc = TC_DEFAULT;
88module_param(tc, int, S_IRUGO | S_IWUSR);
89MODULE_PARM_DESC(tc, "DMA threshold control value");
90
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +010091#define DEFAULT_BUFSIZE 1536
92static int buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070093module_param(buf_sz, int, S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(buf_sz, "DMA buffer size");
95
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +010096#define STMMAC_RX_COPYBREAK 256
97
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070098static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
99 NETIF_MSG_LINK | NETIF_MSG_IFUP |
100 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
101
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000102#define STMMAC_DEFAULT_LPI_TIMER 1000
103static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
104module_param(eee_timer, int, S_IRUGO | S_IWUSR);
105MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200106#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000107
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000108/* By default the driver will use the ring mode to manage tx and rx descriptors
109 * but passing this value so user can force to use the chain instead of the ring
110 */
111static unsigned int chain_mode;
112module_param(chain_mode, int, S_IRUGO);
113MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
114
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700115static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700116
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +0100117#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000118static int stmmac_init_fs(struct net_device *dev);
Mathieu Olivari466c5ac2015-05-22 19:03:29 -0700119static void stmmac_exit_fs(struct net_device *dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000120#endif
121
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000122#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
123
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700124/**
125 * stmmac_verify_args - verify the driver parameters.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100126 * Description: it checks the driver parameters and set a default in case of
127 * errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700128 */
129static void stmmac_verify_args(void)
130{
131 if (unlikely(watchdog < 0))
132 watchdog = TX_TIMEO;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100133 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
134 buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700135 if (unlikely(flow_ctrl > 1))
136 flow_ctrl = FLOW_AUTO;
137 else if (likely(flow_ctrl < 0))
138 flow_ctrl = FLOW_OFF;
139 if (unlikely((pause < 0) || (pause > 0xffff)))
140 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000141 if (eee_timer < 0)
142 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700143}
144
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000145/**
146 * stmmac_clk_csr_set - dynamically set the MDC clock
147 * @priv: driver private structure
148 * Description: this is to dynamically set the MDC clock according to the csr
149 * clock input.
150 * Note:
151 * If a specific clk_csr value is passed from the platform
152 * this means that the CSR Clock Range selection cannot be
153 * changed at run-time and it is fixed (as reported in the driver
154 * documentation). Viceversa the driver will try to set the MDC
155 * clock dynamically according to the actual clock input.
156 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000157static void stmmac_clk_csr_set(struct stmmac_priv *priv)
158{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000159 u32 clk_rate;
160
161 clk_rate = clk_get_rate(priv->stmmac_clk);
162
163 /* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000164 * for all other cases except for the below mentioned ones.
165 * For values higher than the IEEE 802.3 specified frequency
166 * we can not estimate the proper divider as it is not known
167 * the frequency of clk_csr_i. So we do not change the default
168 * divider.
169 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000170 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
171 if (clk_rate < CSR_F_35M)
172 priv->clk_csr = STMMAC_CSR_20_35M;
173 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
174 priv->clk_csr = STMMAC_CSR_35_60M;
175 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
176 priv->clk_csr = STMMAC_CSR_60_100M;
177 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
178 priv->clk_csr = STMMAC_CSR_100_150M;
179 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
180 priv->clk_csr = STMMAC_CSR_150_250M;
Phil Reid19d857c2015-12-14 11:32:01 +0800181 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000182 priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000183 }
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000184}
185
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700186static void print_pkt(unsigned char *buf, int len)
187{
Andy Shevchenko424c4f72014-11-07 16:53:12 +0200188 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
189 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700190}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700191
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700192static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
193{
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100194 unsigned avail;
195
196 if (priv->dirty_tx > priv->cur_tx)
197 avail = priv->dirty_tx - priv->cur_tx - 1;
198 else
199 avail = DMA_TX_SIZE - priv->cur_tx + priv->dirty_tx - 1;
200
201 return avail;
202}
203
204static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv)
205{
206 unsigned dirty;
207
208 if (priv->dirty_rx <= priv->cur_rx)
209 dirty = priv->cur_rx - priv->dirty_rx;
210 else
211 dirty = DMA_RX_SIZE - priv->dirty_rx + priv->cur_rx;
212
213 return dirty;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700214}
215
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000216/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100217 * stmmac_hw_fix_mac_speed - callback for speed selection
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000218 * @priv: driver private structure
219 * Description: on some platforms (e.g. ST), some HW system configuraton
220 * registers have to be set according to the link speed negotiated.
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000221 */
222static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
223{
224 struct phy_device *phydev = priv->phydev;
225
226 if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000227 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000228}
229
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000230/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100231 * stmmac_enable_eee_mode - check and enter in LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000232 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100233 * Description: this function is to verify and enter in LPI mode in case of
234 * EEE.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000235 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000236static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
237{
238 /* Check and enter in LPI mode */
239 if ((priv->dirty_tx == priv->cur_tx) &&
240 (priv->tx_path_in_lpi_mode == false))
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500241 priv->hw->mac->set_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000242}
243
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000244/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100245 * stmmac_disable_eee_mode - disable and exit from LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000246 * @priv: driver private structure
247 * Description: this function is to exit and disable EEE in case of
248 * LPI state is true. This is called by the xmit.
249 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000250void stmmac_disable_eee_mode(struct stmmac_priv *priv)
251{
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500252 priv->hw->mac->reset_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000253 del_timer_sync(&priv->eee_ctrl_timer);
254 priv->tx_path_in_lpi_mode = false;
255}
256
257/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100258 * stmmac_eee_ctrl_timer - EEE TX SW timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000259 * @arg : data hook
260 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000261 * if there is no data transfer and if we are not in LPI state,
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000262 * then MAC Transmitter can be moved to LPI state.
263 */
264static void stmmac_eee_ctrl_timer(unsigned long arg)
265{
266 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
267
268 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200269 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000270}
271
272/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100273 * stmmac_eee_init - init EEE
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000274 * @priv: driver private structure
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000275 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100276 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
277 * can also manage EEE, this function enable the LPI state and start related
278 * timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000279 */
280bool stmmac_eee_init(struct stmmac_priv *priv)
281{
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100282 unsigned long flags;
Jerome Brunet6f237182018-01-03 16:46:29 +0100283 int interface = priv->plat->interface;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000284 bool ret = false;
285
Jerome Brunet6f237182018-01-03 16:46:29 +0100286 if ((interface != PHY_INTERFACE_MODE_MII) &&
287 (interface != PHY_INTERFACE_MODE_GMII) &&
288 !phy_interface_mode_is_rgmii(interface))
289 goto out;
290
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200291 /* Using PCS we cannot dial with the phy registers at this stage
292 * so we do not support extra feature like EEE.
293 */
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200294 if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
295 (priv->hw->pcs == STMMAC_PCS_TBI) ||
296 (priv->hw->pcs == STMMAC_PCS_RTBI))
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200297 goto out;
298
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000299 /* MAC core supports the EEE feature. */
300 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100301 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000302
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100303 /* Check if the PHY supports EEE */
304 if (phy_init_eee(priv->phydev, 1)) {
305 /* To manage at run-time if the EEE cannot be supported
306 * anymore (for example because the lp caps have been
307 * changed).
308 * In that case the driver disable own timers.
309 */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100310 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100311 if (priv->eee_active) {
312 pr_debug("stmmac: disable EEE\n");
313 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500314 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100315 tx_lpi_timer);
316 }
317 priv->eee_active = 0;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100318 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100319 goto out;
320 }
321 /* Activate the EEE and start timers */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100322 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200323 if (!priv->eee_active) {
324 priv->eee_active = 1;
Vaishali Thakkarccb36da2015-02-28 00:12:34 +0530325 setup_timer(&priv->eee_ctrl_timer,
326 stmmac_eee_ctrl_timer,
327 (unsigned long)priv);
328 mod_timer(&priv->eee_ctrl_timer,
329 STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000330
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500331 priv->hw->mac->set_eee_timer(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200332 STMMAC_DEFAULT_LIT_LS,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100333 tx_lpi_timer);
Giuseppe CAVALLARO71965352014-08-28 08:11:44 +0200334 }
335 /* Set HW EEE according to the speed */
336 priv->hw->mac->set_eee_pls(priv->hw, priv->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000337
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000338 ret = true;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100339 spin_unlock_irqrestore(&priv->lock, flags);
340
341 pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000342 }
343out:
344 return ret;
345}
346
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100347/* stmmac_get_tx_hwtstamp - get HW TX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000348 * @priv: driver private structure
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100349 * @p : descriptor pointer
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000350 * @skb : the socket buffer
351 * Description :
352 * This function will read timestamp from the descriptor & pass it to stack.
353 * and also perform some sanity checks.
354 */
355static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100356 struct dma_desc *p, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000357{
358 struct skb_shared_hwtstamps shhwtstamp;
359 u64 ns;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000360
361 if (!priv->hwts_tx_en)
362 return;
363
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000364 /* exit if skb doesn't support hw tstamp */
damuzi00075e43642014-01-17 23:47:59 +0800365 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000366 return;
367
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000368 /* check tx tstamp status */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100369 if (!priv->hw->desc->get_tx_timestamp_status(p)) {
370 /* get the valid tstamp */
371 ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000372
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100373 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
374 shhwtstamp.hwtstamp = ns_to_ktime(ns);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000375
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100376 netdev_info(priv->dev, "get valid TX hw timestamp %llu\n", ns);
377 /* pass tstamp to stack */
378 skb_tstamp_tx(skb, &shhwtstamp);
379 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000380
381 return;
382}
383
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100384/* stmmac_get_rx_hwtstamp - get HW RX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000385 * @priv: driver private structure
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100386 * @p : descriptor pointer
387 * @np : next descriptor pointer
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000388 * @skb : the socket buffer
389 * Description :
390 * This function will read received packet's timestamp from the descriptor
391 * and pass it to stack. It also perform some sanity checks.
392 */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100393static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
394 struct dma_desc *np, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000395{
396 struct skb_shared_hwtstamps *shhwtstamp = NULL;
397 u64 ns;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000398
399 if (!priv->hwts_rx_en)
400 return;
401
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100402 /* Check if timestamp is available */
403 if (!priv->hw->desc->get_rx_timestamp_status(p, priv->adv_ts)) {
404 /* For GMAC4, the valid timestamp is from CTX next desc. */
405 if (priv->plat->has_gmac4)
406 ns = priv->hw->desc->get_timestamp(np, priv->adv_ts);
407 else
408 ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000409
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100410 netdev_info(priv->dev, "get valid RX hw timestamp %llu\n", ns);
411 shhwtstamp = skb_hwtstamps(skb);
412 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
413 shhwtstamp->hwtstamp = ns_to_ktime(ns);
414 } else {
415 netdev_err(priv->dev, "cannot get RX hw timestamp\n");
416 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000417}
418
419/**
420 * stmmac_hwtstamp_ioctl - control hardware timestamping.
421 * @dev: device pointer.
422 * @ifr: An IOCTL specefic structure, that can contain a pointer to
423 * a proprietary structure used to pass information to the driver.
424 * Description:
425 * This function configures the MAC to enable/disable both outgoing(TX)
426 * and incoming(RX) packets time stamping based on user input.
427 * Return Value:
428 * 0 on success and an appropriate -ve integer on failure.
429 */
430static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
431{
432 struct stmmac_priv *priv = netdev_priv(dev);
433 struct hwtstamp_config config;
Arnd Bergmann0a624152015-09-30 13:26:32 +0200434 struct timespec64 now;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000435 u64 temp = 0;
436 u32 ptp_v2 = 0;
437 u32 tstamp_all = 0;
438 u32 ptp_over_ipv4_udp = 0;
439 u32 ptp_over_ipv6_udp = 0;
440 u32 ptp_over_ethernet = 0;
441 u32 snap_type_sel = 0;
442 u32 ts_master_en = 0;
443 u32 ts_event_en = 0;
444 u32 value = 0;
Phil Reid19d857c2015-12-14 11:32:01 +0800445 u32 sec_inc;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000446
447 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
448 netdev_alert(priv->dev, "No support for HW time stamping\n");
449 priv->hwts_tx_en = 0;
450 priv->hwts_rx_en = 0;
451
452 return -EOPNOTSUPP;
453 }
454
455 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000456 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000457 return -EFAULT;
458
459 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
460 __func__, config.flags, config.tx_type, config.rx_filter);
461
462 /* reserved for future extensions */
463 if (config.flags)
464 return -EINVAL;
465
Ben Hutchings5f3da322013-11-14 00:43:41 +0000466 if (config.tx_type != HWTSTAMP_TX_OFF &&
467 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000468 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000469
470 if (priv->adv_ts) {
471 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000472 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000473 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000474 config.rx_filter = HWTSTAMP_FILTER_NONE;
475 break;
476
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000477 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000478 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000479 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
480 /* take time stamp for all event messages */
Mario Molitor3f7b3c72017-06-08 22:41:02 +0200481 if (priv->plat->has_gmac4)
482 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
483 else
484 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000485
486 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
487 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
488 break;
489
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000490 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000491 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000492 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
493 /* take time stamp for SYNC messages only */
494 ts_event_en = PTP_TCR_TSEVNTENA;
495
496 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
497 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
498 break;
499
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000500 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000501 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000502 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
503 /* take time stamp for Delay_Req messages only */
504 ts_master_en = PTP_TCR_TSMSTRENA;
505 ts_event_en = PTP_TCR_TSEVNTENA;
506
507 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
508 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
509 break;
510
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000511 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000512 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000513 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
514 ptp_v2 = PTP_TCR_TSVER2ENA;
515 /* take time stamp for all event messages */
Mario Molitor3f7b3c72017-06-08 22:41:02 +0200516 if (priv->plat->has_gmac4)
517 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
518 else
519 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000520
521 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
522 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
523 break;
524
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000525 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000526 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000527 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
528 ptp_v2 = PTP_TCR_TSVER2ENA;
529 /* take time stamp for SYNC messages only */
530 ts_event_en = PTP_TCR_TSEVNTENA;
531
532 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
533 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
534 break;
535
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000536 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000537 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000538 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
539 ptp_v2 = PTP_TCR_TSVER2ENA;
540 /* take time stamp for Delay_Req messages only */
541 ts_master_en = PTP_TCR_TSMSTRENA;
542 ts_event_en = PTP_TCR_TSEVNTENA;
543
544 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
545 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
546 break;
547
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000548 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000549 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000550 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
551 ptp_v2 = PTP_TCR_TSVER2ENA;
552 /* take time stamp for all event messages */
Mario Molitor3f7b3c72017-06-08 22:41:02 +0200553 if (priv->plat->has_gmac4)
554 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
555 else
556 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000557
558 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
559 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
560 ptp_over_ethernet = PTP_TCR_TSIPENA;
561 break;
562
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000563 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000564 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000565 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
566 ptp_v2 = PTP_TCR_TSVER2ENA;
567 /* take time stamp for SYNC messages only */
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_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000576 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000577 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
578 ptp_v2 = PTP_TCR_TSVER2ENA;
579 /* take time stamp for Delay_Req messages only */
580 ts_master_en = PTP_TCR_TSMSTRENA;
581 ts_event_en = PTP_TCR_TSEVNTENA;
582
583 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
584 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
585 ptp_over_ethernet = PTP_TCR_TSIPENA;
586 break;
587
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000588 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000589 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000590 config.rx_filter = HWTSTAMP_FILTER_ALL;
591 tstamp_all = PTP_TCR_TSENALL;
592 break;
593
594 default:
595 return -ERANGE;
596 }
597 } else {
598 switch (config.rx_filter) {
599 case HWTSTAMP_FILTER_NONE:
600 config.rx_filter = HWTSTAMP_FILTER_NONE;
601 break;
602 default:
603 /* PTP v1, UDP, any kind of event packet */
604 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
605 break;
606 }
607 }
608 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000609 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000610
611 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100612 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, 0);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000613 else {
614 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000615 tstamp_all | ptp_v2 | ptp_over_ethernet |
616 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
617 ts_master_en | snap_type_sel);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100618 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, value);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000619
620 /* program Sub Second Increment reg */
Phil Reid19d857c2015-12-14 11:32:01 +0800621 sec_inc = priv->hw->ptp->config_sub_second_increment(
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100622 priv->ptpaddr, priv->clk_ptp_rate,
623 priv->plat->has_gmac4);
Phil Reid19d857c2015-12-14 11:32:01 +0800624 temp = div_u64(1000000000ULL, sec_inc);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000625
626 /* calculate default added value:
627 * formula is :
628 * addend = (2^32)/freq_div_ratio;
Phil Reid19d857c2015-12-14 11:32:01 +0800629 * where, freq_div_ratio = 1e9ns/sec_inc
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000630 */
Phil Reid19d857c2015-12-14 11:32:01 +0800631 temp = (u64)(temp << 32);
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200632 priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100633 priv->hw->ptp->config_addend(priv->ptpaddr,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000634 priv->default_addend);
635
636 /* initialize system time */
Arnd Bergmann0a624152015-09-30 13:26:32 +0200637 ktime_get_real_ts64(&now);
638
639 /* lower 32 bits of tv_sec are safe until y2106 */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100640 priv->hw->ptp->init_systime(priv->ptpaddr, (u32)now.tv_sec,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000641 now.tv_nsec);
642 }
643
644 return copy_to_user(ifr->ifr_data, &config,
645 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
646}
647
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000648/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100649 * stmmac_init_ptp - init PTP
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000650 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100651 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000652 * This is done by looking at the HW cap. register.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100653 * This function also registers the ptp driver.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000654 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000655static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000656{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000657 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
658 return -EOPNOTSUPP;
659
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200660 /* Fall-back to main clock in case of no PTP ref is passed */
661 priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
662 if (IS_ERR(priv->clk_ptp_ref)) {
663 priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
664 priv->clk_ptp_ref = NULL;
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200665 netdev_dbg(priv->dev, "PTP uses main clock\n");
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200666 } else {
667 clk_prepare_enable(priv->clk_ptp_ref);
668 priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200669 netdev_dbg(priv->dev, "PTP rate %d\n", priv->clk_ptp_rate);
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200670 }
671
Vince Bridgers7cd01392013-12-20 11:19:34 -0600672 priv->adv_ts = 0;
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200673 /* Check if adv_ts can be enabled for dwmac 4.x core */
674 if (priv->plat->has_gmac4 && priv->dma_cap.atime_stamp)
675 priv->adv_ts = 1;
676 /* Dwmac 3.x core with extend_desc can support adv_ts */
677 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
Vince Bridgers7cd01392013-12-20 11:19:34 -0600678 priv->adv_ts = 1;
679
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200680 if (priv->dma_cap.time_stamp)
681 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
Vince Bridgers7cd01392013-12-20 11:19:34 -0600682
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200683 if (priv->adv_ts)
684 netdev_info(priv->dev,
685 "IEEE 1588-2008 Advanced Timestamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000686
687 priv->hw->ptp = &stmmac_ptp;
688 priv->hwts_tx_en = 0;
689 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000690
Giuseppe CAVALLAROc30a70d2016-10-19 09:06:41 +0200691 stmmac_ptp_register(priv);
692
693 return 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000694}
695
696static void stmmac_release_ptp(struct stmmac_priv *priv)
697{
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200698 if (priv->clk_ptp_ref)
699 clk_disable_unprepare(priv->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000700 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000701}
702
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700703/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100704 * stmmac_adjust_link - adjusts the link parameters
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700705 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100706 * Description: this is the helper called by the physical abstraction layer
707 * drivers to communicate the phy link status. According the speed and duplex
708 * this driver can invoke registered glue-logic as well.
709 * It also invoke the eee initialization because it could happen when switch
710 * on different networks (that are eee capable).
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700711 */
712static void stmmac_adjust_link(struct net_device *dev)
713{
714 struct stmmac_priv *priv = netdev_priv(dev);
715 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700716 unsigned long flags;
717 int new_state = 0;
718 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
719
720 if (phydev == NULL)
721 return;
722
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700723 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000724
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700725 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000726 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700727
728 /* Now we make sure that we can be in full duplex mode.
729 * If not, we operate in half-duplex mode. */
730 if (phydev->duplex != priv->oldduplex) {
731 new_state = 1;
732 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000733 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700734 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000735 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700736 priv->oldduplex = phydev->duplex;
737 }
738 /* Flow Control operation */
739 if (phydev->pause)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500740 priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000741 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700742
743 if (phydev->speed != priv->speed) {
744 new_state = 1;
745 switch (phydev->speed) {
746 case 1000:
Alexandre TORGUEf748be52016-04-01 11:37:34 +0200747 if (likely((priv->plat->has_gmac) ||
748 (priv->plat->has_gmac4)))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000749 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000750 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700751 break;
752 case 100:
753 case 10:
Alexandre TORGUEf748be52016-04-01 11:37:34 +0200754 if (likely((priv->plat->has_gmac) ||
755 (priv->plat->has_gmac4))) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000756 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700757 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000758 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700759 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000760 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700761 }
762 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000763 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700764 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000765 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700766 break;
767 default:
768 if (netif_msg_link(priv))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000769 pr_warn("%s: Speed (%d) not 10/100\n",
770 dev->name, phydev->speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700771 break;
772 }
773
774 priv->speed = phydev->speed;
775 }
776
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000777 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700778
779 if (!priv->oldlink) {
780 new_state = 1;
781 priv->oldlink = 1;
782 }
783 } else if (priv->oldlink) {
784 new_state = 1;
785 priv->oldlink = 0;
786 priv->speed = 0;
787 priv->oldduplex = -1;
788 }
789
790 if (new_state && netif_msg_link(priv))
791 phy_print_status(phydev);
792
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100793 spin_unlock_irqrestore(&priv->lock, flags);
794
Giuseppe CAVALLARO52f95bb2016-04-05 08:46:57 +0200795 if (phydev->is_pseudo_fixed_link)
796 /* Stop PHY layer to call the hook to adjust the link in case
797 * of a switch is attached to the stmmac driver.
798 */
799 phydev->irq = PHY_IGNORE_INTERRUPT;
800 else
801 /* At this stage, init the EEE if supported.
802 * Never called in case of fixed_link.
803 */
804 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700805}
806
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000807/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100808 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000809 * @priv: driver private structure
810 * Description: this is to verify if the HW supports the PCS.
811 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
812 * configured for the TBI, RTBI, or SGMII PHY interface.
813 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000814static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
815{
816 int interface = priv->plat->interface;
817
818 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900819 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
820 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
821 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
822 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000823 pr_debug("STMMAC: PCS RGMII support enable\n");
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200824 priv->hw->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900825 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000826 pr_debug("STMMAC: PCS SGMII support enable\n");
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200827 priv->hw->pcs = STMMAC_PCS_SGMII;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000828 }
829 }
830}
831
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700832/**
833 * stmmac_init_phy - PHY initialization
834 * @dev: net device structure
835 * Description: it initializes the driver's PHY state, and attaches the PHY
836 * to the mac driver.
837 * Return value:
838 * 0 on success
839 */
840static int stmmac_init_phy(struct net_device *dev)
841{
842 struct stmmac_priv *priv = netdev_priv(dev);
843 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000844 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000845 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000846 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000847 int max_speed = priv->plat->max_speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700848 priv->oldlink = 0;
849 priv->speed = 0;
850 priv->oldduplex = -1;
851
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700852 if (priv->plat->phy_node) {
853 phydev = of_phy_connect(dev, priv->plat->phy_node,
854 &stmmac_adjust_link, 0, interface);
855 } else {
Giuseppe CAVALLAROa7657f12016-04-01 09:07:16 +0200856 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
857 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000858
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700859 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
860 priv->plat->phy_addr);
861 pr_debug("stmmac_init_phy: trying to attach to %s\n",
862 phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700863
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700864 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
865 interface);
866 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700867
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300868 if (IS_ERR_OR_NULL(phydev)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700869 pr_err("%s: Could not attach to PHY\n", dev->name);
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300870 if (!phydev)
871 return -ENODEV;
872
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700873 return PTR_ERR(phydev);
874 }
875
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000876 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000877 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000878 (interface == PHY_INTERFACE_MODE_RMII) ||
Pavel Macheka77e4ac2014-08-25 13:31:16 +0200879 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000880 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
881 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000882
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700883 /*
884 * Broken HW is sometimes missing the pull-up resistor on the
885 * MDIO line, which results in reads to non-existent devices returning
886 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
887 * device as well.
888 * Note: phydev->phy_id is the result of reading the UID PHY registers.
889 */
Mathieu Olivari27732382015-05-27 11:02:48 -0700890 if (!priv->plat->phy_node && phydev->phy_id == 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700891 phy_disconnect(phydev);
892 return -ENODEV;
893 }
Giuseppe Cavallaro8e99fc52016-02-29 14:27:39 +0100894
Florian Fainellic51e4242016-11-13 17:50:35 -0800895 /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
896 * subsequent PHY polling, make sure we force a link transition if
897 * we have a UP/DOWN/UP transition
898 */
899 if (phydev->is_pseudo_fixed_link)
900 phydev->irq = PHY_POLL;
901
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700902 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000903 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700904
905 priv->phydev = phydev;
906
907 return 0;
908}
909
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000910static void stmmac_display_rings(struct stmmac_priv *priv)
911{
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200912 void *head_rx, *head_tx;
913
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000914 if (priv->extend_desc) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200915 head_rx = (void *)priv->dma_erx;
916 head_tx = (void *)priv->dma_etx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000917 } else {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200918 head_rx = (void *)priv->dma_rx;
919 head_tx = (void *)priv->dma_tx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000920 }
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200921
922 /* Display Rx ring */
923 priv->hw->desc->display_ring(head_rx, DMA_RX_SIZE, true);
924 /* Display Tx ring */
925 priv->hw->desc->display_ring(head_tx, DMA_TX_SIZE, false);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000926}
927
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000928static int stmmac_set_bfsize(int mtu, int bufsize)
929{
930 int ret = bufsize;
931
932 if (mtu >= BUF_SIZE_4KiB)
933 ret = BUF_SIZE_8KiB;
934 else if (mtu >= BUF_SIZE_2KiB)
935 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100936 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000937 ret = BUF_SIZE_2KiB;
938 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100939 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000940
941 return ret;
942}
943
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000944/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100945 * stmmac_clear_descriptors - clear descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000946 * @priv: driver private structure
947 * Description: this function is called to clear the tx and rx descriptors
948 * in case of both basic and extended descriptors are used.
949 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000950static void stmmac_clear_descriptors(struct stmmac_priv *priv)
951{
952 int i;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000953
954 /* Clear the Rx/Tx descriptors */
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100955 for (i = 0; i < DMA_RX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000956 if (priv->extend_desc)
957 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
958 priv->use_riwt, priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100959 (i == DMA_RX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000960 else
961 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
962 priv->use_riwt, priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100963 (i == DMA_RX_SIZE - 1));
964 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000965 if (priv->extend_desc)
966 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
967 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100968 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000969 else
970 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
971 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100972 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000973}
974
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100975/**
976 * stmmac_init_rx_buffers - init the RX descriptor buffer.
977 * @priv: driver private structure
978 * @p: descriptor pointer
979 * @i: descriptor index
980 * @flags: gfp flag.
981 * Description: this function is called to allocate a receive buffer, perform
982 * the DMA mapping and init the descriptor.
983 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000984static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +0100985 int i, gfp_t flags)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000986{
987 struct sk_buff *skb;
988
Vineet Gupta4ec49a32015-05-20 12:04:40 +0530989 skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200990 if (!skb) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000991 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200992 return -ENOMEM;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000993 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000994 priv->rx_skbuff[i] = skb;
995 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
996 priv->dma_buf_sz,
997 DMA_FROM_DEVICE);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200998 if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
999 pr_err("%s: DMA mapping error\n", __func__);
1000 dev_kfree_skb_any(skb);
1001 return -EINVAL;
1002 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001003
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001004 if (priv->synopsys_id >= DWMAC_CORE_4_00)
1005 p->des0 = priv->rx_skbuff_dma[i];
1006 else
1007 p->des2 = priv->rx_skbuff_dma[i];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001008
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001009 if ((priv->hw->mode->init_desc3) &&
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001010 (priv->dma_buf_sz == BUF_SIZE_16KiB))
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001011 priv->hw->mode->init_desc3(p);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001012
1013 return 0;
1014}
1015
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001016static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
1017{
1018 if (priv->rx_skbuff[i]) {
1019 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
1020 priv->dma_buf_sz, DMA_FROM_DEVICE);
1021 dev_kfree_skb_any(priv->rx_skbuff[i]);
1022 }
1023 priv->rx_skbuff[i] = NULL;
1024}
1025
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001026/**
1027 * init_dma_desc_rings - init the RX/TX descriptor rings
1028 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001029 * @flags: gfp flag.
1030 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001031 * and allocates the socket buffers. It suppors the chained and ring
1032 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001033 */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001034static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001035{
1036 int i;
1037 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001038 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001039 int ret = -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001040
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001041 if (priv->hw->mode->set_16kib_bfsize)
1042 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001043
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001044 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001045 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001046
Vince Bridgers2618abb2014-01-20 05:39:01 -06001047 priv->dma_buf_sz = bfsize;
1048
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001049 if (netif_msg_probe(priv)) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001050 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1051 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001052
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001053 /* RX INITIALIZATION */
1054 pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
1055 }
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001056 for (i = 0; i < DMA_RX_SIZE; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001057 struct dma_desc *p;
1058 if (priv->extend_desc)
1059 p = &((priv->dma_erx + i)->basic);
1060 else
1061 p = priv->dma_rx + i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001062
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001063 ret = stmmac_init_rx_buffers(priv, p, i, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001064 if (ret)
1065 goto err_init_rx_buffers;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001066
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001067 if (netif_msg_probe(priv))
1068 pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1069 priv->rx_skbuff[i]->data,
1070 (unsigned int)priv->rx_skbuff_dma[i]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001071 }
1072 priv->cur_rx = 0;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001073 priv->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001074 buf_sz = bfsize;
1075
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001076 /* Setup the chained descriptor addresses */
1077 if (priv->mode == STMMAC_CHAIN_MODE) {
1078 if (priv->extend_desc) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001079 priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001080 DMA_RX_SIZE, 1);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001081 priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001082 DMA_TX_SIZE, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001083 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001084 priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001085 DMA_RX_SIZE, 0);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001086 priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001087 DMA_TX_SIZE, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001088 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001089 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001090
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001091 /* TX INITIALIZATION */
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001092 for (i = 0; i < DMA_TX_SIZE; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001093 struct dma_desc *p;
1094 if (priv->extend_desc)
1095 p = &((priv->dma_etx + i)->basic);
1096 else
1097 p = priv->dma_tx + i;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001098
1099 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1100 p->des0 = 0;
1101 p->des1 = 0;
1102 p->des2 = 0;
1103 p->des3 = 0;
1104 } else {
1105 p->des2 = 0;
1106 }
1107
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001108 priv->tx_skbuff_dma[i].buf = 0;
1109 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001110 priv->tx_skbuff_dma[i].len = 0;
Giuseppe Cavallaro2a6d8e12016-02-29 14:27:32 +01001111 priv->tx_skbuff_dma[i].last_segment = false;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001112 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001113 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001114
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001115 priv->dirty_tx = 0;
1116 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001117 netdev_reset_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001118
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001119 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001120
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001121 if (netif_msg_hw(priv))
1122 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001123
1124 return 0;
1125err_init_rx_buffers:
1126 while (--i >= 0)
1127 stmmac_free_rx_buffers(priv, i);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001128 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001129}
1130
1131static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1132{
1133 int i;
1134
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001135 for (i = 0; i < DMA_RX_SIZE; i++)
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001136 stmmac_free_rx_buffers(priv, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001137}
1138
1139static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1140{
1141 int i;
1142
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001143 for (i = 0; i < DMA_TX_SIZE; i++) {
damuzi00075e43642014-01-17 23:47:59 +08001144 struct dma_desc *p;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001145
damuzi00075e43642014-01-17 23:47:59 +08001146 if (priv->extend_desc)
1147 p = &((priv->dma_etx + i)->basic);
1148 else
1149 p = priv->dma_tx + i;
1150
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001151 if (priv->tx_skbuff_dma[i].buf) {
1152 if (priv->tx_skbuff_dma[i].map_as_page)
1153 dma_unmap_page(priv->device,
1154 priv->tx_skbuff_dma[i].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001155 priv->tx_skbuff_dma[i].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001156 DMA_TO_DEVICE);
1157 else
1158 dma_unmap_single(priv->device,
1159 priv->tx_skbuff_dma[i].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001160 priv->tx_skbuff_dma[i].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001161 DMA_TO_DEVICE);
damuzi00075e43642014-01-17 23:47:59 +08001162 }
1163
1164 if (priv->tx_skbuff[i] != NULL) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001165 dev_kfree_skb_any(priv->tx_skbuff[i]);
1166 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001167 priv->tx_skbuff_dma[i].buf = 0;
1168 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001169 }
1170 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001171}
1172
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001173/**
1174 * alloc_dma_desc_resources - alloc TX/RX resources.
1175 * @priv: private structure
1176 * Description: according to which descriptor can be used (extend or basic)
1177 * this function allocates the resources for TX and RX paths. In case of
1178 * reception, for example, it pre-allocated the RX socket buffer in order to
1179 * allow zero-copy mechanism.
1180 */
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001181static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1182{
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001183 int ret = -ENOMEM;
1184
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001185 priv->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE, sizeof(dma_addr_t),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001186 GFP_KERNEL);
1187 if (!priv->rx_skbuff_dma)
1188 return -ENOMEM;
1189
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001190 priv->rx_skbuff = kmalloc_array(DMA_RX_SIZE, sizeof(struct sk_buff *),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001191 GFP_KERNEL);
1192 if (!priv->rx_skbuff)
1193 goto err_rx_skbuff;
1194
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001195 priv->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001196 sizeof(*priv->tx_skbuff_dma),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001197 GFP_KERNEL);
1198 if (!priv->tx_skbuff_dma)
1199 goto err_tx_skbuff_dma;
1200
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001201 priv->tx_skbuff = kmalloc_array(DMA_TX_SIZE, sizeof(struct sk_buff *),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001202 GFP_KERNEL);
1203 if (!priv->tx_skbuff)
1204 goto err_tx_skbuff;
1205
1206 if (priv->extend_desc) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001207 priv->dma_erx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001208 sizeof(struct
1209 dma_extended_desc),
1210 &priv->dma_rx_phy,
1211 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001212 if (!priv->dma_erx)
1213 goto err_dma;
1214
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001215 priv->dma_etx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001216 sizeof(struct
1217 dma_extended_desc),
1218 &priv->dma_tx_phy,
1219 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001220 if (!priv->dma_etx) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001221 dma_free_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001222 sizeof(struct dma_extended_desc),
1223 priv->dma_erx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001224 goto err_dma;
1225 }
1226 } else {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001227 priv->dma_rx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001228 sizeof(struct dma_desc),
1229 &priv->dma_rx_phy,
1230 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001231 if (!priv->dma_rx)
1232 goto err_dma;
1233
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001234 priv->dma_tx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001235 sizeof(struct dma_desc),
1236 &priv->dma_tx_phy,
1237 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001238 if (!priv->dma_tx) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001239 dma_free_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001240 sizeof(struct dma_desc),
1241 priv->dma_rx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001242 goto err_dma;
1243 }
1244 }
1245
1246 return 0;
1247
1248err_dma:
1249 kfree(priv->tx_skbuff);
1250err_tx_skbuff:
1251 kfree(priv->tx_skbuff_dma);
1252err_tx_skbuff_dma:
1253 kfree(priv->rx_skbuff);
1254err_rx_skbuff:
1255 kfree(priv->rx_skbuff_dma);
1256 return ret;
1257}
1258
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001259static void free_dma_desc_resources(struct stmmac_priv *priv)
1260{
1261 /* Release the DMA TX/RX socket buffers */
1262 dma_free_rx_skbufs(priv);
1263 dma_free_tx_skbufs(priv);
1264
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001265 /* Free DMA regions of consistent memory previously allocated */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001266 if (!priv->extend_desc) {
1267 dma_free_coherent(priv->device,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001268 DMA_TX_SIZE * sizeof(struct dma_desc),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001269 priv->dma_tx, priv->dma_tx_phy);
1270 dma_free_coherent(priv->device,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001271 DMA_RX_SIZE * sizeof(struct dma_desc),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001272 priv->dma_rx, priv->dma_rx_phy);
1273 } else {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001274 dma_free_coherent(priv->device, DMA_TX_SIZE *
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001275 sizeof(struct dma_extended_desc),
1276 priv->dma_etx, priv->dma_tx_phy);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001277 dma_free_coherent(priv->device, DMA_RX_SIZE *
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001278 sizeof(struct dma_extended_desc),
1279 priv->dma_erx, priv->dma_rx_phy);
1280 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001281 kfree(priv->rx_skbuff_dma);
1282 kfree(priv->rx_skbuff);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001283 kfree(priv->tx_skbuff_dma);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001284 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001285}
1286
1287/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001288 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001289 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001290 * Description: it is used for configuring the DMA operation mode register in
1291 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001292 */
1293static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1294{
Vince Bridgersf88203a2015-04-15 11:17:42 -05001295 int rxfifosz = priv->plat->rx_fifo_size;
1296
Sonic Zhange2a240c2013-08-28 18:55:39 +08001297 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001298 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc, rxfifosz);
Sonic Zhange2a240c2013-08-28 18:55:39 +08001299 else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001300 /*
1301 * In case of GMAC, SF mode can be enabled
1302 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001303 * 1) TX COE if actually supported
1304 * 2) There is no bugged Jumbo frame support
1305 * that needs to not insert csum in the TDES.
1306 */
Vince Bridgersf88203a2015-04-15 11:17:42 -05001307 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE,
1308 rxfifosz);
Sonic Zhangb2dec112015-01-30 13:49:32 +08001309 priv->xstats.threshold = SF_DMA_MODE;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001310 } else
Vince Bridgersf88203a2015-04-15 11:17:42 -05001311 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE,
1312 rxfifosz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001313}
1314
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001315/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001316 * stmmac_tx_clean - to manage the transmission completion
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001317 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001318 * Description: it reclaims the transmit resources after transmission completes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001319 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001320static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001321{
Beniamino Galvani38979572015-01-21 19:07:27 +01001322 unsigned int bytes_compl = 0, pkts_compl = 0;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001323 unsigned int entry = priv->dirty_tx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001324
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001325 spin_lock(&priv->tx_lock);
1326
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001327 priv->xstats.tx_clean++;
1328
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001329 while (entry != priv->cur_tx) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001330 struct sk_buff *skb = priv->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001331 struct dma_desc *p;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001332 int status;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001333
1334 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001335 p = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001336 else
1337 p = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001338
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001339 status = priv->hw->desc->tx_status(&priv->dev->stats,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001340 &priv->xstats, p,
1341 priv->ioaddr);
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001342 /* Check if the descriptor is owned by the DMA */
1343 if (unlikely(status & tx_dma_own))
1344 break;
1345
Niklas Cassel82aad322018-02-26 22:47:08 +01001346 /* Make sure descriptor fields are read after reading
1347 * the own bit.
1348 */
1349 dma_rmb();
1350
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001351 /* Just consider the last segment and ...*/
1352 if (likely(!(status & tx_not_ls))) {
1353 /* ... verify the status error condition */
1354 if (unlikely(status & tx_err)) {
1355 priv->dev->stats.tx_errors++;
1356 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001357 priv->dev->stats.tx_packets++;
1358 priv->xstats.tx_pkt_n++;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001359 }
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001360 stmmac_get_tx_hwtstamp(priv, p, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001361 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001362
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001363 if (likely(priv->tx_skbuff_dma[entry].buf)) {
1364 if (priv->tx_skbuff_dma[entry].map_as_page)
1365 dma_unmap_page(priv->device,
1366 priv->tx_skbuff_dma[entry].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001367 priv->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001368 DMA_TO_DEVICE);
1369 else
1370 dma_unmap_single(priv->device,
1371 priv->tx_skbuff_dma[entry].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001372 priv->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001373 DMA_TO_DEVICE);
1374 priv->tx_skbuff_dma[entry].buf = 0;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001375 priv->tx_skbuff_dma[entry].len = 0;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001376 priv->tx_skbuff_dma[entry].map_as_page = false;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001377 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001378
1379 if (priv->hw->mode->clean_desc3)
1380 priv->hw->mode->clean_desc3(priv, p);
1381
Giuseppe Cavallaro2a6d8e12016-02-29 14:27:32 +01001382 priv->tx_skbuff_dma[entry].last_segment = false;
Giuseppe Cavallaro96951362016-02-29 14:27:33 +01001383 priv->tx_skbuff_dma[entry].is_jumbo = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001384
1385 if (likely(skb != NULL)) {
Beniamino Galvani38979572015-01-21 19:07:27 +01001386 pkts_compl++;
1387 bytes_compl += skb->len;
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001388 dev_consume_skb_any(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001389 priv->tx_skbuff[entry] = NULL;
1390 }
1391
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001392 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001393
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001394 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001395 }
Giuseppe Cavallarofbc80822016-02-29 14:27:37 +01001396 priv->dirty_tx = entry;
Beniamino Galvani38979572015-01-21 19:07:27 +01001397
1398 netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
1399
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001400 if (unlikely(netif_queue_stopped(priv->dev) &&
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001401 stmmac_tx_avail(priv) > STMMAC_TX_THRESH)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001402 netif_tx_lock(priv->dev);
1403 if (netif_queue_stopped(priv->dev) &&
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001404 stmmac_tx_avail(priv) > STMMAC_TX_THRESH) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001405 if (netif_msg_tx_done(priv))
1406 pr_debug("%s: restart transmit\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001407 netif_wake_queue(priv->dev);
1408 }
1409 netif_tx_unlock(priv->dev);
1410 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001411
1412 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1413 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001414 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001415 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001416 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001417}
1418
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001419static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001420{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001421 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001422}
1423
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001424static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001425{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001426 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001427}
1428
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001429/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001430 * stmmac_tx_err - to manage the tx error
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001431 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001432 * Description: it cleans the descriptors and restarts the transmission
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001433 * in case of transmission errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001434 */
1435static void stmmac_tx_err(struct stmmac_priv *priv)
1436{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001437 int i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001438 netif_stop_queue(priv->dev);
1439
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001440 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001441 dma_free_tx_skbufs(priv);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001442 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001443 if (priv->extend_desc)
1444 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1445 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001446 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001447 else
1448 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1449 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001450 (i == DMA_TX_SIZE - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001451 priv->dirty_tx = 0;
1452 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001453 netdev_reset_queue(priv->dev);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001454 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001455
1456 priv->dev->stats.tx_errors++;
1457 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001458}
1459
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001460/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001461 * stmmac_dma_interrupt - DMA ISR
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001462 * @priv: driver private structure
1463 * Description: this is the DMA ISR. It is called by the main ISR.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001464 * It calls the dwmac dma routine and schedule poll method in case of some
1465 * work can be done.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001466 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001467static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001468{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001469 int status;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001470 int rxfifosz = priv->plat->rx_fifo_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001471
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001472 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001473 if (likely((status & handle_rx)) || (status & handle_tx)) {
1474 if (likely(napi_schedule_prep(&priv->napi))) {
1475 stmmac_disable_dma_irq(priv);
1476 __napi_schedule(&priv->napi);
1477 }
1478 }
1479 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001480 /* Try to bump up the dma threshold on this failure */
Sonic Zhangb2dec112015-01-30 13:49:32 +08001481 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
1482 (tc <= 256)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001483 tc += 64;
Sonic Zhangc405abe2015-01-22 14:55:56 +08001484 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001485 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc,
1486 rxfifosz);
Sonic Zhangc405abe2015-01-22 14:55:56 +08001487 else
1488 priv->hw->dma->dma_mode(priv->ioaddr, tc,
Vince Bridgersf88203a2015-04-15 11:17:42 -05001489 SF_DMA_MODE, rxfifosz);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001490 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001491 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001492 } else if (unlikely(status == tx_hard_error))
1493 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001494}
1495
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001496/**
1497 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1498 * @priv: driver private structure
1499 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1500 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001501static void stmmac_mmc_setup(struct stmmac_priv *priv)
1502{
1503 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001504 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001505
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001506 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1507 priv->ptpaddr = priv->ioaddr + PTP_GMAC4_OFFSET;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001508 priv->mmcaddr = priv->ioaddr + MMC_GMAC4_OFFSET;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001509 } else {
1510 priv->ptpaddr = priv->ioaddr + PTP_GMAC3_X_OFFSET;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001511 priv->mmcaddr = priv->ioaddr + MMC_GMAC3_X_OFFSET;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001512 }
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001513
1514 dwmac_mmc_intr_all_mask(priv->mmcaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001515
1516 if (priv->dma_cap.rmon) {
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001517 dwmac_mmc_ctrl(priv->mmcaddr, mode);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001518 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1519 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +00001520 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001521}
1522
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001523/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001524 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001525 * @priv: driver private structure
1526 * Description: select the Enhanced/Alternate or Normal descriptors.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001527 * In case of Enhanced/Alternate, it checks if the extended descriptors are
1528 * supported by the HW capability register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001529 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001530static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1531{
1532 if (priv->plat->enh_desc) {
1533 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001534
1535 /* GMAC older than 3.50 has no extended descriptors */
1536 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1537 pr_info("\tEnabled extended descriptors\n");
1538 priv->extend_desc = 1;
1539 } else
1540 pr_warn("Extended descriptors not supported\n");
1541
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001542 priv->hw->desc = &enh_desc_ops;
1543 } else {
1544 pr_info(" Normal descriptors\n");
1545 priv->hw->desc = &ndesc_ops;
1546 }
1547}
1548
1549/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001550 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001551 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001552 * Description:
1553 * new GMAC chip generations have a new register to indicate the
1554 * presence of the optional feature/functions.
1555 * This can be also used to override the value passed through the
1556 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001557 */
1558static int stmmac_get_hw_features(struct stmmac_priv *priv)
1559{
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001560 u32 ret = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001561
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001562 if (priv->hw->dma->get_hw_feature) {
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001563 priv->hw->dma->get_hw_feature(priv->ioaddr,
1564 &priv->dma_cap);
1565 ret = 1;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001566 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001567
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001568 return ret;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001569}
1570
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001571/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001572 * stmmac_check_ether_addr - check if the MAC addr is valid
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001573 * @priv: driver private structure
1574 * Description:
1575 * it is to verify if the MAC address is valid, in case of failures it
1576 * generates a random MAC address
1577 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001578static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1579{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001580 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001581 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001582 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001583 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001584 eth_hw_addr_random(priv->dev);
Hans de Goedec88460b2014-01-26 15:50:44 +01001585 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1586 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001587 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001588}
1589
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001590/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001591 * stmmac_init_dma_engine - DMA init.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001592 * @priv: driver private structure
1593 * Description:
1594 * It inits the DMA invoking the specific MAC/GMAC callback.
1595 * Some DMA parameters can be passed from the platform;
1596 * in case of these are not passed a default is kept for the MAC or GMAC.
1597 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001598static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1599{
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001600 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, aal = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001601 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001602 int atds = 0;
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001603 int ret = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001604
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001605 if (priv->plat->dma_cfg) {
1606 pbl = priv->plat->dma_cfg->pbl;
1607 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001608 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001609 aal = priv->plat->dma_cfg->aal;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001610 }
1611
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001612 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1613 atds = 1;
1614
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001615 ret = priv->hw->dma->reset(priv->ioaddr);
1616 if (ret) {
1617 dev_err(priv->device, "Failed to reset the dma\n");
1618 return ret;
1619 }
1620
1621 priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001622 aal, priv->dma_tx_phy, priv->dma_rx_phy, atds);
1623
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001624 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1625 priv->rx_tail_addr = priv->dma_rx_phy +
1626 (DMA_RX_SIZE * sizeof(struct dma_desc));
1627 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr, priv->rx_tail_addr,
1628 STMMAC_CHAN0);
1629
1630 priv->tx_tail_addr = priv->dma_tx_phy +
1631 (DMA_TX_SIZE * sizeof(struct dma_desc));
1632 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
1633 STMMAC_CHAN0);
1634 }
1635
1636 if (priv->plat->axi && priv->hw->dma->axi)
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001637 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
1638
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001639 return ret;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001640}
1641
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001642/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001643 * stmmac_tx_timer - mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001644 * @data: data pointer
1645 * Description:
1646 * This is the timer handler to directly invoke the stmmac_tx_clean.
1647 */
1648static void stmmac_tx_timer(unsigned long data)
1649{
1650 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1651
1652 stmmac_tx_clean(priv);
1653}
1654
1655/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001656 * stmmac_init_tx_coalesce - init tx mitigation options.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001657 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001658 * Description:
1659 * This inits the transmit coalesce parameters: i.e. timer rate,
1660 * timer handler and default threshold used for enabling the
1661 * interrupt on completion bit.
1662 */
1663static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1664{
1665 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1666 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1667 init_timer(&priv->txtimer);
1668 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1669 priv->txtimer.data = (unsigned long)priv;
1670 priv->txtimer.function = stmmac_tx_timer;
1671 add_timer(&priv->txtimer);
1672}
1673
1674/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001675 * stmmac_hw_setup - setup mac in a usable state.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001676 * @dev : pointer to the device structure.
1677 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001678 * this is the main function to setup the HW in a usable state because the
1679 * dma engine is reset, the core registers are configured (e.g. AXI,
1680 * Checksum features, timers). The DMA is ready to start receiving and
1681 * transmitting.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001682 * Return value:
1683 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1684 * file on failure.
1685 */
Huacai Chenfe1319292014-12-19 22:38:18 +08001686static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001687{
1688 struct stmmac_priv *priv = netdev_priv(dev);
1689 int ret;
1690
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001691 /* DMA initialization and SW reset */
1692 ret = stmmac_init_dma_engine(priv);
1693 if (ret < 0) {
1694 pr_err("%s: DMA engine initialization failed\n", __func__);
1695 return ret;
1696 }
1697
1698 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001699 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001700
1701 /* If required, perform hw setup of the bus. */
1702 if (priv->plat->bus_setup)
1703 priv->plat->bus_setup(priv->ioaddr);
1704
Giuseppe CAVALLARO02e57b92016-06-24 15:16:26 +02001705 /* PS and related bits will be programmed according to the speed */
1706 if (priv->hw->pcs) {
1707 int speed = priv->plat->mac_port_sel_speed;
1708
1709 if ((speed == SPEED_10) || (speed == SPEED_100) ||
1710 (speed == SPEED_1000)) {
1711 priv->hw->ps = speed;
1712 } else {
1713 dev_warn(priv->device, "invalid port speed\n");
1714 priv->hw->ps = 0;
1715 }
1716 }
1717
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001718 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001719 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001720
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001721 ret = priv->hw->mac->rx_ipc(priv->hw);
1722 if (!ret) {
1723 pr_warn(" RX IPC Checksum Offload disabled\n");
1724 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02001725 priv->hw->rx_csum = 0;
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001726 }
1727
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001728 /* Enable the MAC Rx/Tx */
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001729 if (priv->synopsys_id >= DWMAC_CORE_4_00)
1730 stmmac_dwmac4_set_mac(priv->ioaddr, true);
1731 else
1732 stmmac_set_mac(priv->ioaddr, true);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001733
1734 /* Set the HW DMA mode and the COE */
1735 stmmac_dma_operation_mode(priv);
1736
1737 stmmac_mmc_setup(priv);
1738
Huacai Chenfe1319292014-12-19 22:38:18 +08001739 if (init_ptp) {
1740 ret = stmmac_init_ptp(priv);
Giuseppe CAVALLARO70866052016-10-12 15:42:04 +02001741 if (ret)
Giuseppe CAVALLAROc30a70d2016-10-19 09:06:41 +02001742 netdev_warn(priv->dev, "fail to init PTP.\n");
Huacai Chenfe1319292014-12-19 22:38:18 +08001743 }
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001744
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001745#ifdef CONFIG_DEBUG_FS
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001746 ret = stmmac_init_fs(dev);
1747 if (ret < 0)
1748 pr_warn("%s: failed debugFS registration\n", __func__);
1749#endif
1750 /* Start the ball rolling... */
1751 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1752 priv->hw->dma->start_tx(priv->ioaddr);
1753 priv->hw->dma->start_rx(priv->ioaddr);
1754
1755 /* Dump DMA/MAC registers */
1756 if (netif_msg_hw(priv)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001757 priv->hw->mac->dump_regs(priv->hw);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001758 priv->hw->dma->dump_regs(priv->ioaddr);
1759 }
1760 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1761
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001762 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1763 priv->rx_riwt = MAX_DMA_RIWT;
1764 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1765 }
1766
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02001767 if (priv->hw->pcs && priv->hw->mac->pcs_ctrl_ane)
Giuseppe CAVALLARO02e57b92016-06-24 15:16:26 +02001768 priv->hw->mac->pcs_ctrl_ane(priv->hw, 1, priv->hw->ps, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001769
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001770 /* set TX ring length */
1771 if (priv->hw->dma->set_tx_ring_len)
1772 priv->hw->dma->set_tx_ring_len(priv->ioaddr,
1773 (DMA_TX_SIZE - 1));
1774 /* set RX ring length */
1775 if (priv->hw->dma->set_rx_ring_len)
1776 priv->hw->dma->set_rx_ring_len(priv->ioaddr,
1777 (DMA_RX_SIZE - 1));
1778 /* Enable TSO */
1779 if (priv->tso)
1780 priv->hw->dma->enable_tso(priv->ioaddr, 1, STMMAC_CHAN0);
1781
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001782 return 0;
1783}
1784
1785/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001786 * stmmac_open - open entry point of the driver
1787 * @dev : pointer to the device structure.
1788 * Description:
1789 * This function is the open entry point of the driver.
1790 * Return value:
1791 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1792 * file on failure.
1793 */
1794static int stmmac_open(struct net_device *dev)
1795{
1796 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001797 int ret;
1798
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001799 stmmac_check_ether_addr(priv);
1800
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02001801 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
1802 priv->hw->pcs != STMMAC_PCS_TBI &&
1803 priv->hw->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001804 ret = stmmac_init_phy(dev);
1805 if (ret) {
1806 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1807 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02001808 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001809 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001810 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001811
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001812 /* Extra statistics */
1813 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1814 priv->xstats.threshold = tc;
1815
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001816 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01001817 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
Lars Persson30985e32017-12-01 11:12:44 +01001818 priv->mss = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001819
Tobias Klauser7262b7b2014-02-22 13:09:03 +01001820 ret = alloc_dma_desc_resources(priv);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001821 if (ret < 0) {
1822 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1823 goto dma_desc_error;
1824 }
1825
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001826 ret = init_dma_desc_rings(dev, GFP_KERNEL);
1827 if (ret < 0) {
1828 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1829 goto init_error;
1830 }
1831
Huacai Chenfe1319292014-12-19 22:38:18 +08001832 ret = stmmac_hw_setup(dev, true);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001833 if (ret < 0) {
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001834 pr_err("%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001835 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001836 }
1837
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001838 stmmac_init_tx_coalesce(priv);
1839
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001840 if (priv->phydev)
1841 phy_start(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001842
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001843 /* Request the IRQ lines */
1844 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001845 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001846 if (unlikely(ret < 0)) {
1847 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1848 __func__, dev->irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001849 goto init_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001850 }
1851
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001852 /* Request the Wake IRQ in case of another line is used for WoL */
1853 if (priv->wol_irq != dev->irq) {
1854 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1855 IRQF_SHARED, dev->name, dev);
1856 if (unlikely(ret < 0)) {
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001857 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1858 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001859 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001860 }
1861 }
1862
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001863 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001864 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001865 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1866 dev->name, dev);
1867 if (unlikely(ret < 0)) {
1868 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1869 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001870 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001871 }
1872 }
1873
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001874 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001875 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001876
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001877 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001878
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001879lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001880 if (priv->wol_irq != dev->irq)
1881 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001882wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001883 free_irq(dev->irq, dev);
1884
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001885init_error:
1886 free_dma_desc_resources(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001887dma_desc_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001888 if (priv->phydev)
1889 phy_disconnect(priv->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001890
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001891 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001892}
1893
1894/**
1895 * stmmac_release - close entry point of the driver
1896 * @dev : device pointer.
1897 * Description:
1898 * This is the stop entry point of the driver.
1899 */
1900static int stmmac_release(struct net_device *dev)
1901{
1902 struct stmmac_priv *priv = netdev_priv(dev);
1903
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001904 if (priv->eee_enabled)
1905 del_timer_sync(&priv->eee_ctrl_timer);
1906
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001907 /* Stop and disconnect the PHY */
1908 if (priv->phydev) {
1909 phy_stop(priv->phydev);
1910 phy_disconnect(priv->phydev);
1911 priv->phydev = NULL;
1912 }
1913
1914 netif_stop_queue(dev);
1915
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001916 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001917
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001918 del_timer_sync(&priv->txtimer);
1919
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001920 /* Free the IRQ lines */
1921 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001922 if (priv->wol_irq != dev->irq)
1923 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001924 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001925 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001926
1927 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001928 priv->hw->dma->stop_tx(priv->ioaddr);
1929 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001930
1931 /* Release and free the Rx/Tx resources */
1932 free_dma_desc_resources(priv);
1933
avisconti19449bf2010-10-25 18:58:14 +00001934 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001935 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001936
1937 netif_carrier_off(dev);
1938
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001939#ifdef CONFIG_DEBUG_FS
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07001940 stmmac_exit_fs(dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001941#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001942
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00001943 stmmac_release_ptp(priv);
1944
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001945 return 0;
1946}
1947
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001948/**
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001949 * stmmac_tso_allocator - close entry point of the driver
1950 * @priv: driver private structure
1951 * @des: buffer start address
1952 * @total_len: total length to fill in descriptors
1953 * @last_segmant: condition for the last descriptor
1954 * Description:
1955 * This function fills descriptor and request new descriptors according to
1956 * buffer length to fill
1957 */
1958static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
1959 int total_len, bool last_segment)
1960{
1961 struct dma_desc *desc;
1962 int tmp_len;
1963 u32 buff_size;
1964
1965 tmp_len = total_len;
1966
1967 while (tmp_len > 0) {
1968 priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
1969 desc = priv->dma_tx + priv->cur_tx;
1970
1971 desc->des0 = des + (total_len - tmp_len);
1972 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
1973 TSO_MAX_BUFF_SIZE : tmp_len;
1974
1975 priv->hw->desc->prepare_tso_tx_desc(desc, 0, buff_size,
1976 0, 1,
Niklas Cassel3dd4daf2017-06-06 09:25:00 +02001977 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001978 0, 0);
1979
1980 tmp_len -= TSO_MAX_BUFF_SIZE;
1981 }
1982}
1983
1984/**
1985 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
1986 * @skb : the socket buffer
1987 * @dev : device pointer
1988 * Description: this is the transmit function that is called on TSO frames
1989 * (support available on GMAC4 and newer chips).
1990 * Diagram below show the ring programming in case of TSO frames:
1991 *
1992 * First Descriptor
1993 * --------
1994 * | DES0 |---> buffer1 = L2/L3/L4 header
1995 * | DES1 |---> TCP Payload (can continue on next descr...)
1996 * | DES2 |---> buffer 1 and 2 len
1997 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
1998 * --------
1999 * |
2000 * ...
2001 * |
2002 * --------
2003 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
2004 * | DES1 | --|
2005 * | DES2 | --> buffer 1 and 2 len
2006 * | DES3 |
2007 * --------
2008 *
2009 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
2010 */
2011static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
2012{
2013 u32 pay_len, mss;
2014 int tmp_pay_len = 0;
2015 struct stmmac_priv *priv = netdev_priv(dev);
2016 int nfrags = skb_shinfo(skb)->nr_frags;
2017 unsigned int first_entry, des;
2018 struct dma_desc *desc, *first, *mss_desc = NULL;
2019 u8 proto_hdr_len;
2020 int i;
2021
2022 spin_lock(&priv->tx_lock);
2023
2024 /* Compute header lengths */
2025 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2026
2027 /* Desc availability based on threshold should be enough safe */
2028 if (unlikely(stmmac_tx_avail(priv) <
2029 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
2030 if (!netif_queue_stopped(dev)) {
2031 netif_stop_queue(dev);
2032 /* This is a hard error, log it. */
2033 pr_err("%s: Tx Ring full when queue awake\n", __func__);
2034 }
2035 spin_unlock(&priv->tx_lock);
2036 return NETDEV_TX_BUSY;
2037 }
2038
2039 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
2040
2041 mss = skb_shinfo(skb)->gso_size;
2042
2043 /* set new MSS value if needed */
2044 if (mss != priv->mss) {
2045 mss_desc = priv->dma_tx + priv->cur_tx;
2046 priv->hw->desc->set_mss(mss_desc, mss);
2047 priv->mss = mss;
2048 priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
2049 }
2050
2051 if (netif_msg_tx_queued(priv)) {
2052 pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
2053 __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
2054 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
2055 skb->data_len);
2056 }
2057
2058 first_entry = priv->cur_tx;
2059
2060 desc = priv->dma_tx + first_entry;
2061 first = desc;
2062
2063 /* first descriptor: fill Headers on Buf1 */
2064 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
2065 DMA_TO_DEVICE);
2066 if (dma_mapping_error(priv->device, des))
2067 goto dma_map_err;
2068
2069 priv->tx_skbuff_dma[first_entry].buf = des;
2070 priv->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
2071 priv->tx_skbuff[first_entry] = skb;
2072
2073 first->des0 = des;
2074
2075 /* Fill start of payload in buff2 of first descriptor */
2076 if (pay_len)
2077 first->des1 = des + proto_hdr_len;
2078
2079 /* If needed take extra descriptors to fill the remaining payload */
2080 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
2081
2082 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0));
2083
2084 /* Prepare fragments */
2085 for (i = 0; i < nfrags; i++) {
2086 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2087
2088 des = skb_frag_dma_map(priv->device, frag, 0,
2089 skb_frag_size(frag),
2090 DMA_TO_DEVICE);
2091
2092 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
2093 (i == nfrags - 1));
2094
2095 priv->tx_skbuff_dma[priv->cur_tx].buf = des;
2096 priv->tx_skbuff_dma[priv->cur_tx].len = skb_frag_size(frag);
2097 priv->tx_skbuff[priv->cur_tx] = NULL;
2098 priv->tx_skbuff_dma[priv->cur_tx].map_as_page = true;
2099 }
2100
2101 priv->tx_skbuff_dma[priv->cur_tx].last_segment = true;
2102
2103 priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
2104
2105 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
2106 if (netif_msg_hw(priv))
2107 pr_debug("%s: stop transmitted packets\n", __func__);
2108 netif_stop_queue(dev);
2109 }
2110
2111 dev->stats.tx_bytes += skb->len;
2112 priv->xstats.tx_tso_frames++;
2113 priv->xstats.tx_tso_nfrags += nfrags;
2114
2115 /* Manage tx mitigation */
2116 priv->tx_count_frames += nfrags + 1;
2117 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
2118 mod_timer(&priv->txtimer,
2119 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2120 } else {
2121 priv->tx_count_frames = 0;
2122 priv->hw->desc->set_tx_ic(desc);
2123 priv->xstats.tx_set_ic_bit++;
2124 }
2125
2126 if (!priv->hwts_tx_en)
2127 skb_tx_timestamp(skb);
2128
2129 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2130 priv->hwts_tx_en)) {
2131 /* declare that device is doing timestamping */
2132 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2133 priv->hw->desc->enable_tx_timestamp(first);
2134 }
2135
2136 /* Complete the first descriptor before granting the DMA */
2137 priv->hw->desc->prepare_tso_tx_desc(first, 1,
2138 proto_hdr_len,
2139 pay_len,
2140 1, priv->tx_skbuff_dma[first_entry].last_segment,
2141 tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
2142
2143 /* If context desc is used to change MSS */
Niklas Cassel5b3b32d2018-02-26 22:47:06 +01002144 if (mss_desc) {
2145 /* Make sure that first descriptor has been completely
2146 * written, including its own bit. This is because MSS is
2147 * actually before first descriptor, so we need to make
2148 * sure that MSS's own bit is the last thing written.
2149 */
2150 dma_wmb();
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002151 priv->hw->desc->set_tx_owner(mss_desc);
Niklas Cassel5b3b32d2018-02-26 22:47:06 +01002152 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002153
2154 /* The own bit must be the latest setting done when prepare the
2155 * descriptor and then barrier is needed to make sure that
2156 * all is coherent before granting the DMA engine.
2157 */
2158 smp_wmb();
2159
2160 if (netif_msg_pktdata(priv)) {
2161 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
2162 __func__, priv->cur_tx, priv->dirty_tx, first_entry,
2163 priv->cur_tx, first, nfrags);
2164
2165 priv->hw->desc->display_ring((void *)priv->dma_tx, DMA_TX_SIZE,
2166 0);
2167
2168 pr_info(">>> frame to be transmitted: ");
2169 print_pkt(skb->data, skb_headlen(skb));
2170 }
2171
2172 netdev_sent_queue(dev, skb->len);
2173
2174 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
2175 STMMAC_CHAN0);
2176
2177 spin_unlock(&priv->tx_lock);
2178 return NETDEV_TX_OK;
2179
2180dma_map_err:
2181 spin_unlock(&priv->tx_lock);
2182 dev_err(priv->device, "Tx dma map failed\n");
2183 dev_kfree_skb(skb);
2184 priv->dev->stats.tx_dropped++;
2185 return NETDEV_TX_OK;
2186}
2187
2188/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002189 * stmmac_xmit - Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002190 * @skb : the socket buffer
2191 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002192 * Description : this is the tx entry point of the driver.
2193 * It programs the chain or the ring and supports oversized frames
2194 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002195 */
2196static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
2197{
2198 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002199 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002200 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002201 int nfrags = skb_shinfo(skb)->nr_frags;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002202 unsigned int entry, first_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002203 struct dma_desc *desc, *first;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002204 unsigned int enh_desc;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002205 unsigned int des;
2206
2207 /* Manage oversized TCP frames for GMAC4 device */
2208 if (skb_is_gso(skb) && priv->tso) {
2209 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2210 return stmmac_tso_xmit(skb, dev);
2211 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002212
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01002213 spin_lock(&priv->tx_lock);
2214
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002215 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01002216 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002217 if (!netif_queue_stopped(dev)) {
2218 netif_stop_queue(dev);
2219 /* This is a hard error, log it. */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002220 pr_err("%s: Tx Ring full when queue awake\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002221 }
2222 return NETDEV_TX_BUSY;
2223 }
2224
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002225 if (priv->tx_path_in_lpi_mode)
2226 stmmac_disable_eee_mode(priv);
2227
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002228 entry = priv->cur_tx;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002229 first_entry = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002230
Michał Mirosław5e982f32011-04-09 02:46:55 +00002231 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002232
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002233 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002234 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002235 else
2236 desc = priv->dma_tx + entry;
2237
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002238 first = desc;
2239
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002240 priv->tx_skbuff[first_entry] = skb;
2241
2242 enh_desc = priv->plat->enh_desc;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002243 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002244 if (enh_desc)
2245 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
2246
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002247 if (unlikely(is_jumbo) && likely(priv->synopsys_id <
2248 DWMAC_CORE_4_00)) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002249 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002250 if (unlikely(entry < 0))
2251 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002252 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002253
2254 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002255 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2256 int len = skb_frag_size(frag);
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01002257 bool last_segment = (i == (nfrags - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002258
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002259 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2260
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002261 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002262 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002263 else
2264 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002265
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002266 des = skb_frag_dma_map(priv->device, frag, 0, len,
2267 DMA_TO_DEVICE);
2268 if (dma_mapping_error(priv->device, des))
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002269 goto dma_map_err; /* should reuse desc w/o issues */
2270
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002271 priv->tx_skbuff[entry] = NULL;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002272
2273 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
2274 desc->des0 = des;
2275 priv->tx_skbuff_dma[entry].buf = desc->des0;
2276 } else {
2277 desc->des2 = des;
2278 priv->tx_skbuff_dma[entry].buf = desc->des2;
2279 }
2280
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002281 priv->tx_skbuff_dma[entry].map_as_page = true;
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01002282 priv->tx_skbuff_dma[entry].len = len;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002283 priv->tx_skbuff_dma[entry].last_segment = last_segment;
2284
2285 /* Prepare the descriptor and set the own bit too */
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002286 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01002287 priv->mode, 1, last_segment);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002288 }
2289
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002290 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2291
2292 priv->cur_tx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002293
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002294 if (netif_msg_pktdata(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002295 void *tx_head;
2296
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002297 pr_debug("%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
2298 __func__, priv->cur_tx, priv->dirty_tx, first_entry,
2299 entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002300
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002301 if (priv->extend_desc)
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002302 tx_head = (void *)priv->dma_etx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002303 else
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002304 tx_head = (void *)priv->dma_tx;
2305
2306 priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002307
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002308 pr_debug(">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002309 print_pkt(skb->data, skb->len);
2310 }
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002311
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002312 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002313 if (netif_msg_hw(priv))
2314 pr_debug("%s: stop transmitted packets\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002315 netif_stop_queue(dev);
2316 }
2317
2318 dev->stats.tx_bytes += skb->len;
2319
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002320 /* According to the coalesce parameter the IC bit for the latest
2321 * segment is reset and the timer re-started to clean the tx status.
2322 * This approach takes care about the fragments: desc is the first
2323 * element in case of no SG.
2324 */
2325 priv->tx_count_frames += nfrags + 1;
2326 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
2327 mod_timer(&priv->txtimer,
2328 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2329 } else {
2330 priv->tx_count_frames = 0;
2331 priv->hw->desc->set_tx_ic(desc);
2332 priv->xstats.tx_set_ic_bit++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002333 }
2334
2335 if (!priv->hwts_tx_en)
2336 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00002337
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002338 /* Ready to fill the first descriptor and set the OWN bit w/o any
2339 * problems because all the descriptors are actually ready to be
2340 * passed to the DMA engine.
2341 */
2342 if (likely(!is_jumbo)) {
2343 bool last_segment = (nfrags == 0);
2344
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002345 des = dma_map_single(priv->device, skb->data,
2346 nopaged_len, DMA_TO_DEVICE);
2347 if (dma_mapping_error(priv->device, des))
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002348 goto dma_map_err;
2349
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002350 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
2351 first->des0 = des;
2352 priv->tx_skbuff_dma[first_entry].buf = first->des0;
2353 } else {
2354 first->des2 = des;
2355 priv->tx_skbuff_dma[first_entry].buf = first->des2;
2356 }
2357
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002358 priv->tx_skbuff_dma[first_entry].len = nopaged_len;
2359 priv->tx_skbuff_dma[first_entry].last_segment = last_segment;
2360
2361 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2362 priv->hwts_tx_en)) {
2363 /* declare that device is doing timestamping */
2364 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2365 priv->hw->desc->enable_tx_timestamp(first);
2366 }
2367
2368 /* Prepare the first descriptor setting the OWN bit too */
2369 priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
2370 csum_insertion, priv->mode, 1,
2371 last_segment);
2372
2373 /* The own bit must be the latest setting done when prepare the
2374 * descriptor and then barrier is needed to make sure that
2375 * all is coherent before granting the DMA engine.
2376 */
2377 smp_wmb();
2378 }
2379
Beniamino Galvani38979572015-01-21 19:07:27 +01002380 netdev_sent_queue(dev, skb->len);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002381
2382 if (priv->synopsys_id < DWMAC_CORE_4_00)
2383 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2384 else
2385 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
2386 STMMAC_CHAN0);
Richard Cochran52f64fa2011-06-19 03:31:43 +00002387
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002388 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002389 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002390
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002391dma_map_err:
Fabrice Gasnier758a0ab2014-11-04 17:08:06 +01002392 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002393 dev_err(priv->device, "Tx dma map failed\n");
2394 dev_kfree_skb(skb);
2395 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002396 return NETDEV_TX_OK;
2397}
2398
Vince Bridgersb9381982014-01-14 13:42:05 -06002399static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2400{
2401 struct ethhdr *ehdr;
2402 u16 vlanid;
2403
2404 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2405 NETIF_F_HW_VLAN_CTAG_RX &&
2406 !__vlan_get_tag(skb, &vlanid)) {
2407 /* pop the vlan tag */
2408 ehdr = (struct ethhdr *)skb->data;
2409 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2410 skb_pull(skb, VLAN_HLEN);
2411 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2412 }
2413}
2414
2415
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002416static inline int stmmac_rx_threshold_count(struct stmmac_priv *priv)
2417{
2418 if (priv->rx_zeroc_thresh < STMMAC_RX_THRESH)
2419 return 0;
2420
2421 return 1;
2422}
2423
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002424/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002425 * stmmac_rx_refill - refill used skb preallocated buffers
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002426 * @priv: driver private structure
2427 * Description : this is to reallocate the skb for the reception process
2428 * that is based on zero-copy.
2429 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002430static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2431{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002432 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002433 unsigned int entry = priv->dirty_rx;
2434 int dirty = stmmac_rx_dirty(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002435
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002436 while (dirty-- > 0) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002437 struct dma_desc *p;
2438
2439 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002440 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002441 else
2442 p = priv->dma_rx + entry;
2443
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002444 if (likely(priv->rx_skbuff[entry] == NULL)) {
2445 struct sk_buff *skb;
2446
Eric Dumazetacb600d2012-10-05 06:23:55 +00002447 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002448 if (unlikely(!skb)) {
2449 /* so for a while no zero-copy! */
2450 priv->rx_zeroc_thresh = STMMAC_RX_THRESH;
2451 if (unlikely(net_ratelimit()))
2452 dev_err(priv->device,
2453 "fail to alloc skb entry %d\n",
2454 entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002455 break;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002456 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002457
2458 priv->rx_skbuff[entry] = skb;
2459 priv->rx_skbuff_dma[entry] =
2460 dma_map_single(priv->device, skb->data, bfsize,
2461 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002462 if (dma_mapping_error(priv->device,
2463 priv->rx_skbuff_dma[entry])) {
2464 dev_err(priv->device, "Rx dma map failed\n");
2465 dev_kfree_skb(skb);
2466 break;
2467 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002468
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002469 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
2470 p->des0 = priv->rx_skbuff_dma[entry];
2471 p->des1 = 0;
2472 } else {
2473 p->des2 = priv->rx_skbuff_dma[entry];
2474 }
2475 if (priv->hw->mode->refill_desc3)
2476 priv->hw->mode->refill_desc3(priv, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002477
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002478 if (priv->rx_zeroc_thresh > 0)
2479 priv->rx_zeroc_thresh--;
2480
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002481 if (netif_msg_rx_status(priv))
2482 pr_debug("\trefill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002483 }
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002484 wmb();
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002485
2486 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
2487 priv->hw->desc->init_rx_desc(p, priv->use_riwt, 0, 0);
2488 else
2489 priv->hw->desc->set_rx_owner(p);
2490
Deepak Sikri8e839892012-07-08 21:14:45 +00002491 wmb();
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002492
2493 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002494 }
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002495 priv->dirty_rx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002496}
2497
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002498/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002499 * stmmac_rx - manage the receive process
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002500 * @priv: driver private structure
2501 * @limit: napi bugget.
2502 * Description : this the function called by the napi poll method.
2503 * It gets all the frames inside the ring.
2504 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002505static int stmmac_rx(struct stmmac_priv *priv, int limit)
2506{
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002507 unsigned int entry = priv->cur_rx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002508 unsigned int next_entry;
2509 unsigned int count = 0;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002510 int coe = priv->hw->rx_csum;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002511
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002512 if (netif_msg_rx_status(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002513 void *rx_head;
2514
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002515 pr_info(">>>>>> %s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002516 if (priv->extend_desc)
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002517 rx_head = (void *)priv->dma_erx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002518 else
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002519 rx_head = (void *)priv->dma_rx;
2520
2521 priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002522 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002523 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002524 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002525 struct dma_desc *p;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002526 struct dma_desc *np;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002527
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002528 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002529 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002530 else
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002531 p = priv->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002532
Fabrice Gasnierc1fa3212016-02-29 14:27:34 +01002533 /* read the status of the incoming frame */
2534 status = priv->hw->desc->rx_status(&priv->dev->stats,
2535 &priv->xstats, p);
2536 /* check if managed by the DMA otherwise go ahead */
2537 if (unlikely(status & dma_own))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002538 break;
2539
2540 count++;
2541
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002542 priv->cur_rx = STMMAC_GET_ENTRY(priv->cur_rx, DMA_RX_SIZE);
2543 next_entry = priv->cur_rx;
2544
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002545 if (priv->extend_desc)
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002546 np = (struct dma_desc *)(priv->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002547 else
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002548 np = priv->dma_rx + next_entry;
2549
2550 prefetch(np);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002551
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002552 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2553 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2554 &priv->xstats,
2555 priv->dma_erx +
2556 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002557 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002558 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002559 if (priv->hwts_rx_en && !priv->extend_desc) {
2560 /* DESC2 & DESC3 will be overwitten by device
2561 * with timestamp value, hence reinitialize
2562 * them in stmmac_rx_refill() function so that
2563 * device can reuse it.
2564 */
2565 priv->rx_skbuff[entry] = NULL;
2566 dma_unmap_single(priv->device,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002567 priv->rx_skbuff_dma[entry],
2568 priv->dma_buf_sz,
2569 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002570 }
2571 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002572 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002573 int frame_len;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002574 unsigned int des;
2575
2576 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
2577 des = p->des0;
2578 else
2579 des = p->des2;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002580
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002581 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2582
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002583 /* If frame length is greather than skb buffer size
2584 * (preallocated during init) then the packet is
2585 * ignored
2586 */
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01002587 if (frame_len > priv->dma_buf_sz) {
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002588 pr_err("%s: len %d larger than size (%d)\n",
2589 priv->dev->name, frame_len,
2590 priv->dma_buf_sz);
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01002591 priv->dev->stats.rx_length_errors++;
2592 break;
2593 }
2594
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002595 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002596 * Type frames (LLC/LLC-SNAP)
2597 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002598 if (unlikely(status != llc_snap))
2599 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002600
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002601 if (netif_msg_rx_status(priv)) {
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002602 pr_info("\tdesc: %p [entry %d] buff=0x%x\n",
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002603 p, entry, des);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002604 if (frame_len > ETH_FRAME_LEN)
2605 pr_debug("\tframe size %d, COE: %d\n",
2606 frame_len, status);
2607 }
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002608
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002609 /* The zero-copy is always used for all the sizes
2610 * in case of GMAC4 because it needs
2611 * to refill the used descriptors, always.
2612 */
2613 if (unlikely(!priv->plat->has_gmac4 &&
2614 ((frame_len < priv->rx_copybreak) ||
2615 stmmac_rx_threshold_count(priv)))) {
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002616 skb = netdev_alloc_skb_ip_align(priv->dev,
2617 frame_len);
2618 if (unlikely(!skb)) {
2619 if (net_ratelimit())
2620 dev_warn(priv->device,
2621 "packet dropped\n");
2622 priv->dev->stats.rx_dropped++;
2623 break;
2624 }
2625
2626 dma_sync_single_for_cpu(priv->device,
2627 priv->rx_skbuff_dma
2628 [entry], frame_len,
2629 DMA_FROM_DEVICE);
2630 skb_copy_to_linear_data(skb,
2631 priv->
2632 rx_skbuff[entry]->data,
2633 frame_len);
2634
2635 skb_put(skb, frame_len);
2636 dma_sync_single_for_device(priv->device,
2637 priv->rx_skbuff_dma
2638 [entry], frame_len,
2639 DMA_FROM_DEVICE);
2640 } else {
2641 skb = priv->rx_skbuff[entry];
2642 if (unlikely(!skb)) {
2643 pr_err("%s: Inconsistent Rx chain\n",
2644 priv->dev->name);
2645 priv->dev->stats.rx_dropped++;
2646 break;
2647 }
2648 prefetch(skb->data - NET_IP_ALIGN);
2649 priv->rx_skbuff[entry] = NULL;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002650 priv->rx_zeroc_thresh++;
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002651
2652 skb_put(skb, frame_len);
2653 dma_unmap_single(priv->device,
2654 priv->rx_skbuff_dma[entry],
2655 priv->dma_buf_sz,
2656 DMA_FROM_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002657 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002658
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002659 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002660 pr_debug("frame received (%dbytes)", frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002661 print_pkt(skb->data, frame_len);
2662 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002663
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002664 stmmac_get_rx_hwtstamp(priv, p, np, skb);
2665
Vince Bridgersb9381982014-01-14 13:42:05 -06002666 stmmac_rx_vlan(priv->dev, skb);
2667
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002668 skb->protocol = eth_type_trans(skb, priv->dev);
2669
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002670 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002671 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002672 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002673 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002674
2675 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002676
2677 priv->dev->stats.rx_packets++;
2678 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002679 }
2680 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002681 }
2682
2683 stmmac_rx_refill(priv);
2684
2685 priv->xstats.rx_pkt_n += count;
2686
2687 return count;
2688}
2689
2690/**
2691 * stmmac_poll - stmmac poll method (NAPI)
2692 * @napi : pointer to the napi structure.
2693 * @budget : maximum number of packets that the current CPU can receive from
2694 * all interfaces.
2695 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002696 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002697 */
2698static int stmmac_poll(struct napi_struct *napi, int budget)
2699{
2700 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2701 int work_done = 0;
2702
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002703 priv->xstats.napi_poll++;
2704 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002705
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002706 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002707 if (work_done < budget) {
2708 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002709 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002710 }
2711 return work_done;
2712}
2713
2714/**
2715 * stmmac_tx_timeout
2716 * @dev : Pointer to net device structure
2717 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00002718 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002719 * netdev structure and arrange for the device to be reset to a sane state
2720 * in order to transmit a new packet.
2721 */
2722static void stmmac_tx_timeout(struct net_device *dev)
2723{
2724 struct stmmac_priv *priv = netdev_priv(dev);
2725
2726 /* Clear Tx resources and restart transmitting again */
2727 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002728}
2729
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002730/**
Jiri Pirko01789342011-08-16 06:29:00 +00002731 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002732 * @dev : pointer to the device structure
2733 * Description:
2734 * This function is a driver entry point which gets called by the kernel
2735 * whenever multicast addresses must be enabled/disabled.
2736 * Return value:
2737 * void.
2738 */
Jiri Pirko01789342011-08-16 06:29:00 +00002739static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002740{
2741 struct stmmac_priv *priv = netdev_priv(dev);
2742
Vince Bridgers3b57de92014-07-31 15:49:17 -05002743 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002744}
2745
2746/**
2747 * stmmac_change_mtu - entry point to change MTU size for the device.
2748 * @dev : device pointer.
2749 * @new_mtu : the new MTU size for the device.
2750 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2751 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2752 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2753 * Return value:
2754 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2755 * file on failure.
2756 */
2757static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2758{
2759 struct stmmac_priv *priv = netdev_priv(dev);
2760 int max_mtu;
2761
2762 if (netif_running(dev)) {
2763 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2764 return -EBUSY;
2765 }
2766
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002767 if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002768 max_mtu = JUMBO_LEN;
2769 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00002770 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002771
Vince Bridgers2618abb2014-01-20 05:39:01 -06002772 if (priv->plat->maxmtu < max_mtu)
2773 max_mtu = priv->plat->maxmtu;
2774
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002775 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2776 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2777 return -EINVAL;
2778 }
2779
Michał Mirosław5e982f32011-04-09 02:46:55 +00002780 dev->mtu = new_mtu;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002781
Michał Mirosław5e982f32011-04-09 02:46:55 +00002782 netdev_update_features(dev);
2783
2784 return 0;
2785}
2786
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002787static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002788 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002789{
2790 struct stmmac_priv *priv = netdev_priv(dev);
2791
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002792 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002793 features &= ~NETIF_F_RXCSUM;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002794
Michał Mirosław5e982f32011-04-09 02:46:55 +00002795 if (!priv->plat->tx_coe)
Tom Herberta1882222015-12-14 11:19:43 -08002796 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosław5e982f32011-04-09 02:46:55 +00002797
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002798 /* Some GMAC devices have a bugged Jumbo frame support that
2799 * needs to have the Tx COE disabled for oversized frames
2800 * (due to limited buffer sizes). In this case we disable
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002801 * the TX csum insertionin the TDES and not use SF.
2802 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00002803 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08002804 features &= ~NETIF_F_CSUM_MASK;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002805
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002806 /* Disable tso if asked by ethtool */
2807 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
2808 if (features & NETIF_F_TSO)
2809 priv->tso = true;
2810 else
2811 priv->tso = false;
2812 }
2813
Michał Mirosław5e982f32011-04-09 02:46:55 +00002814 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002815}
2816
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002817static int stmmac_set_features(struct net_device *netdev,
2818 netdev_features_t features)
2819{
2820 struct stmmac_priv *priv = netdev_priv(netdev);
2821
2822 /* Keep the COE Type in case of csum is supporting */
2823 if (features & NETIF_F_RXCSUM)
2824 priv->hw->rx_csum = priv->plat->rx_coe;
2825 else
2826 priv->hw->rx_csum = 0;
2827 /* No check needed because rx_coe has been set before and it will be
2828 * fixed in case of issue.
2829 */
2830 priv->hw->mac->rx_ipc(priv->hw);
2831
2832 return 0;
2833}
2834
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002835/**
2836 * stmmac_interrupt - main ISR
2837 * @irq: interrupt number.
2838 * @dev_id: to pass the net device pointer.
2839 * Description: this is the main driver interrupt service routine.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002840 * It can call:
2841 * o DMA service routine (to manage incoming frame reception and transmission
2842 * status)
2843 * o Core interrupts to manage: remote wake-up, management counter, LPI
2844 * interrupts.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002845 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002846static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2847{
2848 struct net_device *dev = (struct net_device *)dev_id;
2849 struct stmmac_priv *priv = netdev_priv(dev);
2850
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002851 if (priv->irq_wake)
2852 pm_wakeup_event(priv->device, 0);
2853
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002854 if (unlikely(!dev)) {
2855 pr_err("%s: invalid dev pointer\n", __func__);
2856 return IRQ_NONE;
2857 }
2858
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002859 /* To handle GMAC own interrupts */
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002860 if ((priv->plat->has_gmac) || (priv->plat->has_gmac4)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002861 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002862 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002863 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002864 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002865 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002866 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002867 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002868 priv->tx_path_in_lpi_mode = false;
Matt Coralloa8b7d772016-06-30 19:46:16 +00002869 if (status & CORE_IRQ_MTL_RX_OVERFLOW && priv->hw->dma->set_rx_tail_ptr)
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002870 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
2871 priv->rx_tail_addr,
2872 STMMAC_CHAN0);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002873 }
Giuseppe CAVALLARO70523e632016-06-24 15:16:24 +02002874
2875 /* PCS link status */
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02002876 if (priv->hw->pcs) {
Giuseppe CAVALLARO70523e632016-06-24 15:16:24 +02002877 if (priv->xstats.pcs_link)
2878 netif_carrier_on(dev);
2879 else
2880 netif_carrier_off(dev);
2881 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002882 }
2883
2884 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002885 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002886
2887 return IRQ_HANDLED;
2888}
2889
2890#ifdef CONFIG_NET_POLL_CONTROLLER
2891/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002892 * to allow network I/O with interrupts disabled.
2893 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002894static void stmmac_poll_controller(struct net_device *dev)
2895{
2896 disable_irq(dev->irq);
2897 stmmac_interrupt(dev->irq, dev);
2898 enable_irq(dev->irq);
2899}
2900#endif
2901
2902/**
2903 * stmmac_ioctl - Entry point for the Ioctl
2904 * @dev: Device pointer.
2905 * @rq: An IOCTL specefic structure, that can contain a pointer to
2906 * a proprietary structure used to pass information to the driver.
2907 * @cmd: IOCTL command
2908 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002909 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002910 */
2911static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2912{
2913 struct stmmac_priv *priv = netdev_priv(dev);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002914 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002915
2916 if (!netif_running(dev))
2917 return -EINVAL;
2918
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002919 switch (cmd) {
2920 case SIOCGMIIPHY:
2921 case SIOCGMIIREG:
2922 case SIOCSMIIREG:
2923 if (!priv->phydev)
2924 return -EINVAL;
2925 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2926 break;
2927 case SIOCSHWTSTAMP:
2928 ret = stmmac_hwtstamp_ioctl(dev, rq);
2929 break;
2930 default:
2931 break;
2932 }
Richard Cochran28b04112010-07-17 08:48:55 +00002933
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002934 return ret;
2935}
2936
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002937#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002938static struct dentry *stmmac_fs_dir;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002939
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002940static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002941 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002942{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002943 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002944 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2945 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002946
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002947 for (i = 0; i < size; i++) {
2948 u64 x;
2949 if (extend_desc) {
2950 x = *(u64 *) ep;
2951 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002952 i, (unsigned int)virt_to_phys(ep),
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002953 ep->basic.des0, ep->basic.des1,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002954 ep->basic.des2, ep->basic.des3);
2955 ep++;
2956 } else {
2957 x = *(u64 *) p;
2958 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002959 i, (unsigned int)virt_to_phys(ep),
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002960 p->des0, p->des1, p->des2, p->des3);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002961 p++;
2962 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002963 seq_printf(seq, "\n");
2964 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002965}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002966
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002967static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2968{
2969 struct net_device *dev = seq->private;
2970 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002971
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002972 if (priv->extend_desc) {
2973 seq_printf(seq, "Extended RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002974 sysfs_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002975 seq_printf(seq, "Extended TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002976 sysfs_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002977 } else {
2978 seq_printf(seq, "RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002979 sysfs_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002980 seq_printf(seq, "TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002981 sysfs_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002982 }
2983
2984 return 0;
2985}
2986
2987static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2988{
2989 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2990}
2991
2992static const struct file_operations stmmac_rings_status_fops = {
2993 .owner = THIS_MODULE,
2994 .open = stmmac_sysfs_ring_open,
2995 .read = seq_read,
2996 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002997 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002998};
2999
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003000static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
3001{
3002 struct net_device *dev = seq->private;
3003 struct stmmac_priv *priv = netdev_priv(dev);
3004
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00003005 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003006 seq_printf(seq, "DMA HW features not supported\n");
3007 return 0;
3008 }
3009
3010 seq_printf(seq, "==============================\n");
3011 seq_printf(seq, "\tDMA HW features\n");
3012 seq_printf(seq, "==============================\n");
3013
3014 seq_printf(seq, "\t10/100 Mbps %s\n",
3015 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
3016 seq_printf(seq, "\t1000 Mbps %s\n",
3017 (priv->dma_cap.mbps_1000) ? "Y" : "N");
3018 seq_printf(seq, "\tHalf duple %s\n",
3019 (priv->dma_cap.half_duplex) ? "Y" : "N");
3020 seq_printf(seq, "\tHash Filter: %s\n",
3021 (priv->dma_cap.hash_filter) ? "Y" : "N");
3022 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
3023 (priv->dma_cap.multi_addr) ? "Y" : "N");
3024 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
3025 (priv->dma_cap.pcs) ? "Y" : "N");
3026 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
3027 (priv->dma_cap.sma_mdio) ? "Y" : "N");
3028 seq_printf(seq, "\tPMT Remote wake up: %s\n",
3029 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
3030 seq_printf(seq, "\tPMT Magic Frame: %s\n",
3031 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
3032 seq_printf(seq, "\tRMON module: %s\n",
3033 (priv->dma_cap.rmon) ? "Y" : "N");
3034 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
3035 (priv->dma_cap.time_stamp) ? "Y" : "N");
3036 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
3037 (priv->dma_cap.atime_stamp) ? "Y" : "N");
3038 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
3039 (priv->dma_cap.eee) ? "Y" : "N");
3040 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
3041 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
3042 (priv->dma_cap.tx_coe) ? "Y" : "N");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003043 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3044 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
3045 (priv->dma_cap.rx_coe) ? "Y" : "N");
3046 } else {
3047 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
3048 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
3049 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
3050 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
3051 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003052 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
3053 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
3054 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
3055 priv->dma_cap.number_rx_channel);
3056 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
3057 priv->dma_cap.number_tx_channel);
3058 seq_printf(seq, "\tEnhanced descriptors: %s\n",
3059 (priv->dma_cap.enh_desc) ? "Y" : "N");
3060
3061 return 0;
3062}
3063
3064static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
3065{
3066 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
3067}
3068
3069static const struct file_operations stmmac_dma_cap_fops = {
3070 .owner = THIS_MODULE,
3071 .open = stmmac_sysfs_dma_cap_open,
3072 .read = seq_read,
3073 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00003074 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003075};
3076
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003077static int stmmac_init_fs(struct net_device *dev)
3078{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003079 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003080
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003081 /* Create per netdev entries */
3082 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
3083
3084 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
3085 pr_err("ERROR %s/%s, debugfs create directory failed\n",
3086 STMMAC_RESOURCE_NAME, dev->name);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003087
3088 return -ENOMEM;
3089 }
3090
3091 /* Entry to report DMA RX/TX rings */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003092 priv->dbgfs_rings_status =
3093 debugfs_create_file("descriptors_status", S_IRUGO,
3094 priv->dbgfs_dir, dev,
3095 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003096
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003097 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003098 pr_info("ERROR creating stmmac ring debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003099 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003100
3101 return -ENOMEM;
3102 }
3103
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003104 /* Entry to report the DMA HW features */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003105 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
3106 priv->dbgfs_dir,
3107 dev, &stmmac_dma_cap_fops);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003108
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003109 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003110 pr_info("ERROR creating stmmac MMC debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003111 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003112
3113 return -ENOMEM;
3114 }
3115
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003116 return 0;
3117}
3118
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003119static void stmmac_exit_fs(struct net_device *dev)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003120{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003121 struct stmmac_priv *priv = netdev_priv(dev);
3122
3123 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003124}
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01003125#endif /* CONFIG_DEBUG_FS */
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003126
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003127static const struct net_device_ops stmmac_netdev_ops = {
3128 .ndo_open = stmmac_open,
3129 .ndo_start_xmit = stmmac_xmit,
3130 .ndo_stop = stmmac_release,
3131 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00003132 .ndo_fix_features = stmmac_fix_features,
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003133 .ndo_set_features = stmmac_set_features,
Jiri Pirko01789342011-08-16 06:29:00 +00003134 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003135 .ndo_tx_timeout = stmmac_tx_timeout,
3136 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003137#ifdef CONFIG_NET_POLL_CONTROLLER
3138 .ndo_poll_controller = stmmac_poll_controller,
3139#endif
3140 .ndo_set_mac_address = eth_mac_addr,
3141};
3142
3143/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003144 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003145 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003146 * Description: this function is to configure the MAC device according to
3147 * some platform parameters or the HW capability register. It prepares the
3148 * driver to use either ring or chain modes and to setup either enhanced or
3149 * normal descriptors.
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003150 */
3151static int stmmac_hw_init(struct stmmac_priv *priv)
3152{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003153 struct mac_device_info *mac;
3154
3155 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00003156 if (priv->plat->has_gmac) {
3157 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05003158 mac = dwmac1000_setup(priv->ioaddr,
3159 priv->plat->multicast_filter_bins,
Alexandre TORGUEc623d142016-04-01 11:37:27 +02003160 priv->plat->unicast_filter_entries,
3161 &priv->synopsys_id);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003162 } else if (priv->plat->has_gmac4) {
3163 priv->dev->priv_flags |= IFF_UNICAST_FLT;
3164 mac = dwmac4_setup(priv->ioaddr,
3165 priv->plat->multicast_filter_bins,
3166 priv->plat->unicast_filter_entries,
3167 &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00003168 } else {
Alexandre TORGUEc623d142016-04-01 11:37:27 +02003169 mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00003170 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003171 if (!mac)
3172 return -ENOMEM;
3173
3174 priv->hw = mac;
3175
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003176 /* To use the chained or ring mode */
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003177 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3178 priv->hw->mode = &dwmac4_ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003179 } else {
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003180 if (chain_mode) {
3181 priv->hw->mode = &chain_mode_ops;
3182 pr_info(" Chain mode enabled\n");
3183 priv->mode = STMMAC_CHAIN_MODE;
3184 } else {
3185 priv->hw->mode = &ring_mode_ops;
3186 pr_info(" Ring mode enabled\n");
3187 priv->mode = STMMAC_RING_MODE;
3188 }
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003189 }
3190
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003191 /* Get the HW capability (new GMAC newer than 3.50a) */
3192 priv->hw_cap_support = stmmac_get_hw_features(priv);
3193 if (priv->hw_cap_support) {
3194 pr_info(" DMA HW capability register supported");
3195
3196 /* We can override some gmac/dma configuration fields: e.g.
3197 * enh_desc, tx_coe (e.g. that are passed through the
3198 * platform) with the values from the HW capability
3199 * register (if supported).
3200 */
3201 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003202 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02003203 priv->hw->pmt = priv->plat->pmt;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00003204
Ezequiel Garciaa8df35d2016-05-16 12:41:07 -03003205 /* TXCOE doesn't work in thresh DMA mode */
3206 if (priv->plat->force_thresh_dma_mode)
3207 priv->plat->tx_coe = 0;
3208 else
3209 priv->plat->tx_coe = priv->dma_cap.tx_coe;
3210
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003211 /* In case of GMAC4 rx_coe is from HW cap register. */
3212 priv->plat->rx_coe = priv->dma_cap.rx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00003213
3214 if (priv->dma_cap.rx_coe_type2)
3215 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
3216 else if (priv->dma_cap.rx_coe_type1)
3217 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
3218
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003219 } else
3220 pr_info(" No HW DMA feature register supported");
3221
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003222 /* To use alternate (extended), normal or GMAC4 descriptor structures */
3223 if (priv->synopsys_id >= DWMAC_CORE_4_00)
3224 priv->hw->desc = &dwmac4_desc_ops;
3225 else
3226 stmmac_selec_desc_mode(priv);
Byungho An61369d02013-06-28 16:35:32 +09003227
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003228 if (priv->plat->rx_coe) {
3229 priv->hw->rx_csum = priv->plat->rx_coe;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003230 pr_info(" RX Checksum Offload Engine supported\n");
3231 if (priv->synopsys_id < DWMAC_CORE_4_00)
3232 pr_info("\tCOE Type %d\n", priv->hw->rx_csum);
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003233 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003234 if (priv->plat->tx_coe)
3235 pr_info(" TX Checksum insertion supported\n");
3236
3237 if (priv->plat->pmt) {
3238 pr_info(" Wake-Up On Lan supported\n");
3239 device_set_wakeup_capable(priv->device, 1);
3240 }
3241
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003242 if (priv->dma_cap.tsoen)
3243 pr_info(" TSO supported\n");
3244
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003245 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003246}
3247
3248/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003249 * stmmac_dvr_probe
3250 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00003251 * @plat_dat: platform data pointer
Joachim Eastwoode56788c2015-05-20 20:03:07 +02003252 * @res: stmmac resource pointer
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003253 * Description: this is the main probe function used to
3254 * call the alloc_etherdev, allocate the priv structure.
Andy Shevchenko9afec6e2015-01-27 18:38:03 +02003255 * Return:
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003256 * returns 0 on success, otherwise errno.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003257 */
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003258int stmmac_dvr_probe(struct device *device,
3259 struct plat_stmmacenet_data *plat_dat,
3260 struct stmmac_resources *res)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003261{
3262 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003263 struct net_device *ndev = NULL;
3264 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003265
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003266 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00003267 if (!ndev)
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003268 return -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003269
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003270 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003271
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003272 priv = netdev_priv(ndev);
3273 priv->device = device;
3274 priv->dev = ndev;
3275
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003276 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003277 priv->pause = pause;
3278 priv->plat = plat_dat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +02003279 priv->ioaddr = res->addr;
3280 priv->dev->base_addr = (unsigned long)res->addr;
3281
3282 priv->dev->irq = res->irq;
3283 priv->wol_irq = res->wol_irq;
3284 priv->lpi_irq = res->lpi_irq;
3285
3286 if (res->mac)
3287 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003288
Joachim Eastwooda7a62682015-07-17 23:48:17 +02003289 dev_set_drvdata(device, priv->dev);
Joachim Eastwood803f8fc2015-05-20 20:03:06 +02003290
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003291 /* Verify driver arguments */
3292 stmmac_verify_args();
3293
3294 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003295 * this needs to have multiple instances
3296 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003297 if ((phyaddr >= 0) && (phyaddr <= 31))
3298 priv->plat->phy_addr = phyaddr;
3299
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003300 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
3301 if (IS_ERR(priv->stmmac_clk)) {
3302 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
3303 __func__);
Kweh, Hock Leongc5bb86c2014-09-26 21:42:55 +08003304 /* If failed to obtain stmmac_clk and specific clk_csr value
3305 * is NOT passed from the platform, probe fail.
3306 */
3307 if (!priv->plat->clk_csr) {
3308 ret = PTR_ERR(priv->stmmac_clk);
3309 goto error_clk_get;
3310 } else {
3311 priv->stmmac_clk = NULL;
3312 }
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003313 }
3314 clk_prepare_enable(priv->stmmac_clk);
3315
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003316 priv->pclk = devm_clk_get(priv->device, "pclk");
3317 if (IS_ERR(priv->pclk)) {
3318 if (PTR_ERR(priv->pclk) == -EPROBE_DEFER) {
3319 ret = -EPROBE_DEFER;
3320 goto error_pclk_get;
3321 }
3322 priv->pclk = NULL;
3323 }
3324 clk_prepare_enable(priv->pclk);
3325
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08003326 priv->stmmac_rst = devm_reset_control_get(priv->device,
3327 STMMAC_RESOURCE_NAME);
3328 if (IS_ERR(priv->stmmac_rst)) {
3329 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
3330 ret = -EPROBE_DEFER;
3331 goto error_hw_init;
3332 }
3333 dev_info(priv->device, "no reset control found\n");
3334 priv->stmmac_rst = NULL;
3335 }
3336 if (priv->stmmac_rst)
3337 reset_control_deassert(priv->stmmac_rst);
3338
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003339 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003340 ret = stmmac_hw_init(priv);
3341 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003342 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003343
3344 ndev->netdev_ops = &stmmac_netdev_ops;
3345
3346 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3347 NETIF_F_RXCSUM;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003348
3349 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
3350 ndev->hw_features |= NETIF_F_TSO;
3351 priv->tso = true;
3352 pr_info(" TSO feature enabled\n");
3353 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003354 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
3355 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003356#ifdef STMMAC_VLAN_TAG_USED
3357 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00003358 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003359#endif
3360 priv->msg_enable = netif_msg_init(debug, default_msg_level);
3361
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003362 if (flow_ctrl)
3363 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
3364
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00003365 /* Rx Watchdog is available in the COREs newer than the 3.40.
3366 * In some case, for example on bugged HW this feature
3367 * has to be disable and this can be done by passing the
3368 * riwt_off field from the platform.
3369 */
3370 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
3371 priv->use_riwt = 1;
3372 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
3373 }
3374
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003375 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003376
Vlad Lunguf8e96162010-11-29 22:52:52 +00003377 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00003378 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00003379
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00003380 /* If a specific clk_csr value is passed from the platform
3381 * this means that the CSR Clock Range selection cannot be
3382 * changed at run-time and it is fixed. Viceversa the driver'll try to
3383 * set the MDC clock dynamically according to the csr actual
3384 * clock input.
3385 */
3386 if (!priv->plat->clk_csr)
3387 stmmac_clk_csr_set(priv);
3388 else
3389 priv->clk_csr = priv->plat->clk_csr;
3390
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00003391 stmmac_check_pcs_mode(priv);
3392
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02003393 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
3394 priv->hw->pcs != STMMAC_PCS_TBI &&
3395 priv->hw->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00003396 /* MDIO bus Registration */
3397 ret = stmmac_mdio_register(ndev);
3398 if (ret < 0) {
3399 pr_debug("%s: MDIO bus (id: %d) registration failed",
3400 __func__, priv->plat->bus_id);
Florian Fainelli99f40c62016-12-27 18:23:06 -08003401 goto error_napi_register;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00003402 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00003403 }
3404
Florian Fainelli99f40c62016-12-27 18:23:06 -08003405 ret = register_netdev(ndev);
3406 if (ret) {
3407 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
3408 goto error_netdev_register;
3409 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003410
Florian Fainelli99f40c62016-12-27 18:23:06 -08003411 return ret;
3412
Viresh Kumar6a81c262012-07-30 14:39:41 -07003413error_netdev_register:
Florian Fainelli99f40c62016-12-27 18:23:06 -08003414 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
3415 priv->hw->pcs != STMMAC_PCS_TBI &&
3416 priv->hw->pcs != STMMAC_PCS_RTBI)
3417 stmmac_mdio_unregister(ndev);
3418error_napi_register:
Viresh Kumar6a81c262012-07-30 14:39:41 -07003419 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003420error_hw_init:
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003421 clk_disable_unprepare(priv->pclk);
3422error_pclk_get:
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003423 clk_disable_unprepare(priv->stmmac_clk);
3424error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00003425 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003426
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003427 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003428}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003429EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003430
3431/**
3432 * stmmac_dvr_remove
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003433 * @dev: device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003434 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003435 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003436 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003437int stmmac_dvr_remove(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003438{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003439 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00003440 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003441
3442 pr_info("%s:\n\tremoving driver", __func__);
3443
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00003444 priv->hw->dma->stop_rx(priv->ioaddr);
3445 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003446
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003447 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003448 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003449 unregister_netdev(ndev);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08003450 if (priv->stmmac_rst)
3451 reset_control_assert(priv->stmmac_rst);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003452 clk_disable_unprepare(priv->pclk);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003453 clk_disable_unprepare(priv->stmmac_clk);
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02003454 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
3455 priv->hw->pcs != STMMAC_PCS_TBI &&
3456 priv->hw->pcs != STMMAC_PCS_RTBI)
Bryan O'Donoghuee7434712015-04-16 17:56:03 +01003457 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003458 free_netdev(ndev);
3459
3460 return 0;
3461}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003462EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003463
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003464/**
3465 * stmmac_suspend - suspend callback
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003466 * @dev: device pointer
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003467 * Description: this is the function to suspend the device and it is called
3468 * by the platform driver to stop the network queue, release the resources,
3469 * program the PMT register (for WoL), clean and release driver resources.
3470 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003471int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003472{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003473 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003474 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003475 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003476
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003477 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003478 return 0;
3479
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003480 if (priv->phydev)
3481 phy_stop(priv->phydev);
3482
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003483 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003484
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003485 netif_device_detach(ndev);
3486 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003487
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003488 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003489
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003490 /* Stop TX/RX DMA */
3491 priv->hw->dma->stop_tx(priv->ioaddr);
3492 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003493
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003494 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003495 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003496 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003497 priv->irq_wake = 1;
3498 } else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003499 stmmac_set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003500 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003501 /* Disable clock in case of PWM is off */
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003502 clk_disable(priv->pclk);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003503 clk_disable(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003504 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003505 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05003506
3507 priv->oldlink = 0;
3508 priv->speed = 0;
3509 priv->oldduplex = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003510 return 0;
3511}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003512EXPORT_SYMBOL_GPL(stmmac_suspend);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003513
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003514/**
3515 * stmmac_resume - resume callback
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003516 * @dev: device pointer
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003517 * Description: when resume this function is invoked to setup the DMA and CORE
3518 * in a usable state.
3519 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003520int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003521{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02003522 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003523 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003524 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003525
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003526 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003527 return 0;
3528
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003529 /* Power Down bit, into the PM register, is cleared
3530 * automatically as soon as a magic packet or a Wake-up frame
3531 * is received. Anyway, it's better to manually clear
3532 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003533 * from another devices (e.g. serial console).
3534 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003535 if (device_may_wakeup(priv->device)) {
Vincent Palatinf55d84b2016-06-01 08:53:48 -07003536 spin_lock_irqsave(&priv->lock, flags);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003537 priv->hw->mac->pmt(priv->hw, 0);
Vincent Palatinf55d84b2016-06-01 08:53:48 -07003538 spin_unlock_irqrestore(&priv->lock, flags);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003539 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003540 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003541 pinctrl_pm_select_default_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003542 /* enable the clk prevously disabled */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003543 clk_enable(priv->stmmac_clk);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003544 clk_enable(priv->pclk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003545 /* reset the phy so that it's ready */
3546 if (priv->mii)
3547 stmmac_mdio_reset(priv->mii);
3548 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003549
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003550 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003551
Vincent Palatinf55d84b2016-06-01 08:53:48 -07003552 spin_lock_irqsave(&priv->lock, flags);
3553
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01003554 priv->cur_rx = 0;
3555 priv->dirty_rx = 0;
3556 priv->dirty_tx = 0;
3557 priv->cur_tx = 0;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003558 /* reset private mss value to force mss context settings at
3559 * next tso xmit (only used for gmac4).
3560 */
3561 priv->mss = 0;
3562
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01003563 stmmac_clear_descriptors(priv);
3564
Huacai Chenfe1319292014-12-19 22:38:18 +08003565 stmmac_hw_setup(ndev, false);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003566 stmmac_init_tx_coalesce(priv);
Giuseppe CAVALLAROac316c72015-11-26 08:35:41 +01003567 stmmac_set_rx_mode(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003568
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003569 napi_enable(&priv->napi);
3570
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003571 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003572
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003573 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003574
3575 if (priv->phydev)
3576 phy_start(priv->phydev);
3577
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003578 return 0;
3579}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003580EXPORT_SYMBOL_GPL(stmmac_resume);
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00003581
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003582#ifndef MODULE
3583static int __init stmmac_cmdline_opt(char *str)
3584{
3585 char *opt;
3586
3587 if (!str || !*str)
3588 return -EINVAL;
3589 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003590 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003591 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003592 goto err;
3593 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003594 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003595 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003596 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003597 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003598 goto err;
3599 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003600 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003601 goto err;
3602 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003603 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003604 goto err;
3605 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003606 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003607 goto err;
3608 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003609 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003610 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00003611 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003612 if (kstrtoint(opt + 10, 0, &eee_timer))
3613 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003614 } else if (!strncmp(opt, "chain_mode:", 11)) {
3615 if (kstrtoint(opt + 11, 0, &chain_mode))
3616 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003617 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003618 }
3619 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003620
3621err:
3622 pr_err("%s: ERROR broken module parameter conversion", __func__);
3623 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003624}
3625
3626__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003627#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003628
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003629static int __init stmmac_init(void)
3630{
3631#ifdef CONFIG_DEBUG_FS
3632 /* Create debugfs main directory if it doesn't exist yet */
3633 if (!stmmac_fs_dir) {
3634 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
3635
3636 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
3637 pr_err("ERROR %s, debugfs create directory failed\n",
3638 STMMAC_RESOURCE_NAME);
3639
3640 return -ENOMEM;
3641 }
3642 }
3643#endif
3644
3645 return 0;
3646}
3647
3648static void __exit stmmac_exit(void)
3649{
3650#ifdef CONFIG_DEBUG_FS
3651 debugfs_remove_recursive(stmmac_fs_dir);
3652#endif
3653}
3654
3655module_init(stmmac_init)
3656module_exit(stmmac_exit)
3657
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003658MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3659MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3660MODULE_LICENSE("GPL");