blob: 00e508498a810c2d23d0df0c9fd63f8904b9b9f1 [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 |
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001453 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001454
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001455 priv->mmcaddr = priv->ioaddr + MMC_GMAC3_X_OFFSET;
1456
1457 dwmac_mmc_intr_all_mask(priv->mmcaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001458
1459 if (priv->dma_cap.rmon) {
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02001460 dwmac_mmc_ctrl(priv->mmcaddr, mode);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00001461 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1462 } else
Stefan Roeseaae54cf2012-01-10 01:47:51 +00001463 pr_info(" No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00001464}
1465
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001466/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001467 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001468 * @priv: driver private structure
1469 * Description: select the Enhanced/Alternate or Normal descriptors.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001470 * In case of Enhanced/Alternate, it checks if the extended descriptors are
1471 * supported by the HW capability register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00001472 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001473static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1474{
1475 if (priv->plat->enh_desc) {
1476 pr_info(" Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001477
1478 /* GMAC older than 3.50 has no extended descriptors */
1479 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1480 pr_info("\tEnabled extended descriptors\n");
1481 priv->extend_desc = 1;
1482 } else
1483 pr_warn("Extended descriptors not supported\n");
1484
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001485 priv->hw->desc = &enh_desc_ops;
1486 } else {
1487 pr_info(" Normal descriptors\n");
1488 priv->hw->desc = &ndesc_ops;
1489 }
1490}
1491
1492/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001493 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001494 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001495 * Description:
1496 * new GMAC chip generations have a new register to indicate the
1497 * presence of the optional feature/functions.
1498 * This can be also used to override the value passed through the
1499 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001500 */
1501static int stmmac_get_hw_features(struct stmmac_priv *priv)
1502{
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001503 u32 ret = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00001504
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00001505 if (priv->hw->dma->get_hw_feature) {
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001506 priv->hw->dma->get_hw_feature(priv->ioaddr,
1507 &priv->dma_cap);
1508 ret = 1;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00001509 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001510
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02001511 return ret;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00001512}
1513
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001514/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001515 * stmmac_check_ether_addr - check if the MAC addr is valid
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001516 * @priv: driver private structure
1517 * Description:
1518 * it is to verify if the MAC address is valid, in case of failures it
1519 * generates a random MAC address
1520 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001521static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1522{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001523 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001524 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001525 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001526 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00001527 eth_hw_addr_random(priv->dev);
Hans de Goedec88460b2014-01-26 15:50:44 +01001528 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1529 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001530 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001531}
1532
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001533/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001534 * stmmac_init_dma_engine - DMA init.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001535 * @priv: driver private structure
1536 * Description:
1537 * It inits the DMA invoking the specific MAC/GMAC callback.
1538 * Some DMA parameters can be passed from the platform;
1539 * in case of these are not passed a default is kept for the MAC or GMAC.
1540 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001541static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1542{
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001543 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, aal = 0;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001544 int mixed_burst = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001545 int atds = 0;
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001546 int ret = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001547
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001548 if (priv->plat->dma_cfg) {
1549 pbl = priv->plat->dma_cfg->pbl;
1550 fixed_burst = priv->plat->dma_cfg->fixed_burst;
Giuseppe CAVALLAROb9cde0a2012-05-13 22:18:42 +00001551 mixed_burst = priv->plat->dma_cfg->mixed_burst;
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001552 aal = priv->plat->dma_cfg->aal;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001553 }
1554
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001555 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1556 atds = 1;
1557
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001558 ret = priv->hw->dma->reset(priv->ioaddr);
1559 if (ret) {
1560 dev_err(priv->device, "Failed to reset the dma\n");
1561 return ret;
1562 }
1563
1564 priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01001565 aal, priv->dma_tx_phy, priv->dma_rx_phy, atds);
1566
1567 if ((priv->synopsys_id >= DWMAC_CORE_3_50) &&
1568 (priv->plat->axi && priv->hw->dma->axi))
1569 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
1570
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01001571 return ret;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00001572}
1573
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001574/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001575 * stmmac_tx_timer - mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001576 * @data: data pointer
1577 * Description:
1578 * This is the timer handler to directly invoke the stmmac_tx_clean.
1579 */
1580static void stmmac_tx_timer(unsigned long data)
1581{
1582 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1583
1584 stmmac_tx_clean(priv);
1585}
1586
1587/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001588 * stmmac_init_tx_coalesce - init tx mitigation options.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001589 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001590 * Description:
1591 * This inits the transmit coalesce parameters: i.e. timer rate,
1592 * timer handler and default threshold used for enabling the
1593 * interrupt on completion bit.
1594 */
1595static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1596{
1597 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1598 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1599 init_timer(&priv->txtimer);
1600 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1601 priv->txtimer.data = (unsigned long)priv;
1602 priv->txtimer.function = stmmac_tx_timer;
1603 add_timer(&priv->txtimer);
1604}
1605
1606/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001607 * stmmac_hw_setup - setup mac in a usable state.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001608 * @dev : pointer to the device structure.
1609 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001610 * this is the main function to setup the HW in a usable state because the
1611 * dma engine is reset, the core registers are configured (e.g. AXI,
1612 * Checksum features, timers). The DMA is ready to start receiving and
1613 * transmitting.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001614 * Return value:
1615 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1616 * file on failure.
1617 */
Huacai Chenfe1319292014-12-19 22:38:18 +08001618static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001619{
1620 struct stmmac_priv *priv = netdev_priv(dev);
1621 int ret;
1622
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001623 /* DMA initialization and SW reset */
1624 ret = stmmac_init_dma_engine(priv);
1625 if (ret < 0) {
1626 pr_err("%s: DMA engine initialization failed\n", __func__);
1627 return ret;
1628 }
1629
1630 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001631 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001632
1633 /* If required, perform hw setup of the bus. */
1634 if (priv->plat->bus_setup)
1635 priv->plat->bus_setup(priv->ioaddr);
1636
1637 /* Initialize the MAC Core */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001638 priv->hw->mac->core_init(priv->hw, dev->mtu);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001639
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001640 ret = priv->hw->mac->rx_ipc(priv->hw);
1641 if (!ret) {
1642 pr_warn(" RX IPC Checksum Offload disabled\n");
1643 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02001644 priv->hw->rx_csum = 0;
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02001645 }
1646
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001647 /* Enable the MAC Rx/Tx */
1648 stmmac_set_mac(priv->ioaddr, true);
1649
1650 /* Set the HW DMA mode and the COE */
1651 stmmac_dma_operation_mode(priv);
1652
1653 stmmac_mmc_setup(priv);
1654
Huacai Chenfe1319292014-12-19 22:38:18 +08001655 if (init_ptp) {
1656 ret = stmmac_init_ptp(priv);
1657 if (ret && ret != -EOPNOTSUPP)
1658 pr_warn("%s: failed PTP initialisation\n", __func__);
1659 }
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001660
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001661#ifdef CONFIG_DEBUG_FS
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001662 ret = stmmac_init_fs(dev);
1663 if (ret < 0)
1664 pr_warn("%s: failed debugFS registration\n", __func__);
1665#endif
1666 /* Start the ball rolling... */
1667 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1668 priv->hw->dma->start_tx(priv->ioaddr);
1669 priv->hw->dma->start_rx(priv->ioaddr);
1670
1671 /* Dump DMA/MAC registers */
1672 if (netif_msg_hw(priv)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001673 priv->hw->mac->dump_regs(priv->hw);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001674 priv->hw->dma->dump_regs(priv->ioaddr);
1675 }
1676 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1677
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001678 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1679 priv->rx_riwt = MAX_DMA_RIWT;
1680 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1681 }
1682
1683 if (priv->pcs && priv->hw->mac->ctrl_ane)
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05001684 priv->hw->mac->ctrl_ane(priv->hw, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001685
1686 return 0;
1687}
1688
1689/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001690 * stmmac_open - open entry point of the driver
1691 * @dev : pointer to the device structure.
1692 * Description:
1693 * This function is the open entry point of the driver.
1694 * Return value:
1695 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1696 * file on failure.
1697 */
1698static int stmmac_open(struct net_device *dev)
1699{
1700 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001701 int ret;
1702
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001703 stmmac_check_ether_addr(priv);
1704
Byungho An4d8f0822013-04-07 17:56:16 +00001705 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1706 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001707 ret = stmmac_init_phy(dev);
1708 if (ret) {
1709 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1710 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02001711 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00001712 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001713 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001714
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001715 /* Extra statistics */
1716 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1717 priv->xstats.threshold = tc;
1718
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001719 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01001720 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001721
Tobias Klauser7262b7b2014-02-22 13:09:03 +01001722 ret = alloc_dma_desc_resources(priv);
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001723 if (ret < 0) {
1724 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1725 goto dma_desc_error;
1726 }
1727
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001728 ret = init_dma_desc_rings(dev, GFP_KERNEL);
1729 if (ret < 0) {
1730 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1731 goto init_error;
1732 }
1733
Huacai Chenfe1319292014-12-19 22:38:18 +08001734 ret = stmmac_hw_setup(dev, true);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001735 if (ret < 0) {
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001736 pr_err("%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001737 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001738 }
1739
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01001740 stmmac_init_tx_coalesce(priv);
1741
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00001742 if (priv->phydev)
1743 phy_start(priv->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001744
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001745 /* Request the IRQ lines */
1746 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001747 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001748 if (unlikely(ret < 0)) {
1749 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1750 __func__, dev->irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001751 goto init_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001752 }
1753
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001754 /* Request the Wake IRQ in case of another line is used for WoL */
1755 if (priv->wol_irq != dev->irq) {
1756 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1757 IRQF_SHARED, dev->name, dev);
1758 if (unlikely(ret < 0)) {
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001759 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1760 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001761 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001762 }
1763 }
1764
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001765 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001766 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001767 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1768 dev->name, dev);
1769 if (unlikely(ret < 0)) {
1770 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1771 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001772 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001773 }
1774 }
1775
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001776 napi_enable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001777 netif_start_queue(dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001778
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001779 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001780
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001781lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001782 if (priv->wol_irq != dev->irq)
1783 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001784wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001785 free_irq(dev->irq, dev);
1786
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02001787init_error:
1788 free_dma_desc_resources(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001789dma_desc_error:
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001790 if (priv->phydev)
1791 phy_disconnect(priv->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00001792
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00001793 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001794}
1795
1796/**
1797 * stmmac_release - close entry point of the driver
1798 * @dev : device pointer.
1799 * Description:
1800 * This is the stop entry point of the driver.
1801 */
1802static int stmmac_release(struct net_device *dev)
1803{
1804 struct stmmac_priv *priv = netdev_priv(dev);
1805
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001806 if (priv->eee_enabled)
1807 del_timer_sync(&priv->eee_ctrl_timer);
1808
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001809 /* Stop and disconnect the PHY */
1810 if (priv->phydev) {
1811 phy_stop(priv->phydev);
1812 phy_disconnect(priv->phydev);
1813 priv->phydev = NULL;
1814 }
1815
1816 netif_stop_queue(dev);
1817
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001818 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001819
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001820 del_timer_sync(&priv->txtimer);
1821
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001822 /* Free the IRQ lines */
1823 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00001824 if (priv->wol_irq != dev->irq)
1825 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08001826 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001827 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001828
1829 /* Stop TX/RX DMA and clear the descriptors */
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00001830 priv->hw->dma->stop_tx(priv->ioaddr);
1831 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001832
1833 /* Release and free the Rx/Tx resources */
1834 free_dma_desc_resources(priv);
1835
avisconti19449bf2010-10-25 18:58:14 +00001836 /* Disable the MAC Rx/Tx */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001837 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001838
1839 netif_carrier_off(dev);
1840
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01001841#ifdef CONFIG_DEBUG_FS
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07001842 stmmac_exit_fs(dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001843#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00001844
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00001845 stmmac_release_ptp(priv);
1846
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001847 return 0;
1848}
1849
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001850/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001851 * stmmac_xmit - Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001852 * @skb : the socket buffer
1853 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001854 * Description : this is the tx entry point of the driver.
1855 * It programs the chain or the ring and supports oversized frames
1856 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001857 */
1858static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1859{
1860 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001861 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001862 int i, csum_insertion = 0, is_jumbo = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001863 int nfrags = skb_shinfo(skb)->nr_frags;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001864 unsigned int entry, first_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001865 struct dma_desc *desc, *first;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001866 unsigned int enh_desc;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001867
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001868 spin_lock(&priv->tx_lock);
1869
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001870 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
Fabrice Gasnier16ee8172014-11-04 17:08:05 +01001871 spin_unlock(&priv->tx_lock);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001872 if (!netif_queue_stopped(dev)) {
1873 netif_stop_queue(dev);
1874 /* This is a hard error, log it. */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001875 pr_err("%s: Tx Ring full when queue awake\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001876 }
1877 return NETDEV_TX_BUSY;
1878 }
1879
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001880 if (priv->tx_path_in_lpi_mode)
1881 stmmac_disable_eee_mode(priv);
1882
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001883 entry = priv->cur_tx;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001884 first_entry = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001885
Michał Mirosław5e982f32011-04-09 02:46:55 +00001886 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001887
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001888 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001889 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001890 else
1891 desc = priv->dma_tx + entry;
1892
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001893 first = desc;
1894
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001895 priv->tx_skbuff[first_entry] = skb;
1896
1897 enh_desc = priv->plat->enh_desc;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001898 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001899 if (enh_desc)
1900 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
1901
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001902 if (unlikely(is_jumbo)) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001903 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001904 if (unlikely(entry < 0))
1905 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001906 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001907
1908 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00001909 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1910 int len = skb_frag_size(frag);
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01001911 bool last_segment = (i == (nfrags - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001912
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001913 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
1914
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001915 if (likely(priv->extend_desc))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001916 desc = (struct dma_desc *)(priv->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001917 else
1918 desc = priv->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001919
Ian Campbellf7223802011-09-21 21:53:20 +00001920 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1921 DMA_TO_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001922 if (dma_mapping_error(priv->device, desc->des2))
1923 goto dma_map_err; /* should reuse desc w/o issues */
1924
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001925 priv->tx_skbuff[entry] = NULL;
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001926 priv->tx_skbuff_dma[entry].buf = desc->des2;
1927 priv->tx_skbuff_dma[entry].map_as_page = true;
Giuseppe Cavallaro553e2ab2016-02-29 14:27:31 +01001928 priv->tx_skbuff_dma[entry].len = len;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001929 priv->tx_skbuff_dma[entry].last_segment = last_segment;
1930
1931 /* Prepare the descriptor and set the own bit too */
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001932 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01001933 priv->mode, 1, last_segment);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001934 }
1935
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001936 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
1937
1938 priv->cur_tx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001939
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001940 if (netif_msg_pktdata(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02001941 void *tx_head;
1942
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001943 pr_debug("%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
1944 __func__, priv->cur_tx, priv->dirty_tx, first_entry,
1945 entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001946
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001947 if (priv->extend_desc)
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02001948 tx_head = (void *)priv->dma_etx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001949 else
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02001950 tx_head = (void *)priv->dma_tx;
1951
1952 priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001953
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001954 pr_debug(">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001955 print_pkt(skb->data, skb->len);
1956 }
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001957
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001958 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02001959 if (netif_msg_hw(priv))
1960 pr_debug("%s: stop transmitted packets\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001961 netif_stop_queue(dev);
1962 }
1963
1964 dev->stats.tx_bytes += skb->len;
1965
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001966 /* According to the coalesce parameter the IC bit for the latest
1967 * segment is reset and the timer re-started to clean the tx status.
1968 * This approach takes care about the fragments: desc is the first
1969 * element in case of no SG.
1970 */
1971 priv->tx_count_frames += nfrags + 1;
1972 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
1973 mod_timer(&priv->txtimer,
1974 STMMAC_COAL_TIMER(priv->tx_coal_timer));
1975 } else {
1976 priv->tx_count_frames = 0;
1977 priv->hw->desc->set_tx_ic(desc);
1978 priv->xstats.tx_set_ic_bit++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00001979 }
1980
1981 if (!priv->hwts_tx_en)
1982 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00001983
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01001984 /* Ready to fill the first descriptor and set the OWN bit w/o any
1985 * problems because all the descriptors are actually ready to be
1986 * passed to the DMA engine.
1987 */
1988 if (likely(!is_jumbo)) {
1989 bool last_segment = (nfrags == 0);
1990
1991 first->des2 = dma_map_single(priv->device, skb->data,
1992 nopaged_len, DMA_TO_DEVICE);
1993 if (dma_mapping_error(priv->device, first->des2))
1994 goto dma_map_err;
1995
1996 priv->tx_skbuff_dma[first_entry].buf = first->des2;
1997 priv->tx_skbuff_dma[first_entry].len = nopaged_len;
1998 priv->tx_skbuff_dma[first_entry].last_segment = last_segment;
1999
2000 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2001 priv->hwts_tx_en)) {
2002 /* declare that device is doing timestamping */
2003 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2004 priv->hw->desc->enable_tx_timestamp(first);
2005 }
2006
2007 /* Prepare the first descriptor setting the OWN bit too */
2008 priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
2009 csum_insertion, priv->mode, 1,
2010 last_segment);
2011
2012 /* The own bit must be the latest setting done when prepare the
2013 * descriptor and then barrier is needed to make sure that
2014 * all is coherent before granting the DMA engine.
2015 */
2016 smp_wmb();
2017 }
2018
Beniamino Galvani38979572015-01-21 19:07:27 +01002019 netdev_sent_queue(dev, skb->len);
Richard Cochran52f64fa2011-06-19 03:31:43 +00002020 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2021
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002022 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002023 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002024
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002025dma_map_err:
Fabrice Gasnier758a0ab2014-11-04 17:08:06 +01002026 spin_unlock(&priv->tx_lock);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002027 dev_err(priv->device, "Tx dma map failed\n");
2028 dev_kfree_skb(skb);
2029 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002030 return NETDEV_TX_OK;
2031}
2032
Vince Bridgersb9381982014-01-14 13:42:05 -06002033static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2034{
2035 struct ethhdr *ehdr;
2036 u16 vlanid;
2037
2038 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2039 NETIF_F_HW_VLAN_CTAG_RX &&
2040 !__vlan_get_tag(skb, &vlanid)) {
2041 /* pop the vlan tag */
2042 ehdr = (struct ethhdr *)skb->data;
2043 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2044 skb_pull(skb, VLAN_HLEN);
2045 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2046 }
2047}
2048
2049
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002050static inline int stmmac_rx_threshold_count(struct stmmac_priv *priv)
2051{
2052 if (priv->rx_zeroc_thresh < STMMAC_RX_THRESH)
2053 return 0;
2054
2055 return 1;
2056}
2057
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002058/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002059 * stmmac_rx_refill - refill used skb preallocated buffers
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002060 * @priv: driver private structure
2061 * Description : this is to reallocate the skb for the reception process
2062 * that is based on zero-copy.
2063 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002064static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2065{
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002066 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002067 unsigned int entry = priv->dirty_rx;
2068 int dirty = stmmac_rx_dirty(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002069
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002070 while (dirty-- > 0) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002071 struct dma_desc *p;
2072
2073 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002074 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002075 else
2076 p = priv->dma_rx + entry;
2077
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002078 if (likely(priv->rx_skbuff[entry] == NULL)) {
2079 struct sk_buff *skb;
2080
Eric Dumazetacb600d2012-10-05 06:23:55 +00002081 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002082 if (unlikely(!skb)) {
2083 /* so for a while no zero-copy! */
2084 priv->rx_zeroc_thresh = STMMAC_RX_THRESH;
2085 if (unlikely(net_ratelimit()))
2086 dev_err(priv->device,
2087 "fail to alloc skb entry %d\n",
2088 entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002089 break;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002090 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002091
2092 priv->rx_skbuff[entry] = skb;
2093 priv->rx_skbuff_dma[entry] =
2094 dma_map_single(priv->device, skb->data, bfsize,
2095 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02002096 if (dma_mapping_error(priv->device,
2097 priv->rx_skbuff_dma[entry])) {
2098 dev_err(priv->device, "Rx dma map failed\n");
2099 dev_kfree_skb(skb);
2100 break;
2101 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002102 p->des2 = priv->rx_skbuff_dma[entry];
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002103
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002104 priv->hw->mode->refill_desc3(priv, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00002105
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002106 if (priv->rx_zeroc_thresh > 0)
2107 priv->rx_zeroc_thresh--;
2108
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002109 if (netif_msg_rx_status(priv))
2110 pr_debug("\trefill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002111 }
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002112
Shiraz Hashimeb0dc4b2011-07-17 20:54:08 +00002113 wmb();
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002114 priv->hw->desc->set_rx_owner(p);
Deepak Sikri8e839892012-07-08 21:14:45 +00002115 wmb();
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002116
2117 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002118 }
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002119 priv->dirty_rx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002120}
2121
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002122/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002123 * stmmac_rx - manage the receive process
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002124 * @priv: driver private structure
2125 * @limit: napi bugget.
2126 * Description : this the function called by the napi poll method.
2127 * It gets all the frames inside the ring.
2128 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002129static int stmmac_rx(struct stmmac_priv *priv, int limit)
2130{
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002131 unsigned int entry = priv->cur_rx;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002132 unsigned int next_entry;
2133 unsigned int count = 0;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002134 int coe = priv->hw->rx_csum;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002135
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002136 if (netif_msg_rx_status(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002137 void *rx_head;
2138
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002139 pr_debug("%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002140 if (priv->extend_desc)
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002141 rx_head = (void *)priv->dma_erx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002142 else
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02002143 rx_head = (void *)priv->dma_rx;
2144
2145 priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002146 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002147 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002148 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002149 struct dma_desc *p;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002150
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002151 if (priv->extend_desc)
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002152 p = (struct dma_desc *)(priv->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002153 else
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002154 p = priv->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002155
Fabrice Gasnierc1fa3212016-02-29 14:27:34 +01002156 /* read the status of the incoming frame */
2157 status = priv->hw->desc->rx_status(&priv->dev->stats,
2158 &priv->xstats, p);
2159 /* check if managed by the DMA otherwise go ahead */
2160 if (unlikely(status & dma_own))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002161 break;
2162
2163 count++;
2164
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002165 priv->cur_rx = STMMAC_GET_ENTRY(priv->cur_rx, DMA_RX_SIZE);
2166 next_entry = priv->cur_rx;
2167
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002168 if (priv->extend_desc)
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002169 prefetch(priv->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002170 else
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00002171 prefetch(priv->dma_rx + next_entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002172
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002173 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2174 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2175 &priv->xstats,
2176 priv->dma_erx +
2177 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002178 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002179 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002180 if (priv->hwts_rx_en && !priv->extend_desc) {
2181 /* DESC2 & DESC3 will be overwitten by device
2182 * with timestamp value, hence reinitialize
2183 * them in stmmac_rx_refill() function so that
2184 * device can reuse it.
2185 */
2186 priv->rx_skbuff[entry] = NULL;
2187 dma_unmap_single(priv->device,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002188 priv->rx_skbuff_dma[entry],
2189 priv->dma_buf_sz,
2190 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002191 }
2192 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002193 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002194 int frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002195
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002196 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2197
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01002198 /* check if frame_len fits the preallocated memory */
2199 if (frame_len > priv->dma_buf_sz) {
2200 priv->dev->stats.rx_length_errors++;
2201 break;
2202 }
2203
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002204 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002205 * Type frames (LLC/LLC-SNAP)
2206 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00002207 if (unlikely(status != llc_snap))
2208 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002209
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002210 if (netif_msg_rx_status(priv)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002211 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002212 p, entry, p->des2);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002213 if (frame_len > ETH_FRAME_LEN)
2214 pr_debug("\tframe size %d, COE: %d\n",
2215 frame_len, status);
2216 }
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002217
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002218 if (unlikely((frame_len < priv->rx_copybreak) ||
2219 stmmac_rx_threshold_count(priv))) {
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002220 skb = netdev_alloc_skb_ip_align(priv->dev,
2221 frame_len);
2222 if (unlikely(!skb)) {
2223 if (net_ratelimit())
2224 dev_warn(priv->device,
2225 "packet dropped\n");
2226 priv->dev->stats.rx_dropped++;
2227 break;
2228 }
2229
2230 dma_sync_single_for_cpu(priv->device,
2231 priv->rx_skbuff_dma
2232 [entry], frame_len,
2233 DMA_FROM_DEVICE);
2234 skb_copy_to_linear_data(skb,
2235 priv->
2236 rx_skbuff[entry]->data,
2237 frame_len);
2238
2239 skb_put(skb, frame_len);
2240 dma_sync_single_for_device(priv->device,
2241 priv->rx_skbuff_dma
2242 [entry], frame_len,
2243 DMA_FROM_DEVICE);
2244 } else {
2245 skb = priv->rx_skbuff[entry];
2246 if (unlikely(!skb)) {
2247 pr_err("%s: Inconsistent Rx chain\n",
2248 priv->dev->name);
2249 priv->dev->stats.rx_dropped++;
2250 break;
2251 }
2252 prefetch(skb->data - NET_IP_ALIGN);
2253 priv->rx_skbuff[entry] = NULL;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01002254 priv->rx_zeroc_thresh++;
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002255
2256 skb_put(skb, frame_len);
2257 dma_unmap_single(priv->device,
2258 priv->rx_skbuff_dma[entry],
2259 priv->dma_buf_sz,
2260 DMA_FROM_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002261 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002262
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002263 stmmac_get_rx_hwtstamp(priv, entry, skb);
2264
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002265 if (netif_msg_pktdata(priv)) {
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002266 pr_debug("frame received (%dbytes)", frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002267 print_pkt(skb->data, frame_len);
2268 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02002269
Vince Bridgersb9381982014-01-14 13:42:05 -06002270 stmmac_rx_vlan(priv->dev, skb);
2271
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002272 skb->protocol = eth_type_trans(skb, priv->dev);
2273
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002274 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002275 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002276 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002277 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002278
2279 napi_gro_receive(&priv->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002280
2281 priv->dev->stats.rx_packets++;
2282 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002283 }
2284 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002285 }
2286
2287 stmmac_rx_refill(priv);
2288
2289 priv->xstats.rx_pkt_n += count;
2290
2291 return count;
2292}
2293
2294/**
2295 * stmmac_poll - stmmac poll method (NAPI)
2296 * @napi : pointer to the napi structure.
2297 * @budget : maximum number of packets that the current CPU can receive from
2298 * all interfaces.
2299 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002300 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002301 */
2302static int stmmac_poll(struct napi_struct *napi, int budget)
2303{
2304 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2305 int work_done = 0;
2306
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002307 priv->xstats.napi_poll++;
2308 stmmac_tx_clean(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002309
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002310 work_done = stmmac_rx(priv, budget);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002311 if (work_done < budget) {
2312 napi_complete(napi);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002313 stmmac_enable_dma_irq(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002314 }
2315 return work_done;
2316}
2317
2318/**
2319 * stmmac_tx_timeout
2320 * @dev : Pointer to net device structure
2321 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00002322 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002323 * netdev structure and arrange for the device to be reset to a sane state
2324 * in order to transmit a new packet.
2325 */
2326static void stmmac_tx_timeout(struct net_device *dev)
2327{
2328 struct stmmac_priv *priv = netdev_priv(dev);
2329
2330 /* Clear Tx resources and restart transmitting again */
2331 stmmac_tx_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002332}
2333
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002334/**
Jiri Pirko01789342011-08-16 06:29:00 +00002335 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002336 * @dev : pointer to the device structure
2337 * Description:
2338 * This function is a driver entry point which gets called by the kernel
2339 * whenever multicast addresses must be enabled/disabled.
2340 * Return value:
2341 * void.
2342 */
Jiri Pirko01789342011-08-16 06:29:00 +00002343static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002344{
2345 struct stmmac_priv *priv = netdev_priv(dev);
2346
Vince Bridgers3b57de92014-07-31 15:49:17 -05002347 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002348}
2349
2350/**
2351 * stmmac_change_mtu - entry point to change MTU size for the device.
2352 * @dev : device pointer.
2353 * @new_mtu : the new MTU size for the device.
2354 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2355 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2356 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2357 * Return value:
2358 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2359 * file on failure.
2360 */
2361static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2362{
2363 struct stmmac_priv *priv = netdev_priv(dev);
2364 int max_mtu;
2365
2366 if (netif_running(dev)) {
2367 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2368 return -EBUSY;
2369 }
2370
Giuseppe CAVALLARO48febf72011-10-18 00:01:21 +00002371 if (priv->plat->enh_desc)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002372 max_mtu = JUMBO_LEN;
2373 else
Giuseppe CAVALLARO45db81e2011-10-18 01:39:55 +00002374 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002375
Vince Bridgers2618abb2014-01-20 05:39:01 -06002376 if (priv->plat->maxmtu < max_mtu)
2377 max_mtu = priv->plat->maxmtu;
2378
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002379 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2380 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2381 return -EINVAL;
2382 }
2383
Michał Mirosław5e982f32011-04-09 02:46:55 +00002384 dev->mtu = new_mtu;
2385 netdev_update_features(dev);
2386
2387 return 0;
2388}
2389
Michał Mirosławc8f44af2011-11-15 15:29:55 +00002390static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002391 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002392{
2393 struct stmmac_priv *priv = netdev_priv(dev);
2394
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002395 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00002396 features &= ~NETIF_F_RXCSUM;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002397
Michał Mirosław5e982f32011-04-09 02:46:55 +00002398 if (!priv->plat->tx_coe)
Tom Herberta1882222015-12-14 11:19:43 -08002399 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosław5e982f32011-04-09 02:46:55 +00002400
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002401 /* Some GMAC devices have a bugged Jumbo frame support that
2402 * needs to have the Tx COE disabled for oversized frames
2403 * (due to limited buffer sizes). In this case we disable
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002404 * the TX csum insertionin the TDES and not use SF.
2405 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00002406 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08002407 features &= ~NETIF_F_CSUM_MASK;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00002408
Michał Mirosław5e982f32011-04-09 02:46:55 +00002409 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002410}
2411
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002412static int stmmac_set_features(struct net_device *netdev,
2413 netdev_features_t features)
2414{
2415 struct stmmac_priv *priv = netdev_priv(netdev);
2416
2417 /* Keep the COE Type in case of csum is supporting */
2418 if (features & NETIF_F_RXCSUM)
2419 priv->hw->rx_csum = priv->plat->rx_coe;
2420 else
2421 priv->hw->rx_csum = 0;
2422 /* No check needed because rx_coe has been set before and it will be
2423 * fixed in case of issue.
2424 */
2425 priv->hw->mac->rx_ipc(priv->hw);
2426
2427 return 0;
2428}
2429
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002430/**
2431 * stmmac_interrupt - main ISR
2432 * @irq: interrupt number.
2433 * @dev_id: to pass the net device pointer.
2434 * Description: this is the main driver interrupt service routine.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002435 * It can call:
2436 * o DMA service routine (to manage incoming frame reception and transmission
2437 * status)
2438 * o Core interrupts to manage: remote wake-up, management counter, LPI
2439 * interrupts.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002440 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002441static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2442{
2443 struct net_device *dev = (struct net_device *)dev_id;
2444 struct stmmac_priv *priv = netdev_priv(dev);
2445
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00002446 if (priv->irq_wake)
2447 pm_wakeup_event(priv->device, 0);
2448
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002449 if (unlikely(!dev)) {
2450 pr_err("%s: invalid dev pointer\n", __func__);
2451 return IRQ_NONE;
2452 }
2453
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002454 /* To handle GMAC own interrupts */
2455 if (priv->plat->has_gmac) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002456 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002457 &priv->xstats);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002458 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002459 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002460 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002461 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00002462 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002463 priv->tx_path_in_lpi_mode = false;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002464 }
2465 }
2466
2467 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002468 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002469
2470 return IRQ_HANDLED;
2471}
2472
2473#ifdef CONFIG_NET_POLL_CONTROLLER
2474/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002475 * to allow network I/O with interrupts disabled.
2476 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002477static void stmmac_poll_controller(struct net_device *dev)
2478{
2479 disable_irq(dev->irq);
2480 stmmac_interrupt(dev->irq, dev);
2481 enable_irq(dev->irq);
2482}
2483#endif
2484
2485/**
2486 * stmmac_ioctl - Entry point for the Ioctl
2487 * @dev: Device pointer.
2488 * @rq: An IOCTL specefic structure, that can contain a pointer to
2489 * a proprietary structure used to pass information to the driver.
2490 * @cmd: IOCTL command
2491 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002492 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002493 */
2494static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2495{
2496 struct stmmac_priv *priv = netdev_priv(dev);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002497 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002498
2499 if (!netif_running(dev))
2500 return -EINVAL;
2501
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00002502 switch (cmd) {
2503 case SIOCGMIIPHY:
2504 case SIOCGMIIREG:
2505 case SIOCSMIIREG:
2506 if (!priv->phydev)
2507 return -EINVAL;
2508 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2509 break;
2510 case SIOCSHWTSTAMP:
2511 ret = stmmac_hwtstamp_ioctl(dev, rq);
2512 break;
2513 default:
2514 break;
2515 }
Richard Cochran28b04112010-07-17 08:48:55 +00002516
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002517 return ret;
2518}
2519
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002520#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002521static struct dentry *stmmac_fs_dir;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002522
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002523static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002524 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002525{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002526 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002527 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2528 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002529
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002530 for (i = 0; i < size; i++) {
2531 u64 x;
2532 if (extend_desc) {
2533 x = *(u64 *) ep;
2534 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002535 i, (unsigned int)virt_to_phys(ep),
2536 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002537 ep->basic.des2, ep->basic.des3);
2538 ep++;
2539 } else {
2540 x = *(u64 *) p;
2541 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002542 i, (unsigned int)virt_to_phys(ep),
2543 (unsigned int)x, (unsigned int)(x >> 32),
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002544 p->des2, p->des3);
2545 p++;
2546 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002547 seq_printf(seq, "\n");
2548 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002549}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002550
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002551static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2552{
2553 struct net_device *dev = seq->private;
2554 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002555
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002556 if (priv->extend_desc) {
2557 seq_printf(seq, "Extended RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002558 sysfs_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002559 seq_printf(seq, "Extended TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002560 sysfs_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002561 } else {
2562 seq_printf(seq, "RX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002563 sysfs_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0, seq);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002564 seq_printf(seq, "TX descriptor ring:\n");
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01002565 sysfs_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0, seq);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002566 }
2567
2568 return 0;
2569}
2570
2571static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2572{
2573 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2574}
2575
2576static const struct file_operations stmmac_rings_status_fops = {
2577 .owner = THIS_MODULE,
2578 .open = stmmac_sysfs_ring_open,
2579 .read = seq_read,
2580 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002581 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002582};
2583
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002584static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2585{
2586 struct net_device *dev = seq->private;
2587 struct stmmac_priv *priv = netdev_priv(dev);
2588
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002589 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002590 seq_printf(seq, "DMA HW features not supported\n");
2591 return 0;
2592 }
2593
2594 seq_printf(seq, "==============================\n");
2595 seq_printf(seq, "\tDMA HW features\n");
2596 seq_printf(seq, "==============================\n");
2597
2598 seq_printf(seq, "\t10/100 Mbps %s\n",
2599 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2600 seq_printf(seq, "\t1000 Mbps %s\n",
2601 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2602 seq_printf(seq, "\tHalf duple %s\n",
2603 (priv->dma_cap.half_duplex) ? "Y" : "N");
2604 seq_printf(seq, "\tHash Filter: %s\n",
2605 (priv->dma_cap.hash_filter) ? "Y" : "N");
2606 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2607 (priv->dma_cap.multi_addr) ? "Y" : "N");
2608 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2609 (priv->dma_cap.pcs) ? "Y" : "N");
2610 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2611 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2612 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2613 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2614 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2615 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2616 seq_printf(seq, "\tRMON module: %s\n",
2617 (priv->dma_cap.rmon) ? "Y" : "N");
2618 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2619 (priv->dma_cap.time_stamp) ? "Y" : "N");
2620 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2621 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2622 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2623 (priv->dma_cap.eee) ? "Y" : "N");
2624 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2625 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2626 (priv->dma_cap.tx_coe) ? "Y" : "N");
2627 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2628 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2629 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2630 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2631 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2632 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2633 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2634 priv->dma_cap.number_rx_channel);
2635 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2636 priv->dma_cap.number_tx_channel);
2637 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2638 (priv->dma_cap.enh_desc) ? "Y" : "N");
2639
2640 return 0;
2641}
2642
2643static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2644{
2645 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2646}
2647
2648static const struct file_operations stmmac_dma_cap_fops = {
2649 .owner = THIS_MODULE,
2650 .open = stmmac_sysfs_dma_cap_open,
2651 .read = seq_read,
2652 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00002653 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002654};
2655
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002656static int stmmac_init_fs(struct net_device *dev)
2657{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002658 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002659
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002660 /* Create per netdev entries */
2661 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
2662
2663 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
2664 pr_err("ERROR %s/%s, debugfs create directory failed\n",
2665 STMMAC_RESOURCE_NAME, dev->name);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002666
2667 return -ENOMEM;
2668 }
2669
2670 /* Entry to report DMA RX/TX rings */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002671 priv->dbgfs_rings_status =
2672 debugfs_create_file("descriptors_status", S_IRUGO,
2673 priv->dbgfs_dir, dev,
2674 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002675
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002676 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002677 pr_info("ERROR creating stmmac ring debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002678 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002679
2680 return -ENOMEM;
2681 }
2682
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002683 /* Entry to report the DMA HW features */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002684 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
2685 priv->dbgfs_dir,
2686 dev, &stmmac_dma_cap_fops);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002687
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002688 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002689 pr_info("ERROR creating stmmac MMC debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002690 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002691
2692 return -ENOMEM;
2693 }
2694
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002695 return 0;
2696}
2697
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002698static void stmmac_exit_fs(struct net_device *dev)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002699{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002700 struct stmmac_priv *priv = netdev_priv(dev);
2701
2702 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002703}
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002704#endif /* CONFIG_DEBUG_FS */
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00002705
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002706static const struct net_device_ops stmmac_netdev_ops = {
2707 .ndo_open = stmmac_open,
2708 .ndo_start_xmit = stmmac_xmit,
2709 .ndo_stop = stmmac_release,
2710 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00002711 .ndo_fix_features = stmmac_fix_features,
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002712 .ndo_set_features = stmmac_set_features,
Jiri Pirko01789342011-08-16 06:29:00 +00002713 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002714 .ndo_tx_timeout = stmmac_tx_timeout,
2715 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002716#ifdef CONFIG_NET_POLL_CONTROLLER
2717 .ndo_poll_controller = stmmac_poll_controller,
2718#endif
2719 .ndo_set_mac_address = eth_mac_addr,
2720};
2721
2722/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002723 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002724 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002725 * Description: this function is to configure the MAC device according to
2726 * some platform parameters or the HW capability register. It prepares the
2727 * driver to use either ring or chain modes and to setup either enhanced or
2728 * normal descriptors.
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002729 */
2730static int stmmac_hw_init(struct stmmac_priv *priv)
2731{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002732 struct mac_device_info *mac;
2733
2734 /* Identify the MAC HW device */
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002735 if (priv->plat->has_gmac) {
2736 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05002737 mac = dwmac1000_setup(priv->ioaddr,
2738 priv->plat->multicast_filter_bins,
Alexandre TORGUEc623d142016-04-01 11:37:27 +02002739 priv->plat->unicast_filter_entries,
2740 &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002741 } else {
Alexandre TORGUEc623d142016-04-01 11:37:27 +02002742 mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00002743 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002744 if (!mac)
2745 return -ENOMEM;
2746
2747 priv->hw = mac;
2748
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002749 /* To use the chained or ring mode */
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002750 if (chain_mode) {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002751 priv->hw->mode = &chain_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002752 pr_info(" Chain mode enabled\n");
2753 priv->mode = STMMAC_CHAIN_MODE;
2754 } else {
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01002755 priv->hw->mode = &ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00002756 pr_info(" Ring mode enabled\n");
2757 priv->mode = STMMAC_RING_MODE;
2758 }
2759
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002760 /* Get the HW capability (new GMAC newer than 3.50a) */
2761 priv->hw_cap_support = stmmac_get_hw_features(priv);
2762 if (priv->hw_cap_support) {
2763 pr_info(" DMA HW capability register supported");
2764
2765 /* We can override some gmac/dma configuration fields: e.g.
2766 * enh_desc, tx_coe (e.g. that are passed through the
2767 * platform) with the values from the HW capability
2768 * register (if supported).
2769 */
2770 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002771 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002772
Sonic Zhangdec21652015-01-22 14:55:57 +08002773 /* TXCOE doesn't work in thresh DMA mode */
2774 if (priv->plat->force_thresh_dma_mode)
2775 priv->plat->tx_coe = 0;
2776 else
2777 priv->plat->tx_coe = priv->dma_cap.tx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002778
2779 if (priv->dma_cap.rx_coe_type2)
2780 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2781 else if (priv->dma_cap.rx_coe_type1)
2782 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2783
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002784 } else
2785 pr_info(" No HW DMA feature register supported");
2786
Byungho An61369d02013-06-28 16:35:32 +09002787 /* To use alternate (extended) or normal descriptor structures */
2788 stmmac_selec_desc_mode(priv);
2789
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002790 if (priv->plat->rx_coe) {
2791 priv->hw->rx_csum = priv->plat->rx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00002792 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2793 priv->plat->rx_coe);
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002794 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002795 if (priv->plat->tx_coe)
2796 pr_info(" TX Checksum insertion supported\n");
2797
2798 if (priv->plat->pmt) {
2799 pr_info(" Wake-Up On Lan supported\n");
2800 device_set_wakeup_capable(priv->device, 1);
2801 }
2802
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002803 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002804}
2805
2806/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002807 * stmmac_dvr_probe
2808 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002809 * @plat_dat: platform data pointer
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002810 * @res: stmmac resource pointer
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002811 * Description: this is the main probe function used to
2812 * call the alloc_etherdev, allocate the priv structure.
Andy Shevchenko9afec6e2015-01-27 18:38:03 +02002813 * Return:
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002814 * returns 0 on success, otherwise errno.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002815 */
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002816int stmmac_dvr_probe(struct device *device,
2817 struct plat_stmmacenet_data *plat_dat,
2818 struct stmmac_resources *res)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002819{
2820 int ret = 0;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002821 struct net_device *ndev = NULL;
2822 struct stmmac_priv *priv;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002823
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002824 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
Joe Perches41de8d42012-01-29 13:47:52 +00002825 if (!ndev)
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002826 return -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002827
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002828 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002829
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002830 priv = netdev_priv(ndev);
2831 priv->device = device;
2832 priv->dev = ndev;
2833
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002834 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002835 priv->pause = pause;
2836 priv->plat = plat_dat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +02002837 priv->ioaddr = res->addr;
2838 priv->dev->base_addr = (unsigned long)res->addr;
2839
2840 priv->dev->irq = res->irq;
2841 priv->wol_irq = res->wol_irq;
2842 priv->lpi_irq = res->lpi_irq;
2843
2844 if (res->mac)
2845 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002846
Joachim Eastwooda7a62682015-07-17 23:48:17 +02002847 dev_set_drvdata(device, priv->dev);
Joachim Eastwood803f8fc2015-05-20 20:03:06 +02002848
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002849 /* Verify driver arguments */
2850 stmmac_verify_args();
2851
2852 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002853 * this needs to have multiple instances
2854 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002855 if ((phyaddr >= 0) && (phyaddr <= 31))
2856 priv->plat->phy_addr = phyaddr;
2857
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002858 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
2859 if (IS_ERR(priv->stmmac_clk)) {
2860 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
2861 __func__);
Kweh, Hock Leongc5bb86c2014-09-26 21:42:55 +08002862 /* If failed to obtain stmmac_clk and specific clk_csr value
2863 * is NOT passed from the platform, probe fail.
2864 */
2865 if (!priv->plat->clk_csr) {
2866 ret = PTR_ERR(priv->stmmac_clk);
2867 goto error_clk_get;
2868 } else {
2869 priv->stmmac_clk = NULL;
2870 }
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002871 }
2872 clk_prepare_enable(priv->stmmac_clk);
2873
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002874 priv->pclk = devm_clk_get(priv->device, "pclk");
2875 if (IS_ERR(priv->pclk)) {
2876 if (PTR_ERR(priv->pclk) == -EPROBE_DEFER) {
2877 ret = -EPROBE_DEFER;
2878 goto error_pclk_get;
2879 }
2880 priv->pclk = NULL;
2881 }
2882 clk_prepare_enable(priv->pclk);
2883
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002884 priv->stmmac_rst = devm_reset_control_get(priv->device,
2885 STMMAC_RESOURCE_NAME);
2886 if (IS_ERR(priv->stmmac_rst)) {
2887 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
2888 ret = -EPROBE_DEFER;
2889 goto error_hw_init;
2890 }
2891 dev_info(priv->device, "no reset control found\n");
2892 priv->stmmac_rst = NULL;
2893 }
2894 if (priv->stmmac_rst)
2895 reset_control_deassert(priv->stmmac_rst);
2896
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002897 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002898 ret = stmmac_hw_init(priv);
2899 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002900 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002901
2902 ndev->netdev_ops = &stmmac_netdev_ops;
2903
2904 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2905 NETIF_F_RXCSUM;
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002906 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2907 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002908#ifdef STMMAC_VLAN_TAG_USED
2909 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00002910 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002911#endif
2912 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2913
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002914 if (flow_ctrl)
2915 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2916
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00002917 /* Rx Watchdog is available in the COREs newer than the 3.40.
2918 * In some case, for example on bugged HW this feature
2919 * has to be disable and this can be done by passing the
2920 * riwt_off field from the platform.
2921 */
2922 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2923 priv->use_riwt = 1;
2924 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2925 }
2926
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002927 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002928
Vlad Lunguf8e96162010-11-29 22:52:52 +00002929 spin_lock_init(&priv->lock);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00002930 spin_lock_init(&priv->tx_lock);
Vlad Lunguf8e96162010-11-29 22:52:52 +00002931
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002932 ret = register_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002933 if (ret) {
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00002934 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002935 goto error_netdev_register;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002936 }
2937
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00002938 /* If a specific clk_csr value is passed from the platform
2939 * this means that the CSR Clock Range selection cannot be
2940 * changed at run-time and it is fixed. Viceversa the driver'll try to
2941 * set the MDC clock dynamically according to the csr actual
2942 * clock input.
2943 */
2944 if (!priv->plat->clk_csr)
2945 stmmac_clk_csr_set(priv);
2946 else
2947 priv->clk_csr = priv->plat->clk_csr;
2948
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002949 stmmac_check_pcs_mode(priv);
2950
Byungho An4d8f0822013-04-07 17:56:16 +00002951 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2952 priv->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002953 /* MDIO bus Registration */
2954 ret = stmmac_mdio_register(ndev);
2955 if (ret < 0) {
2956 pr_debug("%s: MDIO bus (id: %d) registration failed",
2957 __func__, priv->plat->bus_id);
2958 goto error_mdio_register;
2959 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002960 }
2961
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002962 return 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002963
Viresh Kumar6a81c262012-07-30 14:39:41 -07002964error_mdio_register:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002965 unregister_netdev(ndev);
Viresh Kumar6a81c262012-07-30 14:39:41 -07002966error_netdev_register:
2967 netif_napi_del(&priv->napi);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002968error_hw_init:
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002969 clk_disable_unprepare(priv->pclk);
2970error_pclk_get:
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08002971 clk_disable_unprepare(priv->stmmac_clk);
2972error_clk_get:
Dan Carpenter34a52f32010-12-20 21:34:56 +00002973 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002974
Joachim Eastwood15ffac72015-05-20 20:03:08 +02002975 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002976}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02002977EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002978
2979/**
2980 * stmmac_dvr_remove
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002981 * @ndev: net device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002982 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002983 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002984 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002985int stmmac_dvr_remove(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002986{
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002987 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002988
2989 pr_info("%s:\n\tremoving driver", __func__);
2990
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +00002991 priv->hw->dma->stop_rx(priv->ioaddr);
2992 priv->hw->dma->stop_tx(priv->ioaddr);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002993
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002994 stmmac_set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002995 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002996 unregister_netdev(ndev);
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08002997 if (priv->stmmac_rst)
2998 reset_control_assert(priv->stmmac_rst);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07002999 clk_disable_unprepare(priv->pclk);
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08003000 clk_disable_unprepare(priv->stmmac_clk);
Bryan O'Donoghuee7434712015-04-16 17:56:03 +01003001 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
3002 priv->pcs != STMMAC_PCS_RTBI)
3003 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003004 free_netdev(ndev);
3005
3006 return 0;
3007}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003008EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003009
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003010/**
3011 * stmmac_suspend - suspend callback
3012 * @ndev: net device pointer
3013 * Description: this is the function to suspend the device and it is called
3014 * by the platform driver to stop the network queue, release the resources,
3015 * program the PMT register (for WoL), clean and release driver resources.
3016 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003017int stmmac_suspend(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003018{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003019 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003020 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003021
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003022 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003023 return 0;
3024
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003025 if (priv->phydev)
3026 phy_stop(priv->phydev);
3027
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003028 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003029
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003030 netif_device_detach(ndev);
3031 netif_stop_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003032
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003033 napi_disable(&priv->napi);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003034
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003035 /* Stop TX/RX DMA */
3036 priv->hw->dma->stop_tx(priv->ioaddr);
3037 priv->hw->dma->stop_rx(priv->ioaddr);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003038
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003039 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003040 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003041 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003042 priv->irq_wake = 1;
3043 } else {
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003044 stmmac_set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003045 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003046 /* Disable clock in case of PWM is off */
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003047 clk_disable(priv->pclk);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003048 clk_disable(priv->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003049 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003050 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05003051
3052 priv->oldlink = 0;
3053 priv->speed = 0;
3054 priv->oldduplex = -1;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003055 return 0;
3056}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003057EXPORT_SYMBOL_GPL(stmmac_suspend);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003058
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003059/**
3060 * stmmac_resume - resume callback
3061 * @ndev: net device pointer
3062 * Description: when resume this function is invoked to setup the DMA and CORE
3063 * in a usable state.
3064 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00003065int stmmac_resume(struct net_device *ndev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003066{
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003067 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003068 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003069
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003070 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003071 return 0;
3072
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003073 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaroc4433be2010-09-06 05:02:11 +02003074
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003075 /* Power Down bit, into the PM register, is cleared
3076 * automatically as soon as a magic packet or a Wake-up frame
3077 * is received. Anyway, it's better to manually clear
3078 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003079 * from another devices (e.g. serial console).
3080 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003081 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003082 priv->hw->mac->pmt(priv->hw, 0);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003083 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003084 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00003085 pinctrl_pm_select_default_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00003086 /* enable the clk prevously disabled */
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003087 clk_enable(priv->stmmac_clk);
Andrew Bresticker5f9755d2015-04-07 13:38:45 -07003088 clk_enable(priv->pclk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00003089 /* reset the phy so that it's ready */
3090 if (priv->mii)
3091 stmmac_mdio_reset(priv->mii);
3092 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003093
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003094 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003095
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01003096 priv->cur_rx = 0;
3097 priv->dirty_rx = 0;
3098 priv->dirty_tx = 0;
3099 priv->cur_tx = 0;
3100 stmmac_clear_descriptors(priv);
3101
Huacai Chenfe1319292014-12-19 22:38:18 +08003102 stmmac_hw_setup(ndev, false);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01003103 stmmac_init_tx_coalesce(priv);
Giuseppe CAVALLAROac316c72015-11-26 08:35:41 +01003104 stmmac_set_rx_mode(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003105
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003106 napi_enable(&priv->napi);
3107
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00003108 netif_start_queue(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003109
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00003110 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00003111
3112 if (priv->phydev)
3113 phy_start(priv->phydev);
3114
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003115 return 0;
3116}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02003117EXPORT_SYMBOL_GPL(stmmac_resume);
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00003118
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003119#ifndef MODULE
3120static int __init stmmac_cmdline_opt(char *str)
3121{
3122 char *opt;
3123
3124 if (!str || !*str)
3125 return -EINVAL;
3126 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003127 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003128 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003129 goto err;
3130 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003131 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003132 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003133 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003134 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003135 goto err;
3136 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003137 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003138 goto err;
3139 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003140 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003141 goto err;
3142 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003143 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003144 goto err;
3145 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00003146 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003147 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00003148 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003149 if (kstrtoint(opt + 10, 0, &eee_timer))
3150 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003151 } else if (!strncmp(opt, "chain_mode:", 11)) {
3152 if (kstrtoint(opt + 11, 0, &chain_mode))
3153 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003154 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003155 }
3156 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00003157
3158err:
3159 pr_err("%s: ERROR broken module parameter conversion", __func__);
3160 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003161}
3162
3163__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003164#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003165
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07003166static int __init stmmac_init(void)
3167{
3168#ifdef CONFIG_DEBUG_FS
3169 /* Create debugfs main directory if it doesn't exist yet */
3170 if (!stmmac_fs_dir) {
3171 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
3172
3173 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
3174 pr_err("ERROR %s, debugfs create directory failed\n",
3175 STMMAC_RESOURCE_NAME);
3176
3177 return -ENOMEM;
3178 }
3179 }
3180#endif
3181
3182 return 0;
3183}
3184
3185static void __exit stmmac_exit(void)
3186{
3187#ifdef CONFIG_DEBUG_FS
3188 debugfs_remove_recursive(stmmac_fs_dir);
3189#endif
3190}
3191
3192module_init(stmmac_init)
3193module_exit(stmmac_exit)
3194
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05003195MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3196MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3197MODULE_LICENSE("GPL");