blob: b75ecf3d19fe855bf777a55e18db6fb92ab526b0 [file] [log] [blame]
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001/*******************************************************************************
2 This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers.
3 ST Ethernet IPs are built around a Synopsys IP Core.
4
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00005 Copyright(C) 2007-2011 STMicroelectronics Ltd
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07006
7 This program is free software; you can redistribute it and/or modify it
8 under the terms and conditions of the GNU General Public License,
9 version 2, as published by the Free Software Foundation.
10
11 This program is distributed in the hope it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070016 The full GNU General Public License is included in this distribution in
17 the file called "COPYING".
18
19 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
20
21 Documentation available at:
22 http://www.stlinux.com
23 Support available at:
24 https://bugzilla.stlinux.com/
25*******************************************************************************/
26
Viresh Kumar6a81c262012-07-30 14:39:41 -070027#include <linux/clk.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070028#include <linux/kernel.h>
29#include <linux/interrupt.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070030#include <linux/ip.h>
31#include <linux/tcp.h>
32#include <linux/skbuff.h>
33#include <linux/ethtool.h>
34#include <linux/if_ether.h>
35#include <linux/crc32.h>
36#include <linux/mii.h>
Jiri Pirko01789342011-08-16 06:29:00 +000037#include <linux/if.h>
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070038#include <linux/if_vlan.h>
39#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040041#include <linux/prefetch.h>
Srinivas Kandagatladb88f102014-01-16 10:52:52 +000042#include <linux/pinctrl/consumer.h>
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +010043#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +000044#include <linux/debugfs.h>
45#include <linux/seq_file.h>
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +010046#endif /* CONFIG_DEBUG_FS */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +000047#include <linux/net_tstamp.h>
48#include "stmmac_ptp.h"
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +000049#include "stmmac.h"
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +080050#include <linux/reset.h>
Mathieu Olivari5790cf32015-05-27 11:02:47 -070051#include <linux/of_mdio.h>
Phil Reid19d857c2015-12-14 11:32:01 +080052#include "dwmac1000.h"
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070053
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070054#define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
Alexandre TORGUEf748be52016-04-01 11:37:34 +020055#define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070056
57/* Module parameters */
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000058#define TX_TIMEO 5000
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070059static int watchdog = TX_TIMEO;
Joe Perchesd3757ba2018-03-23 16:34:44 -070060module_param(watchdog, int, 0644);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000061MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070062
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000063static int debug = -1;
Joe Perchesd3757ba2018-03-23 16:34:44 -070064module_param(debug, int, 0644);
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +000065MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070066
stephen hemminger47d1f712013-12-30 10:38:57 -080067static int phyaddr = -1;
Joe Perchesd3757ba2018-03-23 16:34:44 -070068module_param(phyaddr, int, 0444);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070069MODULE_PARM_DESC(phyaddr, "Physical device address");
70
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +010071#define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +010072#define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070073
74static int flow_ctrl = FLOW_OFF;
Joe Perchesd3757ba2018-03-23 16:34:44 -070075module_param(flow_ctrl, int, 0644);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070076MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
77
78static int pause = PAUSE_TIME;
Joe Perchesd3757ba2018-03-23 16:34:44 -070079module_param(pause, int, 0644);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070080MODULE_PARM_DESC(pause, "Flow Control Pause Time");
81
82#define TC_DEFAULT 64
83static int tc = TC_DEFAULT;
Joe Perchesd3757ba2018-03-23 16:34:44 -070084module_param(tc, int, 0644);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070085MODULE_PARM_DESC(tc, "DMA threshold control value");
86
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +010087#define DEFAULT_BUFSIZE 1536
88static int buf_sz = DEFAULT_BUFSIZE;
Joe Perchesd3757ba2018-03-23 16:34:44 -070089module_param(buf_sz, int, 0644);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070090MODULE_PARM_DESC(buf_sz, "DMA buffer size");
91
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +010092#define STMMAC_RX_COPYBREAK 256
93
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -070094static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
95 NETIF_MSG_LINK | NETIF_MSG_IFUP |
96 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
97
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +000098#define STMMAC_DEFAULT_LPI_TIMER 1000
99static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Joe Perchesd3757ba2018-03-23 16:34:44 -0700100module_param(eee_timer, int, 0644);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000101MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200102#define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000103
Pavel Machek22d3efe2016-11-28 12:55:59 +0100104/* By default the driver will use the ring mode to manage tx and rx descriptors,
105 * but allow user to force to use the chain instead of the ring
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000106 */
107static unsigned int chain_mode;
Joe Perchesd3757ba2018-03-23 16:34:44 -0700108module_param(chain_mode, int, 0444);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +0000109MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
110
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700111static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700112
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +0100113#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000114static int stmmac_init_fs(struct net_device *dev);
Mathieu Olivari466c5ac2015-05-22 19:03:29 -0700115static void stmmac_exit_fs(struct net_device *dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +0000116#endif
117
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +0000118#define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
119
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700120/**
121 * stmmac_verify_args - verify the driver parameters.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100122 * Description: it checks the driver parameters and set a default in case of
123 * errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700124 */
125static void stmmac_verify_args(void)
126{
127 if (unlikely(watchdog < 0))
128 watchdog = TX_TIMEO;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +0100129 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
130 buf_sz = DEFAULT_BUFSIZE;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700131 if (unlikely(flow_ctrl > 1))
132 flow_ctrl = FLOW_AUTO;
133 else if (likely(flow_ctrl < 0))
134 flow_ctrl = FLOW_OFF;
135 if (unlikely((pause < 0) || (pause > 0xffff)))
136 pause = PAUSE_TIME;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000137 if (eee_timer < 0)
138 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700139}
140
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000141/**
Joao Pintoc22a3f42017-04-06 09:49:11 +0100142 * stmmac_disable_all_queues - Disable all queues
143 * @priv: driver private structure
144 */
145static void stmmac_disable_all_queues(struct stmmac_priv *priv)
146{
147 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
148 u32 queue;
149
150 for (queue = 0; queue < rx_queues_cnt; queue++) {
151 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
152
153 napi_disable(&rx_q->napi);
154 }
155}
156
157/**
158 * stmmac_enable_all_queues - Enable all queues
159 * @priv: driver private structure
160 */
161static void stmmac_enable_all_queues(struct stmmac_priv *priv)
162{
163 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
164 u32 queue;
165
166 for (queue = 0; queue < rx_queues_cnt; queue++) {
167 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
168
169 napi_enable(&rx_q->napi);
170 }
171}
172
173/**
174 * stmmac_stop_all_queues - Stop all queues
175 * @priv: driver private structure
176 */
177static void stmmac_stop_all_queues(struct stmmac_priv *priv)
178{
179 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
180 u32 queue;
181
182 for (queue = 0; queue < tx_queues_cnt; queue++)
183 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
184}
185
186/**
187 * stmmac_start_all_queues - Start all queues
188 * @priv: driver private structure
189 */
190static void stmmac_start_all_queues(struct stmmac_priv *priv)
191{
192 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
193 u32 queue;
194
195 for (queue = 0; queue < tx_queues_cnt; queue++)
196 netif_tx_start_queue(netdev_get_tx_queue(priv->dev, queue));
197}
198
Jose Abreu34877a12018-03-29 10:40:18 +0100199static void stmmac_service_event_schedule(struct stmmac_priv *priv)
200{
201 if (!test_bit(STMMAC_DOWN, &priv->state) &&
202 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
203 queue_work(priv->wq, &priv->service_task);
204}
205
206static void stmmac_global_err(struct stmmac_priv *priv)
207{
208 netif_carrier_off(priv->dev);
209 set_bit(STMMAC_RESET_REQUESTED, &priv->state);
210 stmmac_service_event_schedule(priv);
211}
212
Joao Pintoc22a3f42017-04-06 09:49:11 +0100213/**
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000214 * stmmac_clk_csr_set - dynamically set the MDC clock
215 * @priv: driver private structure
216 * Description: this is to dynamically set the MDC clock according to the csr
217 * clock input.
218 * Note:
219 * If a specific clk_csr value is passed from the platform
220 * this means that the CSR Clock Range selection cannot be
221 * changed at run-time and it is fixed (as reported in the driver
222 * documentation). Viceversa the driver will try to set the MDC
223 * clock dynamically according to the actual clock input.
224 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000225static void stmmac_clk_csr_set(struct stmmac_priv *priv)
226{
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000227 u32 clk_rate;
228
jpintof573c0b2017-01-09 12:35:09 +0000229 clk_rate = clk_get_rate(priv->plat->stmmac_clk);
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000230
231 /* Platform provided default clk_csr would be assumed valid
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000232 * for all other cases except for the below mentioned ones.
233 * For values higher than the IEEE 802.3 specified frequency
234 * we can not estimate the proper divider as it is not known
235 * the frequency of clk_csr_i. So we do not change the default
236 * divider.
237 */
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000238 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
239 if (clk_rate < CSR_F_35M)
240 priv->clk_csr = STMMAC_CSR_20_35M;
241 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
242 priv->clk_csr = STMMAC_CSR_35_60M;
243 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
244 priv->clk_csr = STMMAC_CSR_60_100M;
245 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
246 priv->clk_csr = STMMAC_CSR_100_150M;
247 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
248 priv->clk_csr = STMMAC_CSR_150_250M;
Phil Reid19d857c2015-12-14 11:32:01 +0800249 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000250 priv->clk_csr = STMMAC_CSR_250_300M;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000251 }
LABBE Corentin9f93ac82017-05-31 09:18:36 +0200252
253 if (priv->plat->has_sun8i) {
254 if (clk_rate > 160000000)
255 priv->clk_csr = 0x03;
256 else if (clk_rate > 80000000)
257 priv->clk_csr = 0x02;
258 else if (clk_rate > 40000000)
259 priv->clk_csr = 0x01;
260 else
261 priv->clk_csr = 0;
262 }
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +0000263}
264
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700265static void print_pkt(unsigned char *buf, int len)
266{
Andy Shevchenko424c4f72014-11-07 16:53:12 +0200267 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
268 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700269}
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700270
Joao Pintoce736782017-04-06 09:49:10 +0100271static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700272{
Joao Pintoce736782017-04-06 09:49:10 +0100273 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
LABBE Corentina6a3e022017-02-08 09:31:21 +0100274 u32 avail;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100275
Joao Pintoce736782017-04-06 09:49:10 +0100276 if (tx_q->dirty_tx > tx_q->cur_tx)
277 avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100278 else
Joao Pintoce736782017-04-06 09:49:10 +0100279 avail = DMA_TX_SIZE - tx_q->cur_tx + tx_q->dirty_tx - 1;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100280
281 return avail;
282}
283
Joao Pinto54139cf2017-04-06 09:49:09 +0100284/**
285 * stmmac_rx_dirty - Get RX queue dirty
286 * @priv: driver private structure
287 * @queue: RX queue index
288 */
289static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100290{
Joao Pinto54139cf2017-04-06 09:49:09 +0100291 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
LABBE Corentina6a3e022017-02-08 09:31:21 +0100292 u32 dirty;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100293
Joao Pinto54139cf2017-04-06 09:49:09 +0100294 if (rx_q->dirty_rx <= rx_q->cur_rx)
295 dirty = rx_q->cur_rx - rx_q->dirty_rx;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100296 else
Joao Pinto54139cf2017-04-06 09:49:09 +0100297 dirty = DMA_RX_SIZE - rx_q->dirty_rx + rx_q->cur_rx;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +0100298
299 return dirty;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700300}
301
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000302/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100303 * stmmac_hw_fix_mac_speed - callback for speed selection
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000304 * @priv: driver private structure
LABBE Corentin8d45e422017-02-08 09:31:08 +0100305 * Description: on some platforms (e.g. ST), some HW system configuration
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000306 * registers have to be set according to the link speed negotiated.
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000307 */
308static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
309{
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200310 struct net_device *ndev = priv->dev;
311 struct phy_device *phydev = ndev->phydev;
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000312
313 if (likely(priv->plat->fix_mac_speed))
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000314 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
Giuseppe CAVALLARO9dfeb4d2010-11-24 02:37:58 +0000315}
316
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000317/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100318 * stmmac_enable_eee_mode - check and enter in LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000319 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100320 * Description: this function is to verify and enter in LPI mode in case of
321 * EEE.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000322 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000323static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
324{
Joao Pintoce736782017-04-06 09:49:10 +0100325 u32 tx_cnt = priv->plat->tx_queues_to_use;
326 u32 queue;
327
328 /* check if all TX queues have the work finished */
329 for (queue = 0; queue < tx_cnt; queue++) {
330 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
331
332 if (tx_q->dirty_tx != tx_q->cur_tx)
333 return; /* still unfinished work */
334 }
335
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000336 /* Check and enter in LPI mode */
Joao Pintoce736782017-04-06 09:49:10 +0100337 if (!priv->tx_path_in_lpi_mode)
jpintob4b7b772017-01-09 12:35:08 +0000338 priv->hw->mac->set_eee_mode(priv->hw,
339 priv->plat->en_tx_lpi_clockgating);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000340}
341
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000342/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100343 * stmmac_disable_eee_mode - disable and exit from LPI mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000344 * @priv: driver private structure
345 * Description: this function is to exit and disable EEE in case of
346 * LPI state is true. This is called by the xmit.
347 */
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000348void stmmac_disable_eee_mode(struct stmmac_priv *priv)
349{
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500350 priv->hw->mac->reset_eee_mode(priv->hw);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000351 del_timer_sync(&priv->eee_ctrl_timer);
352 priv->tx_path_in_lpi_mode = false;
353}
354
355/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100356 * stmmac_eee_ctrl_timer - EEE TX SW timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000357 * @arg : data hook
358 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000359 * if there is no data transfer and if we are not in LPI state,
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000360 * then MAC Transmitter can be moved to LPI state.
361 */
Kees Cooke99e88a2017-10-16 14:43:17 -0700362static void stmmac_eee_ctrl_timer(struct timer_list *t)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000363{
Kees Cooke99e88a2017-10-16 14:43:17 -0700364 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000365
366 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200367 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000368}
369
370/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100371 * stmmac_eee_init - init EEE
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000372 * @priv: driver private structure
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000373 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100374 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
375 * can also manage EEE, this function enable the LPI state and start related
376 * timer.
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000377 */
378bool stmmac_eee_init(struct stmmac_priv *priv)
379{
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200380 struct net_device *ndev = priv->dev;
Jerome Brunet879626e2018-01-03 16:46:29 +0100381 int interface = priv->plat->interface;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100382 unsigned long flags;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000383 bool ret = false;
384
Jerome Brunet879626e2018-01-03 16:46:29 +0100385 if ((interface != PHY_INTERFACE_MODE_MII) &&
386 (interface != PHY_INTERFACE_MODE_GMII) &&
387 !phy_interface_mode_is_rgmii(interface))
388 goto out;
389
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200390 /* Using PCS we cannot dial with the phy registers at this stage
391 * so we do not support extra feature like EEE.
392 */
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200393 if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
394 (priv->hw->pcs == STMMAC_PCS_TBI) ||
395 (priv->hw->pcs == STMMAC_PCS_RTBI))
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200396 goto out;
397
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000398 /* MAC core supports the EEE feature. */
399 if (priv->dma_cap.eee) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100400 int tx_lpi_timer = priv->tx_lpi_timer;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000401
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100402 /* Check if the PHY supports EEE */
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200403 if (phy_init_eee(ndev->phydev, 1)) {
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100404 /* To manage at run-time if the EEE cannot be supported
405 * anymore (for example because the lp caps have been
406 * changed).
407 * In that case the driver disable own timers.
408 */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100409 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100410 if (priv->eee_active) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100411 netdev_dbg(priv->dev, "disable EEE\n");
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100412 del_timer_sync(&priv->eee_ctrl_timer);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500413 priv->hw->mac->set_eee_timer(priv->hw, 0,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100414 tx_lpi_timer);
415 }
416 priv->eee_active = 0;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100417 spin_unlock_irqrestore(&priv->lock, flags);
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100418 goto out;
419 }
420 /* Activate the EEE and start timers */
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100421 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200422 if (!priv->eee_active) {
423 priv->eee_active = 1;
Kees Cooke99e88a2017-10-16 14:43:17 -0700424 timer_setup(&priv->eee_ctrl_timer,
425 stmmac_eee_ctrl_timer, 0);
Vaishali Thakkarccb36da2015-02-28 00:12:34 +0530426 mod_timer(&priv->eee_ctrl_timer,
427 STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000428
Vince Bridgers7ed24bb2014-07-31 15:49:13 -0500429 priv->hw->mac->set_eee_timer(priv->hw,
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +0200430 STMMAC_DEFAULT_LIT_LS,
Giuseppe CAVALLARO83bf79b2014-03-10 13:40:31 +0100431 tx_lpi_timer);
Giuseppe CAVALLARO71965352014-08-28 08:11:44 +0200432 }
433 /* Set HW EEE according to the speed */
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200434 priv->hw->mac->set_eee_pls(priv->hw, ndev->phydev->link);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000435
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000436 ret = true;
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100437 spin_unlock_irqrestore(&priv->lock, flags);
438
LABBE Corentin38ddc592016-11-16 20:09:39 +0100439 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000440 }
441out:
442 return ret;
443}
444
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100445/* stmmac_get_tx_hwtstamp - get HW TX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000446 * @priv: driver private structure
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100447 * @p : descriptor pointer
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000448 * @skb : the socket buffer
449 * Description :
450 * This function will read timestamp from the descriptor & pass it to stack.
451 * and also perform some sanity checks.
452 */
453static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100454 struct dma_desc *p, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000455{
456 struct skb_shared_hwtstamps shhwtstamp;
457 u64 ns;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000458
459 if (!priv->hwts_tx_en)
460 return;
461
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000462 /* exit if skb doesn't support hw tstamp */
damuzi00075e43642014-01-17 23:47:59 +0800463 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000464 return;
465
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000466 /* check tx tstamp status */
Mario Molitor33d4c482017-06-08 23:03:09 +0200467 if (priv->hw->desc->get_tx_timestamp_status(p)) {
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100468 /* get the valid tstamp */
469 ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000470
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100471 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
472 shhwtstamp.hwtstamp = ns_to_ktime(ns);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000473
Mario Molitor33d4c482017-06-08 23:03:09 +0200474 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100475 /* pass tstamp to stack */
476 skb_tstamp_tx(skb, &shhwtstamp);
477 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000478
479 return;
480}
481
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100482/* stmmac_get_rx_hwtstamp - get HW RX timestamps
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000483 * @priv: driver private structure
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100484 * @p : descriptor pointer
485 * @np : next descriptor pointer
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000486 * @skb : the socket buffer
487 * Description :
488 * This function will read received packet's timestamp from the descriptor
489 * and pass it to stack. It also perform some sanity checks.
490 */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100491static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
492 struct dma_desc *np, struct sk_buff *skb)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000493{
494 struct skb_shared_hwtstamps *shhwtstamp = NULL;
Jose Abreu98870942017-10-20 14:37:35 +0100495 struct dma_desc *desc = p;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000496 u64 ns;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000497
498 if (!priv->hwts_rx_en)
499 return;
Jose Abreu98870942017-10-20 14:37:35 +0100500 /* For GMAC4, the valid timestamp is from CTX next desc. */
501 if (priv->plat->has_gmac4)
502 desc = np;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000503
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100504 /* Check if timestamp is available */
Fredrik Hallenberga1762452017-12-18 23:34:00 +0100505 if (priv->hw->desc->get_rx_timestamp_status(p, np, priv->adv_ts)) {
Jose Abreu98870942017-10-20 14:37:35 +0100506 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
Mario Molitor33d4c482017-06-08 23:03:09 +0200507 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100508 shhwtstamp = skb_hwtstamps(skb);
509 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
510 shhwtstamp->hwtstamp = ns_to_ktime(ns);
511 } else {
Mario Molitor33d4c482017-06-08 23:03:09 +0200512 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100513 }
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000514}
515
516/**
517 * stmmac_hwtstamp_ioctl - control hardware timestamping.
518 * @dev: device pointer.
LABBE Corentin8d45e422017-02-08 09:31:08 +0100519 * @ifr: An IOCTL specific structure, that can contain a pointer to
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000520 * a proprietary structure used to pass information to the driver.
521 * Description:
522 * This function configures the MAC to enable/disable both outgoing(TX)
523 * and incoming(RX) packets time stamping based on user input.
524 * Return Value:
525 * 0 on success and an appropriate -ve integer on failure.
526 */
527static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
528{
529 struct stmmac_priv *priv = netdev_priv(dev);
530 struct hwtstamp_config config;
Arnd Bergmann0a624152015-09-30 13:26:32 +0200531 struct timespec64 now;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000532 u64 temp = 0;
533 u32 ptp_v2 = 0;
534 u32 tstamp_all = 0;
535 u32 ptp_over_ipv4_udp = 0;
536 u32 ptp_over_ipv6_udp = 0;
537 u32 ptp_over_ethernet = 0;
538 u32 snap_type_sel = 0;
539 u32 ts_master_en = 0;
540 u32 ts_event_en = 0;
541 u32 value = 0;
Phil Reid19d857c2015-12-14 11:32:01 +0800542 u32 sec_inc;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000543
544 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
545 netdev_alert(priv->dev, "No support for HW time stamping\n");
546 priv->hwts_tx_en = 0;
547 priv->hwts_rx_en = 0;
548
549 return -EOPNOTSUPP;
550 }
551
552 if (copy_from_user(&config, ifr->ifr_data,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000553 sizeof(struct hwtstamp_config)))
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000554 return -EFAULT;
555
LABBE Corentin38ddc592016-11-16 20:09:39 +0100556 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
557 __func__, config.flags, config.tx_type, config.rx_filter);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000558
559 /* reserved for future extensions */
560 if (config.flags)
561 return -EINVAL;
562
Ben Hutchings5f3da322013-11-14 00:43:41 +0000563 if (config.tx_type != HWTSTAMP_TX_OFF &&
564 config.tx_type != HWTSTAMP_TX_ON)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000565 return -ERANGE;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000566
567 if (priv->adv_ts) {
568 switch (config.rx_filter) {
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000569 case HWTSTAMP_FILTER_NONE:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000570 /* time stamp no incoming packet at all */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000571 config.rx_filter = HWTSTAMP_FILTER_NONE;
572 break;
573
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000574 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000575 /* PTP v1, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000576 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
577 /* take time stamp for all event messages */
Mario Molitorfd6720a2017-06-08 22:41:02 +0200578 if (priv->plat->has_gmac4)
579 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
580 else
581 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000582
583 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
584 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
585 break;
586
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000587 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000588 /* PTP v1, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000589 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
590 /* take time stamp for SYNC messages only */
591 ts_event_en = PTP_TCR_TSEVNTENA;
592
593 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
594 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
595 break;
596
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000597 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000598 /* PTP v1, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000599 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
600 /* take time stamp for Delay_Req messages only */
601 ts_master_en = PTP_TCR_TSMSTRENA;
602 ts_event_en = PTP_TCR_TSEVNTENA;
603
604 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
605 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
606 break;
607
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000608 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000609 /* PTP v2, UDP, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000610 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
611 ptp_v2 = PTP_TCR_TSVER2ENA;
612 /* take time stamp for all event messages */
Mario Molitorfd6720a2017-06-08 22:41:02 +0200613 if (priv->plat->has_gmac4)
614 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
615 else
616 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000617
618 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
619 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
620 break;
621
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000622 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000623 /* PTP v2, UDP, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000624 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
625 ptp_v2 = PTP_TCR_TSVER2ENA;
626 /* take time stamp for SYNC messages only */
627 ts_event_en = PTP_TCR_TSEVNTENA;
628
629 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
630 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
631 break;
632
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000633 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000634 /* PTP v2, UDP, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000635 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
636 ptp_v2 = PTP_TCR_TSVER2ENA;
637 /* take time stamp for Delay_Req messages only */
638 ts_master_en = PTP_TCR_TSMSTRENA;
639 ts_event_en = PTP_TCR_TSEVNTENA;
640
641 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
642 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
643 break;
644
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000645 case HWTSTAMP_FILTER_PTP_V2_EVENT:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000646 /* PTP v2/802.AS1 any layer, any kind of event packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000647 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
648 ptp_v2 = PTP_TCR_TSVER2ENA;
649 /* take time stamp for all event messages */
Mario Molitorfd6720a2017-06-08 22:41:02 +0200650 if (priv->plat->has_gmac4)
651 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
652 else
653 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000654
655 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
656 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
657 ptp_over_ethernet = PTP_TCR_TSIPENA;
658 break;
659
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000660 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000661 /* PTP v2/802.AS1, any layer, Sync packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000662 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
663 ptp_v2 = PTP_TCR_TSVER2ENA;
664 /* take time stamp for SYNC messages only */
665 ts_event_en = PTP_TCR_TSEVNTENA;
666
667 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
668 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
669 ptp_over_ethernet = PTP_TCR_TSIPENA;
670 break;
671
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000672 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000673 /* PTP v2/802.AS1, any layer, Delay_req packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000674 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
675 ptp_v2 = PTP_TCR_TSVER2ENA;
676 /* take time stamp for Delay_Req messages only */
677 ts_master_en = PTP_TCR_TSMSTRENA;
678 ts_event_en = PTP_TCR_TSEVNTENA;
679
680 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
681 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
682 ptp_over_ethernet = PTP_TCR_TSIPENA;
683 break;
684
Miroslav Lichvare3412572017-05-19 17:52:36 +0200685 case HWTSTAMP_FILTER_NTP_ALL:
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000686 case HWTSTAMP_FILTER_ALL:
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000687 /* time stamp any incoming packet */
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000688 config.rx_filter = HWTSTAMP_FILTER_ALL;
689 tstamp_all = PTP_TCR_TSENALL;
690 break;
691
692 default:
693 return -ERANGE;
694 }
695 } else {
696 switch (config.rx_filter) {
697 case HWTSTAMP_FILTER_NONE:
698 config.rx_filter = HWTSTAMP_FILTER_NONE;
699 break;
700 default:
701 /* PTP v1, UDP, any kind of event packet */
702 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
703 break;
704 }
705 }
706 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
Ben Hutchings5f3da322013-11-14 00:43:41 +0000707 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000708
709 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100710 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, 0);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000711 else {
712 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +0000713 tstamp_all | ptp_v2 | ptp_over_ethernet |
714 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
715 ts_master_en | snap_type_sel);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100716 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, value);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000717
718 /* program Sub Second Increment reg */
Phil Reid19d857c2015-12-14 11:32:01 +0800719 sec_inc = priv->hw->ptp->config_sub_second_increment(
jpintof573c0b2017-01-09 12:35:09 +0000720 priv->ptpaddr, priv->plat->clk_ptp_rate,
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100721 priv->plat->has_gmac4);
Phil Reid19d857c2015-12-14 11:32:01 +0800722 temp = div_u64(1000000000ULL, sec_inc);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000723
724 /* calculate default added value:
725 * formula is :
726 * addend = (2^32)/freq_div_ratio;
Phil Reid19d857c2015-12-14 11:32:01 +0800727 * where, freq_div_ratio = 1e9ns/sec_inc
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000728 */
Phil Reid19d857c2015-12-14 11:32:01 +0800729 temp = (u64)(temp << 32);
jpintof573c0b2017-01-09 12:35:09 +0000730 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100731 priv->hw->ptp->config_addend(priv->ptpaddr,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000732 priv->default_addend);
733
734 /* initialize system time */
Arnd Bergmann0a624152015-09-30 13:26:32 +0200735 ktime_get_real_ts64(&now);
736
737 /* lower 32 bits of tv_sec are safe until y2106 */
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +0100738 priv->hw->ptp->init_systime(priv->ptpaddr, (u32)now.tv_sec,
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000739 now.tv_nsec);
740 }
741
742 return copy_to_user(ifr->ifr_data, &config,
743 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
744}
745
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000746/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100747 * stmmac_init_ptp - init PTP
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000748 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100749 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000750 * This is done by looking at the HW cap. register.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100751 * This function also registers the ptp driver.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000752 */
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000753static int stmmac_init_ptp(struct stmmac_priv *priv)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000754{
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000755 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
756 return -EOPNOTSUPP;
757
Vince Bridgers7cd01392013-12-20 11:19:34 -0600758 priv->adv_ts = 0;
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200759 /* Check if adv_ts can be enabled for dwmac 4.x core */
760 if (priv->plat->has_gmac4 && priv->dma_cap.atime_stamp)
761 priv->adv_ts = 1;
762 /* Dwmac 3.x core with extend_desc can support adv_ts */
763 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
Vince Bridgers7cd01392013-12-20 11:19:34 -0600764 priv->adv_ts = 1;
765
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200766 if (priv->dma_cap.time_stamp)
767 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
Vince Bridgers7cd01392013-12-20 11:19:34 -0600768
Giuseppe CAVALLARObe9b3172016-10-12 15:42:03 +0200769 if (priv->adv_ts)
770 netdev_info(priv->dev,
771 "IEEE 1588-2008 Advanced Timestamp supported\n");
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000772
773 priv->hw->ptp = &stmmac_ptp;
774 priv->hwts_tx_en = 0;
775 priv->hwts_rx_en = 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000776
Giuseppe CAVALLAROc30a70d2016-10-19 09:06:41 +0200777 stmmac_ptp_register(priv);
778
779 return 0;
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000780}
781
782static void stmmac_release_ptp(struct stmmac_priv *priv)
783{
jpintof573c0b2017-01-09 12:35:09 +0000784 if (priv->plat->clk_ptp_ref)
785 clk_disable_unprepare(priv->plat->clk_ptp_ref);
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +0000786 stmmac_ptp_unregister(priv);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +0000787}
788
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700789/**
Joao Pinto29feff32017-03-10 18:24:56 +0000790 * stmmac_mac_flow_ctrl - Configure flow control in all queues
791 * @priv: driver private structure
792 * Description: It is used for configuring the flow control in all queues
793 */
794static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
795{
796 u32 tx_cnt = priv->plat->tx_queues_to_use;
797
798 priv->hw->mac->flow_ctrl(priv->hw, duplex, priv->flow_ctrl,
799 priv->pause, tx_cnt);
800}
801
802/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100803 * stmmac_adjust_link - adjusts the link parameters
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700804 * @dev: net device structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100805 * Description: this is the helper called by the physical abstraction layer
806 * drivers to communicate the phy link status. According the speed and duplex
807 * this driver can invoke registered glue-logic as well.
808 * It also invoke the eee initialization because it could happen when switch
809 * on different networks (that are eee capable).
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700810 */
811static void stmmac_adjust_link(struct net_device *dev)
812{
813 struct stmmac_priv *priv = netdev_priv(dev);
Philippe Reynesd6d50c72016-10-03 08:28:19 +0200814 struct phy_device *phydev = dev->phydev;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700815 unsigned long flags;
LABBE Corentin99a4cca2017-05-24 09:16:43 +0200816 bool new_state = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700817
LABBE Corentin662ec2b2017-02-08 09:31:16 +0100818 if (!phydev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700819 return;
820
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700821 spin_lock_irqsave(&priv->lock, flags);
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000822
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700823 if (phydev->link) {
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000824 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700825
826 /* Now we make sure that we can be in full duplex mode.
827 * If not, we operate in half-duplex mode. */
828 if (phydev->duplex != priv->oldduplex) {
LABBE Corentin99a4cca2017-05-24 09:16:43 +0200829 new_state = true;
LABBE Corentin50cb16d2017-05-24 09:16:44 +0200830 if (!phydev->duplex)
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000831 ctrl &= ~priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700832 else
Giuseppe CAVALLAROdb98a0b2010-01-06 23:07:17 +0000833 ctrl |= priv->hw->link.duplex;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700834 priv->oldduplex = phydev->duplex;
835 }
836 /* Flow Control operation */
837 if (phydev->pause)
Joao Pinto29feff32017-03-10 18:24:56 +0000838 stmmac_mac_flow_ctrl(priv, phydev->duplex);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700839
840 if (phydev->speed != priv->speed) {
LABBE Corentin99a4cca2017-05-24 09:16:43 +0200841 new_state = true;
LABBE Corentinca84dfb2017-05-24 09:16:47 +0200842 ctrl &= ~priv->hw->link.speed_mask;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700843 switch (phydev->speed) {
LABBE Corentinafbe17a2017-05-24 09:16:45 +0200844 case SPEED_1000:
LABBE Corentinca84dfb2017-05-24 09:16:47 +0200845 ctrl |= priv->hw->link.speed1000;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700846 break;
LABBE Corentinafbe17a2017-05-24 09:16:45 +0200847 case SPEED_100:
LABBE Corentinca84dfb2017-05-24 09:16:47 +0200848 ctrl |= priv->hw->link.speed100;
LABBE Corentin9beae262017-02-15 10:46:43 +0100849 break;
LABBE Corentinafbe17a2017-05-24 09:16:45 +0200850 case SPEED_10:
LABBE Corentinca84dfb2017-05-24 09:16:47 +0200851 ctrl |= priv->hw->link.speed10;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700852 break;
853 default:
LABBE Corentinb3e51062016-11-16 20:09:41 +0100854 netif_warn(priv, link, priv->dev,
LABBE Corentincba920a2017-02-08 09:31:15 +0100855 "broken speed: %d\n", phydev->speed);
LABBE Corentin688495b2017-02-15 10:46:41 +0100856 phydev->speed = SPEED_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700857 break;
858 }
LABBE Corentin5db13552017-02-15 10:46:42 +0100859 if (phydev->speed != SPEED_UNKNOWN)
860 stmmac_hw_fix_mac_speed(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700861 priv->speed = phydev->speed;
862 }
863
Giuseppe CAVALLAROad01b7d2010-08-23 20:40:42 +0000864 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700865
866 if (!priv->oldlink) {
LABBE Corentin99a4cca2017-05-24 09:16:43 +0200867 new_state = true;
LABBE Corentin4d869b02017-05-24 09:16:46 +0200868 priv->oldlink = true;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700869 }
870 } else if (priv->oldlink) {
LABBE Corentin99a4cca2017-05-24 09:16:43 +0200871 new_state = true;
LABBE Corentin4d869b02017-05-24 09:16:46 +0200872 priv->oldlink = false;
LABBE Corentinbd006322017-02-15 10:46:40 +0100873 priv->speed = SPEED_UNKNOWN;
874 priv->oldduplex = DUPLEX_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700875 }
876
877 if (new_state && netif_msg_link(priv))
878 phy_print_status(phydev);
879
Giuseppe CAVALLARO4741cf92014-11-04 17:08:08 +0100880 spin_unlock_irqrestore(&priv->lock, flags);
881
Giuseppe CAVALLARO52f95bb2016-04-05 08:46:57 +0200882 if (phydev->is_pseudo_fixed_link)
883 /* Stop PHY layer to call the hook to adjust the link in case
884 * of a switch is attached to the stmmac driver.
885 */
886 phydev->irq = PHY_IGNORE_INTERRUPT;
887 else
888 /* At this stage, init the EEE if supported.
889 * Never called in case of fixed_link.
890 */
891 priv->eee_enabled = stmmac_eee_init(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700892}
893
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000894/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +0100895 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +0000896 * @priv: driver private structure
897 * Description: this is to verify if the HW supports the PCS.
898 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
899 * configured for the TBI, RTBI, or SGMII PHY interface.
900 */
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000901static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
902{
903 int interface = priv->plat->interface;
904
905 if (priv->dma_cap.pcs) {
Byungho An0d909dc2013-06-28 16:35:31 +0900906 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
907 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
908 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
909 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100910 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200911 priv->hw->pcs = STMMAC_PCS_RGMII;
Byungho An0d909dc2013-06-28 16:35:31 +0900912 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100913 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +0200914 priv->hw->pcs = STMMAC_PCS_SGMII;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +0000915 }
916 }
917}
918
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700919/**
920 * stmmac_init_phy - PHY initialization
921 * @dev: net device structure
922 * Description: it initializes the driver's PHY state, and attaches the PHY
923 * to the mac driver.
924 * Return value:
925 * 0 on success
926 */
927static int stmmac_init_phy(struct net_device *dev)
928{
929 struct stmmac_priv *priv = netdev_priv(dev);
930 struct phy_device *phydev;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +0000931 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
Giuseppe CAVALLARO109cdd62010-01-06 23:07:11 +0000932 char bus_id[MII_BUS_ID_SIZE];
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000933 int interface = priv->plat->interface;
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000934 int max_speed = priv->plat->max_speed;
LABBE Corentin4d869b02017-05-24 09:16:46 +0200935 priv->oldlink = false;
LABBE Corentinbd006322017-02-15 10:46:40 +0100936 priv->speed = SPEED_UNKNOWN;
937 priv->oldduplex = DUPLEX_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700938
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700939 if (priv->plat->phy_node) {
940 phydev = of_phy_connect(dev, priv->plat->phy_node,
941 &stmmac_adjust_link, 0, interface);
942 } else {
Giuseppe CAVALLAROa7657f12016-04-01 09:07:16 +0200943 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
944 priv->plat->bus_id);
Srinivas Kandagatlaf142af22012-04-04 04:33:19 +0000945
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700946 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
947 priv->plat->phy_addr);
LABBE Corentinde9a2162016-11-16 20:09:40 +0100948 netdev_dbg(priv->dev, "%s: trying to attach to %s\n", __func__,
LABBE Corentin38ddc592016-11-16 20:09:39 +0100949 phy_id_fmt);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700950
Mathieu Olivari5790cf32015-05-27 11:02:47 -0700951 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
952 interface);
953 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700954
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300955 if (IS_ERR_OR_NULL(phydev)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +0100956 netdev_err(priv->dev, "Could not attach to PHY\n");
Alexey Brodkindfc50fc2015-09-09 18:01:08 +0300957 if (!phydev)
958 return -ENODEV;
959
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700960 return PTR_ERR(phydev);
961 }
962
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000963 /* Stop Advertising 1000BASE Capability if interface is not GMII */
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000964 if ((interface == PHY_INTERFACE_MODE_MII) ||
Srinivas Kandagatla9cbadf02014-01-16 10:51:43 +0000965 (interface == PHY_INTERFACE_MODE_RMII) ||
Pavel Macheka77e4ac2014-08-25 13:31:16 +0200966 (max_speed < 1000 && max_speed > 0))
Srinivas Kandagatlac5b9b4e2011-11-16 21:57:59 +0000967 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
968 SUPPORTED_1000baseT_Full);
Srinivas Kandagatla79ee1dc2011-10-18 00:01:18 +0000969
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700970 /*
971 * Broken HW is sometimes missing the pull-up resistor on the
972 * MDIO line, which results in reads to non-existent devices returning
973 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
974 * device as well.
975 * Note: phydev->phy_id is the result of reading the UID PHY registers.
976 */
Mathieu Olivari27732382015-05-27 11:02:48 -0700977 if (!priv->plat->phy_node && phydev->phy_id == 0) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700978 phy_disconnect(phydev);
979 return -ENODEV;
980 }
Giuseppe Cavallaro8e99fc52016-02-29 14:27:39 +0100981
Florian Fainellic51e4242016-11-13 17:50:35 -0800982 /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
983 * subsequent PHY polling, make sure we force a link transition if
984 * we have a UP/DOWN/UP transition
985 */
986 if (phydev->is_pseudo_fixed_link)
987 phydev->irq = PHY_POLL;
988
LABBE Corentinb05c76a2017-02-08 09:31:18 +0100989 phy_attached_info(phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -0700990 return 0;
991}
992
Joao Pinto71fedb02017-04-06 09:49:08 +0100993static void stmmac_display_rx_rings(struct stmmac_priv *priv)
994{
Joao Pinto54139cf2017-04-06 09:49:09 +0100995 u32 rx_cnt = priv->plat->rx_queues_to_use;
Joao Pinto71fedb02017-04-06 09:49:08 +0100996 void *head_rx;
Joao Pinto54139cf2017-04-06 09:49:09 +0100997 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +0100998
Joao Pinto54139cf2017-04-06 09:49:09 +0100999 /* Display RX rings */
1000 for (queue = 0; queue < rx_cnt; queue++) {
1001 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001002
Joao Pinto54139cf2017-04-06 09:49:09 +01001003 pr_info("\tRX Queue %u rings\n", queue);
1004
1005 if (priv->extend_desc)
1006 head_rx = (void *)rx_q->dma_erx;
1007 else
1008 head_rx = (void *)rx_q->dma_rx;
1009
1010 /* Display RX ring */
1011 priv->hw->desc->display_ring(head_rx, DMA_RX_SIZE, true);
1012 }
Joao Pinto71fedb02017-04-06 09:49:08 +01001013}
1014
1015static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1016{
Joao Pintoce736782017-04-06 09:49:10 +01001017 u32 tx_cnt = priv->plat->tx_queues_to_use;
Joao Pinto71fedb02017-04-06 09:49:08 +01001018 void *head_tx;
Joao Pintoce736782017-04-06 09:49:10 +01001019 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +01001020
Joao Pintoce736782017-04-06 09:49:10 +01001021 /* Display TX rings */
1022 for (queue = 0; queue < tx_cnt; queue++) {
1023 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001024
Joao Pintoce736782017-04-06 09:49:10 +01001025 pr_info("\tTX Queue %d rings\n", queue);
1026
1027 if (priv->extend_desc)
1028 head_tx = (void *)tx_q->dma_etx;
1029 else
1030 head_tx = (void *)tx_q->dma_tx;
1031
1032 priv->hw->desc->display_ring(head_tx, DMA_TX_SIZE, false);
1033 }
Joao Pinto71fedb02017-04-06 09:49:08 +01001034}
1035
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001036static void stmmac_display_rings(struct stmmac_priv *priv)
1037{
Joao Pinto71fedb02017-04-06 09:49:08 +01001038 /* Display RX ring */
1039 stmmac_display_rx_rings(priv);
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02001040
Joao Pinto71fedb02017-04-06 09:49:08 +01001041 /* Display TX ring */
1042 stmmac_display_tx_rings(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001043}
1044
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001045static int stmmac_set_bfsize(int mtu, int bufsize)
1046{
1047 int ret = bufsize;
1048
1049 if (mtu >= BUF_SIZE_4KiB)
1050 ret = BUF_SIZE_8KiB;
1051 else if (mtu >= BUF_SIZE_2KiB)
1052 ret = BUF_SIZE_4KiB;
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +01001053 else if (mtu > DEFAULT_BUFSIZE)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001054 ret = BUF_SIZE_2KiB;
1055 else
Giuseppe CAVALLAROd9167012014-03-10 13:40:32 +01001056 ret = DEFAULT_BUFSIZE;
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001057
1058 return ret;
1059}
1060
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001061/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001062 * stmmac_clear_rx_descriptors - clear RX descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001063 * @priv: driver private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01001064 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001065 * Description: this function is called to clear the RX descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001066 * in case of both basic and extended descriptors are used.
1067 */
Joao Pinto54139cf2017-04-06 09:49:09 +01001068static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001069{
Joao Pinto54139cf2017-04-06 09:49:09 +01001070 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
LABBE Corentin5bacd772017-03-29 07:05:40 +02001071 int i;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001072
Joao Pinto71fedb02017-04-06 09:49:08 +01001073 /* Clear the RX descriptors */
LABBE Corentin5bacd772017-03-29 07:05:40 +02001074 for (i = 0; i < DMA_RX_SIZE; i++)
1075 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01001076 priv->hw->desc->init_rx_desc(&rx_q->dma_erx[i].basic,
LABBE Corentin5bacd772017-03-29 07:05:40 +02001077 priv->use_riwt, priv->mode,
1078 (i == DMA_RX_SIZE - 1));
1079 else
Joao Pinto54139cf2017-04-06 09:49:09 +01001080 priv->hw->desc->init_rx_desc(&rx_q->dma_rx[i],
LABBE Corentin5bacd772017-03-29 07:05:40 +02001081 priv->use_riwt, priv->mode,
1082 (i == DMA_RX_SIZE - 1));
Joao Pinto71fedb02017-04-06 09:49:08 +01001083}
1084
1085/**
1086 * stmmac_clear_tx_descriptors - clear tx descriptors
1087 * @priv: driver private structure
Joao Pintoce736782017-04-06 09:49:10 +01001088 * @queue: TX queue index.
Joao Pinto71fedb02017-04-06 09:49:08 +01001089 * Description: this function is called to clear the TX descriptors
1090 * in case of both basic and extended descriptors are used.
1091 */
Joao Pintoce736782017-04-06 09:49:10 +01001092static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
Joao Pinto71fedb02017-04-06 09:49:08 +01001093{
Joao Pintoce736782017-04-06 09:49:10 +01001094 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001095 int i;
1096
1097 /* Clear the TX descriptors */
LABBE Corentin5bacd772017-03-29 07:05:40 +02001098 for (i = 0; i < DMA_TX_SIZE; i++)
1099 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01001100 priv->hw->desc->init_tx_desc(&tx_q->dma_etx[i].basic,
LABBE Corentin5bacd772017-03-29 07:05:40 +02001101 priv->mode,
1102 (i == DMA_TX_SIZE - 1));
1103 else
Joao Pintoce736782017-04-06 09:49:10 +01001104 priv->hw->desc->init_tx_desc(&tx_q->dma_tx[i],
LABBE Corentin5bacd772017-03-29 07:05:40 +02001105 priv->mode,
1106 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001107}
1108
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001109/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001110 * stmmac_clear_descriptors - clear descriptors
1111 * @priv: driver private structure
1112 * Description: this function is called to clear the TX and RX descriptors
1113 * in case of both basic and extended descriptors are used.
1114 */
1115static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1116{
Joao Pinto54139cf2017-04-06 09:49:09 +01001117 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
Joao Pintoce736782017-04-06 09:49:10 +01001118 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01001119 u32 queue;
1120
Joao Pinto71fedb02017-04-06 09:49:08 +01001121 /* Clear the RX descriptors */
Joao Pinto54139cf2017-04-06 09:49:09 +01001122 for (queue = 0; queue < rx_queue_cnt; queue++)
1123 stmmac_clear_rx_descriptors(priv, queue);
Joao Pinto71fedb02017-04-06 09:49:08 +01001124
1125 /* Clear the TX descriptors */
Joao Pintoce736782017-04-06 09:49:10 +01001126 for (queue = 0; queue < tx_queue_cnt; queue++)
1127 stmmac_clear_tx_descriptors(priv, queue);
Joao Pinto71fedb02017-04-06 09:49:08 +01001128}
1129
1130/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001131 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1132 * @priv: driver private structure
1133 * @p: descriptor pointer
1134 * @i: descriptor index
Joao Pinto54139cf2017-04-06 09:49:09 +01001135 * @flags: gfp flag
1136 * @queue: RX queue index
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001137 * Description: this function is called to allocate a receive buffer, perform
1138 * the DMA mapping and init the descriptor.
1139 */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001140static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
Joao Pinto54139cf2017-04-06 09:49:09 +01001141 int i, gfp_t flags, u32 queue)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001142{
Joao Pinto54139cf2017-04-06 09:49:09 +01001143 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001144 struct sk_buff *skb;
1145
Vineet Gupta4ec49a32015-05-20 12:04:40 +05301146 skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001147 if (!skb) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01001148 netdev_err(priv->dev,
1149 "%s: Rx init fails; skb is NULL\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001150 return -ENOMEM;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001151 }
Joao Pinto54139cf2017-04-06 09:49:09 +01001152 rx_q->rx_skbuff[i] = skb;
1153 rx_q->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001154 priv->dma_buf_sz,
1155 DMA_FROM_DEVICE);
Joao Pinto54139cf2017-04-06 09:49:09 +01001156 if (dma_mapping_error(priv->device, rx_q->rx_skbuff_dma[i])) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01001157 netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001158 dev_kfree_skb_any(skb);
1159 return -EINVAL;
1160 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001161
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001162 if (priv->synopsys_id >= DWMAC_CORE_4_00)
Joao Pinto54139cf2017-04-06 09:49:09 +01001163 p->des0 = cpu_to_le32(rx_q->rx_skbuff_dma[i]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001164 else
Joao Pinto54139cf2017-04-06 09:49:09 +01001165 p->des2 = cpu_to_le32(rx_q->rx_skbuff_dma[i]);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001166
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001167 if ((priv->hw->mode->init_desc3) &&
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001168 (priv->dma_buf_sz == BUF_SIZE_16KiB))
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001169 priv->hw->mode->init_desc3(p);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001170
1171 return 0;
1172}
1173
Joao Pinto71fedb02017-04-06 09:49:08 +01001174/**
1175 * stmmac_free_rx_buffer - free RX dma buffers
1176 * @priv: private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01001177 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001178 * @i: buffer index.
1179 */
Joao Pinto54139cf2017-04-06 09:49:09 +01001180static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001181{
Joao Pinto54139cf2017-04-06 09:49:09 +01001182 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1183
1184 if (rx_q->rx_skbuff[i]) {
1185 dma_unmap_single(priv->device, rx_q->rx_skbuff_dma[i],
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001186 priv->dma_buf_sz, DMA_FROM_DEVICE);
Joao Pinto54139cf2017-04-06 09:49:09 +01001187 dev_kfree_skb_any(rx_q->rx_skbuff[i]);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001188 }
Joao Pinto54139cf2017-04-06 09:49:09 +01001189 rx_q->rx_skbuff[i] = NULL;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001190}
1191
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001192/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001193 * stmmac_free_tx_buffer - free RX dma buffers
1194 * @priv: private structure
Joao Pintoce736782017-04-06 09:49:10 +01001195 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001196 * @i: buffer index.
1197 */
Joao Pintoce736782017-04-06 09:49:10 +01001198static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
Joao Pinto71fedb02017-04-06 09:49:08 +01001199{
Joao Pintoce736782017-04-06 09:49:10 +01001200 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1201
1202 if (tx_q->tx_skbuff_dma[i].buf) {
1203 if (tx_q->tx_skbuff_dma[i].map_as_page)
Joao Pinto71fedb02017-04-06 09:49:08 +01001204 dma_unmap_page(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001205 tx_q->tx_skbuff_dma[i].buf,
1206 tx_q->tx_skbuff_dma[i].len,
Joao Pinto71fedb02017-04-06 09:49:08 +01001207 DMA_TO_DEVICE);
1208 else
1209 dma_unmap_single(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001210 tx_q->tx_skbuff_dma[i].buf,
1211 tx_q->tx_skbuff_dma[i].len,
Joao Pinto71fedb02017-04-06 09:49:08 +01001212 DMA_TO_DEVICE);
1213 }
1214
Joao Pintoce736782017-04-06 09:49:10 +01001215 if (tx_q->tx_skbuff[i]) {
1216 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1217 tx_q->tx_skbuff[i] = NULL;
1218 tx_q->tx_skbuff_dma[i].buf = 0;
1219 tx_q->tx_skbuff_dma[i].map_as_page = false;
Joao Pinto71fedb02017-04-06 09:49:08 +01001220 }
1221}
1222
1223/**
1224 * init_dma_rx_desc_rings - init the RX descriptor rings
Joao Pintoaff3d9e2017-03-17 16:11:05 +00001225 * @dev: net device structure
1226 * @flags: gfp flag.
Joao Pinto71fedb02017-04-06 09:49:08 +01001227 * Description: this function initializes the DMA RX descriptors
LABBE Corentin5bacd772017-03-29 07:05:40 +02001228 * and allocates the socket buffers. It supports the chained and ring
Joao Pintoaff3d9e2017-03-17 16:11:05 +00001229 * modes.
1230 */
Joao Pinto71fedb02017-04-06 09:49:08 +01001231static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
Joao Pintoaff3d9e2017-03-17 16:11:05 +00001232{
1233 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto54139cf2017-04-06 09:49:09 +01001234 u32 rx_count = priv->plat->rx_queues_to_use;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001235 unsigned int bfsize = 0;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001236 int ret = -ENOMEM;
Colin Ian King1d3028f2017-06-06 14:10:49 +01001237 int queue;
Joao Pinto54139cf2017-04-06 09:49:09 +01001238 int i;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001239
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01001240 if (priv->hw->mode->set_16kib_bfsize)
1241 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001242
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001243 if (bfsize < BUF_SIZE_16KiB)
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001244 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001245
Vince Bridgers2618abb2014-01-20 05:39:01 -06001246 priv->dma_buf_sz = bfsize;
1247
Joao Pinto54139cf2017-04-06 09:49:09 +01001248 /* RX INITIALIZATION */
LABBE Corentinb3e51062016-11-16 20:09:41 +01001249 netif_dbg(priv, probe, priv->dev,
1250 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1251
Joao Pinto54139cf2017-04-06 09:49:09 +01001252 for (queue = 0; queue < rx_count; queue++) {
1253 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001254
Joao Pinto54139cf2017-04-06 09:49:09 +01001255 netif_dbg(priv, probe, priv->dev,
1256 "(%s) dma_rx_phy=0x%08x\n", __func__,
1257 (u32)rx_q->dma_rx_phy);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001258
Joao Pinto54139cf2017-04-06 09:49:09 +01001259 for (i = 0; i < DMA_RX_SIZE; i++) {
1260 struct dma_desc *p;
1261
1262 if (priv->extend_desc)
1263 p = &((rx_q->dma_erx + i)->basic);
1264 else
1265 p = rx_q->dma_rx + i;
1266
1267 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1268 queue);
1269 if (ret)
1270 goto err_init_rx_buffers;
1271
1272 netif_dbg(priv, probe, priv->dev, "[%p]\t[%p]\t[%x]\n",
1273 rx_q->rx_skbuff[i], rx_q->rx_skbuff[i]->data,
1274 (unsigned int)rx_q->rx_skbuff_dma[i]);
1275 }
1276
1277 rx_q->cur_rx = 0;
1278 rx_q->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
1279
1280 stmmac_clear_rx_descriptors(priv, queue);
1281
1282 /* Setup the chained descriptor addresses */
1283 if (priv->mode == STMMAC_CHAIN_MODE) {
1284 if (priv->extend_desc)
1285 priv->hw->mode->init(rx_q->dma_erx,
1286 rx_q->dma_rx_phy,
1287 DMA_RX_SIZE, 1);
1288 else
1289 priv->hw->mode->init(rx_q->dma_rx,
1290 rx_q->dma_rx_phy,
1291 DMA_RX_SIZE, 0);
1292 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001293 }
Joao Pinto54139cf2017-04-06 09:49:09 +01001294
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001295 buf_sz = bfsize;
1296
Joao Pinto54139cf2017-04-06 09:49:09 +01001297 return 0;
1298
1299err_init_rx_buffers:
1300 while (queue >= 0) {
1301 while (--i >= 0)
1302 stmmac_free_rx_buffer(priv, queue, i);
1303
1304 if (queue == 0)
1305 break;
1306
1307 i = DMA_RX_SIZE;
1308 queue--;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001309 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00001310
Joao Pinto71fedb02017-04-06 09:49:08 +01001311 return ret;
1312}
1313
1314/**
1315 * init_dma_tx_desc_rings - init the TX descriptor rings
1316 * @dev: net device structure.
1317 * Description: this function initializes the DMA TX descriptors
1318 * and allocates the socket buffers. It supports the chained and ring
1319 * modes.
1320 */
1321static int init_dma_tx_desc_rings(struct net_device *dev)
1322{
1323 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pintoce736782017-04-06 09:49:10 +01001324 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1325 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +01001326 int i;
1327
Joao Pintoce736782017-04-06 09:49:10 +01001328 for (queue = 0; queue < tx_queue_cnt; queue++) {
1329 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001330
Joao Pintoce736782017-04-06 09:49:10 +01001331 netif_dbg(priv, probe, priv->dev,
1332 "(%s) dma_tx_phy=0x%08x\n", __func__,
1333 (u32)tx_q->dma_tx_phy);
Joao Pinto71fedb02017-04-06 09:49:08 +01001334
Joao Pintoce736782017-04-06 09:49:10 +01001335 /* Setup the chained descriptor addresses */
1336 if (priv->mode == STMMAC_CHAIN_MODE) {
1337 if (priv->extend_desc)
1338 priv->hw->mode->init(tx_q->dma_etx,
1339 tx_q->dma_tx_phy,
1340 DMA_TX_SIZE, 1);
1341 else
1342 priv->hw->mode->init(tx_q->dma_tx,
1343 tx_q->dma_tx_phy,
1344 DMA_TX_SIZE, 0);
LABBE Corentin5bacd772017-03-29 07:05:40 +02001345 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001346
Joao Pintoce736782017-04-06 09:49:10 +01001347 for (i = 0; i < DMA_TX_SIZE; i++) {
1348 struct dma_desc *p;
Joao Pintoce736782017-04-06 09:49:10 +01001349 if (priv->extend_desc)
1350 p = &((tx_q->dma_etx + i)->basic);
1351 else
1352 p = tx_q->dma_tx + i;
1353
1354 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1355 p->des0 = 0;
1356 p->des1 = 0;
1357 p->des2 = 0;
1358 p->des3 = 0;
1359 } else {
1360 p->des2 = 0;
1361 }
1362
1363 tx_q->tx_skbuff_dma[i].buf = 0;
1364 tx_q->tx_skbuff_dma[i].map_as_page = false;
1365 tx_q->tx_skbuff_dma[i].len = 0;
1366 tx_q->tx_skbuff_dma[i].last_segment = false;
1367 tx_q->tx_skbuff[i] = NULL;
1368 }
1369
1370 tx_q->dirty_tx = 0;
1371 tx_q->cur_tx = 0;
Niklas Cassel8d212a9e2018-02-19 18:11:09 +01001372 tx_q->mss = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001373
Joao Pintoc22a3f42017-04-06 09:49:11 +01001374 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1375 }
LABBE Corentin5bacd772017-03-29 07:05:40 +02001376
Joao Pinto71fedb02017-04-06 09:49:08 +01001377 return 0;
1378}
1379
1380/**
1381 * init_dma_desc_rings - init the RX/TX descriptor rings
1382 * @dev: net device structure
1383 * @flags: gfp flag.
1384 * Description: this function initializes the DMA RX/TX descriptors
1385 * and allocates the socket buffers. It supports the chained and ring
1386 * modes.
1387 */
1388static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1389{
1390 struct stmmac_priv *priv = netdev_priv(dev);
1391 int ret;
1392
1393 ret = init_dma_rx_desc_rings(dev, flags);
1394 if (ret)
1395 return ret;
1396
1397 ret = init_dma_tx_desc_rings(dev);
1398
LABBE Corentin5bacd772017-03-29 07:05:40 +02001399 stmmac_clear_descriptors(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001400
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001401 if (netif_msg_hw(priv))
1402 stmmac_display_rings(priv);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001403
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02001404 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001405}
1406
Joao Pinto71fedb02017-04-06 09:49:08 +01001407/**
1408 * dma_free_rx_skbufs - free RX dma buffers
1409 * @priv: private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01001410 * @queue: RX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001411 */
Joao Pinto54139cf2017-04-06 09:49:09 +01001412static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001413{
1414 int i;
1415
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001416 for (i = 0; i < DMA_RX_SIZE; i++)
Joao Pinto54139cf2017-04-06 09:49:09 +01001417 stmmac_free_rx_buffer(priv, queue, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001418}
1419
Joao Pinto71fedb02017-04-06 09:49:08 +01001420/**
1421 * dma_free_tx_skbufs - free TX dma buffers
1422 * @priv: private structure
Joao Pintoce736782017-04-06 09:49:10 +01001423 * @queue: TX queue index
Joao Pinto71fedb02017-04-06 09:49:08 +01001424 */
Joao Pintoce736782017-04-06 09:49:10 +01001425static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001426{
1427 int i;
1428
Joao Pinto71fedb02017-04-06 09:49:08 +01001429 for (i = 0; i < DMA_TX_SIZE; i++)
Joao Pintoce736782017-04-06 09:49:10 +01001430 stmmac_free_tx_buffer(priv, queue, i);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001431}
1432
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001433/**
Joao Pinto54139cf2017-04-06 09:49:09 +01001434 * free_dma_rx_desc_resources - free RX dma desc resources
1435 * @priv: private structure
1436 */
1437static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1438{
1439 u32 rx_count = priv->plat->rx_queues_to_use;
1440 u32 queue;
1441
1442 /* Free RX queue resources */
1443 for (queue = 0; queue < rx_count; queue++) {
1444 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1445
1446 /* Release the DMA RX socket buffers */
1447 dma_free_rx_skbufs(priv, queue);
1448
1449 /* Free DMA regions of consistent memory previously allocated */
1450 if (!priv->extend_desc)
1451 dma_free_coherent(priv->device,
1452 DMA_RX_SIZE * sizeof(struct dma_desc),
1453 rx_q->dma_rx, rx_q->dma_rx_phy);
1454 else
1455 dma_free_coherent(priv->device, DMA_RX_SIZE *
1456 sizeof(struct dma_extended_desc),
1457 rx_q->dma_erx, rx_q->dma_rx_phy);
1458
1459 kfree(rx_q->rx_skbuff_dma);
1460 kfree(rx_q->rx_skbuff);
1461 }
1462}
1463
1464/**
Joao Pintoce736782017-04-06 09:49:10 +01001465 * free_dma_tx_desc_resources - free TX dma desc resources
1466 * @priv: private structure
1467 */
1468static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1469{
1470 u32 tx_count = priv->plat->tx_queues_to_use;
Christophe Jaillet62242262017-07-08 09:46:54 +02001471 u32 queue;
Joao Pintoce736782017-04-06 09:49:10 +01001472
1473 /* Free TX queue resources */
1474 for (queue = 0; queue < tx_count; queue++) {
1475 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1476
1477 /* Release the DMA TX socket buffers */
1478 dma_free_tx_skbufs(priv, queue);
1479
1480 /* Free DMA regions of consistent memory previously allocated */
1481 if (!priv->extend_desc)
1482 dma_free_coherent(priv->device,
1483 DMA_TX_SIZE * sizeof(struct dma_desc),
1484 tx_q->dma_tx, tx_q->dma_tx_phy);
1485 else
1486 dma_free_coherent(priv->device, DMA_TX_SIZE *
1487 sizeof(struct dma_extended_desc),
1488 tx_q->dma_etx, tx_q->dma_tx_phy);
1489
1490 kfree(tx_q->tx_skbuff_dma);
1491 kfree(tx_q->tx_skbuff);
1492 }
1493}
1494
1495/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001496 * alloc_dma_rx_desc_resources - alloc RX resources.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001497 * @priv: private structure
1498 * Description: according to which descriptor can be used (extend or basic)
1499 * this function allocates the resources for TX and RX paths. In case of
1500 * reception, for example, it pre-allocated the RX socket buffer in order to
1501 * allow zero-copy mechanism.
1502 */
Joao Pinto71fedb02017-04-06 09:49:08 +01001503static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001504{
Joao Pinto54139cf2017-04-06 09:49:09 +01001505 u32 rx_count = priv->plat->rx_queues_to_use;
LABBE Corentin5bacd772017-03-29 07:05:40 +02001506 int ret = -ENOMEM;
Joao Pinto54139cf2017-04-06 09:49:09 +01001507 u32 queue;
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001508
Joao Pinto54139cf2017-04-06 09:49:09 +01001509 /* RX queues buffers and DMA */
1510 for (queue = 0; queue < rx_count; queue++) {
1511 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001512
Joao Pinto54139cf2017-04-06 09:49:09 +01001513 rx_q->queue_index = queue;
1514 rx_q->priv_data = priv;
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001515
Joao Pinto54139cf2017-04-06 09:49:09 +01001516 rx_q->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE,
1517 sizeof(dma_addr_t),
LABBE Corentin5bacd772017-03-29 07:05:40 +02001518 GFP_KERNEL);
Joao Pinto54139cf2017-04-06 09:49:09 +01001519 if (!rx_q->rx_skbuff_dma)
Christophe Jaillet63c3aa62017-07-08 09:46:33 +02001520 goto err_dma;
Joao Pinto54139cf2017-04-06 09:49:09 +01001521
1522 rx_q->rx_skbuff = kmalloc_array(DMA_RX_SIZE,
1523 sizeof(struct sk_buff *),
1524 GFP_KERNEL);
1525 if (!rx_q->rx_skbuff)
LABBE Corentin5bacd772017-03-29 07:05:40 +02001526 goto err_dma;
1527
Joao Pinto54139cf2017-04-06 09:49:09 +01001528 if (priv->extend_desc) {
1529 rx_q->dma_erx = dma_zalloc_coherent(priv->device,
1530 DMA_RX_SIZE *
1531 sizeof(struct
1532 dma_extended_desc),
1533 &rx_q->dma_rx_phy,
1534 GFP_KERNEL);
1535 if (!rx_q->dma_erx)
1536 goto err_dma;
1537
1538 } else {
1539 rx_q->dma_rx = dma_zalloc_coherent(priv->device,
1540 DMA_RX_SIZE *
1541 sizeof(struct
1542 dma_desc),
1543 &rx_q->dma_rx_phy,
1544 GFP_KERNEL);
1545 if (!rx_q->dma_rx)
1546 goto err_dma;
1547 }
Joao Pinto71fedb02017-04-06 09:49:08 +01001548 }
1549
1550 return 0;
1551
1552err_dma:
Joao Pinto54139cf2017-04-06 09:49:09 +01001553 free_dma_rx_desc_resources(priv);
1554
Joao Pinto71fedb02017-04-06 09:49:08 +01001555 return ret;
1556}
1557
1558/**
1559 * alloc_dma_tx_desc_resources - alloc TX resources.
1560 * @priv: private structure
1561 * Description: according to which descriptor can be used (extend or basic)
1562 * this function allocates the resources for TX and RX paths. In case of
1563 * reception, for example, it pre-allocated the RX socket buffer in order to
1564 * allow zero-copy mechanism.
1565 */
1566static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
1567{
Joao Pintoce736782017-04-06 09:49:10 +01001568 u32 tx_count = priv->plat->tx_queues_to_use;
Joao Pinto71fedb02017-04-06 09:49:08 +01001569 int ret = -ENOMEM;
Joao Pintoce736782017-04-06 09:49:10 +01001570 u32 queue;
Joao Pinto71fedb02017-04-06 09:49:08 +01001571
Joao Pintoce736782017-04-06 09:49:10 +01001572 /* TX queues buffers and DMA */
1573 for (queue = 0; queue < tx_count; queue++) {
1574 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Joao Pinto71fedb02017-04-06 09:49:08 +01001575
Joao Pintoce736782017-04-06 09:49:10 +01001576 tx_q->queue_index = queue;
1577 tx_q->priv_data = priv;
Joao Pinto71fedb02017-04-06 09:49:08 +01001578
Joao Pintoce736782017-04-06 09:49:10 +01001579 tx_q->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
1580 sizeof(*tx_q->tx_skbuff_dma),
LABBE Corentin5bacd772017-03-29 07:05:40 +02001581 GFP_KERNEL);
Joao Pintoce736782017-04-06 09:49:10 +01001582 if (!tx_q->tx_skbuff_dma)
Christophe Jaillet62242262017-07-08 09:46:54 +02001583 goto err_dma;
Joao Pintoce736782017-04-06 09:49:10 +01001584
1585 tx_q->tx_skbuff = kmalloc_array(DMA_TX_SIZE,
1586 sizeof(struct sk_buff *),
1587 GFP_KERNEL);
1588 if (!tx_q->tx_skbuff)
Christophe Jaillet62242262017-07-08 09:46:54 +02001589 goto err_dma;
Joao Pintoce736782017-04-06 09:49:10 +01001590
1591 if (priv->extend_desc) {
1592 tx_q->dma_etx = dma_zalloc_coherent(priv->device,
1593 DMA_TX_SIZE *
1594 sizeof(struct
1595 dma_extended_desc),
1596 &tx_q->dma_tx_phy,
1597 GFP_KERNEL);
1598 if (!tx_q->dma_etx)
Christophe Jaillet62242262017-07-08 09:46:54 +02001599 goto err_dma;
Joao Pintoce736782017-04-06 09:49:10 +01001600 } else {
1601 tx_q->dma_tx = dma_zalloc_coherent(priv->device,
1602 DMA_TX_SIZE *
1603 sizeof(struct
1604 dma_desc),
1605 &tx_q->dma_tx_phy,
1606 GFP_KERNEL);
1607 if (!tx_q->dma_tx)
Christophe Jaillet62242262017-07-08 09:46:54 +02001608 goto err_dma;
Joao Pintoce736782017-04-06 09:49:10 +01001609 }
LABBE Corentin5bacd772017-03-29 07:05:40 +02001610 }
1611
1612 return 0;
1613
Christophe Jaillet62242262017-07-08 09:46:54 +02001614err_dma:
Joao Pintoce736782017-04-06 09:49:10 +01001615 free_dma_tx_desc_resources(priv);
1616
Srinivas Kandagatla09f8d692014-01-16 10:52:06 +00001617 return ret;
1618}
1619
Joao Pinto71fedb02017-04-06 09:49:08 +01001620/**
1621 * alloc_dma_desc_resources - alloc TX/RX resources.
1622 * @priv: private structure
1623 * Description: according to which descriptor can be used (extend or basic)
1624 * this function allocates the resources for TX and RX paths. In case of
1625 * reception, for example, it pre-allocated the RX socket buffer in order to
1626 * allow zero-copy mechanism.
1627 */
1628static int alloc_dma_desc_resources(struct stmmac_priv *priv)
LABBE Corentin5bacd772017-03-29 07:05:40 +02001629{
Joao Pinto54139cf2017-04-06 09:49:09 +01001630 /* RX Allocation */
Joao Pinto71fedb02017-04-06 09:49:08 +01001631 int ret = alloc_dma_rx_desc_resources(priv);
1632
1633 if (ret)
1634 return ret;
1635
1636 ret = alloc_dma_tx_desc_resources(priv);
1637
1638 return ret;
1639}
1640
1641/**
Joao Pinto71fedb02017-04-06 09:49:08 +01001642 * free_dma_desc_resources - free dma desc resources
1643 * @priv: private structure
1644 */
1645static void free_dma_desc_resources(struct stmmac_priv *priv)
1646{
1647 /* Release the DMA RX socket buffers */
1648 free_dma_rx_desc_resources(priv);
1649
1650 /* Release the DMA TX socket buffers */
1651 free_dma_tx_desc_resources(priv);
1652}
1653
1654/**
jpinto9eb12472016-12-28 12:57:48 +00001655 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
1656 * @priv: driver private structure
1657 * Description: It is used for enabling the rx queues in the MAC
1658 */
1659static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
1660{
Joao Pinto4f6046f2017-03-10 18:24:54 +00001661 u32 rx_queues_count = priv->plat->rx_queues_to_use;
1662 int queue;
1663 u8 mode;
jpinto9eb12472016-12-28 12:57:48 +00001664
Joao Pinto4f6046f2017-03-10 18:24:54 +00001665 for (queue = 0; queue < rx_queues_count; queue++) {
1666 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
1667 priv->hw->mac->rx_queue_enable(priv->hw, mode, queue);
1668 }
jpinto9eb12472016-12-28 12:57:48 +00001669}
1670
1671/**
Joao Pintoae4f0d42017-03-15 11:04:47 +00001672 * stmmac_start_rx_dma - start RX DMA channel
1673 * @priv: driver private structure
1674 * @chan: RX channel index
1675 * Description:
1676 * This starts a RX DMA channel
1677 */
1678static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
1679{
1680 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
1681 priv->hw->dma->start_rx(priv->ioaddr, chan);
1682}
1683
1684/**
1685 * stmmac_start_tx_dma - start TX DMA channel
1686 * @priv: driver private structure
1687 * @chan: TX channel index
1688 * Description:
1689 * This starts a TX DMA channel
1690 */
1691static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
1692{
1693 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
1694 priv->hw->dma->start_tx(priv->ioaddr, chan);
1695}
1696
1697/**
1698 * stmmac_stop_rx_dma - stop RX DMA channel
1699 * @priv: driver private structure
1700 * @chan: RX channel index
1701 * Description:
1702 * This stops a RX DMA channel
1703 */
1704static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
1705{
1706 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
1707 priv->hw->dma->stop_rx(priv->ioaddr, chan);
1708}
1709
1710/**
1711 * stmmac_stop_tx_dma - stop TX DMA channel
1712 * @priv: driver private structure
1713 * @chan: TX channel index
1714 * Description:
1715 * This stops a TX DMA channel
1716 */
1717static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
1718{
1719 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
1720 priv->hw->dma->stop_tx(priv->ioaddr, chan);
1721}
1722
1723/**
1724 * stmmac_start_all_dma - start all RX and TX DMA channels
1725 * @priv: driver private structure
1726 * Description:
1727 * This starts all the RX and TX DMA channels
1728 */
1729static void stmmac_start_all_dma(struct stmmac_priv *priv)
1730{
1731 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1732 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1733 u32 chan = 0;
1734
1735 for (chan = 0; chan < rx_channels_count; chan++)
1736 stmmac_start_rx_dma(priv, chan);
1737
1738 for (chan = 0; chan < tx_channels_count; chan++)
1739 stmmac_start_tx_dma(priv, chan);
1740}
1741
1742/**
1743 * stmmac_stop_all_dma - stop all RX and TX DMA channels
1744 * @priv: driver private structure
1745 * Description:
1746 * This stops the RX and TX DMA channels
1747 */
1748static void stmmac_stop_all_dma(struct stmmac_priv *priv)
1749{
1750 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1751 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1752 u32 chan = 0;
1753
1754 for (chan = 0; chan < rx_channels_count; chan++)
1755 stmmac_stop_rx_dma(priv, chan);
1756
1757 for (chan = 0; chan < tx_channels_count; chan++)
1758 stmmac_stop_tx_dma(priv, chan);
1759}
1760
1761/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001762 * stmmac_dma_operation_mode - HW DMA operation mode
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001763 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001764 * Description: it is used for configuring the DMA operation mode register in
1765 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001766 */
1767static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1768{
Joao Pinto6deee222017-03-15 11:04:45 +00001769 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1770 u32 tx_channels_count = priv->plat->tx_queues_to_use;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001771 int rxfifosz = priv->plat->rx_fifo_size;
Jose Abreu52a76232017-10-13 10:58:36 +01001772 int txfifosz = priv->plat->tx_fifo_size;
Joao Pinto6deee222017-03-15 11:04:45 +00001773 u32 txmode = 0;
1774 u32 rxmode = 0;
1775 u32 chan = 0;
Jose Abreua0daae12017-10-13 10:58:37 +01001776 u8 qmode = 0;
Vince Bridgersf88203a2015-04-15 11:17:42 -05001777
Thierry Reding11fbf812017-03-10 17:34:58 +01001778 if (rxfifosz == 0)
1779 rxfifosz = priv->dma_cap.rx_fifo_size;
Jose Abreu52a76232017-10-13 10:58:36 +01001780 if (txfifosz == 0)
1781 txfifosz = priv->dma_cap.tx_fifo_size;
1782
1783 /* Adjust for real per queue fifo size */
1784 rxfifosz /= rx_channels_count;
1785 txfifosz /= tx_channels_count;
Thierry Reding11fbf812017-03-10 17:34:58 +01001786
Joao Pinto6deee222017-03-15 11:04:45 +00001787 if (priv->plat->force_thresh_dma_mode) {
1788 txmode = tc;
1789 rxmode = tc;
1790 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
Srinivas Kandagatla61b80132011-07-17 20:54:09 +00001791 /*
1792 * In case of GMAC, SF mode can be enabled
1793 * to perform the TX COE in HW. This depends on:
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00001794 * 1) TX COE if actually supported
1795 * 2) There is no bugged Jumbo frame support
1796 * that needs to not insert csum in the TDES.
1797 */
Joao Pinto6deee222017-03-15 11:04:45 +00001798 txmode = SF_DMA_MODE;
1799 rxmode = SF_DMA_MODE;
Sonic Zhangb2dec112015-01-30 13:49:32 +08001800 priv->xstats.threshold = SF_DMA_MODE;
Joao Pinto6deee222017-03-15 11:04:45 +00001801 } else {
1802 txmode = tc;
1803 rxmode = SF_DMA_MODE;
1804 }
1805
1806 /* configure all channels */
1807 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
Jose Abreua0daae12017-10-13 10:58:37 +01001808 for (chan = 0; chan < rx_channels_count; chan++) {
1809 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
Joao Pinto6deee222017-03-15 11:04:45 +00001810
Jose Abreua0daae12017-10-13 10:58:37 +01001811 priv->hw->dma->dma_rx_mode(priv->ioaddr, rxmode, chan,
1812 rxfifosz, qmode);
1813 }
1814
1815 for (chan = 0; chan < tx_channels_count; chan++) {
1816 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
1817
Jose Abreu52a76232017-10-13 10:58:36 +01001818 priv->hw->dma->dma_tx_mode(priv->ioaddr, txmode, chan,
Jose Abreua0daae12017-10-13 10:58:37 +01001819 txfifosz, qmode);
1820 }
Joao Pinto6deee222017-03-15 11:04:45 +00001821 } else {
1822 priv->hw->dma->dma_mode(priv->ioaddr, txmode, rxmode,
Vince Bridgersf88203a2015-04-15 11:17:42 -05001823 rxfifosz);
Joao Pinto6deee222017-03-15 11:04:45 +00001824 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001825}
1826
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001827/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001828 * stmmac_tx_clean - to manage the transmission completion
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001829 * @priv: driver private structure
Joao Pintoce736782017-04-06 09:49:10 +01001830 * @queue: TX queue index
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001831 * Description: it reclaims the transmit resources after transmission completes.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001832 */
Joao Pintoce736782017-04-06 09:49:10 +01001833static void stmmac_tx_clean(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001834{
Joao Pintoce736782017-04-06 09:49:10 +01001835 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Beniamino Galvani38979572015-01-21 19:07:27 +01001836 unsigned int bytes_compl = 0, pkts_compl = 0;
Bernd Edlinger8d5f4b02017-10-21 06:51:30 +00001837 unsigned int entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001838
Lino Sanfilippo739c8e12016-12-09 00:55:43 +01001839 netif_tx_lock(priv->dev);
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00001840
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00001841 priv->xstats.tx_clean++;
1842
Bernd Edlinger8d5f4b02017-10-21 06:51:30 +00001843 entry = tx_q->dirty_tx;
Joao Pintoce736782017-04-06 09:49:10 +01001844 while (entry != tx_q->cur_tx) {
1845 struct sk_buff *skb = tx_q->tx_skbuff[entry];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001846 struct dma_desc *p;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001847 int status;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001848
1849 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01001850 p = (struct dma_desc *)(tx_q->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001851 else
Joao Pintoce736782017-04-06 09:49:10 +01001852 p = tx_q->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001853
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001854 status = priv->hw->desc->tx_status(&priv->dev->stats,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00001855 &priv->xstats, p,
1856 priv->ioaddr);
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001857 /* Check if the descriptor is owned by the DMA */
1858 if (unlikely(status & tx_dma_own))
1859 break;
1860
Niklas Cassela6b25da2018-02-26 22:47:08 +01001861 /* Make sure descriptor fields are read after reading
1862 * the own bit.
1863 */
1864 dma_rmb();
1865
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001866 /* Just consider the last segment and ...*/
1867 if (likely(!(status & tx_not_ls))) {
1868 /* ... verify the status error condition */
1869 if (unlikely(status & tx_err)) {
1870 priv->dev->stats.tx_errors++;
1871 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001872 priv->dev->stats.tx_packets++;
1873 priv->xstats.tx_pkt_n++;
Fabrice Gasnierc363b652016-02-29 14:27:36 +01001874 }
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01001875 stmmac_get_tx_hwtstamp(priv, p, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001876 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001877
Joao Pintoce736782017-04-06 09:49:10 +01001878 if (likely(tx_q->tx_skbuff_dma[entry].buf)) {
1879 if (tx_q->tx_skbuff_dma[entry].map_as_page)
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001880 dma_unmap_page(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001881 tx_q->tx_skbuff_dma[entry].buf,
1882 tx_q->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001883 DMA_TO_DEVICE);
1884 else
1885 dma_unmap_single(priv->device,
Joao Pintoce736782017-04-06 09:49:10 +01001886 tx_q->tx_skbuff_dma[entry].buf,
1887 tx_q->tx_skbuff_dma[entry].len,
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02001888 DMA_TO_DEVICE);
Joao Pintoce736782017-04-06 09:49:10 +01001889 tx_q->tx_skbuff_dma[entry].buf = 0;
1890 tx_q->tx_skbuff_dma[entry].len = 0;
1891 tx_q->tx_skbuff_dma[entry].map_as_page = false;
Rayagond Kokatanurcf32dee2013-03-26 04:43:09 +00001892 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001893
1894 if (priv->hw->mode->clean_desc3)
Joao Pintoce736782017-04-06 09:49:10 +01001895 priv->hw->mode->clean_desc3(tx_q, p);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02001896
Joao Pintoce736782017-04-06 09:49:10 +01001897 tx_q->tx_skbuff_dma[entry].last_segment = false;
1898 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001899
1900 if (likely(skb != NULL)) {
Beniamino Galvani38979572015-01-21 19:07:27 +01001901 pkts_compl++;
1902 bytes_compl += skb->len;
Eric W. Biederman7c565c32014-03-15 18:11:09 -07001903 dev_consume_skb_any(skb);
Joao Pintoce736782017-04-06 09:49:10 +01001904 tx_q->tx_skbuff[entry] = NULL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001905 }
1906
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00001907 priv->hw->desc->release_tx_desc(p, priv->mode);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001908
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001909 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001910 }
Joao Pintoce736782017-04-06 09:49:10 +01001911 tx_q->dirty_tx = entry;
Beniamino Galvani38979572015-01-21 19:07:27 +01001912
Joao Pintoc22a3f42017-04-06 09:49:11 +01001913 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
1914 pkts_compl, bytes_compl);
Beniamino Galvani38979572015-01-21 19:07:27 +01001915
Joao Pintoc22a3f42017-04-06 09:49:11 +01001916 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
1917 queue))) &&
1918 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH) {
1919
Lino Sanfilippo739c8e12016-12-09 00:55:43 +01001920 netif_dbg(priv, tx_done, priv->dev,
1921 "%s: restart transmit\n", __func__);
Joao Pintoc22a3f42017-04-06 09:49:11 +01001922 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001923 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001924
1925 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1926 stmmac_enable_eee_mode(priv);
Giuseppe CAVALLAROf5351ef2013-06-18 07:03:23 +02001927 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00001928 }
Lino Sanfilippo739c8e12016-12-09 00:55:43 +01001929 netif_tx_unlock(priv->dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001930}
1931
Joao Pinto4f513ec2017-03-15 11:04:46 +00001932static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv, u32 chan)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001933{
Joao Pinto4f513ec2017-03-15 11:04:46 +00001934 priv->hw->dma->enable_dma_irq(priv->ioaddr, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001935}
1936
Joao Pinto4f513ec2017-03-15 11:04:46 +00001937static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv, u32 chan)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001938{
Joao Pinto4f513ec2017-03-15 11:04:46 +00001939 priv->hw->dma->disable_dma_irq(priv->ioaddr, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001940}
1941
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001942/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001943 * stmmac_tx_err - to manage the tx error
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001944 * @priv: driver private structure
LABBE Corentin5bacd772017-03-29 07:05:40 +02001945 * @chan: channel index
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001946 * Description: it cleans the descriptors and restarts the transmission
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01001947 * in case of transmission errors.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001948 */
LABBE Corentin5bacd772017-03-29 07:05:40 +02001949static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001950{
Joao Pintoce736782017-04-06 09:49:10 +01001951 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001952 int i;
Joao Pintoce736782017-04-06 09:49:10 +01001953
Joao Pintoc22a3f42017-04-06 09:49:11 +01001954 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001955
Joao Pintoae4f0d42017-03-15 11:04:47 +00001956 stmmac_stop_tx_dma(priv, chan);
Joao Pintoce736782017-04-06 09:49:10 +01001957 dma_free_tx_skbufs(priv, chan);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001958 for (i = 0; i < DMA_TX_SIZE; i++)
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001959 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01001960 priv->hw->desc->init_tx_desc(&tx_q->dma_etx[i].basic,
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001961 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001962 (i == DMA_TX_SIZE - 1));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001963 else
Joao Pintoce736782017-04-06 09:49:10 +01001964 priv->hw->desc->init_tx_desc(&tx_q->dma_tx[i],
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00001965 priv->mode,
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01001966 (i == DMA_TX_SIZE - 1));
Joao Pintoce736782017-04-06 09:49:10 +01001967 tx_q->dirty_tx = 0;
1968 tx_q->cur_tx = 0;
Niklas Cassel8d212a9e2018-02-19 18:11:09 +01001969 tx_q->mss = 0;
Joao Pintoc22a3f42017-04-06 09:49:11 +01001970 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
Joao Pintoae4f0d42017-03-15 11:04:47 +00001971 stmmac_start_tx_dma(priv, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001972
1973 priv->dev->stats.tx_errors++;
Joao Pintoc22a3f42017-04-06 09:49:11 +01001974 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07001975}
1976
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00001977/**
Joao Pinto6deee222017-03-15 11:04:45 +00001978 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
1979 * @priv: driver private structure
1980 * @txmode: TX operating mode
1981 * @rxmode: RX operating mode
1982 * @chan: channel index
1983 * Description: it is used for configuring of the DMA operation mode in
1984 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
1985 * mode.
1986 */
1987static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
1988 u32 rxmode, u32 chan)
1989{
Jose Abreua0daae12017-10-13 10:58:37 +01001990 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
1991 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
Jose Abreu52a76232017-10-13 10:58:36 +01001992 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1993 u32 tx_channels_count = priv->plat->tx_queues_to_use;
Joao Pinto6deee222017-03-15 11:04:45 +00001994 int rxfifosz = priv->plat->rx_fifo_size;
Jose Abreu52a76232017-10-13 10:58:36 +01001995 int txfifosz = priv->plat->tx_fifo_size;
Joao Pinto6deee222017-03-15 11:04:45 +00001996
1997 if (rxfifosz == 0)
1998 rxfifosz = priv->dma_cap.rx_fifo_size;
Jose Abreu52a76232017-10-13 10:58:36 +01001999 if (txfifosz == 0)
2000 txfifosz = priv->dma_cap.tx_fifo_size;
2001
2002 /* Adjust for real per queue fifo size */
2003 rxfifosz /= rx_channels_count;
2004 txfifosz /= tx_channels_count;
Joao Pinto6deee222017-03-15 11:04:45 +00002005
2006 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
2007 priv->hw->dma->dma_rx_mode(priv->ioaddr, rxmode, chan,
Jose Abreua0daae12017-10-13 10:58:37 +01002008 rxfifosz, rxqmode);
Jose Abreu52a76232017-10-13 10:58:36 +01002009 priv->hw->dma->dma_tx_mode(priv->ioaddr, txmode, chan,
Jose Abreua0daae12017-10-13 10:58:37 +01002010 txfifosz, txqmode);
Joao Pinto6deee222017-03-15 11:04:45 +00002011 } else {
2012 priv->hw->dma->dma_mode(priv->ioaddr, txmode, rxmode,
2013 rxfifosz);
2014 }
2015}
2016
2017/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002018 * stmmac_dma_interrupt - DMA ISR
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002019 * @priv: driver private structure
2020 * Description: this is the DMA ISR. It is called by the main ISR.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002021 * It calls the dwmac dma routine and schedule poll method in case of some
2022 * work can be done.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002023 */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00002024static void stmmac_dma_interrupt(struct stmmac_priv *priv)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002025{
Joao Pintod62a1072017-03-15 11:04:49 +00002026 u32 tx_channel_count = priv->plat->tx_queues_to_use;
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002027 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2028 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2029 tx_channel_count : rx_channel_count;
Joao Pintod62a1072017-03-15 11:04:49 +00002030 u32 chan;
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002031 bool poll_scheduled = false;
2032 int status[channels_to_check];
Joao Pinto68e5cfa2017-03-13 10:36:29 +00002033
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002034 /* Each DMA channel can be used for rx and tx simultaneously, yet
2035 * napi_struct is embedded in struct stmmac_rx_queue rather than in a
2036 * stmmac_channel struct.
2037 * Because of this, stmmac_poll currently checks (and possibly wakes)
2038 * all tx queues rather than just a single tx queue.
2039 */
2040 for (chan = 0; chan < channels_to_check; chan++)
2041 status[chan] = priv->hw->dma->dma_interrupt(priv->ioaddr,
2042 &priv->xstats,
2043 chan);
Joao Pintoc22a3f42017-04-06 09:49:11 +01002044
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002045 for (chan = 0; chan < rx_channel_count; chan++) {
2046 if (likely(status[chan] & handle_rx)) {
2047 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan];
2048
Joao Pintoc22a3f42017-04-06 09:49:11 +01002049 if (likely(napi_schedule_prep(&rx_q->napi))) {
Joao Pintod62a1072017-03-15 11:04:49 +00002050 stmmac_disable_dma_irq(priv, chan);
Joao Pintoc22a3f42017-04-06 09:49:11 +01002051 __napi_schedule(&rx_q->napi);
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002052 poll_scheduled = true;
Joao Pintod62a1072017-03-15 11:04:49 +00002053 }
2054 }
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002055 }
Joao Pintod62a1072017-03-15 11:04:49 +00002056
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002057 /* If we scheduled poll, we already know that tx queues will be checked.
2058 * If we didn't schedule poll, see if any DMA channel (used by tx) has a
2059 * completed transmission, if so, call stmmac_poll (once).
2060 */
2061 if (!poll_scheduled) {
2062 for (chan = 0; chan < tx_channel_count; chan++) {
2063 if (status[chan] & handle_tx) {
2064 /* It doesn't matter what rx queue we choose
2065 * here. We use 0 since it always exists.
2066 */
2067 struct stmmac_rx_queue *rx_q =
2068 &priv->rx_queue[0];
2069
2070 if (likely(napi_schedule_prep(&rx_q->napi))) {
2071 stmmac_disable_dma_irq(priv, chan);
2072 __napi_schedule(&rx_q->napi);
2073 }
2074 break;
2075 }
2076 }
2077 }
2078
2079 for (chan = 0; chan < tx_channel_count; chan++) {
2080 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
Joao Pintod62a1072017-03-15 11:04:49 +00002081 /* Try to bump up the dma threshold on this failure */
2082 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
2083 (tc <= 256)) {
2084 tc += 64;
2085 if (priv->plat->force_thresh_dma_mode)
2086 stmmac_set_dma_operation_mode(priv,
2087 tc,
2088 tc,
2089 chan);
2090 else
2091 stmmac_set_dma_operation_mode(priv,
2092 tc,
2093 SF_DMA_MODE,
2094 chan);
2095 priv->xstats.threshold = tc;
2096 }
Niklas Cassel5a6a0442017-12-07 23:56:10 +01002097 } else if (unlikely(status[chan] == tx_hard_error)) {
Joao Pintod62a1072017-03-15 11:04:49 +00002098 stmmac_tx_err(priv, chan);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002099 }
2100 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002101}
2102
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002103/**
2104 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2105 * @priv: driver private structure
2106 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2107 */
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00002108static void stmmac_mmc_setup(struct stmmac_priv *priv)
2109{
2110 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02002111 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00002112
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002113 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
2114 priv->ptpaddr = priv->ioaddr + PTP_GMAC4_OFFSET;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002115 priv->mmcaddr = priv->ioaddr + MMC_GMAC4_OFFSET;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002116 } else {
2117 priv->ptpaddr = priv->ioaddr + PTP_GMAC3_X_OFFSET;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002118 priv->mmcaddr = priv->ioaddr + MMC_GMAC3_X_OFFSET;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01002119 }
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02002120
2121 dwmac_mmc_intr_all_mask(priv->mmcaddr);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00002122
2123 if (priv->dma_cap.rmon) {
Alexandre TORGUE36ff7c12016-04-01 11:37:32 +02002124 dwmac_mmc_ctrl(priv->mmcaddr, mode);
Giuseppe CAVALLARO4f795b22011-11-18 05:00:20 +00002125 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2126 } else
LABBE Corentin38ddc592016-11-16 20:09:39 +01002127 netdev_info(priv->dev, "No MAC Management Counters available\n");
Giuseppe CAVALLARO1c901a42011-09-01 21:51:38 +00002128}
2129
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002130/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002131 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002132 * @priv: driver private structure
2133 * Description: select the Enhanced/Alternate or Normal descriptors.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002134 * In case of Enhanced/Alternate, it checks if the extended descriptors are
2135 * supported by the HW capability register.
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00002136 */
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002137static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
2138{
2139 if (priv->plat->enh_desc) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002140 dev_info(priv->device, "Enhanced/Alternate descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002141
2142 /* GMAC older than 3.50 has no extended descriptors */
2143 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002144 dev_info(priv->device, "Enabled extended descriptors\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002145 priv->extend_desc = 1;
2146 } else
LABBE Corentin38ddc592016-11-16 20:09:39 +01002147 dev_warn(priv->device, "Extended descriptors not supported\n");
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002148
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002149 priv->hw->desc = &enh_desc_ops;
2150 } else {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002151 dev_info(priv->device, "Normal descriptors\n");
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002152 priv->hw->desc = &ndesc_ops;
2153 }
2154}
2155
2156/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002157 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002158 * @priv: driver private structure
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002159 * Description:
2160 * new GMAC chip generations have a new register to indicate the
2161 * presence of the optional feature/functions.
2162 * This can be also used to override the value passed through the
2163 * platform and necessary for old MAC10/100 and GMAC chips.
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002164 */
2165static int stmmac_get_hw_features(struct stmmac_priv *priv)
2166{
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02002167 u32 ret = 0;
Giuseppe CAVALLARO3c20f722011-10-26 19:43:09 +00002168
Giuseppe CAVALLARO5e6efe82011-10-26 19:43:07 +00002169 if (priv->hw->dma->get_hw_feature) {
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02002170 priv->hw->dma->get_hw_feature(priv->ioaddr,
2171 &priv->dma_cap);
2172 ret = 1;
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00002173 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002174
Alexandre TORGUEf10a6a32016-04-01 11:37:25 +02002175 return ret;
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00002176}
2177
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002178/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002179 * stmmac_check_ether_addr - check if the MAC addr is valid
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002180 * @priv: driver private structure
2181 * Description:
2182 * it is to verify if the MAC address is valid, in case of failures it
2183 * generates a random MAC address
2184 */
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002185static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2186{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002187 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002188 priv->hw->mac->get_umac_addr(priv->hw,
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002189 priv->dev->dev_addr, 0);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002190 if (!is_valid_ether_addr(priv->dev->dev_addr))
Danny Kukawkaf2cedb62012-02-15 06:45:39 +00002191 eth_hw_addr_random(priv->dev);
LABBE Corentin38ddc592016-11-16 20:09:39 +01002192 netdev_info(priv->dev, "device MAC address %pM\n",
2193 priv->dev->dev_addr);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002194 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002195}
2196
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002197/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002198 * stmmac_init_dma_engine - DMA init.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002199 * @priv: driver private structure
2200 * Description:
2201 * It inits the DMA invoking the specific MAC/GMAC callback.
2202 * Some DMA parameters can be passed from the platform;
2203 * in case of these are not passed a default is kept for the MAC or GMAC.
2204 */
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002205static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2206{
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002207 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2208 u32 tx_channels_count = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01002209 struct stmmac_rx_queue *rx_q;
Joao Pintoce736782017-04-06 09:49:10 +01002210 struct stmmac_tx_queue *tx_q;
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002211 u32 dummy_dma_rx_phy = 0;
2212 u32 dummy_dma_tx_phy = 0;
2213 u32 chan = 0;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002214 int atds = 0;
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01002215 int ret = 0;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002216
Niklas Cassela332e2f2016-12-07 15:20:05 +01002217 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2218 dev_err(priv->device, "Invalid DMA configuration\n");
Niklas Cassel89ab75b2016-12-07 15:20:03 +01002219 return -EINVAL;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002220 }
2221
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00002222 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2223 atds = 1;
2224
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01002225 ret = priv->hw->dma->reset(priv->ioaddr);
2226 if (ret) {
2227 dev_err(priv->device, "Failed to reset the dma\n");
2228 return ret;
2229 }
2230
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002231 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002232 /* DMA Configuration */
2233 priv->hw->dma->init(priv->ioaddr, priv->plat->dma_cfg,
2234 dummy_dma_tx_phy, dummy_dma_rx_phy, atds);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002235
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002236 /* DMA RX Channel Configuration */
2237 for (chan = 0; chan < rx_channels_count; chan++) {
Joao Pinto54139cf2017-04-06 09:49:09 +01002238 rx_q = &priv->rx_queue[chan];
2239
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002240 priv->hw->dma->init_rx_chan(priv->ioaddr,
2241 priv->plat->dma_cfg,
Joao Pinto54139cf2017-04-06 09:49:09 +01002242 rx_q->dma_rx_phy, chan);
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002243
Joao Pinto54139cf2017-04-06 09:49:09 +01002244 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002245 (DMA_RX_SIZE * sizeof(struct dma_desc));
2246 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
Joao Pinto54139cf2017-04-06 09:49:09 +01002247 rx_q->rx_tail_addr,
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002248 chan);
2249 }
2250
2251 /* DMA TX Channel Configuration */
2252 for (chan = 0; chan < tx_channels_count; chan++) {
Joao Pintoce736782017-04-06 09:49:10 +01002253 tx_q = &priv->tx_queue[chan];
2254
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002255 priv->hw->dma->init_chan(priv->ioaddr,
Joao Pintoce736782017-04-06 09:49:10 +01002256 priv->plat->dma_cfg,
2257 chan);
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002258
2259 priv->hw->dma->init_tx_chan(priv->ioaddr,
2260 priv->plat->dma_cfg,
Joao Pintoce736782017-04-06 09:49:10 +01002261 tx_q->dma_tx_phy, chan);
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002262
Joao Pintoce736782017-04-06 09:49:10 +01002263 tx_q->tx_tail_addr = tx_q->dma_tx_phy +
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002264 (DMA_TX_SIZE * sizeof(struct dma_desc));
2265 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr,
Joao Pintoce736782017-04-06 09:49:10 +01002266 tx_q->tx_tail_addr,
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002267 chan);
2268 }
2269 } else {
Joao Pinto54139cf2017-04-06 09:49:09 +01002270 rx_q = &priv->rx_queue[chan];
Joao Pintoce736782017-04-06 09:49:10 +01002271 tx_q = &priv->tx_queue[chan];
Joao Pinto47f2a9c2017-03-15 11:04:53 +00002272 priv->hw->dma->init(priv->ioaddr, priv->plat->dma_cfg,
Joao Pintoce736782017-04-06 09:49:10 +01002273 tx_q->dma_tx_phy, rx_q->dma_rx_phy, atds);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002274 }
2275
2276 if (priv->plat->axi && priv->hw->dma->axi)
Giuseppe Cavallaroafea0362016-02-29 14:27:28 +01002277 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
2278
Giuseppe Cavallaro495db272016-02-29 14:27:27 +01002279 return ret;
Giuseppe CAVALLARO0f1f88a2012-04-18 19:48:21 +00002280}
2281
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002282/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002283 * stmmac_tx_timer - mitigation sw timer for tx.
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002284 * @data: data pointer
2285 * Description:
2286 * This is the timer handler to directly invoke the stmmac_tx_clean.
2287 */
Kees Cooke99e88a2017-10-16 14:43:17 -07002288static void stmmac_tx_timer(struct timer_list *t)
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002289{
Kees Cooke99e88a2017-10-16 14:43:17 -07002290 struct stmmac_priv *priv = from_timer(priv, t, txtimer);
Joao Pintoce736782017-04-06 09:49:10 +01002291 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2292 u32 queue;
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002293
Joao Pintoce736782017-04-06 09:49:10 +01002294 /* let's scan all the tx queues */
2295 for (queue = 0; queue < tx_queues_count; queue++)
2296 stmmac_tx_clean(priv, queue);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002297}
2298
2299/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002300 * stmmac_init_tx_coalesce - init tx mitigation options.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00002301 * @priv: driver private structure
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002302 * Description:
2303 * This inits the transmit coalesce parameters: i.e. timer rate,
2304 * timer handler and default threshold used for enabling the
2305 * interrupt on completion bit.
2306 */
2307static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
2308{
2309 priv->tx_coal_frames = STMMAC_TX_FRAMES;
2310 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
Kees Cooke99e88a2017-10-16 14:43:17 -07002311 timer_setup(&priv->txtimer, stmmac_tx_timer, 0);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002312 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002313 add_timer(&priv->txtimer);
2314}
2315
Joao Pinto4854ab92017-03-15 11:04:51 +00002316static void stmmac_set_rings_length(struct stmmac_priv *priv)
2317{
2318 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2319 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2320 u32 chan;
2321
2322 /* set TX ring length */
2323 if (priv->hw->dma->set_tx_ring_len) {
2324 for (chan = 0; chan < tx_channels_count; chan++)
2325 priv->hw->dma->set_tx_ring_len(priv->ioaddr,
2326 (DMA_TX_SIZE - 1), chan);
2327 }
2328
2329 /* set RX ring length */
2330 if (priv->hw->dma->set_rx_ring_len) {
2331 for (chan = 0; chan < rx_channels_count; chan++)
2332 priv->hw->dma->set_rx_ring_len(priv->ioaddr,
2333 (DMA_RX_SIZE - 1), chan);
2334 }
2335}
2336
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002337/**
Joao Pinto6a3a7192017-03-10 18:24:53 +00002338 * stmmac_set_tx_queue_weight - Set TX queue weight
2339 * @priv: driver private structure
2340 * Description: It is used for setting TX queues weight
2341 */
2342static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2343{
2344 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2345 u32 weight;
2346 u32 queue;
2347
2348 for (queue = 0; queue < tx_queues_count; queue++) {
2349 weight = priv->plat->tx_queues_cfg[queue].weight;
2350 priv->hw->mac->set_mtl_tx_queue_weight(priv->hw, weight, queue);
2351 }
2352}
2353
2354/**
Joao Pinto19d91872017-03-10 18:24:59 +00002355 * stmmac_configure_cbs - Configure CBS in TX queue
2356 * @priv: driver private structure
2357 * Description: It is used for configuring CBS in AVB TX queues
2358 */
2359static void stmmac_configure_cbs(struct stmmac_priv *priv)
2360{
2361 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2362 u32 mode_to_use;
2363 u32 queue;
2364
Joao Pinto44781fe2017-03-31 14:22:02 +01002365 /* queue 0 is reserved for legacy traffic */
2366 for (queue = 1; queue < tx_queues_count; queue++) {
Joao Pinto19d91872017-03-10 18:24:59 +00002367 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
2368 if (mode_to_use == MTL_QUEUE_DCB)
2369 continue;
2370
2371 priv->hw->mac->config_cbs(priv->hw,
2372 priv->plat->tx_queues_cfg[queue].send_slope,
2373 priv->plat->tx_queues_cfg[queue].idle_slope,
2374 priv->plat->tx_queues_cfg[queue].high_credit,
2375 priv->plat->tx_queues_cfg[queue].low_credit,
2376 queue);
2377 }
2378}
2379
2380/**
Joao Pintod43042f2017-03-10 18:24:55 +00002381 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
2382 * @priv: driver private structure
2383 * Description: It is used for mapping RX queues to RX dma channels
2384 */
2385static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
2386{
2387 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2388 u32 queue;
2389 u32 chan;
2390
2391 for (queue = 0; queue < rx_queues_count; queue++) {
2392 chan = priv->plat->rx_queues_cfg[queue].chan;
2393 priv->hw->mac->map_mtl_to_dma(priv->hw, queue, chan);
2394 }
2395}
2396
2397/**
Joao Pintoa8f51022017-03-17 16:11:06 +00002398 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
2399 * @priv: driver private structure
2400 * Description: It is used for configuring the RX Queue Priority
2401 */
2402static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
2403{
2404 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2405 u32 queue;
2406 u32 prio;
2407
2408 for (queue = 0; queue < rx_queues_count; queue++) {
2409 if (!priv->plat->rx_queues_cfg[queue].use_prio)
2410 continue;
2411
2412 prio = priv->plat->rx_queues_cfg[queue].prio;
2413 priv->hw->mac->rx_queue_prio(priv->hw, prio, queue);
2414 }
2415}
2416
2417/**
2418 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
2419 * @priv: driver private structure
2420 * Description: It is used for configuring the TX Queue Priority
2421 */
2422static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
2423{
2424 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2425 u32 queue;
2426 u32 prio;
2427
2428 for (queue = 0; queue < tx_queues_count; queue++) {
2429 if (!priv->plat->tx_queues_cfg[queue].use_prio)
2430 continue;
2431
2432 prio = priv->plat->tx_queues_cfg[queue].prio;
2433 priv->hw->mac->tx_queue_prio(priv->hw, prio, queue);
2434 }
2435}
2436
2437/**
Joao Pintoabe80fd2017-03-17 16:11:07 +00002438 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
2439 * @priv: driver private structure
2440 * Description: It is used for configuring the RX queue routing
2441 */
2442static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
2443{
2444 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2445 u32 queue;
2446 u8 packet;
2447
2448 for (queue = 0; queue < rx_queues_count; queue++) {
2449 /* no specific packet type routing specified for the queue */
2450 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
2451 continue;
2452
2453 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
Niklas Cassel13138de2018-02-19 18:11:13 +01002454 priv->hw->mac->rx_queue_routing(priv->hw, packet, queue);
Joao Pintoabe80fd2017-03-17 16:11:07 +00002455 }
2456}
2457
2458/**
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002459 * stmmac_mtl_configuration - Configure MTL
2460 * @priv: driver private structure
2461 * Description: It is used for configurring MTL
2462 */
2463static void stmmac_mtl_configuration(struct stmmac_priv *priv)
2464{
2465 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2466 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2467
Joao Pinto6a3a7192017-03-10 18:24:53 +00002468 if (tx_queues_count > 1 && priv->hw->mac->set_mtl_tx_queue_weight)
2469 stmmac_set_tx_queue_weight(priv);
2470
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002471 /* Configure MTL RX algorithms */
2472 if (rx_queues_count > 1 && priv->hw->mac->prog_mtl_rx_algorithms)
2473 priv->hw->mac->prog_mtl_rx_algorithms(priv->hw,
2474 priv->plat->rx_sched_algorithm);
2475
2476 /* Configure MTL TX algorithms */
2477 if (tx_queues_count > 1 && priv->hw->mac->prog_mtl_tx_algorithms)
2478 priv->hw->mac->prog_mtl_tx_algorithms(priv->hw,
2479 priv->plat->tx_sched_algorithm);
2480
Joao Pinto19d91872017-03-10 18:24:59 +00002481 /* Configure CBS in AVB TX queues */
2482 if (tx_queues_count > 1 && priv->hw->mac->config_cbs)
2483 stmmac_configure_cbs(priv);
2484
Joao Pintod43042f2017-03-10 18:24:55 +00002485 /* Map RX MTL to DMA channels */
Joao Pinto03cf65a2017-04-03 16:34:04 +01002486 if (priv->hw->mac->map_mtl_to_dma)
Joao Pintod43042f2017-03-10 18:24:55 +00002487 stmmac_rx_queue_dma_chan_map(priv);
2488
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002489 /* Enable MAC RX Queues */
Thierry Redingf3976872017-03-21 16:12:09 +01002490 if (priv->hw->mac->rx_queue_enable)
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002491 stmmac_mac_enable_rx_queues(priv);
Joao Pinto6deee222017-03-15 11:04:45 +00002492
Joao Pintoa8f51022017-03-17 16:11:06 +00002493 /* Set RX priorities */
2494 if (rx_queues_count > 1 && priv->hw->mac->rx_queue_prio)
2495 stmmac_mac_config_rx_queues_prio(priv);
2496
2497 /* Set TX priorities */
2498 if (tx_queues_count > 1 && priv->hw->mac->tx_queue_prio)
2499 stmmac_mac_config_tx_queues_prio(priv);
Joao Pintoabe80fd2017-03-17 16:11:07 +00002500
2501 /* Set RX routing */
2502 if (rx_queues_count > 1 && priv->hw->mac->rx_queue_routing)
2503 stmmac_mac_config_rx_queues_routing(priv);
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002504}
2505
2506/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002507 * stmmac_hw_setup - setup mac in a usable state.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002508 * @dev : pointer to the device structure.
2509 * Description:
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01002510 * this is the main function to setup the HW in a usable state because the
2511 * dma engine is reset, the core registers are configured (e.g. AXI,
2512 * Checksum features, timers). The DMA is ready to start receiving and
2513 * transmitting.
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002514 * Return value:
2515 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2516 * file on failure.
2517 */
Huacai Chenfe1319292014-12-19 22:38:18 +08002518static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002519{
2520 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto3c55d4d2017-03-15 11:04:50 +00002521 u32 rx_cnt = priv->plat->rx_queues_to_use;
Joao Pinto146617b2017-03-15 11:04:54 +00002522 u32 tx_cnt = priv->plat->tx_queues_to_use;
2523 u32 chan;
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002524 int ret;
2525
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002526 /* DMA initialization and SW reset */
2527 ret = stmmac_init_dma_engine(priv);
2528 if (ret < 0) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002529 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
2530 __func__);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002531 return ret;
2532 }
2533
2534 /* Copy the MAC addr into the HW */
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05002535 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002536
Giuseppe CAVALLARO02e57b92016-06-24 15:16:26 +02002537 /* PS and related bits will be programmed according to the speed */
2538 if (priv->hw->pcs) {
2539 int speed = priv->plat->mac_port_sel_speed;
2540
2541 if ((speed == SPEED_10) || (speed == SPEED_100) ||
2542 (speed == SPEED_1000)) {
2543 priv->hw->ps = speed;
2544 } else {
2545 dev_warn(priv->device, "invalid port speed\n");
2546 priv->hw->ps = 0;
2547 }
2548 }
2549
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002550 /* Initialize the MAC Core */
Florian Fainelli8cad4432018-01-18 15:12:21 -08002551 priv->hw->mac->core_init(priv->hw, dev);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002552
Joao Pintod0a9c9f2017-03-10 18:24:52 +00002553 /* Initialize MTL*/
2554 if (priv->synopsys_id >= DWMAC_CORE_4_00)
2555 stmmac_mtl_configuration(priv);
jpinto9eb12472016-12-28 12:57:48 +00002556
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02002557 ret = priv->hw->mac->rx_ipc(priv->hw);
2558 if (!ret) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002559 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02002560 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02002561 priv->hw->rx_csum = 0;
Giuseppe CAVALLARO978aded2014-08-25 14:56:18 +02002562 }
2563
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002564 /* Enable the MAC Rx/Tx */
LABBE Corentin270c7752017-03-23 14:40:22 +01002565 priv->hw->mac->set_mac(priv->ioaddr, true);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002566
Joao Pintob4f0a662017-03-22 11:56:05 +00002567 /* Set the HW DMA mode and the COE */
2568 stmmac_dma_operation_mode(priv);
2569
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002570 stmmac_mmc_setup(priv);
2571
Huacai Chenfe1319292014-12-19 22:38:18 +08002572 if (init_ptp) {
Thierry Reding0ad2be72017-03-10 17:34:56 +01002573 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
2574 if (ret < 0)
2575 netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret);
2576
Huacai Chenfe1319292014-12-19 22:38:18 +08002577 ret = stmmac_init_ptp(priv);
Heiner Kallweit722eef22017-02-01 22:02:02 +01002578 if (ret == -EOPNOTSUPP)
2579 netdev_warn(priv->dev, "PTP not supported by HW\n");
2580 else if (ret)
2581 netdev_warn(priv->dev, "PTP init failed\n");
Huacai Chenfe1319292014-12-19 22:38:18 +08002582 }
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002583
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002584#ifdef CONFIG_DEBUG_FS
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002585 ret = stmmac_init_fs(dev);
2586 if (ret < 0)
LABBE Corentin38ddc592016-11-16 20:09:39 +01002587 netdev_warn(priv->dev, "%s: failed debugFS registration\n",
2588 __func__);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002589#endif
2590 /* Start the ball rolling... */
Joao Pintoae4f0d42017-03-15 11:04:47 +00002591 stmmac_start_all_dma(priv);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002592
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002593 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
2594
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002595 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
2596 priv->rx_riwt = MAX_DMA_RIWT;
Joao Pinto3c55d4d2017-03-15 11:04:50 +00002597 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT, rx_cnt);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002598 }
2599
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02002600 if (priv->hw->pcs && priv->hw->mac->pcs_ctrl_ane)
Giuseppe CAVALLARO02e57b92016-06-24 15:16:26 +02002601 priv->hw->mac->pcs_ctrl_ane(priv->hw, 1, priv->hw->ps, 0);
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002602
Joao Pinto4854ab92017-03-15 11:04:51 +00002603 /* set TX and RX rings length */
2604 stmmac_set_rings_length(priv);
2605
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002606 /* Enable TSO */
Joao Pinto146617b2017-03-15 11:04:54 +00002607 if (priv->tso) {
2608 for (chan = 0; chan < tx_cnt; chan++)
2609 priv->hw->dma->enable_tso(priv->ioaddr, 1, chan);
2610 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002611
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002612 return 0;
2613}
2614
Thierry Redingc66f6c32017-03-10 17:34:55 +01002615static void stmmac_hw_teardown(struct net_device *dev)
2616{
2617 struct stmmac_priv *priv = netdev_priv(dev);
2618
2619 clk_disable_unprepare(priv->plat->clk_ptp_ref);
2620}
2621
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002622/**
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002623 * stmmac_open - open entry point of the driver
2624 * @dev : pointer to the device structure.
2625 * Description:
2626 * This function is the open entry point of the driver.
2627 * Return value:
2628 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2629 * file on failure.
2630 */
2631static int stmmac_open(struct net_device *dev)
2632{
2633 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002634 int ret;
2635
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002636 stmmac_check_ether_addr(priv);
2637
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02002638 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
2639 priv->hw->pcs != STMMAC_PCS_TBI &&
2640 priv->hw->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002641 ret = stmmac_init_phy(dev);
2642 if (ret) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002643 netdev_err(priv->dev,
2644 "%s: Cannot attach to PHY (error: %d)\n",
2645 __func__, ret);
Hans de Goede89df20d2014-05-20 11:38:18 +02002646 return ret;
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00002647 }
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002648 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002649
Srinivas Kandagatla523f11b2014-01-16 10:52:14 +00002650 /* Extra statistics */
2651 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
2652 priv->xstats.threshold = tc;
2653
LABBE Corentin5bacd772017-03-29 07:05:40 +02002654 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01002655 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02002656
LABBE Corentin5bacd772017-03-29 07:05:40 +02002657 ret = alloc_dma_desc_resources(priv);
2658 if (ret < 0) {
2659 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
2660 __func__);
2661 goto dma_desc_error;
2662 }
2663
2664 ret = init_dma_desc_rings(dev, GFP_KERNEL);
2665 if (ret < 0) {
2666 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
2667 __func__);
2668 goto init_error;
2669 }
2670
Huacai Chenfe1319292014-12-19 22:38:18 +08002671 ret = stmmac_hw_setup(dev, true);
Bartlomiej Zolnierkiewicz56329132013-08-09 14:02:08 +02002672 if (ret < 0) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002673 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002674 goto init_error;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002675 }
2676
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01002677 stmmac_init_tx_coalesce(priv);
2678
Philippe Reynesd6d50c72016-10-03 08:28:19 +02002679 if (dev->phydev)
2680 phy_start(dev->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002681
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002682 /* Request the IRQ lines */
2683 ret = request_irq(dev->irq, stmmac_interrupt,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00002684 IRQF_SHARED, dev->name, dev);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002685 if (unlikely(ret < 0)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002686 netdev_err(priv->dev,
2687 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
2688 __func__, dev->irq, ret);
Thierry Reding6c1e5ab2017-03-10 17:34:54 +01002689 goto irq_error;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002690 }
2691
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002692 /* Request the Wake IRQ in case of another line is used for WoL */
2693 if (priv->wol_irq != dev->irq) {
2694 ret = request_irq(priv->wol_irq, stmmac_interrupt,
2695 IRQF_SHARED, dev->name, dev);
2696 if (unlikely(ret < 0)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002697 netdev_err(priv->dev,
2698 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
2699 __func__, priv->wol_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002700 goto wolirq_error;
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002701 }
2702 }
2703
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002704 /* Request the IRQ lines */
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08002705 if (priv->lpi_irq > 0) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002706 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
2707 dev->name, dev);
2708 if (unlikely(ret < 0)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01002709 netdev_err(priv->dev,
2710 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
2711 __func__, priv->lpi_irq, ret);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002712 goto lpiirq_error;
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002713 }
2714 }
2715
Joao Pintoc22a3f42017-04-06 09:49:11 +01002716 stmmac_enable_all_queues(priv);
2717 stmmac_start_all_queues(priv);
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002718
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002719 return 0;
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002720
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002721lpiirq_error:
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002722 if (priv->wol_irq != dev->irq)
2723 free_irq(priv->wol_irq, dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002724wolirq_error:
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002725 free_irq(dev->irq, dev);
Thierry Reding6c1e5ab2017-03-10 17:34:54 +01002726irq_error:
2727 if (dev->phydev)
2728 phy_stop(dev->phydev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002729
Thierry Reding6c1e5ab2017-03-10 17:34:54 +01002730 del_timer_sync(&priv->txtimer);
Thierry Redingc66f6c32017-03-10 17:34:55 +01002731 stmmac_hw_teardown(dev);
Giuseppe CAVALLAROc9324d12013-07-04 06:18:07 +02002732init_error:
2733 free_dma_desc_resources(priv);
LABBE Corentin5bacd772017-03-29 07:05:40 +02002734dma_desc_error:
Philippe Reynesd6d50c72016-10-03 08:28:19 +02002735 if (dev->phydev)
2736 phy_disconnect(dev->phydev);
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00002737
Giuseppe CAVALLAROf66ffe22011-04-10 23:16:45 +00002738 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002739}
2740
2741/**
2742 * stmmac_release - close entry point of the driver
2743 * @dev : device pointer.
2744 * Description:
2745 * This is the stop entry point of the driver.
2746 */
2747static int stmmac_release(struct net_device *dev)
2748{
2749 struct stmmac_priv *priv = netdev_priv(dev);
2750
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002751 if (priv->eee_enabled)
2752 del_timer_sync(&priv->eee_ctrl_timer);
2753
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002754 /* Stop and disconnect the PHY */
Philippe Reynesd6d50c72016-10-03 08:28:19 +02002755 if (dev->phydev) {
2756 phy_stop(dev->phydev);
2757 phy_disconnect(dev->phydev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002758 }
2759
Joao Pintoc22a3f42017-04-06 09:49:11 +01002760 stmmac_stop_all_queues(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002761
Joao Pintoc22a3f42017-04-06 09:49:11 +01002762 stmmac_disable_all_queues(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002763
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00002764 del_timer_sync(&priv->txtimer);
2765
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002766 /* Free the IRQ lines */
2767 free_irq(dev->irq, dev);
Francesco Virlinzi7a13f8f2012-02-15 00:10:38 +00002768 if (priv->wol_irq != dev->irq)
2769 free_irq(priv->wol_irq, dev);
Chen-Yu Tsaid7ec8582014-05-29 22:31:40 +08002770 if (priv->lpi_irq > 0)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00002771 free_irq(priv->lpi_irq, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002772
2773 /* Stop TX/RX DMA and clear the descriptors */
Joao Pintoae4f0d42017-03-15 11:04:47 +00002774 stmmac_stop_all_dma(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002775
2776 /* Release and free the Rx/Tx resources */
2777 free_dma_desc_resources(priv);
2778
avisconti19449bf2010-10-25 18:58:14 +00002779 /* Disable the MAC Rx/Tx */
LABBE Corentin270c7752017-03-23 14:40:22 +01002780 priv->hw->mac->set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002781
2782 netif_carrier_off(dev);
2783
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01002784#ifdef CONFIG_DEBUG_FS
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07002785 stmmac_exit_fs(dev);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002786#endif
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00002787
Rayagond Kokatanur92ba6882013-03-26 04:43:11 +00002788 stmmac_release_ptp(priv);
2789
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002790 return 0;
2791}
2792
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07002793/**
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002794 * stmmac_tso_allocator - close entry point of the driver
2795 * @priv: driver private structure
2796 * @des: buffer start address
2797 * @total_len: total length to fill in descriptors
2798 * @last_segmant: condition for the last descriptor
Joao Pintoce736782017-04-06 09:49:10 +01002799 * @queue: TX queue index
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002800 * Description:
2801 * This function fills descriptor and request new descriptors according to
2802 * buffer length to fill
2803 */
2804static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
Joao Pintoce736782017-04-06 09:49:10 +01002805 int total_len, bool last_segment, u32 queue)
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002806{
Joao Pintoce736782017-04-06 09:49:10 +01002807 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002808 struct dma_desc *desc;
LABBE Corentin5bacd772017-03-29 07:05:40 +02002809 u32 buff_size;
Joao Pintoce736782017-04-06 09:49:10 +01002810 int tmp_len;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002811
2812 tmp_len = total_len;
2813
2814 while (tmp_len > 0) {
Joao Pintoce736782017-04-06 09:49:10 +01002815 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
Niklas Casselb4c97842018-02-19 18:11:11 +01002816 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
Joao Pintoce736782017-04-06 09:49:10 +01002817 desc = tx_q->dma_tx + tx_q->cur_tx;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002818
Michael Weiserf8be0d72016-11-14 18:58:05 +01002819 desc->des0 = cpu_to_le32(des + (total_len - tmp_len));
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002820 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
2821 TSO_MAX_BUFF_SIZE : tmp_len;
2822
2823 priv->hw->desc->prepare_tso_tx_desc(desc, 0, buff_size,
2824 0, 1,
Niklas Cassel426849e2017-06-06 09:25:00 +02002825 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002826 0, 0);
2827
2828 tmp_len -= TSO_MAX_BUFF_SIZE;
2829 }
2830}
2831
2832/**
2833 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
2834 * @skb : the socket buffer
2835 * @dev : device pointer
2836 * Description: this is the transmit function that is called on TSO frames
2837 * (support available on GMAC4 and newer chips).
2838 * Diagram below show the ring programming in case of TSO frames:
2839 *
2840 * First Descriptor
2841 * --------
2842 * | DES0 |---> buffer1 = L2/L3/L4 header
2843 * | DES1 |---> TCP Payload (can continue on next descr...)
2844 * | DES2 |---> buffer 1 and 2 len
2845 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
2846 * --------
2847 * |
2848 * ...
2849 * |
2850 * --------
2851 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
2852 * | DES1 | --|
2853 * | DES2 | --> buffer 1 and 2 len
2854 * | DES3 |
2855 * --------
2856 *
2857 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
2858 */
2859static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
2860{
Joao Pintoce736782017-04-06 09:49:10 +01002861 struct dma_desc *desc, *first, *mss_desc = NULL;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002862 struct stmmac_priv *priv = netdev_priv(dev);
2863 int nfrags = skb_shinfo(skb)->nr_frags;
Joao Pintoce736782017-04-06 09:49:10 +01002864 u32 queue = skb_get_queue_mapping(skb);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002865 unsigned int first_entry, des;
Joao Pintoce736782017-04-06 09:49:10 +01002866 struct stmmac_tx_queue *tx_q;
2867 int tmp_pay_len = 0;
2868 u32 pay_len, mss;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002869 u8 proto_hdr_len;
2870 int i;
2871
Joao Pintoce736782017-04-06 09:49:10 +01002872 tx_q = &priv->tx_queue[queue];
2873
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002874 /* Compute header lengths */
2875 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2876
2877 /* Desc availability based on threshold should be enough safe */
Joao Pintoce736782017-04-06 09:49:10 +01002878 if (unlikely(stmmac_tx_avail(priv, queue) <
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002879 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
Joao Pintoc22a3f42017-04-06 09:49:11 +01002880 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
2881 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
2882 queue));
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002883 /* This is a hard error, log it. */
LABBE Corentin38ddc592016-11-16 20:09:39 +01002884 netdev_err(priv->dev,
2885 "%s: Tx Ring full when queue awake\n",
2886 __func__);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002887 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002888 return NETDEV_TX_BUSY;
2889 }
2890
2891 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
2892
2893 mss = skb_shinfo(skb)->gso_size;
2894
2895 /* set new MSS value if needed */
Niklas Cassel8d212a9e2018-02-19 18:11:09 +01002896 if (mss != tx_q->mss) {
Joao Pintoce736782017-04-06 09:49:10 +01002897 mss_desc = tx_q->dma_tx + tx_q->cur_tx;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002898 priv->hw->desc->set_mss(mss_desc, mss);
Niklas Cassel8d212a9e2018-02-19 18:11:09 +01002899 tx_q->mss = mss;
Joao Pintoce736782017-04-06 09:49:10 +01002900 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
Niklas Casselb4c97842018-02-19 18:11:11 +01002901 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002902 }
2903
2904 if (netif_msg_tx_queued(priv)) {
2905 pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
2906 __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
2907 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
2908 skb->data_len);
2909 }
2910
Joao Pintoce736782017-04-06 09:49:10 +01002911 first_entry = tx_q->cur_tx;
Niklas Casselb4c97842018-02-19 18:11:11 +01002912 WARN_ON(tx_q->tx_skbuff[first_entry]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002913
Joao Pintoce736782017-04-06 09:49:10 +01002914 desc = tx_q->dma_tx + first_entry;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002915 first = desc;
2916
2917 /* first descriptor: fill Headers on Buf1 */
2918 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
2919 DMA_TO_DEVICE);
2920 if (dma_mapping_error(priv->device, des))
2921 goto dma_map_err;
2922
Joao Pintoce736782017-04-06 09:49:10 +01002923 tx_q->tx_skbuff_dma[first_entry].buf = des;
2924 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002925
Michael Weiserf8be0d72016-11-14 18:58:05 +01002926 first->des0 = cpu_to_le32(des);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002927
2928 /* Fill start of payload in buff2 of first descriptor */
2929 if (pay_len)
Michael Weiserf8be0d72016-11-14 18:58:05 +01002930 first->des1 = cpu_to_le32(des + proto_hdr_len);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002931
2932 /* If needed take extra descriptors to fill the remaining payload */
2933 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
2934
Joao Pintoce736782017-04-06 09:49:10 +01002935 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002936
2937 /* Prepare fragments */
2938 for (i = 0; i < nfrags; i++) {
2939 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2940
2941 des = skb_frag_dma_map(priv->device, frag, 0,
2942 skb_frag_size(frag),
2943 DMA_TO_DEVICE);
Thierry Reding937071c2017-03-10 17:34:57 +01002944 if (dma_mapping_error(priv->device, des))
2945 goto dma_map_err;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002946
2947 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
Joao Pintoce736782017-04-06 09:49:10 +01002948 (i == nfrags - 1), queue);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002949
Joao Pintoce736782017-04-06 09:49:10 +01002950 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
2951 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
Joao Pintoce736782017-04-06 09:49:10 +01002952 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002953 }
2954
Joao Pintoce736782017-04-06 09:49:10 +01002955 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002956
Niklas Cassel05cf0d12017-06-20 14:32:41 +02002957 /* Only the last descriptor gets to point to the skb. */
2958 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
2959
2960 /* We've used all descriptors we need for this skb, however,
2961 * advance cur_tx so that it references a fresh descriptor.
2962 * ndo_start_xmit will fill this descriptor the next time it's
2963 * called and stmmac_tx_clean may clean up to this descriptor.
2964 */
Joao Pintoce736782017-04-06 09:49:10 +01002965 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002966
Joao Pintoce736782017-04-06 09:49:10 +01002967 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
LABBE Corentinb3e51062016-11-16 20:09:41 +01002968 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
2969 __func__);
Joao Pintoc22a3f42017-04-06 09:49:11 +01002970 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002971 }
2972
2973 dev->stats.tx_bytes += skb->len;
2974 priv->xstats.tx_tso_frames++;
2975 priv->xstats.tx_tso_nfrags += nfrags;
2976
2977 /* Manage tx mitigation */
2978 priv->tx_count_frames += nfrags + 1;
2979 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
2980 mod_timer(&priv->txtimer,
2981 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2982 } else {
2983 priv->tx_count_frames = 0;
2984 priv->hw->desc->set_tx_ic(desc);
2985 priv->xstats.tx_set_ic_bit++;
2986 }
2987
Miroslav Lichvar74abc9b12017-05-19 17:52:41 +02002988 skb_tx_timestamp(skb);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02002989
2990 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2991 priv->hwts_tx_en)) {
2992 /* declare that device is doing timestamping */
2993 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2994 priv->hw->desc->enable_tx_timestamp(first);
2995 }
2996
2997 /* Complete the first descriptor before granting the DMA */
2998 priv->hw->desc->prepare_tso_tx_desc(first, 1,
2999 proto_hdr_len,
3000 pay_len,
Joao Pintoce736782017-04-06 09:49:10 +01003001 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003002 tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
3003
3004 /* If context desc is used to change MSS */
Niklas Cassel15d2ee42018-02-26 22:47:06 +01003005 if (mss_desc) {
3006 /* Make sure that first descriptor has been completely
3007 * written, including its own bit. This is because MSS is
3008 * actually before first descriptor, so we need to make
3009 * sure that MSS's own bit is the last thing written.
3010 */
3011 dma_wmb();
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003012 priv->hw->desc->set_tx_owner(mss_desc);
Niklas Cassel15d2ee42018-02-26 22:47:06 +01003013 }
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003014
3015 /* The own bit must be the latest setting done when prepare the
3016 * descriptor and then barrier is needed to make sure that
3017 * all is coherent before granting the DMA engine.
3018 */
Niklas Cassel95eb9302018-02-26 22:47:07 +01003019 wmb();
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003020
3021 if (netif_msg_pktdata(priv)) {
3022 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
Joao Pintoce736782017-04-06 09:49:10 +01003023 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
3024 tx_q->cur_tx, first, nfrags);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003025
Joao Pintoce736782017-04-06 09:49:10 +01003026 priv->hw->desc->display_ring((void *)tx_q->dma_tx, DMA_TX_SIZE,
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003027 0);
3028
3029 pr_info(">>> frame to be transmitted: ");
3030 print_pkt(skb->data, skb_headlen(skb));
3031 }
3032
Joao Pintoc22a3f42017-04-06 09:49:11 +01003033 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003034
Joao Pintoce736782017-04-06 09:49:10 +01003035 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, tx_q->tx_tail_addr,
3036 queue);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003037
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003038 return NETDEV_TX_OK;
3039
3040dma_map_err:
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003041 dev_err(priv->device, "Tx dma map failed\n");
3042 dev_kfree_skb(skb);
3043 priv->dev->stats.tx_dropped++;
3044 return NETDEV_TX_OK;
3045}
3046
3047/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003048 * stmmac_xmit - Tx entry point of the driver
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003049 * @skb : the socket buffer
3050 * @dev : device pointer
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003051 * Description : this is the tx entry point of the driver.
3052 * It programs the chain or the ring and supports oversized frames
3053 * and SG feature.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003054 */
3055static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
3056{
3057 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003058 unsigned int nopaged_len = skb_headlen(skb);
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003059 int i, csum_insertion = 0, is_jumbo = 0;
Joao Pintoce736782017-04-06 09:49:10 +01003060 u32 queue = skb_get_queue_mapping(skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003061 int nfrags = skb_shinfo(skb)->nr_frags;
Colin Ian King59423812017-06-05 10:04:52 +01003062 int entry;
3063 unsigned int first_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003064 struct dma_desc *desc, *first;
Joao Pintoce736782017-04-06 09:49:10 +01003065 struct stmmac_tx_queue *tx_q;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003066 unsigned int enh_desc;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003067 unsigned int des;
3068
Joao Pintoce736782017-04-06 09:49:10 +01003069 tx_q = &priv->tx_queue[queue];
3070
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003071 /* Manage oversized TCP frames for GMAC4 device */
3072 if (skb_is_gso(skb) && priv->tso) {
Niklas Cassel9edfa7d2017-06-19 18:36:44 +02003073 if (skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003074 return stmmac_tso_xmit(skb, dev);
3075 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003076
Joao Pintoce736782017-04-06 09:49:10 +01003077 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
Joao Pintoc22a3f42017-04-06 09:49:11 +01003078 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3079 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3080 queue));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003081 /* This is a hard error, log it. */
LABBE Corentin38ddc592016-11-16 20:09:39 +01003082 netdev_err(priv->dev,
3083 "%s: Tx Ring full when queue awake\n",
3084 __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003085 }
3086 return NETDEV_TX_BUSY;
3087 }
3088
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003089 if (priv->tx_path_in_lpi_mode)
3090 stmmac_disable_eee_mode(priv);
3091
Joao Pintoce736782017-04-06 09:49:10 +01003092 entry = tx_q->cur_tx;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003093 first_entry = entry;
Niklas Casselb4c97842018-02-19 18:11:11 +01003094 WARN_ON(tx_q->tx_skbuff[first_entry]);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003095
Michał Mirosław5e982f32011-04-09 02:46:55 +00003096 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003097
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003098 if (likely(priv->extend_desc))
Joao Pintoce736782017-04-06 09:49:10 +01003099 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003100 else
Joao Pintoce736782017-04-06 09:49:10 +01003101 desc = tx_q->dma_tx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003102
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003103 first = desc;
3104
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003105 enh_desc = priv->plat->enh_desc;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003106 /* To program the descriptors according to the size of the frame */
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01003107 if (enh_desc)
3108 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
3109
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003110 if (unlikely(is_jumbo) && likely(priv->synopsys_id <
3111 DWMAC_CORE_4_00)) {
Joao Pintoce736782017-04-06 09:49:10 +01003112 entry = priv->hw->mode->jumbo_frm(tx_q, skb, csum_insertion);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003113 if (unlikely(entry < 0))
3114 goto dma_map_err;
Giuseppe CAVALLARO29896a62014-03-10 13:40:33 +01003115 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003116
3117 for (i = 0; i < nfrags; i++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +00003118 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3119 int len = skb_frag_size(frag);
Giuseppe Cavallarobe434d52016-02-29 14:27:35 +01003120 bool last_segment = (i == (nfrags - 1));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003121
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003122 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
Niklas Casselb4c97842018-02-19 18:11:11 +01003123 WARN_ON(tx_q->tx_skbuff[entry]);
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003124
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003125 if (likely(priv->extend_desc))
Joao Pintoce736782017-04-06 09:49:10 +01003126 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003127 else
Joao Pintoce736782017-04-06 09:49:10 +01003128 desc = tx_q->dma_tx + entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003129
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003130 des = skb_frag_dma_map(priv->device, frag, 0, len,
3131 DMA_TO_DEVICE);
3132 if (dma_mapping_error(priv->device, des))
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003133 goto dma_map_err; /* should reuse desc w/o issues */
3134
Joao Pintoce736782017-04-06 09:49:10 +01003135 tx_q->tx_skbuff_dma[entry].buf = des;
Michael Weiserf8be0d72016-11-14 18:58:05 +01003136 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
3137 desc->des0 = cpu_to_le32(des);
3138 else
3139 desc->des2 = cpu_to_le32(des);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003140
Joao Pintoce736782017-04-06 09:49:10 +01003141 tx_q->tx_skbuff_dma[entry].map_as_page = true;
3142 tx_q->tx_skbuff_dma[entry].len = len;
3143 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003144
3145 /* Prepare the descriptor and set the own bit too */
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00003146 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
Niklas Casselfe6af0e2017-04-10 20:33:29 +02003147 priv->mode, 1, last_segment,
3148 skb->len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003149 }
3150
Niklas Cassel05cf0d12017-06-20 14:32:41 +02003151 /* Only the last descriptor gets to point to the skb. */
3152 tx_q->tx_skbuff[entry] = skb;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003153
Niklas Cassel05cf0d12017-06-20 14:32:41 +02003154 /* We've used all descriptors we need for this skb, however,
3155 * advance cur_tx so that it references a fresh descriptor.
3156 * ndo_start_xmit will fill this descriptor the next time it's
3157 * called and stmmac_tx_clean may clean up to this descriptor.
3158 */
3159 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
Joao Pintoce736782017-04-06 09:49:10 +01003160 tx_q->cur_tx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003161
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003162 if (netif_msg_pktdata(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02003163 void *tx_head;
3164
LABBE Corentin38ddc592016-11-16 20:09:39 +01003165 netdev_dbg(priv->dev,
3166 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
Joao Pintoce736782017-04-06 09:49:10 +01003167 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
LABBE Corentin38ddc592016-11-16 20:09:39 +01003168 entry, first, nfrags);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003169
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003170 if (priv->extend_desc)
Joao Pintoce736782017-04-06 09:49:10 +01003171 tx_head = (void *)tx_q->dma_etx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003172 else
Joao Pintoce736782017-04-06 09:49:10 +01003173 tx_head = (void *)tx_q->dma_tx;
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02003174
3175 priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003176
LABBE Corentin38ddc592016-11-16 20:09:39 +01003177 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003178 print_pkt(skb->data, skb->len);
3179 }
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003180
Joao Pintoce736782017-04-06 09:49:10 +01003181 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
LABBE Corentinb3e51062016-11-16 20:09:41 +01003182 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
3183 __func__);
Joao Pintoc22a3f42017-04-06 09:49:11 +01003184 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003185 }
3186
3187 dev->stats.tx_bytes += skb->len;
3188
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003189 /* According to the coalesce parameter the IC bit for the latest
3190 * segment is reset and the timer re-started to clean the tx status.
3191 * This approach takes care about the fragments: desc is the first
3192 * element in case of no SG.
3193 */
3194 priv->tx_count_frames += nfrags + 1;
3195 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
3196 mod_timer(&priv->txtimer,
3197 STMMAC_COAL_TIMER(priv->tx_coal_timer));
3198 } else {
3199 priv->tx_count_frames = 0;
3200 priv->hw->desc->set_tx_ic(desc);
3201 priv->xstats.tx_set_ic_bit++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003202 }
3203
Miroslav Lichvar74abc9b12017-05-19 17:52:41 +02003204 skb_tx_timestamp(skb);
Richard Cochran3e82ce12011-06-12 02:19:06 +00003205
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003206 /* Ready to fill the first descriptor and set the OWN bit w/o any
3207 * problems because all the descriptors are actually ready to be
3208 * passed to the DMA engine.
3209 */
3210 if (likely(!is_jumbo)) {
3211 bool last_segment = (nfrags == 0);
3212
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003213 des = dma_map_single(priv->device, skb->data,
3214 nopaged_len, DMA_TO_DEVICE);
3215 if (dma_mapping_error(priv->device, des))
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003216 goto dma_map_err;
3217
Joao Pintoce736782017-04-06 09:49:10 +01003218 tx_q->tx_skbuff_dma[first_entry].buf = des;
Michael Weiserf8be0d72016-11-14 18:58:05 +01003219 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
3220 first->des0 = cpu_to_le32(des);
3221 else
3222 first->des2 = cpu_to_le32(des);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003223
Joao Pintoce736782017-04-06 09:49:10 +01003224 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
3225 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003226
3227 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3228 priv->hwts_tx_en)) {
3229 /* declare that device is doing timestamping */
3230 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3231 priv->hw->desc->enable_tx_timestamp(first);
3232 }
3233
3234 /* Prepare the first descriptor setting the OWN bit too */
3235 priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
3236 csum_insertion, priv->mode, 1,
Niklas Casselfe6af0e2017-04-10 20:33:29 +02003237 last_segment, skb->len);
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003238
3239 /* The own bit must be the latest setting done when prepare the
3240 * descriptor and then barrier is needed to make sure that
3241 * all is coherent before granting the DMA engine.
3242 */
Niklas Cassel95eb9302018-02-26 22:47:07 +01003243 wmb();
Giuseppe Cavallaro0e80bdc2016-02-29 14:27:38 +01003244 }
3245
Joao Pintoc22a3f42017-04-06 09:49:11 +01003246 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003247
3248 if (priv->synopsys_id < DWMAC_CORE_4_00)
3249 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
3250 else
Joao Pintoce736782017-04-06 09:49:10 +01003251 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, tx_q->tx_tail_addr,
3252 queue);
Richard Cochran52f64fa2011-06-19 03:31:43 +00003253
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003254 return NETDEV_TX_OK;
Giuseppe CAVALLAROa9097a92011-10-18 00:01:19 +00003255
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003256dma_map_err:
LABBE Corentin38ddc592016-11-16 20:09:39 +01003257 netdev_err(priv->dev, "Tx DMA map failed\n");
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003258 dev_kfree_skb(skb);
3259 priv->dev->stats.tx_dropped++;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003260 return NETDEV_TX_OK;
3261}
3262
Vince Bridgersb9381982014-01-14 13:42:05 -06003263static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
3264{
3265 struct ethhdr *ehdr;
3266 u16 vlanid;
3267
3268 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
3269 NETIF_F_HW_VLAN_CTAG_RX &&
3270 !__vlan_get_tag(skb, &vlanid)) {
3271 /* pop the vlan tag */
3272 ehdr = (struct ethhdr *)skb->data;
3273 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
3274 skb_pull(skb, VLAN_HLEN);
3275 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
3276 }
3277}
3278
3279
Joao Pinto54139cf2017-04-06 09:49:09 +01003280static inline int stmmac_rx_threshold_count(struct stmmac_rx_queue *rx_q)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003281{
Joao Pinto54139cf2017-04-06 09:49:09 +01003282 if (rx_q->rx_zeroc_thresh < STMMAC_RX_THRESH)
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003283 return 0;
3284
3285 return 1;
3286}
3287
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003288/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003289 * stmmac_rx_refill - refill used skb preallocated buffers
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003290 * @priv: driver private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01003291 * @queue: RX queue index
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003292 * Description : this is to reallocate the skb for the reception process
3293 * that is based on zero-copy.
3294 */
Joao Pinto54139cf2017-04-06 09:49:09 +01003295static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003296{
Joao Pinto54139cf2017-04-06 09:49:09 +01003297 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3298 int dirty = stmmac_rx_dirty(priv, queue);
3299 unsigned int entry = rx_q->dirty_rx;
3300
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003301 int bfsize = priv->dma_buf_sz;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003302
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003303 while (dirty-- > 0) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003304 struct dma_desc *p;
3305
3306 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003307 p = (struct dma_desc *)(rx_q->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003308 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003309 p = rx_q->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003310
Joao Pinto54139cf2017-04-06 09:49:09 +01003311 if (likely(!rx_q->rx_skbuff[entry])) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003312 struct sk_buff *skb;
3313
Eric Dumazetacb600d2012-10-05 06:23:55 +00003314 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003315 if (unlikely(!skb)) {
3316 /* so for a while no zero-copy! */
Joao Pinto54139cf2017-04-06 09:49:09 +01003317 rx_q->rx_zeroc_thresh = STMMAC_RX_THRESH;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003318 if (unlikely(net_ratelimit()))
3319 dev_err(priv->device,
3320 "fail to alloc skb entry %d\n",
3321 entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003322 break;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003323 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003324
Joao Pinto54139cf2017-04-06 09:49:09 +01003325 rx_q->rx_skbuff[entry] = skb;
3326 rx_q->rx_skbuff_dma[entry] =
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003327 dma_map_single(priv->device, skb->data, bfsize,
3328 DMA_FROM_DEVICE);
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003329 if (dma_mapping_error(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003330 rx_q->rx_skbuff_dma[entry])) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003331 netdev_err(priv->dev, "Rx DMA map failed\n");
Giuseppe CAVALLARO362b37b2014-08-27 11:27:00 +02003332 dev_kfree_skb(skb);
3333 break;
3334 }
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00003335
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003336 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
Joao Pinto54139cf2017-04-06 09:49:09 +01003337 p->des0 = cpu_to_le32(rx_q->rx_skbuff_dma[entry]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003338 p->des1 = 0;
3339 } else {
Joao Pinto54139cf2017-04-06 09:49:09 +01003340 p->des2 = cpu_to_le32(rx_q->rx_skbuff_dma[entry]);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003341 }
3342 if (priv->hw->mode->refill_desc3)
Joao Pinto54139cf2017-04-06 09:49:09 +01003343 priv->hw->mode->refill_desc3(rx_q, p);
Giuseppe CAVALLARO286a8372011-10-18 00:01:24 +00003344
Joao Pinto54139cf2017-04-06 09:49:09 +01003345 if (rx_q->rx_zeroc_thresh > 0)
3346 rx_q->rx_zeroc_thresh--;
Giuseppe Cavallaro120e87f2016-02-29 14:27:42 +01003347
LABBE Corentinb3e51062016-11-16 20:09:41 +01003348 netif_dbg(priv, rx_status, priv->dev,
3349 "refill entry #%d\n", entry);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003350 }
Pavel Machekad688cd2016-12-18 21:38:12 +01003351 dma_wmb();
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003352
3353 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
3354 priv->hw->desc->init_rx_desc(p, priv->use_riwt, 0, 0);
3355 else
3356 priv->hw->desc->set_rx_owner(p);
3357
Pavel Machekad688cd2016-12-18 21:38:12 +01003358 dma_wmb();
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003359
3360 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003361 }
Joao Pinto54139cf2017-04-06 09:49:09 +01003362 rx_q->dirty_rx = entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003363}
3364
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003365/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003366 * stmmac_rx - manage the receive process
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003367 * @priv: driver private structure
Joao Pinto54139cf2017-04-06 09:49:09 +01003368 * @limit: napi bugget
3369 * @queue: RX queue index.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003370 * Description : this the function called by the napi poll method.
3371 * It gets all the frames inside the ring.
3372 */
Joao Pinto54139cf2017-04-06 09:49:09 +01003373static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003374{
Joao Pinto54139cf2017-04-06 09:49:09 +01003375 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3376 unsigned int entry = rx_q->cur_rx;
3377 int coe = priv->hw->rx_csum;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003378 unsigned int next_entry;
3379 unsigned int count = 0;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003380
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003381 if (netif_msg_rx_status(priv)) {
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02003382 void *rx_head;
3383
LABBE Corentin38ddc592016-11-16 20:09:39 +01003384 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003385 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003386 rx_head = (void *)rx_q->dma_erx;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003387 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003388 rx_head = (void *)rx_q->dma_rx;
Alexandre TORGUEd0225e72016-04-01 11:37:26 +02003389
3390 priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003391 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003392 while (count < limit) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003393 int status;
Giuseppe CAVALLARO9401bb52013-04-08 02:10:03 +00003394 struct dma_desc *p;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01003395 struct dma_desc *np;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003396
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003397 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003398 p = (struct dma_desc *)(rx_q->dma_erx + entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003399 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003400 p = rx_q->dma_rx + entry;
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003401
Fabrice Gasnierc1fa3212016-02-29 14:27:34 +01003402 /* read the status of the incoming frame */
3403 status = priv->hw->desc->rx_status(&priv->dev->stats,
3404 &priv->xstats, p);
3405 /* check if managed by the DMA otherwise go ahead */
3406 if (unlikely(status & dma_own))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003407 break;
3408
3409 count++;
3410
Joao Pinto54139cf2017-04-06 09:49:09 +01003411 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, DMA_RX_SIZE);
3412 next_entry = rx_q->cur_rx;
Giuseppe Cavallaroe3ad57c2016-02-29 14:27:30 +01003413
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003414 if (priv->extend_desc)
Joao Pinto54139cf2017-04-06 09:49:09 +01003415 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003416 else
Joao Pinto54139cf2017-04-06 09:49:09 +01003417 np = rx_q->dma_rx + next_entry;
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01003418
3419 prefetch(np);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003420
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003421 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
3422 priv->hw->desc->rx_extended_status(&priv->dev->stats,
3423 &priv->xstats,
Joao Pinto54139cf2017-04-06 09:49:09 +01003424 rx_q->dma_erx +
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003425 entry);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003426 if (unlikely(status == discard_frame)) {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003427 priv->dev->stats.rx_errors++;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003428 if (priv->hwts_rx_en && !priv->extend_desc) {
LABBE Corentin8d45e422017-02-08 09:31:08 +01003429 /* DESC2 & DESC3 will be overwritten by device
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003430 * with timestamp value, hence reinitialize
3431 * them in stmmac_rx_refill() function so that
3432 * device can reuse it.
3433 */
Jose Abreu9c8080d2017-10-20 14:37:34 +01003434 dev_kfree_skb_any(rx_q->rx_skbuff[entry]);
Joao Pinto54139cf2017-04-06 09:49:09 +01003435 rx_q->rx_skbuff[entry] = NULL;
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003436 dma_unmap_single(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003437 rx_q->rx_skbuff_dma[entry],
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003438 priv->dma_buf_sz,
3439 DMA_FROM_DEVICE);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003440 }
3441 } else {
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003442 struct sk_buff *skb;
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00003443 int frame_len;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003444 unsigned int des;
3445
3446 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
Michael Weiserf8be0d72016-11-14 18:58:05 +01003447 des = le32_to_cpu(p->des0);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003448 else
Michael Weiserf8be0d72016-11-14 18:58:05 +01003449 des = le32_to_cpu(p->des2);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003450
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003451 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
3452
LABBE Corentin8d45e422017-02-08 09:31:08 +01003453 /* If frame length is greater than skb buffer size
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003454 * (preallocated during init) then the packet is
3455 * ignored
3456 */
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01003457 if (frame_len > priv->dma_buf_sz) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003458 netdev_err(priv->dev,
3459 "len %d larger than size (%d)\n",
3460 frame_len, priv->dma_buf_sz);
Giuseppe CAVALLAROe527c4a2015-11-26 08:35:45 +01003461 priv->dev->stats.rx_length_errors++;
3462 break;
3463 }
3464
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00003465 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003466 * Type frames (LLC/LLC-SNAP)
3467 */
Giuseppe CAVALLARO3eeb2992010-07-27 00:09:47 +00003468 if (unlikely(status != llc_snap))
3469 frame_len -= ETH_FCS_LEN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003470
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003471 if (netif_msg_rx_status(priv)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003472 netdev_dbg(priv->dev, "\tdesc: %p [entry %d] buff=0x%x\n",
3473 p, entry, des);
Florian Fainelli1ca79922017-12-29 19:56:33 -08003474 netdev_dbg(priv->dev, "frame size %d, COE: %d\n",
3475 frame_len, status);
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003476 }
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003477
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003478 /* The zero-copy is always used for all the sizes
3479 * in case of GMAC4 because it needs
3480 * to refill the used descriptors, always.
3481 */
3482 if (unlikely(!priv->plat->has_gmac4 &&
3483 ((frame_len < priv->rx_copybreak) ||
Joao Pinto54139cf2017-04-06 09:49:09 +01003484 stmmac_rx_threshold_count(rx_q)))) {
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003485 skb = netdev_alloc_skb_ip_align(priv->dev,
3486 frame_len);
3487 if (unlikely(!skb)) {
3488 if (net_ratelimit())
3489 dev_warn(priv->device,
3490 "packet dropped\n");
3491 priv->dev->stats.rx_dropped++;
3492 break;
3493 }
3494
3495 dma_sync_single_for_cpu(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003496 rx_q->rx_skbuff_dma
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003497 [entry], frame_len,
3498 DMA_FROM_DEVICE);
3499 skb_copy_to_linear_data(skb,
Joao Pinto54139cf2017-04-06 09:49:09 +01003500 rx_q->
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003501 rx_skbuff[entry]->data,
3502 frame_len);
3503
3504 skb_put(skb, frame_len);
3505 dma_sync_single_for_device(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003506 rx_q->rx_skbuff_dma
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003507 [entry], frame_len,
3508 DMA_FROM_DEVICE);
3509 } else {
Joao Pinto54139cf2017-04-06 09:49:09 +01003510 skb = rx_q->rx_skbuff[entry];
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003511 if (unlikely(!skb)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003512 netdev_err(priv->dev,
3513 "%s: Inconsistent Rx chain\n",
3514 priv->dev->name);
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003515 priv->dev->stats.rx_dropped++;
3516 break;
3517 }
3518 prefetch(skb->data - NET_IP_ALIGN);
Joao Pinto54139cf2017-04-06 09:49:09 +01003519 rx_q->rx_skbuff[entry] = NULL;
3520 rx_q->rx_zeroc_thresh++;
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003521
3522 skb_put(skb, frame_len);
3523 dma_unmap_single(priv->device,
Joao Pinto54139cf2017-04-06 09:49:09 +01003524 rx_q->rx_skbuff_dma[entry],
Giuseppe Cavallaro22ad3832016-02-29 14:27:41 +01003525 priv->dma_buf_sz,
3526 DMA_FROM_DEVICE);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003527 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003528
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003529 if (netif_msg_pktdata(priv)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003530 netdev_dbg(priv->dev, "frame received (%dbytes)",
3531 frame_len);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003532 print_pkt(skb->data, frame_len);
3533 }
Giuseppe CAVALLARO83d7af62013-07-02 14:12:36 +02003534
Giuseppe CAVALLAROba1ffd72016-11-14 09:27:29 +01003535 stmmac_get_rx_hwtstamp(priv, p, np, skb);
3536
Vince Bridgersb9381982014-01-14 13:42:05 -06003537 stmmac_rx_vlan(priv->dev, skb);
3538
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003539 skb->protocol = eth_type_trans(skb, priv->dev);
3540
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003541 if (unlikely(!coe))
Eric Dumazetbc8acf22010-09-02 13:07:41 -07003542 skb_checksum_none_assert(skb);
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00003543 else
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003544 skb->ip_summed = CHECKSUM_UNNECESSARY;
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00003545
Joao Pintoc22a3f42017-04-06 09:49:11 +01003546 napi_gro_receive(&rx_q->napi, skb);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003547
3548 priv->dev->stats.rx_packets++;
3549 priv->dev->stats.rx_bytes += frame_len;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003550 }
3551 entry = next_entry;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003552 }
3553
Joao Pinto54139cf2017-04-06 09:49:09 +01003554 stmmac_rx_refill(priv, queue);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003555
3556 priv->xstats.rx_pkt_n += count;
3557
3558 return count;
3559}
3560
3561/**
3562 * stmmac_poll - stmmac poll method (NAPI)
3563 * @napi : pointer to the napi structure.
3564 * @budget : maximum number of packets that the current CPU can receive from
3565 * all interfaces.
3566 * Description :
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00003567 * To look at the incoming frames and clear the tx resources.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003568 */
3569static int stmmac_poll(struct napi_struct *napi, int budget)
3570{
Joao Pintoc22a3f42017-04-06 09:49:11 +01003571 struct stmmac_rx_queue *rx_q =
3572 container_of(napi, struct stmmac_rx_queue, napi);
3573 struct stmmac_priv *priv = rx_q->priv_data;
Joao Pintoce736782017-04-06 09:49:10 +01003574 u32 tx_count = priv->plat->tx_queues_to_use;
Joao Pintoc22a3f42017-04-06 09:49:11 +01003575 u32 chan = rx_q->queue_index;
Joao Pinto54139cf2017-04-06 09:49:09 +01003576 int work_done = 0;
Joao Pintoc22a3f42017-04-06 09:49:11 +01003577 u32 queue;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003578
Giuseppe CAVALLARO9125cdd2012-11-25 23:10:42 +00003579 priv->xstats.napi_poll++;
Joao Pintoce736782017-04-06 09:49:10 +01003580
3581 /* check all the queues */
3582 for (queue = 0; queue < tx_count; queue++)
3583 stmmac_tx_clean(priv, queue);
3584
Joao Pintoc22a3f42017-04-06 09:49:11 +01003585 work_done = stmmac_rx(priv, budget, rx_q->queue_index);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003586 if (work_done < budget) {
Eric Dumazet6ad20162017-01-30 08:22:01 -08003587 napi_complete_done(napi, work_done);
Joao Pinto4f513ec2017-03-15 11:04:46 +00003588 stmmac_enable_dma_irq(priv, chan);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003589 }
3590 return work_done;
3591}
3592
3593/**
3594 * stmmac_tx_timeout
3595 * @dev : Pointer to net device structure
3596 * Description: this function is called when a packet transmission fails to
Giuseppe CAVALLARO7284a3f2012-11-25 23:10:41 +00003597 * complete within a reasonable time. The driver will mark the error in the
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003598 * netdev structure and arrange for the device to be reset to a sane state
3599 * in order to transmit a new packet.
3600 */
3601static void stmmac_tx_timeout(struct net_device *dev)
3602{
3603 struct stmmac_priv *priv = netdev_priv(dev);
3604
Jose Abreu34877a12018-03-29 10:40:18 +01003605 stmmac_global_err(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003606}
3607
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003608/**
Jiri Pirko01789342011-08-16 06:29:00 +00003609 * stmmac_set_rx_mode - entry point for multicast addressing
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003610 * @dev : pointer to the device structure
3611 * Description:
3612 * This function is a driver entry point which gets called by the kernel
3613 * whenever multicast addresses must be enabled/disabled.
3614 * Return value:
3615 * void.
3616 */
Jiri Pirko01789342011-08-16 06:29:00 +00003617static void stmmac_set_rx_mode(struct net_device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003618{
3619 struct stmmac_priv *priv = netdev_priv(dev);
3620
Vince Bridgers3b57de92014-07-31 15:49:17 -05003621 priv->hw->mac->set_filter(priv->hw, dev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003622}
3623
3624/**
3625 * stmmac_change_mtu - entry point to change MTU size for the device.
3626 * @dev : device pointer.
3627 * @new_mtu : the new MTU size for the device.
3628 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
3629 * to drive packet transmission. Ethernet has an MTU of 1500 octets
3630 * (ETH_DATA_LEN). This value can be changed with ifconfig.
3631 * Return value:
3632 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3633 * file on failure.
3634 */
3635static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
3636{
LABBE Corentin38ddc592016-11-16 20:09:39 +01003637 struct stmmac_priv *priv = netdev_priv(dev);
3638
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003639 if (netif_running(dev)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003640 netdev_err(priv->dev, "must be stopped to change its MTU\n");
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003641 return -EBUSY;
3642 }
3643
Michał Mirosław5e982f32011-04-09 02:46:55 +00003644 dev->mtu = new_mtu;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003645
Michał Mirosław5e982f32011-04-09 02:46:55 +00003646 netdev_update_features(dev);
3647
3648 return 0;
3649}
3650
Michał Mirosławc8f44af2011-11-15 15:29:55 +00003651static netdev_features_t stmmac_fix_features(struct net_device *dev,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003652 netdev_features_t features)
Michał Mirosław5e982f32011-04-09 02:46:55 +00003653{
3654 struct stmmac_priv *priv = netdev_priv(dev);
3655
Deepak SIKRI38912bd2012-04-04 04:33:21 +00003656 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
Michał Mirosław5e982f32011-04-09 02:46:55 +00003657 features &= ~NETIF_F_RXCSUM;
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003658
Michał Mirosław5e982f32011-04-09 02:46:55 +00003659 if (!priv->plat->tx_coe)
Tom Herberta1882222015-12-14 11:19:43 -08003660 features &= ~NETIF_F_CSUM_MASK;
Michał Mirosław5e982f32011-04-09 02:46:55 +00003661
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00003662 /* Some GMAC devices have a bugged Jumbo frame support that
3663 * needs to have the Tx COE disabled for oversized frames
3664 * (due to limited buffer sizes). In this case we disable
LABBE Corentin8d45e422017-02-08 09:31:08 +01003665 * the TX csum insertion in the TDES and not use SF.
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003666 */
Michał Mirosław5e982f32011-04-09 02:46:55 +00003667 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
Tom Herberta1882222015-12-14 11:19:43 -08003668 features &= ~NETIF_F_CSUM_MASK;
Giuseppe CAVALLAROebbb2932010-09-17 03:23:40 +00003669
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003670 /* Disable tso if asked by ethtool */
3671 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
3672 if (features & NETIF_F_TSO)
3673 priv->tso = true;
3674 else
3675 priv->tso = false;
3676 }
3677
Michał Mirosław5e982f32011-04-09 02:46:55 +00003678 return features;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003679}
3680
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02003681static int stmmac_set_features(struct net_device *netdev,
3682 netdev_features_t features)
3683{
3684 struct stmmac_priv *priv = netdev_priv(netdev);
3685
3686 /* Keep the COE Type in case of csum is supporting */
3687 if (features & NETIF_F_RXCSUM)
3688 priv->hw->rx_csum = priv->plat->rx_coe;
3689 else
3690 priv->hw->rx_csum = 0;
3691 /* No check needed because rx_coe has been set before and it will be
3692 * fixed in case of issue.
3693 */
3694 priv->hw->mac->rx_ipc(priv->hw);
3695
3696 return 0;
3697}
3698
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003699/**
3700 * stmmac_interrupt - main ISR
3701 * @irq: interrupt number.
3702 * @dev_id: to pass the net device pointer.
3703 * Description: this is the main driver interrupt service routine.
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01003704 * It can call:
3705 * o DMA service routine (to manage incoming frame reception and transmission
3706 * status)
3707 * o Core interrupts to manage: remote wake-up, management counter, LPI
3708 * interrupts.
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003709 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003710static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
3711{
3712 struct net_device *dev = (struct net_device *)dev_id;
3713 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto7bac4e12017-03-15 11:04:55 +00003714 u32 rx_cnt = priv->plat->rx_queues_to_use;
3715 u32 tx_cnt = priv->plat->tx_queues_to_use;
3716 u32 queues_count;
3717 u32 queue;
3718
3719 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003720
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00003721 if (priv->irq_wake)
3722 pm_wakeup_event(priv->device, 0);
3723
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003724 if (unlikely(!dev)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01003725 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003726 return IRQ_NONE;
3727 }
3728
Jose Abreu34877a12018-03-29 10:40:18 +01003729 /* Check if adapter is up */
3730 if (test_bit(STMMAC_DOWN, &priv->state))
3731 return IRQ_HANDLED;
3732
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003733 /* To handle GMAC own interrupts */
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003734 if ((priv->plat->has_gmac) || (priv->plat->has_gmac4)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05003735 int status = priv->hw->mac->host_irq_status(priv->hw,
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00003736 &priv->xstats);
Joao Pinto8f71a882017-03-10 18:24:57 +00003737
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003738 if (unlikely(status)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003739 /* For LPI we need to save the tx status */
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00003740 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003741 priv->tx_path_in_lpi_mode = true;
Giuseppe CAVALLARO0982a0f2013-03-26 04:43:07 +00003742 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003743 priv->tx_path_in_lpi_mode = false;
Joao Pinto7bac4e12017-03-15 11:04:55 +00003744 }
3745
3746 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3747 for (queue = 0; queue < queues_count; queue++) {
Joao Pinto54139cf2017-04-06 09:49:09 +01003748 struct stmmac_rx_queue *rx_q =
3749 &priv->rx_queue[queue];
3750
Joao Pinto7bac4e12017-03-15 11:04:55 +00003751 status |=
3752 priv->hw->mac->host_mtl_irq_status(priv->hw,
3753 queue);
3754
3755 if (status & CORE_IRQ_MTL_RX_OVERFLOW &&
3756 priv->hw->dma->set_rx_tail_ptr)
3757 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
Joao Pinto54139cf2017-04-06 09:49:09 +01003758 rx_q->rx_tail_addr,
Joao Pinto7bac4e12017-03-15 11:04:55 +00003759 queue);
3760 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003761 }
Giuseppe CAVALLARO70523e632016-06-24 15:16:24 +02003762
3763 /* PCS link status */
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02003764 if (priv->hw->pcs) {
Giuseppe CAVALLARO70523e632016-06-24 15:16:24 +02003765 if (priv->xstats.pcs_link)
3766 netif_carrier_on(dev);
3767 else
3768 netif_carrier_off(dev);
3769 }
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00003770 }
3771
3772 /* To handle DMA interrupts */
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00003773 stmmac_dma_interrupt(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003774
3775 return IRQ_HANDLED;
3776}
3777
3778#ifdef CONFIG_NET_POLL_CONTROLLER
3779/* Polling receive - used by NETCONSOLE and other diagnostic tools
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003780 * to allow network I/O with interrupts disabled.
3781 */
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003782static void stmmac_poll_controller(struct net_device *dev)
3783{
3784 disable_irq(dev->irq);
3785 stmmac_interrupt(dev->irq, dev);
3786 enable_irq(dev->irq);
3787}
3788#endif
3789
3790/**
3791 * stmmac_ioctl - Entry point for the Ioctl
3792 * @dev: Device pointer.
3793 * @rq: An IOCTL specefic structure, that can contain a pointer to
3794 * a proprietary structure used to pass information to the driver.
3795 * @cmd: IOCTL command
3796 * Description:
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00003797 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003798 */
3799static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3800{
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003801 int ret = -EOPNOTSUPP;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003802
3803 if (!netif_running(dev))
3804 return -EINVAL;
3805
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003806 switch (cmd) {
3807 case SIOCGMIIPHY:
3808 case SIOCGMIIREG:
3809 case SIOCSMIIREG:
Philippe Reynesd6d50c72016-10-03 08:28:19 +02003810 if (!dev->phydev)
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003811 return -EINVAL;
Philippe Reynesd6d50c72016-10-03 08:28:19 +02003812 ret = phy_mii_ioctl(dev->phydev, rq, cmd);
Rayagond Kokatanur891434b2013-03-26 04:43:10 +00003813 break;
3814 case SIOCSHWTSTAMP:
3815 ret = stmmac_hwtstamp_ioctl(dev, rq);
3816 break;
3817 default:
3818 break;
3819 }
Richard Cochran28b04112010-07-17 08:48:55 +00003820
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07003821 return ret;
3822}
3823
Bhadram Varkaa8304052017-10-27 08:22:02 +05303824static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
3825{
3826 struct stmmac_priv *priv = netdev_priv(ndev);
3827 int ret = 0;
3828
3829 ret = eth_mac_addr(ndev, addr);
3830 if (ret)
3831 return ret;
3832
3833 priv->hw->mac->set_umac_addr(priv->hw, ndev->dev_addr, 0);
3834
3835 return ret;
3836}
3837
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01003838#ifdef CONFIG_DEBUG_FS
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003839static struct dentry *stmmac_fs_dir;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003840
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003841static void sysfs_display_ring(void *head, int size, int extend_desc,
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003842 struct seq_file *seq)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003843{
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003844 int i;
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003845 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
3846 struct dma_desc *p = (struct dma_desc *)head;
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003847
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003848 for (i = 0; i < size; i++) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003849 if (extend_desc) {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003850 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00003851 i, (unsigned int)virt_to_phys(ep),
Michael Weiserf8be0d72016-11-14 18:58:05 +01003852 le32_to_cpu(ep->basic.des0),
3853 le32_to_cpu(ep->basic.des1),
3854 le32_to_cpu(ep->basic.des2),
3855 le32_to_cpu(ep->basic.des3));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003856 ep++;
3857 } else {
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003858 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
Niklas Cassel66c25f62017-05-15 10:56:06 +02003859 i, (unsigned int)virt_to_phys(p),
Michael Weiserf8be0d72016-11-14 18:58:05 +01003860 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
3861 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003862 p++;
3863 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003864 seq_printf(seq, "\n");
3865 }
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003866}
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003867
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00003868static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
3869{
3870 struct net_device *dev = seq->private;
3871 struct stmmac_priv *priv = netdev_priv(dev);
Joao Pinto54139cf2017-04-06 09:49:09 +01003872 u32 rx_count = priv->plat->rx_queues_to_use;
Joao Pintoce736782017-04-06 09:49:10 +01003873 u32 tx_count = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01003874 u32 queue;
3875
3876 for (queue = 0; queue < rx_count; queue++) {
3877 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3878
3879 seq_printf(seq, "RX Queue %d:\n", queue);
3880
3881 if (priv->extend_desc) {
3882 seq_printf(seq, "Extended descriptor ring:\n");
3883 sysfs_display_ring((void *)rx_q->dma_erx,
3884 DMA_RX_SIZE, 1, seq);
3885 } else {
3886 seq_printf(seq, "Descriptor ring:\n");
3887 sysfs_display_ring((void *)rx_q->dma_rx,
3888 DMA_RX_SIZE, 0, seq);
3889 }
3890 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003891
Joao Pintoce736782017-04-06 09:49:10 +01003892 for (queue = 0; queue < tx_count; queue++) {
3893 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3894
3895 seq_printf(seq, "TX Queue %d:\n", queue);
3896
3897 if (priv->extend_desc) {
3898 seq_printf(seq, "Extended descriptor ring:\n");
3899 sysfs_display_ring((void *)tx_q->dma_etx,
3900 DMA_TX_SIZE, 1, seq);
3901 } else {
3902 seq_printf(seq, "Descriptor ring:\n");
3903 sysfs_display_ring((void *)tx_q->dma_tx,
3904 DMA_TX_SIZE, 0, seq);
3905 }
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003906 }
3907
3908 return 0;
3909}
3910
3911static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
3912{
3913 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
3914}
3915
Pavel Machek22d3efe2016-11-28 12:55:59 +01003916/* Debugfs files, should appear in /sys/kernel/debug/stmmaceth/eth0 */
3917
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003918static const struct file_operations stmmac_rings_status_fops = {
3919 .owner = THIS_MODULE,
3920 .open = stmmac_sysfs_ring_open,
3921 .read = seq_read,
3922 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00003923 .release = single_release,
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00003924};
3925
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003926static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
3927{
3928 struct net_device *dev = seq->private;
3929 struct stmmac_priv *priv = netdev_priv(dev);
3930
Giuseppe CAVALLARO19e30c12011-11-16 21:58:00 +00003931 if (!priv->hw_cap_support) {
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003932 seq_printf(seq, "DMA HW features not supported\n");
3933 return 0;
3934 }
3935
3936 seq_printf(seq, "==============================\n");
3937 seq_printf(seq, "\tDMA HW features\n");
3938 seq_printf(seq, "==============================\n");
3939
Pavel Machek22d3efe2016-11-28 12:55:59 +01003940 seq_printf(seq, "\t10/100 Mbps: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003941 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003942 seq_printf(seq, "\t1000 Mbps: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003943 (priv->dma_cap.mbps_1000) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003944 seq_printf(seq, "\tHalf duplex: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003945 (priv->dma_cap.half_duplex) ? "Y" : "N");
3946 seq_printf(seq, "\tHash Filter: %s\n",
3947 (priv->dma_cap.hash_filter) ? "Y" : "N");
3948 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
3949 (priv->dma_cap.multi_addr) ? "Y" : "N");
LABBE Corentin8d45e422017-02-08 09:31:08 +01003950 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003951 (priv->dma_cap.pcs) ? "Y" : "N");
3952 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
3953 (priv->dma_cap.sma_mdio) ? "Y" : "N");
3954 seq_printf(seq, "\tPMT Remote wake up: %s\n",
3955 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
3956 seq_printf(seq, "\tPMT Magic Frame: %s\n",
3957 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
3958 seq_printf(seq, "\tRMON module: %s\n",
3959 (priv->dma_cap.rmon) ? "Y" : "N");
3960 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
3961 (priv->dma_cap.time_stamp) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003962 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003963 (priv->dma_cap.atime_stamp) ? "Y" : "N");
Pavel Machek22d3efe2016-11-28 12:55:59 +01003964 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003965 (priv->dma_cap.eee) ? "Y" : "N");
3966 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
3967 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
3968 (priv->dma_cap.tx_coe) ? "Y" : "N");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02003969 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3970 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
3971 (priv->dma_cap.rx_coe) ? "Y" : "N");
3972 } else {
3973 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
3974 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
3975 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
3976 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
3977 }
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00003978 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
3979 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
3980 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
3981 priv->dma_cap.number_rx_channel);
3982 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
3983 priv->dma_cap.number_tx_channel);
3984 seq_printf(seq, "\tEnhanced descriptors: %s\n",
3985 (priv->dma_cap.enh_desc) ? "Y" : "N");
3986
3987 return 0;
3988}
3989
3990static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
3991{
3992 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
3993}
3994
3995static const struct file_operations stmmac_dma_cap_fops = {
3996 .owner = THIS_MODULE,
3997 .open = stmmac_sysfs_dma_cap_open,
3998 .read = seq_read,
3999 .llseek = seq_lseek,
Djalal Harouni74863942012-05-20 13:55:30 +00004000 .release = single_release,
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00004001};
4002
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004003static int stmmac_init_fs(struct net_device *dev)
4004{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004005 struct stmmac_priv *priv = netdev_priv(dev);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004006
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004007 /* Create per netdev entries */
4008 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
4009
4010 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01004011 netdev_err(priv->dev, "ERROR failed to create debugfs directory\n");
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004012
4013 return -ENOMEM;
4014 }
4015
4016 /* Entry to report DMA RX/TX rings */
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004017 priv->dbgfs_rings_status =
Joe Perchesd3757ba2018-03-23 16:34:44 -07004018 debugfs_create_file("descriptors_status", 0444,
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004019 priv->dbgfs_dir, dev,
4020 &stmmac_rings_status_fops);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004021
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004022 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01004023 netdev_err(priv->dev, "ERROR creating stmmac ring debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004024 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004025
4026 return -ENOMEM;
4027 }
4028
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00004029 /* Entry to report the DMA HW features */
Joe Perchesd3757ba2018-03-23 16:34:44 -07004030 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", 0444,
4031 priv->dbgfs_dir,
4032 dev, &stmmac_dma_cap_fops);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00004033
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004034 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01004035 netdev_err(priv->dev, "ERROR creating stmmac MMC debugfs file\n");
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004036 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLAROe7434822011-09-01 21:51:41 +00004037
4038 return -ENOMEM;
4039 }
4040
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004041 return 0;
4042}
4043
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004044static void stmmac_exit_fs(struct net_device *dev)
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004045{
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004046 struct stmmac_priv *priv = netdev_priv(dev);
4047
4048 debugfs_remove_recursive(priv->dbgfs_dir);
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004049}
Giuseppe CAVALLARO50fb4f742014-11-04 15:49:33 +01004050#endif /* CONFIG_DEBUG_FS */
Giuseppe CAVALLARO7ac29052011-09-01 21:51:39 +00004051
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004052static const struct net_device_ops stmmac_netdev_ops = {
4053 .ndo_open = stmmac_open,
4054 .ndo_start_xmit = stmmac_xmit,
4055 .ndo_stop = stmmac_release,
4056 .ndo_change_mtu = stmmac_change_mtu,
Michał Mirosław5e982f32011-04-09 02:46:55 +00004057 .ndo_fix_features = stmmac_fix_features,
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02004058 .ndo_set_features = stmmac_set_features,
Jiri Pirko01789342011-08-16 06:29:00 +00004059 .ndo_set_rx_mode = stmmac_set_rx_mode,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004060 .ndo_tx_timeout = stmmac_tx_timeout,
4061 .ndo_do_ioctl = stmmac_ioctl,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004062#ifdef CONFIG_NET_POLL_CONTROLLER
4063 .ndo_poll_controller = stmmac_poll_controller,
4064#endif
Bhadram Varkaa8304052017-10-27 08:22:02 +05304065 .ndo_set_mac_address = stmmac_set_mac_address,
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004066};
4067
Jose Abreu34877a12018-03-29 10:40:18 +01004068static void stmmac_reset_subtask(struct stmmac_priv *priv)
4069{
4070 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
4071 return;
4072 if (test_bit(STMMAC_DOWN, &priv->state))
4073 return;
4074
4075 netdev_err(priv->dev, "Reset adapter.\n");
4076
4077 rtnl_lock();
4078 netif_trans_update(priv->dev);
4079 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
4080 usleep_range(1000, 2000);
4081
4082 set_bit(STMMAC_DOWN, &priv->state);
4083 dev_close(priv->dev);
4084 dev_open(priv->dev);
4085 clear_bit(STMMAC_DOWN, &priv->state);
4086 clear_bit(STMMAC_RESETING, &priv->state);
4087 rtnl_unlock();
4088}
4089
4090static void stmmac_service_task(struct work_struct *work)
4091{
4092 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
4093 service_task);
4094
4095 stmmac_reset_subtask(priv);
4096 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
4097}
4098
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004099/**
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004100 * stmmac_hw_init - Init the MAC device
Giuseppe CAVALLARO32ceabc2013-04-08 02:10:00 +00004101 * @priv: driver private structure
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004102 * Description: this function is to configure the MAC device according to
4103 * some platform parameters or the HW capability register. It prepares the
4104 * driver to use either ring or chain modes and to setup either enhanced or
4105 * normal descriptors.
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004106 */
4107static int stmmac_hw_init(struct stmmac_priv *priv)
4108{
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004109 struct mac_device_info *mac;
4110
4111 /* Identify the MAC HW device */
LABBE Corentinec33d712017-05-31 09:18:33 +02004112 if (priv->plat->setup) {
4113 mac = priv->plat->setup(priv);
4114 } else if (priv->plat->has_gmac) {
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00004115 priv->dev->priv_flags |= IFF_UNICAST_FLT;
Vince Bridgers3b57de92014-07-31 15:49:17 -05004116 mac = dwmac1000_setup(priv->ioaddr,
4117 priv->plat->multicast_filter_bins,
Alexandre TORGUEc623d142016-04-01 11:37:27 +02004118 priv->plat->unicast_filter_entries,
4119 &priv->synopsys_id);
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004120 } else if (priv->plat->has_gmac4) {
4121 priv->dev->priv_flags |= IFF_UNICAST_FLT;
4122 mac = dwmac4_setup(priv->ioaddr,
4123 priv->plat->multicast_filter_bins,
4124 priv->plat->unicast_filter_entries,
4125 &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00004126 } else {
Alexandre TORGUEc623d142016-04-01 11:37:27 +02004127 mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
Marc Kleine-Budde03f2eec2012-04-03 22:13:01 +00004128 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004129 if (!mac)
4130 return -ENOMEM;
4131
4132 priv->hw = mac;
4133
LABBE Corentin9f93ac82017-05-31 09:18:36 +02004134 /* dwmac-sun8i only work in chain mode */
4135 if (priv->plat->has_sun8i)
4136 chain_mode = 1;
4137
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00004138 /* To use the chained or ring mode */
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004139 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
4140 priv->hw->mode = &dwmac4_ring_mode_ops;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00004141 } else {
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004142 if (chain_mode) {
4143 priv->hw->mode = &chain_mode_ops;
LABBE Corentin38ddc592016-11-16 20:09:39 +01004144 dev_info(priv->device, "Chain mode enabled\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004145 priv->mode = STMMAC_CHAIN_MODE;
4146 } else {
4147 priv->hw->mode = &ring_mode_ops;
LABBE Corentin38ddc592016-11-16 20:09:39 +01004148 dev_info(priv->device, "Ring mode enabled\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004149 priv->mode = STMMAC_RING_MODE;
4150 }
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00004151 }
4152
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004153 /* Get the HW capability (new GMAC newer than 3.50a) */
4154 priv->hw_cap_support = stmmac_get_hw_features(priv);
4155 if (priv->hw_cap_support) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01004156 dev_info(priv->device, "DMA HW capability register supported\n");
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004157
4158 /* We can override some gmac/dma configuration fields: e.g.
4159 * enh_desc, tx_coe (e.g. that are passed through the
4160 * platform) with the values from the HW capability
4161 * register (if supported).
4162 */
4163 priv->plat->enh_desc = priv->dma_cap.enh_desc;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004164 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02004165 priv->hw->pmt = priv->plat->pmt;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00004166
Ezequiel Garciaa8df35d2016-05-16 12:41:07 -03004167 /* TXCOE doesn't work in thresh DMA mode */
4168 if (priv->plat->force_thresh_dma_mode)
4169 priv->plat->tx_coe = 0;
4170 else
4171 priv->plat->tx_coe = priv->dma_cap.tx_coe;
4172
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004173 /* In case of GMAC4 rx_coe is from HW cap register. */
4174 priv->plat->rx_coe = priv->dma_cap.rx_coe;
Deepak SIKRI38912bd2012-04-04 04:33:21 +00004175
4176 if (priv->dma_cap.rx_coe_type2)
4177 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
4178 else if (priv->dma_cap.rx_coe_type1)
4179 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
4180
LABBE Corentin38ddc592016-11-16 20:09:39 +01004181 } else {
4182 dev_info(priv->device, "No HW DMA feature register supported\n");
4183 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004184
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004185 /* To use alternate (extended), normal or GMAC4 descriptor structures */
4186 if (priv->synopsys_id >= DWMAC_CORE_4_00)
4187 priv->hw->desc = &dwmac4_desc_ops;
4188 else
4189 stmmac_selec_desc_mode(priv);
Byungho An61369d02013-06-28 16:35:32 +09004190
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02004191 if (priv->plat->rx_coe) {
4192 priv->hw->rx_csum = priv->plat->rx_coe;
LABBE Corentin38ddc592016-11-16 20:09:39 +01004193 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004194 if (priv->synopsys_id < DWMAC_CORE_4_00)
LABBE Corentin38ddc592016-11-16 20:09:39 +01004195 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
Giuseppe CAVALLAROd2afb5b2014-09-01 09:17:52 +02004196 }
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004197 if (priv->plat->tx_coe)
LABBE Corentin38ddc592016-11-16 20:09:39 +01004198 dev_info(priv->device, "TX Checksum insertion supported\n");
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004199
4200 if (priv->plat->pmt) {
LABBE Corentin38ddc592016-11-16 20:09:39 +01004201 dev_info(priv->device, "Wake-Up On Lan supported\n");
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004202 device_set_wakeup_capable(priv->device, 1);
4203 }
4204
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004205 if (priv->dma_cap.tsoen)
LABBE Corentin38ddc592016-11-16 20:09:39 +01004206 dev_info(priv->device, "TSO supported\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004207
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00004208 return 0;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004209}
4210
4211/**
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004212 * stmmac_dvr_probe
4213 * @device: device pointer
Giuseppe CAVALLAROff3dd782012-06-04 19:22:55 +00004214 * @plat_dat: platform data pointer
Joachim Eastwoode56788c2015-05-20 20:03:07 +02004215 * @res: stmmac resource pointer
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004216 * Description: this is the main probe function used to
4217 * call the alloc_etherdev, allocate the priv structure.
Andy Shevchenko9afec6e2015-01-27 18:38:03 +02004218 * Return:
Joachim Eastwood15ffac72015-05-20 20:03:08 +02004219 * returns 0 on success, otherwise errno.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004220 */
Joachim Eastwood15ffac72015-05-20 20:03:08 +02004221int stmmac_dvr_probe(struct device *device,
4222 struct plat_stmmacenet_data *plat_dat,
4223 struct stmmac_resources *res)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004224{
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004225 struct net_device *ndev = NULL;
4226 struct stmmac_priv *priv;
Joao Pintoc22a3f42017-04-06 09:49:11 +01004227 int ret = 0;
4228 u32 queue;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004229
Joao Pintoc22a3f42017-04-06 09:49:11 +01004230 ndev = alloc_etherdev_mqs(sizeof(struct stmmac_priv),
4231 MTL_MAX_TX_QUEUES,
4232 MTL_MAX_RX_QUEUES);
Joe Perches41de8d42012-01-29 13:47:52 +00004233 if (!ndev)
Joachim Eastwood15ffac72015-05-20 20:03:08 +02004234 return -ENOMEM;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004235
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004236 SET_NETDEV_DEV(ndev, device);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004237
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004238 priv = netdev_priv(ndev);
4239 priv->device = device;
4240 priv->dev = ndev;
4241
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004242 stmmac_set_ethtool_ops(ndev);
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004243 priv->pause = pause;
4244 priv->plat = plat_dat;
Joachim Eastwoode56788c2015-05-20 20:03:07 +02004245 priv->ioaddr = res->addr;
4246 priv->dev->base_addr = (unsigned long)res->addr;
4247
4248 priv->dev->irq = res->irq;
4249 priv->wol_irq = res->wol_irq;
4250 priv->lpi_irq = res->lpi_irq;
4251
4252 if (res->mac)
4253 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004254
Joachim Eastwooda7a62682015-07-17 23:48:17 +02004255 dev_set_drvdata(device, priv->dev);
Joachim Eastwood803f8fc2015-05-20 20:03:06 +02004256
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004257 /* Verify driver arguments */
4258 stmmac_verify_args();
4259
Jose Abreu34877a12018-03-29 10:40:18 +01004260 /* Allocate workqueue */
4261 priv->wq = create_singlethread_workqueue("stmmac_wq");
4262 if (!priv->wq) {
4263 dev_err(priv->device, "failed to create workqueue\n");
4264 goto error_wq;
4265 }
4266
4267 INIT_WORK(&priv->service_task, stmmac_service_task);
4268
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004269 /* Override with kernel parameters if supplied XXX CRS XXX
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00004270 * this needs to have multiple instances
4271 */
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004272 if ((phyaddr >= 0) && (phyaddr <= 31))
4273 priv->plat->phy_addr = phyaddr;
4274
Eugeniy Paltsev90f522a2017-07-18 17:07:15 +03004275 if (priv->plat->stmmac_rst) {
4276 ret = reset_control_assert(priv->plat->stmmac_rst);
jpintof573c0b2017-01-09 12:35:09 +00004277 reset_control_deassert(priv->plat->stmmac_rst);
Eugeniy Paltsev90f522a2017-07-18 17:07:15 +03004278 /* Some reset controllers have only reset callback instead of
4279 * assert + deassert callbacks pair.
4280 */
4281 if (ret == -ENOTSUPP)
4282 reset_control_reset(priv->plat->stmmac_rst);
4283 }
Chen-Yu Tsaic5e4ddb2014-01-17 21:24:41 +08004284
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004285 /* Init MAC and get the capabilities */
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00004286 ret = stmmac_hw_init(priv);
4287 if (ret)
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08004288 goto error_hw_init;
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004289
Joao Pintoc22a3f42017-04-06 09:49:11 +01004290 /* Configure real RX and TX queues */
Joao Pintoc02b7a92017-04-10 11:32:14 +01004291 netif_set_real_num_rx_queues(ndev, priv->plat->rx_queues_to_use);
4292 netif_set_real_num_tx_queues(ndev, priv->plat->tx_queues_to_use);
Joao Pintoc22a3f42017-04-06 09:49:11 +01004293
Giuseppe CAVALLAROcf3f0472012-02-15 00:10:39 +00004294 ndev->netdev_ops = &stmmac_netdev_ops;
4295
4296 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4297 NETIF_F_RXCSUM;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004298
4299 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
Niklas Cassel9edfa7d2017-06-19 18:36:44 +02004300 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004301 priv->tso = true;
LABBE Corentin38ddc592016-11-16 20:09:39 +01004302 dev_info(priv->device, "TSO feature enabled\n");
Alexandre TORGUEf748be52016-04-01 11:37:34 +02004303 }
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004304 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
4305 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004306#ifdef STMMAC_VLAN_TAG_USED
4307 /* Both mac100 and gmac support receive VLAN tag detection */
Patrick McHardyf6469682013-04-19 02:04:27 +00004308 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004309#endif
4310 priv->msg_enable = netif_msg_init(debug, default_msg_level);
4311
Jarod Wilson44770e12016-10-17 15:54:17 -04004312 /* MTU range: 46 - hw-specific max */
4313 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
4314 if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
4315 ndev->max_mtu = JUMBO_LEN;
4316 else
4317 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
Kweh, Hock Leonga2cd64f2017-01-07 17:32:03 +08004318 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
4319 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
4320 */
4321 if ((priv->plat->maxmtu < ndev->max_mtu) &&
4322 (priv->plat->maxmtu >= ndev->min_mtu))
Jarod Wilson44770e12016-10-17 15:54:17 -04004323 ndev->max_mtu = priv->plat->maxmtu;
Kweh, Hock Leonga2cd64f2017-01-07 17:32:03 +08004324 else if (priv->plat->maxmtu < ndev->min_mtu)
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004325 dev_warn(priv->device,
4326 "%s: warning: maxmtu having invalid value (%d)\n",
4327 __func__, priv->plat->maxmtu);
Jarod Wilson44770e12016-10-17 15:54:17 -04004328
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004329 if (flow_ctrl)
4330 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
4331
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00004332 /* Rx Watchdog is available in the COREs newer than the 3.40.
4333 * In some case, for example on bugged HW this feature
4334 * has to be disable and this can be done by passing the
4335 * riwt_off field from the platform.
4336 */
4337 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
4338 priv->use_riwt = 1;
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004339 dev_info(priv->device,
4340 "Enable RX Mitigation via HW Watchdog Timer\n");
Giuseppe CAVALLARO62a2ab92012-11-25 23:10:43 +00004341 }
4342
Joao Pintoc22a3f42017-04-06 09:49:11 +01004343 for (queue = 0; queue < priv->plat->rx_queues_to_use; queue++) {
4344 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4345
4346 netif_napi_add(ndev, &rx_q->napi, stmmac_poll,
4347 (8 * priv->plat->rx_queues_to_use));
4348 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004349
Vlad Lunguf8e96162010-11-29 22:52:52 +00004350 spin_lock_init(&priv->lock);
4351
Giuseppe CAVALLAROcd7201f2012-04-04 04:33:27 +00004352 /* If a specific clk_csr value is passed from the platform
4353 * this means that the CSR Clock Range selection cannot be
4354 * changed at run-time and it is fixed. Viceversa the driver'll try to
4355 * set the MDC clock dynamically according to the csr actual
4356 * clock input.
4357 */
4358 if (!priv->plat->clk_csr)
4359 stmmac_clk_csr_set(priv);
4360 else
4361 priv->clk_csr = priv->plat->clk_csr;
4362
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00004363 stmmac_check_pcs_mode(priv);
4364
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02004365 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4366 priv->hw->pcs != STMMAC_PCS_TBI &&
4367 priv->hw->pcs != STMMAC_PCS_RTBI) {
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00004368 /* MDIO bus Registration */
4369 ret = stmmac_mdio_register(ndev);
4370 if (ret < 0) {
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004371 dev_err(priv->device,
4372 "%s: MDIO bus (id: %d) registration failed",
4373 __func__, priv->plat->bus_id);
Giuseppe CAVALLAROe58bb432013-03-26 04:43:08 +00004374 goto error_mdio_register;
4375 }
Francesco Virlinzi4bfcbd72012-04-18 19:48:20 +00004376 }
4377
Florian Fainelli57016592016-12-27 18:23:06 -08004378 ret = register_netdev(ndev);
Florian Fainellib2eb09a2016-12-28 15:44:41 -08004379 if (ret) {
Heiner Kallweitb618ab42017-01-15 19:19:00 +01004380 dev_err(priv->device, "%s: ERROR %i registering the device\n",
4381 __func__, ret);
Florian Fainellib2eb09a2016-12-28 15:44:41 -08004382 goto error_netdev_register;
4383 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004384
Florian Fainelli57016592016-12-27 18:23:06 -08004385 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004386
Viresh Kumar6a81c262012-07-30 14:39:41 -07004387error_netdev_register:
Florian Fainellib2eb09a2016-12-28 15:44:41 -08004388 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4389 priv->hw->pcs != STMMAC_PCS_TBI &&
4390 priv->hw->pcs != STMMAC_PCS_RTBI)
4391 stmmac_mdio_unregister(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004392error_mdio_register:
Joao Pintoc22a3f42017-04-06 09:49:11 +01004393 for (queue = 0; queue < priv->plat->rx_queues_to_use; queue++) {
4394 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4395
4396 netif_napi_del(&rx_q->napi);
4397 }
Chen-Yu Tsai62866e92014-01-17 21:24:40 +08004398error_hw_init:
Jose Abreu34877a12018-03-29 10:40:18 +01004399 destroy_workqueue(priv->wq);
4400error_wq:
Dan Carpenter34a52f32010-12-20 21:34:56 +00004401 free_netdev(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004402
Joachim Eastwood15ffac72015-05-20 20:03:08 +02004403 return ret;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004404}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004405EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004406
4407/**
4408 * stmmac_dvr_remove
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004409 * @dev: device pointer
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004410 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
Giuseppe CAVALLARObfab27a2011-12-21 03:58:19 +00004411 * changes the link status, releases the DMA descriptor rings.
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004412 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004413int stmmac_dvr_remove(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004414{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004415 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLAROaec7ff22010-01-06 23:07:18 +00004416 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004417
LABBE Corentin38ddc592016-11-16 20:09:39 +01004418 netdev_info(priv->dev, "%s: removing driver", __func__);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004419
Joao Pintoae4f0d42017-03-15 11:04:47 +00004420 stmmac_stop_all_dma(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004421
LABBE Corentin270c7752017-03-23 14:40:22 +01004422 priv->hw->mac->set_mac(priv->ioaddr, false);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004423 netif_carrier_off(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004424 unregister_netdev(ndev);
jpintof573c0b2017-01-09 12:35:09 +00004425 if (priv->plat->stmmac_rst)
4426 reset_control_assert(priv->plat->stmmac_rst);
4427 clk_disable_unprepare(priv->plat->pclk);
4428 clk_disable_unprepare(priv->plat->stmmac_clk);
Giuseppe CAVALLARO3fe5cad2016-06-24 15:16:25 +02004429 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4430 priv->hw->pcs != STMMAC_PCS_TBI &&
4431 priv->hw->pcs != STMMAC_PCS_RTBI)
Bryan O'Donoghuee7434712015-04-16 17:56:03 +01004432 stmmac_mdio_unregister(ndev);
Jose Abreu34877a12018-03-29 10:40:18 +01004433 destroy_workqueue(priv->wq);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004434 free_netdev(ndev);
4435
4436 return 0;
4437}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004438EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004439
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004440/**
4441 * stmmac_suspend - suspend callback
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004442 * @dev: device pointer
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004443 * Description: this is the function to suspend the device and it is called
4444 * by the platform driver to stop the network queue, release the resources,
4445 * program the PMT register (for WoL), clean and release driver resources.
4446 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004447int stmmac_suspend(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004448{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004449 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004450 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004451 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004452
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004453 if (!ndev || !netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004454 return 0;
4455
Philippe Reynesd6d50c72016-10-03 08:28:19 +02004456 if (ndev->phydev)
4457 phy_stop(ndev->phydev);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00004458
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004459 spin_lock_irqsave(&priv->lock, flags);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004460
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004461 netif_device_detach(ndev);
Joao Pintoc22a3f42017-04-06 09:49:11 +01004462 stmmac_stop_all_queues(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004463
Joao Pintoc22a3f42017-04-06 09:49:11 +01004464 stmmac_disable_all_queues(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004465
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004466 /* Stop TX/RX DMA */
Joao Pintoae4f0d42017-03-15 11:04:47 +00004467 stmmac_stop_all_dma(priv);
Giuseppe CAVALLAROc24602e2013-03-26 04:43:06 +00004468
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004469 /* Enable Power down mode by programming the PMT regs */
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00004470 if (device_may_wakeup(priv->device)) {
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05004471 priv->hw->mac->pmt(priv->hw, priv->wolopts);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00004472 priv->irq_wake = 1;
4473 } else {
LABBE Corentin270c7752017-03-23 14:40:22 +01004474 priv->hw->mac->set_mac(priv->ioaddr, false);
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00004475 pinctrl_pm_select_sleep_state(priv->device);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00004476 /* Disable clock in case of PWM is off */
jpintof573c0b2017-01-09 12:35:09 +00004477 clk_disable(priv->plat->pclk);
4478 clk_disable(priv->plat->stmmac_clk);
Giuseppe CAVALLAROba1377ff2012-04-04 04:33:25 +00004479 }
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004480 spin_unlock_irqrestore(&priv->lock, flags);
Vince Bridgers2d871aa2014-07-28 14:07:58 -05004481
LABBE Corentin4d869b02017-05-24 09:16:46 +02004482 priv->oldlink = false;
LABBE Corentinbd006322017-02-15 10:46:40 +01004483 priv->speed = SPEED_UNKNOWN;
4484 priv->oldduplex = DUPLEX_UNKNOWN;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004485 return 0;
4486}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004487EXPORT_SYMBOL_GPL(stmmac_suspend);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004488
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004489/**
Joao Pinto54139cf2017-04-06 09:49:09 +01004490 * stmmac_reset_queues_param - reset queue parameters
4491 * @dev: device pointer
4492 */
4493static void stmmac_reset_queues_param(struct stmmac_priv *priv)
4494{
4495 u32 rx_cnt = priv->plat->rx_queues_to_use;
Joao Pintoce736782017-04-06 09:49:10 +01004496 u32 tx_cnt = priv->plat->tx_queues_to_use;
Joao Pinto54139cf2017-04-06 09:49:09 +01004497 u32 queue;
4498
4499 for (queue = 0; queue < rx_cnt; queue++) {
4500 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4501
4502 rx_q->cur_rx = 0;
4503 rx_q->dirty_rx = 0;
4504 }
4505
Joao Pintoce736782017-04-06 09:49:10 +01004506 for (queue = 0; queue < tx_cnt; queue++) {
4507 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4508
4509 tx_q->cur_tx = 0;
4510 tx_q->dirty_tx = 0;
Niklas Cassel8d212a9e2018-02-19 18:11:09 +01004511 tx_q->mss = 0;
Joao Pintoce736782017-04-06 09:49:10 +01004512 }
Joao Pinto54139cf2017-04-06 09:49:09 +01004513}
4514
4515/**
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004516 * stmmac_resume - resume callback
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004517 * @dev: device pointer
Giuseppe CAVALLARO732fdf02014-11-18 09:47:01 +01004518 * Description: when resume this function is invoked to setup the DMA and CORE
4519 * in a usable state.
4520 */
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004521int stmmac_resume(struct device *dev)
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004522{
Joachim Eastwoodf4e7bd82016-05-01 22:58:19 +02004523 struct net_device *ndev = dev_get_drvdata(dev);
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004524 struct stmmac_priv *priv = netdev_priv(ndev);
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004525 unsigned long flags;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004526
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004527 if (!netif_running(ndev))
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004528 return 0;
4529
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004530 /* Power Down bit, into the PM register, is cleared
4531 * automatically as soon as a magic packet or a Wake-up frame
4532 * is received. Anyway, it's better to manually clear
4533 * this bit because it can generate problems while resuming
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00004534 * from another devices (e.g. serial console).
4535 */
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00004536 if (device_may_wakeup(priv->device)) {
Vincent Palatinf55d84b2016-06-01 08:53:48 -07004537 spin_lock_irqsave(&priv->lock, flags);
Vince Bridgers7ed24bb2014-07-31 15:49:13 -05004538 priv->hw->mac->pmt(priv->hw, 0);
Vincent Palatinf55d84b2016-06-01 08:53:48 -07004539 spin_unlock_irqrestore(&priv->lock, flags);
Srinivas Kandagatla89f7f2c2014-01-16 10:53:00 +00004540 priv->irq_wake = 0;
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00004541 } else {
Srinivas Kandagatladb88f102014-01-16 10:52:52 +00004542 pinctrl_pm_select_default_state(priv->device);
LABBE Corentin8d45e422017-02-08 09:31:08 +01004543 /* enable the clk previously disabled */
jpintof573c0b2017-01-09 12:35:09 +00004544 clk_enable(priv->plat->stmmac_clk);
4545 clk_enable(priv->plat->pclk);
Srinivas Kandagatla623997f2014-01-16 10:52:35 +00004546 /* reset the phy so that it's ready */
4547 if (priv->mii)
4548 stmmac_mdio_reset(priv->mii);
4549 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004550
Giuseppe CAVALLARO874bd422010-11-24 02:38:11 +00004551 netif_device_attach(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004552
Vincent Palatinf55d84b2016-06-01 08:53:48 -07004553 spin_lock_irqsave(&priv->lock, flags);
4554
Joao Pinto54139cf2017-04-06 09:49:09 +01004555 stmmac_reset_queues_param(priv);
4556
Giuseppe CAVALLAROae79a632015-12-04 07:21:06 +01004557 stmmac_clear_descriptors(priv);
4558
Huacai Chenfe1319292014-12-19 22:38:18 +08004559 stmmac_hw_setup(ndev, false);
Giuseppe CAVALLARO777da232014-11-04 17:08:09 +01004560 stmmac_init_tx_coalesce(priv);
Giuseppe CAVALLAROac316c72015-11-26 08:35:41 +01004561 stmmac_set_rx_mode(ndev);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004562
Joao Pintoc22a3f42017-04-06 09:49:11 +01004563 stmmac_enable_all_queues(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004564
Joao Pintoc22a3f42017-04-06 09:49:11 +01004565 stmmac_start_all_queues(priv);
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004566
Giuseppe CAVALLAROf8c5a872012-05-13 22:18:43 +00004567 spin_unlock_irqrestore(&priv->lock, flags);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00004568
Philippe Reynesd6d50c72016-10-03 08:28:19 +02004569 if (ndev->phydev)
4570 phy_start(ndev->phydev);
Francesco Virlinzi102463b2011-11-16 21:58:02 +00004571
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004572 return 0;
4573}
Andy Shevchenkob2e2f0c2014-11-10 12:38:59 +02004574EXPORT_SYMBOL_GPL(stmmac_resume);
Giuseppe CAVALLAROba27ec62012-06-04 19:22:57 +00004575
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004576#ifndef MODULE
4577static int __init stmmac_cmdline_opt(char *str)
4578{
4579 char *opt;
4580
4581 if (!str || !*str)
4582 return -EINVAL;
4583 while ((opt = strsep(&str, ",")) != NULL) {
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004584 if (!strncmp(opt, "debug:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004585 if (kstrtoint(opt + 6, 0, &debug))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004586 goto err;
4587 } else if (!strncmp(opt, "phyaddr:", 8)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004588 if (kstrtoint(opt + 8, 0, &phyaddr))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004589 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004590 } else if (!strncmp(opt, "buf_sz:", 7)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004591 if (kstrtoint(opt + 7, 0, &buf_sz))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004592 goto err;
4593 } else if (!strncmp(opt, "tc:", 3)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004594 if (kstrtoint(opt + 3, 0, &tc))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004595 goto err;
4596 } else if (!strncmp(opt, "watchdog:", 9)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004597 if (kstrtoint(opt + 9, 0, &watchdog))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004598 goto err;
4599 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004600 if (kstrtoint(opt + 10, 0, &flow_ctrl))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004601 goto err;
4602 } else if (!strncmp(opt, "pause:", 6)) {
Giuseppe CAVALLAROea2ab872012-06-27 21:14:35 +00004603 if (kstrtoint(opt + 6, 0, &pause))
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004604 goto err;
Giuseppe CAVALLARO506f6692013-02-14 23:00:13 +00004605 } else if (!strncmp(opt, "eee_timer:", 10)) {
Giuseppe CAVALLAROd7659552012-06-27 21:14:37 +00004606 if (kstrtoint(opt + 10, 0, &eee_timer))
4607 goto err;
Giuseppe CAVALLARO4a7d6662013-03-26 04:43:05 +00004608 } else if (!strncmp(opt, "chain_mode:", 11)) {
4609 if (kstrtoint(opt + 11, 0, &chain_mode))
4610 goto err;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004611 }
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004612 }
4613 return 0;
Giuseppe CAVALLAROf3240e22011-07-20 00:05:22 +00004614
4615err:
4616 pr_err("%s: ERROR broken module parameter conversion", __func__);
4617 return -EINVAL;
Giuseppe Cavallaro47dd7a52009-10-14 15:13:45 -07004618}
4619
4620__setup("stmmaceth=", stmmac_cmdline_opt);
Giuseppe CAVALLAROceb694992013-04-08 02:10:01 +00004621#endif /* MODULE */
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05004622
Mathieu Olivari466c5ac2015-05-22 19:03:29 -07004623static int __init stmmac_init(void)
4624{
4625#ifdef CONFIG_DEBUG_FS
4626 /* Create debugfs main directory if it doesn't exist yet */
4627 if (!stmmac_fs_dir) {
4628 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
4629
4630 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
4631 pr_err("ERROR %s, debugfs create directory failed\n",
4632 STMMAC_RESOURCE_NAME);
4633
4634 return -ENOMEM;
4635 }
4636 }
4637#endif
4638
4639 return 0;
4640}
4641
4642static void __exit stmmac_exit(void)
4643{
4644#ifdef CONFIG_DEBUG_FS
4645 debugfs_remove_recursive(stmmac_fs_dir);
4646#endif
4647}
4648
4649module_init(stmmac_init)
4650module_exit(stmmac_exit)
4651
Giuseppe Cavallaro6fc0d0f2011-12-23 14:21:20 -05004652MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
4653MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
4654MODULE_LICENSE("GPL");