blob: 1186ac902becb1dcca42a2be770a581ff3831d88 [file] [log] [blame]
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001/*******************************************************************************
2 This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
3 ST Ethernet IPs are built around a Synopsys IP Core.
4
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00005 Copyright(C) 2007-2011 STMicroelectronics Ltd
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07006
7 This program is free software; you can redistribute it and/or modify it
8 under the terms and conditions of the GNU General Public License,
9 version 2, as published by the Free Software Foundation.
10
11 This program is distributed in the hope it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19
20 The full GNU General Public License is included in this distribution in
21 the file called "COPYING".
22
23 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
24
25 Documentation available at:
26 http://www.stlinux.com
27 Support available at:
28 https://bugzilla.stlinux.com/
29*******************************************************************************/
30
Viresh Kumar6a81c262012-07-30 14:39:41 -070031#include <linux/clk.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070032#include <linux/kernel.h>
33#include <linux/interrupt.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070034#include <linux/ip.h>
35#include <linux/tcp.h>
36#include <linux/skbuff.h>
37#include <linux/ethtool.h>
38#include <linux/if_ether.h>
39#include <linux/crc32.h>
40#include <linux/mii.h>
Jiri Pirko01789342011-08-16 06:29:00 +000041#include <linux/if.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070042#include <linux/if_vlan.h>
43#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040045#include <linux/prefetch.h>
Srinivas Kandagatladb88f102014-01-16 10:52:52 +000046#include <linux/pinctrl/consumer.h>
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +010047#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000048#include <linux/debugfs.h>
49#include <linux/seq_file.h>
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +010050#endif /* CONFIG_DEBUG_FS */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +000051#include <linux/net_tstamp.h>
52#include "stmmac_ptp.h"
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +000053#include "stmmac.h"
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +080054#include <linux/reset.h>
Mathieu Olivari5790cf32015-05-27 11:02:47 -070055#include <linux/of_mdio.h>
Phil Reid19d857c2015-12-14 11:32:01 +080056#include "dwmac1000.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070057
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070058#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070059
60/* Module parameters */
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000061#define TX_TIMEO 5000
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070062static int watchdog = TX_TIMEO;
63module_param(watchdog, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000064MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070065
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000066static int debug = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070067module_param(debug, int, S_IRUGO | S_IWUSR);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000068MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070069
stephen hemminger47d1f712013-12-30 10:38:57 -080070static int phyaddr = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070071module_param(phyaddr, int, S_IRUGO);
72MODULE_PARM_DESC(phyaddr, "Physical device address");
73
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +010074#define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +010075#define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070076
77static int flow_ctrl = FLOW_OFF;
78module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
79MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
80
81static int pause = PAUSE_TIME;
82module_param(pause, int, S_IRUGO | S_IWUSR);
83MODULE_PARM_DESC(pause, "Flow Control Pause Time");
84
85#define TC_DEFAULT 64
86static int tc = TC_DEFAULT;
87module_param(tc, int, S_IRUGO | S_IWUSR);
88MODULE_PARM_DESC(tc, "DMA threshold control value");
89
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +010090#define DEFAULT_BUFSIZE 1536
91static int buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070092module_param(buf_sz, int, S_IRUGO | S_IWUSR);
93MODULE_PARM_DESC(buf_sz, "DMA buffer size");
94
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +010095#define STMMAC_RX_COPYBREAK 256
96
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070097static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
98 NETIF_MSG_LINK | NETIF_MSG_IFUP |
99 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
100
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000101#define STMMAC_DEFAULT_LPI_TIMER 1000
102static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
103module_param(eee_timer, int, S_IRUGO | S_IWUSR);
104MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200105#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000106
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000107/* By default the driver will use the ring mode to manage tx and rx descriptors
108 * but passing this value so user can force to use the chain instead of the ring
109 */
110static unsigned int chain_mode;
111module_param(chain_mode, int, S_IRUGO);
112MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
113
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700114static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700115
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +0100116#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000117static int stmmac_init_fs(struct net_device *dev);
Mathieu Olivari466c5ac2015-05-22 19:03:29 -0700118static void stmmac_exit_fs(struct net_device *dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000119#endif
120
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000121#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
122
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700123/**
124 * stmmac_verify_args - verify the driver parameters.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100125 * Description: it checks the driver parameters and set a default in case of
126 * errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700127 */
128static void stmmac_verify_args(void)
129{
130 if (unlikely(watchdog < 0))
131 watchdog = TX_TIMEO;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100132 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
133 buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700134 if (unlikely(flow_ctrl > 1))
135 flow_ctrl = FLOW_AUTO;
136 else if (likely(flow_ctrl < 0))
137 flow_ctrl = FLOW_OFF;
138 if (unlikely((pause < 0) || (pause > 0xffff)))
139 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000140 if (eee_timer < 0)
141 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700142}
143
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000144/**
145 * stmmac_clk_csr_set - dynamically set the MDC clock
146 * @priv: driver private structure
147 * Description: this is to dynamically set the MDC clock according to the csr
148 * clock input.
149 * Note:
150 * If a specific clk_csr value is passed from the platform
151 * this means that the CSR Clock Range selection cannot be
152 * changed at run-time and it is fixed (as reported in the driver
153 * documentation). Viceversa the driver will try to set the MDC
154 * clock dynamically according to the actual clock input.
155 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000156static void stmmac_clk_csr_set(struct stmmac_priv *priv)
157{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000158 u32 clk_rate;
159
160 clk_rate = clk_get_rate(priv->stmmac_clk);
161
162 /* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000163 * for all other cases except for the below mentioned ones.
164 * For values higher than the IEEE 802.3 specified frequency
165 * we can not estimate the proper divider as it is not known
166 * the frequency of clk_csr_i. So we do not change the default
167 * divider.
168 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000169 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
170 if (clk_rate < CSR_F_35M)
171 priv->clk_csr = STMMAC_CSR_20_35M;
172 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
173 priv->clk_csr = STMMAC_CSR_35_60M;
174 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
175 priv->clk_csr = STMMAC_CSR_60_100M;
176 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
177 priv->clk_csr = STMMAC_CSR_100_150M;
178 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
179 priv->clk_csr = STMMAC_CSR_150_250M;
Phil Reid19d857c2015-12-14 11:32:01 +0800180 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000181 priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000182 }
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000183}
184
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700185static void print_pkt(unsigned char *buf, int len)
186{
Andy Shevchenko424c4f72014-11-07 16:53:12 +0200187 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
188 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700189}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700190
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700191static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
192{
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100193 unsigned avail;
194
195 if (priv->dirty_tx > priv->cur_tx)
196 avail = priv->dirty_tx - priv->cur_tx - 1;
197 else
198 avail = DMA_TX_SIZE - priv->cur_tx + priv->dirty_tx - 1;
199
200 return avail;
201}
202
203static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv)
204{
205 unsigned dirty;
206
207 if (priv->dirty_rx <= priv->cur_rx)
208 dirty = priv->cur_rx - priv->dirty_rx;
209 else
210 dirty = DMA_RX_SIZE - priv->dirty_rx + priv->cur_rx;
211
212 return dirty;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700213}
214
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000215/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100216 * stmmac_hw_fix_mac_speed - callback for speed selection
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000217 * @priv: driver private structure
218 * Description: on some platforms (e.g. ST), some HW system configuraton
219 * registers have to be set according to the link speed negotiated.
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000220 */
221static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
222{
223 struct phy_device *phydev = priv->phydev;
224
225 if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000226 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000227}
228
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000229/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100230 * stmmac_enable_eee_mode - check and enter in LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000231 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100232 * Description: this function is to verify and enter in LPI mode in case of
233 * EEE.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000234 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000235static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
236{
237 /* Check and enter in LPI mode */
238 if ((priv->dirty_tx == priv->cur_tx) &&
239 (priv->tx_path_in_lpi_mode == false))
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500240 priv->hw->mac->set_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000241}
242
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000243/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100244 * stmmac_disable_eee_mode - disable and exit from LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000245 * @priv: driver private structure
246 * Description: this function is to exit and disable EEE in case of
247 * LPI state is true. This is called by the xmit.
248 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000249void stmmac_disable_eee_mode(struct stmmac_priv *priv)
250{
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500251 priv->hw->mac->reset_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000252 del_timer_sync(&priv->eee_ctrl_timer);
253 priv->tx_path_in_lpi_mode = false;
254}
255
256/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100257 * stmmac_eee_ctrl_timer - EEE TX SW timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000258 * @arg : data hook
259 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000260 * if there is no data transfer and if we are not in LPI state,
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000261 * then MAC Transmitter can be moved to LPI state.
262 */
263static void stmmac_eee_ctrl_timer(unsigned long arg)
264{
265 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
266
267 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200268 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000269}
270
271/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100272 * stmmac_eee_init - init EEE
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000273 * @priv: driver private structure
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000274 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100275 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
276 * can also manage EEE, this function enable the LPI state and start related
277 * timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000278 */
279bool stmmac_eee_init(struct stmmac_priv *priv)
280{
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100281 unsigned long flags;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000282 bool ret = false;
283
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200284 /* Using PCS we cannot dial with the phy registers at this stage
285 * so we do not support extra feature like EEE.
286 */
287 if ((priv->pcs == STMMAC_PCS_RGMII) || (priv->pcs == STMMAC_PCS_TBI) ||
288 (priv->pcs == STMMAC_PCS_RTBI))
289 goto out;
290
Giuseppe CAVALLARO56b88c22014-08-28 08:11:43 +0200291 /* Never init EEE in case of a switch is attached */
Giuseppe CAVALLAROa7657f12016-04-01 09:07:16 +0200292 if (priv->phydev->is_pseudo_fixed_link)
Giuseppe CAVALLARO56b88c22014-08-28 08:11:43 +0200293 goto out;
294
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000295 /* MAC core supports the EEE feature. */
296 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100297 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000298
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100299 /* Check if the PHY supports EEE */
300 if (phy_init_eee(priv->phydev, 1)) {
301 /* To manage at run-time if the EEE cannot be supported
302 * anymore (for example because the lp caps have been
303 * changed).
304 * In that case the driver disable own timers.
305 */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100306 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100307 if (priv->eee_active) {
308 pr_debug("stmmac: disable EEE\n");
309 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500310 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100311 tx_lpi_timer);
312 }
313 priv->eee_active = 0;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100314 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100315 goto out;
316 }
317 /* Activate the EEE and start timers */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100318 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200319 if (!priv->eee_active) {
320 priv->eee_active = 1;
Vaishali Thakkarccb36da2015-02-28 00:12:34 +0530321 setup_timer(&priv->eee_ctrl_timer,
322 stmmac_eee_ctrl_timer,
323 (unsigned long)priv);
324 mod_timer(&priv->eee_ctrl_timer,
325 STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000326
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500327 priv->hw->mac->set_eee_timer(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200328 STMMAC_DEFAULT_LIT_LS,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100329 tx_lpi_timer);
Giuseppe CAVALLARO71965352014-08-28 08:11:44 +0200330 }
331 /* Set HW EEE according to the speed */
332 priv->hw->mac->set_eee_pls(priv->hw, priv->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000333
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000334 ret = true;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100335 spin_unlock_irqrestore(&priv->lock, flags);
336
337 pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000338 }
339out:
340 return ret;
341}
342
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100343/* stmmac_get_tx_hwtstamp - get HW TX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000344 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000345 * @entry : descriptor index to be used.
346 * @skb : the socket buffer
347 * Description :
348 * This function will read timestamp from the descriptor & pass it to stack.
349 * and also perform some sanity checks.
350 */
351static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000352 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000353{
354 struct skb_shared_hwtstamps shhwtstamp;
355 u64 ns;
356 void *desc = NULL;
357
358 if (!priv->hwts_tx_en)
359 return;
360
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000361 /* exit if skb doesn't support hw tstamp */
damuzi00075e43642014-01-17 23:47:59 +0800362 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000363 return;
364
365 if (priv->adv_ts)
366 desc = (priv->dma_etx + entry);
367 else
368 desc = (priv->dma_tx + entry);
369
370 /* check tx tstamp status */
371 if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
372 return;
373
374 /* get the valid tstamp */
375 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
376
377 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
378 shhwtstamp.hwtstamp = ns_to_ktime(ns);
379 /* pass tstamp to stack */
380 skb_tstamp_tx(skb, &shhwtstamp);
381
382 return;
383}
384
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100385/* stmmac_get_rx_hwtstamp - get HW RX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000386 * @priv: driver private structure
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000387 * @entry : descriptor index to be used.
388 * @skb : the socket buffer
389 * Description :
390 * This function will read received packet's timestamp from the descriptor
391 * and pass it to stack. It also perform some sanity checks.
392 */
393static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000394 unsigned int entry, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000395{
396 struct skb_shared_hwtstamps *shhwtstamp = NULL;
397 u64 ns;
398 void *desc = NULL;
399
400 if (!priv->hwts_rx_en)
401 return;
402
403 if (priv->adv_ts)
404 desc = (priv->dma_erx + entry);
405 else
406 desc = (priv->dma_rx + entry);
407
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000408 /* exit if rx tstamp is not valid */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000409 if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
410 return;
411
412 /* get valid tstamp */
413 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
414 shhwtstamp = skb_hwtstamps(skb);
415 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
416 shhwtstamp->hwtstamp = ns_to_ktime(ns);
417}
418
419/**
420 * stmmac_hwtstamp_ioctl - control hardware timestamping.
421 * @dev: device pointer.
422 * @ifr: An IOCTL specefic structure, that can contain a pointer to
423 * a proprietary structure used to pass information to the driver.
424 * Description:
425 * This function configures the MAC to enable/disable both outgoing(TX)
426 * and incoming(RX) packets time stamping based on user input.
427 * Return Value:
428 * 0 on success and an appropriate -ve integer on failure.
429 */
430static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
431{
432 struct stmmac_priv *priv = netdev_priv(dev);
433 struct hwtstamp_config config;
Arnd Bergmann0a624152015-09-30 13:26:32 +0200434 struct timespec64 now;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000435 u64 temp = 0;
436 u32 ptp_v2 = 0;
437 u32 tstamp_all = 0;
438 u32 ptp_over_ipv4_udp = 0;
439 u32 ptp_over_ipv6_udp = 0;
440 u32 ptp_over_ethernet = 0;
441 u32 snap_type_sel = 0;
442 u32 ts_master_en = 0;
443 u32 ts_event_en = 0;
444 u32 value = 0;
Phil Reid19d857c2015-12-14 11:32:01 +0800445 u32 sec_inc;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000446
447 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
448 netdev_alert(priv->dev, "No support for HW time stamping\n");
449 priv->hwts_tx_en = 0;
450 priv->hwts_rx_en = 0;
451
452 return -EOPNOTSUPP;
453 }
454
455 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000456 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000457 return -EFAULT;
458
459 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
460 __func__, config.flags, config.tx_type, config.rx_filter);
461
462 /* reserved for future extensions */
463 if (config.flags)
464 return -EINVAL;
465
Ben Hutchings5f3da322013-11-14 00:43:41 +0000466 if (config.tx_type != HWTSTAMP_TX_OFF &&
467 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000468 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000469
470 if (priv->adv_ts) {
471 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000472 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000473 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000474 config.rx_filter = HWTSTAMP_FILTER_NONE;
475 break;
476
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000477 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000478 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000479 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
480 /* take time stamp for all event messages */
481 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
482
483 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
484 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
485 break;
486
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000487 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000488 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000489 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
490 /* take time stamp for SYNC messages only */
491 ts_event_en = PTP_TCR_TSEVNTENA;
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_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000498 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000499 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
500 /* take time stamp for Delay_Req messages only */
501 ts_master_en = PTP_TCR_TSMSTRENA;
502 ts_event_en = PTP_TCR_TSEVNTENA;
503
504 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
505 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
506 break;
507
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000508 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000509 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000510 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
511 ptp_v2 = PTP_TCR_TSVER2ENA;
512 /* take time stamp for all event messages */
513 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
514
515 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
516 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
517 break;
518
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000519 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000520 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000521 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
522 ptp_v2 = PTP_TCR_TSVER2ENA;
523 /* take time stamp for SYNC messages only */
524 ts_event_en = PTP_TCR_TSEVNTENA;
525
526 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
527 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
528 break;
529
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000530 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000531 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000532 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
533 ptp_v2 = PTP_TCR_TSVER2ENA;
534 /* take time stamp for Delay_Req messages only */
535 ts_master_en = PTP_TCR_TSMSTRENA;
536 ts_event_en = PTP_TCR_TSEVNTENA;
537
538 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
539 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
540 break;
541
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000542 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000543 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000544 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
545 ptp_v2 = PTP_TCR_TSVER2ENA;
546 /* take time stamp for all event messages */
547 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
548
549 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
550 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
551 ptp_over_ethernet = PTP_TCR_TSIPENA;
552 break;
553
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000554 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000555 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000556 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
557 ptp_v2 = PTP_TCR_TSVER2ENA;
558 /* take time stamp for SYNC messages only */
559 ts_event_en = PTP_TCR_TSEVNTENA;
560
561 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
562 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
563 ptp_over_ethernet = PTP_TCR_TSIPENA;
564 break;
565
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000566 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000567 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000568 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
569 ptp_v2 = PTP_TCR_TSVER2ENA;
570 /* take time stamp for Delay_Req messages only */
571 ts_master_en = PTP_TCR_TSMSTRENA;
572 ts_event_en = PTP_TCR_TSEVNTENA;
573
574 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
575 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
576 ptp_over_ethernet = PTP_TCR_TSIPENA;
577 break;
578
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000579 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000580 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000581 config.rx_filter = HWTSTAMP_FILTER_ALL;
582 tstamp_all = PTP_TCR_TSENALL;
583 break;
584
585 default:
586 return -ERANGE;
587 }
588 } else {
589 switch (config.rx_filter) {
590 case HWTSTAMP_FILTER_NONE:
591 config.rx_filter = HWTSTAMP_FILTER_NONE;
592 break;
593 default:
594 /* PTP v1, UDP, any kind of event packet */
595 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
596 break;
597 }
598 }
599 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000600 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000601
602 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
603 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
604 else {
605 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000606 tstamp_all | ptp_v2 | ptp_over_ethernet |
607 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
608 ts_master_en | snap_type_sel);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000609 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
610
611 /* program Sub Second Increment reg */
Phil Reid19d857c2015-12-14 11:32:01 +0800612 sec_inc = priv->hw->ptp->config_sub_second_increment(
613 priv->ioaddr, priv->clk_ptp_rate);
614 temp = div_u64(1000000000ULL, sec_inc);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000615
616 /* calculate default added value:
617 * formula is :
618 * addend = (2^32)/freq_div_ratio;
Phil Reid19d857c2015-12-14 11:32:01 +0800619 * where, freq_div_ratio = 1e9ns/sec_inc
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000620 */
Phil Reid19d857c2015-12-14 11:32:01 +0800621 temp = (u64)(temp << 32);
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200622 priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000623 priv->hw->ptp->config_addend(priv->ioaddr,
624 priv->default_addend);
625
626 /* initialize system time */
Arnd Bergmann0a624152015-09-30 13:26:32 +0200627 ktime_get_real_ts64(&now);
628
629 /* lower 32 bits of tv_sec are safe until y2106 */
630 priv->hw->ptp->init_systime(priv->ioaddr, (u32)now.tv_sec,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000631 now.tv_nsec);
632 }
633
634 return copy_to_user(ifr->ifr_data, &config,
635 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
636}
637
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000638/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100639 * stmmac_init_ptp - init PTP
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000640 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100641 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000642 * This is done by looking at the HW cap. register.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100643 * This function also registers the ptp driver.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000644 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000645static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000646{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000647 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
648 return -EOPNOTSUPP;
649
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200650 /* Fall-back to main clock in case of no PTP ref is passed */
651 priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
652 if (IS_ERR(priv->clk_ptp_ref)) {
653 priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
654 priv->clk_ptp_ref = NULL;
655 } else {
656 clk_prepare_enable(priv->clk_ptp_ref);
657 priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
658 }
659
Vince Bridgers7cd01392013-12-20 11:19:34 -0600660 priv->adv_ts = 0;
661 if (priv->dma_cap.atime_stamp && priv->extend_desc)
662 priv->adv_ts = 1;
663
664 if (netif_msg_hw(priv) && priv->dma_cap.time_stamp)
665 pr_debug("IEEE 1588-2002 Time Stamp supported\n");
666
667 if (netif_msg_hw(priv) && priv->adv_ts)
668 pr_debug("IEEE 1588-2008 Advanced Time Stamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000669
670 priv->hw->ptp = &stmmac_ptp;
671 priv->hwts_tx_en = 0;
672 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000673
674 return stmmac_ptp_register(priv);
675}
676
677static void stmmac_release_ptp(struct stmmac_priv *priv)
678{
Giuseppe CAVALLARO55664012014-08-27 10:37:49 +0200679 if (priv->clk_ptp_ref)
680 clk_disable_unprepare(priv->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000681 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000682}
683
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700684/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100685 * stmmac_adjust_link - adjusts the link parameters
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700686 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100687 * Description: this is the helper called by the physical abstraction layer
688 * drivers to communicate the phy link status. According the speed and duplex
689 * this driver can invoke registered glue-logic as well.
690 * It also invoke the eee initialization because it could happen when switch
691 * on different networks (that are eee capable).
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700692 */
693static void stmmac_adjust_link(struct net_device *dev)
694{
695 struct stmmac_priv *priv = netdev_priv(dev);
696 struct phy_device *phydev = priv->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700697 unsigned long flags;
698 int new_state = 0;
699 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
700
701 if (phydev == NULL)
702 return;
703
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700704 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000705
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700706 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000707 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700708
709 /* Now we make sure that we can be in full duplex mode.
710 * If not, we operate in half-duplex mode. */
711 if (phydev->duplex != priv->oldduplex) {
712 new_state = 1;
713 if (!(phydev->duplex))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000714 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700715 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000716 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700717 priv->oldduplex = phydev->duplex;
718 }
719 /* Flow Control operation */
720 if (phydev->pause)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500721 priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000722 fc, pause_time);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700723
724 if (phydev->speed != priv->speed) {
725 new_state = 1;
726 switch (phydev->speed) {
727 case 1000:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000728 if (likely(priv->plat->has_gmac))
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000729 ctrl &= ~priv->hw->link.port;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000730 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700731 break;
732 case 100:
733 case 10:
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000734 if (priv->plat->has_gmac) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000735 ctrl |= priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700736 if (phydev->speed == SPEED_100) {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000737 ctrl |= priv->hw->link.speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700738 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000739 ctrl &= ~(priv->hw->link.speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700740 }
741 } else {
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000742 ctrl &= ~priv->hw->link.port;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700743 }
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000744 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700745 break;
746 default:
747 if (netif_msg_link(priv))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000748 pr_warn("%s: Speed (%d) not 10/100\n",
749 dev->name, phydev->speed);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700750 break;
751 }
752
753 priv->speed = phydev->speed;
754 }
755
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000756 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700757
758 if (!priv->oldlink) {
759 new_state = 1;
760 priv->oldlink = 1;
761 }
762 } else if (priv->oldlink) {
763 new_state = 1;
764 priv->oldlink = 0;
765 priv->speed = 0;
766 priv->oldduplex = -1;
767 }
768
769 if (new_state && netif_msg_link(priv))
770 phy_print_status(phydev);
771
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100772 spin_unlock_irqrestore(&priv->lock, flags);
773
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200774 /* At this stage, it could be needed to setup the EEE or adjust some
775 * MAC related HW registers.
776 */
777 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700778}
779
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000780/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100781 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000782 * @priv: driver private structure
783 * Description: this is to verify if the HW supports the PCS.
784 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
785 * configured for the TBI, RTBI, or SGMII PHY interface.
786 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000787static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
788{
789 int interface = priv->plat->interface;
790
791 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900792 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
793 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
794 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
795 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000796 pr_debug("STMMAC: PCS RGMII support enable\n");
797 priv->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900798 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000799 pr_debug("STMMAC: PCS SGMII support enable\n");
800 priv->pcs = STMMAC_PCS_SGMII;
801 }
802 }
803}
804
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700805/**
806 * stmmac_init_phy - PHY initialization
807 * @dev: net device structure
808 * Description: it initializes the driver's PHY state, and attaches the PHY
809 * to the mac driver.
810 * Return value:
811 * 0 on success
812 */
813static int stmmac_init_phy(struct net_device *dev)
814{
815 struct stmmac_priv *priv = netdev_priv(dev);
816 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000817 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000818 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000819 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000820 int max_speed = priv->plat->max_speed;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700821 priv->oldlink = 0;
822 priv->speed = 0;
823 priv->oldduplex = -1;
824
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700825 if (priv->plat->phy_node) {
826 phydev = of_phy_connect(dev, priv->plat->phy_node,
827 &stmmac_adjust_link, 0, interface);
828 } else {
Giuseppe CAVALLAROa7657f12016-04-01 09:07:16 +0200829 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
830 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000831
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700832 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
833 priv->plat->phy_addr);
834 pr_debug("stmmac_init_phy: trying to attach to %s\n",
835 phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700836
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700837 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
838 interface);
839 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700840
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300841 if (IS_ERR_OR_NULL(phydev)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700842 pr_err("%s: Could not attach to PHY\n", dev->name);
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300843 if (!phydev)
844 return -ENODEV;
845
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700846 return PTR_ERR(phydev);
847 }
848
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000849 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000850 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000851 (interface == PHY_INTERFACE_MODE_RMII) ||
Pavel Macheka77e4ac2014-08-25 13:31:16 +0200852 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000853 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
854 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000855
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700856 /*
857 * Broken HW is sometimes missing the pull-up resistor on the
858 * MDIO line, which results in reads to non-existent devices returning
859 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
860 * device as well.
861 * Note: phydev->phy_id is the result of reading the UID PHY registers.
862 */
Mathieu Olivari27732382015-05-27 11:02:48 -0700863 if (!priv->plat->phy_node && phydev->phy_id == 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700864 phy_disconnect(phydev);
865 return -ENODEV;
866 }
Giuseppe Cavallaro8e99fc52016-02-29 14:27:39 +0100867
868 /* If attached to a switch, there is no reason to poll phy handler */
Giuseppe CAVALLAROa7657f12016-04-01 09:07:16 +0200869 if (phydev->is_pseudo_fixed_link)
870 phydev->irq = PHY_IGNORE_INTERRUPT;
Giuseppe Cavallaro8e99fc52016-02-29 14:27:39 +0100871
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700872 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
Giuseppe CAVALLARO36bcfe72011-07-20 00:05:23 +0000873 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700874
875 priv->phydev = phydev;
876
877 return 0;
878}
879
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000880static void stmmac_display_rings(struct stmmac_priv *priv)
881{
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200882 void *head_rx, *head_tx;
883
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000884 if (priv->extend_desc) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200885 head_rx = (void *)priv->dma_erx;
886 head_tx = (void *)priv->dma_etx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000887 } else {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200888 head_rx = (void *)priv->dma_rx;
889 head_tx = (void *)priv->dma_tx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000890 }
Alexandre TORGUEd0225e72016-04-01 11:37:26 +0200891
892 /* Display Rx ring */
893 priv->hw->desc->display_ring(head_rx, DMA_RX_SIZE, true);
894 /* Display Tx ring */
895 priv->hw->desc->display_ring(head_tx, DMA_TX_SIZE, false);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000896}
897
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000898static int stmmac_set_bfsize(int mtu, int bufsize)
899{
900 int ret = bufsize;
901
902 if (mtu >= BUF_SIZE_4KiB)
903 ret = BUF_SIZE_8KiB;
904 else if (mtu >= BUF_SIZE_2KiB)
905 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100906 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000907 ret = BUF_SIZE_2KiB;
908 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100909 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000910
911 return ret;
912}
913
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000914/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100915 * stmmac_clear_descriptors - clear descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000916 * @priv: driver private structure
917 * Description: this function is called to clear the tx and rx descriptors
918 * in case of both basic and extended descriptors are used.
919 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000920static void stmmac_clear_descriptors(struct stmmac_priv *priv)
921{
922 int i;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000923
924 /* Clear the Rx/Tx descriptors */
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100925 for (i = 0; i < DMA_RX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000926 if (priv->extend_desc)
927 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
928 priv->use_riwt, priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100929 (i == DMA_RX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000930 else
931 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
932 priv->use_riwt, priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100933 (i == DMA_RX_SIZE - 1));
934 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000935 if (priv->extend_desc)
936 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
937 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100938 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000939 else
940 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
941 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100942 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000943}
944
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100945/**
946 * stmmac_init_rx_buffers - init the RX descriptor buffer.
947 * @priv: driver private structure
948 * @p: descriptor pointer
949 * @i: descriptor index
950 * @flags: gfp flag.
951 * Description: this function is called to allocate a receive buffer, perform
952 * the DMA mapping and init the descriptor.
953 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000954static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +0100955 int i, gfp_t flags)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000956{
957 struct sk_buff *skb;
958
Vineet Gupta4ec49a32015-05-20 12:04:40 +0530959 skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200960 if (!skb) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000961 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200962 return -ENOMEM;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000963 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000964 priv->rx_skbuff[i] = skb;
965 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
966 priv->dma_buf_sz,
967 DMA_FROM_DEVICE);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200968 if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
969 pr_err("%s: DMA mapping error\n", __func__);
970 dev_kfree_skb_any(skb);
971 return -EINVAL;
972 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000973
974 p->des2 = priv->rx_skbuff_dma[i];
975
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +0100976 if ((priv->hw->mode->init_desc3) &&
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000977 (priv->dma_buf_sz == BUF_SIZE_16KiB))
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +0100978 priv->hw->mode->init_desc3(p);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +0000979
980 return 0;
981}
982
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +0200983static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
984{
985 if (priv->rx_skbuff[i]) {
986 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
987 priv->dma_buf_sz, DMA_FROM_DEVICE);
988 dev_kfree_skb_any(priv->rx_skbuff[i]);
989 }
990 priv->rx_skbuff[i] = NULL;
991}
992
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700993/**
994 * init_dma_desc_rings - init the RX/TX descriptor rings
995 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100996 * @flags: gfp flag.
997 * Description: this function initializes the DMA RX/TX descriptors
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +0000998 * and allocates the socket buffers. It suppors the chained and ring
999 * modes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001000 */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001001static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001002{
1003 int i;
1004 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001005 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001006 int ret = -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001007
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001008 if (priv->hw->mode->set_16kib_bfsize)
1009 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001010
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001011 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001012 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001013
Vince Bridgers2618abb2014-01-20 05:39:01 -06001014 priv->dma_buf_sz = bfsize;
1015
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001016 if (netif_msg_probe(priv)) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001017 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1018 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001019
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001020 /* RX INITIALIZATION */
1021 pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
1022 }
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001023 for (i = 0; i < DMA_RX_SIZE; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001024 struct dma_desc *p;
1025 if (priv->extend_desc)
1026 p = &((priv->dma_erx + i)->basic);
1027 else
1028 p = priv->dma_rx + i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001029
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001030 ret = stmmac_init_rx_buffers(priv, p, i, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001031 if (ret)
1032 goto err_init_rx_buffers;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001033
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001034 if (netif_msg_probe(priv))
1035 pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1036 priv->rx_skbuff[i]->data,
1037 (unsigned int)priv->rx_skbuff_dma[i]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001038 }
1039 priv->cur_rx = 0;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001040 priv->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001041 buf_sz = bfsize;
1042
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001043 /* Setup the chained descriptor addresses */
1044 if (priv->mode == STMMAC_CHAIN_MODE) {
1045 if (priv->extend_desc) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001046 priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001047 DMA_RX_SIZE, 1);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001048 priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001049 DMA_TX_SIZE, 1);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001050 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001051 priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001052 DMA_RX_SIZE, 0);
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001053 priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001054 DMA_TX_SIZE, 0);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001055 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001056 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001057
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001058 /* TX INITIALIZATION */
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001059 for (i = 0; i < DMA_TX_SIZE; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001060 struct dma_desc *p;
1061 if (priv->extend_desc)
1062 p = &((priv->dma_etx + i)->basic);
1063 else
1064 p = priv->dma_tx + i;
1065 p->des2 = 0;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001066 priv->tx_skbuff_dma[i].buf = 0;
1067 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001068 priv->tx_skbuff_dma[i].len = 0;
Giuseppe Cavallaro2a6d8e12016-02-29 14:27:32 +01001069 priv->tx_skbuff_dma[i].last_segment = false;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001070 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001071 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001072
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001073 priv->dirty_tx = 0;
1074 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001075 netdev_reset_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001076
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001077 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001078
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001079 if (netif_msg_hw(priv))
1080 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001081
1082 return 0;
1083err_init_rx_buffers:
1084 while (--i >= 0)
1085 stmmac_free_rx_buffers(priv, i);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001086 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001087}
1088
1089static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1090{
1091 int i;
1092
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001093 for (i = 0; i < DMA_RX_SIZE; i++)
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001094 stmmac_free_rx_buffers(priv, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001095}
1096
1097static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1098{
1099 int i;
1100
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001101 for (i = 0; i < DMA_TX_SIZE; i++) {
damuzi00075e43642014-01-17 23:47:59 +08001102 struct dma_desc *p;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001103
damuzi00075e43642014-01-17 23:47:59 +08001104 if (priv->extend_desc)
1105 p = &((priv->dma_etx + i)->basic);
1106 else
1107 p = priv->dma_tx + i;
1108
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001109 if (priv->tx_skbuff_dma[i].buf) {
1110 if (priv->tx_skbuff_dma[i].map_as_page)
1111 dma_unmap_page(priv->device,
1112 priv->tx_skbuff_dma[i].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001113 priv->tx_skbuff_dma[i].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001114 DMA_TO_DEVICE);
1115 else
1116 dma_unmap_single(priv->device,
1117 priv->tx_skbuff_dma[i].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001118 priv->tx_skbuff_dma[i].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001119 DMA_TO_DEVICE);
damuzi00075e43642014-01-17 23:47:59 +08001120 }
1121
1122 if (priv->tx_skbuff[i] != NULL) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001123 dev_kfree_skb_any(priv->tx_skbuff[i]);
1124 priv->tx_skbuff[i] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001125 priv->tx_skbuff_dma[i].buf = 0;
1126 priv->tx_skbuff_dma[i].map_as_page = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001127 }
1128 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001129}
1130
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001131/**
1132 * alloc_dma_desc_resources - alloc TX/RX resources.
1133 * @priv: private structure
1134 * Description: according to which descriptor can be used (extend or basic)
1135 * this function allocates the resources for TX and RX paths. In case of
1136 * reception, for example, it pre-allocated the RX socket buffer in order to
1137 * allow zero-copy mechanism.
1138 */
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001139static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1140{
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001141 int ret = -ENOMEM;
1142
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001143 priv->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE, sizeof(dma_addr_t),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001144 GFP_KERNEL);
1145 if (!priv->rx_skbuff_dma)
1146 return -ENOMEM;
1147
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001148 priv->rx_skbuff = kmalloc_array(DMA_RX_SIZE, sizeof(struct sk_buff *),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001149 GFP_KERNEL);
1150 if (!priv->rx_skbuff)
1151 goto err_rx_skbuff;
1152
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001153 priv->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001154 sizeof(*priv->tx_skbuff_dma),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001155 GFP_KERNEL);
1156 if (!priv->tx_skbuff_dma)
1157 goto err_tx_skbuff_dma;
1158
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001159 priv->tx_skbuff = kmalloc_array(DMA_TX_SIZE, sizeof(struct sk_buff *),
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001160 GFP_KERNEL);
1161 if (!priv->tx_skbuff)
1162 goto err_tx_skbuff;
1163
1164 if (priv->extend_desc) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001165 priv->dma_erx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001166 sizeof(struct
1167 dma_extended_desc),
1168 &priv->dma_rx_phy,
1169 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001170 if (!priv->dma_erx)
1171 goto err_dma;
1172
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001173 priv->dma_etx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001174 sizeof(struct
1175 dma_extended_desc),
1176 &priv->dma_tx_phy,
1177 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001178 if (!priv->dma_etx) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001179 dma_free_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001180 sizeof(struct dma_extended_desc),
1181 priv->dma_erx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001182 goto err_dma;
1183 }
1184 } else {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001185 priv->dma_rx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001186 sizeof(struct dma_desc),
1187 &priv->dma_rx_phy,
1188 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001189 if (!priv->dma_rx)
1190 goto err_dma;
1191
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001192 priv->dma_tx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001193 sizeof(struct dma_desc),
1194 &priv->dma_tx_phy,
1195 GFP_KERNEL);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001196 if (!priv->dma_tx) {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001197 dma_free_coherent(priv->device, DMA_RX_SIZE *
Alexey Brodkinf1590672015-06-24 11:47:41 +03001198 sizeof(struct dma_desc),
1199 priv->dma_rx, priv->dma_rx_phy);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001200 goto err_dma;
1201 }
1202 }
1203
1204 return 0;
1205
1206err_dma:
1207 kfree(priv->tx_skbuff);
1208err_tx_skbuff:
1209 kfree(priv->tx_skbuff_dma);
1210err_tx_skbuff_dma:
1211 kfree(priv->rx_skbuff);
1212err_rx_skbuff:
1213 kfree(priv->rx_skbuff_dma);
1214 return ret;
1215}
1216
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001217static void free_dma_desc_resources(struct stmmac_priv *priv)
1218{
1219 /* Release the DMA TX/RX socket buffers */
1220 dma_free_rx_skbufs(priv);
1221 dma_free_tx_skbufs(priv);
1222
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001223 /* Free DMA regions of consistent memory previously allocated */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001224 if (!priv->extend_desc) {
1225 dma_free_coherent(priv->device,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001226 DMA_TX_SIZE * sizeof(struct dma_desc),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001227 priv->dma_tx, priv->dma_tx_phy);
1228 dma_free_coherent(priv->device,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001229 DMA_RX_SIZE * sizeof(struct dma_desc),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001230 priv->dma_rx, priv->dma_rx_phy);
1231 } else {
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001232 dma_free_coherent(priv->device, DMA_TX_SIZE *
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001233 sizeof(struct dma_extended_desc),
1234 priv->dma_etx, priv->dma_tx_phy);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001235 dma_free_coherent(priv->device, DMA_RX_SIZE *
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001236 sizeof(struct dma_extended_desc),
1237 priv->dma_erx, priv->dma_rx_phy);
1238 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001239 kfree(priv->rx_skbuff_dma);
1240 kfree(priv->rx_skbuff);
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001241 kfree(priv->tx_skbuff_dma);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001242 kfree(priv->tx_skbuff);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001243}
1244
1245/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001246 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001247 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001248 * Description: it is used for configuring the DMA operation mode register in
1249 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001250 */
1251static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1252{
Vince Bridgersf88203a2015-04-15 11:17:42 -05001253 int rxfifosz = priv->plat->rx_fifo_size;
1254
Sonic Zhange2a240c2013-08-28 18:55:39 +08001255 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001256 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc, rxfifosz);
Sonic Zhange2a240c2013-08-28 18:55:39 +08001257 else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001258 /*
1259 * In case of GMAC, SF mode can be enabled
1260 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001261 * 1) TX COE if actually supported
1262 * 2) There is no bugged Jumbo frame support
1263 * that needs to not insert csum in the TDES.
1264 */
Vince Bridgersf88203a2015-04-15 11:17:42 -05001265 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE,
1266 rxfifosz);
Sonic Zhangb2dec112015-01-30 13:49:32 +08001267 priv->xstats.threshold = SF_DMA_MODE;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001268 } else
Vince Bridgersf88203a2015-04-15 11:17:42 -05001269 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE,
1270 rxfifosz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001271}
1272
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001273/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001274 * stmmac_tx_clean - to manage the transmission completion
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001275 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001276 * Description: it reclaims the transmit resources after transmission completes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001277 */
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001278static void stmmac_tx_clean(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001279{
Beniamino Galvani38979572015-01-21 19:07:27 +01001280 unsigned int bytes_compl = 0, pkts_compl = 0;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001281 unsigned int entry = priv->dirty_tx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001282
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001283 spin_lock(&priv->tx_lock);
1284
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001285 priv->xstats.tx_clean++;
1286
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001287 while (entry != priv->cur_tx) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001288 struct sk_buff *skb = priv->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001289 struct dma_desc *p;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001290 int status;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001291
1292 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001293 p = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001294 else
1295 p = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001296
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001297 status = priv->hw->desc->tx_status(&priv->dev->stats,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001298 &priv->xstats, p,
1299 priv->ioaddr);
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001300 /* Check if the descriptor is owned by the DMA */
1301 if (unlikely(status & tx_dma_own))
1302 break;
1303
1304 /* Just consider the last segment and ...*/
1305 if (likely(!(status & tx_not_ls))) {
1306 /* ... verify the status error condition */
1307 if (unlikely(status & tx_err)) {
1308 priv->dev->stats.tx_errors++;
1309 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001310 priv->dev->stats.tx_packets++;
1311 priv->xstats.tx_pkt_n++;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001312 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001313 stmmac_get_tx_hwtstamp(priv, entry, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001314 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001315
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001316 if (likely(priv->tx_skbuff_dma[entry].buf)) {
1317 if (priv->tx_skbuff_dma[entry].map_as_page)
1318 dma_unmap_page(priv->device,
1319 priv->tx_skbuff_dma[entry].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001320 priv->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001321 DMA_TO_DEVICE);
1322 else
1323 dma_unmap_single(priv->device,
1324 priv->tx_skbuff_dma[entry].buf,
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001325 priv->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001326 DMA_TO_DEVICE);
1327 priv->tx_skbuff_dma[entry].buf = 0;
1328 priv->tx_skbuff_dma[entry].map_as_page = false;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001329 }
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001330 priv->hw->mode->clean_desc3(priv, p);
Giuseppe Cavallaro2a6d8e12016-02-29 14:27:32 +01001331 priv->tx_skbuff_dma[entry].last_segment = false;
Giuseppe Cavallaro96951362016-02-29 14:27:33 +01001332 priv->tx_skbuff_dma[entry].is_jumbo = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001333
1334 if (likely(skb != NULL)) {
Beniamino Galvani38979572015-01-21 19:07:27 +01001335 pkts_compl++;
1336 bytes_compl += skb->len;
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001337 dev_consume_skb_any(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001338 priv->tx_skbuff[entry] = NULL;
1339 }
1340
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001341 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001342
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001343 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001344 }
Giuseppe Cavallarofbc80822016-02-29 14:27:37 +01001345 priv->dirty_tx = entry;
Beniamino Galvani38979572015-01-21 19:07:27 +01001346
1347 netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
1348
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001349 if (unlikely(netif_queue_stopped(priv->dev) &&
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001350 stmmac_tx_avail(priv) > STMMAC_TX_THRESH)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001351 netif_tx_lock(priv->dev);
1352 if (netif_queue_stopped(priv->dev) &&
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001353 stmmac_tx_avail(priv) > STMMAC_TX_THRESH) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001354 if (netif_msg_tx_done(priv))
1355 pr_debug("%s: restart transmit\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001356 netif_wake_queue(priv->dev);
1357 }
1358 netif_tx_unlock(priv->dev);
1359 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001360
1361 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1362 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001363 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001364 }
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001365 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001366}
1367
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001368static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001369{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001370 priv->hw->dma->enable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001371}
1372
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001373static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001374{
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00001375 priv->hw->dma->disable_dma_irq(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001376}
1377
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001378/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001379 * stmmac_tx_err - to manage the tx error
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001380 * @priv: driver private structure
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001381 * Description: it cleans the descriptors and restarts the transmission
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001382 * in case of transmission errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001383 */
1384static void stmmac_tx_err(struct stmmac_priv *priv)
1385{
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001386 int i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001387 netif_stop_queue(priv->dev);
1388
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001389 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001390 dma_free_tx_skbufs(priv);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001391 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001392 if (priv->extend_desc)
1393 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1394 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001395 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001396 else
1397 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1398 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001399 (i == DMA_TX_SIZE - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001400 priv->dirty_tx = 0;
1401 priv->cur_tx = 0;
Beniamino Galvani38979572015-01-21 19:07:27 +01001402 netdev_reset_queue(priv->dev);
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001403 priv->hw->dma->start_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001404
1405 priv->dev->stats.tx_errors++;
1406 netif_wake_queue(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001407}
1408
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001409/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001410 * stmmac_dma_interrupt - DMA ISR
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001411 * @priv: driver private structure
1412 * Description: this is the DMA ISR. It is called by the main ISR.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001413 * It calls the dwmac dma routine and schedule poll method in case of some
1414 * work can be done.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001415 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001416static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001417{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001418 int status;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001419 int rxfifosz = priv->plat->rx_fifo_size;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001420
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001421 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001422 if (likely((status & handle_rx)) || (status & handle_tx)) {
1423 if (likely(napi_schedule_prep(&priv->napi))) {
1424 stmmac_disable_dma_irq(priv);
1425 __napi_schedule(&priv->napi);
1426 }
1427 }
1428 if (unlikely(status & tx_hard_error_bump_tc)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001429 /* Try to bump up the dma threshold on this failure */
Sonic Zhangb2dec112015-01-30 13:49:32 +08001430 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
1431 (tc <= 256)) {
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001432 tc += 64;
Sonic Zhangc405abe2015-01-22 14:55:56 +08001433 if (priv->plat->force_thresh_dma_mode)
Vince Bridgersf88203a2015-04-15 11:17:42 -05001434 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc,
1435 rxfifosz);
Sonic Zhangc405abe2015-01-22 14:55:56 +08001436 else
1437 priv->hw->dma->dma_mode(priv->ioaddr, tc,
Vince Bridgersf88203a2015-04-15 11:17:42 -05001438 SF_DMA_MODE, rxfifosz);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001439 priv->xstats.threshold = tc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001440 }
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00001441 } else if (unlikely(status == tx_hard_error))
1442 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001443}
1444
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001445/**
1446 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1447 * @priv: driver private structure
1448 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1449 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001450static void stmmac_mmc_setup(struct stmmac_priv *priv)
1451{
1452 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001453 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001454
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001455 dwmac_mmc_intr_all_mask(priv->ioaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001456
1457 if (priv->dma_cap.rmon) {
1458 dwmac_mmc_ctrl(priv->ioaddr, mode);
1459 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1460 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +00001461 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001462}
1463
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001464/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001465 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001466 * @priv: driver private structure
1467 * Description: select the Enhanced/Alternate or Normal descriptors.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001468 * In case of Enhanced/Alternate, it checks if the extended descriptors are
1469 * supported by the HW capability register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001470 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001471static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1472{
1473 if (priv->plat->enh_desc) {
1474 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001475
1476 /* GMAC older than 3.50 has no extended descriptors */
1477 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1478 pr_info("\tEnabled extended descriptors\n");
1479 priv->extend_desc = 1;
1480 } else
1481 pr_warn("Extended descriptors not supported\n");
1482
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001483 priv->hw->desc = &enh_desc_ops;
1484 } else {
1485 pr_info(" Normal descriptors\n");
1486 priv->hw->desc = &ndesc_ops;
1487 }
1488}
1489
1490/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001491 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001492 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001493 * Description:
1494 * new GMAC chip generations have a new register to indicate the
1495 * presence of the optional feature/functions.
1496 * This can be also used to override the value passed through the
1497 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001498 */
1499static int stmmac_get_hw_features(struct stmmac_priv *priv)
1500{
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001501 u32 ret = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001502
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001503 if (priv->hw->dma->get_hw_feature) {
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001504 priv->hw->dma->get_hw_feature(priv->ioaddr,
1505 &priv->dma_cap);
1506 ret = 1;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001507 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001508
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001509 return ret;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001510}
1511
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001512/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001513 * stmmac_check_ether_addr - check if the MAC addr is valid
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001514 * @priv: driver private structure
1515 * Description:
1516 * it is to verify if the MAC address is valid, in case of failures it
1517 * generates a random MAC address
1518 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001519static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1520{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001521 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001522 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001523 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001524 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001525 eth_hw_addr_random(priv->dev);
Hans de Goedec88460b2014-01-26 15:50:44 +01001526 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1527 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001528 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001529}
1530
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001531/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001532 * stmmac_init_dma_engine - DMA init.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001533 * @priv: driver private structure
1534 * Description:
1535 * It inits the DMA invoking the specific MAC/GMAC callback.
1536 * Some DMA parameters can be passed from the platform;
1537 * in case of these are not passed a default is kept for the MAC or GMAC.
1538 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001539static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1540{
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001541 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, aal = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001542 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001543 int atds = 0;
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001544 int ret = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001545
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001546 if (priv->plat->dma_cfg) {
1547 pbl = priv->plat->dma_cfg->pbl;
1548 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001549 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001550 aal = priv->plat->dma_cfg->aal;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001551 }
1552
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001553 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1554 atds = 1;
1555
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001556 ret = priv->hw->dma->reset(priv->ioaddr);
1557 if (ret) {
1558 dev_err(priv->device, "Failed to reset the dma\n");
1559 return ret;
1560 }
1561
1562 priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001563 aal, priv->dma_tx_phy, priv->dma_rx_phy, atds);
1564
1565 if ((priv->synopsys_id >= DWMAC_CORE_3_50) &&
1566 (priv->plat->axi && priv->hw->dma->axi))
1567 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
1568
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001569 return ret;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001570}
1571
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001572/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001573 * stmmac_tx_timer - mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001574 * @data: data pointer
1575 * Description:
1576 * This is the timer handler to directly invoke the stmmac_tx_clean.
1577 */
1578static void stmmac_tx_timer(unsigned long data)
1579{
1580 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1581
1582 stmmac_tx_clean(priv);
1583}
1584
1585/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001586 * stmmac_init_tx_coalesce - init tx mitigation options.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001587 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001588 * Description:
1589 * This inits the transmit coalesce parameters: i.e. timer rate,
1590 * timer handler and default threshold used for enabling the
1591 * interrupt on completion bit.
1592 */
1593static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1594{
1595 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1596 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1597 init_timer(&priv->txtimer);
1598 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1599 priv->txtimer.data = (unsigned long)priv;
1600 priv->txtimer.function = stmmac_tx_timer;
1601 add_timer(&priv->txtimer);
1602}
1603
1604/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001605 * stmmac_hw_setup - setup mac in a usable state.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001606 * @dev : pointer to the device structure.
1607 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001608 * this is the main function to setup the HW in a usable state because the
1609 * dma engine is reset, the core registers are configured (e.g. AXI,
1610 * Checksum features, timers). The DMA is ready to start receiving and
1611 * transmitting.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001612 * Return value:
1613 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1614 * file on failure.
1615 */
Huacai Chenfe1319292014-12-19 22:38:18 +08001616static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001617{
1618 struct stmmac_priv *priv = netdev_priv(dev);
1619 int ret;
1620
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001621 /* DMA initialization and SW reset */
1622 ret = stmmac_init_dma_engine(priv);
1623 if (ret < 0) {
1624 pr_err("%s: DMA engine initialization failed\n", __func__);
1625 return ret;
1626 }
1627
1628 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001629 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001630
1631 /* If required, perform hw setup of the bus. */
1632 if (priv->plat->bus_setup)
1633 priv->plat->bus_setup(priv->ioaddr);
1634
1635 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001636 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001637
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001638 ret = priv->hw->mac->rx_ipc(priv->hw);
1639 if (!ret) {
1640 pr_warn(" RX IPC Checksum Offload disabled\n");
1641 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02001642 priv->hw->rx_csum = 0;
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001643 }
1644
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001645 /* Enable the MAC Rx/Tx */
1646 stmmac_set_mac(priv->ioaddr, true);
1647
1648 /* Set the HW DMA mode and the COE */
1649 stmmac_dma_operation_mode(priv);
1650
1651 stmmac_mmc_setup(priv);
1652
Huacai Chenfe1319292014-12-19 22:38:18 +08001653 if (init_ptp) {
1654 ret = stmmac_init_ptp(priv);
1655 if (ret && ret != -EOPNOTSUPP)
1656 pr_warn("%s: failed PTP initialisation\n", __func__);
1657 }
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001658
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001659#ifdef CONFIG_DEBUG_FS
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001660 ret = stmmac_init_fs(dev);
1661 if (ret < 0)
1662 pr_warn("%s: failed debugFS registration\n", __func__);
1663#endif
1664 /* Start the ball rolling... */
1665 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1666 priv->hw->dma->start_tx(priv->ioaddr);
1667 priv->hw->dma->start_rx(priv->ioaddr);
1668
1669 /* Dump DMA/MAC registers */
1670 if (netif_msg_hw(priv)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001671 priv->hw->mac->dump_regs(priv->hw);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001672 priv->hw->dma->dump_regs(priv->ioaddr);
1673 }
1674 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1675
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001676 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1677 priv->rx_riwt = MAX_DMA_RIWT;
1678 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1679 }
1680
1681 if (priv->pcs && priv->hw->mac->ctrl_ane)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001682 priv->hw->mac->ctrl_ane(priv->hw, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001683
1684 return 0;
1685}
1686
1687/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001688 * stmmac_open - open entry point of the driver
1689 * @dev : pointer to the device structure.
1690 * Description:
1691 * This function is the open entry point of the driver.
1692 * Return value:
1693 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1694 * file on failure.
1695 */
1696static int stmmac_open(struct net_device *dev)
1697{
1698 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001699 int ret;
1700
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001701 stmmac_check_ether_addr(priv);
1702
Byungho An4d8f0822013-04-07 17:56:16 +00001703 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1704 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001705 ret = stmmac_init_phy(dev);
1706 if (ret) {
1707 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1708 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02001709 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001710 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001711 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001712
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001713 /* Extra statistics */
1714 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1715 priv->xstats.threshold = tc;
1716
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001717 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01001718 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001719
Tobias Klauser7262b7b2014-02-22 13:09:03 +01001720 ret = alloc_dma_desc_resources(priv);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001721 if (ret < 0) {
1722 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1723 goto dma_desc_error;
1724 }
1725
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001726 ret = init_dma_desc_rings(dev, GFP_KERNEL);
1727 if (ret < 0) {
1728 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1729 goto init_error;
1730 }
1731
Huacai Chenfe1319292014-12-19 22:38:18 +08001732 ret = stmmac_hw_setup(dev, true);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001733 if (ret < 0) {
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001734 pr_err("%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001735 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001736 }
1737
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001738 stmmac_init_tx_coalesce(priv);
1739
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001740 if (priv->phydev)
1741 phy_start(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001742
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001743 /* Request the IRQ lines */
1744 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001745 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001746 if (unlikely(ret < 0)) {
1747 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1748 __func__, dev->irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001749 goto init_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001750 }
1751
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001752 /* Request the Wake IRQ in case of another line is used for WoL */
1753 if (priv->wol_irq != dev->irq) {
1754 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1755 IRQF_SHARED, dev->name, dev);
1756 if (unlikely(ret < 0)) {
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001757 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1758 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001759 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001760 }
1761 }
1762
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001763 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001764 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001765 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1766 dev->name, dev);
1767 if (unlikely(ret < 0)) {
1768 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1769 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001770 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001771 }
1772 }
1773
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001774 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001775 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001776
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001777 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001778
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001779lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001780 if (priv->wol_irq != dev->irq)
1781 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001782wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001783 free_irq(dev->irq, dev);
1784
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001785init_error:
1786 free_dma_desc_resources(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001787dma_desc_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001788 if (priv->phydev)
1789 phy_disconnect(priv->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001790
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001791 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001792}
1793
1794/**
1795 * stmmac_release - close entry point of the driver
1796 * @dev : device pointer.
1797 * Description:
1798 * This is the stop entry point of the driver.
1799 */
1800static int stmmac_release(struct net_device *dev)
1801{
1802 struct stmmac_priv *priv = netdev_priv(dev);
1803
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001804 if (priv->eee_enabled)
1805 del_timer_sync(&priv->eee_ctrl_timer);
1806
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001807 /* Stop and disconnect the PHY */
1808 if (priv->phydev) {
1809 phy_stop(priv->phydev);
1810 phy_disconnect(priv->phydev);
1811 priv->phydev = NULL;
1812 }
1813
1814 netif_stop_queue(dev);
1815
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001816 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001817
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001818 del_timer_sync(&priv->txtimer);
1819
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001820 /* Free the IRQ lines */
1821 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001822 if (priv->wol_irq != dev->irq)
1823 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001824 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001825 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001826
1827 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001828 priv->hw->dma->stop_tx(priv->ioaddr);
1829 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001830
1831 /* Release and free the Rx/Tx resources */
1832 free_dma_desc_resources(priv);
1833
avisconti19449bf2010-10-25 18:58:14 +00001834 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001835 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001836
1837 netif_carrier_off(dev);
1838
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001839#ifdef CONFIG_DEBUG_FS
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07001840 stmmac_exit_fs(dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001841#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001842
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00001843 stmmac_release_ptp(priv);
1844
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001845 return 0;
1846}
1847
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001848/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001849 * stmmac_xmit - Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001850 * @skb : the socket buffer
1851 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001852 * Description : this is the tx entry point of the driver.
1853 * It programs the chain or the ring and supports oversized frames
1854 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001855 */
1856static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1857{
1858 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001859 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001860 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001861 int nfrags = skb_shinfo(skb)->nr_frags;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001862 unsigned int entry, first_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001863 struct dma_desc *desc, *first;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001864 unsigned int enh_desc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001865
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001866 spin_lock(&priv->tx_lock);
1867
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001868 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001869 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001870 if (!netif_queue_stopped(dev)) {
1871 netif_stop_queue(dev);
1872 /* This is a hard error, log it. */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001873 pr_err("%s: Tx Ring full when queue awake\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001874 }
1875 return NETDEV_TX_BUSY;
1876 }
1877
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001878 if (priv->tx_path_in_lpi_mode)
1879 stmmac_disable_eee_mode(priv);
1880
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001881 entry = priv->cur_tx;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001882 first_entry = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001883
Michał Mirosław5e982f32011-04-09 02:46:55 +00001884 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001885
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001886 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001887 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001888 else
1889 desc = priv->dma_tx + entry;
1890
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001891 first = desc;
1892
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001893 priv->tx_skbuff[first_entry] = skb;
1894
1895 enh_desc = priv->plat->enh_desc;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001896 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001897 if (enh_desc)
1898 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
1899
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001900 if (unlikely(is_jumbo)) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001901 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001902 if (unlikely(entry < 0))
1903 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001904 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001905
1906 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001907 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1908 int len = skb_frag_size(frag);
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01001909 bool last_segment = (i == (nfrags - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001910
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001911 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
1912
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001913 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001914 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001915 else
1916 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001917
Ian Campbellf7223802011-09-21 21:53:20 +00001918 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1919 DMA_TO_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001920 if (dma_mapping_error(priv->device, desc->des2))
1921 goto dma_map_err; /* should reuse desc w/o issues */
1922
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001923 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001924 priv->tx_skbuff_dma[entry].buf = desc->des2;
1925 priv->tx_skbuff_dma[entry].map_as_page = true;
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001926 priv->tx_skbuff_dma[entry].len = len;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001927 priv->tx_skbuff_dma[entry].last_segment = last_segment;
1928
1929 /* Prepare the descriptor and set the own bit too */
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001930 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01001931 priv->mode, 1, last_segment);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001932 }
1933
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001934 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
1935
1936 priv->cur_tx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001937
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001938 if (netif_msg_pktdata(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02001939 void *tx_head;
1940
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001941 pr_debug("%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
1942 __func__, priv->cur_tx, priv->dirty_tx, first_entry,
1943 entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001944
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001945 if (priv->extend_desc)
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02001946 tx_head = (void *)priv->dma_etx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001947 else
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02001948 tx_head = (void *)priv->dma_tx;
1949
1950 priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001951
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001952 pr_debug(">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001953 print_pkt(skb->data, skb->len);
1954 }
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001955
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001956 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001957 if (netif_msg_hw(priv))
1958 pr_debug("%s: stop transmitted packets\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001959 netif_stop_queue(dev);
1960 }
1961
1962 dev->stats.tx_bytes += skb->len;
1963
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001964 /* According to the coalesce parameter the IC bit for the latest
1965 * segment is reset and the timer re-started to clean the tx status.
1966 * This approach takes care about the fragments: desc is the first
1967 * element in case of no SG.
1968 */
1969 priv->tx_count_frames += nfrags + 1;
1970 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
1971 mod_timer(&priv->txtimer,
1972 STMMAC_COAL_TIMER(priv->tx_coal_timer));
1973 } else {
1974 priv->tx_count_frames = 0;
1975 priv->hw->desc->set_tx_ic(desc);
1976 priv->xstats.tx_set_ic_bit++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001977 }
1978
1979 if (!priv->hwts_tx_en)
1980 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00001981
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001982 /* Ready to fill the first descriptor and set the OWN bit w/o any
1983 * problems because all the descriptors are actually ready to be
1984 * passed to the DMA engine.
1985 */
1986 if (likely(!is_jumbo)) {
1987 bool last_segment = (nfrags == 0);
1988
1989 first->des2 = dma_map_single(priv->device, skb->data,
1990 nopaged_len, DMA_TO_DEVICE);
1991 if (dma_mapping_error(priv->device, first->des2))
1992 goto dma_map_err;
1993
1994 priv->tx_skbuff_dma[first_entry].buf = first->des2;
1995 priv->tx_skbuff_dma[first_entry].len = nopaged_len;
1996 priv->tx_skbuff_dma[first_entry].last_segment = last_segment;
1997
1998 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1999 priv->hwts_tx_en)) {
2000 /* declare that device is doing timestamping */
2001 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2002 priv->hw->desc->enable_tx_timestamp(first);
2003 }
2004
2005 /* Prepare the first descriptor setting the OWN bit too */
2006 priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
2007 csum_insertion, priv->mode, 1,
2008 last_segment);
2009
2010 /* The own bit must be the latest setting done when prepare the
2011 * descriptor and then barrier is needed to make sure that
2012 * all is coherent before granting the DMA engine.
2013 */
2014 smp_wmb();
2015 }
2016
Beniamino Galvani38979572015-01-21 19:07:27 +01002017 netdev_sent_queue(dev, skb->len);
Richard Cochran52f64fa2011-06-19 03:31:43 +00002018 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2019
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002020 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002021 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002022
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002023dma_map_err:
Fabrice Gasnier758a0ab2014-11-04 17:08:06 +01002024 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002025 dev_err(priv->device, "Tx dma map failed\n");
2026 dev_kfree_skb(skb);
2027 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002028 return NETDEV_TX_OK;
2029}
2030
Vince Bridgersb9381982014-01-14 13:42:05 -06002031static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2032{
2033 struct ethhdr *ehdr;
2034 u16 vlanid;
2035
2036 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2037 NETIF_F_HW_VLAN_CTAG_RX &&
2038 !__vlan_get_tag(skb, &vlanid)) {
2039 /* pop the vlan tag */
2040 ehdr = (struct ethhdr *)skb->data;
2041 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2042 skb_pull(skb, VLAN_HLEN);
2043 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2044 }
2045}
2046
2047
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002048static inline int stmmac_rx_threshold_count(struct stmmac_priv *priv)
2049{
2050 if (priv->rx_zeroc_thresh < STMMAC_RX_THRESH)
2051 return 0;
2052
2053 return 1;
2054}
2055
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002056/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002057 * stmmac_rx_refill - refill used skb preallocated buffers
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002058 * @priv: driver private structure
2059 * Description : this is to reallocate the skb for the reception process
2060 * that is based on zero-copy.
2061 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002062static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2063{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002064 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002065 unsigned int entry = priv->dirty_rx;
2066 int dirty = stmmac_rx_dirty(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002067
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002068 while (dirty-- > 0) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002069 struct dma_desc *p;
2070
2071 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002072 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002073 else
2074 p = priv->dma_rx + entry;
2075
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002076 if (likely(priv->rx_skbuff[entry] == NULL)) {
2077 struct sk_buff *skb;
2078
Eric Dumazetacb600d2012-10-05 06:23:55 +00002079 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002080 if (unlikely(!skb)) {
2081 /* so for a while no zero-copy! */
2082 priv->rx_zeroc_thresh = STMMAC_RX_THRESH;
2083 if (unlikely(net_ratelimit()))
2084 dev_err(priv->device,
2085 "fail to alloc skb entry %d\n",
2086 entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002087 break;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002088 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002089
2090 priv->rx_skbuff[entry] = skb;
2091 priv->rx_skbuff_dma[entry] =
2092 dma_map_single(priv->device, skb->data, bfsize,
2093 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002094 if (dma_mapping_error(priv->device,
2095 priv->rx_skbuff_dma[entry])) {
2096 dev_err(priv->device, "Rx dma map failed\n");
2097 dev_kfree_skb(skb);
2098 break;
2099 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002100 p->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002101
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002102 priv->hw->mode->refill_desc3(priv, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002103
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002104 if (priv->rx_zeroc_thresh > 0)
2105 priv->rx_zeroc_thresh--;
2106
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002107 if (netif_msg_rx_status(priv))
2108 pr_debug("\trefill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002109 }
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002110
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002111 wmb();
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002112 priv->hw->desc->set_rx_owner(p);
Deepak Sikri8e839892012-07-08 21:14:45 +00002113 wmb();
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002114
2115 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002116 }
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002117 priv->dirty_rx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002118}
2119
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002120/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002121 * stmmac_rx - manage the receive process
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002122 * @priv: driver private structure
2123 * @limit: napi bugget.
2124 * Description : this the function called by the napi poll method.
2125 * It gets all the frames inside the ring.
2126 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002127static int stmmac_rx(struct stmmac_priv *priv, int limit)
2128{
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002129 unsigned int entry = priv->cur_rx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002130 unsigned int next_entry;
2131 unsigned int count = 0;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002132 int coe = priv->hw->rx_csum;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002133
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002134 if (netif_msg_rx_status(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002135 void *rx_head;
2136
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002137 pr_debug("%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002138 if (priv->extend_desc)
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002139 rx_head = (void *)priv->dma_erx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002140 else
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002141 rx_head = (void *)priv->dma_rx;
2142
2143 priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002144 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002145 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002146 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002147 struct dma_desc *p;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002148
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002149 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002150 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002151 else
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002152 p = priv->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002153
Fabrice Gasnierc1fa3212016-02-29 14:27:34 +01002154 /* read the status of the incoming frame */
2155 status = priv->hw->desc->rx_status(&priv->dev->stats,
2156 &priv->xstats, p);
2157 /* check if managed by the DMA otherwise go ahead */
2158 if (unlikely(status & dma_own))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002159 break;
2160
2161 count++;
2162
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002163 priv->cur_rx = STMMAC_GET_ENTRY(priv->cur_rx, DMA_RX_SIZE);
2164 next_entry = priv->cur_rx;
2165
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002166 if (priv->extend_desc)
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002167 prefetch(priv->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002168 else
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002169 prefetch(priv->dma_rx + next_entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002170
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002171 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2172 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2173 &priv->xstats,
2174 priv->dma_erx +
2175 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002176 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002177 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002178 if (priv->hwts_rx_en && !priv->extend_desc) {
2179 /* DESC2 & DESC3 will be overwitten by device
2180 * with timestamp value, hence reinitialize
2181 * them in stmmac_rx_refill() function so that
2182 * device can reuse it.
2183 */
2184 priv->rx_skbuff[entry] = NULL;
2185 dma_unmap_single(priv->device,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002186 priv->rx_skbuff_dma[entry],
2187 priv->dma_buf_sz,
2188 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002189 }
2190 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002191 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002192 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002193
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002194 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2195
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01002196 /* check if frame_len fits the preallocated memory */
2197 if (frame_len > priv->dma_buf_sz) {
2198 priv->dev->stats.rx_length_errors++;
2199 break;
2200 }
2201
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002202 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002203 * Type frames (LLC/LLC-SNAP)
2204 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002205 if (unlikely(status != llc_snap))
2206 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002207
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002208 if (netif_msg_rx_status(priv)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002209 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002210 p, entry, p->des2);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002211 if (frame_len > ETH_FRAME_LEN)
2212 pr_debug("\tframe size %d, COE: %d\n",
2213 frame_len, status);
2214 }
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002215
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002216 if (unlikely((frame_len < priv->rx_copybreak) ||
2217 stmmac_rx_threshold_count(priv))) {
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002218 skb = netdev_alloc_skb_ip_align(priv->dev,
2219 frame_len);
2220 if (unlikely(!skb)) {
2221 if (net_ratelimit())
2222 dev_warn(priv->device,
2223 "packet dropped\n");
2224 priv->dev->stats.rx_dropped++;
2225 break;
2226 }
2227
2228 dma_sync_single_for_cpu(priv->device,
2229 priv->rx_skbuff_dma
2230 [entry], frame_len,
2231 DMA_FROM_DEVICE);
2232 skb_copy_to_linear_data(skb,
2233 priv->
2234 rx_skbuff[entry]->data,
2235 frame_len);
2236
2237 skb_put(skb, frame_len);
2238 dma_sync_single_for_device(priv->device,
2239 priv->rx_skbuff_dma
2240 [entry], frame_len,
2241 DMA_FROM_DEVICE);
2242 } else {
2243 skb = priv->rx_skbuff[entry];
2244 if (unlikely(!skb)) {
2245 pr_err("%s: Inconsistent Rx chain\n",
2246 priv->dev->name);
2247 priv->dev->stats.rx_dropped++;
2248 break;
2249 }
2250 prefetch(skb->data - NET_IP_ALIGN);
2251 priv->rx_skbuff[entry] = NULL;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002252 priv->rx_zeroc_thresh++;
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002253
2254 skb_put(skb, frame_len);
2255 dma_unmap_single(priv->device,
2256 priv->rx_skbuff_dma[entry],
2257 priv->dma_buf_sz,
2258 DMA_FROM_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002259 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002260
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002261 stmmac_get_rx_hwtstamp(priv, entry, skb);
2262
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002263 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002264 pr_debug("frame received (%dbytes)", frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002265 print_pkt(skb->data, frame_len);
2266 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002267
Vince Bridgersb9381982014-01-14 13:42:05 -06002268 stmmac_rx_vlan(priv->dev, skb);
2269
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002270 skb->protocol = eth_type_trans(skb, priv->dev);
2271
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002272 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002273 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002274 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002275 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002276
2277 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002278
2279 priv->dev->stats.rx_packets++;
2280 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002281 }
2282 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002283 }
2284
2285 stmmac_rx_refill(priv);
2286
2287 priv->xstats.rx_pkt_n += count;
2288
2289 return count;
2290}
2291
2292/**
2293 * stmmac_poll - stmmac poll method (NAPI)
2294 * @napi : pointer to the napi structure.
2295 * @budget : maximum number of packets that the current CPU can receive from
2296 * all interfaces.
2297 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002298 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002299 */
2300static int stmmac_poll(struct napi_struct *napi, int budget)
2301{
2302 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2303 int work_done = 0;
2304
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002305 priv->xstats.napi_poll++;
2306 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002307
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002308 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002309 if (work_done < budget) {
2310 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002311 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002312 }
2313 return work_done;
2314}
2315
2316/**
2317 * stmmac_tx_timeout
2318 * @dev : Pointer to net device structure
2319 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00002320 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002321 * netdev structure and arrange for the device to be reset to a sane state
2322 * in order to transmit a new packet.
2323 */
2324static void stmmac_tx_timeout(struct net_device *dev)
2325{
2326 struct stmmac_priv *priv = netdev_priv(dev);
2327
2328 /* Clear Tx resources and restart transmitting again */
2329 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002330}
2331
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002332/**
Jiri Pirko01789342011-08-16 06:29:00 +00002333 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002334 * @dev : pointer to the device structure
2335 * Description:
2336 * This function is a driver entry point which gets called by the kernel
2337 * whenever multicast addresses must be enabled/disabled.
2338 * Return value:
2339 * void.
2340 */
Jiri Pirko01789342011-08-16 06:29:00 +00002341static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002342{
2343 struct stmmac_priv *priv = netdev_priv(dev);
2344
Vince Bridgers3b57de92014-07-31 15:49:17 -05002345 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002346}
2347
2348/**
2349 * stmmac_change_mtu - entry point to change MTU size for the device.
2350 * @dev : device pointer.
2351 * @new_mtu : the new MTU size for the device.
2352 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2353 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2354 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2355 * Return value:
2356 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2357 * file on failure.
2358 */
2359static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2360{
2361 struct stmmac_priv *priv = netdev_priv(dev);
2362 int max_mtu;
2363
2364 if (netif_running(dev)) {
2365 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2366 return -EBUSY;
2367 }
2368
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00002369 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002370 max_mtu = JUMBO_LEN;
2371 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00002372 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002373
Vince Bridgers2618abb2014-01-20 05:39:01 -06002374 if (priv->plat->maxmtu < max_mtu)
2375 max_mtu = priv->plat->maxmtu;
2376
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002377 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2378 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2379 return -EINVAL;
2380 }
2381
Michał Mirosław5e982f32011-04-09 02:46:55 +00002382 dev->mtu = new_mtu;
2383 netdev_update_features(dev);
2384
2385 return 0;
2386}
2387
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002388static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002389 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002390{
2391 struct stmmac_priv *priv = netdev_priv(dev);
2392
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002393 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002394 features &= ~NETIF_F_RXCSUM;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002395
Michał Mirosław5e982f32011-04-09 02:46:55 +00002396 if (!priv->plat->tx_coe)
Tom Herberta1882222015-12-14 11:19:43 -08002397 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosław5e982f32011-04-09 02:46:55 +00002398
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002399 /* Some GMAC devices have a bugged Jumbo frame support that
2400 * needs to have the Tx COE disabled for oversized frames
2401 * (due to limited buffer sizes). In this case we disable
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002402 * the TX csum insertionin the TDES and not use SF.
2403 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00002404 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08002405 features &= ~NETIF_F_CSUM_MASK;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002406
Michał Mirosław5e982f32011-04-09 02:46:55 +00002407 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002408}
2409
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002410static int stmmac_set_features(struct net_device *netdev,
2411 netdev_features_t features)
2412{
2413 struct stmmac_priv *priv = netdev_priv(netdev);
2414
2415 /* Keep the COE Type in case of csum is supporting */
2416 if (features & NETIF_F_RXCSUM)
2417 priv->hw->rx_csum = priv->plat->rx_coe;
2418 else
2419 priv->hw->rx_csum = 0;
2420 /* No check needed because rx_coe has been set before and it will be
2421 * fixed in case of issue.
2422 */
2423 priv->hw->mac->rx_ipc(priv->hw);
2424
2425 return 0;
2426}
2427
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002428/**
2429 * stmmac_interrupt - main ISR
2430 * @irq: interrupt number.
2431 * @dev_id: to pass the net device pointer.
2432 * Description: this is the main driver interrupt service routine.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002433 * It can call:
2434 * o DMA service routine (to manage incoming frame reception and transmission
2435 * status)
2436 * o Core interrupts to manage: remote wake-up, management counter, LPI
2437 * interrupts.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002438 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002439static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2440{
2441 struct net_device *dev = (struct net_device *)dev_id;
2442 struct stmmac_priv *priv = netdev_priv(dev);
2443
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002444 if (priv->irq_wake)
2445 pm_wakeup_event(priv->device, 0);
2446
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002447 if (unlikely(!dev)) {
2448 pr_err("%s: invalid dev pointer\n", __func__);
2449 return IRQ_NONE;
2450 }
2451
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002452 /* To handle GMAC own interrupts */
2453 if (priv->plat->has_gmac) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002454 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002455 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002456 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002457 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002458 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002459 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002460 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002461 priv->tx_path_in_lpi_mode = false;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002462 }
2463 }
2464
2465 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002466 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002467
2468 return IRQ_HANDLED;
2469}
2470
2471#ifdef CONFIG_NET_POLL_CONTROLLER
2472/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002473 * to allow network I/O with interrupts disabled.
2474 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002475static void stmmac_poll_controller(struct net_device *dev)
2476{
2477 disable_irq(dev->irq);
2478 stmmac_interrupt(dev->irq, dev);
2479 enable_irq(dev->irq);
2480}
2481#endif
2482
2483/**
2484 * stmmac_ioctl - Entry point for the Ioctl
2485 * @dev: Device pointer.
2486 * @rq: An IOCTL specefic structure, that can contain a pointer to
2487 * a proprietary structure used to pass information to the driver.
2488 * @cmd: IOCTL command
2489 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002490 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002491 */
2492static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2493{
2494 struct stmmac_priv *priv = netdev_priv(dev);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002495 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002496
2497 if (!netif_running(dev))
2498 return -EINVAL;
2499
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002500 switch (cmd) {
2501 case SIOCGMIIPHY:
2502 case SIOCGMIIREG:
2503 case SIOCSMIIREG:
2504 if (!priv->phydev)
2505 return -EINVAL;
2506 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2507 break;
2508 case SIOCSHWTSTAMP:
2509 ret = stmmac_hwtstamp_ioctl(dev, rq);
2510 break;
2511 default:
2512 break;
2513 }
Richard Cochran28b04112010-07-17 08:48:55 +00002514
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002515 return ret;
2516}
2517
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002518#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002519static struct dentry *stmmac_fs_dir;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002520
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002521static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002522 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002523{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002524 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002525 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2526 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002527
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002528 for (i = 0; i < size; i++) {
2529 u64 x;
2530 if (extend_desc) {
2531 x = *(u64 *) ep;
2532 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002533 i, (unsigned int)virt_to_phys(ep),
2534 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002535 ep->basic.des2, ep->basic.des3);
2536 ep++;
2537 } else {
2538 x = *(u64 *) p;
2539 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002540 i, (unsigned int)virt_to_phys(ep),
2541 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002542 p->des2, p->des3);
2543 p++;
2544 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002545 seq_printf(seq, "\n");
2546 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002547}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002548
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002549static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2550{
2551 struct net_device *dev = seq->private;
2552 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002553
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002554 if (priv->extend_desc) {
2555 seq_printf(seq, "Extended RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002556 sysfs_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002557 seq_printf(seq, "Extended TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002558 sysfs_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002559 } else {
2560 seq_printf(seq, "RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002561 sysfs_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002562 seq_printf(seq, "TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002563 sysfs_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002564 }
2565
2566 return 0;
2567}
2568
2569static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2570{
2571 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2572}
2573
2574static const struct file_operations stmmac_rings_status_fops = {
2575 .owner = THIS_MODULE,
2576 .open = stmmac_sysfs_ring_open,
2577 .read = seq_read,
2578 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002579 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002580};
2581
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002582static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2583{
2584 struct net_device *dev = seq->private;
2585 struct stmmac_priv *priv = netdev_priv(dev);
2586
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002587 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002588 seq_printf(seq, "DMA HW features not supported\n");
2589 return 0;
2590 }
2591
2592 seq_printf(seq, "==============================\n");
2593 seq_printf(seq, "\tDMA HW features\n");
2594 seq_printf(seq, "==============================\n");
2595
2596 seq_printf(seq, "\t10/100 Mbps %s\n",
2597 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2598 seq_printf(seq, "\t1000 Mbps %s\n",
2599 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2600 seq_printf(seq, "\tHalf duple %s\n",
2601 (priv->dma_cap.half_duplex) ? "Y" : "N");
2602 seq_printf(seq, "\tHash Filter: %s\n",
2603 (priv->dma_cap.hash_filter) ? "Y" : "N");
2604 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2605 (priv->dma_cap.multi_addr) ? "Y" : "N");
2606 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2607 (priv->dma_cap.pcs) ? "Y" : "N");
2608 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2609 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2610 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2611 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2612 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2613 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2614 seq_printf(seq, "\tRMON module: %s\n",
2615 (priv->dma_cap.rmon) ? "Y" : "N");
2616 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2617 (priv->dma_cap.time_stamp) ? "Y" : "N");
2618 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2619 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2620 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2621 (priv->dma_cap.eee) ? "Y" : "N");
2622 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2623 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2624 (priv->dma_cap.tx_coe) ? "Y" : "N");
2625 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2626 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2627 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2628 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2629 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2630 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2631 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2632 priv->dma_cap.number_rx_channel);
2633 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2634 priv->dma_cap.number_tx_channel);
2635 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2636 (priv->dma_cap.enh_desc) ? "Y" : "N");
2637
2638 return 0;
2639}
2640
2641static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2642{
2643 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2644}
2645
2646static const struct file_operations stmmac_dma_cap_fops = {
2647 .owner = THIS_MODULE,
2648 .open = stmmac_sysfs_dma_cap_open,
2649 .read = seq_read,
2650 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002651 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002652};
2653
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002654static int stmmac_init_fs(struct net_device *dev)
2655{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002656 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002657
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002658 /* Create per netdev entries */
2659 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
2660
2661 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
2662 pr_err("ERROR %s/%s, debugfs create directory failed\n",
2663 STMMAC_RESOURCE_NAME, dev->name);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002664
2665 return -ENOMEM;
2666 }
2667
2668 /* Entry to report DMA RX/TX rings */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002669 priv->dbgfs_rings_status =
2670 debugfs_create_file("descriptors_status", S_IRUGO,
2671 priv->dbgfs_dir, dev,
2672 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002673
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002674 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002675 pr_info("ERROR creating stmmac ring debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002676 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002677
2678 return -ENOMEM;
2679 }
2680
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002681 /* Entry to report the DMA HW features */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002682 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
2683 priv->dbgfs_dir,
2684 dev, &stmmac_dma_cap_fops);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002685
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002686 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002687 pr_info("ERROR creating stmmac MMC debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002688 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002689
2690 return -ENOMEM;
2691 }
2692
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002693 return 0;
2694}
2695
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002696static void stmmac_exit_fs(struct net_device *dev)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002697{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002698 struct stmmac_priv *priv = netdev_priv(dev);
2699
2700 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002701}
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002702#endif /* CONFIG_DEBUG_FS */
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002703
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002704static const struct net_device_ops stmmac_netdev_ops = {
2705 .ndo_open = stmmac_open,
2706 .ndo_start_xmit = stmmac_xmit,
2707 .ndo_stop = stmmac_release,
2708 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00002709 .ndo_fix_features = stmmac_fix_features,
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002710 .ndo_set_features = stmmac_set_features,
Jiri Pirko01789342011-08-16 06:29:00 +00002711 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002712 .ndo_tx_timeout = stmmac_tx_timeout,
2713 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002714#ifdef CONFIG_NET_POLL_CONTROLLER
2715 .ndo_poll_controller = stmmac_poll_controller,
2716#endif
2717 .ndo_set_mac_address = eth_mac_addr,
2718};
2719
2720/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002721 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002722 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002723 * Description: this function is to configure the MAC device according to
2724 * some platform parameters or the HW capability register. It prepares the
2725 * driver to use either ring or chain modes and to setup either enhanced or
2726 * normal descriptors.
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002727 */
2728static int stmmac_hw_init(struct stmmac_priv *priv)
2729{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002730 struct mac_device_info *mac;
2731
2732 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002733 if (priv->plat->has_gmac) {
2734 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05002735 mac = dwmac1000_setup(priv->ioaddr,
2736 priv->plat->multicast_filter_bins,
Alexandre TORGUEc623d142016-04-01 11:37:27 +02002737 priv->plat->unicast_filter_entries,
2738 &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002739 } else {
Alexandre TORGUEc623d142016-04-01 11:37:27 +02002740 mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002741 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002742 if (!mac)
2743 return -ENOMEM;
2744
2745 priv->hw = mac;
2746
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002747 /* To use the chained or ring mode */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002748 if (chain_mode) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002749 priv->hw->mode = &chain_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002750 pr_info(" Chain mode enabled\n");
2751 priv->mode = STMMAC_CHAIN_MODE;
2752 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002753 priv->hw->mode = &ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002754 pr_info(" Ring mode enabled\n");
2755 priv->mode = STMMAC_RING_MODE;
2756 }
2757
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002758 /* Get the HW capability (new GMAC newer than 3.50a) */
2759 priv->hw_cap_support = stmmac_get_hw_features(priv);
2760 if (priv->hw_cap_support) {
2761 pr_info(" DMA HW capability register supported");
2762
2763 /* We can override some gmac/dma configuration fields: e.g.
2764 * enh_desc, tx_coe (e.g. that are passed through the
2765 * platform) with the values from the HW capability
2766 * register (if supported).
2767 */
2768 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002769 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002770
Sonic Zhangdec21652015-01-22 14:55:57 +08002771 /* TXCOE doesn't work in thresh DMA mode */
2772 if (priv->plat->force_thresh_dma_mode)
2773 priv->plat->tx_coe = 0;
2774 else
2775 priv->plat->tx_coe = priv->dma_cap.tx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002776
2777 if (priv->dma_cap.rx_coe_type2)
2778 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2779 else if (priv->dma_cap.rx_coe_type1)
2780 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2781
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002782 } else
2783 pr_info(" No HW DMA feature register supported");
2784
Byungho An61369d02013-06-28 16:35:32 +09002785 /* To use alternate (extended) or normal descriptor structures */
2786 stmmac_selec_desc_mode(priv);
2787
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002788 if (priv->plat->rx_coe) {
2789 priv->hw->rx_csum = priv->plat->rx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002790 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2791 priv->plat->rx_coe);
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002792 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002793 if (priv->plat->tx_coe)
2794 pr_info(" TX Checksum insertion supported\n");
2795
2796 if (priv->plat->pmt) {
2797 pr_info(" Wake-Up On Lan supported\n");
2798 device_set_wakeup_capable(priv->device, 1);
2799 }
2800
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002801 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002802}
2803
2804/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002805 * stmmac_dvr_probe
2806 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002807 * @plat_dat: platform data pointer
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002808 * @res: stmmac resource pointer
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002809 * Description: this is the main probe function used to
2810 * call the alloc_etherdev, allocate the priv structure.
Andy Shevchenko9afec6e2015-01-27 18:38:03 +02002811 * Return:
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002812 * returns 0 on success, otherwise errno.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002813 */
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002814int stmmac_dvr_probe(struct device *device,
2815 struct plat_stmmacenet_data *plat_dat,
2816 struct stmmac_resources *res)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002817{
2818 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002819 struct net_device *ndev = NULL;
2820 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002821
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002822 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002823 if (!ndev)
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002824 return -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002825
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002826 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002827
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002828 priv = netdev_priv(ndev);
2829 priv->device = device;
2830 priv->dev = ndev;
2831
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002832 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002833 priv->pause = pause;
2834 priv->plat = plat_dat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002835 priv->ioaddr = res->addr;
2836 priv->dev->base_addr = (unsigned long)res->addr;
2837
2838 priv->dev->irq = res->irq;
2839 priv->wol_irq = res->wol_irq;
2840 priv->lpi_irq = res->lpi_irq;
2841
2842 if (res->mac)
2843 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002844
Joachim Eastwooda7a62682015-07-17 23:48:17 +02002845 dev_set_drvdata(device, priv->dev);
Joachim Eastwood803f8fc2015-05-20 20:03:06 +02002846
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002847 /* Verify driver arguments */
2848 stmmac_verify_args();
2849
2850 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002851 * this needs to have multiple instances
2852 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002853 if ((phyaddr >= 0) && (phyaddr <= 31))
2854 priv->plat->phy_addr = phyaddr;
2855
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002856 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
2857 if (IS_ERR(priv->stmmac_clk)) {
2858 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
2859 __func__);
Kweh, Hock Leongc5bb86c2014-09-26 21:42:55 +08002860 /* If failed to obtain stmmac_clk and specific clk_csr value
2861 * is NOT passed from the platform, probe fail.
2862 */
2863 if (!priv->plat->clk_csr) {
2864 ret = PTR_ERR(priv->stmmac_clk);
2865 goto error_clk_get;
2866 } else {
2867 priv->stmmac_clk = NULL;
2868 }
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002869 }
2870 clk_prepare_enable(priv->stmmac_clk);
2871
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002872 priv->pclk = devm_clk_get(priv->device, "pclk");
2873 if (IS_ERR(priv->pclk)) {
2874 if (PTR_ERR(priv->pclk) == -EPROBE_DEFER) {
2875 ret = -EPROBE_DEFER;
2876 goto error_pclk_get;
2877 }
2878 priv->pclk = NULL;
2879 }
2880 clk_prepare_enable(priv->pclk);
2881
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002882 priv->stmmac_rst = devm_reset_control_get(priv->device,
2883 STMMAC_RESOURCE_NAME);
2884 if (IS_ERR(priv->stmmac_rst)) {
2885 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
2886 ret = -EPROBE_DEFER;
2887 goto error_hw_init;
2888 }
2889 dev_info(priv->device, "no reset control found\n");
2890 priv->stmmac_rst = NULL;
2891 }
2892 if (priv->stmmac_rst)
2893 reset_control_deassert(priv->stmmac_rst);
2894
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002895 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002896 ret = stmmac_hw_init(priv);
2897 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002898 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002899
2900 ndev->netdev_ops = &stmmac_netdev_ops;
2901
2902 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2903 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002904 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2905 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002906#ifdef STMMAC_VLAN_TAG_USED
2907 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00002908 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002909#endif
2910 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2911
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002912 if (flow_ctrl)
2913 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2914
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002915 /* Rx Watchdog is available in the COREs newer than the 3.40.
2916 * In some case, for example on bugged HW this feature
2917 * has to be disable and this can be done by passing the
2918 * riwt_off field from the platform.
2919 */
2920 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2921 priv->use_riwt = 1;
2922 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2923 }
2924
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002925 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002926
Vlad Lunguf8e96162010-11-29 22:52:52 +00002927 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002928 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002929
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002930 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002931 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002932 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002933 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002934 }
2935
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002936 /* If a specific clk_csr value is passed from the platform
2937 * this means that the CSR Clock Range selection cannot be
2938 * changed at run-time and it is fixed. Viceversa the driver'll try to
2939 * set the MDC clock dynamically according to the csr actual
2940 * clock input.
2941 */
2942 if (!priv->plat->clk_csr)
2943 stmmac_clk_csr_set(priv);
2944 else
2945 priv->clk_csr = priv->plat->clk_csr;
2946
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002947 stmmac_check_pcs_mode(priv);
2948
Byungho An4d8f0822013-04-07 17:56:16 +00002949 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2950 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002951 /* MDIO bus Registration */
2952 ret = stmmac_mdio_register(ndev);
2953 if (ret < 0) {
2954 pr_debug("%s: MDIO bus (id: %d) registration failed",
2955 __func__, priv->plat->bus_id);
2956 goto error_mdio_register;
2957 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002958 }
2959
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002960 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002961
Viresh Kumar6a81c262012-07-30 14:39:41 -07002962error_mdio_register:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002963 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002964error_netdev_register:
2965 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002966error_hw_init:
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002967 clk_disable_unprepare(priv->pclk);
2968error_pclk_get:
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002969 clk_disable_unprepare(priv->stmmac_clk);
2970error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002971 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002972
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002973 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002974}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02002975EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002976
2977/**
2978 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002979 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002980 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002981 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002982 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002983int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002984{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002985 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002986
2987 pr_info("%s:\n\tremoving driver", __func__);
2988
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002989 priv->hw->dma->stop_rx(priv->ioaddr);
2990 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002991
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002992 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002993 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002994 unregister_netdev(ndev);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002995 if (priv->stmmac_rst)
2996 reset_control_assert(priv->stmmac_rst);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002997 clk_disable_unprepare(priv->pclk);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002998 clk_disable_unprepare(priv->stmmac_clk);
Bryan O'Donoghuee7434712015-04-16 17:56:03 +01002999 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
3000 priv->pcs != STMMAC_PCS_RTBI)
3001 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003002 free_netdev(ndev);
3003
3004 return 0;
3005}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003006EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003007
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003008/**
3009 * stmmac_suspend - suspend callback
3010 * @ndev: net device pointer
3011 * Description: this is the function to suspend the device and it is called
3012 * by the platform driver to stop the network queue, release the resources,
3013 * program the PMT register (for WoL), clean and release driver resources.
3014 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003015int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003016{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003017 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003018 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003019
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003020 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003021 return 0;
3022
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003023 if (priv->phydev)
3024 phy_stop(priv->phydev);
3025
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003026 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003027
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003028 netif_device_detach(ndev);
3029 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003030
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003031 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003032
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003033 /* Stop TX/RX DMA */
3034 priv->hw->dma->stop_tx(priv->ioaddr);
3035 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003036
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003037 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003038 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003039 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003040 priv->irq_wake = 1;
3041 } else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003042 stmmac_set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003043 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003044 /* Disable clock in case of PWM is off */
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003045 clk_disable(priv->pclk);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003046 clk_disable(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003047 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003048 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05003049
3050 priv->oldlink = 0;
3051 priv->speed = 0;
3052 priv->oldduplex = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003053 return 0;
3054}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003055EXPORT_SYMBOL_GPL(stmmac_suspend);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003056
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003057/**
3058 * stmmac_resume - resume callback
3059 * @ndev: net device pointer
3060 * Description: when resume this function is invoked to setup the DMA and CORE
3061 * in a usable state.
3062 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003063int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003064{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003065 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003066 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003067
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003068 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003069 return 0;
3070
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003071 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02003072
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003073 /* Power Down bit, into the PM register, is cleared
3074 * automatically as soon as a magic packet or a Wake-up frame
3075 * is received. Anyway, it's better to manually clear
3076 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003077 * from another devices (e.g. serial console).
3078 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003079 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003080 priv->hw->mac->pmt(priv->hw, 0);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003081 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003082 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003083 pinctrl_pm_select_default_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003084 /* enable the clk prevously disabled */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003085 clk_enable(priv->stmmac_clk);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003086 clk_enable(priv->pclk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003087 /* reset the phy so that it's ready */
3088 if (priv->mii)
3089 stmmac_mdio_reset(priv->mii);
3090 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003091
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003092 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003093
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01003094 priv->cur_rx = 0;
3095 priv->dirty_rx = 0;
3096 priv->dirty_tx = 0;
3097 priv->cur_tx = 0;
3098 stmmac_clear_descriptors(priv);
3099
Huacai Chenfe1319292014-12-19 22:38:18 +08003100 stmmac_hw_setup(ndev, false);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003101 stmmac_init_tx_coalesce(priv);
Giuseppe CAVALLAROac316c72015-11-26 08:35:41 +01003102 stmmac_set_rx_mode(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003103
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003104 napi_enable(&priv->napi);
3105
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003106 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003107
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003108 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003109
3110 if (priv->phydev)
3111 phy_start(priv->phydev);
3112
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003113 return 0;
3114}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003115EXPORT_SYMBOL_GPL(stmmac_resume);
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00003116
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003117#ifndef MODULE
3118static int __init stmmac_cmdline_opt(char *str)
3119{
3120 char *opt;
3121
3122 if (!str || !*str)
3123 return -EINVAL;
3124 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003125 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003126 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003127 goto err;
3128 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003129 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003130 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003131 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003132 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003133 goto err;
3134 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003135 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003136 goto err;
3137 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003138 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003139 goto err;
3140 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003141 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003142 goto err;
3143 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003144 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003145 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00003146 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003147 if (kstrtoint(opt + 10, 0, &eee_timer))
3148 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003149 } else if (!strncmp(opt, "chain_mode:", 11)) {
3150 if (kstrtoint(opt + 11, 0, &chain_mode))
3151 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003152 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003153 }
3154 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003155
3156err:
3157 pr_err("%s: ERROR broken module parameter conversion", __func__);
3158 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003159}
3160
3161__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003162#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003163
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003164static int __init stmmac_init(void)
3165{
3166#ifdef CONFIG_DEBUG_FS
3167 /* Create debugfs main directory if it doesn't exist yet */
3168 if (!stmmac_fs_dir) {
3169 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
3170
3171 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
3172 pr_err("ERROR %s, debugfs create directory failed\n",
3173 STMMAC_RESOURCE_NAME);
3174
3175 return -ENOMEM;
3176 }
3177 }
3178#endif
3179
3180 return 0;
3181}
3182
3183static void __exit stmmac_exit(void)
3184{
3185#ifdef CONFIG_DEBUG_FS
3186 debugfs_remove_recursive(stmmac_fs_dir);
3187#endif
3188}
3189
3190module_init(stmmac_init)
3191module_exit(stmmac_exit)
3192
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003193MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3194MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3195MODULE_LICENSE("GPL");