blob: 56a081f937d2759db85dd3354dc3fdd4c6c878e9 [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
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070016 The full GNU General Public License is included in this distribution in
17 the file called "COPYING".
18
19 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
20
21 Documentation available at:
22 http://www.stlinux.com
23 Support available at:
24 https://bugzilla.stlinux.com/
25*******************************************************************************/
26
Viresh Kumar6a81c262012-07-30 14:39:41 -070027#include <linux/clk.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070028#include <linux/kernel.h>
29#include <linux/interrupt.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070030#include <linux/ip.h>
31#include <linux/tcp.h>
32#include <linux/skbuff.h>
33#include <linux/ethtool.h>
34#include <linux/if_ether.h>
35#include <linux/crc32.h>
36#include <linux/mii.h>
Jiri Pirko01789342011-08-16 06:29:00 +000037#include <linux/if.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070038#include <linux/if_vlan.h>
39#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040041#include <linux/prefetch.h>
Srinivas Kandagatladb88f102014-01-16 10:52:52 +000042#include <linux/pinctrl/consumer.h>
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +010043#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000044#include <linux/debugfs.h>
45#include <linux/seq_file.h>
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +010046#endif /* CONFIG_DEBUG_FS */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +000047#include <linux/net_tstamp.h>
48#include "stmmac_ptp.h"
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +000049#include "stmmac.h"
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +080050#include <linux/reset.h>
Mathieu Olivari5790cf32015-05-27 11:02:47 -070051#include <linux/of_mdio.h>
Phil Reid19d857c2015-12-14 11:32:01 +080052#include "dwmac1000.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070053
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070054#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
Alexandre TORGUEf748be52016-04-01 11:37:34 +020055#define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070056
57/* Module parameters */
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000058#define TX_TIMEO 5000
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070059static int watchdog = TX_TIMEO;
60module_param(watchdog, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000061MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070062
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000063static int debug = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070064module_param(debug, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000065MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070066
stephen hemminger47d1f712013-12-30 10:38:57 -080067static int phyaddr = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070068module_param(phyaddr, int, S_IRUGO);
69MODULE_PARM_DESC(phyaddr, "Physical device address");
70
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +010071#define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +010072#define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070073
74static int flow_ctrl = FLOW_OFF;
75module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
76MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
77
78static int pause = PAUSE_TIME;
79module_param(pause, int, S_IRUGO | S_IWUSR);
80MODULE_PARM_DESC(pause, "Flow Control Pause Time");
81
82#define TC_DEFAULT 64
83static int tc = TC_DEFAULT;
84module_param(tc, int, S_IRUGO | S_IWUSR);
85MODULE_PARM_DESC(tc, "DMA threshold control value");
86
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +010087#define DEFAULT_BUFSIZE 1536
88static int buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070089module_param(buf_sz, int, S_IRUGO | S_IWUSR);
90MODULE_PARM_DESC(buf_sz, "DMA buffer size");
91
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +010092#define STMMAC_RX_COPYBREAK 256
93
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070094static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
95 NETIF_MSG_LINK | NETIF_MSG_IFUP |
96 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
97
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +000098#define STMMAC_DEFAULT_LPI_TIMER 1000
99static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
100module_param(eee_timer, int, S_IRUGO | S_IWUSR);
101MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200102#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000103
Pavel Machek22d3efe2016-11-28 12:55:59 +0100104/* By default the driver will use the ring mode to manage tx and rx descriptors,
105 * but allow user to force to use the chain instead of the ring
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000106 */
107static unsigned int chain_mode;
108module_param(chain_mode, int, S_IRUGO);
109MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
110
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700111static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700112
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +0100113#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000114static int stmmac_init_fs(struct net_device *dev);
Mathieu Olivari466c5ac2015-05-22 19:03:29 -0700115static void stmmac_exit_fs(struct net_device *dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000116#endif
117
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000118#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
119
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700120/**
121 * stmmac_verify_args - verify the driver parameters.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100122 * Description: it checks the driver parameters and set a default in case of
123 * errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700124 */
125static void stmmac_verify_args(void)
126{
127 if (unlikely(watchdog < 0))
128 watchdog = TX_TIMEO;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100129 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
130 buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700131 if (unlikely(flow_ctrl > 1))
132 flow_ctrl = FLOW_AUTO;
133 else if (likely(flow_ctrl < 0))
134 flow_ctrl = FLOW_OFF;
135 if (unlikely((pause < 0) || (pause > 0xffff)))
136 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000137 if (eee_timer < 0)
138 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700139}
140
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000141/**
142 * stmmac_clk_csr_set - dynamically set the MDC clock
143 * @priv: driver private structure
144 * Description: this is to dynamically set the MDC clock according to the csr
145 * clock input.
146 * Note:
147 * If a specific clk_csr value is passed from the platform
148 * this means that the CSR Clock Range selection cannot be
149 * changed at run-time and it is fixed (as reported in the driver
150 * documentation). Viceversa the driver will try to set the MDC
151 * clock dynamically according to the actual clock input.
152 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000153static void stmmac_clk_csr_set(struct stmmac_priv *priv)
154{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000155 u32 clk_rate;
156
jpintof573c0b2017-01-09 12:35:09 +0000157 clk_rate = clk_get_rate(priv->plat->stmmac_clk);
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000158
159 /* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000160 * for all other cases except for the below mentioned ones.
161 * For values higher than the IEEE 802.3 specified frequency
162 * we can not estimate the proper divider as it is not known
163 * the frequency of clk_csr_i. So we do not change the default
164 * divider.
165 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000166 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
167 if (clk_rate < CSR_F_35M)
168 priv->clk_csr = STMMAC_CSR_20_35M;
169 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
170 priv->clk_csr = STMMAC_CSR_35_60M;
171 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
172 priv->clk_csr = STMMAC_CSR_60_100M;
173 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
174 priv->clk_csr = STMMAC_CSR_100_150M;
175 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
176 priv->clk_csr = STMMAC_CSR_150_250M;
Phil Reid19d857c2015-12-14 11:32:01 +0800177 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000178 priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000179 }
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000180}
181
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700182static void print_pkt(unsigned char *buf, int len)
183{
Andy Shevchenko424c4f72014-11-07 16:53:12 +0200184 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
185 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700186}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700187
Joao Pintoce736782017-04-06 09:49:10 +0100188static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700189{
Joao Pintoce736782017-04-06 09:49:10 +0100190 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
LABBE Corentina6a3e022017-02-08 09:31:21 +0100191 u32 avail;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100192
Joao Pintoce736782017-04-06 09:49:10 +0100193 if (tx_q->dirty_tx > tx_q->cur_tx)
194 avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100195 else
Joao Pintoce736782017-04-06 09:49:10 +0100196 avail = DMA_TX_SIZE - tx_q->cur_tx + tx_q->dirty_tx - 1;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100197
198 return avail;
199}
200
Joao Pinto54139cf2017-04-06 09:49:09 +0100201/**
202 * stmmac_rx_dirty - Get RX queue dirty
203 * @priv: driver private structure
204 * @queue: RX queue index
205 */
206static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100207{
Joao Pinto54139cf2017-04-06 09:49:09 +0100208 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
LABBE Corentina6a3e022017-02-08 09:31:21 +0100209 u32 dirty;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100210
Joao Pinto54139cf2017-04-06 09:49:09 +0100211 if (rx_q->dirty_rx <= rx_q->cur_rx)
212 dirty = rx_q->cur_rx - rx_q->dirty_rx;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100213 else
Joao Pinto54139cf2017-04-06 09:49:09 +0100214 dirty = DMA_RX_SIZE - rx_q->dirty_rx + rx_q->cur_rx;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100215
216 return dirty;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700217}
218
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000219/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100220 * stmmac_hw_fix_mac_speed - callback for speed selection
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000221 * @priv: driver private structure
LABBE Corentin8d45e422017-02-08 09:31:08 +0100222 * Description: on some platforms (e.g. ST), some HW system configuration
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000223 * registers have to be set according to the link speed negotiated.
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000224 */
225static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
226{
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200227 struct net_device *ndev = priv->dev;
228 struct phy_device *phydev = ndev->phydev;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000229
230 if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000231 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000232}
233
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000234/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100235 * stmmac_enable_eee_mode - check and enter in LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000236 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100237 * Description: this function is to verify and enter in LPI mode in case of
238 * EEE.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000239 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000240static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
241{
Joao Pintoce736782017-04-06 09:49:10 +0100242 u32 tx_cnt = priv->plat->tx_queues_to_use;
243 u32 queue;
244
245 /* check if all TX queues have the work finished */
246 for (queue = 0; queue < tx_cnt; queue++) {
247 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
248
249 if (tx_q->dirty_tx != tx_q->cur_tx)
250 return; /* still unfinished work */
251 }
252
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000253 /* Check and enter in LPI mode */
Joao Pintoce736782017-04-06 09:49:10 +0100254 if (!priv->tx_path_in_lpi_mode)
jpintob4b7b772017-01-09 12:35:08 +0000255 priv->hw->mac->set_eee_mode(priv->hw,
256 priv->plat->en_tx_lpi_clockgating);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000257}
258
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000259/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100260 * stmmac_disable_eee_mode - disable and exit from LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000261 * @priv: driver private structure
262 * Description: this function is to exit and disable EEE in case of
263 * LPI state is true. This is called by the xmit.
264 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000265void stmmac_disable_eee_mode(struct stmmac_priv *priv)
266{
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500267 priv->hw->mac->reset_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000268 del_timer_sync(&priv->eee_ctrl_timer);
269 priv->tx_path_in_lpi_mode = false;
270}
271
272/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100273 * stmmac_eee_ctrl_timer - EEE TX SW timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000274 * @arg : data hook
275 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000276 * if there is no data transfer and if we are not in LPI state,
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000277 * then MAC Transmitter can be moved to LPI state.
278 */
279static void stmmac_eee_ctrl_timer(unsigned long arg)
280{
281 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
282
283 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200284 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000285}
286
287/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100288 * stmmac_eee_init - init EEE
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000289 * @priv: driver private structure
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000290 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100291 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
292 * can also manage EEE, this function enable the LPI state and start related
293 * timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000294 */
295bool stmmac_eee_init(struct stmmac_priv *priv)
296{
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200297 struct net_device *ndev = priv->dev;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100298 unsigned long flags;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000299 bool ret = false;
300
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200301 /* Using PCS we cannot dial with the phy registers at this stage
302 * so we do not support extra feature like EEE.
303 */
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200304 if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
305 (priv->hw->pcs == STMMAC_PCS_TBI) ||
306 (priv->hw->pcs == STMMAC_PCS_RTBI))
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200307 goto out;
308
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000309 /* MAC core supports the EEE feature. */
310 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100311 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000312
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100313 /* Check if the PHY supports EEE */
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200314 if (phy_init_eee(ndev->phydev, 1)) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100315 /* To manage at run-time if the EEE cannot be supported
316 * anymore (for example because the lp caps have been
317 * changed).
318 * In that case the driver disable own timers.
319 */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100320 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100321 if (priv->eee_active) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100322 netdev_dbg(priv->dev, "disable EEE\n");
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100323 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500324 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100325 tx_lpi_timer);
326 }
327 priv->eee_active = 0;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100328 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100329 goto out;
330 }
331 /* Activate the EEE and start timers */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100332 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200333 if (!priv->eee_active) {
334 priv->eee_active = 1;
Vaishali Thakkarccb36da2015-02-28 00:12:34 +0530335 setup_timer(&priv->eee_ctrl_timer,
336 stmmac_eee_ctrl_timer,
337 (unsigned long)priv);
338 mod_timer(&priv->eee_ctrl_timer,
339 STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000340
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500341 priv->hw->mac->set_eee_timer(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200342 STMMAC_DEFAULT_LIT_LS,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100343 tx_lpi_timer);
Giuseppe CAVALLARO71965352014-08-28 08:11:44 +0200344 }
345 /* Set HW EEE according to the speed */
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200346 priv->hw->mac->set_eee_pls(priv->hw, ndev->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000347
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000348 ret = true;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100349 spin_unlock_irqrestore(&priv->lock, flags);
350
LABBE Corentin38ddc592016-11-16 20:09:39 +0100351 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000352 }
353out:
354 return ret;
355}
356
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100357/* stmmac_get_tx_hwtstamp - get HW TX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000358 * @priv: driver private structure
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100359 * @p : descriptor pointer
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000360 * @skb : the socket buffer
361 * Description :
362 * This function will read timestamp from the descriptor & pass it to stack.
363 * and also perform some sanity checks.
364 */
365static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100366 struct dma_desc *p, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000367{
368 struct skb_shared_hwtstamps shhwtstamp;
369 u64 ns;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000370
371 if (!priv->hwts_tx_en)
372 return;
373
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000374 /* exit if skb doesn't support hw tstamp */
damuzi00075e43642014-01-17 23:47:59 +0800375 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000376 return;
377
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000378 /* check tx tstamp status */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100379 if (!priv->hw->desc->get_tx_timestamp_status(p)) {
380 /* get the valid tstamp */
381 ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000382
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100383 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
384 shhwtstamp.hwtstamp = ns_to_ktime(ns);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000385
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100386 netdev_info(priv->dev, "get valid TX hw timestamp %llu\n", ns);
387 /* pass tstamp to stack */
388 skb_tstamp_tx(skb, &shhwtstamp);
389 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000390
391 return;
392}
393
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100394/* stmmac_get_rx_hwtstamp - get HW RX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000395 * @priv: driver private structure
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100396 * @p : descriptor pointer
397 * @np : next descriptor pointer
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000398 * @skb : the socket buffer
399 * Description :
400 * This function will read received packet's timestamp from the descriptor
401 * and pass it to stack. It also perform some sanity checks.
402 */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100403static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
404 struct dma_desc *np, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000405{
406 struct skb_shared_hwtstamps *shhwtstamp = NULL;
407 u64 ns;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000408
409 if (!priv->hwts_rx_en)
410 return;
411
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100412 /* Check if timestamp is available */
413 if (!priv->hw->desc->get_rx_timestamp_status(p, priv->adv_ts)) {
414 /* For GMAC4, the valid timestamp is from CTX next desc. */
415 if (priv->plat->has_gmac4)
416 ns = priv->hw->desc->get_timestamp(np, priv->adv_ts);
417 else
418 ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000419
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100420 netdev_info(priv->dev, "get valid RX hw timestamp %llu\n", ns);
421 shhwtstamp = skb_hwtstamps(skb);
422 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
423 shhwtstamp->hwtstamp = ns_to_ktime(ns);
424 } else {
425 netdev_err(priv->dev, "cannot get RX hw timestamp\n");
426 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000427}
428
429/**
430 * stmmac_hwtstamp_ioctl - control hardware timestamping.
431 * @dev: device pointer.
LABBE Corentin8d45e422017-02-08 09:31:08 +0100432 * @ifr: An IOCTL specific structure, that can contain a pointer to
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000433 * a proprietary structure used to pass information to the driver.
434 * Description:
435 * This function configures the MAC to enable/disable both outgoing(TX)
436 * and incoming(RX) packets time stamping based on user input.
437 * Return Value:
438 * 0 on success and an appropriate -ve integer on failure.
439 */
440static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
441{
442 struct stmmac_priv *priv = netdev_priv(dev);
443 struct hwtstamp_config config;
Arnd Bergmann0a624152015-09-30 13:26:32 +0200444 struct timespec64 now;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000445 u64 temp = 0;
446 u32 ptp_v2 = 0;
447 u32 tstamp_all = 0;
448 u32 ptp_over_ipv4_udp = 0;
449 u32 ptp_over_ipv6_udp = 0;
450 u32 ptp_over_ethernet = 0;
451 u32 snap_type_sel = 0;
452 u32 ts_master_en = 0;
453 u32 ts_event_en = 0;
454 u32 value = 0;
Phil Reid19d857c2015-12-14 11:32:01 +0800455 u32 sec_inc;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000456
457 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
458 netdev_alert(priv->dev, "No support for HW time stamping\n");
459 priv->hwts_tx_en = 0;
460 priv->hwts_rx_en = 0;
461
462 return -EOPNOTSUPP;
463 }
464
465 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000466 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000467 return -EFAULT;
468
LABBE Corentin38ddc592016-11-16 20:09:39 +0100469 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
470 __func__, config.flags, config.tx_type, config.rx_filter);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000471
472 /* reserved for future extensions */
473 if (config.flags)
474 return -EINVAL;
475
Ben Hutchings5f3da322013-11-14 00:43:41 +0000476 if (config.tx_type != HWTSTAMP_TX_OFF &&
477 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000478 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000479
480 if (priv->adv_ts) {
481 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000482 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000483 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000484 config.rx_filter = HWTSTAMP_FILTER_NONE;
485 break;
486
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000487 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000488 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000489 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
490 /* take time stamp for all event messages */
491 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
492
493 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
494 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
495 break;
496
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000497 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000498 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000499 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
500 /* take time stamp for SYNC messages only */
501 ts_event_en = PTP_TCR_TSEVNTENA;
502
503 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
504 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
505 break;
506
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000507 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000508 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000509 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
510 /* take time stamp for Delay_Req messages only */
511 ts_master_en = PTP_TCR_TSMSTRENA;
512 ts_event_en = PTP_TCR_TSEVNTENA;
513
514 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
515 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
516 break;
517
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000518 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000519 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000520 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
521 ptp_v2 = PTP_TCR_TSVER2ENA;
522 /* take time stamp for all event messages */
523 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
524
525 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
526 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
527 break;
528
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000529 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000530 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000531 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
532 ptp_v2 = PTP_TCR_TSVER2ENA;
533 /* take time stamp for SYNC messages only */
534 ts_event_en = PTP_TCR_TSEVNTENA;
535
536 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
537 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
538 break;
539
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000540 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000541 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000542 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
543 ptp_v2 = PTP_TCR_TSVER2ENA;
544 /* take time stamp for Delay_Req messages only */
545 ts_master_en = PTP_TCR_TSMSTRENA;
546 ts_event_en = PTP_TCR_TSEVNTENA;
547
548 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
549 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
550 break;
551
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000552 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000553 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000554 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
555 ptp_v2 = PTP_TCR_TSVER2ENA;
556 /* take time stamp for all event messages */
557 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
558
559 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
560 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
561 ptp_over_ethernet = PTP_TCR_TSIPENA;
562 break;
563
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000564 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000565 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000566 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
567 ptp_v2 = PTP_TCR_TSVER2ENA;
568 /* take time stamp for SYNC messages only */
569 ts_event_en = PTP_TCR_TSEVNTENA;
570
571 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
572 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
573 ptp_over_ethernet = PTP_TCR_TSIPENA;
574 break;
575
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000576 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000577 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000578 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
579 ptp_v2 = PTP_TCR_TSVER2ENA;
580 /* take time stamp for Delay_Req messages only */
581 ts_master_en = PTP_TCR_TSMSTRENA;
582 ts_event_en = PTP_TCR_TSEVNTENA;
583
584 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
585 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
586 ptp_over_ethernet = PTP_TCR_TSIPENA;
587 break;
588
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000589 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000590 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000591 config.rx_filter = HWTSTAMP_FILTER_ALL;
592 tstamp_all = PTP_TCR_TSENALL;
593 break;
594
595 default:
596 return -ERANGE;
597 }
598 } else {
599 switch (config.rx_filter) {
600 case HWTSTAMP_FILTER_NONE:
601 config.rx_filter = HWTSTAMP_FILTER_NONE;
602 break;
603 default:
604 /* PTP v1, UDP, any kind of event packet */
605 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
606 break;
607 }
608 }
609 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000610 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000611
612 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100613 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, 0);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000614 else {
615 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000616 tstamp_all | ptp_v2 | ptp_over_ethernet |
617 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
618 ts_master_en | snap_type_sel);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100619 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, value);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000620
621 /* program Sub Second Increment reg */
Phil Reid19d857c2015-12-14 11:32:01 +0800622 sec_inc = priv->hw->ptp->config_sub_second_increment(
jpintof573c0b2017-01-09 12:35:09 +0000623 priv->ptpaddr, priv->plat->clk_ptp_rate,
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100624 priv->plat->has_gmac4);
Phil Reid19d857c2015-12-14 11:32:01 +0800625 temp = div_u64(1000000000ULL, sec_inc);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000626
627 /* calculate default added value:
628 * formula is :
629 * addend = (2^32)/freq_div_ratio;
Phil Reid19d857c2015-12-14 11:32:01 +0800630 * where, freq_div_ratio = 1e9ns/sec_inc
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000631 */
Phil Reid19d857c2015-12-14 11:32:01 +0800632 temp = (u64)(temp << 32);
jpintof573c0b2017-01-09 12:35:09 +0000633 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100634 priv->hw->ptp->config_addend(priv->ptpaddr,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000635 priv->default_addend);
636
637 /* initialize system time */
Arnd Bergmann0a624152015-09-30 13:26:32 +0200638 ktime_get_real_ts64(&now);
639
640 /* lower 32 bits of tv_sec are safe until y2106 */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100641 priv->hw->ptp->init_systime(priv->ptpaddr, (u32)now.tv_sec,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000642 now.tv_nsec);
643 }
644
645 return copy_to_user(ifr->ifr_data, &config,
646 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
647}
648
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000649/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100650 * stmmac_init_ptp - init PTP
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000651 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100652 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000653 * This is done by looking at the HW cap. register.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100654 * This function also registers the ptp driver.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000655 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000656static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000657{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000658 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
659 return -EOPNOTSUPP;
660
Vince Bridgers7cd01392013-12-20 11:19:34 -0600661 priv->adv_ts = 0;
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200662 /* Check if adv_ts can be enabled for dwmac 4.x core */
663 if (priv->plat->has_gmac4 && priv->dma_cap.atime_stamp)
664 priv->adv_ts = 1;
665 /* Dwmac 3.x core with extend_desc can support adv_ts */
666 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
Vince Bridgers7cd01392013-12-20 11:19:34 -0600667 priv->adv_ts = 1;
668
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200669 if (priv->dma_cap.time_stamp)
670 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
Vince Bridgers7cd01392013-12-20 11:19:34 -0600671
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200672 if (priv->adv_ts)
673 netdev_info(priv->dev,
674 "IEEE 1588-2008 Advanced Timestamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000675
676 priv->hw->ptp = &stmmac_ptp;
677 priv->hwts_tx_en = 0;
678 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000679
Giuseppe CAVALLAROc30a70d2016-10-19 09:06:41 +0200680 stmmac_ptp_register(priv);
681
682 return 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000683}
684
685static void stmmac_release_ptp(struct stmmac_priv *priv)
686{
jpintof573c0b2017-01-09 12:35:09 +0000687 if (priv->plat->clk_ptp_ref)
688 clk_disable_unprepare(priv->plat->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000689 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000690}
691
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700692/**
Joao Pinto29feff32017-03-10 18:24:56 +0000693 * stmmac_mac_flow_ctrl - Configure flow control in all queues
694 * @priv: driver private structure
695 * Description: It is used for configuring the flow control in all queues
696 */
697static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
698{
699 u32 tx_cnt = priv->plat->tx_queues_to_use;
700
701 priv->hw->mac->flow_ctrl(priv->hw, duplex, priv->flow_ctrl,
702 priv->pause, tx_cnt);
703}
704
705/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100706 * stmmac_adjust_link - adjusts the link parameters
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700707 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100708 * Description: this is the helper called by the physical abstraction layer
709 * drivers to communicate the phy link status. According the speed and duplex
710 * this driver can invoke registered glue-logic as well.
711 * It also invoke the eee initialization because it could happen when switch
712 * on different networks (that are eee capable).
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700713 */
714static void stmmac_adjust_link(struct net_device *dev)
715{
716 struct stmmac_priv *priv = netdev_priv(dev);
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200717 struct phy_device *phydev = dev->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700718 unsigned long flags;
719 int new_state = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700720
LABBE Corentin662ec2b2017-02-08 09:31:16 +0100721 if (!phydev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700722 return;
723
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700724 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000725
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700726 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000727 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700728
729 /* Now we make sure that we can be in full duplex mode.
730 * If not, we operate in half-duplex mode. */
731 if (phydev->duplex != priv->oldduplex) {
732 new_state = 1;
733 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000734 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700735 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000736 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700737 priv->oldduplex = phydev->duplex;
738 }
739 /* Flow Control operation */
740 if (phydev->pause)
Joao Pinto29feff32017-03-10 18:24:56 +0000741 stmmac_mac_flow_ctrl(priv, phydev->duplex);
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:
LABBE Corentin3e12790e2017-02-15 10:46:39 +0100747 if (priv->plat->has_gmac ||
748 priv->plat->has_gmac4)
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000749 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700750 break;
751 case 100:
LABBE Corentin9beae262017-02-15 10:46:43 +0100752 if (priv->plat->has_gmac ||
753 priv->plat->has_gmac4) {
754 ctrl |= priv->hw->link.port;
755 ctrl |= priv->hw->link.speed;
756 } else {
757 ctrl &= ~priv->hw->link.port;
758 }
759 break;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700760 case 10:
LABBE Corentin3e12790e2017-02-15 10:46:39 +0100761 if (priv->plat->has_gmac ||
762 priv->plat->has_gmac4) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000763 ctrl |= priv->hw->link.port;
LABBE Corentin9beae262017-02-15 10:46:43 +0100764 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700765 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000766 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700767 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700768 break;
769 default:
LABBE Corentinb3e51062016-11-16 20:09:41 +0100770 netif_warn(priv, link, priv->dev,
LABBE Corentincba920a2017-02-08 09:31:15 +0100771 "broken speed: %d\n", phydev->speed);
LABBE Corentin688495b2017-02-15 10:46:41 +0100772 phydev->speed = SPEED_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700773 break;
774 }
LABBE Corentin5db13552017-02-15 10:46:42 +0100775 if (phydev->speed != SPEED_UNKNOWN)
776 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700777 priv->speed = phydev->speed;
778 }
779
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000780 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700781
782 if (!priv->oldlink) {
783 new_state = 1;
784 priv->oldlink = 1;
785 }
786 } else if (priv->oldlink) {
787 new_state = 1;
788 priv->oldlink = 0;
LABBE Corentinbd006322017-02-15 10:46:40 +0100789 priv->speed = SPEED_UNKNOWN;
790 priv->oldduplex = DUPLEX_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700791 }
792
793 if (new_state && netif_msg_link(priv))
794 phy_print_status(phydev);
795
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100796 spin_unlock_irqrestore(&priv->lock, flags);
797
Giuseppe CAVALLARO52f95bb2016-04-05 08:46:57 +0200798 if (phydev->is_pseudo_fixed_link)
799 /* Stop PHY layer to call the hook to adjust the link in case
800 * of a switch is attached to the stmmac driver.
801 */
802 phydev->irq = PHY_IGNORE_INTERRUPT;
803 else
804 /* At this stage, init the EEE if supported.
805 * Never called in case of fixed_link.
806 */
807 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700808}
809
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000810/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100811 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000812 * @priv: driver private structure
813 * Description: this is to verify if the HW supports the PCS.
814 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
815 * configured for the TBI, RTBI, or SGMII PHY interface.
816 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000817static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
818{
819 int interface = priv->plat->interface;
820
821 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900822 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
823 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
824 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
825 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100826 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200827 priv->hw->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900828 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100829 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200830 priv->hw->pcs = STMMAC_PCS_SGMII;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000831 }
832 }
833}
834
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700835/**
836 * stmmac_init_phy - PHY initialization
837 * @dev: net device structure
838 * Description: it initializes the driver's PHY state, and attaches the PHY
839 * to the mac driver.
840 * Return value:
841 * 0 on success
842 */
843static int stmmac_init_phy(struct net_device *dev)
844{
845 struct stmmac_priv *priv = netdev_priv(dev);
846 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000847 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000848 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000849 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000850 int max_speed = priv->plat->max_speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700851 priv->oldlink = 0;
LABBE Corentinbd006322017-02-15 10:46:40 +0100852 priv->speed = SPEED_UNKNOWN;
853 priv->oldduplex = DUPLEX_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700854
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700855 if (priv->plat->phy_node) {
856 phydev = of_phy_connect(dev, priv->plat->phy_node,
857 &stmmac_adjust_link, 0, interface);
858 } else {
Giuseppe CAVALLAROa7657f12016-04-01 09:07:16 +0200859 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
860 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000861
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700862 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
863 priv->plat->phy_addr);
LABBE Corentinde9a2162016-11-16 20:09:40 +0100864 netdev_dbg(priv->dev, "%s: trying to attach to %s\n", __func__,
LABBE Corentin38ddc592016-11-16 20:09:39 +0100865 phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700866
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700867 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
868 interface);
869 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700870
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300871 if (IS_ERR_OR_NULL(phydev)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100872 netdev_err(priv->dev, "Could not attach to PHY\n");
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300873 if (!phydev)
874 return -ENODEV;
875
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700876 return PTR_ERR(phydev);
877 }
878
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000879 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000880 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000881 (interface == PHY_INTERFACE_MODE_RMII) ||
Pavel Macheka77e4ac2014-08-25 13:31:16 +0200882 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000883 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
884 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000885
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700886 /*
887 * Broken HW is sometimes missing the pull-up resistor on the
888 * MDIO line, which results in reads to non-existent devices returning
889 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
890 * device as well.
891 * Note: phydev->phy_id is the result of reading the UID PHY registers.
892 */
Mathieu Olivari27732382015-05-27 11:02:48 -0700893 if (!priv->plat->phy_node && phydev->phy_id == 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700894 phy_disconnect(phydev);
895 return -ENODEV;
896 }
Giuseppe Cavallaro8e99fc52016-02-29 14:27:39 +0100897
Florian Fainellic51e4242016-11-13 17:50:35 -0800898 /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
899 * subsequent PHY polling, make sure we force a link transition if
900 * we have a UP/DOWN/UP transition
901 */
902 if (phydev->is_pseudo_fixed_link)
903 phydev->irq = PHY_POLL;
904
LABBE Corentinb05c76a2017-02-08 09:31:18 +0100905 phy_attached_info(phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700906 return 0;
907}
908
Joao Pinto71fedb02017-04-06 09:49:08 +0100909static void stmmac_display_rx_rings(struct stmmac_priv *priv)
910{
Joao Pinto54139cf2017-04-06 09:49:09 +0100911 u32 rx_cnt = priv->plat->rx_queues_to_use;
Joao Pinto71fedb02017-04-06 09:49:08 +0100912 void *head_rx;
Joao Pinto54139cf2017-04-06 09:49:09 +0100913 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +0100914
Joao Pinto54139cf2017-04-06 09:49:09 +0100915 /* Display RX rings */
916 for (queue = 0; queue < rx_cnt; queue++) {
917 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +0100918
Joao Pinto54139cf2017-04-06 09:49:09 +0100919 pr_info("\tRX Queue %u rings\n", queue);
920
921 if (priv->extend_desc)
922 head_rx = (void *)rx_q->dma_erx;
923 else
924 head_rx = (void *)rx_q->dma_rx;
925
926 /* Display RX ring */
927 priv->hw->desc->display_ring(head_rx, DMA_RX_SIZE, true);
928 }
Joao Pinto71fedb02017-04-06 09:49:08 +0100929}
930
931static void stmmac_display_tx_rings(struct stmmac_priv *priv)
932{
Joao Pintoce736782017-04-06 09:49:10 +0100933 u32 tx_cnt = priv->plat->tx_queues_to_use;
Joao Pinto71fedb02017-04-06 09:49:08 +0100934 void *head_tx;
Joao Pintoce736782017-04-06 09:49:10 +0100935 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +0100936
Joao Pintoce736782017-04-06 09:49:10 +0100937 /* Display TX rings */
938 for (queue = 0; queue < tx_cnt; queue++) {
939 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +0100940
Joao Pintoce736782017-04-06 09:49:10 +0100941 pr_info("\tTX Queue %d rings\n", queue);
942
943 if (priv->extend_desc)
944 head_tx = (void *)tx_q->dma_etx;
945 else
946 head_tx = (void *)tx_q->dma_tx;
947
948 priv->hw->desc->display_ring(head_tx, DMA_TX_SIZE, false);
949 }
Joao Pinto71fedb02017-04-06 09:49:08 +0100950}
951
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000952static void stmmac_display_rings(struct stmmac_priv *priv)
953{
Joao Pinto71fedb02017-04-06 09:49:08 +0100954 /* Display RX ring */
955 stmmac_display_rx_rings(priv);
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200956
Joao Pinto71fedb02017-04-06 09:49:08 +0100957 /* Display TX ring */
958 stmmac_display_tx_rings(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000959}
960
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000961static int stmmac_set_bfsize(int mtu, int bufsize)
962{
963 int ret = bufsize;
964
965 if (mtu >= BUF_SIZE_4KiB)
966 ret = BUF_SIZE_8KiB;
967 else if (mtu >= BUF_SIZE_2KiB)
968 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100969 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000970 ret = BUF_SIZE_2KiB;
971 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100972 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000973
974 return ret;
975}
976
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000977/**
Joao Pinto71fedb02017-04-06 09:49:08 +0100978 * stmmac_clear_rx_descriptors - clear RX descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000979 * @priv: driver private structure
Joao Pinto54139cf2017-04-06 09:49:09 +0100980 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +0100981 * Description: this function is called to clear the RX descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000982 * in case of both basic and extended descriptors are used.
983 */
Joao Pinto54139cf2017-04-06 09:49:09 +0100984static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000985{
Joao Pinto54139cf2017-04-06 09:49:09 +0100986 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
LABBE Corentin5bacd772017-03-29 07:05:40 +0200987 int i;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000988
Joao Pinto71fedb02017-04-06 09:49:08 +0100989 /* Clear the RX descriptors */
LABBE Corentin5bacd772017-03-29 07:05:40 +0200990 for (i = 0; i < DMA_RX_SIZE; i++)
991 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +0100992 priv->hw->desc->init_rx_desc(&rx_q->dma_erx[i].basic,
LABBE Corentin5bacd772017-03-29 07:05:40 +0200993 priv->use_riwt, priv->mode,
994 (i == DMA_RX_SIZE - 1));
995 else
Joao Pinto54139cf2017-04-06 09:49:09 +0100996 priv->hw->desc->init_rx_desc(&rx_q->dma_rx[i],
LABBE Corentin5bacd772017-03-29 07:05:40 +0200997 priv->use_riwt, priv->mode,
998 (i == DMA_RX_SIZE - 1));
Joao Pinto71fedb02017-04-06 09:49:08 +0100999}
1000
1001/**
1002 * stmmac_clear_tx_descriptors - clear tx descriptors
1003 * @priv: driver private structure
Joao Pintoce736782017-04-06 09:49:10 +01001004 * @queue: TX queue index.
Joao Pinto71fedb02017-04-06 09:49:08 +01001005 * Description: this function is called to clear the TX descriptors
1006 * in case of both basic and extended descriptors are used.
1007 */
Joao Pintoce736782017-04-06 09:49:10 +01001008static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
Joao Pinto71fedb02017-04-06 09:49:08 +01001009{
Joao Pintoce736782017-04-06 09:49:10 +01001010 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001011 int i;
1012
1013 /* Clear the TX descriptors */
LABBE Corentin5bacd772017-03-29 07:05:40 +02001014 for (i = 0; i < DMA_TX_SIZE; i++)
1015 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01001016 priv->hw->desc->init_tx_desc(&tx_q->dma_etx[i].basic,
LABBE Corentin5bacd772017-03-29 07:05:40 +02001017 priv->mode,
1018 (i == DMA_TX_SIZE - 1));
1019 else
Joao Pintoce736782017-04-06 09:49:10 +01001020 priv->hw->desc->init_tx_desc(&tx_q->dma_tx[i],
LABBE Corentin5bacd772017-03-29 07:05:40 +02001021 priv->mode,
1022 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001023}
1024
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001025/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001026 * stmmac_clear_descriptors - clear descriptors
1027 * @priv: driver private structure
1028 * Description: this function is called to clear the TX and RX descriptors
1029 * in case of both basic and extended descriptors are used.
1030 */
1031static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1032{
Joao Pinto54139cf2017-04-06 09:49:09 +01001033 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
Joao Pintoce736782017-04-06 09:49:10 +01001034 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01001035 u32 queue;
1036
Joao Pinto71fedb02017-04-06 09:49:08 +01001037 /* Clear the RX descriptors */
Joao Pinto54139cf2017-04-06 09:49:09 +01001038 for (queue = 0; queue < rx_queue_cnt; queue++)
1039 stmmac_clear_rx_descriptors(priv, queue);
Joao Pinto71fedb02017-04-06 09:49:08 +01001040
1041 /* Clear the TX descriptors */
Joao Pintoce736782017-04-06 09:49:10 +01001042 for (queue = 0; queue < tx_queue_cnt; queue++)
1043 stmmac_clear_tx_descriptors(priv, queue);
Joao Pinto71fedb02017-04-06 09:49:08 +01001044}
1045
1046/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001047 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1048 * @priv: driver private structure
1049 * @p: descriptor pointer
1050 * @i: descriptor index
Joao Pinto54139cf2017-04-06 09:49:09 +01001051 * @flags: gfp flag
1052 * @queue: RX queue index
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001053 * Description: this function is called to allocate a receive buffer, perform
1054 * the DMA mapping and init the descriptor.
1055 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001056static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
Joao Pinto54139cf2017-04-06 09:49:09 +01001057 int i, gfp_t flags, u32 queue)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001058{
Joao Pinto54139cf2017-04-06 09:49:09 +01001059 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001060 struct sk_buff *skb;
1061
Vineet Gupta4ec49a32015-05-20 12:04:40 +05301062 skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001063 if (!skb) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01001064 netdev_err(priv->dev,
1065 "%s: Rx init fails; skb is NULL\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001066 return -ENOMEM;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001067 }
Joao Pinto54139cf2017-04-06 09:49:09 +01001068 rx_q->rx_skbuff[i] = skb;
1069 rx_q->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001070 priv->dma_buf_sz,
1071 DMA_FROM_DEVICE);
Joao Pinto54139cf2017-04-06 09:49:09 +01001072 if (dma_mapping_error(priv->device, rx_q->rx_skbuff_dma[i])) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01001073 netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001074 dev_kfree_skb_any(skb);
1075 return -EINVAL;
1076 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001077
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001078 if (priv->synopsys_id >= DWMAC_CORE_4_00)
Joao Pinto54139cf2017-04-06 09:49:09 +01001079 p->des0 = cpu_to_le32(rx_q->rx_skbuff_dma[i]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001080 else
Joao Pinto54139cf2017-04-06 09:49:09 +01001081 p->des2 = cpu_to_le32(rx_q->rx_skbuff_dma[i]);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001082
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001083 if ((priv->hw->mode->init_desc3) &&
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001084 (priv->dma_buf_sz == BUF_SIZE_16KiB))
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001085 priv->hw->mode->init_desc3(p);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001086
1087 return 0;
1088}
1089
Joao Pinto71fedb02017-04-06 09:49:08 +01001090/**
1091 * stmmac_free_rx_buffer - free RX dma buffers
1092 * @priv: private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01001093 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001094 * @i: buffer index.
1095 */
Joao Pinto54139cf2017-04-06 09:49:09 +01001096static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001097{
Joao Pinto54139cf2017-04-06 09:49:09 +01001098 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1099
1100 if (rx_q->rx_skbuff[i]) {
1101 dma_unmap_single(priv->device, rx_q->rx_skbuff_dma[i],
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001102 priv->dma_buf_sz, DMA_FROM_DEVICE);
Joao Pinto54139cf2017-04-06 09:49:09 +01001103 dev_kfree_skb_any(rx_q->rx_skbuff[i]);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001104 }
Joao Pinto54139cf2017-04-06 09:49:09 +01001105 rx_q->rx_skbuff[i] = NULL;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001106}
1107
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001108/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001109 * stmmac_free_tx_buffer - free RX dma buffers
1110 * @priv: private structure
Joao Pintoce736782017-04-06 09:49:10 +01001111 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001112 * @i: buffer index.
1113 */
Joao Pintoce736782017-04-06 09:49:10 +01001114static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
Joao Pinto71fedb02017-04-06 09:49:08 +01001115{
Joao Pintoce736782017-04-06 09:49:10 +01001116 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1117
1118 if (tx_q->tx_skbuff_dma[i].buf) {
1119 if (tx_q->tx_skbuff_dma[i].map_as_page)
Joao Pinto71fedb02017-04-06 09:49:08 +01001120 dma_unmap_page(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001121 tx_q->tx_skbuff_dma[i].buf,
1122 tx_q->tx_skbuff_dma[i].len,
Joao Pinto71fedb02017-04-06 09:49:08 +01001123 DMA_TO_DEVICE);
1124 else
1125 dma_unmap_single(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001126 tx_q->tx_skbuff_dma[i].buf,
1127 tx_q->tx_skbuff_dma[i].len,
Joao Pinto71fedb02017-04-06 09:49:08 +01001128 DMA_TO_DEVICE);
1129 }
1130
Joao Pintoce736782017-04-06 09:49:10 +01001131 if (tx_q->tx_skbuff[i]) {
1132 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1133 tx_q->tx_skbuff[i] = NULL;
1134 tx_q->tx_skbuff_dma[i].buf = 0;
1135 tx_q->tx_skbuff_dma[i].map_as_page = false;
Joao Pinto71fedb02017-04-06 09:49:08 +01001136 }
1137}
1138
1139/**
1140 * init_dma_rx_desc_rings - init the RX descriptor rings
Joao Pintoaff3d9e2017-03-17 16:11:05 +00001141 * @dev: net device structure
1142 * @flags: gfp flag.
Joao Pinto71fedb02017-04-06 09:49:08 +01001143 * Description: this function initializes the DMA RX descriptors
LABBE Corentin5bacd772017-03-29 07:05:40 +02001144 * and allocates the socket buffers. It supports the chained and ring
Joao Pintoaff3d9e2017-03-17 16:11:05 +00001145 * modes.
1146 */
Joao Pinto71fedb02017-04-06 09:49:08 +01001147static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
Joao Pintoaff3d9e2017-03-17 16:11:05 +00001148{
1149 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto54139cf2017-04-06 09:49:09 +01001150 u32 rx_count = priv->plat->rx_queues_to_use;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001151 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001152 int ret = -ENOMEM;
Joao Pinto54139cf2017-04-06 09:49:09 +01001153 u32 queue;
1154 int i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001155
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001156 if (priv->hw->mode->set_16kib_bfsize)
1157 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001158
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001159 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001160 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001161
Vince Bridgers2618abb2014-01-20 05:39:01 -06001162 priv->dma_buf_sz = bfsize;
1163
Joao Pinto54139cf2017-04-06 09:49:09 +01001164 /* RX INITIALIZATION */
LABBE Corentinb3e51062016-11-16 20:09:41 +01001165 netif_dbg(priv, probe, priv->dev,
1166 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1167
Joao Pinto54139cf2017-04-06 09:49:09 +01001168 for (queue = 0; queue < rx_count; queue++) {
1169 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001170
Joao Pinto54139cf2017-04-06 09:49:09 +01001171 netif_dbg(priv, probe, priv->dev,
1172 "(%s) dma_rx_phy=0x%08x\n", __func__,
1173 (u32)rx_q->dma_rx_phy);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001174
Joao Pinto54139cf2017-04-06 09:49:09 +01001175 for (i = 0; i < DMA_RX_SIZE; i++) {
1176 struct dma_desc *p;
1177
1178 if (priv->extend_desc)
1179 p = &((rx_q->dma_erx + i)->basic);
1180 else
1181 p = rx_q->dma_rx + i;
1182
1183 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1184 queue);
1185 if (ret)
1186 goto err_init_rx_buffers;
1187
1188 netif_dbg(priv, probe, priv->dev, "[%p]\t[%p]\t[%x]\n",
1189 rx_q->rx_skbuff[i], rx_q->rx_skbuff[i]->data,
1190 (unsigned int)rx_q->rx_skbuff_dma[i]);
1191 }
1192
1193 rx_q->cur_rx = 0;
1194 rx_q->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
1195
1196 stmmac_clear_rx_descriptors(priv, queue);
1197
1198 /* Setup the chained descriptor addresses */
1199 if (priv->mode == STMMAC_CHAIN_MODE) {
1200 if (priv->extend_desc)
1201 priv->hw->mode->init(rx_q->dma_erx,
1202 rx_q->dma_rx_phy,
1203 DMA_RX_SIZE, 1);
1204 else
1205 priv->hw->mode->init(rx_q->dma_rx,
1206 rx_q->dma_rx_phy,
1207 DMA_RX_SIZE, 0);
1208 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001209 }
Joao Pinto54139cf2017-04-06 09:49:09 +01001210
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001211 buf_sz = bfsize;
1212
Joao Pinto54139cf2017-04-06 09:49:09 +01001213 return 0;
1214
1215err_init_rx_buffers:
1216 while (queue >= 0) {
1217 while (--i >= 0)
1218 stmmac_free_rx_buffer(priv, queue, i);
1219
1220 if (queue == 0)
1221 break;
1222
1223 i = DMA_RX_SIZE;
1224 queue--;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001225 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001226
Joao Pinto71fedb02017-04-06 09:49:08 +01001227 return ret;
1228}
1229
1230/**
1231 * init_dma_tx_desc_rings - init the TX descriptor rings
1232 * @dev: net device structure.
1233 * Description: this function initializes the DMA TX descriptors
1234 * and allocates the socket buffers. It supports the chained and ring
1235 * modes.
1236 */
1237static int init_dma_tx_desc_rings(struct net_device *dev)
1238{
1239 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pintoce736782017-04-06 09:49:10 +01001240 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1241 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +01001242 int i;
1243
Joao Pintoce736782017-04-06 09:49:10 +01001244 for (queue = 0; queue < tx_queue_cnt; queue++) {
1245 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001246
Joao Pintoce736782017-04-06 09:49:10 +01001247 netif_dbg(priv, probe, priv->dev,
1248 "(%s) dma_tx_phy=0x%08x\n", __func__,
1249 (u32)tx_q->dma_tx_phy);
Joao Pinto71fedb02017-04-06 09:49:08 +01001250
Joao Pintoce736782017-04-06 09:49:10 +01001251 /* Setup the chained descriptor addresses */
1252 if (priv->mode == STMMAC_CHAIN_MODE) {
1253 if (priv->extend_desc)
1254 priv->hw->mode->init(tx_q->dma_etx,
1255 tx_q->dma_tx_phy,
1256 DMA_TX_SIZE, 1);
1257 else
1258 priv->hw->mode->init(tx_q->dma_tx,
1259 tx_q->dma_tx_phy,
1260 DMA_TX_SIZE, 0);
LABBE Corentin5bacd772017-03-29 07:05:40 +02001261 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001262
Joao Pintoce736782017-04-06 09:49:10 +01001263 for (i = 0; i < DMA_TX_SIZE; i++) {
1264 struct dma_desc *p;
1265
1266 if (priv->extend_desc)
1267 p = &((tx_q->dma_etx + i)->basic);
1268 else
1269 p = tx_q->dma_tx + i;
1270
1271 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1272 p->des0 = 0;
1273 p->des1 = 0;
1274 p->des2 = 0;
1275 p->des3 = 0;
1276 } else {
1277 p->des2 = 0;
1278 }
1279
1280 tx_q->tx_skbuff_dma[i].buf = 0;
1281 tx_q->tx_skbuff_dma[i].map_as_page = false;
1282 tx_q->tx_skbuff_dma[i].len = 0;
1283 tx_q->tx_skbuff_dma[i].last_segment = false;
1284 tx_q->tx_skbuff[i] = NULL;
1285 }
1286
1287 tx_q->dirty_tx = 0;
1288 tx_q->cur_tx = 0;
LABBE Corentin5bacd772017-03-29 07:05:40 +02001289 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001290
LABBE Corentin5bacd772017-03-29 07:05:40 +02001291 netdev_reset_queue(priv->dev);
1292
Joao Pinto71fedb02017-04-06 09:49:08 +01001293 return 0;
1294}
1295
1296/**
1297 * init_dma_desc_rings - init the RX/TX descriptor rings
1298 * @dev: net device structure
1299 * @flags: gfp flag.
1300 * Description: this function initializes the DMA RX/TX descriptors
1301 * and allocates the socket buffers. It supports the chained and ring
1302 * modes.
1303 */
1304static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1305{
1306 struct stmmac_priv *priv = netdev_priv(dev);
1307 int ret;
1308
1309 ret = init_dma_rx_desc_rings(dev, flags);
1310 if (ret)
1311 return ret;
1312
1313 ret = init_dma_tx_desc_rings(dev);
1314
LABBE Corentin5bacd772017-03-29 07:05:40 +02001315 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001316
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001317 if (netif_msg_hw(priv))
1318 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001319
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001320 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001321}
1322
Joao Pinto71fedb02017-04-06 09:49:08 +01001323/**
1324 * dma_free_rx_skbufs - free RX dma buffers
1325 * @priv: private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01001326 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001327 */
Joao Pinto54139cf2017-04-06 09:49:09 +01001328static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001329{
1330 int i;
1331
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001332 for (i = 0; i < DMA_RX_SIZE; i++)
Joao Pinto54139cf2017-04-06 09:49:09 +01001333 stmmac_free_rx_buffer(priv, queue, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001334}
1335
Joao Pinto71fedb02017-04-06 09:49:08 +01001336/**
1337 * dma_free_tx_skbufs - free TX dma buffers
1338 * @priv: private structure
Joao Pintoce736782017-04-06 09:49:10 +01001339 * @queue: TX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001340 */
Joao Pintoce736782017-04-06 09:49:10 +01001341static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001342{
1343 int i;
1344
Joao Pinto71fedb02017-04-06 09:49:08 +01001345 for (i = 0; i < DMA_TX_SIZE; i++)
Joao Pintoce736782017-04-06 09:49:10 +01001346 stmmac_free_tx_buffer(priv, queue, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001347}
1348
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001349/**
Joao Pinto54139cf2017-04-06 09:49:09 +01001350 * free_dma_rx_desc_resources - free RX dma desc resources
1351 * @priv: private structure
1352 */
1353static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1354{
1355 u32 rx_count = priv->plat->rx_queues_to_use;
1356 u32 queue;
1357
1358 /* Free RX queue resources */
1359 for (queue = 0; queue < rx_count; queue++) {
1360 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1361
1362 /* Release the DMA RX socket buffers */
1363 dma_free_rx_skbufs(priv, queue);
1364
1365 /* Free DMA regions of consistent memory previously allocated */
1366 if (!priv->extend_desc)
1367 dma_free_coherent(priv->device,
1368 DMA_RX_SIZE * sizeof(struct dma_desc),
1369 rx_q->dma_rx, rx_q->dma_rx_phy);
1370 else
1371 dma_free_coherent(priv->device, DMA_RX_SIZE *
1372 sizeof(struct dma_extended_desc),
1373 rx_q->dma_erx, rx_q->dma_rx_phy);
1374
1375 kfree(rx_q->rx_skbuff_dma);
1376 kfree(rx_q->rx_skbuff);
1377 }
1378}
1379
1380/**
Joao Pintoce736782017-04-06 09:49:10 +01001381 * free_dma_tx_desc_resources - free TX dma desc resources
1382 * @priv: private structure
1383 */
1384static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1385{
1386 u32 tx_count = priv->plat->tx_queues_to_use;
1387 u32 queue = 0;
1388
1389 /* Free TX queue resources */
1390 for (queue = 0; queue < tx_count; queue++) {
1391 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1392
1393 /* Release the DMA TX socket buffers */
1394 dma_free_tx_skbufs(priv, queue);
1395
1396 /* Free DMA regions of consistent memory previously allocated */
1397 if (!priv->extend_desc)
1398 dma_free_coherent(priv->device,
1399 DMA_TX_SIZE * sizeof(struct dma_desc),
1400 tx_q->dma_tx, tx_q->dma_tx_phy);
1401 else
1402 dma_free_coherent(priv->device, DMA_TX_SIZE *
1403 sizeof(struct dma_extended_desc),
1404 tx_q->dma_etx, tx_q->dma_tx_phy);
1405
1406 kfree(tx_q->tx_skbuff_dma);
1407 kfree(tx_q->tx_skbuff);
1408 }
1409}
1410
1411/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001412 * alloc_dma_rx_desc_resources - alloc RX resources.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001413 * @priv: private structure
1414 * Description: according to which descriptor can be used (extend or basic)
1415 * this function allocates the resources for TX and RX paths. In case of
1416 * reception, for example, it pre-allocated the RX socket buffer in order to
1417 * allow zero-copy mechanism.
1418 */
Joao Pinto71fedb02017-04-06 09:49:08 +01001419static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001420{
Joao Pinto54139cf2017-04-06 09:49:09 +01001421 u32 rx_count = priv->plat->rx_queues_to_use;
LABBE Corentin5bacd772017-03-29 07:05:40 +02001422 int ret = -ENOMEM;
Joao Pinto54139cf2017-04-06 09:49:09 +01001423 u32 queue;
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001424
Joao Pinto54139cf2017-04-06 09:49:09 +01001425 /* RX queues buffers and DMA */
1426 for (queue = 0; queue < rx_count; queue++) {
1427 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001428
Joao Pinto54139cf2017-04-06 09:49:09 +01001429 rx_q->queue_index = queue;
1430 rx_q->priv_data = priv;
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001431
Joao Pinto54139cf2017-04-06 09:49:09 +01001432 rx_q->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE,
1433 sizeof(dma_addr_t),
LABBE Corentin5bacd772017-03-29 07:05:40 +02001434 GFP_KERNEL);
Joao Pinto54139cf2017-04-06 09:49:09 +01001435 if (!rx_q->rx_skbuff_dma)
1436 return -ENOMEM;
1437
1438 rx_q->rx_skbuff = kmalloc_array(DMA_RX_SIZE,
1439 sizeof(struct sk_buff *),
1440 GFP_KERNEL);
1441 if (!rx_q->rx_skbuff)
LABBE Corentin5bacd772017-03-29 07:05:40 +02001442 goto err_dma;
1443
Joao Pinto54139cf2017-04-06 09:49:09 +01001444 if (priv->extend_desc) {
1445 rx_q->dma_erx = dma_zalloc_coherent(priv->device,
1446 DMA_RX_SIZE *
1447 sizeof(struct
1448 dma_extended_desc),
1449 &rx_q->dma_rx_phy,
1450 GFP_KERNEL);
1451 if (!rx_q->dma_erx)
1452 goto err_dma;
1453
1454 } else {
1455 rx_q->dma_rx = dma_zalloc_coherent(priv->device,
1456 DMA_RX_SIZE *
1457 sizeof(struct
1458 dma_desc),
1459 &rx_q->dma_rx_phy,
1460 GFP_KERNEL);
1461 if (!rx_q->dma_rx)
1462 goto err_dma;
1463 }
Joao Pinto71fedb02017-04-06 09:49:08 +01001464 }
1465
1466 return 0;
1467
1468err_dma:
Joao Pinto54139cf2017-04-06 09:49:09 +01001469 free_dma_rx_desc_resources(priv);
1470
Joao Pinto71fedb02017-04-06 09:49:08 +01001471 return ret;
1472}
1473
1474/**
1475 * alloc_dma_tx_desc_resources - alloc TX resources.
1476 * @priv: private structure
1477 * Description: according to which descriptor can be used (extend or basic)
1478 * this function allocates the resources for TX and RX paths. In case of
1479 * reception, for example, it pre-allocated the RX socket buffer in order to
1480 * allow zero-copy mechanism.
1481 */
1482static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
1483{
Joao Pintoce736782017-04-06 09:49:10 +01001484 u32 tx_count = priv->plat->tx_queues_to_use;
Joao Pinto71fedb02017-04-06 09:49:08 +01001485 int ret = -ENOMEM;
Joao Pintoce736782017-04-06 09:49:10 +01001486 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +01001487
Joao Pintoce736782017-04-06 09:49:10 +01001488 /* TX queues buffers and DMA */
1489 for (queue = 0; queue < tx_count; queue++) {
1490 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001491
Joao Pintoce736782017-04-06 09:49:10 +01001492 tx_q->queue_index = queue;
1493 tx_q->priv_data = priv;
Joao Pinto71fedb02017-04-06 09:49:08 +01001494
Joao Pintoce736782017-04-06 09:49:10 +01001495 tx_q->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
1496 sizeof(*tx_q->tx_skbuff_dma),
LABBE Corentin5bacd772017-03-29 07:05:40 +02001497 GFP_KERNEL);
Joao Pintoce736782017-04-06 09:49:10 +01001498 if (!tx_q->tx_skbuff_dma)
1499 return -ENOMEM;
1500
1501 tx_q->tx_skbuff = kmalloc_array(DMA_TX_SIZE,
1502 sizeof(struct sk_buff *),
1503 GFP_KERNEL);
1504 if (!tx_q->tx_skbuff)
1505 goto err_dma_buffers;
1506
1507 if (priv->extend_desc) {
1508 tx_q->dma_etx = dma_zalloc_coherent(priv->device,
1509 DMA_TX_SIZE *
1510 sizeof(struct
1511 dma_extended_desc),
1512 &tx_q->dma_tx_phy,
1513 GFP_KERNEL);
1514 if (!tx_q->dma_etx)
1515 goto err_dma_buffers;
1516 } else {
1517 tx_q->dma_tx = dma_zalloc_coherent(priv->device,
1518 DMA_TX_SIZE *
1519 sizeof(struct
1520 dma_desc),
1521 &tx_q->dma_tx_phy,
1522 GFP_KERNEL);
1523 if (!tx_q->dma_tx)
1524 goto err_dma_buffers;
1525 }
LABBE Corentin5bacd772017-03-29 07:05:40 +02001526 }
1527
1528 return 0;
1529
Joao Pintoce736782017-04-06 09:49:10 +01001530err_dma_buffers:
1531 free_dma_tx_desc_resources(priv);
1532
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001533 return ret;
1534}
1535
Joao Pinto71fedb02017-04-06 09:49:08 +01001536/**
1537 * alloc_dma_desc_resources - alloc TX/RX resources.
1538 * @priv: private structure
1539 * Description: according to which descriptor can be used (extend or basic)
1540 * this function allocates the resources for TX and RX paths. In case of
1541 * reception, for example, it pre-allocated the RX socket buffer in order to
1542 * allow zero-copy mechanism.
1543 */
1544static int alloc_dma_desc_resources(struct stmmac_priv *priv)
LABBE Corentin5bacd772017-03-29 07:05:40 +02001545{
Joao Pinto54139cf2017-04-06 09:49:09 +01001546 /* RX Allocation */
Joao Pinto71fedb02017-04-06 09:49:08 +01001547 int ret = alloc_dma_rx_desc_resources(priv);
1548
1549 if (ret)
1550 return ret;
1551
1552 ret = alloc_dma_tx_desc_resources(priv);
1553
1554 return ret;
1555}
1556
1557/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001558 * free_dma_desc_resources - free dma desc resources
1559 * @priv: private structure
1560 */
1561static void free_dma_desc_resources(struct stmmac_priv *priv)
1562{
1563 /* Release the DMA RX socket buffers */
1564 free_dma_rx_desc_resources(priv);
1565
1566 /* Release the DMA TX socket buffers */
1567 free_dma_tx_desc_resources(priv);
1568}
1569
1570/**
jpinto9eb12472016-12-28 12:57:48 +00001571 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
1572 * @priv: driver private structure
1573 * Description: It is used for enabling the rx queues in the MAC
1574 */
1575static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
1576{
Joao Pinto4f6046f2017-03-10 18:24:54 +00001577 u32 rx_queues_count = priv->plat->rx_queues_to_use;
1578 int queue;
1579 u8 mode;
jpinto9eb12472016-12-28 12:57:48 +00001580
Joao Pinto4f6046f2017-03-10 18:24:54 +00001581 for (queue = 0; queue < rx_queues_count; queue++) {
1582 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
1583 priv->hw->mac->rx_queue_enable(priv->hw, mode, queue);
1584 }
jpinto9eb12472016-12-28 12:57:48 +00001585}
1586
1587/**
Joao Pintoae4f0d42017-03-15 11:04:47 +00001588 * stmmac_start_rx_dma - start RX DMA channel
1589 * @priv: driver private structure
1590 * @chan: RX channel index
1591 * Description:
1592 * This starts a RX DMA channel
1593 */
1594static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
1595{
1596 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
1597 priv->hw->dma->start_rx(priv->ioaddr, chan);
1598}
1599
1600/**
1601 * stmmac_start_tx_dma - start TX DMA channel
1602 * @priv: driver private structure
1603 * @chan: TX channel index
1604 * Description:
1605 * This starts a TX DMA channel
1606 */
1607static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
1608{
1609 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
1610 priv->hw->dma->start_tx(priv->ioaddr, chan);
1611}
1612
1613/**
1614 * stmmac_stop_rx_dma - stop RX DMA channel
1615 * @priv: driver private structure
1616 * @chan: RX channel index
1617 * Description:
1618 * This stops a RX DMA channel
1619 */
1620static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
1621{
1622 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
1623 priv->hw->dma->stop_rx(priv->ioaddr, chan);
1624}
1625
1626/**
1627 * stmmac_stop_tx_dma - stop TX DMA channel
1628 * @priv: driver private structure
1629 * @chan: TX channel index
1630 * Description:
1631 * This stops a TX DMA channel
1632 */
1633static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
1634{
1635 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
1636 priv->hw->dma->stop_tx(priv->ioaddr, chan);
1637}
1638
1639/**
1640 * stmmac_start_all_dma - start all RX and TX DMA channels
1641 * @priv: driver private structure
1642 * Description:
1643 * This starts all the RX and TX DMA channels
1644 */
1645static void stmmac_start_all_dma(struct stmmac_priv *priv)
1646{
1647 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1648 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1649 u32 chan = 0;
1650
1651 for (chan = 0; chan < rx_channels_count; chan++)
1652 stmmac_start_rx_dma(priv, chan);
1653
1654 for (chan = 0; chan < tx_channels_count; chan++)
1655 stmmac_start_tx_dma(priv, chan);
1656}
1657
1658/**
1659 * stmmac_stop_all_dma - stop all RX and TX DMA channels
1660 * @priv: driver private structure
1661 * Description:
1662 * This stops the RX and TX DMA channels
1663 */
1664static void stmmac_stop_all_dma(struct stmmac_priv *priv)
1665{
1666 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1667 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1668 u32 chan = 0;
1669
1670 for (chan = 0; chan < rx_channels_count; chan++)
1671 stmmac_stop_rx_dma(priv, chan);
1672
1673 for (chan = 0; chan < tx_channels_count; chan++)
1674 stmmac_stop_tx_dma(priv, chan);
1675}
1676
1677/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001678 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001679 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001680 * Description: it is used for configuring the DMA operation mode register in
1681 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001682 */
1683static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1684{
Joao Pinto6deee222017-03-15 11:04:45 +00001685 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1686 u32 tx_channels_count = priv->plat->tx_queues_to_use;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001687 int rxfifosz = priv->plat->rx_fifo_size;
Joao Pinto6deee222017-03-15 11:04:45 +00001688 u32 txmode = 0;
1689 u32 rxmode = 0;
1690 u32 chan = 0;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001691
Thierry Reding11fbf812017-03-10 17:34:58 +01001692 if (rxfifosz == 0)
1693 rxfifosz = priv->dma_cap.rx_fifo_size;
1694
Joao Pinto6deee222017-03-15 11:04:45 +00001695 if (priv->plat->force_thresh_dma_mode) {
1696 txmode = tc;
1697 rxmode = tc;
1698 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001699 /*
1700 * In case of GMAC, SF mode can be enabled
1701 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001702 * 1) TX COE if actually supported
1703 * 2) There is no bugged Jumbo frame support
1704 * that needs to not insert csum in the TDES.
1705 */
Joao Pinto6deee222017-03-15 11:04:45 +00001706 txmode = SF_DMA_MODE;
1707 rxmode = SF_DMA_MODE;
Sonic Zhangb2dec112015-01-30 13:49:32 +08001708 priv->xstats.threshold = SF_DMA_MODE;
Joao Pinto6deee222017-03-15 11:04:45 +00001709 } else {
1710 txmode = tc;
1711 rxmode = SF_DMA_MODE;
1712 }
1713
1714 /* configure all channels */
1715 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1716 for (chan = 0; chan < rx_channels_count; chan++)
1717 priv->hw->dma->dma_rx_mode(priv->ioaddr, rxmode, chan,
1718 rxfifosz);
1719
1720 for (chan = 0; chan < tx_channels_count; chan++)
1721 priv->hw->dma->dma_tx_mode(priv->ioaddr, txmode, chan);
1722 } else {
1723 priv->hw->dma->dma_mode(priv->ioaddr, txmode, rxmode,
Vince Bridgersf88203a2015-04-15 11:17:42 -05001724 rxfifosz);
Joao Pinto6deee222017-03-15 11:04:45 +00001725 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001726}
1727
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001728/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001729 * stmmac_tx_clean - to manage the transmission completion
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001730 * @priv: driver private structure
Joao Pintoce736782017-04-06 09:49:10 +01001731 * @queue: TX queue index
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001732 * Description: it reclaims the transmit resources after transmission completes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001733 */
Joao Pintoce736782017-04-06 09:49:10 +01001734static void stmmac_tx_clean(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001735{
Joao Pintoce736782017-04-06 09:49:10 +01001736 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Beniamino Galvani38979572015-01-21 19:07:27 +01001737 unsigned int bytes_compl = 0, pkts_compl = 0;
Joao Pintoce736782017-04-06 09:49:10 +01001738 unsigned int entry = tx_q->dirty_tx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001739
Lino Sanfilippo739c8e12016-12-09 00:55:43 +01001740 netif_tx_lock(priv->dev);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001741
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001742 priv->xstats.tx_clean++;
1743
Joao Pintoce736782017-04-06 09:49:10 +01001744 while (entry != tx_q->cur_tx) {
1745 struct sk_buff *skb = tx_q->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001746 struct dma_desc *p;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001747 int status;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001748
1749 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01001750 p = (struct dma_desc *)(tx_q->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001751 else
Joao Pintoce736782017-04-06 09:49:10 +01001752 p = tx_q->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001753
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001754 status = priv->hw->desc->tx_status(&priv->dev->stats,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001755 &priv->xstats, p,
1756 priv->ioaddr);
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001757 /* Check if the descriptor is owned by the DMA */
1758 if (unlikely(status & tx_dma_own))
1759 break;
1760
1761 /* Just consider the last segment and ...*/
1762 if (likely(!(status & tx_not_ls))) {
1763 /* ... verify the status error condition */
1764 if (unlikely(status & tx_err)) {
1765 priv->dev->stats.tx_errors++;
1766 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001767 priv->dev->stats.tx_packets++;
1768 priv->xstats.tx_pkt_n++;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001769 }
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001770 stmmac_get_tx_hwtstamp(priv, p, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001771 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001772
Joao Pintoce736782017-04-06 09:49:10 +01001773 if (likely(tx_q->tx_skbuff_dma[entry].buf)) {
1774 if (tx_q->tx_skbuff_dma[entry].map_as_page)
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001775 dma_unmap_page(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001776 tx_q->tx_skbuff_dma[entry].buf,
1777 tx_q->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001778 DMA_TO_DEVICE);
1779 else
1780 dma_unmap_single(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001781 tx_q->tx_skbuff_dma[entry].buf,
1782 tx_q->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001783 DMA_TO_DEVICE);
Joao Pintoce736782017-04-06 09:49:10 +01001784 tx_q->tx_skbuff_dma[entry].buf = 0;
1785 tx_q->tx_skbuff_dma[entry].len = 0;
1786 tx_q->tx_skbuff_dma[entry].map_as_page = false;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001787 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001788
1789 if (priv->hw->mode->clean_desc3)
Joao Pintoce736782017-04-06 09:49:10 +01001790 priv->hw->mode->clean_desc3(tx_q, p);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001791
Joao Pintoce736782017-04-06 09:49:10 +01001792 tx_q->tx_skbuff_dma[entry].last_segment = false;
1793 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001794
1795 if (likely(skb != NULL)) {
Beniamino Galvani38979572015-01-21 19:07:27 +01001796 pkts_compl++;
1797 bytes_compl += skb->len;
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001798 dev_consume_skb_any(skb);
Joao Pintoce736782017-04-06 09:49:10 +01001799 tx_q->tx_skbuff[entry] = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001800 }
1801
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001802 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001803
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001804 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001805 }
Joao Pintoce736782017-04-06 09:49:10 +01001806 tx_q->dirty_tx = entry;
Beniamino Galvani38979572015-01-21 19:07:27 +01001807
LABBE Corentin5bacd772017-03-29 07:05:40 +02001808 netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
Beniamino Galvani38979572015-01-21 19:07:27 +01001809
LABBE Corentin5bacd772017-03-29 07:05:40 +02001810 if (unlikely(netif_queue_stopped(priv->dev) &&
Joao Pintoce736782017-04-06 09:49:10 +01001811 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH)) {
Lino Sanfilippo739c8e12016-12-09 00:55:43 +01001812 netif_dbg(priv, tx_done, priv->dev,
1813 "%s: restart transmit\n", __func__);
LABBE Corentin5bacd772017-03-29 07:05:40 +02001814 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001815 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001816
1817 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1818 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001819 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001820 }
Lino Sanfilippo739c8e12016-12-09 00:55:43 +01001821 netif_tx_unlock(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001822}
1823
Joao Pinto4f513ec2017-03-15 11:04:46 +00001824static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv, u32 chan)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001825{
Joao Pinto4f513ec2017-03-15 11:04:46 +00001826 priv->hw->dma->enable_dma_irq(priv->ioaddr, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001827}
1828
Joao Pinto4f513ec2017-03-15 11:04:46 +00001829static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv, u32 chan)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001830{
Joao Pinto4f513ec2017-03-15 11:04:46 +00001831 priv->hw->dma->disable_dma_irq(priv->ioaddr, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001832}
1833
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001834/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001835 * stmmac_tx_err - to manage the tx error
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001836 * @priv: driver private structure
LABBE Corentin5bacd772017-03-29 07:05:40 +02001837 * @chan: channel index
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001838 * Description: it cleans the descriptors and restarts the transmission
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001839 * in case of transmission errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001840 */
LABBE Corentin5bacd772017-03-29 07:05:40 +02001841static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001842{
Joao Pintoce736782017-04-06 09:49:10 +01001843 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001844 int i;
Joao Pintoce736782017-04-06 09:49:10 +01001845
LABBE Corentin5bacd772017-03-29 07:05:40 +02001846 netif_stop_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001847
Joao Pintoae4f0d42017-03-15 11:04:47 +00001848 stmmac_stop_tx_dma(priv, chan);
Joao Pintoce736782017-04-06 09:49:10 +01001849 dma_free_tx_skbufs(priv, chan);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001850 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001851 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01001852 priv->hw->desc->init_tx_desc(&tx_q->dma_etx[i].basic,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001853 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001854 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001855 else
Joao Pintoce736782017-04-06 09:49:10 +01001856 priv->hw->desc->init_tx_desc(&tx_q->dma_tx[i],
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001857 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001858 (i == DMA_TX_SIZE - 1));
Joao Pintoce736782017-04-06 09:49:10 +01001859 tx_q->dirty_tx = 0;
1860 tx_q->cur_tx = 0;
LABBE Corentin5bacd772017-03-29 07:05:40 +02001861 netdev_reset_queue(priv->dev);
Joao Pintoae4f0d42017-03-15 11:04:47 +00001862 stmmac_start_tx_dma(priv, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001863
1864 priv->dev->stats.tx_errors++;
LABBE Corentin5bacd772017-03-29 07:05:40 +02001865 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001866}
1867
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001868/**
Joao Pinto6deee222017-03-15 11:04:45 +00001869 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
1870 * @priv: driver private structure
1871 * @txmode: TX operating mode
1872 * @rxmode: RX operating mode
1873 * @chan: channel index
1874 * Description: it is used for configuring of the DMA operation mode in
1875 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
1876 * mode.
1877 */
1878static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
1879 u32 rxmode, u32 chan)
1880{
1881 int rxfifosz = priv->plat->rx_fifo_size;
1882
1883 if (rxfifosz == 0)
1884 rxfifosz = priv->dma_cap.rx_fifo_size;
1885
1886 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1887 priv->hw->dma->dma_rx_mode(priv->ioaddr, rxmode, chan,
1888 rxfifosz);
1889 priv->hw->dma->dma_tx_mode(priv->ioaddr, txmode, chan);
1890 } else {
1891 priv->hw->dma->dma_mode(priv->ioaddr, txmode, rxmode,
1892 rxfifosz);
1893 }
1894}
1895
1896/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001897 * stmmac_dma_interrupt - DMA ISR
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001898 * @priv: driver private structure
1899 * Description: this is the DMA ISR. It is called by the main ISR.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001900 * It calls the dwmac dma routine and schedule poll method in case of some
1901 * work can be done.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001902 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001903static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001904{
Joao Pintod62a1072017-03-15 11:04:49 +00001905 u32 tx_channel_count = priv->plat->tx_queues_to_use;
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001906 int status;
Joao Pintod62a1072017-03-15 11:04:49 +00001907 u32 chan;
Joao Pinto68e5cfa2017-03-13 10:36:29 +00001908
Joao Pintod62a1072017-03-15 11:04:49 +00001909 for (chan = 0; chan < tx_channel_count; chan++) {
1910 status = priv->hw->dma->dma_interrupt(priv->ioaddr,
1911 &priv->xstats, chan);
1912 if (likely((status & handle_rx)) || (status & handle_tx)) {
LABBE Corentin5bacd772017-03-29 07:05:40 +02001913 if (likely(napi_schedule_prep(&priv->napi))) {
Joao Pintod62a1072017-03-15 11:04:49 +00001914 stmmac_disable_dma_irq(priv, chan);
LABBE Corentin5bacd772017-03-29 07:05:40 +02001915 __napi_schedule(&priv->napi);
Joao Pintod62a1072017-03-15 11:04:49 +00001916 }
1917 }
1918
1919 if (unlikely(status & tx_hard_error_bump_tc)) {
1920 /* Try to bump up the dma threshold on this failure */
1921 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
1922 (tc <= 256)) {
1923 tc += 64;
1924 if (priv->plat->force_thresh_dma_mode)
1925 stmmac_set_dma_operation_mode(priv,
1926 tc,
1927 tc,
1928 chan);
1929 else
1930 stmmac_set_dma_operation_mode(priv,
1931 tc,
1932 SF_DMA_MODE,
1933 chan);
1934 priv->xstats.threshold = tc;
1935 }
1936 } else if (unlikely(status == tx_hard_error)) {
1937 stmmac_tx_err(priv, chan);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001938 }
1939 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001940}
1941
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001942/**
1943 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1944 * @priv: driver private structure
1945 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1946 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001947static void stmmac_mmc_setup(struct stmmac_priv *priv)
1948{
1949 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001950 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001951
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001952 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1953 priv->ptpaddr = priv->ioaddr + PTP_GMAC4_OFFSET;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001954 priv->mmcaddr = priv->ioaddr + MMC_GMAC4_OFFSET;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001955 } else {
1956 priv->ptpaddr = priv->ioaddr + PTP_GMAC3_X_OFFSET;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001957 priv->mmcaddr = priv->ioaddr + MMC_GMAC3_X_OFFSET;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001958 }
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001959
1960 dwmac_mmc_intr_all_mask(priv->mmcaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001961
1962 if (priv->dma_cap.rmon) {
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001963 dwmac_mmc_ctrl(priv->mmcaddr, mode);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001964 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1965 } else
LABBE Corentin38ddc592016-11-16 20:09:39 +01001966 netdev_info(priv->dev, "No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001967}
1968
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001969/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001970 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001971 * @priv: driver private structure
1972 * Description: select the Enhanced/Alternate or Normal descriptors.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001973 * In case of Enhanced/Alternate, it checks if the extended descriptors are
1974 * supported by the HW capability register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001975 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001976static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1977{
1978 if (priv->plat->enh_desc) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01001979 dev_info(priv->device, "Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001980
1981 /* GMAC older than 3.50 has no extended descriptors */
1982 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01001983 dev_info(priv->device, "Enabled extended descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001984 priv->extend_desc = 1;
1985 } else
LABBE Corentin38ddc592016-11-16 20:09:39 +01001986 dev_warn(priv->device, "Extended descriptors not supported\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001987
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001988 priv->hw->desc = &enh_desc_ops;
1989 } else {
LABBE Corentin38ddc592016-11-16 20:09:39 +01001990 dev_info(priv->device, "Normal descriptors\n");
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001991 priv->hw->desc = &ndesc_ops;
1992 }
1993}
1994
1995/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001996 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001997 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001998 * Description:
1999 * new GMAC chip generations have a new register to indicate the
2000 * presence of the optional feature/functions.
2001 * This can be also used to override the value passed through the
2002 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002003 */
2004static int stmmac_get_hw_features(struct stmmac_priv *priv)
2005{
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02002006 u32 ret = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00002007
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00002008 if (priv->hw->dma->get_hw_feature) {
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02002009 priv->hw->dma->get_hw_feature(priv->ioaddr,
2010 &priv->dma_cap);
2011 ret = 1;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002012 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002013
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02002014 return ret;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002015}
2016
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002017/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002018 * stmmac_check_ether_addr - check if the MAC addr is valid
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002019 * @priv: driver private structure
2020 * Description:
2021 * it is to verify if the MAC address is valid, in case of failures it
2022 * generates a random MAC address
2023 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002024static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2025{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002026 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002027 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002028 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002029 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002030 eth_hw_addr_random(priv->dev);
LABBE Corentin38ddc592016-11-16 20:09:39 +01002031 netdev_info(priv->dev, "device MAC address %pM\n",
2032 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002033 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002034}
2035
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002036/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002037 * stmmac_init_dma_engine - DMA init.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002038 * @priv: driver private structure
2039 * Description:
2040 * It inits the DMA invoking the specific MAC/GMAC callback.
2041 * Some DMA parameters can be passed from the platform;
2042 * in case of these are not passed a default is kept for the MAC or GMAC.
2043 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002044static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2045{
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002046 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2047 u32 tx_channels_count = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01002048 struct stmmac_rx_queue *rx_q;
Joao Pintoce736782017-04-06 09:49:10 +01002049 struct stmmac_tx_queue *tx_q;
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002050 u32 dummy_dma_rx_phy = 0;
2051 u32 dummy_dma_tx_phy = 0;
2052 u32 chan = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002053 int atds = 0;
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01002054 int ret = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002055
Niklas Cassela332e2f2016-12-07 15:20:05 +01002056 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2057 dev_err(priv->device, "Invalid DMA configuration\n");
Niklas Cassel89ab75b2016-12-07 15:20:03 +01002058 return -EINVAL;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002059 }
2060
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002061 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2062 atds = 1;
2063
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01002064 ret = priv->hw->dma->reset(priv->ioaddr);
2065 if (ret) {
2066 dev_err(priv->device, "Failed to reset the dma\n");
2067 return ret;
2068 }
2069
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002070 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002071 /* DMA Configuration */
2072 priv->hw->dma->init(priv->ioaddr, priv->plat->dma_cfg,
2073 dummy_dma_tx_phy, dummy_dma_rx_phy, atds);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002074
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002075 /* DMA RX Channel Configuration */
2076 for (chan = 0; chan < rx_channels_count; chan++) {
Joao Pinto54139cf2017-04-06 09:49:09 +01002077 rx_q = &priv->rx_queue[chan];
2078
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002079 priv->hw->dma->init_rx_chan(priv->ioaddr,
2080 priv->plat->dma_cfg,
Joao Pinto54139cf2017-04-06 09:49:09 +01002081 rx_q->dma_rx_phy, chan);
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002082
Joao Pinto54139cf2017-04-06 09:49:09 +01002083 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002084 (DMA_RX_SIZE * sizeof(struct dma_desc));
2085 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
Joao Pinto54139cf2017-04-06 09:49:09 +01002086 rx_q->rx_tail_addr,
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002087 chan);
2088 }
2089
2090 /* DMA TX Channel Configuration */
2091 for (chan = 0; chan < tx_channels_count; chan++) {
Joao Pintoce736782017-04-06 09:49:10 +01002092 tx_q = &priv->tx_queue[chan];
2093
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002094 priv->hw->dma->init_chan(priv->ioaddr,
Joao Pintoce736782017-04-06 09:49:10 +01002095 priv->plat->dma_cfg,
2096 chan);
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002097
2098 priv->hw->dma->init_tx_chan(priv->ioaddr,
2099 priv->plat->dma_cfg,
Joao Pintoce736782017-04-06 09:49:10 +01002100 tx_q->dma_tx_phy, chan);
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002101
Joao Pintoce736782017-04-06 09:49:10 +01002102 tx_q->tx_tail_addr = tx_q->dma_tx_phy +
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002103 (DMA_TX_SIZE * sizeof(struct dma_desc));
2104 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr,
Joao Pintoce736782017-04-06 09:49:10 +01002105 tx_q->tx_tail_addr,
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002106 chan);
2107 }
2108 } else {
Joao Pinto54139cf2017-04-06 09:49:09 +01002109 rx_q = &priv->rx_queue[chan];
Joao Pintoce736782017-04-06 09:49:10 +01002110 tx_q = &priv->tx_queue[chan];
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002111 priv->hw->dma->init(priv->ioaddr, priv->plat->dma_cfg,
Joao Pintoce736782017-04-06 09:49:10 +01002112 tx_q->dma_tx_phy, rx_q->dma_rx_phy, atds);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002113 }
2114
2115 if (priv->plat->axi && priv->hw->dma->axi)
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01002116 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
2117
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01002118 return ret;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002119}
2120
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002121/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002122 * stmmac_tx_timer - mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002123 * @data: data pointer
2124 * Description:
2125 * This is the timer handler to directly invoke the stmmac_tx_clean.
2126 */
2127static void stmmac_tx_timer(unsigned long data)
2128{
2129 struct stmmac_priv *priv = (struct stmmac_priv *)data;
Joao Pintoce736782017-04-06 09:49:10 +01002130 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2131 u32 queue;
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002132
Joao Pintoce736782017-04-06 09:49:10 +01002133 /* let's scan all the tx queues */
2134 for (queue = 0; queue < tx_queues_count; queue++)
2135 stmmac_tx_clean(priv, queue);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002136}
2137
2138/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002139 * stmmac_init_tx_coalesce - init tx mitigation options.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002140 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002141 * Description:
2142 * This inits the transmit coalesce parameters: i.e. timer rate,
2143 * timer handler and default threshold used for enabling the
2144 * interrupt on completion bit.
2145 */
2146static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
2147{
2148 priv->tx_coal_frames = STMMAC_TX_FRAMES;
2149 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
2150 init_timer(&priv->txtimer);
2151 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
2152 priv->txtimer.data = (unsigned long)priv;
2153 priv->txtimer.function = stmmac_tx_timer;
2154 add_timer(&priv->txtimer);
2155}
2156
Joao Pinto4854ab92017-03-15 11:04:51 +00002157static void stmmac_set_rings_length(struct stmmac_priv *priv)
2158{
2159 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2160 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2161 u32 chan;
2162
2163 /* set TX ring length */
2164 if (priv->hw->dma->set_tx_ring_len) {
2165 for (chan = 0; chan < tx_channels_count; chan++)
2166 priv->hw->dma->set_tx_ring_len(priv->ioaddr,
2167 (DMA_TX_SIZE - 1), chan);
2168 }
2169
2170 /* set RX ring length */
2171 if (priv->hw->dma->set_rx_ring_len) {
2172 for (chan = 0; chan < rx_channels_count; chan++)
2173 priv->hw->dma->set_rx_ring_len(priv->ioaddr,
2174 (DMA_RX_SIZE - 1), chan);
2175 }
2176}
2177
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002178/**
Joao Pinto6a3a7192017-03-10 18:24:53 +00002179 * stmmac_set_tx_queue_weight - Set TX queue weight
2180 * @priv: driver private structure
2181 * Description: It is used for setting TX queues weight
2182 */
2183static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2184{
2185 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2186 u32 weight;
2187 u32 queue;
2188
2189 for (queue = 0; queue < tx_queues_count; queue++) {
2190 weight = priv->plat->tx_queues_cfg[queue].weight;
2191 priv->hw->mac->set_mtl_tx_queue_weight(priv->hw, weight, queue);
2192 }
2193}
2194
2195/**
Joao Pinto19d91872017-03-10 18:24:59 +00002196 * stmmac_configure_cbs - Configure CBS in TX queue
2197 * @priv: driver private structure
2198 * Description: It is used for configuring CBS in AVB TX queues
2199 */
2200static void stmmac_configure_cbs(struct stmmac_priv *priv)
2201{
2202 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2203 u32 mode_to_use;
2204 u32 queue;
2205
Joao Pinto44781fe2017-03-31 14:22:02 +01002206 /* queue 0 is reserved for legacy traffic */
2207 for (queue = 1; queue < tx_queues_count; queue++) {
Joao Pinto19d91872017-03-10 18:24:59 +00002208 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
2209 if (mode_to_use == MTL_QUEUE_DCB)
2210 continue;
2211
2212 priv->hw->mac->config_cbs(priv->hw,
2213 priv->plat->tx_queues_cfg[queue].send_slope,
2214 priv->plat->tx_queues_cfg[queue].idle_slope,
2215 priv->plat->tx_queues_cfg[queue].high_credit,
2216 priv->plat->tx_queues_cfg[queue].low_credit,
2217 queue);
2218 }
2219}
2220
2221/**
Joao Pintod43042f2017-03-10 18:24:55 +00002222 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
2223 * @priv: driver private structure
2224 * Description: It is used for mapping RX queues to RX dma channels
2225 */
2226static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
2227{
2228 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2229 u32 queue;
2230 u32 chan;
2231
2232 for (queue = 0; queue < rx_queues_count; queue++) {
2233 chan = priv->plat->rx_queues_cfg[queue].chan;
2234 priv->hw->mac->map_mtl_to_dma(priv->hw, queue, chan);
2235 }
2236}
2237
2238/**
Joao Pintoa8f51022017-03-17 16:11:06 +00002239 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
2240 * @priv: driver private structure
2241 * Description: It is used for configuring the RX Queue Priority
2242 */
2243static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
2244{
2245 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2246 u32 queue;
2247 u32 prio;
2248
2249 for (queue = 0; queue < rx_queues_count; queue++) {
2250 if (!priv->plat->rx_queues_cfg[queue].use_prio)
2251 continue;
2252
2253 prio = priv->plat->rx_queues_cfg[queue].prio;
2254 priv->hw->mac->rx_queue_prio(priv->hw, prio, queue);
2255 }
2256}
2257
2258/**
2259 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
2260 * @priv: driver private structure
2261 * Description: It is used for configuring the TX Queue Priority
2262 */
2263static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
2264{
2265 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2266 u32 queue;
2267 u32 prio;
2268
2269 for (queue = 0; queue < tx_queues_count; queue++) {
2270 if (!priv->plat->tx_queues_cfg[queue].use_prio)
2271 continue;
2272
2273 prio = priv->plat->tx_queues_cfg[queue].prio;
2274 priv->hw->mac->tx_queue_prio(priv->hw, prio, queue);
2275 }
2276}
2277
2278/**
Joao Pintoabe80fd2017-03-17 16:11:07 +00002279 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
2280 * @priv: driver private structure
2281 * Description: It is used for configuring the RX queue routing
2282 */
2283static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
2284{
2285 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2286 u32 queue;
2287 u8 packet;
2288
2289 for (queue = 0; queue < rx_queues_count; queue++) {
2290 /* no specific packet type routing specified for the queue */
2291 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
2292 continue;
2293
2294 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
2295 priv->hw->mac->rx_queue_prio(priv->hw, packet, queue);
2296 }
2297}
2298
2299/**
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002300 * stmmac_mtl_configuration - Configure MTL
2301 * @priv: driver private structure
2302 * Description: It is used for configurring MTL
2303 */
2304static void stmmac_mtl_configuration(struct stmmac_priv *priv)
2305{
2306 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2307 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2308
Joao Pinto6a3a7192017-03-10 18:24:53 +00002309 if (tx_queues_count > 1 && priv->hw->mac->set_mtl_tx_queue_weight)
2310 stmmac_set_tx_queue_weight(priv);
2311
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002312 /* Configure MTL RX algorithms */
2313 if (rx_queues_count > 1 && priv->hw->mac->prog_mtl_rx_algorithms)
2314 priv->hw->mac->prog_mtl_rx_algorithms(priv->hw,
2315 priv->plat->rx_sched_algorithm);
2316
2317 /* Configure MTL TX algorithms */
2318 if (tx_queues_count > 1 && priv->hw->mac->prog_mtl_tx_algorithms)
2319 priv->hw->mac->prog_mtl_tx_algorithms(priv->hw,
2320 priv->plat->tx_sched_algorithm);
2321
Joao Pinto19d91872017-03-10 18:24:59 +00002322 /* Configure CBS in AVB TX queues */
2323 if (tx_queues_count > 1 && priv->hw->mac->config_cbs)
2324 stmmac_configure_cbs(priv);
2325
Joao Pintod43042f2017-03-10 18:24:55 +00002326 /* Map RX MTL to DMA channels */
Joao Pinto03cf65a2017-04-03 16:34:04 +01002327 if (priv->hw->mac->map_mtl_to_dma)
Joao Pintod43042f2017-03-10 18:24:55 +00002328 stmmac_rx_queue_dma_chan_map(priv);
2329
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002330 /* Enable MAC RX Queues */
Thierry Redingf3976872017-03-21 16:12:09 +01002331 if (priv->hw->mac->rx_queue_enable)
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002332 stmmac_mac_enable_rx_queues(priv);
Joao Pinto6deee222017-03-15 11:04:45 +00002333
Joao Pintoa8f51022017-03-17 16:11:06 +00002334 /* Set RX priorities */
2335 if (rx_queues_count > 1 && priv->hw->mac->rx_queue_prio)
2336 stmmac_mac_config_rx_queues_prio(priv);
2337
2338 /* Set TX priorities */
2339 if (tx_queues_count > 1 && priv->hw->mac->tx_queue_prio)
2340 stmmac_mac_config_tx_queues_prio(priv);
Joao Pintoabe80fd2017-03-17 16:11:07 +00002341
2342 /* Set RX routing */
2343 if (rx_queues_count > 1 && priv->hw->mac->rx_queue_routing)
2344 stmmac_mac_config_rx_queues_routing(priv);
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002345}
2346
2347/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002348 * stmmac_hw_setup - setup mac in a usable state.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002349 * @dev : pointer to the device structure.
2350 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002351 * this is the main function to setup the HW in a usable state because the
2352 * dma engine is reset, the core registers are configured (e.g. AXI,
2353 * Checksum features, timers). The DMA is ready to start receiving and
2354 * transmitting.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002355 * Return value:
2356 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2357 * file on failure.
2358 */
Huacai Chenfe1319292014-12-19 22:38:18 +08002359static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002360{
2361 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto3c55d4d2017-03-15 11:04:50 +00002362 u32 rx_cnt = priv->plat->rx_queues_to_use;
Joao Pinto146617b2017-03-15 11:04:54 +00002363 u32 tx_cnt = priv->plat->tx_queues_to_use;
2364 u32 chan;
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002365 int ret;
2366
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002367 /* DMA initialization and SW reset */
2368 ret = stmmac_init_dma_engine(priv);
2369 if (ret < 0) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002370 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
2371 __func__);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002372 return ret;
2373 }
2374
2375 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002376 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002377
Giuseppe CAVALLARO02e57b92016-06-24 15:16:26 +02002378 /* PS and related bits will be programmed according to the speed */
2379 if (priv->hw->pcs) {
2380 int speed = priv->plat->mac_port_sel_speed;
2381
2382 if ((speed == SPEED_10) || (speed == SPEED_100) ||
2383 (speed == SPEED_1000)) {
2384 priv->hw->ps = speed;
2385 } else {
2386 dev_warn(priv->device, "invalid port speed\n");
2387 priv->hw->ps = 0;
2388 }
2389 }
2390
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002391 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002392 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002393
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002394 /* Initialize MTL*/
2395 if (priv->synopsys_id >= DWMAC_CORE_4_00)
2396 stmmac_mtl_configuration(priv);
jpinto9eb12472016-12-28 12:57:48 +00002397
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02002398 ret = priv->hw->mac->rx_ipc(priv->hw);
2399 if (!ret) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002400 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02002401 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002402 priv->hw->rx_csum = 0;
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02002403 }
2404
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002405 /* Enable the MAC Rx/Tx */
LABBE Corentin270c7752017-03-23 14:40:22 +01002406 priv->hw->mac->set_mac(priv->ioaddr, true);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002407
Joao Pintob4f0a662017-03-22 11:56:05 +00002408 /* Set the HW DMA mode and the COE */
2409 stmmac_dma_operation_mode(priv);
2410
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002411 stmmac_mmc_setup(priv);
2412
Huacai Chenfe1319292014-12-19 22:38:18 +08002413 if (init_ptp) {
Thierry Reding0ad2be72017-03-10 17:34:56 +01002414 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
2415 if (ret < 0)
2416 netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret);
2417
Huacai Chenfe1319292014-12-19 22:38:18 +08002418 ret = stmmac_init_ptp(priv);
Heiner Kallweit722eef22017-02-01 22:02:02 +01002419 if (ret == -EOPNOTSUPP)
2420 netdev_warn(priv->dev, "PTP not supported by HW\n");
2421 else if (ret)
2422 netdev_warn(priv->dev, "PTP init failed\n");
Huacai Chenfe1319292014-12-19 22:38:18 +08002423 }
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002424
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002425#ifdef CONFIG_DEBUG_FS
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002426 ret = stmmac_init_fs(dev);
2427 if (ret < 0)
LABBE Corentin38ddc592016-11-16 20:09:39 +01002428 netdev_warn(priv->dev, "%s: failed debugFS registration\n",
2429 __func__);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002430#endif
2431 /* Start the ball rolling... */
Joao Pintoae4f0d42017-03-15 11:04:47 +00002432 stmmac_start_all_dma(priv);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002433
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002434 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
2435
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002436 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
2437 priv->rx_riwt = MAX_DMA_RIWT;
Joao Pinto3c55d4d2017-03-15 11:04:50 +00002438 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT, rx_cnt);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002439 }
2440
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02002441 if (priv->hw->pcs && priv->hw->mac->pcs_ctrl_ane)
Giuseppe CAVALLARO02e57b92016-06-24 15:16:26 +02002442 priv->hw->mac->pcs_ctrl_ane(priv->hw, 1, priv->hw->ps, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002443
Joao Pinto4854ab92017-03-15 11:04:51 +00002444 /* set TX and RX rings length */
2445 stmmac_set_rings_length(priv);
2446
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002447 /* Enable TSO */
Joao Pinto146617b2017-03-15 11:04:54 +00002448 if (priv->tso) {
2449 for (chan = 0; chan < tx_cnt; chan++)
2450 priv->hw->dma->enable_tso(priv->ioaddr, 1, chan);
2451 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002452
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002453 return 0;
2454}
2455
Thierry Redingc66f6c32017-03-10 17:34:55 +01002456static void stmmac_hw_teardown(struct net_device *dev)
2457{
2458 struct stmmac_priv *priv = netdev_priv(dev);
2459
2460 clk_disable_unprepare(priv->plat->clk_ptp_ref);
2461}
2462
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002463/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002464 * stmmac_open - open entry point of the driver
2465 * @dev : pointer to the device structure.
2466 * Description:
2467 * This function is the open entry point of the driver.
2468 * Return value:
2469 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2470 * file on failure.
2471 */
2472static int stmmac_open(struct net_device *dev)
2473{
2474 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002475 int ret;
2476
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002477 stmmac_check_ether_addr(priv);
2478
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02002479 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
2480 priv->hw->pcs != STMMAC_PCS_TBI &&
2481 priv->hw->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002482 ret = stmmac_init_phy(dev);
2483 if (ret) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002484 netdev_err(priv->dev,
2485 "%s: Cannot attach to PHY (error: %d)\n",
2486 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02002487 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002488 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002489 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002490
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002491 /* Extra statistics */
2492 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
2493 priv->xstats.threshold = tc;
2494
LABBE Corentin5bacd772017-03-29 07:05:40 +02002495 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002496 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02002497
LABBE Corentin5bacd772017-03-29 07:05:40 +02002498 ret = alloc_dma_desc_resources(priv);
2499 if (ret < 0) {
2500 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
2501 __func__);
2502 goto dma_desc_error;
2503 }
2504
2505 ret = init_dma_desc_rings(dev, GFP_KERNEL);
2506 if (ret < 0) {
2507 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
2508 __func__);
2509 goto init_error;
2510 }
2511
Huacai Chenfe1319292014-12-19 22:38:18 +08002512 ret = stmmac_hw_setup(dev, true);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02002513 if (ret < 0) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002514 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002515 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002516 }
2517
Giuseppe CAVALLARO777da2302014-11-04 17:08:09 +01002518 stmmac_init_tx_coalesce(priv);
2519
Philippe Reynesd6d50c72016-10-03 08:28:19 +02002520 if (dev->phydev)
2521 phy_start(dev->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002522
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002523 /* Request the IRQ lines */
2524 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002525 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002526 if (unlikely(ret < 0)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002527 netdev_err(priv->dev,
2528 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
2529 __func__, dev->irq, ret);
Thierry Reding6c1e5ab2017-03-10 17:34:54 +01002530 goto irq_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002531 }
2532
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002533 /* Request the Wake IRQ in case of another line is used for WoL */
2534 if (priv->wol_irq != dev->irq) {
2535 ret = request_irq(priv->wol_irq, stmmac_interrupt,
2536 IRQF_SHARED, dev->name, dev);
2537 if (unlikely(ret < 0)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002538 netdev_err(priv->dev,
2539 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
2540 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002541 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002542 }
2543 }
2544
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002545 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08002546 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002547 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
2548 dev->name, dev);
2549 if (unlikely(ret < 0)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002550 netdev_err(priv->dev,
2551 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
2552 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002553 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002554 }
2555 }
2556
LABBE Corentin5bacd772017-03-29 07:05:40 +02002557 napi_enable(&priv->napi);
2558 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002559
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002560 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002561
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002562lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002563 if (priv->wol_irq != dev->irq)
2564 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002565wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002566 free_irq(dev->irq, dev);
Thierry Reding6c1e5ab2017-03-10 17:34:54 +01002567irq_error:
2568 if (dev->phydev)
2569 phy_stop(dev->phydev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002570
Thierry Reding6c1e5ab2017-03-10 17:34:54 +01002571 del_timer_sync(&priv->txtimer);
Thierry Redingc66f6c32017-03-10 17:34:55 +01002572 stmmac_hw_teardown(dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002573init_error:
2574 free_dma_desc_resources(priv);
LABBE Corentin5bacd772017-03-29 07:05:40 +02002575dma_desc_error:
Philippe Reynesd6d50c72016-10-03 08:28:19 +02002576 if (dev->phydev)
2577 phy_disconnect(dev->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002578
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002579 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002580}
2581
2582/**
2583 * stmmac_release - close entry point of the driver
2584 * @dev : device pointer.
2585 * Description:
2586 * This is the stop entry point of the driver.
2587 */
2588static int stmmac_release(struct net_device *dev)
2589{
2590 struct stmmac_priv *priv = netdev_priv(dev);
2591
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002592 if (priv->eee_enabled)
2593 del_timer_sync(&priv->eee_ctrl_timer);
2594
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002595 /* Stop and disconnect the PHY */
Philippe Reynesd6d50c72016-10-03 08:28:19 +02002596 if (dev->phydev) {
2597 phy_stop(dev->phydev);
2598 phy_disconnect(dev->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002599 }
2600
LABBE Corentin5bacd772017-03-29 07:05:40 +02002601 netif_stop_queue(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002602
LABBE Corentin5bacd772017-03-29 07:05:40 +02002603 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002604
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002605 del_timer_sync(&priv->txtimer);
2606
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002607 /* Free the IRQ lines */
2608 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002609 if (priv->wol_irq != dev->irq)
2610 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08002611 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002612 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002613
2614 /* Stop TX/RX DMA and clear the descriptors */
Joao Pintoae4f0d42017-03-15 11:04:47 +00002615 stmmac_stop_all_dma(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002616
2617 /* Release and free the Rx/Tx resources */
2618 free_dma_desc_resources(priv);
2619
avisconti19449bf2010-10-25 18:58:14 +00002620 /* Disable the MAC Rx/Tx */
LABBE Corentin270c7752017-03-23 14:40:22 +01002621 priv->hw->mac->set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002622
2623 netif_carrier_off(dev);
2624
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002625#ifdef CONFIG_DEBUG_FS
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002626 stmmac_exit_fs(dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002627#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002628
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00002629 stmmac_release_ptp(priv);
2630
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002631 return 0;
2632}
2633
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002634/**
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002635 * stmmac_tso_allocator - close entry point of the driver
2636 * @priv: driver private structure
2637 * @des: buffer start address
2638 * @total_len: total length to fill in descriptors
2639 * @last_segmant: condition for the last descriptor
Joao Pintoce736782017-04-06 09:49:10 +01002640 * @queue: TX queue index
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002641 * Description:
2642 * This function fills descriptor and request new descriptors according to
2643 * buffer length to fill
2644 */
2645static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
Joao Pintoce736782017-04-06 09:49:10 +01002646 int total_len, bool last_segment, u32 queue)
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002647{
Joao Pintoce736782017-04-06 09:49:10 +01002648 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002649 struct dma_desc *desc;
LABBE Corentin5bacd772017-03-29 07:05:40 +02002650 u32 buff_size;
Joao Pintoce736782017-04-06 09:49:10 +01002651 int tmp_len;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002652
2653 tmp_len = total_len;
2654
2655 while (tmp_len > 0) {
Joao Pintoce736782017-04-06 09:49:10 +01002656 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
2657 desc = tx_q->dma_tx + tx_q->cur_tx;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002658
Michael Weiserf8be0d72016-11-14 18:58:05 +01002659 desc->des0 = cpu_to_le32(des + (total_len - tmp_len));
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002660 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
2661 TSO_MAX_BUFF_SIZE : tmp_len;
2662
2663 priv->hw->desc->prepare_tso_tx_desc(desc, 0, buff_size,
2664 0, 1,
2665 (last_segment) && (buff_size < TSO_MAX_BUFF_SIZE),
2666 0, 0);
2667
2668 tmp_len -= TSO_MAX_BUFF_SIZE;
2669 }
2670}
2671
2672/**
2673 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
2674 * @skb : the socket buffer
2675 * @dev : device pointer
2676 * Description: this is the transmit function that is called on TSO frames
2677 * (support available on GMAC4 and newer chips).
2678 * Diagram below show the ring programming in case of TSO frames:
2679 *
2680 * First Descriptor
2681 * --------
2682 * | DES0 |---> buffer1 = L2/L3/L4 header
2683 * | DES1 |---> TCP Payload (can continue on next descr...)
2684 * | DES2 |---> buffer 1 and 2 len
2685 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
2686 * --------
2687 * |
2688 * ...
2689 * |
2690 * --------
2691 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
2692 * | DES1 | --|
2693 * | DES2 | --> buffer 1 and 2 len
2694 * | DES3 |
2695 * --------
2696 *
2697 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
2698 */
2699static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
2700{
Joao Pintoce736782017-04-06 09:49:10 +01002701 struct dma_desc *desc, *first, *mss_desc = NULL;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002702 struct stmmac_priv *priv = netdev_priv(dev);
2703 int nfrags = skb_shinfo(skb)->nr_frags;
Joao Pintoce736782017-04-06 09:49:10 +01002704 u32 queue = skb_get_queue_mapping(skb);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002705 unsigned int first_entry, des;
Joao Pintoce736782017-04-06 09:49:10 +01002706 struct stmmac_tx_queue *tx_q;
2707 int tmp_pay_len = 0;
2708 u32 pay_len, mss;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002709 u8 proto_hdr_len;
2710 int i;
2711
Joao Pintoce736782017-04-06 09:49:10 +01002712 tx_q = &priv->tx_queue[queue];
2713
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002714 /* Compute header lengths */
2715 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2716
2717 /* Desc availability based on threshold should be enough safe */
Joao Pintoce736782017-04-06 09:49:10 +01002718 if (unlikely(stmmac_tx_avail(priv, queue) <
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002719 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
LABBE Corentin5bacd772017-03-29 07:05:40 +02002720 if (!netif_queue_stopped(dev)) {
2721 netif_stop_queue(dev);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002722 /* This is a hard error, log it. */
LABBE Corentin38ddc592016-11-16 20:09:39 +01002723 netdev_err(priv->dev,
2724 "%s: Tx Ring full when queue awake\n",
2725 __func__);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002726 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002727 return NETDEV_TX_BUSY;
2728 }
2729
2730 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
2731
2732 mss = skb_shinfo(skb)->gso_size;
2733
2734 /* set new MSS value if needed */
2735 if (mss != priv->mss) {
Joao Pintoce736782017-04-06 09:49:10 +01002736 mss_desc = tx_q->dma_tx + tx_q->cur_tx;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002737 priv->hw->desc->set_mss(mss_desc, mss);
2738 priv->mss = mss;
Joao Pintoce736782017-04-06 09:49:10 +01002739 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002740 }
2741
2742 if (netif_msg_tx_queued(priv)) {
2743 pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
2744 __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
2745 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
2746 skb->data_len);
2747 }
2748
Joao Pintoce736782017-04-06 09:49:10 +01002749 first_entry = tx_q->cur_tx;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002750
Joao Pintoce736782017-04-06 09:49:10 +01002751 desc = tx_q->dma_tx + first_entry;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002752 first = desc;
2753
2754 /* first descriptor: fill Headers on Buf1 */
2755 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
2756 DMA_TO_DEVICE);
2757 if (dma_mapping_error(priv->device, des))
2758 goto dma_map_err;
2759
Joao Pintoce736782017-04-06 09:49:10 +01002760 tx_q->tx_skbuff_dma[first_entry].buf = des;
2761 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
2762 tx_q->tx_skbuff[first_entry] = skb;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002763
Michael Weiserf8be0d72016-11-14 18:58:05 +01002764 first->des0 = cpu_to_le32(des);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002765
2766 /* Fill start of payload in buff2 of first descriptor */
2767 if (pay_len)
Michael Weiserf8be0d72016-11-14 18:58:05 +01002768 first->des1 = cpu_to_le32(des + proto_hdr_len);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002769
2770 /* If needed take extra descriptors to fill the remaining payload */
2771 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
2772
Joao Pintoce736782017-04-06 09:49:10 +01002773 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002774
2775 /* Prepare fragments */
2776 for (i = 0; i < nfrags; i++) {
2777 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2778
2779 des = skb_frag_dma_map(priv->device, frag, 0,
2780 skb_frag_size(frag),
2781 DMA_TO_DEVICE);
Thierry Reding937071c2017-03-10 17:34:57 +01002782 if (dma_mapping_error(priv->device, des))
2783 goto dma_map_err;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002784
2785 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
Joao Pintoce736782017-04-06 09:49:10 +01002786 (i == nfrags - 1), queue);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002787
Joao Pintoce736782017-04-06 09:49:10 +01002788 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
2789 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
2790 tx_q->tx_skbuff[tx_q->cur_tx] = NULL;
2791 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002792 }
2793
Joao Pintoce736782017-04-06 09:49:10 +01002794 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002795
Joao Pintoce736782017-04-06 09:49:10 +01002796 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002797
Joao Pintoce736782017-04-06 09:49:10 +01002798 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
LABBE Corentinb3e51062016-11-16 20:09:41 +01002799 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
2800 __func__);
LABBE Corentin5bacd772017-03-29 07:05:40 +02002801 netif_stop_queue(dev);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002802 }
2803
2804 dev->stats.tx_bytes += skb->len;
2805 priv->xstats.tx_tso_frames++;
2806 priv->xstats.tx_tso_nfrags += nfrags;
2807
2808 /* Manage tx mitigation */
2809 priv->tx_count_frames += nfrags + 1;
2810 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
2811 mod_timer(&priv->txtimer,
2812 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2813 } else {
2814 priv->tx_count_frames = 0;
2815 priv->hw->desc->set_tx_ic(desc);
2816 priv->xstats.tx_set_ic_bit++;
2817 }
2818
2819 if (!priv->hwts_tx_en)
2820 skb_tx_timestamp(skb);
2821
2822 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2823 priv->hwts_tx_en)) {
2824 /* declare that device is doing timestamping */
2825 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2826 priv->hw->desc->enable_tx_timestamp(first);
2827 }
2828
2829 /* Complete the first descriptor before granting the DMA */
2830 priv->hw->desc->prepare_tso_tx_desc(first, 1,
2831 proto_hdr_len,
2832 pay_len,
Joao Pintoce736782017-04-06 09:49:10 +01002833 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002834 tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
2835
2836 /* If context desc is used to change MSS */
2837 if (mss_desc)
2838 priv->hw->desc->set_tx_owner(mss_desc);
2839
2840 /* The own bit must be the latest setting done when prepare the
2841 * descriptor and then barrier is needed to make sure that
2842 * all is coherent before granting the DMA engine.
2843 */
Pavel Machekad688cd2016-12-18 21:38:12 +01002844 dma_wmb();
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002845
2846 if (netif_msg_pktdata(priv)) {
2847 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
Joao Pintoce736782017-04-06 09:49:10 +01002848 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
2849 tx_q->cur_tx, first, nfrags);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002850
Joao Pintoce736782017-04-06 09:49:10 +01002851 priv->hw->desc->display_ring((void *)tx_q->dma_tx, DMA_TX_SIZE,
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002852 0);
2853
2854 pr_info(">>> frame to be transmitted: ");
2855 print_pkt(skb->data, skb_headlen(skb));
2856 }
2857
LABBE Corentin5bacd772017-03-29 07:05:40 +02002858 netdev_sent_queue(dev, skb->len);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002859
Joao Pintoce736782017-04-06 09:49:10 +01002860 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, tx_q->tx_tail_addr,
2861 queue);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002862
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002863 return NETDEV_TX_OK;
2864
2865dma_map_err:
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002866 dev_err(priv->device, "Tx dma map failed\n");
2867 dev_kfree_skb(skb);
2868 priv->dev->stats.tx_dropped++;
2869 return NETDEV_TX_OK;
2870}
2871
2872/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002873 * stmmac_xmit - Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002874 * @skb : the socket buffer
2875 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002876 * Description : this is the tx entry point of the driver.
2877 * It programs the chain or the ring and supports oversized frames
2878 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002879 */
2880static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
2881{
2882 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002883 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002884 int i, csum_insertion = 0, is_jumbo = 0;
Joao Pintoce736782017-04-06 09:49:10 +01002885 u32 queue = skb_get_queue_mapping(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002886 int nfrags = skb_shinfo(skb)->nr_frags;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002887 unsigned int entry, first_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002888 struct dma_desc *desc, *first;
Joao Pintoce736782017-04-06 09:49:10 +01002889 struct stmmac_tx_queue *tx_q;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002890 unsigned int enh_desc;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002891 unsigned int des;
2892
Joao Pintoce736782017-04-06 09:49:10 +01002893 tx_q = &priv->tx_queue[queue];
2894
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002895 /* Manage oversized TCP frames for GMAC4 device */
2896 if (skb_is_gso(skb) && priv->tso) {
2897 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2898 return stmmac_tso_xmit(skb, dev);
2899 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002900
Joao Pintoce736782017-04-06 09:49:10 +01002901 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
LABBE Corentin5bacd772017-03-29 07:05:40 +02002902 if (!netif_queue_stopped(dev)) {
2903 netif_stop_queue(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002904 /* This is a hard error, log it. */
LABBE Corentin38ddc592016-11-16 20:09:39 +01002905 netdev_err(priv->dev,
2906 "%s: Tx Ring full when queue awake\n",
2907 __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002908 }
2909 return NETDEV_TX_BUSY;
2910 }
2911
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002912 if (priv->tx_path_in_lpi_mode)
2913 stmmac_disable_eee_mode(priv);
2914
Joao Pintoce736782017-04-06 09:49:10 +01002915 entry = tx_q->cur_tx;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002916 first_entry = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002917
Michał Mirosław5e982f32011-04-09 02:46:55 +00002918 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002919
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002920 if (likely(priv->extend_desc))
Joao Pintoce736782017-04-06 09:49:10 +01002921 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002922 else
Joao Pintoce736782017-04-06 09:49:10 +01002923 desc = tx_q->dma_tx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002924
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002925 first = desc;
2926
Joao Pintoce736782017-04-06 09:49:10 +01002927 tx_q->tx_skbuff[first_entry] = skb;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002928
2929 enh_desc = priv->plat->enh_desc;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002930 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002931 if (enh_desc)
2932 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
2933
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002934 if (unlikely(is_jumbo) && likely(priv->synopsys_id <
2935 DWMAC_CORE_4_00)) {
Joao Pintoce736782017-04-06 09:49:10 +01002936 entry = priv->hw->mode->jumbo_frm(tx_q, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002937 if (unlikely(entry < 0))
2938 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002939 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002940
2941 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00002942 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2943 int len = skb_frag_size(frag);
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01002944 bool last_segment = (i == (nfrags - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002945
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002946 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2947
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002948 if (likely(priv->extend_desc))
Joao Pintoce736782017-04-06 09:49:10 +01002949 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002950 else
Joao Pintoce736782017-04-06 09:49:10 +01002951 desc = tx_q->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002952
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002953 des = skb_frag_dma_map(priv->device, frag, 0, len,
2954 DMA_TO_DEVICE);
2955 if (dma_mapping_error(priv->device, des))
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002956 goto dma_map_err; /* should reuse desc w/o issues */
2957
Joao Pintoce736782017-04-06 09:49:10 +01002958 tx_q->tx_skbuff[entry] = NULL;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002959
Joao Pintoce736782017-04-06 09:49:10 +01002960 tx_q->tx_skbuff_dma[entry].buf = des;
Michael Weiserf8be0d72016-11-14 18:58:05 +01002961 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
2962 desc->des0 = cpu_to_le32(des);
2963 else
2964 desc->des2 = cpu_to_le32(des);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002965
Joao Pintoce736782017-04-06 09:49:10 +01002966 tx_q->tx_skbuff_dma[entry].map_as_page = true;
2967 tx_q->tx_skbuff_dma[entry].len = len;
2968 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002969
2970 /* Prepare the descriptor and set the own bit too */
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002971 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01002972 priv->mode, 1, last_segment);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002973 }
2974
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002975 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2976
Joao Pintoce736782017-04-06 09:49:10 +01002977 tx_q->cur_tx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002978
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002979 if (netif_msg_pktdata(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002980 void *tx_head;
2981
LABBE Corentin38ddc592016-11-16 20:09:39 +01002982 netdev_dbg(priv->dev,
2983 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
Joao Pintoce736782017-04-06 09:49:10 +01002984 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
LABBE Corentin38ddc592016-11-16 20:09:39 +01002985 entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002986
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002987 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01002988 tx_head = (void *)tx_q->dma_etx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002989 else
Joao Pintoce736782017-04-06 09:49:10 +01002990 tx_head = (void *)tx_q->dma_tx;
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002991
2992 priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002993
LABBE Corentin38ddc592016-11-16 20:09:39 +01002994 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002995 print_pkt(skb->data, skb->len);
2996 }
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01002997
Joao Pintoce736782017-04-06 09:49:10 +01002998 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
LABBE Corentinb3e51062016-11-16 20:09:41 +01002999 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
3000 __func__);
LABBE Corentin5bacd772017-03-29 07:05:40 +02003001 netif_stop_queue(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003002 }
3003
3004 dev->stats.tx_bytes += skb->len;
3005
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003006 /* According to the coalesce parameter the IC bit for the latest
3007 * segment is reset and the timer re-started to clean the tx status.
3008 * This approach takes care about the fragments: desc is the first
3009 * element in case of no SG.
3010 */
3011 priv->tx_count_frames += nfrags + 1;
3012 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
3013 mod_timer(&priv->txtimer,
3014 STMMAC_COAL_TIMER(priv->tx_coal_timer));
3015 } else {
3016 priv->tx_count_frames = 0;
3017 priv->hw->desc->set_tx_ic(desc);
3018 priv->xstats.tx_set_ic_bit++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003019 }
3020
3021 if (!priv->hwts_tx_en)
3022 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00003023
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003024 /* Ready to fill the first descriptor and set the OWN bit w/o any
3025 * problems because all the descriptors are actually ready to be
3026 * passed to the DMA engine.
3027 */
3028 if (likely(!is_jumbo)) {
3029 bool last_segment = (nfrags == 0);
3030
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003031 des = dma_map_single(priv->device, skb->data,
3032 nopaged_len, DMA_TO_DEVICE);
3033 if (dma_mapping_error(priv->device, des))
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003034 goto dma_map_err;
3035
Joao Pintoce736782017-04-06 09:49:10 +01003036 tx_q->tx_skbuff_dma[first_entry].buf = des;
Michael Weiserf8be0d72016-11-14 18:58:05 +01003037 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
3038 first->des0 = cpu_to_le32(des);
3039 else
3040 first->des2 = cpu_to_le32(des);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003041
Joao Pintoce736782017-04-06 09:49:10 +01003042 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
3043 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003044
3045 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3046 priv->hwts_tx_en)) {
3047 /* declare that device is doing timestamping */
3048 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3049 priv->hw->desc->enable_tx_timestamp(first);
3050 }
3051
3052 /* Prepare the first descriptor setting the OWN bit too */
3053 priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
3054 csum_insertion, priv->mode, 1,
3055 last_segment);
3056
3057 /* The own bit must be the latest setting done when prepare the
3058 * descriptor and then barrier is needed to make sure that
3059 * all is coherent before granting the DMA engine.
3060 */
Pavel Machekad688cd2016-12-18 21:38:12 +01003061 dma_wmb();
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003062 }
3063
LABBE Corentin5bacd772017-03-29 07:05:40 +02003064 netdev_sent_queue(dev, skb->len);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003065
3066 if (priv->synopsys_id < DWMAC_CORE_4_00)
3067 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
3068 else
Joao Pintoce736782017-04-06 09:49:10 +01003069 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, tx_q->tx_tail_addr,
3070 queue);
Richard Cochran52f64fa2011-06-19 03:31:43 +00003071
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003072 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00003073
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003074dma_map_err:
LABBE Corentin38ddc592016-11-16 20:09:39 +01003075 netdev_err(priv->dev, "Tx DMA map failed\n");
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003076 dev_kfree_skb(skb);
3077 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003078 return NETDEV_TX_OK;
3079}
3080
Vince Bridgersb9381982014-01-14 13:42:05 -06003081static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
3082{
3083 struct ethhdr *ehdr;
3084 u16 vlanid;
3085
3086 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
3087 NETIF_F_HW_VLAN_CTAG_RX &&
3088 !__vlan_get_tag(skb, &vlanid)) {
3089 /* pop the vlan tag */
3090 ehdr = (struct ethhdr *)skb->data;
3091 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
3092 skb_pull(skb, VLAN_HLEN);
3093 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
3094 }
3095}
3096
3097
Joao Pinto54139cf2017-04-06 09:49:09 +01003098static inline int stmmac_rx_threshold_count(struct stmmac_rx_queue *rx_q)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003099{
Joao Pinto54139cf2017-04-06 09:49:09 +01003100 if (rx_q->rx_zeroc_thresh < STMMAC_RX_THRESH)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003101 return 0;
3102
3103 return 1;
3104}
3105
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003106/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003107 * stmmac_rx_refill - refill used skb preallocated buffers
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003108 * @priv: driver private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01003109 * @queue: RX queue index
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003110 * Description : this is to reallocate the skb for the reception process
3111 * that is based on zero-copy.
3112 */
Joao Pinto54139cf2017-04-06 09:49:09 +01003113static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003114{
Joao Pinto54139cf2017-04-06 09:49:09 +01003115 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3116 int dirty = stmmac_rx_dirty(priv, queue);
3117 unsigned int entry = rx_q->dirty_rx;
3118
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003119 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003120
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003121 while (dirty-- > 0) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003122 struct dma_desc *p;
3123
3124 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003125 p = (struct dma_desc *)(rx_q->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003126 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003127 p = rx_q->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003128
Joao Pinto54139cf2017-04-06 09:49:09 +01003129 if (likely(!rx_q->rx_skbuff[entry])) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003130 struct sk_buff *skb;
3131
Eric Dumazetacb600d2012-10-05 06:23:55 +00003132 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003133 if (unlikely(!skb)) {
3134 /* so for a while no zero-copy! */
Joao Pinto54139cf2017-04-06 09:49:09 +01003135 rx_q->rx_zeroc_thresh = STMMAC_RX_THRESH;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003136 if (unlikely(net_ratelimit()))
3137 dev_err(priv->device,
3138 "fail to alloc skb entry %d\n",
3139 entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003140 break;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003141 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003142
Joao Pinto54139cf2017-04-06 09:49:09 +01003143 rx_q->rx_skbuff[entry] = skb;
3144 rx_q->rx_skbuff_dma[entry] =
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003145 dma_map_single(priv->device, skb->data, bfsize,
3146 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003147 if (dma_mapping_error(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003148 rx_q->rx_skbuff_dma[entry])) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003149 netdev_err(priv->dev, "Rx DMA map failed\n");
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003150 dev_kfree_skb(skb);
3151 break;
3152 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00003153
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003154 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
Joao Pinto54139cf2017-04-06 09:49:09 +01003155 p->des0 = cpu_to_le32(rx_q->rx_skbuff_dma[entry]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003156 p->des1 = 0;
3157 } else {
Joao Pinto54139cf2017-04-06 09:49:09 +01003158 p->des2 = cpu_to_le32(rx_q->rx_skbuff_dma[entry]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003159 }
3160 if (priv->hw->mode->refill_desc3)
Joao Pinto54139cf2017-04-06 09:49:09 +01003161 priv->hw->mode->refill_desc3(rx_q, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00003162
Joao Pinto54139cf2017-04-06 09:49:09 +01003163 if (rx_q->rx_zeroc_thresh > 0)
3164 rx_q->rx_zeroc_thresh--;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003165
LABBE Corentinb3e51062016-11-16 20:09:41 +01003166 netif_dbg(priv, rx_status, priv->dev,
3167 "refill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003168 }
Pavel Machekad688cd2016-12-18 21:38:12 +01003169 dma_wmb();
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003170
3171 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
3172 priv->hw->desc->init_rx_desc(p, priv->use_riwt, 0, 0);
3173 else
3174 priv->hw->desc->set_rx_owner(p);
3175
Pavel Machekad688cd2016-12-18 21:38:12 +01003176 dma_wmb();
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003177
3178 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003179 }
Joao Pinto54139cf2017-04-06 09:49:09 +01003180 rx_q->dirty_rx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003181}
3182
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003183/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003184 * stmmac_rx - manage the receive process
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003185 * @priv: driver private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01003186 * @limit: napi bugget
3187 * @queue: RX queue index.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003188 * Description : this the function called by the napi poll method.
3189 * It gets all the frames inside the ring.
3190 */
Joao Pinto54139cf2017-04-06 09:49:09 +01003191static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003192{
Joao Pinto54139cf2017-04-06 09:49:09 +01003193 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3194 unsigned int entry = rx_q->cur_rx;
3195 int coe = priv->hw->rx_csum;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003196 unsigned int next_entry;
3197 unsigned int count = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003198
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003199 if (netif_msg_rx_status(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02003200 void *rx_head;
3201
LABBE Corentin38ddc592016-11-16 20:09:39 +01003202 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003203 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003204 rx_head = (void *)rx_q->dma_erx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003205 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003206 rx_head = (void *)rx_q->dma_rx;
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02003207
3208 priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003209 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003210 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003211 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00003212 struct dma_desc *p;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01003213 struct dma_desc *np;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003214
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003215 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003216 p = (struct dma_desc *)(rx_q->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003217 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003218 p = rx_q->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003219
Fabrice Gasnierc1fa3212016-02-29 14:27:34 +01003220 /* read the status of the incoming frame */
3221 status = priv->hw->desc->rx_status(&priv->dev->stats,
3222 &priv->xstats, p);
3223 /* check if managed by the DMA otherwise go ahead */
3224 if (unlikely(status & dma_own))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003225 break;
3226
3227 count++;
3228
Joao Pinto54139cf2017-04-06 09:49:09 +01003229 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, DMA_RX_SIZE);
3230 next_entry = rx_q->cur_rx;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003231
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003232 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003233 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003234 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003235 np = rx_q->dma_rx + next_entry;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01003236
3237 prefetch(np);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003238
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003239 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
3240 priv->hw->desc->rx_extended_status(&priv->dev->stats,
3241 &priv->xstats,
Joao Pinto54139cf2017-04-06 09:49:09 +01003242 rx_q->dma_erx +
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003243 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003244 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003245 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003246 if (priv->hwts_rx_en && !priv->extend_desc) {
LABBE Corentin8d45e422017-02-08 09:31:08 +01003247 /* DESC2 & DESC3 will be overwritten by device
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003248 * with timestamp value, hence reinitialize
3249 * them in stmmac_rx_refill() function so that
3250 * device can reuse it.
3251 */
Joao Pinto54139cf2017-04-06 09:49:09 +01003252 rx_q->rx_skbuff[entry] = NULL;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003253 dma_unmap_single(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003254 rx_q->rx_skbuff_dma[entry],
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003255 priv->dma_buf_sz,
3256 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003257 }
3258 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003259 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00003260 int frame_len;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003261 unsigned int des;
3262
3263 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
Michael Weiserf8be0d72016-11-14 18:58:05 +01003264 des = le32_to_cpu(p->des0);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003265 else
Michael Weiserf8be0d72016-11-14 18:58:05 +01003266 des = le32_to_cpu(p->des2);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003267
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003268 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
3269
LABBE Corentin8d45e422017-02-08 09:31:08 +01003270 /* If frame length is greater than skb buffer size
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003271 * (preallocated during init) then the packet is
3272 * ignored
3273 */
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01003274 if (frame_len > priv->dma_buf_sz) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003275 netdev_err(priv->dev,
3276 "len %d larger than size (%d)\n",
3277 frame_len, priv->dma_buf_sz);
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01003278 priv->dev->stats.rx_length_errors++;
3279 break;
3280 }
3281
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00003282 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003283 * Type frames (LLC/LLC-SNAP)
3284 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00003285 if (unlikely(status != llc_snap))
3286 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003287
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003288 if (netif_msg_rx_status(priv)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003289 netdev_dbg(priv->dev, "\tdesc: %p [entry %d] buff=0x%x\n",
3290 p, entry, des);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003291 if (frame_len > ETH_FRAME_LEN)
LABBE Corentin38ddc592016-11-16 20:09:39 +01003292 netdev_dbg(priv->dev, "frame size %d, COE: %d\n",
3293 frame_len, status);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003294 }
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003295
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003296 /* The zero-copy is always used for all the sizes
3297 * in case of GMAC4 because it needs
3298 * to refill the used descriptors, always.
3299 */
3300 if (unlikely(!priv->plat->has_gmac4 &&
3301 ((frame_len < priv->rx_copybreak) ||
Joao Pinto54139cf2017-04-06 09:49:09 +01003302 stmmac_rx_threshold_count(rx_q)))) {
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003303 skb = netdev_alloc_skb_ip_align(priv->dev,
3304 frame_len);
3305 if (unlikely(!skb)) {
3306 if (net_ratelimit())
3307 dev_warn(priv->device,
3308 "packet dropped\n");
3309 priv->dev->stats.rx_dropped++;
3310 break;
3311 }
3312
3313 dma_sync_single_for_cpu(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003314 rx_q->rx_skbuff_dma
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003315 [entry], frame_len,
3316 DMA_FROM_DEVICE);
3317 skb_copy_to_linear_data(skb,
Joao Pinto54139cf2017-04-06 09:49:09 +01003318 rx_q->
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003319 rx_skbuff[entry]->data,
3320 frame_len);
3321
3322 skb_put(skb, frame_len);
3323 dma_sync_single_for_device(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003324 rx_q->rx_skbuff_dma
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003325 [entry], frame_len,
3326 DMA_FROM_DEVICE);
3327 } else {
Joao Pinto54139cf2017-04-06 09:49:09 +01003328 skb = rx_q->rx_skbuff[entry];
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003329 if (unlikely(!skb)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003330 netdev_err(priv->dev,
3331 "%s: Inconsistent Rx chain\n",
3332 priv->dev->name);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003333 priv->dev->stats.rx_dropped++;
3334 break;
3335 }
3336 prefetch(skb->data - NET_IP_ALIGN);
Joao Pinto54139cf2017-04-06 09:49:09 +01003337 rx_q->rx_skbuff[entry] = NULL;
3338 rx_q->rx_zeroc_thresh++;
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003339
3340 skb_put(skb, frame_len);
3341 dma_unmap_single(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003342 rx_q->rx_skbuff_dma[entry],
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003343 priv->dma_buf_sz,
3344 DMA_FROM_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003345 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003346
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003347 if (netif_msg_pktdata(priv)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003348 netdev_dbg(priv->dev, "frame received (%dbytes)",
3349 frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003350 print_pkt(skb->data, frame_len);
3351 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003352
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01003353 stmmac_get_rx_hwtstamp(priv, p, np, skb);
3354
Vince Bridgersb9381982014-01-14 13:42:05 -06003355 stmmac_rx_vlan(priv->dev, skb);
3356
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003357 skb->protocol = eth_type_trans(skb, priv->dev);
3358
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003359 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003360 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00003361 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003362 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00003363
LABBE Corentin5bacd772017-03-29 07:05:40 +02003364 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003365
3366 priv->dev->stats.rx_packets++;
3367 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003368 }
3369 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003370 }
3371
Joao Pinto54139cf2017-04-06 09:49:09 +01003372 stmmac_rx_refill(priv, queue);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003373
3374 priv->xstats.rx_pkt_n += count;
3375
3376 return count;
3377}
3378
3379/**
3380 * stmmac_poll - stmmac poll method (NAPI)
3381 * @napi : pointer to the napi structure.
3382 * @budget : maximum number of packets that the current CPU can receive from
3383 * all interfaces.
3384 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00003385 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003386 */
3387static int stmmac_poll(struct napi_struct *napi, int budget)
3388{
LABBE Corentin5bacd772017-03-29 07:05:40 +02003389 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
Joao Pintoce736782017-04-06 09:49:10 +01003390 u32 tx_count = priv->plat->tx_queues_to_use;
LABBE Corentin5bacd772017-03-29 07:05:40 +02003391 u32 chan = STMMAC_CHAN0;
Joao Pinto54139cf2017-04-06 09:49:09 +01003392 int work_done = 0;
3393 u32 queue = chan;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003394
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00003395 priv->xstats.napi_poll++;
Joao Pintoce736782017-04-06 09:49:10 +01003396
3397 /* check all the queues */
3398 for (queue = 0; queue < tx_count; queue++)
3399 stmmac_tx_clean(priv, queue);
3400
3401 queue = chan;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003402
Joao Pinto54139cf2017-04-06 09:49:09 +01003403 work_done = stmmac_rx(priv, budget, queue);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003404 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08003405 napi_complete_done(napi, work_done);
Joao Pinto4f513ec2017-03-15 11:04:46 +00003406 stmmac_enable_dma_irq(priv, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003407 }
3408 return work_done;
3409}
3410
3411/**
3412 * stmmac_tx_timeout
3413 * @dev : Pointer to net device structure
3414 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00003415 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003416 * netdev structure and arrange for the device to be reset to a sane state
3417 * in order to transmit a new packet.
3418 */
3419static void stmmac_tx_timeout(struct net_device *dev)
3420{
3421 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pintoce736782017-04-06 09:49:10 +01003422 u32 tx_count = priv->plat->tx_queues_to_use;
3423 u32 chan;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003424
3425 /* Clear Tx resources and restart transmitting again */
Joao Pintoce736782017-04-06 09:49:10 +01003426 for (chan = 0; chan < tx_count; chan++)
3427 stmmac_tx_err(priv, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003428}
3429
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003430/**
Jiri Pirko01789342011-08-16 06:29:00 +00003431 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003432 * @dev : pointer to the device structure
3433 * Description:
3434 * This function is a driver entry point which gets called by the kernel
3435 * whenever multicast addresses must be enabled/disabled.
3436 * Return value:
3437 * void.
3438 */
Jiri Pirko01789342011-08-16 06:29:00 +00003439static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003440{
3441 struct stmmac_priv *priv = netdev_priv(dev);
3442
Vince Bridgers3b57de92014-07-31 15:49:17 -05003443 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003444}
3445
3446/**
3447 * stmmac_change_mtu - entry point to change MTU size for the device.
3448 * @dev : device pointer.
3449 * @new_mtu : the new MTU size for the device.
3450 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
3451 * to drive packet transmission. Ethernet has an MTU of 1500 octets
3452 * (ETH_DATA_LEN). This value can be changed with ifconfig.
3453 * Return value:
3454 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3455 * file on failure.
3456 */
3457static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
3458{
LABBE Corentin38ddc592016-11-16 20:09:39 +01003459 struct stmmac_priv *priv = netdev_priv(dev);
3460
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003461 if (netif_running(dev)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003462 netdev_err(priv->dev, "must be stopped to change its MTU\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003463 return -EBUSY;
3464 }
3465
Michał Mirosław5e982f32011-04-09 02:46:55 +00003466 dev->mtu = new_mtu;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003467
Michał Mirosław5e982f32011-04-09 02:46:55 +00003468 netdev_update_features(dev);
3469
3470 return 0;
3471}
3472
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003473static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003474 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00003475{
3476 struct stmmac_priv *priv = netdev_priv(dev);
3477
Deepak SIKRI38912bd2012-04-04 04:33:21 +00003478 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00003479 features &= ~NETIF_F_RXCSUM;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003480
Michał Mirosław5e982f32011-04-09 02:46:55 +00003481 if (!priv->plat->tx_coe)
Tom Herberta1882222015-12-14 11:19:43 -08003482 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosław5e982f32011-04-09 02:46:55 +00003483
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00003484 /* Some GMAC devices have a bugged Jumbo frame support that
3485 * needs to have the Tx COE disabled for oversized frames
3486 * (due to limited buffer sizes). In this case we disable
LABBE Corentin8d45e422017-02-08 09:31:08 +01003487 * the TX csum insertion in the TDES and not use SF.
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003488 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00003489 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08003490 features &= ~NETIF_F_CSUM_MASK;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00003491
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003492 /* Disable tso if asked by ethtool */
3493 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
3494 if (features & NETIF_F_TSO)
3495 priv->tso = true;
3496 else
3497 priv->tso = false;
3498 }
3499
Michał Mirosław5e982f32011-04-09 02:46:55 +00003500 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003501}
3502
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003503static int stmmac_set_features(struct net_device *netdev,
3504 netdev_features_t features)
3505{
3506 struct stmmac_priv *priv = netdev_priv(netdev);
3507
3508 /* Keep the COE Type in case of csum is supporting */
3509 if (features & NETIF_F_RXCSUM)
3510 priv->hw->rx_csum = priv->plat->rx_coe;
3511 else
3512 priv->hw->rx_csum = 0;
3513 /* No check needed because rx_coe has been set before and it will be
3514 * fixed in case of issue.
3515 */
3516 priv->hw->mac->rx_ipc(priv->hw);
3517
3518 return 0;
3519}
3520
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003521/**
3522 * stmmac_interrupt - main ISR
3523 * @irq: interrupt number.
3524 * @dev_id: to pass the net device pointer.
3525 * Description: this is the main driver interrupt service routine.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003526 * It can call:
3527 * o DMA service routine (to manage incoming frame reception and transmission
3528 * status)
3529 * o Core interrupts to manage: remote wake-up, management counter, LPI
3530 * interrupts.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003531 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003532static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
3533{
3534 struct net_device *dev = (struct net_device *)dev_id;
3535 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto7bac4e12017-03-15 11:04:55 +00003536 u32 rx_cnt = priv->plat->rx_queues_to_use;
3537 u32 tx_cnt = priv->plat->tx_queues_to_use;
3538 u32 queues_count;
3539 u32 queue;
3540
3541 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003542
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003543 if (priv->irq_wake)
3544 pm_wakeup_event(priv->device, 0);
3545
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003546 if (unlikely(!dev)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003547 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003548 return IRQ_NONE;
3549 }
3550
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003551 /* To handle GMAC own interrupts */
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003552 if ((priv->plat->has_gmac) || (priv->plat->has_gmac4)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003553 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00003554 &priv->xstats);
Joao Pinto8f71a882017-03-10 18:24:57 +00003555
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003556 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003557 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00003558 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003559 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00003560 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003561 priv->tx_path_in_lpi_mode = false;
Joao Pinto7bac4e12017-03-15 11:04:55 +00003562 }
3563
3564 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3565 for (queue = 0; queue < queues_count; queue++) {
Joao Pinto54139cf2017-04-06 09:49:09 +01003566 struct stmmac_rx_queue *rx_q =
3567 &priv->rx_queue[queue];
3568
Joao Pinto7bac4e12017-03-15 11:04:55 +00003569 status |=
3570 priv->hw->mac->host_mtl_irq_status(priv->hw,
3571 queue);
3572
3573 if (status & CORE_IRQ_MTL_RX_OVERFLOW &&
3574 priv->hw->dma->set_rx_tail_ptr)
3575 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
Joao Pinto54139cf2017-04-06 09:49:09 +01003576 rx_q->rx_tail_addr,
Joao Pinto7bac4e12017-03-15 11:04:55 +00003577 queue);
3578 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003579 }
Giuseppe CAVALLARO70523e632016-06-24 15:16:24 +02003580
3581 /* PCS link status */
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02003582 if (priv->hw->pcs) {
Giuseppe CAVALLARO70523e632016-06-24 15:16:24 +02003583 if (priv->xstats.pcs_link)
3584 netif_carrier_on(dev);
3585 else
3586 netif_carrier_off(dev);
3587 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003588 }
3589
3590 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00003591 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003592
3593 return IRQ_HANDLED;
3594}
3595
3596#ifdef CONFIG_NET_POLL_CONTROLLER
3597/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003598 * to allow network I/O with interrupts disabled.
3599 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003600static void stmmac_poll_controller(struct net_device *dev)
3601{
3602 disable_irq(dev->irq);
3603 stmmac_interrupt(dev->irq, dev);
3604 enable_irq(dev->irq);
3605}
3606#endif
3607
3608/**
3609 * stmmac_ioctl - Entry point for the Ioctl
3610 * @dev: Device pointer.
3611 * @rq: An IOCTL specefic structure, that can contain a pointer to
3612 * a proprietary structure used to pass information to the driver.
3613 * @cmd: IOCTL command
3614 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003615 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003616 */
3617static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3618{
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003619 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003620
3621 if (!netif_running(dev))
3622 return -EINVAL;
3623
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003624 switch (cmd) {
3625 case SIOCGMIIPHY:
3626 case SIOCGMIIREG:
3627 case SIOCSMIIREG:
Philippe Reynesd6d50c72016-10-03 08:28:19 +02003628 if (!dev->phydev)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003629 return -EINVAL;
Philippe Reynesd6d50c72016-10-03 08:28:19 +02003630 ret = phy_mii_ioctl(dev->phydev, rq, cmd);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003631 break;
3632 case SIOCSHWTSTAMP:
3633 ret = stmmac_hwtstamp_ioctl(dev, rq);
3634 break;
3635 default:
3636 break;
3637 }
Richard Cochran28b04112010-07-17 08:48:55 +00003638
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003639 return ret;
3640}
3641
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01003642#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003643static struct dentry *stmmac_fs_dir;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003644
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003645static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003646 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003647{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003648 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003649 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
3650 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003651
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003652 for (i = 0; i < size; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003653 if (extend_desc) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003654 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003655 i, (unsigned int)virt_to_phys(ep),
Michael Weiserf8be0d72016-11-14 18:58:05 +01003656 le32_to_cpu(ep->basic.des0),
3657 le32_to_cpu(ep->basic.des1),
3658 le32_to_cpu(ep->basic.des2),
3659 le32_to_cpu(ep->basic.des3));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003660 ep++;
3661 } else {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003662 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003663 i, (unsigned int)virt_to_phys(ep),
Michael Weiserf8be0d72016-11-14 18:58:05 +01003664 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
3665 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003666 p++;
3667 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003668 seq_printf(seq, "\n");
3669 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003670}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003671
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003672static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
3673{
3674 struct net_device *dev = seq->private;
3675 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto54139cf2017-04-06 09:49:09 +01003676 u32 rx_count = priv->plat->rx_queues_to_use;
Joao Pintoce736782017-04-06 09:49:10 +01003677 u32 tx_count = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01003678 u32 queue;
3679
3680 for (queue = 0; queue < rx_count; queue++) {
3681 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3682
3683 seq_printf(seq, "RX Queue %d:\n", queue);
3684
3685 if (priv->extend_desc) {
3686 seq_printf(seq, "Extended descriptor ring:\n");
3687 sysfs_display_ring((void *)rx_q->dma_erx,
3688 DMA_RX_SIZE, 1, seq);
3689 } else {
3690 seq_printf(seq, "Descriptor ring:\n");
3691 sysfs_display_ring((void *)rx_q->dma_rx,
3692 DMA_RX_SIZE, 0, seq);
3693 }
3694 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003695
Joao Pintoce736782017-04-06 09:49:10 +01003696 for (queue = 0; queue < tx_count; queue++) {
3697 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3698
3699 seq_printf(seq, "TX Queue %d:\n", queue);
3700
3701 if (priv->extend_desc) {
3702 seq_printf(seq, "Extended descriptor ring:\n");
3703 sysfs_display_ring((void *)tx_q->dma_etx,
3704 DMA_TX_SIZE, 1, seq);
3705 } else {
3706 seq_printf(seq, "Descriptor ring:\n");
3707 sysfs_display_ring((void *)tx_q->dma_tx,
3708 DMA_TX_SIZE, 0, seq);
3709 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003710 }
3711
3712 return 0;
3713}
3714
3715static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
3716{
3717 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
3718}
3719
Pavel Machek22d3efe2016-11-28 12:55:59 +01003720/* Debugfs files, should appear in /sys/kernel/debug/stmmaceth/eth0 */
3721
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003722static const struct file_operations stmmac_rings_status_fops = {
3723 .owner = THIS_MODULE,
3724 .open = stmmac_sysfs_ring_open,
3725 .read = seq_read,
3726 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00003727 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003728};
3729
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003730static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
3731{
3732 struct net_device *dev = seq->private;
3733 struct stmmac_priv *priv = netdev_priv(dev);
3734
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00003735 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003736 seq_printf(seq, "DMA HW features not supported\n");
3737 return 0;
3738 }
3739
3740 seq_printf(seq, "==============================\n");
3741 seq_printf(seq, "\tDMA HW features\n");
3742 seq_printf(seq, "==============================\n");
3743
Pavel Machek22d3efe2016-11-28 12:55:59 +01003744 seq_printf(seq, "\t10/100 Mbps: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003745 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003746 seq_printf(seq, "\t1000 Mbps: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003747 (priv->dma_cap.mbps_1000) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003748 seq_printf(seq, "\tHalf duplex: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003749 (priv->dma_cap.half_duplex) ? "Y" : "N");
3750 seq_printf(seq, "\tHash Filter: %s\n",
3751 (priv->dma_cap.hash_filter) ? "Y" : "N");
3752 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
3753 (priv->dma_cap.multi_addr) ? "Y" : "N");
LABBE Corentin8d45e422017-02-08 09:31:08 +01003754 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003755 (priv->dma_cap.pcs) ? "Y" : "N");
3756 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
3757 (priv->dma_cap.sma_mdio) ? "Y" : "N");
3758 seq_printf(seq, "\tPMT Remote wake up: %s\n",
3759 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
3760 seq_printf(seq, "\tPMT Magic Frame: %s\n",
3761 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
3762 seq_printf(seq, "\tRMON module: %s\n",
3763 (priv->dma_cap.rmon) ? "Y" : "N");
3764 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
3765 (priv->dma_cap.time_stamp) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003766 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003767 (priv->dma_cap.atime_stamp) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003768 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003769 (priv->dma_cap.eee) ? "Y" : "N");
3770 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
3771 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
3772 (priv->dma_cap.tx_coe) ? "Y" : "N");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003773 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3774 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
3775 (priv->dma_cap.rx_coe) ? "Y" : "N");
3776 } else {
3777 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
3778 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
3779 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
3780 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
3781 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003782 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
3783 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
3784 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
3785 priv->dma_cap.number_rx_channel);
3786 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
3787 priv->dma_cap.number_tx_channel);
3788 seq_printf(seq, "\tEnhanced descriptors: %s\n",
3789 (priv->dma_cap.enh_desc) ? "Y" : "N");
3790
3791 return 0;
3792}
3793
3794static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
3795{
3796 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
3797}
3798
3799static const struct file_operations stmmac_dma_cap_fops = {
3800 .owner = THIS_MODULE,
3801 .open = stmmac_sysfs_dma_cap_open,
3802 .read = seq_read,
3803 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00003804 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003805};
3806
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003807static int stmmac_init_fs(struct net_device *dev)
3808{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003809 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003810
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003811 /* Create per netdev entries */
3812 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
3813
3814 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003815 netdev_err(priv->dev, "ERROR failed to create debugfs directory\n");
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003816
3817 return -ENOMEM;
3818 }
3819
3820 /* Entry to report DMA RX/TX rings */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003821 priv->dbgfs_rings_status =
3822 debugfs_create_file("descriptors_status", S_IRUGO,
3823 priv->dbgfs_dir, dev,
3824 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003825
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003826 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003827 netdev_err(priv->dev, "ERROR creating stmmac ring debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003828 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003829
3830 return -ENOMEM;
3831 }
3832
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003833 /* Entry to report the DMA HW features */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003834 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
3835 priv->dbgfs_dir,
3836 dev, &stmmac_dma_cap_fops);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003837
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003838 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003839 netdev_err(priv->dev, "ERROR creating stmmac MMC debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003840 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003841
3842 return -ENOMEM;
3843 }
3844
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003845 return 0;
3846}
3847
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003848static void stmmac_exit_fs(struct net_device *dev)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003849{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003850 struct stmmac_priv *priv = netdev_priv(dev);
3851
3852 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003853}
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01003854#endif /* CONFIG_DEBUG_FS */
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003855
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003856static const struct net_device_ops stmmac_netdev_ops = {
3857 .ndo_open = stmmac_open,
3858 .ndo_start_xmit = stmmac_xmit,
3859 .ndo_stop = stmmac_release,
3860 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00003861 .ndo_fix_features = stmmac_fix_features,
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003862 .ndo_set_features = stmmac_set_features,
Jiri Pirko01789342011-08-16 06:29:00 +00003863 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003864 .ndo_tx_timeout = stmmac_tx_timeout,
3865 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003866#ifdef CONFIG_NET_POLL_CONTROLLER
3867 .ndo_poll_controller = stmmac_poll_controller,
3868#endif
3869 .ndo_set_mac_address = eth_mac_addr,
3870};
3871
3872/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003873 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003874 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003875 * Description: this function is to configure the MAC device according to
3876 * some platform parameters or the HW capability register. It prepares the
3877 * driver to use either ring or chain modes and to setup either enhanced or
3878 * normal descriptors.
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003879 */
3880static int stmmac_hw_init(struct stmmac_priv *priv)
3881{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003882 struct mac_device_info *mac;
3883
3884 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00003885 if (priv->plat->has_gmac) {
3886 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05003887 mac = dwmac1000_setup(priv->ioaddr,
3888 priv->plat->multicast_filter_bins,
Alexandre TORGUEc623d142016-04-01 11:37:27 +02003889 priv->plat->unicast_filter_entries,
3890 &priv->synopsys_id);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003891 } else if (priv->plat->has_gmac4) {
3892 priv->dev->priv_flags |= IFF_UNICAST_FLT;
3893 mac = dwmac4_setup(priv->ioaddr,
3894 priv->plat->multicast_filter_bins,
3895 priv->plat->unicast_filter_entries,
3896 &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00003897 } else {
Alexandre TORGUEc623d142016-04-01 11:37:27 +02003898 mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00003899 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003900 if (!mac)
3901 return -ENOMEM;
3902
3903 priv->hw = mac;
3904
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003905 /* To use the chained or ring mode */
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003906 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3907 priv->hw->mode = &dwmac4_ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003908 } else {
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003909 if (chain_mode) {
3910 priv->hw->mode = &chain_mode_ops;
LABBE Corentin38ddc592016-11-16 20:09:39 +01003911 dev_info(priv->device, "Chain mode enabled\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003912 priv->mode = STMMAC_CHAIN_MODE;
3913 } else {
3914 priv->hw->mode = &ring_mode_ops;
LABBE Corentin38ddc592016-11-16 20:09:39 +01003915 dev_info(priv->device, "Ring mode enabled\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003916 priv->mode = STMMAC_RING_MODE;
3917 }
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003918 }
3919
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003920 /* Get the HW capability (new GMAC newer than 3.50a) */
3921 priv->hw_cap_support = stmmac_get_hw_features(priv);
3922 if (priv->hw_cap_support) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003923 dev_info(priv->device, "DMA HW capability register supported\n");
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003924
3925 /* We can override some gmac/dma configuration fields: e.g.
3926 * enh_desc, tx_coe (e.g. that are passed through the
3927 * platform) with the values from the HW capability
3928 * register (if supported).
3929 */
3930 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003931 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02003932 priv->hw->pmt = priv->plat->pmt;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00003933
Ezequiel Garciaa8df35d2016-05-16 12:41:07 -03003934 /* TXCOE doesn't work in thresh DMA mode */
3935 if (priv->plat->force_thresh_dma_mode)
3936 priv->plat->tx_coe = 0;
3937 else
3938 priv->plat->tx_coe = priv->dma_cap.tx_coe;
3939
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003940 /* In case of GMAC4 rx_coe is from HW cap register. */
3941 priv->plat->rx_coe = priv->dma_cap.rx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00003942
3943 if (priv->dma_cap.rx_coe_type2)
3944 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
3945 else if (priv->dma_cap.rx_coe_type1)
3946 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
3947
LABBE Corentin38ddc592016-11-16 20:09:39 +01003948 } else {
3949 dev_info(priv->device, "No HW DMA feature register supported\n");
3950 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003951
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003952 /* To use alternate (extended), normal or GMAC4 descriptor structures */
3953 if (priv->synopsys_id >= DWMAC_CORE_4_00)
3954 priv->hw->desc = &dwmac4_desc_ops;
3955 else
3956 stmmac_selec_desc_mode(priv);
Byungho An61369d02013-06-28 16:35:32 +09003957
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003958 if (priv->plat->rx_coe) {
3959 priv->hw->rx_csum = priv->plat->rx_coe;
LABBE Corentin38ddc592016-11-16 20:09:39 +01003960 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003961 if (priv->synopsys_id < DWMAC_CORE_4_00)
LABBE Corentin38ddc592016-11-16 20:09:39 +01003962 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003963 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003964 if (priv->plat->tx_coe)
LABBE Corentin38ddc592016-11-16 20:09:39 +01003965 dev_info(priv->device, "TX Checksum insertion supported\n");
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003966
3967 if (priv->plat->pmt) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003968 dev_info(priv->device, "Wake-Up On Lan supported\n");
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003969 device_set_wakeup_capable(priv->device, 1);
3970 }
3971
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003972 if (priv->dma_cap.tsoen)
LABBE Corentin38ddc592016-11-16 20:09:39 +01003973 dev_info(priv->device, "TSO supported\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003974
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003975 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00003976}
3977
3978/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003979 * stmmac_dvr_probe
3980 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00003981 * @plat_dat: platform data pointer
Joachim Eastwoode56788c2015-05-20 20:03:07 +02003982 * @res: stmmac resource pointer
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003983 * Description: this is the main probe function used to
3984 * call the alloc_etherdev, allocate the priv structure.
Andy Shevchenko9afec6e2015-01-27 18:38:03 +02003985 * Return:
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003986 * returns 0 on success, otherwise errno.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003987 */
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003988int stmmac_dvr_probe(struct device *device,
3989 struct plat_stmmacenet_data *plat_dat,
3990 struct stmmac_resources *res)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003991{
LABBE Corentin5bacd772017-03-29 07:05:40 +02003992 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003993 struct net_device *ndev = NULL;
3994 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003995
LABBE Corentin5bacd772017-03-29 07:05:40 +02003996 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00003997 if (!ndev)
Joachim Eastwood15ffac72015-05-20 20:03:08 +02003998 return -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003999
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004000 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004001
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004002 priv = netdev_priv(ndev);
4003 priv->device = device;
4004 priv->dev = ndev;
4005
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004006 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004007 priv->pause = pause;
4008 priv->plat = plat_dat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +02004009 priv->ioaddr = res->addr;
4010 priv->dev->base_addr = (unsigned long)res->addr;
4011
4012 priv->dev->irq = res->irq;
4013 priv->wol_irq = res->wol_irq;
4014 priv->lpi_irq = res->lpi_irq;
4015
4016 if (res->mac)
4017 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004018
Joachim Eastwooda7a62682015-07-17 23:48:17 +02004019 dev_set_drvdata(device, priv->dev);
Joachim Eastwood803f8fc2015-05-20 20:03:06 +02004020
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004021 /* Verify driver arguments */
4022 stmmac_verify_args();
4023
4024 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00004025 * this needs to have multiple instances
4026 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004027 if ((phyaddr >= 0) && (phyaddr <= 31))
4028 priv->plat->phy_addr = phyaddr;
4029
jpintof573c0b2017-01-09 12:35:09 +00004030 if (priv->plat->stmmac_rst)
4031 reset_control_deassert(priv->plat->stmmac_rst);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08004032
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004033 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00004034 ret = stmmac_hw_init(priv);
4035 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08004036 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004037
4038 ndev->netdev_ops = &stmmac_netdev_ops;
4039
4040 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4041 NETIF_F_RXCSUM;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004042
4043 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
4044 ndev->hw_features |= NETIF_F_TSO;
4045 priv->tso = true;
LABBE Corentin38ddc592016-11-16 20:09:39 +01004046 dev_info(priv->device, "TSO feature enabled\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004047 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004048 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
4049 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004050#ifdef STMMAC_VLAN_TAG_USED
4051 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00004052 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004053#endif
4054 priv->msg_enable = netif_msg_init(debug, default_msg_level);
4055
Jarod Wilson44770e12016-10-17 15:54:17 -04004056 /* MTU range: 46 - hw-specific max */
4057 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
4058 if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
4059 ndev->max_mtu = JUMBO_LEN;
4060 else
4061 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Kweh, Hock Leonga2cd64f2017-01-07 17:32:03 +08004062 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
4063 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
4064 */
4065 if ((priv->plat->maxmtu < ndev->max_mtu) &&
4066 (priv->plat->maxmtu >= ndev->min_mtu))
Jarod Wilson44770e12016-10-17 15:54:17 -04004067 ndev->max_mtu = priv->plat->maxmtu;
Kweh, Hock Leonga2cd64f2017-01-07 17:32:03 +08004068 else if (priv->plat->maxmtu < ndev->min_mtu)
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004069 dev_warn(priv->device,
4070 "%s: warning: maxmtu having invalid value (%d)\n",
4071 __func__, priv->plat->maxmtu);
Jarod Wilson44770e12016-10-17 15:54:17 -04004072
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004073 if (flow_ctrl)
4074 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
4075
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00004076 /* Rx Watchdog is available in the COREs newer than the 3.40.
4077 * In some case, for example on bugged HW this feature
4078 * has to be disable and this can be done by passing the
4079 * riwt_off field from the platform.
4080 */
4081 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
4082 priv->use_riwt = 1;
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004083 dev_info(priv->device,
4084 "Enable RX Mitigation via HW Watchdog Timer\n");
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00004085 }
4086
LABBE Corentin5bacd772017-03-29 07:05:40 +02004087 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004088
Vlad Lunguf8e96162010-11-29 22:52:52 +00004089 spin_lock_init(&priv->lock);
4090
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00004091 /* If a specific clk_csr value is passed from the platform
4092 * this means that the CSR Clock Range selection cannot be
4093 * changed at run-time and it is fixed. Viceversa the driver'll try to
4094 * set the MDC clock dynamically according to the csr actual
4095 * clock input.
4096 */
4097 if (!priv->plat->clk_csr)
4098 stmmac_clk_csr_set(priv);
4099 else
4100 priv->clk_csr = priv->plat->clk_csr;
4101
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00004102 stmmac_check_pcs_mode(priv);
4103
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02004104 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4105 priv->hw->pcs != STMMAC_PCS_TBI &&
4106 priv->hw->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00004107 /* MDIO bus Registration */
4108 ret = stmmac_mdio_register(ndev);
4109 if (ret < 0) {
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004110 dev_err(priv->device,
4111 "%s: MDIO bus (id: %d) registration failed",
4112 __func__, priv->plat->bus_id);
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00004113 goto error_mdio_register;
4114 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00004115 }
4116
Florian Fainelli57016592016-12-27 18:23:06 -08004117 ret = register_netdev(ndev);
Florian Fainellib2eb09a2016-12-28 15:44:41 -08004118 if (ret) {
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004119 dev_err(priv->device, "%s: ERROR %i registering the device\n",
4120 __func__, ret);
Florian Fainellib2eb09a2016-12-28 15:44:41 -08004121 goto error_netdev_register;
4122 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004123
Florian Fainelli57016592016-12-27 18:23:06 -08004124 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004125
Viresh Kumar6a81c262012-07-30 14:39:41 -07004126error_netdev_register:
Florian Fainellib2eb09a2016-12-28 15:44:41 -08004127 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4128 priv->hw->pcs != STMMAC_PCS_TBI &&
4129 priv->hw->pcs != STMMAC_PCS_RTBI)
4130 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004131error_mdio_register:
LABBE Corentin5bacd772017-03-29 07:05:40 +02004132 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08004133error_hw_init:
Dan Carpenter34a52f32010-12-20 21:34:56 +00004134 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004135
Joachim Eastwood15ffac72015-05-20 20:03:08 +02004136 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004137}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004138EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004139
4140/**
4141 * stmmac_dvr_remove
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004142 * @dev: device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004143 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004144 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004145 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004146int stmmac_dvr_remove(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004147{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004148 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00004149 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004150
LABBE Corentin38ddc592016-11-16 20:09:39 +01004151 netdev_info(priv->dev, "%s: removing driver", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004152
Joao Pintoae4f0d42017-03-15 11:04:47 +00004153 stmmac_stop_all_dma(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004154
LABBE Corentin270c7752017-03-23 14:40:22 +01004155 priv->hw->mac->set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004156 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004157 unregister_netdev(ndev);
jpintof573c0b2017-01-09 12:35:09 +00004158 if (priv->plat->stmmac_rst)
4159 reset_control_assert(priv->plat->stmmac_rst);
4160 clk_disable_unprepare(priv->plat->pclk);
4161 clk_disable_unprepare(priv->plat->stmmac_clk);
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02004162 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4163 priv->hw->pcs != STMMAC_PCS_TBI &&
4164 priv->hw->pcs != STMMAC_PCS_RTBI)
Bryan O'Donoghuee7434712015-04-16 17:56:03 +01004165 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004166 free_netdev(ndev);
4167
4168 return 0;
4169}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004170EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004171
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004172/**
4173 * stmmac_suspend - suspend callback
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004174 * @dev: device pointer
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004175 * Description: this is the function to suspend the device and it is called
4176 * by the platform driver to stop the network queue, release the resources,
4177 * program the PMT register (for WoL), clean and release driver resources.
4178 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004179int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004180{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004181 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004182 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004183 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004184
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004185 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004186 return 0;
4187
Philippe Reynesd6d50c72016-10-03 08:28:19 +02004188 if (ndev->phydev)
4189 phy_stop(ndev->phydev);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00004190
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004191 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004192
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004193 netif_device_detach(ndev);
LABBE Corentin5bacd772017-03-29 07:05:40 +02004194 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004195
LABBE Corentin5bacd772017-03-29 07:05:40 +02004196 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004197
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004198 /* Stop TX/RX DMA */
Joao Pintoae4f0d42017-03-15 11:04:47 +00004199 stmmac_stop_all_dma(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00004200
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004201 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00004202 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05004203 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00004204 priv->irq_wake = 1;
4205 } else {
LABBE Corentin270c7752017-03-23 14:40:22 +01004206 priv->hw->mac->set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00004207 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00004208 /* Disable clock in case of PWM is off */
jpintof573c0b2017-01-09 12:35:09 +00004209 clk_disable(priv->plat->pclk);
4210 clk_disable(priv->plat->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00004211 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004212 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05004213
4214 priv->oldlink = 0;
LABBE Corentinbd006322017-02-15 10:46:40 +01004215 priv->speed = SPEED_UNKNOWN;
4216 priv->oldduplex = DUPLEX_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004217 return 0;
4218}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004219EXPORT_SYMBOL_GPL(stmmac_suspend);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004220
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004221/**
Joao Pinto54139cf2017-04-06 09:49:09 +01004222 * stmmac_reset_queues_param - reset queue parameters
4223 * @dev: device pointer
4224 */
4225static void stmmac_reset_queues_param(struct stmmac_priv *priv)
4226{
4227 u32 rx_cnt = priv->plat->rx_queues_to_use;
Joao Pintoce736782017-04-06 09:49:10 +01004228 u32 tx_cnt = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01004229 u32 queue;
4230
4231 for (queue = 0; queue < rx_cnt; queue++) {
4232 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4233
4234 rx_q->cur_rx = 0;
4235 rx_q->dirty_rx = 0;
4236 }
4237
Joao Pintoce736782017-04-06 09:49:10 +01004238 for (queue = 0; queue < tx_cnt; queue++) {
4239 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4240
4241 tx_q->cur_tx = 0;
4242 tx_q->dirty_tx = 0;
4243 }
Joao Pinto54139cf2017-04-06 09:49:09 +01004244}
4245
4246/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004247 * stmmac_resume - resume callback
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004248 * @dev: device pointer
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004249 * Description: when resume this function is invoked to setup the DMA and CORE
4250 * in a usable state.
4251 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004252int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004253{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004254 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004255 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004256 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004257
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004258 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004259 return 0;
4260
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004261 /* Power Down bit, into the PM register, is cleared
4262 * automatically as soon as a magic packet or a Wake-up frame
4263 * is received. Anyway, it's better to manually clear
4264 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00004265 * from another devices (e.g. serial console).
4266 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00004267 if (device_may_wakeup(priv->device)) {
Vincent Palatinf55d84b2016-06-01 08:53:48 -07004268 spin_lock_irqsave(&priv->lock, flags);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05004269 priv->hw->mac->pmt(priv->hw, 0);
Vincent Palatinf55d84b2016-06-01 08:53:48 -07004270 spin_unlock_irqrestore(&priv->lock, flags);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00004271 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00004272 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00004273 pinctrl_pm_select_default_state(priv->device);
LABBE Corentin8d45e422017-02-08 09:31:08 +01004274 /* enable the clk previously disabled */
jpintof573c0b2017-01-09 12:35:09 +00004275 clk_enable(priv->plat->stmmac_clk);
4276 clk_enable(priv->plat->pclk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00004277 /* reset the phy so that it's ready */
4278 if (priv->mii)
4279 stmmac_mdio_reset(priv->mii);
4280 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004281
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004282 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004283
Vincent Palatinf55d84b2016-06-01 08:53:48 -07004284 spin_lock_irqsave(&priv->lock, flags);
4285
Joao Pinto54139cf2017-04-06 09:49:09 +01004286 stmmac_reset_queues_param(priv);
4287
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004288 /* reset private mss value to force mss context settings at
4289 * next tso xmit (only used for gmac4).
4290 */
4291 priv->mss = 0;
4292
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01004293 stmmac_clear_descriptors(priv);
4294
Huacai Chenfe1319292014-12-19 22:38:18 +08004295 stmmac_hw_setup(ndev, false);
Giuseppe CAVALLARO777da2302014-11-04 17:08:09 +01004296 stmmac_init_tx_coalesce(priv);
Giuseppe CAVALLAROac316c72015-11-26 08:35:41 +01004297 stmmac_set_rx_mode(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004298
LABBE Corentin5bacd772017-03-29 07:05:40 +02004299 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004300
LABBE Corentin5bacd772017-03-29 07:05:40 +02004301 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004302
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004303 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00004304
Philippe Reynesd6d50c72016-10-03 08:28:19 +02004305 if (ndev->phydev)
4306 phy_start(ndev->phydev);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00004307
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004308 return 0;
4309}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004310EXPORT_SYMBOL_GPL(stmmac_resume);
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00004311
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004312#ifndef MODULE
4313static int __init stmmac_cmdline_opt(char *str)
4314{
4315 char *opt;
4316
4317 if (!str || !*str)
4318 return -EINVAL;
4319 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004320 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004321 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004322 goto err;
4323 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004324 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004325 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004326 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004327 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004328 goto err;
4329 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004330 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004331 goto err;
4332 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004333 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004334 goto err;
4335 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004336 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004337 goto err;
4338 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004339 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004340 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00004341 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00004342 if (kstrtoint(opt + 10, 0, &eee_timer))
4343 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00004344 } else if (!strncmp(opt, "chain_mode:", 11)) {
4345 if (kstrtoint(opt + 11, 0, &chain_mode))
4346 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004347 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004348 }
4349 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004350
4351err:
4352 pr_err("%s: ERROR broken module parameter conversion", __func__);
4353 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004354}
4355
4356__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00004357#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05004358
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004359static int __init stmmac_init(void)
4360{
4361#ifdef CONFIG_DEBUG_FS
4362 /* Create debugfs main directory if it doesn't exist yet */
4363 if (!stmmac_fs_dir) {
4364 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
4365
4366 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
4367 pr_err("ERROR %s, debugfs create directory failed\n",
4368 STMMAC_RESOURCE_NAME);
4369
4370 return -ENOMEM;
4371 }
4372 }
4373#endif
4374
4375 return 0;
4376}
4377
4378static void __exit stmmac_exit(void)
4379{
4380#ifdef CONFIG_DEBUG_FS
4381 debugfs_remove_recursive(stmmac_fs_dir);
4382#endif
4383}
4384
4385module_init(stmmac_init)
4386module_exit(stmmac_exit)
4387
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05004388MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
4389MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
4390MODULE_LICENSE("GPL");