Linux 4.19.133
[linux/fpc-iii.git] / drivers / net / ethernet / stmicro / stmmac / stmmac_main.c
blobc41879a955b57e45dcbbcfa533daf6505a9e1d02
1 /*******************************************************************************
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.
5 Copyright(C) 2007-2011 STMicroelectronics Ltd
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.
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.
16 The full GNU General Public License is included in this distribution in
17 the file called "COPYING".
19 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
21 Documentation available at:
22 http://www.stlinux.com
23 Support available at:
24 https://bugzilla.stlinux.com/
25 *******************************************************************************/
27 #include <linux/clk.h>
28 #include <linux/kernel.h>
29 #include <linux/interrupt.h>
30 #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>
37 #include <linux/if.h>
38 #include <linux/if_vlan.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/slab.h>
41 #include <linux/prefetch.h>
42 #include <linux/pinctrl/consumer.h>
43 #ifdef CONFIG_DEBUG_FS
44 #include <linux/debugfs.h>
45 #include <linux/seq_file.h>
46 #endif /* CONFIG_DEBUG_FS */
47 #include <linux/net_tstamp.h>
48 #include <net/pkt_cls.h>
49 #include "stmmac_ptp.h"
50 #include "stmmac.h"
51 #include <linux/reset.h>
52 #include <linux/of_mdio.h>
53 #include "dwmac1000.h"
54 #include "dwxgmac2.h"
55 #include "hwif.h"
57 #define STMMAC_ALIGN(x) ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16)
58 #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
60 /* Module parameters */
61 #define TX_TIMEO 5000
62 static int watchdog = TX_TIMEO;
63 module_param(watchdog, int, 0644);
64 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
66 static int debug = -1;
67 module_param(debug, int, 0644);
68 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
70 static int phyaddr = -1;
71 module_param(phyaddr, int, 0444);
72 MODULE_PARM_DESC(phyaddr, "Physical device address");
74 #define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
75 #define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
77 static int flow_ctrl = FLOW_OFF;
78 module_param(flow_ctrl, int, 0644);
79 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
81 static int pause = PAUSE_TIME;
82 module_param(pause, int, 0644);
83 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
85 #define TC_DEFAULT 64
86 static int tc = TC_DEFAULT;
87 module_param(tc, int, 0644);
88 MODULE_PARM_DESC(tc, "DMA threshold control value");
90 #define DEFAULT_BUFSIZE 1536
91 static int buf_sz = DEFAULT_BUFSIZE;
92 module_param(buf_sz, int, 0644);
93 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
95 #define STMMAC_RX_COPYBREAK 256
97 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
98 NETIF_MSG_LINK | NETIF_MSG_IFUP |
99 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
101 #define STMMAC_DEFAULT_LPI_TIMER 1000
102 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
103 module_param(eee_timer, int, 0644);
104 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
105 #define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
107 /* By default the driver will use the ring mode to manage tx and rx descriptors,
108 * but allow user to force to use the chain instead of the ring
110 static unsigned int chain_mode;
111 module_param(chain_mode, int, 0444);
112 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
114 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
116 #ifdef CONFIG_DEBUG_FS
117 static int stmmac_init_fs(struct net_device *dev);
118 static void stmmac_exit_fs(struct net_device *dev);
119 #endif
121 #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
124 * stmmac_verify_args - verify the driver parameters.
125 * Description: it checks the driver parameters and set a default in case of
126 * errors.
128 static void stmmac_verify_args(void)
130 if (unlikely(watchdog < 0))
131 watchdog = TX_TIMEO;
132 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
133 buf_sz = DEFAULT_BUFSIZE;
134 if (unlikely(flow_ctrl > 1))
135 flow_ctrl = FLOW_AUTO;
136 else if (likely(flow_ctrl < 0))
137 flow_ctrl = FLOW_OFF;
138 if (unlikely((pause < 0) || (pause > 0xffff)))
139 pause = PAUSE_TIME;
140 if (eee_timer < 0)
141 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
145 * stmmac_disable_all_queues - Disable all queues
146 * @priv: driver private structure
148 static void stmmac_disable_all_queues(struct stmmac_priv *priv)
150 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
151 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
152 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
153 u32 queue;
155 for (queue = 0; queue < maxq; queue++) {
156 struct stmmac_channel *ch = &priv->channel[queue];
158 napi_disable(&ch->napi);
163 * stmmac_enable_all_queues - Enable all queues
164 * @priv: driver private structure
166 static void stmmac_enable_all_queues(struct stmmac_priv *priv)
168 u32 rx_queues_cnt = priv->plat->rx_queues_to_use;
169 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
170 u32 maxq = max(rx_queues_cnt, tx_queues_cnt);
171 u32 queue;
173 for (queue = 0; queue < maxq; queue++) {
174 struct stmmac_channel *ch = &priv->channel[queue];
176 napi_enable(&ch->napi);
181 * stmmac_stop_all_queues - Stop all queues
182 * @priv: driver private structure
184 static void stmmac_stop_all_queues(struct stmmac_priv *priv)
186 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
187 u32 queue;
189 for (queue = 0; queue < tx_queues_cnt; queue++)
190 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
194 * stmmac_start_all_queues - Start all queues
195 * @priv: driver private structure
197 static void stmmac_start_all_queues(struct stmmac_priv *priv)
199 u32 tx_queues_cnt = priv->plat->tx_queues_to_use;
200 u32 queue;
202 for (queue = 0; queue < tx_queues_cnt; queue++)
203 netif_tx_start_queue(netdev_get_tx_queue(priv->dev, queue));
206 static void stmmac_service_event_schedule(struct stmmac_priv *priv)
208 if (!test_bit(STMMAC_DOWN, &priv->state) &&
209 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state))
210 queue_work(priv->wq, &priv->service_task);
213 static void stmmac_global_err(struct stmmac_priv *priv)
215 netif_carrier_off(priv->dev);
216 set_bit(STMMAC_RESET_REQUESTED, &priv->state);
217 stmmac_service_event_schedule(priv);
221 * stmmac_clk_csr_set - dynamically set the MDC clock
222 * @priv: driver private structure
223 * Description: this is to dynamically set the MDC clock according to the csr
224 * clock input.
225 * Note:
226 * If a specific clk_csr value is passed from the platform
227 * this means that the CSR Clock Range selection cannot be
228 * changed at run-time and it is fixed (as reported in the driver
229 * documentation). Viceversa the driver will try to set the MDC
230 * clock dynamically according to the actual clock input.
232 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
234 u32 clk_rate;
236 clk_rate = clk_get_rate(priv->plat->stmmac_clk);
238 /* Platform provided default clk_csr would be assumed valid
239 * for all other cases except for the below mentioned ones.
240 * For values higher than the IEEE 802.3 specified frequency
241 * we can not estimate the proper divider as it is not known
242 * the frequency of clk_csr_i. So we do not change the default
243 * divider.
245 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
246 if (clk_rate < CSR_F_35M)
247 priv->clk_csr = STMMAC_CSR_20_35M;
248 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
249 priv->clk_csr = STMMAC_CSR_35_60M;
250 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
251 priv->clk_csr = STMMAC_CSR_60_100M;
252 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
253 priv->clk_csr = STMMAC_CSR_100_150M;
254 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
255 priv->clk_csr = STMMAC_CSR_150_250M;
256 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
257 priv->clk_csr = STMMAC_CSR_250_300M;
260 if (priv->plat->has_sun8i) {
261 if (clk_rate > 160000000)
262 priv->clk_csr = 0x03;
263 else if (clk_rate > 80000000)
264 priv->clk_csr = 0x02;
265 else if (clk_rate > 40000000)
266 priv->clk_csr = 0x01;
267 else
268 priv->clk_csr = 0;
271 if (priv->plat->has_xgmac) {
272 if (clk_rate > 400000000)
273 priv->clk_csr = 0x5;
274 else if (clk_rate > 350000000)
275 priv->clk_csr = 0x4;
276 else if (clk_rate > 300000000)
277 priv->clk_csr = 0x3;
278 else if (clk_rate > 250000000)
279 priv->clk_csr = 0x2;
280 else if (clk_rate > 150000000)
281 priv->clk_csr = 0x1;
282 else
283 priv->clk_csr = 0x0;
287 static void print_pkt(unsigned char *buf, int len)
289 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
290 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
293 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue)
295 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
296 u32 avail;
298 if (tx_q->dirty_tx > tx_q->cur_tx)
299 avail = tx_q->dirty_tx - tx_q->cur_tx - 1;
300 else
301 avail = DMA_TX_SIZE - tx_q->cur_tx + tx_q->dirty_tx - 1;
303 return avail;
307 * stmmac_rx_dirty - Get RX queue dirty
308 * @priv: driver private structure
309 * @queue: RX queue index
311 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue)
313 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
314 u32 dirty;
316 if (rx_q->dirty_rx <= rx_q->cur_rx)
317 dirty = rx_q->cur_rx - rx_q->dirty_rx;
318 else
319 dirty = DMA_RX_SIZE - rx_q->dirty_rx + rx_q->cur_rx;
321 return dirty;
325 * stmmac_hw_fix_mac_speed - callback for speed selection
326 * @priv: driver private structure
327 * Description: on some platforms (e.g. ST), some HW system configuration
328 * registers have to be set according to the link speed negotiated.
330 static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
332 struct net_device *ndev = priv->dev;
333 struct phy_device *phydev = ndev->phydev;
335 if (likely(priv->plat->fix_mac_speed))
336 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
340 * stmmac_enable_eee_mode - check and enter in LPI mode
341 * @priv: driver private structure
342 * Description: this function is to verify and enter in LPI mode in case of
343 * EEE.
345 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
347 u32 tx_cnt = priv->plat->tx_queues_to_use;
348 u32 queue;
350 /* check if all TX queues have the work finished */
351 for (queue = 0; queue < tx_cnt; queue++) {
352 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
354 if (tx_q->dirty_tx != tx_q->cur_tx)
355 return; /* still unfinished work */
358 /* Check and enter in LPI mode */
359 if (!priv->tx_path_in_lpi_mode)
360 stmmac_set_eee_mode(priv, priv->hw,
361 priv->plat->en_tx_lpi_clockgating);
365 * stmmac_disable_eee_mode - disable and exit from LPI mode
366 * @priv: driver private structure
367 * Description: this function is to exit and disable EEE in case of
368 * LPI state is true. This is called by the xmit.
370 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
372 stmmac_reset_eee_mode(priv, priv->hw);
373 del_timer_sync(&priv->eee_ctrl_timer);
374 priv->tx_path_in_lpi_mode = false;
378 * stmmac_eee_ctrl_timer - EEE TX SW timer.
379 * @arg : data hook
380 * Description:
381 * if there is no data transfer and if we are not in LPI state,
382 * then MAC Transmitter can be moved to LPI state.
384 static void stmmac_eee_ctrl_timer(struct timer_list *t)
386 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer);
388 stmmac_enable_eee_mode(priv);
389 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
393 * stmmac_eee_init - init EEE
394 * @priv: driver private structure
395 * Description:
396 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
397 * can also manage EEE, this function enable the LPI state and start related
398 * timer.
400 bool stmmac_eee_init(struct stmmac_priv *priv)
402 struct net_device *ndev = priv->dev;
403 int interface = priv->plat->interface;
404 bool ret = false;
406 if ((interface != PHY_INTERFACE_MODE_MII) &&
407 (interface != PHY_INTERFACE_MODE_GMII) &&
408 !phy_interface_mode_is_rgmii(interface))
409 goto out;
411 /* Using PCS we cannot dial with the phy registers at this stage
412 * so we do not support extra feature like EEE.
414 if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
415 (priv->hw->pcs == STMMAC_PCS_TBI) ||
416 (priv->hw->pcs == STMMAC_PCS_RTBI))
417 goto out;
419 /* MAC core supports the EEE feature. */
420 if (priv->dma_cap.eee) {
421 int tx_lpi_timer = priv->tx_lpi_timer;
423 /* Check if the PHY supports EEE */
424 if (phy_init_eee(ndev->phydev, 1)) {
425 /* To manage at run-time if the EEE cannot be supported
426 * anymore (for example because the lp caps have been
427 * changed).
428 * In that case the driver disable own timers.
430 mutex_lock(&priv->lock);
431 if (priv->eee_active) {
432 netdev_dbg(priv->dev, "disable EEE\n");
433 del_timer_sync(&priv->eee_ctrl_timer);
434 stmmac_set_eee_timer(priv, priv->hw, 0,
435 tx_lpi_timer);
437 priv->eee_active = 0;
438 mutex_unlock(&priv->lock);
439 goto out;
441 /* Activate the EEE and start timers */
442 mutex_lock(&priv->lock);
443 if (!priv->eee_active) {
444 priv->eee_active = 1;
445 timer_setup(&priv->eee_ctrl_timer,
446 stmmac_eee_ctrl_timer, 0);
447 mod_timer(&priv->eee_ctrl_timer,
448 STMMAC_LPI_T(eee_timer));
450 stmmac_set_eee_timer(priv, priv->hw,
451 STMMAC_DEFAULT_LIT_LS, tx_lpi_timer);
453 /* Set HW EEE according to the speed */
454 stmmac_set_eee_pls(priv, priv->hw, ndev->phydev->link);
456 ret = true;
457 mutex_unlock(&priv->lock);
459 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n");
461 out:
462 return ret;
465 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
466 * @priv: driver private structure
467 * @p : descriptor pointer
468 * @skb : the socket buffer
469 * Description :
470 * This function will read timestamp from the descriptor & pass it to stack.
471 * and also perform some sanity checks.
473 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
474 struct dma_desc *p, struct sk_buff *skb)
476 struct skb_shared_hwtstamps shhwtstamp;
477 u64 ns = 0;
479 if (!priv->hwts_tx_en)
480 return;
482 /* exit if skb doesn't support hw tstamp */
483 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
484 return;
486 /* check tx tstamp status */
487 if (stmmac_get_tx_timestamp_status(priv, p)) {
488 /* get the valid tstamp */
489 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns);
491 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
492 shhwtstamp.hwtstamp = ns_to_ktime(ns);
494 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns);
495 /* pass tstamp to stack */
496 skb_tstamp_tx(skb, &shhwtstamp);
499 return;
502 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
503 * @priv: driver private structure
504 * @p : descriptor pointer
505 * @np : next descriptor pointer
506 * @skb : the socket buffer
507 * Description :
508 * This function will read received packet's timestamp from the descriptor
509 * and pass it to stack. It also perform some sanity checks.
511 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
512 struct dma_desc *np, struct sk_buff *skb)
514 struct skb_shared_hwtstamps *shhwtstamp = NULL;
515 struct dma_desc *desc = p;
516 u64 ns = 0;
518 if (!priv->hwts_rx_en)
519 return;
520 /* For GMAC4, the valid timestamp is from CTX next desc. */
521 if (priv->plat->has_gmac4 || priv->plat->has_xgmac)
522 desc = np;
524 /* Check if timestamp is available */
525 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) {
526 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns);
527 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns);
528 shhwtstamp = skb_hwtstamps(skb);
529 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
530 shhwtstamp->hwtstamp = ns_to_ktime(ns);
531 } else {
532 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n");
537 * stmmac_hwtstamp_ioctl - control hardware timestamping.
538 * @dev: device pointer.
539 * @ifr: An IOCTL specific structure, that can contain a pointer to
540 * a proprietary structure used to pass information to the driver.
541 * Description:
542 * This function configures the MAC to enable/disable both outgoing(TX)
543 * and incoming(RX) packets time stamping based on user input.
544 * Return Value:
545 * 0 on success and an appropriate -ve integer on failure.
547 static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
549 struct stmmac_priv *priv = netdev_priv(dev);
550 struct hwtstamp_config config;
551 struct timespec64 now;
552 u64 temp = 0;
553 u32 ptp_v2 = 0;
554 u32 tstamp_all = 0;
555 u32 ptp_over_ipv4_udp = 0;
556 u32 ptp_over_ipv6_udp = 0;
557 u32 ptp_over_ethernet = 0;
558 u32 snap_type_sel = 0;
559 u32 ts_master_en = 0;
560 u32 ts_event_en = 0;
561 u32 sec_inc = 0;
562 u32 value = 0;
563 bool xmac;
565 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
567 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
568 netdev_alert(priv->dev, "No support for HW time stamping\n");
569 priv->hwts_tx_en = 0;
570 priv->hwts_rx_en = 0;
572 return -EOPNOTSUPP;
575 if (copy_from_user(&config, ifr->ifr_data,
576 sizeof(struct hwtstamp_config)))
577 return -EFAULT;
579 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
580 __func__, config.flags, config.tx_type, config.rx_filter);
582 /* reserved for future extensions */
583 if (config.flags)
584 return -EINVAL;
586 if (config.tx_type != HWTSTAMP_TX_OFF &&
587 config.tx_type != HWTSTAMP_TX_ON)
588 return -ERANGE;
590 if (priv->adv_ts) {
591 switch (config.rx_filter) {
592 case HWTSTAMP_FILTER_NONE:
593 /* time stamp no incoming packet at all */
594 config.rx_filter = HWTSTAMP_FILTER_NONE;
595 break;
597 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
598 /* PTP v1, UDP, any kind of event packet */
599 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
600 /* take time stamp for all event messages */
601 if (xmac)
602 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
603 else
604 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
606 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
607 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
608 break;
610 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
611 /* PTP v1, UDP, Sync packet */
612 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
613 /* take time stamp for SYNC messages only */
614 ts_event_en = PTP_TCR_TSEVNTENA;
616 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
617 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
618 break;
620 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
621 /* PTP v1, UDP, Delay_req packet */
622 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
623 /* take time stamp for Delay_Req messages only */
624 ts_master_en = PTP_TCR_TSMSTRENA;
625 ts_event_en = PTP_TCR_TSEVNTENA;
627 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
628 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
629 break;
631 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
632 /* PTP v2, UDP, any kind of event packet */
633 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
634 ptp_v2 = PTP_TCR_TSVER2ENA;
635 /* take time stamp for all event messages */
636 if (xmac)
637 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
638 else
639 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
641 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
642 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
643 break;
645 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
646 /* PTP v2, UDP, Sync packet */
647 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
648 ptp_v2 = PTP_TCR_TSVER2ENA;
649 /* take time stamp for SYNC messages only */
650 ts_event_en = PTP_TCR_TSEVNTENA;
652 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
653 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
654 break;
656 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
657 /* PTP v2, UDP, Delay_req packet */
658 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
659 ptp_v2 = PTP_TCR_TSVER2ENA;
660 /* take time stamp for Delay_Req messages only */
661 ts_master_en = PTP_TCR_TSMSTRENA;
662 ts_event_en = PTP_TCR_TSEVNTENA;
664 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
665 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
666 break;
668 case HWTSTAMP_FILTER_PTP_V2_EVENT:
669 /* PTP v2/802.AS1 any layer, any kind of event packet */
670 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
671 ptp_v2 = PTP_TCR_TSVER2ENA;
672 /* take time stamp for all event messages */
673 if (xmac)
674 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
675 else
676 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
678 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
679 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
680 ptp_over_ethernet = PTP_TCR_TSIPENA;
681 break;
683 case HWTSTAMP_FILTER_PTP_V2_SYNC:
684 /* PTP v2/802.AS1, any layer, Sync packet */
685 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
686 ptp_v2 = PTP_TCR_TSVER2ENA;
687 /* take time stamp for SYNC messages only */
688 ts_event_en = PTP_TCR_TSEVNTENA;
690 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
691 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
692 ptp_over_ethernet = PTP_TCR_TSIPENA;
693 break;
695 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
696 /* PTP v2/802.AS1, any layer, Delay_req packet */
697 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
698 ptp_v2 = PTP_TCR_TSVER2ENA;
699 /* take time stamp for Delay_Req messages only */
700 ts_master_en = PTP_TCR_TSMSTRENA;
701 ts_event_en = PTP_TCR_TSEVNTENA;
703 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
704 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
705 ptp_over_ethernet = PTP_TCR_TSIPENA;
706 break;
708 case HWTSTAMP_FILTER_NTP_ALL:
709 case HWTSTAMP_FILTER_ALL:
710 /* time stamp any incoming packet */
711 config.rx_filter = HWTSTAMP_FILTER_ALL;
712 tstamp_all = PTP_TCR_TSENALL;
713 break;
715 default:
716 return -ERANGE;
718 } else {
719 switch (config.rx_filter) {
720 case HWTSTAMP_FILTER_NONE:
721 config.rx_filter = HWTSTAMP_FILTER_NONE;
722 break;
723 default:
724 /* PTP v1, UDP, any kind of event packet */
725 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
726 break;
729 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
730 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
732 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
733 stmmac_config_hw_tstamping(priv, priv->ptpaddr, 0);
734 else {
735 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
736 tstamp_all | ptp_v2 | ptp_over_ethernet |
737 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
738 ts_master_en | snap_type_sel);
739 stmmac_config_hw_tstamping(priv, priv->ptpaddr, value);
741 /* program Sub Second Increment reg */
742 stmmac_config_sub_second_increment(priv,
743 priv->ptpaddr, priv->plat->clk_ptp_rate,
744 xmac, &sec_inc);
745 temp = div_u64(1000000000ULL, sec_inc);
747 /* Store sub second increment and flags for later use */
748 priv->sub_second_inc = sec_inc;
749 priv->systime_flags = value;
751 /* calculate default added value:
752 * formula is :
753 * addend = (2^32)/freq_div_ratio;
754 * where, freq_div_ratio = 1e9ns/sec_inc
756 temp = (u64)(temp << 32);
757 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate);
758 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend);
760 /* initialize system time */
761 ktime_get_real_ts64(&now);
763 /* lower 32 bits of tv_sec are safe until y2106 */
764 stmmac_init_systime(priv, priv->ptpaddr,
765 (u32)now.tv_sec, now.tv_nsec);
768 return copy_to_user(ifr->ifr_data, &config,
769 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
773 * stmmac_init_ptp - init PTP
774 * @priv: driver private structure
775 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
776 * This is done by looking at the HW cap. register.
777 * This function also registers the ptp driver.
779 static int stmmac_init_ptp(struct stmmac_priv *priv)
781 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
783 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
784 return -EOPNOTSUPP;
786 priv->adv_ts = 0;
787 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */
788 if (xmac && priv->dma_cap.atime_stamp)
789 priv->adv_ts = 1;
790 /* Dwmac 3.x core with extend_desc can support adv_ts */
791 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
792 priv->adv_ts = 1;
794 if (priv->dma_cap.time_stamp)
795 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
797 if (priv->adv_ts)
798 netdev_info(priv->dev,
799 "IEEE 1588-2008 Advanced Timestamp supported\n");
801 priv->hwts_tx_en = 0;
802 priv->hwts_rx_en = 0;
804 stmmac_ptp_register(priv);
806 return 0;
809 static void stmmac_release_ptp(struct stmmac_priv *priv)
811 if (priv->plat->clk_ptp_ref)
812 clk_disable_unprepare(priv->plat->clk_ptp_ref);
813 stmmac_ptp_unregister(priv);
817 * stmmac_mac_flow_ctrl - Configure flow control in all queues
818 * @priv: driver private structure
819 * Description: It is used for configuring the flow control in all queues
821 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex)
823 u32 tx_cnt = priv->plat->tx_queues_to_use;
825 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl,
826 priv->pause, tx_cnt);
830 * stmmac_adjust_link - adjusts the link parameters
831 * @dev: net device structure
832 * Description: this is the helper called by the physical abstraction layer
833 * drivers to communicate the phy link status. According the speed and duplex
834 * this driver can invoke registered glue-logic as well.
835 * It also invoke the eee initialization because it could happen when switch
836 * on different networks (that are eee capable).
838 static void stmmac_adjust_link(struct net_device *dev)
840 struct stmmac_priv *priv = netdev_priv(dev);
841 struct phy_device *phydev = dev->phydev;
842 bool new_state = false;
844 if (!phydev)
845 return;
847 mutex_lock(&priv->lock);
849 if (phydev->link) {
850 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
852 /* Now we make sure that we can be in full duplex mode.
853 * If not, we operate in half-duplex mode. */
854 if (phydev->duplex != priv->oldduplex) {
855 new_state = true;
856 if (!phydev->duplex)
857 ctrl &= ~priv->hw->link.duplex;
858 else
859 ctrl |= priv->hw->link.duplex;
860 priv->oldduplex = phydev->duplex;
862 /* Flow Control operation */
863 if (phydev->pause)
864 stmmac_mac_flow_ctrl(priv, phydev->duplex);
866 if (phydev->speed != priv->speed) {
867 new_state = true;
868 ctrl &= ~priv->hw->link.speed_mask;
869 switch (phydev->speed) {
870 case SPEED_1000:
871 ctrl |= priv->hw->link.speed1000;
872 break;
873 case SPEED_100:
874 ctrl |= priv->hw->link.speed100;
875 break;
876 case SPEED_10:
877 ctrl |= priv->hw->link.speed10;
878 break;
879 default:
880 netif_warn(priv, link, priv->dev,
881 "broken speed: %d\n", phydev->speed);
882 phydev->speed = SPEED_UNKNOWN;
883 break;
885 if (phydev->speed != SPEED_UNKNOWN)
886 stmmac_hw_fix_mac_speed(priv);
887 priv->speed = phydev->speed;
890 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
892 if (!priv->oldlink) {
893 new_state = true;
894 priv->oldlink = true;
896 } else if (priv->oldlink) {
897 new_state = true;
898 priv->oldlink = false;
899 priv->speed = SPEED_UNKNOWN;
900 priv->oldduplex = DUPLEX_UNKNOWN;
903 if (new_state && netif_msg_link(priv))
904 phy_print_status(phydev);
906 mutex_unlock(&priv->lock);
908 if (phydev->is_pseudo_fixed_link)
909 /* Stop PHY layer to call the hook to adjust the link in case
910 * of a switch is attached to the stmmac driver.
912 phydev->irq = PHY_IGNORE_INTERRUPT;
913 else
914 /* At this stage, init the EEE if supported.
915 * Never called in case of fixed_link.
917 priv->eee_enabled = stmmac_eee_init(priv);
921 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
922 * @priv: driver private structure
923 * Description: this is to verify if the HW supports the PCS.
924 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
925 * configured for the TBI, RTBI, or SGMII PHY interface.
927 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
929 int interface = priv->plat->interface;
931 if (priv->dma_cap.pcs) {
932 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
933 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
934 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
935 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
936 netdev_dbg(priv->dev, "PCS RGMII support enabled\n");
937 priv->hw->pcs = STMMAC_PCS_RGMII;
938 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
939 netdev_dbg(priv->dev, "PCS SGMII support enabled\n");
940 priv->hw->pcs = STMMAC_PCS_SGMII;
946 * stmmac_init_phy - PHY initialization
947 * @dev: net device structure
948 * Description: it initializes the driver's PHY state, and attaches the PHY
949 * to the mac driver.
950 * Return value:
951 * 0 on success
953 static int stmmac_init_phy(struct net_device *dev)
955 struct stmmac_priv *priv = netdev_priv(dev);
956 u32 tx_cnt = priv->plat->tx_queues_to_use;
957 struct phy_device *phydev;
958 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
959 char bus_id[MII_BUS_ID_SIZE];
960 int interface = priv->plat->interface;
961 int max_speed = priv->plat->max_speed;
962 priv->oldlink = false;
963 priv->speed = SPEED_UNKNOWN;
964 priv->oldduplex = DUPLEX_UNKNOWN;
966 if (priv->plat->phy_node) {
967 phydev = of_phy_connect(dev, priv->plat->phy_node,
968 &stmmac_adjust_link, 0, interface);
969 } else {
970 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
971 priv->plat->bus_id);
973 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
974 priv->plat->phy_addr);
975 netdev_dbg(priv->dev, "%s: trying to attach to %s\n", __func__,
976 phy_id_fmt);
978 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
979 interface);
982 if (IS_ERR_OR_NULL(phydev)) {
983 netdev_err(priv->dev, "Could not attach to PHY\n");
984 if (!phydev)
985 return -ENODEV;
987 return PTR_ERR(phydev);
990 /* Stop Advertising 1000BASE Capability if interface is not GMII */
991 if ((interface == PHY_INTERFACE_MODE_MII) ||
992 (interface == PHY_INTERFACE_MODE_RMII) ||
993 (max_speed < 1000 && max_speed > 0))
994 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
995 SUPPORTED_1000baseT_Full);
998 * Half-duplex mode not supported with multiqueue
999 * half-duplex can only works with single queue
1001 if (tx_cnt > 1)
1002 phydev->supported &= ~(SUPPORTED_1000baseT_Half |
1003 SUPPORTED_100baseT_Half |
1004 SUPPORTED_10baseT_Half);
1007 * Broken HW is sometimes missing the pull-up resistor on the
1008 * MDIO line, which results in reads to non-existent devices returning
1009 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
1010 * device as well.
1011 * Note: phydev->phy_id is the result of reading the UID PHY registers.
1013 if (!priv->plat->phy_node && phydev->phy_id == 0) {
1014 phy_disconnect(phydev);
1015 return -ENODEV;
1018 /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
1019 * subsequent PHY polling, make sure we force a link transition if
1020 * we have a UP/DOWN/UP transition
1022 if (phydev->is_pseudo_fixed_link)
1023 phydev->irq = PHY_POLL;
1025 phy_attached_info(phydev);
1026 return 0;
1029 static void stmmac_display_rx_rings(struct stmmac_priv *priv)
1031 u32 rx_cnt = priv->plat->rx_queues_to_use;
1032 void *head_rx;
1033 u32 queue;
1035 /* Display RX rings */
1036 for (queue = 0; queue < rx_cnt; queue++) {
1037 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1039 pr_info("\tRX Queue %u rings\n", queue);
1041 if (priv->extend_desc)
1042 head_rx = (void *)rx_q->dma_erx;
1043 else
1044 head_rx = (void *)rx_q->dma_rx;
1046 /* Display RX ring */
1047 stmmac_display_ring(priv, head_rx, DMA_RX_SIZE, true);
1051 static void stmmac_display_tx_rings(struct stmmac_priv *priv)
1053 u32 tx_cnt = priv->plat->tx_queues_to_use;
1054 void *head_tx;
1055 u32 queue;
1057 /* Display TX rings */
1058 for (queue = 0; queue < tx_cnt; queue++) {
1059 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1061 pr_info("\tTX Queue %d rings\n", queue);
1063 if (priv->extend_desc)
1064 head_tx = (void *)tx_q->dma_etx;
1065 else
1066 head_tx = (void *)tx_q->dma_tx;
1068 stmmac_display_ring(priv, head_tx, DMA_TX_SIZE, false);
1072 static void stmmac_display_rings(struct stmmac_priv *priv)
1074 /* Display RX ring */
1075 stmmac_display_rx_rings(priv);
1077 /* Display TX ring */
1078 stmmac_display_tx_rings(priv);
1081 static int stmmac_set_bfsize(int mtu, int bufsize)
1083 int ret = bufsize;
1085 if (mtu >= BUF_SIZE_8KiB)
1086 ret = BUF_SIZE_16KiB;
1087 else if (mtu >= BUF_SIZE_4KiB)
1088 ret = BUF_SIZE_8KiB;
1089 else if (mtu >= BUF_SIZE_2KiB)
1090 ret = BUF_SIZE_4KiB;
1091 else if (mtu > DEFAULT_BUFSIZE)
1092 ret = BUF_SIZE_2KiB;
1093 else
1094 ret = DEFAULT_BUFSIZE;
1096 return ret;
1100 * stmmac_clear_rx_descriptors - clear RX descriptors
1101 * @priv: driver private structure
1102 * @queue: RX queue index
1103 * Description: this function is called to clear the RX descriptors
1104 * in case of both basic and extended descriptors are used.
1106 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue)
1108 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1109 int i;
1111 /* Clear the RX descriptors */
1112 for (i = 0; i < DMA_RX_SIZE; i++)
1113 if (priv->extend_desc)
1114 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic,
1115 priv->use_riwt, priv->mode,
1116 (i == DMA_RX_SIZE - 1),
1117 priv->dma_buf_sz);
1118 else
1119 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i],
1120 priv->use_riwt, priv->mode,
1121 (i == DMA_RX_SIZE - 1),
1122 priv->dma_buf_sz);
1126 * stmmac_clear_tx_descriptors - clear tx descriptors
1127 * @priv: driver private structure
1128 * @queue: TX queue index.
1129 * Description: this function is called to clear the TX descriptors
1130 * in case of both basic and extended descriptors are used.
1132 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue)
1134 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1135 int i;
1137 /* Clear the TX descriptors */
1138 for (i = 0; i < DMA_TX_SIZE; i++)
1139 if (priv->extend_desc)
1140 stmmac_init_tx_desc(priv, &tx_q->dma_etx[i].basic,
1141 priv->mode, (i == DMA_TX_SIZE - 1));
1142 else
1143 stmmac_init_tx_desc(priv, &tx_q->dma_tx[i],
1144 priv->mode, (i == DMA_TX_SIZE - 1));
1148 * stmmac_clear_descriptors - clear descriptors
1149 * @priv: driver private structure
1150 * Description: this function is called to clear the TX and RX descriptors
1151 * in case of both basic and extended descriptors are used.
1153 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1155 u32 rx_queue_cnt = priv->plat->rx_queues_to_use;
1156 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1157 u32 queue;
1159 /* Clear the RX descriptors */
1160 for (queue = 0; queue < rx_queue_cnt; queue++)
1161 stmmac_clear_rx_descriptors(priv, queue);
1163 /* Clear the TX descriptors */
1164 for (queue = 0; queue < tx_queue_cnt; queue++)
1165 stmmac_clear_tx_descriptors(priv, queue);
1169 * stmmac_init_rx_buffers - init the RX descriptor buffer.
1170 * @priv: driver private structure
1171 * @p: descriptor pointer
1172 * @i: descriptor index
1173 * @flags: gfp flag
1174 * @queue: RX queue index
1175 * Description: this function is called to allocate a receive buffer, perform
1176 * the DMA mapping and init the descriptor.
1178 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1179 int i, gfp_t flags, u32 queue)
1181 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1182 struct sk_buff *skb;
1184 skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
1185 if (!skb) {
1186 netdev_err(priv->dev,
1187 "%s: Rx init fails; skb is NULL\n", __func__);
1188 return -ENOMEM;
1190 rx_q->rx_skbuff[i] = skb;
1191 rx_q->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
1192 priv->dma_buf_sz,
1193 DMA_FROM_DEVICE);
1194 if (dma_mapping_error(priv->device, rx_q->rx_skbuff_dma[i])) {
1195 netdev_err(priv->dev, "%s: DMA mapping error\n", __func__);
1196 dev_kfree_skb_any(skb);
1197 return -EINVAL;
1200 stmmac_set_desc_addr(priv, p, rx_q->rx_skbuff_dma[i]);
1202 if (priv->dma_buf_sz == BUF_SIZE_16KiB)
1203 stmmac_init_desc3(priv, p);
1205 return 0;
1209 * stmmac_free_rx_buffer - free RX dma buffers
1210 * @priv: private structure
1211 * @queue: RX queue index
1212 * @i: buffer index.
1214 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1216 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1218 if (rx_q->rx_skbuff[i]) {
1219 dma_unmap_single(priv->device, rx_q->rx_skbuff_dma[i],
1220 priv->dma_buf_sz, DMA_FROM_DEVICE);
1221 dev_kfree_skb_any(rx_q->rx_skbuff[i]);
1223 rx_q->rx_skbuff[i] = NULL;
1227 * stmmac_free_tx_buffer - free RX dma buffers
1228 * @priv: private structure
1229 * @queue: RX queue index
1230 * @i: buffer index.
1232 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i)
1234 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1236 if (tx_q->tx_skbuff_dma[i].buf) {
1237 if (tx_q->tx_skbuff_dma[i].map_as_page)
1238 dma_unmap_page(priv->device,
1239 tx_q->tx_skbuff_dma[i].buf,
1240 tx_q->tx_skbuff_dma[i].len,
1241 DMA_TO_DEVICE);
1242 else
1243 dma_unmap_single(priv->device,
1244 tx_q->tx_skbuff_dma[i].buf,
1245 tx_q->tx_skbuff_dma[i].len,
1246 DMA_TO_DEVICE);
1249 if (tx_q->tx_skbuff[i]) {
1250 dev_kfree_skb_any(tx_q->tx_skbuff[i]);
1251 tx_q->tx_skbuff[i] = NULL;
1252 tx_q->tx_skbuff_dma[i].buf = 0;
1253 tx_q->tx_skbuff_dma[i].map_as_page = false;
1258 * init_dma_rx_desc_rings - init the RX descriptor rings
1259 * @dev: net device structure
1260 * @flags: gfp flag.
1261 * Description: this function initializes the DMA RX descriptors
1262 * and allocates the socket buffers. It supports the chained and ring
1263 * modes.
1265 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags)
1267 struct stmmac_priv *priv = netdev_priv(dev);
1268 u32 rx_count = priv->plat->rx_queues_to_use;
1269 int ret = -ENOMEM;
1270 int bfsize = 0;
1271 int queue;
1272 int i;
1274 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu);
1275 if (bfsize < 0)
1276 bfsize = 0;
1278 if (bfsize < BUF_SIZE_16KiB)
1279 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
1281 priv->dma_buf_sz = bfsize;
1283 /* RX INITIALIZATION */
1284 netif_dbg(priv, probe, priv->dev,
1285 "SKB addresses:\nskb\t\tskb data\tdma data\n");
1287 for (queue = 0; queue < rx_count; queue++) {
1288 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1290 netif_dbg(priv, probe, priv->dev,
1291 "(%s) dma_rx_phy=0x%08x\n", __func__,
1292 (u32)rx_q->dma_rx_phy);
1294 for (i = 0; i < DMA_RX_SIZE; i++) {
1295 struct dma_desc *p;
1297 if (priv->extend_desc)
1298 p = &((rx_q->dma_erx + i)->basic);
1299 else
1300 p = rx_q->dma_rx + i;
1302 ret = stmmac_init_rx_buffers(priv, p, i, flags,
1303 queue);
1304 if (ret)
1305 goto err_init_rx_buffers;
1307 netif_dbg(priv, probe, priv->dev, "[%p]\t[%p]\t[%x]\n",
1308 rx_q->rx_skbuff[i], rx_q->rx_skbuff[i]->data,
1309 (unsigned int)rx_q->rx_skbuff_dma[i]);
1312 rx_q->cur_rx = 0;
1313 rx_q->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
1315 stmmac_clear_rx_descriptors(priv, queue);
1317 /* Setup the chained descriptor addresses */
1318 if (priv->mode == STMMAC_CHAIN_MODE) {
1319 if (priv->extend_desc)
1320 stmmac_mode_init(priv, rx_q->dma_erx,
1321 rx_q->dma_rx_phy, DMA_RX_SIZE, 1);
1322 else
1323 stmmac_mode_init(priv, rx_q->dma_rx,
1324 rx_q->dma_rx_phy, DMA_RX_SIZE, 0);
1328 buf_sz = bfsize;
1330 return 0;
1332 err_init_rx_buffers:
1333 while (queue >= 0) {
1334 while (--i >= 0)
1335 stmmac_free_rx_buffer(priv, queue, i);
1337 if (queue == 0)
1338 break;
1340 i = DMA_RX_SIZE;
1341 queue--;
1344 return ret;
1348 * init_dma_tx_desc_rings - init the TX descriptor rings
1349 * @dev: net device structure.
1350 * Description: this function initializes the DMA TX descriptors
1351 * and allocates the socket buffers. It supports the chained and ring
1352 * modes.
1354 static int init_dma_tx_desc_rings(struct net_device *dev)
1356 struct stmmac_priv *priv = netdev_priv(dev);
1357 u32 tx_queue_cnt = priv->plat->tx_queues_to_use;
1358 u32 queue;
1359 int i;
1361 for (queue = 0; queue < tx_queue_cnt; queue++) {
1362 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1364 netif_dbg(priv, probe, priv->dev,
1365 "(%s) dma_tx_phy=0x%08x\n", __func__,
1366 (u32)tx_q->dma_tx_phy);
1368 /* Setup the chained descriptor addresses */
1369 if (priv->mode == STMMAC_CHAIN_MODE) {
1370 if (priv->extend_desc)
1371 stmmac_mode_init(priv, tx_q->dma_etx,
1372 tx_q->dma_tx_phy, DMA_TX_SIZE, 1);
1373 else
1374 stmmac_mode_init(priv, tx_q->dma_tx,
1375 tx_q->dma_tx_phy, DMA_TX_SIZE, 0);
1378 for (i = 0; i < DMA_TX_SIZE; i++) {
1379 struct dma_desc *p;
1380 if (priv->extend_desc)
1381 p = &((tx_q->dma_etx + i)->basic);
1382 else
1383 p = tx_q->dma_tx + i;
1385 stmmac_clear_desc(priv, p);
1387 tx_q->tx_skbuff_dma[i].buf = 0;
1388 tx_q->tx_skbuff_dma[i].map_as_page = false;
1389 tx_q->tx_skbuff_dma[i].len = 0;
1390 tx_q->tx_skbuff_dma[i].last_segment = false;
1391 tx_q->tx_skbuff[i] = NULL;
1394 tx_q->dirty_tx = 0;
1395 tx_q->cur_tx = 0;
1396 tx_q->mss = 0;
1398 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue));
1401 return 0;
1405 * init_dma_desc_rings - init the RX/TX descriptor rings
1406 * @dev: net device structure
1407 * @flags: gfp flag.
1408 * Description: this function initializes the DMA RX/TX descriptors
1409 * and allocates the socket buffers. It supports the chained and ring
1410 * modes.
1412 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1414 struct stmmac_priv *priv = netdev_priv(dev);
1415 int ret;
1417 ret = init_dma_rx_desc_rings(dev, flags);
1418 if (ret)
1419 return ret;
1421 ret = init_dma_tx_desc_rings(dev);
1423 stmmac_clear_descriptors(priv);
1425 if (netif_msg_hw(priv))
1426 stmmac_display_rings(priv);
1428 return ret;
1432 * dma_free_rx_skbufs - free RX dma buffers
1433 * @priv: private structure
1434 * @queue: RX queue index
1436 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue)
1438 int i;
1440 for (i = 0; i < DMA_RX_SIZE; i++)
1441 stmmac_free_rx_buffer(priv, queue, i);
1445 * dma_free_tx_skbufs - free TX dma buffers
1446 * @priv: private structure
1447 * @queue: TX queue index
1449 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue)
1451 int i;
1453 for (i = 0; i < DMA_TX_SIZE; i++)
1454 stmmac_free_tx_buffer(priv, queue, i);
1458 * free_dma_rx_desc_resources - free RX dma desc resources
1459 * @priv: private structure
1461 static void free_dma_rx_desc_resources(struct stmmac_priv *priv)
1463 u32 rx_count = priv->plat->rx_queues_to_use;
1464 u32 queue;
1466 /* Free RX queue resources */
1467 for (queue = 0; queue < rx_count; queue++) {
1468 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1470 /* Release the DMA RX socket buffers */
1471 dma_free_rx_skbufs(priv, queue);
1473 /* Free DMA regions of consistent memory previously allocated */
1474 if (!priv->extend_desc)
1475 dma_free_coherent(priv->device,
1476 DMA_RX_SIZE * sizeof(struct dma_desc),
1477 rx_q->dma_rx, rx_q->dma_rx_phy);
1478 else
1479 dma_free_coherent(priv->device, DMA_RX_SIZE *
1480 sizeof(struct dma_extended_desc),
1481 rx_q->dma_erx, rx_q->dma_rx_phy);
1483 kfree(rx_q->rx_skbuff_dma);
1484 kfree(rx_q->rx_skbuff);
1489 * free_dma_tx_desc_resources - free TX dma desc resources
1490 * @priv: private structure
1492 static void free_dma_tx_desc_resources(struct stmmac_priv *priv)
1494 u32 tx_count = priv->plat->tx_queues_to_use;
1495 u32 queue;
1497 /* Free TX queue resources */
1498 for (queue = 0; queue < tx_count; queue++) {
1499 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1501 /* Release the DMA TX socket buffers */
1502 dma_free_tx_skbufs(priv, queue);
1504 /* Free DMA regions of consistent memory previously allocated */
1505 if (!priv->extend_desc)
1506 dma_free_coherent(priv->device,
1507 DMA_TX_SIZE * sizeof(struct dma_desc),
1508 tx_q->dma_tx, tx_q->dma_tx_phy);
1509 else
1510 dma_free_coherent(priv->device, DMA_TX_SIZE *
1511 sizeof(struct dma_extended_desc),
1512 tx_q->dma_etx, tx_q->dma_tx_phy);
1514 kfree(tx_q->tx_skbuff_dma);
1515 kfree(tx_q->tx_skbuff);
1520 * alloc_dma_rx_desc_resources - alloc RX resources.
1521 * @priv: private structure
1522 * Description: according to which descriptor can be used (extend or basic)
1523 * this function allocates the resources for TX and RX paths. In case of
1524 * reception, for example, it pre-allocated the RX socket buffer in order to
1525 * allow zero-copy mechanism.
1527 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv)
1529 u32 rx_count = priv->plat->rx_queues_to_use;
1530 int ret = -ENOMEM;
1531 u32 queue;
1533 /* RX queues buffers and DMA */
1534 for (queue = 0; queue < rx_count; queue++) {
1535 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
1537 rx_q->queue_index = queue;
1538 rx_q->priv_data = priv;
1540 rx_q->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE,
1541 sizeof(dma_addr_t),
1542 GFP_KERNEL);
1543 if (!rx_q->rx_skbuff_dma)
1544 goto err_dma;
1546 rx_q->rx_skbuff = kmalloc_array(DMA_RX_SIZE,
1547 sizeof(struct sk_buff *),
1548 GFP_KERNEL);
1549 if (!rx_q->rx_skbuff)
1550 goto err_dma;
1552 if (priv->extend_desc) {
1553 rx_q->dma_erx = dma_zalloc_coherent(priv->device,
1554 DMA_RX_SIZE *
1555 sizeof(struct
1556 dma_extended_desc),
1557 &rx_q->dma_rx_phy,
1558 GFP_KERNEL);
1559 if (!rx_q->dma_erx)
1560 goto err_dma;
1562 } else {
1563 rx_q->dma_rx = dma_zalloc_coherent(priv->device,
1564 DMA_RX_SIZE *
1565 sizeof(struct
1566 dma_desc),
1567 &rx_q->dma_rx_phy,
1568 GFP_KERNEL);
1569 if (!rx_q->dma_rx)
1570 goto err_dma;
1574 return 0;
1576 err_dma:
1577 free_dma_rx_desc_resources(priv);
1579 return ret;
1583 * alloc_dma_tx_desc_resources - alloc TX resources.
1584 * @priv: private structure
1585 * Description: according to which descriptor can be used (extend or basic)
1586 * this function allocates the resources for TX and RX paths. In case of
1587 * reception, for example, it pre-allocated the RX socket buffer in order to
1588 * allow zero-copy mechanism.
1590 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv)
1592 u32 tx_count = priv->plat->tx_queues_to_use;
1593 int ret = -ENOMEM;
1594 u32 queue;
1596 /* TX queues buffers and DMA */
1597 for (queue = 0; queue < tx_count; queue++) {
1598 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1600 tx_q->queue_index = queue;
1601 tx_q->priv_data = priv;
1603 tx_q->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
1604 sizeof(*tx_q->tx_skbuff_dma),
1605 GFP_KERNEL);
1606 if (!tx_q->tx_skbuff_dma)
1607 goto err_dma;
1609 tx_q->tx_skbuff = kmalloc_array(DMA_TX_SIZE,
1610 sizeof(struct sk_buff *),
1611 GFP_KERNEL);
1612 if (!tx_q->tx_skbuff)
1613 goto err_dma;
1615 if (priv->extend_desc) {
1616 tx_q->dma_etx = dma_zalloc_coherent(priv->device,
1617 DMA_TX_SIZE *
1618 sizeof(struct
1619 dma_extended_desc),
1620 &tx_q->dma_tx_phy,
1621 GFP_KERNEL);
1622 if (!tx_q->dma_etx)
1623 goto err_dma;
1624 } else {
1625 tx_q->dma_tx = dma_zalloc_coherent(priv->device,
1626 DMA_TX_SIZE *
1627 sizeof(struct
1628 dma_desc),
1629 &tx_q->dma_tx_phy,
1630 GFP_KERNEL);
1631 if (!tx_q->dma_tx)
1632 goto err_dma;
1636 return 0;
1638 err_dma:
1639 free_dma_tx_desc_resources(priv);
1641 return ret;
1645 * alloc_dma_desc_resources - alloc TX/RX resources.
1646 * @priv: private structure
1647 * Description: according to which descriptor can be used (extend or basic)
1648 * this function allocates the resources for TX and RX paths. In case of
1649 * reception, for example, it pre-allocated the RX socket buffer in order to
1650 * allow zero-copy mechanism.
1652 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1654 /* RX Allocation */
1655 int ret = alloc_dma_rx_desc_resources(priv);
1657 if (ret)
1658 return ret;
1660 ret = alloc_dma_tx_desc_resources(priv);
1662 return ret;
1666 * free_dma_desc_resources - free dma desc resources
1667 * @priv: private structure
1669 static void free_dma_desc_resources(struct stmmac_priv *priv)
1671 /* Release the DMA RX socket buffers */
1672 free_dma_rx_desc_resources(priv);
1674 /* Release the DMA TX socket buffers */
1675 free_dma_tx_desc_resources(priv);
1679 * stmmac_mac_enable_rx_queues - Enable MAC rx queues
1680 * @priv: driver private structure
1681 * Description: It is used for enabling the rx queues in the MAC
1683 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv)
1685 u32 rx_queues_count = priv->plat->rx_queues_to_use;
1686 int queue;
1687 u8 mode;
1689 for (queue = 0; queue < rx_queues_count; queue++) {
1690 mode = priv->plat->rx_queues_cfg[queue].mode_to_use;
1691 stmmac_rx_queue_enable(priv, priv->hw, mode, queue);
1696 * stmmac_start_rx_dma - start RX DMA channel
1697 * @priv: driver private structure
1698 * @chan: RX channel index
1699 * Description:
1700 * This starts a RX DMA channel
1702 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan)
1704 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan);
1705 stmmac_start_rx(priv, priv->ioaddr, chan);
1709 * stmmac_start_tx_dma - start TX DMA channel
1710 * @priv: driver private structure
1711 * @chan: TX channel index
1712 * Description:
1713 * This starts a TX DMA channel
1715 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan)
1717 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan);
1718 stmmac_start_tx(priv, priv->ioaddr, chan);
1722 * stmmac_stop_rx_dma - stop RX DMA channel
1723 * @priv: driver private structure
1724 * @chan: RX channel index
1725 * Description:
1726 * This stops a RX DMA channel
1728 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan)
1730 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan);
1731 stmmac_stop_rx(priv, priv->ioaddr, chan);
1735 * stmmac_stop_tx_dma - stop TX DMA channel
1736 * @priv: driver private structure
1737 * @chan: TX channel index
1738 * Description:
1739 * This stops a TX DMA channel
1741 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan)
1743 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan);
1744 stmmac_stop_tx(priv, priv->ioaddr, chan);
1748 * stmmac_start_all_dma - start all RX and TX DMA channels
1749 * @priv: driver private structure
1750 * Description:
1751 * This starts all the RX and TX DMA channels
1753 static void stmmac_start_all_dma(struct stmmac_priv *priv)
1755 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1756 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1757 u32 chan = 0;
1759 for (chan = 0; chan < rx_channels_count; chan++)
1760 stmmac_start_rx_dma(priv, chan);
1762 for (chan = 0; chan < tx_channels_count; chan++)
1763 stmmac_start_tx_dma(priv, chan);
1767 * stmmac_stop_all_dma - stop all RX and TX DMA channels
1768 * @priv: driver private structure
1769 * Description:
1770 * This stops the RX and TX DMA channels
1772 static void stmmac_stop_all_dma(struct stmmac_priv *priv)
1774 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1775 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1776 u32 chan = 0;
1778 for (chan = 0; chan < rx_channels_count; chan++)
1779 stmmac_stop_rx_dma(priv, chan);
1781 for (chan = 0; chan < tx_channels_count; chan++)
1782 stmmac_stop_tx_dma(priv, chan);
1786 * stmmac_dma_operation_mode - HW DMA operation mode
1787 * @priv: driver private structure
1788 * Description: it is used for configuring the DMA operation mode register in
1789 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
1791 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1793 u32 rx_channels_count = priv->plat->rx_queues_to_use;
1794 u32 tx_channels_count = priv->plat->tx_queues_to_use;
1795 int rxfifosz = priv->plat->rx_fifo_size;
1796 int txfifosz = priv->plat->tx_fifo_size;
1797 u32 txmode = 0;
1798 u32 rxmode = 0;
1799 u32 chan = 0;
1800 u8 qmode = 0;
1802 if (rxfifosz == 0)
1803 rxfifosz = priv->dma_cap.rx_fifo_size;
1804 if (txfifosz == 0)
1805 txfifosz = priv->dma_cap.tx_fifo_size;
1807 /* Adjust for real per queue fifo size */
1808 rxfifosz /= rx_channels_count;
1809 txfifosz /= tx_channels_count;
1811 if (priv->plat->force_thresh_dma_mode) {
1812 txmode = tc;
1813 rxmode = tc;
1814 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
1816 * In case of GMAC, SF mode can be enabled
1817 * to perform the TX COE in HW. This depends on:
1818 * 1) TX COE if actually supported
1819 * 2) There is no bugged Jumbo frame support
1820 * that needs to not insert csum in the TDES.
1822 txmode = SF_DMA_MODE;
1823 rxmode = SF_DMA_MODE;
1824 priv->xstats.threshold = SF_DMA_MODE;
1825 } else {
1826 txmode = tc;
1827 rxmode = SF_DMA_MODE;
1830 /* configure all channels */
1831 for (chan = 0; chan < rx_channels_count; chan++) {
1832 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
1834 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan,
1835 rxfifosz, qmode);
1836 stmmac_set_dma_bfsize(priv, priv->ioaddr, priv->dma_buf_sz,
1837 chan);
1840 for (chan = 0; chan < tx_channels_count; chan++) {
1841 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
1843 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan,
1844 txfifosz, qmode);
1849 * stmmac_tx_clean - to manage the transmission completion
1850 * @priv: driver private structure
1851 * @queue: TX queue index
1852 * Description: it reclaims the transmit resources after transmission completes.
1854 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue)
1856 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
1857 unsigned int bytes_compl = 0, pkts_compl = 0;
1858 unsigned int entry, count = 0;
1860 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue));
1862 priv->xstats.tx_clean++;
1864 entry = tx_q->dirty_tx;
1865 while ((entry != tx_q->cur_tx) && (count < budget)) {
1866 struct sk_buff *skb = tx_q->tx_skbuff[entry];
1867 struct dma_desc *p;
1868 int status;
1870 if (priv->extend_desc)
1871 p = (struct dma_desc *)(tx_q->dma_etx + entry);
1872 else
1873 p = tx_q->dma_tx + entry;
1875 status = stmmac_tx_status(priv, &priv->dev->stats,
1876 &priv->xstats, p, priv->ioaddr);
1877 /* Check if the descriptor is owned by the DMA */
1878 if (unlikely(status & tx_dma_own))
1879 break;
1881 count++;
1883 /* Make sure descriptor fields are read after reading
1884 * the own bit.
1886 dma_rmb();
1888 /* Just consider the last segment and ...*/
1889 if (likely(!(status & tx_not_ls))) {
1890 /* ... verify the status error condition */
1891 if (unlikely(status & tx_err)) {
1892 priv->dev->stats.tx_errors++;
1893 } else {
1894 priv->dev->stats.tx_packets++;
1895 priv->xstats.tx_pkt_n++;
1897 stmmac_get_tx_hwtstamp(priv, p, skb);
1900 if (likely(tx_q->tx_skbuff_dma[entry].buf)) {
1901 if (tx_q->tx_skbuff_dma[entry].map_as_page)
1902 dma_unmap_page(priv->device,
1903 tx_q->tx_skbuff_dma[entry].buf,
1904 tx_q->tx_skbuff_dma[entry].len,
1905 DMA_TO_DEVICE);
1906 else
1907 dma_unmap_single(priv->device,
1908 tx_q->tx_skbuff_dma[entry].buf,
1909 tx_q->tx_skbuff_dma[entry].len,
1910 DMA_TO_DEVICE);
1911 tx_q->tx_skbuff_dma[entry].buf = 0;
1912 tx_q->tx_skbuff_dma[entry].len = 0;
1913 tx_q->tx_skbuff_dma[entry].map_as_page = false;
1916 stmmac_clean_desc3(priv, tx_q, p);
1918 tx_q->tx_skbuff_dma[entry].last_segment = false;
1919 tx_q->tx_skbuff_dma[entry].is_jumbo = false;
1921 if (likely(skb != NULL)) {
1922 pkts_compl++;
1923 bytes_compl += skb->len;
1924 dev_consume_skb_any(skb);
1925 tx_q->tx_skbuff[entry] = NULL;
1928 stmmac_release_tx_desc(priv, p, priv->mode);
1930 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
1932 tx_q->dirty_tx = entry;
1934 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue),
1935 pkts_compl, bytes_compl);
1937 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev,
1938 queue))) &&
1939 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH) {
1941 netif_dbg(priv, tx_done, priv->dev,
1942 "%s: restart transmit\n", __func__);
1943 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue));
1946 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1947 stmmac_enable_eee_mode(priv);
1948 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
1951 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue));
1953 return count;
1957 * stmmac_tx_err - to manage the tx error
1958 * @priv: driver private structure
1959 * @chan: channel index
1960 * Description: it cleans the descriptors and restarts the transmission
1961 * in case of transmission errors.
1963 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan)
1965 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
1966 int i;
1968 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan));
1970 stmmac_stop_tx_dma(priv, chan);
1971 dma_free_tx_skbufs(priv, chan);
1972 for (i = 0; i < DMA_TX_SIZE; i++)
1973 if (priv->extend_desc)
1974 stmmac_init_tx_desc(priv, &tx_q->dma_etx[i].basic,
1975 priv->mode, (i == DMA_TX_SIZE - 1));
1976 else
1977 stmmac_init_tx_desc(priv, &tx_q->dma_tx[i],
1978 priv->mode, (i == DMA_TX_SIZE - 1));
1979 tx_q->dirty_tx = 0;
1980 tx_q->cur_tx = 0;
1981 tx_q->mss = 0;
1982 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan));
1983 stmmac_start_tx_dma(priv, chan);
1985 priv->dev->stats.tx_errors++;
1986 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan));
1990 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel
1991 * @priv: driver private structure
1992 * @txmode: TX operating mode
1993 * @rxmode: RX operating mode
1994 * @chan: channel index
1995 * Description: it is used for configuring of the DMA operation mode in
1996 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward
1997 * mode.
1999 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode,
2000 u32 rxmode, u32 chan)
2002 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use;
2003 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use;
2004 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2005 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2006 int rxfifosz = priv->plat->rx_fifo_size;
2007 int txfifosz = priv->plat->tx_fifo_size;
2009 if (rxfifosz == 0)
2010 rxfifosz = priv->dma_cap.rx_fifo_size;
2011 if (txfifosz == 0)
2012 txfifosz = priv->dma_cap.tx_fifo_size;
2014 /* Adjust for real per queue fifo size */
2015 rxfifosz /= rx_channels_count;
2016 txfifosz /= tx_channels_count;
2018 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode);
2019 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode);
2022 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv)
2024 int ret;
2026 ret = stmmac_safety_feat_irq_status(priv, priv->dev,
2027 priv->ioaddr, priv->dma_cap.asp, &priv->sstats);
2028 if (ret && (ret != -EINVAL)) {
2029 stmmac_global_err(priv);
2030 return true;
2033 return false;
2036 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan)
2038 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr,
2039 &priv->xstats, chan);
2040 struct stmmac_channel *ch = &priv->channel[chan];
2041 bool needs_work = false;
2043 if ((status & handle_rx) && ch->has_rx) {
2044 needs_work = true;
2045 } else {
2046 status &= ~handle_rx;
2049 if ((status & handle_tx) && ch->has_tx) {
2050 needs_work = true;
2051 } else {
2052 status &= ~handle_tx;
2055 if (needs_work && napi_schedule_prep(&ch->napi)) {
2056 stmmac_disable_dma_irq(priv, priv->ioaddr, chan);
2057 __napi_schedule(&ch->napi);
2060 return status;
2064 * stmmac_dma_interrupt - DMA ISR
2065 * @priv: driver private structure
2066 * Description: this is the DMA ISR. It is called by the main ISR.
2067 * It calls the dwmac dma routine and schedule poll method in case of some
2068 * work can be done.
2070 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
2072 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2073 u32 rx_channel_count = priv->plat->rx_queues_to_use;
2074 u32 channels_to_check = tx_channel_count > rx_channel_count ?
2075 tx_channel_count : rx_channel_count;
2076 u32 chan;
2077 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)];
2079 /* Make sure we never check beyond our status buffer. */
2080 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status)))
2081 channels_to_check = ARRAY_SIZE(status);
2083 for (chan = 0; chan < channels_to_check; chan++)
2084 status[chan] = stmmac_napi_check(priv, chan);
2086 for (chan = 0; chan < tx_channel_count; chan++) {
2087 if (unlikely(status[chan] & tx_hard_error_bump_tc)) {
2088 /* Try to bump up the dma threshold on this failure */
2089 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
2090 (tc <= 256)) {
2091 tc += 64;
2092 if (priv->plat->force_thresh_dma_mode)
2093 stmmac_set_dma_operation_mode(priv,
2096 chan);
2097 else
2098 stmmac_set_dma_operation_mode(priv,
2100 SF_DMA_MODE,
2101 chan);
2102 priv->xstats.threshold = tc;
2104 } else if (unlikely(status[chan] == tx_hard_error)) {
2105 stmmac_tx_err(priv, chan);
2111 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
2112 * @priv: driver private structure
2113 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
2115 static void stmmac_mmc_setup(struct stmmac_priv *priv)
2117 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
2118 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
2120 dwmac_mmc_intr_all_mask(priv->mmcaddr);
2122 if (priv->dma_cap.rmon) {
2123 dwmac_mmc_ctrl(priv->mmcaddr, mode);
2124 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
2125 } else
2126 netdev_info(priv->dev, "No MAC Management Counters available\n");
2130 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
2131 * @priv: driver private structure
2132 * Description:
2133 * new GMAC chip generations have a new register to indicate the
2134 * presence of the optional feature/functions.
2135 * This can be also used to override the value passed through the
2136 * platform and necessary for old MAC10/100 and GMAC chips.
2138 static int stmmac_get_hw_features(struct stmmac_priv *priv)
2140 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0;
2144 * stmmac_check_ether_addr - check if the MAC addr is valid
2145 * @priv: driver private structure
2146 * Description:
2147 * it is to verify if the MAC address is valid, in case of failures it
2148 * generates a random MAC address
2150 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
2152 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
2153 stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0);
2154 if (!is_valid_ether_addr(priv->dev->dev_addr))
2155 eth_hw_addr_random(priv->dev);
2156 netdev_info(priv->dev, "device MAC address %pM\n",
2157 priv->dev->dev_addr);
2162 * stmmac_init_dma_engine - DMA init.
2163 * @priv: driver private structure
2164 * Description:
2165 * It inits the DMA invoking the specific MAC/GMAC callback.
2166 * Some DMA parameters can be passed from the platform;
2167 * in case of these are not passed a default is kept for the MAC or GMAC.
2169 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
2171 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2172 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2173 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count);
2174 struct stmmac_rx_queue *rx_q;
2175 struct stmmac_tx_queue *tx_q;
2176 u32 chan = 0;
2177 int atds = 0;
2178 int ret = 0;
2180 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) {
2181 dev_err(priv->device, "Invalid DMA configuration\n");
2182 return -EINVAL;
2185 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
2186 atds = 1;
2188 ret = stmmac_reset(priv, priv->ioaddr);
2189 if (ret) {
2190 dev_err(priv->device, "Failed to reset the dma\n");
2191 return ret;
2194 /* DMA Configuration */
2195 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds);
2197 if (priv->plat->axi)
2198 stmmac_axi(priv, priv->ioaddr, priv->plat->axi);
2200 /* DMA CSR Channel configuration */
2201 for (chan = 0; chan < dma_csr_ch; chan++)
2202 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan);
2204 /* DMA RX Channel Configuration */
2205 for (chan = 0; chan < rx_channels_count; chan++) {
2206 rx_q = &priv->rx_queue[chan];
2208 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2209 rx_q->dma_rx_phy, chan);
2211 rx_q->rx_tail_addr = rx_q->dma_rx_phy +
2212 (DMA_RX_SIZE * sizeof(struct dma_desc));
2213 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
2214 rx_q->rx_tail_addr, chan);
2217 /* DMA TX Channel Configuration */
2218 for (chan = 0; chan < tx_channels_count; chan++) {
2219 tx_q = &priv->tx_queue[chan];
2221 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg,
2222 tx_q->dma_tx_phy, chan);
2224 tx_q->tx_tail_addr = tx_q->dma_tx_phy;
2225 stmmac_set_tx_tail_ptr(priv, priv->ioaddr,
2226 tx_q->tx_tail_addr, chan);
2229 return ret;
2232 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue)
2234 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2236 mod_timer(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer));
2240 * stmmac_tx_timer - mitigation sw timer for tx.
2241 * @data: data pointer
2242 * Description:
2243 * This is the timer handler to directly invoke the stmmac_tx_clean.
2245 static void stmmac_tx_timer(struct timer_list *t)
2247 struct stmmac_tx_queue *tx_q = from_timer(tx_q, t, txtimer);
2248 struct stmmac_priv *priv = tx_q->priv_data;
2249 struct stmmac_channel *ch;
2251 ch = &priv->channel[tx_q->queue_index];
2253 if (likely(napi_schedule_prep(&ch->napi)))
2254 __napi_schedule(&ch->napi);
2258 * stmmac_init_tx_coalesce - init tx mitigation options.
2259 * @priv: driver private structure
2260 * Description:
2261 * This inits the transmit coalesce parameters: i.e. timer rate,
2262 * timer handler and default threshold used for enabling the
2263 * interrupt on completion bit.
2265 static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
2267 u32 tx_channel_count = priv->plat->tx_queues_to_use;
2268 u32 chan;
2270 priv->tx_coal_frames = STMMAC_TX_FRAMES;
2271 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
2273 for (chan = 0; chan < tx_channel_count; chan++) {
2274 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan];
2276 timer_setup(&tx_q->txtimer, stmmac_tx_timer, 0);
2280 static void stmmac_set_rings_length(struct stmmac_priv *priv)
2282 u32 rx_channels_count = priv->plat->rx_queues_to_use;
2283 u32 tx_channels_count = priv->plat->tx_queues_to_use;
2284 u32 chan;
2286 /* set TX ring length */
2287 for (chan = 0; chan < tx_channels_count; chan++)
2288 stmmac_set_tx_ring_len(priv, priv->ioaddr,
2289 (DMA_TX_SIZE - 1), chan);
2291 /* set RX ring length */
2292 for (chan = 0; chan < rx_channels_count; chan++)
2293 stmmac_set_rx_ring_len(priv, priv->ioaddr,
2294 (DMA_RX_SIZE - 1), chan);
2298 * stmmac_set_tx_queue_weight - Set TX queue weight
2299 * @priv: driver private structure
2300 * Description: It is used for setting TX queues weight
2302 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv)
2304 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2305 u32 weight;
2306 u32 queue;
2308 for (queue = 0; queue < tx_queues_count; queue++) {
2309 weight = priv->plat->tx_queues_cfg[queue].weight;
2310 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue);
2315 * stmmac_configure_cbs - Configure CBS in TX queue
2316 * @priv: driver private structure
2317 * Description: It is used for configuring CBS in AVB TX queues
2319 static void stmmac_configure_cbs(struct stmmac_priv *priv)
2321 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2322 u32 mode_to_use;
2323 u32 queue;
2325 /* queue 0 is reserved for legacy traffic */
2326 for (queue = 1; queue < tx_queues_count; queue++) {
2327 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use;
2328 if (mode_to_use == MTL_QUEUE_DCB)
2329 continue;
2331 stmmac_config_cbs(priv, priv->hw,
2332 priv->plat->tx_queues_cfg[queue].send_slope,
2333 priv->plat->tx_queues_cfg[queue].idle_slope,
2334 priv->plat->tx_queues_cfg[queue].high_credit,
2335 priv->plat->tx_queues_cfg[queue].low_credit,
2336 queue);
2341 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel
2342 * @priv: driver private structure
2343 * Description: It is used for mapping RX queues to RX dma channels
2345 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv)
2347 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2348 u32 queue;
2349 u32 chan;
2351 for (queue = 0; queue < rx_queues_count; queue++) {
2352 chan = priv->plat->rx_queues_cfg[queue].chan;
2353 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan);
2358 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority
2359 * @priv: driver private structure
2360 * Description: It is used for configuring the RX Queue Priority
2362 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv)
2364 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2365 u32 queue;
2366 u32 prio;
2368 for (queue = 0; queue < rx_queues_count; queue++) {
2369 if (!priv->plat->rx_queues_cfg[queue].use_prio)
2370 continue;
2372 prio = priv->plat->rx_queues_cfg[queue].prio;
2373 stmmac_rx_queue_prio(priv, priv->hw, prio, queue);
2378 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority
2379 * @priv: driver private structure
2380 * Description: It is used for configuring the TX Queue Priority
2382 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv)
2384 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2385 u32 queue;
2386 u32 prio;
2388 for (queue = 0; queue < tx_queues_count; queue++) {
2389 if (!priv->plat->tx_queues_cfg[queue].use_prio)
2390 continue;
2392 prio = priv->plat->tx_queues_cfg[queue].prio;
2393 stmmac_tx_queue_prio(priv, priv->hw, prio, queue);
2398 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing
2399 * @priv: driver private structure
2400 * Description: It is used for configuring the RX queue routing
2402 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv)
2404 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2405 u32 queue;
2406 u8 packet;
2408 for (queue = 0; queue < rx_queues_count; queue++) {
2409 /* no specific packet type routing specified for the queue */
2410 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0)
2411 continue;
2413 packet = priv->plat->rx_queues_cfg[queue].pkt_route;
2414 stmmac_rx_queue_routing(priv, priv->hw, packet, queue);
2419 * stmmac_mtl_configuration - Configure MTL
2420 * @priv: driver private structure
2421 * Description: It is used for configurring MTL
2423 static void stmmac_mtl_configuration(struct stmmac_priv *priv)
2425 u32 rx_queues_count = priv->plat->rx_queues_to_use;
2426 u32 tx_queues_count = priv->plat->tx_queues_to_use;
2428 if (tx_queues_count > 1)
2429 stmmac_set_tx_queue_weight(priv);
2431 /* Configure MTL RX algorithms */
2432 if (rx_queues_count > 1)
2433 stmmac_prog_mtl_rx_algorithms(priv, priv->hw,
2434 priv->plat->rx_sched_algorithm);
2436 /* Configure MTL TX algorithms */
2437 if (tx_queues_count > 1)
2438 stmmac_prog_mtl_tx_algorithms(priv, priv->hw,
2439 priv->plat->tx_sched_algorithm);
2441 /* Configure CBS in AVB TX queues */
2442 if (tx_queues_count > 1)
2443 stmmac_configure_cbs(priv);
2445 /* Map RX MTL to DMA channels */
2446 stmmac_rx_queue_dma_chan_map(priv);
2448 /* Enable MAC RX Queues */
2449 stmmac_mac_enable_rx_queues(priv);
2451 /* Set RX priorities */
2452 if (rx_queues_count > 1)
2453 stmmac_mac_config_rx_queues_prio(priv);
2455 /* Set TX priorities */
2456 if (tx_queues_count > 1)
2457 stmmac_mac_config_tx_queues_prio(priv);
2459 /* Set RX routing */
2460 if (rx_queues_count > 1)
2461 stmmac_mac_config_rx_queues_routing(priv);
2464 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv)
2466 if (priv->dma_cap.asp) {
2467 netdev_info(priv->dev, "Enabling Safety Features\n");
2468 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp);
2469 } else {
2470 netdev_info(priv->dev, "No Safety Features support found\n");
2475 * stmmac_hw_setup - setup mac in a usable state.
2476 * @dev : pointer to the device structure.
2477 * Description:
2478 * this is the main function to setup the HW in a usable state because the
2479 * dma engine is reset, the core registers are configured (e.g. AXI,
2480 * Checksum features, timers). The DMA is ready to start receiving and
2481 * transmitting.
2482 * Return value:
2483 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2484 * file on failure.
2486 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
2488 struct stmmac_priv *priv = netdev_priv(dev);
2489 u32 rx_cnt = priv->plat->rx_queues_to_use;
2490 u32 tx_cnt = priv->plat->tx_queues_to_use;
2491 u32 chan;
2492 int ret;
2494 /* DMA initialization and SW reset */
2495 ret = stmmac_init_dma_engine(priv);
2496 if (ret < 0) {
2497 netdev_err(priv->dev, "%s: DMA engine initialization failed\n",
2498 __func__);
2499 return ret;
2502 /* Copy the MAC addr into the HW */
2503 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0);
2505 /* PS and related bits will be programmed according to the speed */
2506 if (priv->hw->pcs) {
2507 int speed = priv->plat->mac_port_sel_speed;
2509 if ((speed == SPEED_10) || (speed == SPEED_100) ||
2510 (speed == SPEED_1000)) {
2511 priv->hw->ps = speed;
2512 } else {
2513 dev_warn(priv->device, "invalid port speed\n");
2514 priv->hw->ps = 0;
2518 /* Initialize the MAC Core */
2519 stmmac_core_init(priv, priv->hw, dev);
2521 /* Initialize MTL*/
2522 stmmac_mtl_configuration(priv);
2524 /* Initialize Safety Features */
2525 stmmac_safety_feat_configuration(priv);
2527 ret = stmmac_rx_ipc(priv, priv->hw);
2528 if (!ret) {
2529 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n");
2530 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
2531 priv->hw->rx_csum = 0;
2534 /* Enable the MAC Rx/Tx */
2535 stmmac_mac_set(priv, priv->ioaddr, true);
2537 /* Set the HW DMA mode and the COE */
2538 stmmac_dma_operation_mode(priv);
2540 stmmac_mmc_setup(priv);
2542 if (init_ptp) {
2543 ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
2544 if (ret < 0)
2545 netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret);
2547 ret = stmmac_init_ptp(priv);
2548 if (ret == -EOPNOTSUPP)
2549 netdev_warn(priv->dev, "PTP not supported by HW\n");
2550 else if (ret)
2551 netdev_warn(priv->dev, "PTP init failed\n");
2554 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
2556 if (priv->use_riwt) {
2557 ret = stmmac_rx_watchdog(priv, priv->ioaddr, MAX_DMA_RIWT, rx_cnt);
2558 if (!ret)
2559 priv->rx_riwt = MAX_DMA_RIWT;
2562 if (priv->hw->pcs)
2563 stmmac_pcs_ctrl_ane(priv, priv->hw, 1, priv->hw->ps, 0);
2565 /* set TX and RX rings length */
2566 stmmac_set_rings_length(priv);
2568 /* Enable TSO */
2569 if (priv->tso) {
2570 for (chan = 0; chan < tx_cnt; chan++)
2571 stmmac_enable_tso(priv, priv->ioaddr, 1, chan);
2574 /* Start the ball rolling... */
2575 stmmac_start_all_dma(priv);
2577 return 0;
2580 static void stmmac_hw_teardown(struct net_device *dev)
2582 struct stmmac_priv *priv = netdev_priv(dev);
2584 clk_disable_unprepare(priv->plat->clk_ptp_ref);
2588 * stmmac_open - open entry point of the driver
2589 * @dev : pointer to the device structure.
2590 * Description:
2591 * This function is the open entry point of the driver.
2592 * Return value:
2593 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2594 * file on failure.
2596 static int stmmac_open(struct net_device *dev)
2598 struct stmmac_priv *priv = netdev_priv(dev);
2599 u32 chan;
2600 int ret;
2602 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
2603 priv->hw->pcs != STMMAC_PCS_TBI &&
2604 priv->hw->pcs != STMMAC_PCS_RTBI) {
2605 ret = stmmac_init_phy(dev);
2606 if (ret) {
2607 netdev_err(priv->dev,
2608 "%s: Cannot attach to PHY (error: %d)\n",
2609 __func__, ret);
2610 return ret;
2614 /* Extra statistics */
2615 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
2616 priv->xstats.threshold = tc;
2618 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
2619 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
2621 ret = alloc_dma_desc_resources(priv);
2622 if (ret < 0) {
2623 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n",
2624 __func__);
2625 goto dma_desc_error;
2628 ret = init_dma_desc_rings(dev, GFP_KERNEL);
2629 if (ret < 0) {
2630 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n",
2631 __func__);
2632 goto init_error;
2635 ret = stmmac_hw_setup(dev, true);
2636 if (ret < 0) {
2637 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__);
2638 goto init_error;
2641 stmmac_init_tx_coalesce(priv);
2643 if (dev->phydev)
2644 phy_start(dev->phydev);
2646 /* Request the IRQ lines */
2647 ret = request_irq(dev->irq, stmmac_interrupt,
2648 IRQF_SHARED, dev->name, dev);
2649 if (unlikely(ret < 0)) {
2650 netdev_err(priv->dev,
2651 "%s: ERROR: allocating the IRQ %d (error: %d)\n",
2652 __func__, dev->irq, ret);
2653 goto irq_error;
2656 /* Request the Wake IRQ in case of another line is used for WoL */
2657 if (priv->wol_irq != dev->irq) {
2658 ret = request_irq(priv->wol_irq, stmmac_interrupt,
2659 IRQF_SHARED, dev->name, dev);
2660 if (unlikely(ret < 0)) {
2661 netdev_err(priv->dev,
2662 "%s: ERROR: allocating the WoL IRQ %d (%d)\n",
2663 __func__, priv->wol_irq, ret);
2664 goto wolirq_error;
2668 /* Request the IRQ lines */
2669 if (priv->lpi_irq > 0) {
2670 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
2671 dev->name, dev);
2672 if (unlikely(ret < 0)) {
2673 netdev_err(priv->dev,
2674 "%s: ERROR: allocating the LPI IRQ %d (%d)\n",
2675 __func__, priv->lpi_irq, ret);
2676 goto lpiirq_error;
2680 stmmac_enable_all_queues(priv);
2681 stmmac_start_all_queues(priv);
2683 return 0;
2685 lpiirq_error:
2686 if (priv->wol_irq != dev->irq)
2687 free_irq(priv->wol_irq, dev);
2688 wolirq_error:
2689 free_irq(dev->irq, dev);
2690 irq_error:
2691 if (dev->phydev)
2692 phy_stop(dev->phydev);
2694 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
2695 del_timer_sync(&priv->tx_queue[chan].txtimer);
2697 stmmac_hw_teardown(dev);
2698 init_error:
2699 free_dma_desc_resources(priv);
2700 dma_desc_error:
2701 if (dev->phydev)
2702 phy_disconnect(dev->phydev);
2704 return ret;
2708 * stmmac_release - close entry point of the driver
2709 * @dev : device pointer.
2710 * Description:
2711 * This is the stop entry point of the driver.
2713 static int stmmac_release(struct net_device *dev)
2715 struct stmmac_priv *priv = netdev_priv(dev);
2716 u32 chan;
2718 if (priv->eee_enabled)
2719 del_timer_sync(&priv->eee_ctrl_timer);
2721 /* Stop and disconnect the PHY */
2722 if (dev->phydev) {
2723 phy_stop(dev->phydev);
2724 phy_disconnect(dev->phydev);
2727 stmmac_stop_all_queues(priv);
2729 stmmac_disable_all_queues(priv);
2731 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
2732 del_timer_sync(&priv->tx_queue[chan].txtimer);
2734 /* Free the IRQ lines */
2735 free_irq(dev->irq, dev);
2736 if (priv->wol_irq != dev->irq)
2737 free_irq(priv->wol_irq, dev);
2738 if (priv->lpi_irq > 0)
2739 free_irq(priv->lpi_irq, dev);
2741 /* Stop TX/RX DMA and clear the descriptors */
2742 stmmac_stop_all_dma(priv);
2744 /* Release and free the Rx/Tx resources */
2745 free_dma_desc_resources(priv);
2747 /* Disable the MAC Rx/Tx */
2748 stmmac_mac_set(priv, priv->ioaddr, false);
2750 netif_carrier_off(dev);
2752 stmmac_release_ptp(priv);
2754 return 0;
2758 * stmmac_tso_allocator - close entry point of the driver
2759 * @priv: driver private structure
2760 * @des: buffer start address
2761 * @total_len: total length to fill in descriptors
2762 * @last_segmant: condition for the last descriptor
2763 * @queue: TX queue index
2764 * Description:
2765 * This function fills descriptor and request new descriptors according to
2766 * buffer length to fill
2768 static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
2769 int total_len, bool last_segment, u32 queue)
2771 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
2772 struct dma_desc *desc;
2773 u32 buff_size;
2774 int tmp_len;
2776 tmp_len = total_len;
2778 while (tmp_len > 0) {
2779 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
2780 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
2781 desc = tx_q->dma_tx + tx_q->cur_tx;
2783 desc->des0 = cpu_to_le32(des + (total_len - tmp_len));
2784 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
2785 TSO_MAX_BUFF_SIZE : tmp_len;
2787 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size,
2788 0, 1,
2789 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
2790 0, 0);
2792 tmp_len -= TSO_MAX_BUFF_SIZE;
2797 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
2798 * @skb : the socket buffer
2799 * @dev : device pointer
2800 * Description: this is the transmit function that is called on TSO frames
2801 * (support available on GMAC4 and newer chips).
2802 * Diagram below show the ring programming in case of TSO frames:
2804 * First Descriptor
2805 * --------
2806 * | DES0 |---> buffer1 = L2/L3/L4 header
2807 * | DES1 |---> TCP Payload (can continue on next descr...)
2808 * | DES2 |---> buffer 1 and 2 len
2809 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
2810 * --------
2812 * ...
2814 * --------
2815 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
2816 * | DES1 | --|
2817 * | DES2 | --> buffer 1 and 2 len
2818 * | DES3 |
2819 * --------
2821 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
2823 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
2825 struct dma_desc *desc, *first, *mss_desc = NULL;
2826 struct stmmac_priv *priv = netdev_priv(dev);
2827 int nfrags = skb_shinfo(skb)->nr_frags;
2828 u32 queue = skb_get_queue_mapping(skb);
2829 unsigned int first_entry, des;
2830 struct stmmac_tx_queue *tx_q;
2831 int tmp_pay_len = 0;
2832 u32 pay_len, mss;
2833 u8 proto_hdr_len;
2834 int i;
2836 tx_q = &priv->tx_queue[queue];
2838 /* Compute header lengths */
2839 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2841 /* Desc availability based on threshold should be enough safe */
2842 if (unlikely(stmmac_tx_avail(priv, queue) <
2843 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
2844 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
2845 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
2846 queue));
2847 /* This is a hard error, log it. */
2848 netdev_err(priv->dev,
2849 "%s: Tx Ring full when queue awake\n",
2850 __func__);
2852 return NETDEV_TX_BUSY;
2855 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
2857 mss = skb_shinfo(skb)->gso_size;
2859 /* set new MSS value if needed */
2860 if (mss != tx_q->mss) {
2861 mss_desc = tx_q->dma_tx + tx_q->cur_tx;
2862 stmmac_set_mss(priv, mss_desc, mss);
2863 tx_q->mss = mss;
2864 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
2865 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]);
2868 if (netif_msg_tx_queued(priv)) {
2869 pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
2870 __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
2871 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
2872 skb->data_len);
2875 first_entry = tx_q->cur_tx;
2876 WARN_ON(tx_q->tx_skbuff[first_entry]);
2878 desc = tx_q->dma_tx + first_entry;
2879 first = desc;
2881 /* first descriptor: fill Headers on Buf1 */
2882 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
2883 DMA_TO_DEVICE);
2884 if (dma_mapping_error(priv->device, des))
2885 goto dma_map_err;
2887 tx_q->tx_skbuff_dma[first_entry].buf = des;
2888 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
2890 first->des0 = cpu_to_le32(des);
2892 /* Fill start of payload in buff2 of first descriptor */
2893 if (pay_len)
2894 first->des1 = cpu_to_le32(des + proto_hdr_len);
2896 /* If needed take extra descriptors to fill the remaining payload */
2897 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
2899 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue);
2901 /* Prepare fragments */
2902 for (i = 0; i < nfrags; i++) {
2903 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2905 des = skb_frag_dma_map(priv->device, frag, 0,
2906 skb_frag_size(frag),
2907 DMA_TO_DEVICE);
2908 if (dma_mapping_error(priv->device, des))
2909 goto dma_map_err;
2911 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
2912 (i == nfrags - 1), queue);
2914 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des;
2915 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag);
2916 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true;
2919 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true;
2921 /* Only the last descriptor gets to point to the skb. */
2922 tx_q->tx_skbuff[tx_q->cur_tx] = skb;
2924 /* We've used all descriptors we need for this skb, however,
2925 * advance cur_tx so that it references a fresh descriptor.
2926 * ndo_start_xmit will fill this descriptor the next time it's
2927 * called and stmmac_tx_clean may clean up to this descriptor.
2929 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, DMA_TX_SIZE);
2931 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
2932 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
2933 __func__);
2934 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
2937 dev->stats.tx_bytes += skb->len;
2938 priv->xstats.tx_tso_frames++;
2939 priv->xstats.tx_tso_nfrags += nfrags;
2941 /* Manage tx mitigation */
2942 tx_q->tx_count_frames += nfrags + 1;
2943 if (likely(priv->tx_coal_frames > tx_q->tx_count_frames) &&
2944 !(priv->synopsys_id >= DWMAC_CORE_4_00 &&
2945 (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2946 priv->hwts_tx_en)) {
2947 stmmac_tx_timer_arm(priv, queue);
2948 } else {
2949 tx_q->tx_count_frames = 0;
2950 stmmac_set_tx_ic(priv, desc);
2951 priv->xstats.tx_set_ic_bit++;
2954 skb_tx_timestamp(skb);
2956 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2957 priv->hwts_tx_en)) {
2958 /* declare that device is doing timestamping */
2959 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2960 stmmac_enable_tx_timestamp(priv, first);
2963 /* Complete the first descriptor before granting the DMA */
2964 stmmac_prepare_tso_tx_desc(priv, first, 1,
2965 proto_hdr_len,
2966 pay_len,
2967 1, tx_q->tx_skbuff_dma[first_entry].last_segment,
2968 tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
2970 /* If context desc is used to change MSS */
2971 if (mss_desc) {
2972 /* Make sure that first descriptor has been completely
2973 * written, including its own bit. This is because MSS is
2974 * actually before first descriptor, so we need to make
2975 * sure that MSS's own bit is the last thing written.
2977 dma_wmb();
2978 stmmac_set_tx_owner(priv, mss_desc);
2981 /* The own bit must be the latest setting done when prepare the
2982 * descriptor and then barrier is needed to make sure that
2983 * all is coherent before granting the DMA engine.
2985 wmb();
2987 if (netif_msg_pktdata(priv)) {
2988 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
2989 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
2990 tx_q->cur_tx, first, nfrags);
2992 stmmac_display_ring(priv, (void *)tx_q->dma_tx, DMA_TX_SIZE, 0);
2994 pr_info(">>> frame to be transmitted: ");
2995 print_pkt(skb->data, skb_headlen(skb));
2998 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
3000 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * sizeof(*desc));
3001 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3002 stmmac_tx_timer_arm(priv, queue);
3004 return NETDEV_TX_OK;
3006 dma_map_err:
3007 dev_err(priv->device, "Tx dma map failed\n");
3008 dev_kfree_skb(skb);
3009 priv->dev->stats.tx_dropped++;
3010 return NETDEV_TX_OK;
3014 * stmmac_xmit - Tx entry point of the driver
3015 * @skb : the socket buffer
3016 * @dev : device pointer
3017 * Description : this is the tx entry point of the driver.
3018 * It programs the chain or the ring and supports oversized frames
3019 * and SG feature.
3021 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
3023 struct stmmac_priv *priv = netdev_priv(dev);
3024 unsigned int nopaged_len = skb_headlen(skb);
3025 int i, csum_insertion = 0, is_jumbo = 0;
3026 u32 queue = skb_get_queue_mapping(skb);
3027 int nfrags = skb_shinfo(skb)->nr_frags;
3028 int entry;
3029 unsigned int first_entry;
3030 struct dma_desc *desc, *first;
3031 struct stmmac_tx_queue *tx_q;
3032 unsigned int enh_desc;
3033 unsigned int des;
3035 tx_q = &priv->tx_queue[queue];
3037 if (priv->tx_path_in_lpi_mode)
3038 stmmac_disable_eee_mode(priv);
3040 /* Manage oversized TCP frames for GMAC4 device */
3041 if (skb_is_gso(skb) && priv->tso) {
3042 if (skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))
3043 return stmmac_tso_xmit(skb, dev);
3046 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) {
3047 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) {
3048 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev,
3049 queue));
3050 /* This is a hard error, log it. */
3051 netdev_err(priv->dev,
3052 "%s: Tx Ring full when queue awake\n",
3053 __func__);
3055 return NETDEV_TX_BUSY;
3058 entry = tx_q->cur_tx;
3059 first_entry = entry;
3060 WARN_ON(tx_q->tx_skbuff[first_entry]);
3062 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
3064 if (likely(priv->extend_desc))
3065 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
3066 else
3067 desc = tx_q->dma_tx + entry;
3069 first = desc;
3071 enh_desc = priv->plat->enh_desc;
3072 /* To program the descriptors according to the size of the frame */
3073 if (enh_desc)
3074 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc);
3076 if (unlikely(is_jumbo)) {
3077 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion);
3078 if (unlikely(entry < 0) && (entry != -EINVAL))
3079 goto dma_map_err;
3082 for (i = 0; i < nfrags; i++) {
3083 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
3084 int len = skb_frag_size(frag);
3085 bool last_segment = (i == (nfrags - 1));
3087 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
3088 WARN_ON(tx_q->tx_skbuff[entry]);
3090 if (likely(priv->extend_desc))
3091 desc = (struct dma_desc *)(tx_q->dma_etx + entry);
3092 else
3093 desc = tx_q->dma_tx + entry;
3095 des = skb_frag_dma_map(priv->device, frag, 0, len,
3096 DMA_TO_DEVICE);
3097 if (dma_mapping_error(priv->device, des))
3098 goto dma_map_err; /* should reuse desc w/o issues */
3100 tx_q->tx_skbuff_dma[entry].buf = des;
3102 stmmac_set_desc_addr(priv, desc, des);
3104 tx_q->tx_skbuff_dma[entry].map_as_page = true;
3105 tx_q->tx_skbuff_dma[entry].len = len;
3106 tx_q->tx_skbuff_dma[entry].last_segment = last_segment;
3108 /* Prepare the descriptor and set the own bit too */
3109 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion,
3110 priv->mode, 1, last_segment, skb->len);
3113 /* Only the last descriptor gets to point to the skb. */
3114 tx_q->tx_skbuff[entry] = skb;
3116 /* We've used all descriptors we need for this skb, however,
3117 * advance cur_tx so that it references a fresh descriptor.
3118 * ndo_start_xmit will fill this descriptor the next time it's
3119 * called and stmmac_tx_clean may clean up to this descriptor.
3121 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
3122 tx_q->cur_tx = entry;
3124 if (netif_msg_pktdata(priv)) {
3125 void *tx_head;
3127 netdev_dbg(priv->dev,
3128 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
3129 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry,
3130 entry, first, nfrags);
3132 if (priv->extend_desc)
3133 tx_head = (void *)tx_q->dma_etx;
3134 else
3135 tx_head = (void *)tx_q->dma_tx;
3137 stmmac_display_ring(priv, tx_head, DMA_TX_SIZE, false);
3139 netdev_dbg(priv->dev, ">>> frame to be transmitted: ");
3140 print_pkt(skb->data, skb->len);
3143 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) {
3144 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n",
3145 __func__);
3146 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue));
3149 dev->stats.tx_bytes += skb->len;
3151 /* According to the coalesce parameter the IC bit for the latest
3152 * segment is reset and the timer re-started to clean the tx status.
3153 * This approach takes care about the fragments: desc is the first
3154 * element in case of no SG.
3156 tx_q->tx_count_frames += nfrags + 1;
3157 if (likely(priv->tx_coal_frames > tx_q->tx_count_frames) &&
3158 !(priv->synopsys_id >= DWMAC_CORE_4_00 &&
3159 (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3160 priv->hwts_tx_en)) {
3161 stmmac_tx_timer_arm(priv, queue);
3162 } else {
3163 tx_q->tx_count_frames = 0;
3164 stmmac_set_tx_ic(priv, desc);
3165 priv->xstats.tx_set_ic_bit++;
3168 skb_tx_timestamp(skb);
3170 /* Ready to fill the first descriptor and set the OWN bit w/o any
3171 * problems because all the descriptors are actually ready to be
3172 * passed to the DMA engine.
3174 if (likely(!is_jumbo)) {
3175 bool last_segment = (nfrags == 0);
3177 des = dma_map_single(priv->device, skb->data,
3178 nopaged_len, DMA_TO_DEVICE);
3179 if (dma_mapping_error(priv->device, des))
3180 goto dma_map_err;
3182 tx_q->tx_skbuff_dma[first_entry].buf = des;
3184 stmmac_set_desc_addr(priv, first, des);
3186 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len;
3187 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment;
3189 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
3190 priv->hwts_tx_en)) {
3191 /* declare that device is doing timestamping */
3192 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
3193 stmmac_enable_tx_timestamp(priv, first);
3196 /* Prepare the first descriptor setting the OWN bit too */
3197 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len,
3198 csum_insertion, priv->mode, 1, last_segment,
3199 skb->len);
3200 } else {
3201 stmmac_set_tx_owner(priv, first);
3204 /* The own bit must be the latest setting done when prepare the
3205 * descriptor and then barrier is needed to make sure that
3206 * all is coherent before granting the DMA engine.
3208 wmb();
3210 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len);
3212 stmmac_enable_dma_transmission(priv, priv->ioaddr);
3214 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * sizeof(*desc));
3215 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue);
3216 stmmac_tx_timer_arm(priv, queue);
3218 return NETDEV_TX_OK;
3220 dma_map_err:
3221 netdev_err(priv->dev, "Tx DMA map failed\n");
3222 dev_kfree_skb(skb);
3223 priv->dev->stats.tx_dropped++;
3224 return NETDEV_TX_OK;
3227 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
3229 struct vlan_ethhdr *veth;
3230 __be16 vlan_proto;
3231 u16 vlanid;
3233 veth = (struct vlan_ethhdr *)skb->data;
3234 vlan_proto = veth->h_vlan_proto;
3236 if ((vlan_proto == htons(ETH_P_8021Q) &&
3237 dev->features & NETIF_F_HW_VLAN_CTAG_RX) ||
3238 (vlan_proto == htons(ETH_P_8021AD) &&
3239 dev->features & NETIF_F_HW_VLAN_STAG_RX)) {
3240 /* pop the vlan tag */
3241 vlanid = ntohs(veth->h_vlan_TCI);
3242 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2);
3243 skb_pull(skb, VLAN_HLEN);
3244 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid);
3249 static inline int stmmac_rx_threshold_count(struct stmmac_rx_queue *rx_q)
3251 if (rx_q->rx_zeroc_thresh < STMMAC_RX_THRESH)
3252 return 0;
3254 return 1;
3258 * stmmac_rx_refill - refill used skb preallocated buffers
3259 * @priv: driver private structure
3260 * @queue: RX queue index
3261 * Description : this is to reallocate the skb for the reception process
3262 * that is based on zero-copy.
3264 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue)
3266 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3267 int dirty = stmmac_rx_dirty(priv, queue);
3268 unsigned int entry = rx_q->dirty_rx;
3270 int bfsize = priv->dma_buf_sz;
3272 while (dirty-- > 0) {
3273 struct dma_desc *p;
3275 if (priv->extend_desc)
3276 p = (struct dma_desc *)(rx_q->dma_erx + entry);
3277 else
3278 p = rx_q->dma_rx + entry;
3280 if (likely(!rx_q->rx_skbuff[entry])) {
3281 struct sk_buff *skb;
3283 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
3284 if (unlikely(!skb)) {
3285 /* so for a while no zero-copy! */
3286 rx_q->rx_zeroc_thresh = STMMAC_RX_THRESH;
3287 if (unlikely(net_ratelimit()))
3288 dev_err(priv->device,
3289 "fail to alloc skb entry %d\n",
3290 entry);
3291 break;
3294 rx_q->rx_skbuff[entry] = skb;
3295 rx_q->rx_skbuff_dma[entry] =
3296 dma_map_single(priv->device, skb->data, bfsize,
3297 DMA_FROM_DEVICE);
3298 if (dma_mapping_error(priv->device,
3299 rx_q->rx_skbuff_dma[entry])) {
3300 netdev_err(priv->dev, "Rx DMA map failed\n");
3301 dev_kfree_skb(skb);
3302 break;
3305 stmmac_set_desc_addr(priv, p, rx_q->rx_skbuff_dma[entry]);
3306 stmmac_refill_desc3(priv, rx_q, p);
3308 if (rx_q->rx_zeroc_thresh > 0)
3309 rx_q->rx_zeroc_thresh--;
3311 netif_dbg(priv, rx_status, priv->dev,
3312 "refill entry #%d\n", entry);
3314 dma_wmb();
3316 stmmac_set_rx_owner(priv, p, priv->use_riwt);
3318 dma_wmb();
3320 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
3322 rx_q->dirty_rx = entry;
3323 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue);
3327 * stmmac_rx - manage the receive process
3328 * @priv: driver private structure
3329 * @limit: napi bugget
3330 * @queue: RX queue index.
3331 * Description : this the function called by the napi poll method.
3332 * It gets all the frames inside the ring.
3334 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue)
3336 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3337 struct stmmac_channel *ch = &priv->channel[queue];
3338 unsigned int next_entry = rx_q->cur_rx;
3339 int coe = priv->hw->rx_csum;
3340 unsigned int count = 0;
3341 bool xmac;
3343 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
3345 if (netif_msg_rx_status(priv)) {
3346 void *rx_head;
3348 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__);
3349 if (priv->extend_desc)
3350 rx_head = (void *)rx_q->dma_erx;
3351 else
3352 rx_head = (void *)rx_q->dma_rx;
3354 stmmac_display_ring(priv, rx_head, DMA_RX_SIZE, true);
3356 while (count < limit) {
3357 int entry, status;
3358 struct dma_desc *p;
3359 struct dma_desc *np;
3361 entry = next_entry;
3363 if (priv->extend_desc)
3364 p = (struct dma_desc *)(rx_q->dma_erx + entry);
3365 else
3366 p = rx_q->dma_rx + entry;
3368 /* read the status of the incoming frame */
3369 status = stmmac_rx_status(priv, &priv->dev->stats,
3370 &priv->xstats, p);
3371 /* check if managed by the DMA otherwise go ahead */
3372 if (unlikely(status & dma_own))
3373 break;
3375 count++;
3377 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, DMA_RX_SIZE);
3378 next_entry = rx_q->cur_rx;
3380 if (priv->extend_desc)
3381 np = (struct dma_desc *)(rx_q->dma_erx + next_entry);
3382 else
3383 np = rx_q->dma_rx + next_entry;
3385 prefetch(np);
3387 if (priv->extend_desc)
3388 stmmac_rx_extended_status(priv, &priv->dev->stats,
3389 &priv->xstats, rx_q->dma_erx + entry);
3390 if (unlikely(status == discard_frame)) {
3391 priv->dev->stats.rx_errors++;
3392 if (priv->hwts_rx_en && !priv->extend_desc) {
3393 /* DESC2 & DESC3 will be overwritten by device
3394 * with timestamp value, hence reinitialize
3395 * them in stmmac_rx_refill() function so that
3396 * device can reuse it.
3398 dev_kfree_skb_any(rx_q->rx_skbuff[entry]);
3399 rx_q->rx_skbuff[entry] = NULL;
3400 dma_unmap_single(priv->device,
3401 rx_q->rx_skbuff_dma[entry],
3402 priv->dma_buf_sz,
3403 DMA_FROM_DEVICE);
3405 } else {
3406 struct sk_buff *skb;
3407 int frame_len;
3408 unsigned int des;
3410 stmmac_get_desc_addr(priv, p, &des);
3411 frame_len = stmmac_get_rx_frame_len(priv, p, coe);
3413 /* If frame length is greater than skb buffer size
3414 * (preallocated during init) then the packet is
3415 * ignored
3417 if (frame_len > priv->dma_buf_sz) {
3418 if (net_ratelimit())
3419 netdev_err(priv->dev,
3420 "len %d larger than size (%d)\n",
3421 frame_len, priv->dma_buf_sz);
3422 priv->dev->stats.rx_length_errors++;
3423 continue;
3426 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
3427 * Type frames (LLC/LLC-SNAP)
3429 * llc_snap is never checked in GMAC >= 4, so this ACS
3430 * feature is always disabled and packets need to be
3431 * stripped manually.
3433 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00) ||
3434 unlikely(status != llc_snap))
3435 frame_len -= ETH_FCS_LEN;
3437 if (netif_msg_rx_status(priv)) {
3438 netdev_dbg(priv->dev, "\tdesc: %p [entry %d] buff=0x%x\n",
3439 p, entry, des);
3440 netdev_dbg(priv->dev, "frame size %d, COE: %d\n",
3441 frame_len, status);
3444 /* The zero-copy is always used for all the sizes
3445 * in case of GMAC4 because it needs
3446 * to refill the used descriptors, always.
3448 if (unlikely(!xmac &&
3449 ((frame_len < priv->rx_copybreak) ||
3450 stmmac_rx_threshold_count(rx_q)))) {
3451 skb = netdev_alloc_skb_ip_align(priv->dev,
3452 frame_len);
3453 if (unlikely(!skb)) {
3454 if (net_ratelimit())
3455 dev_warn(priv->device,
3456 "packet dropped\n");
3457 priv->dev->stats.rx_dropped++;
3458 continue;
3461 dma_sync_single_for_cpu(priv->device,
3462 rx_q->rx_skbuff_dma
3463 [entry], frame_len,
3464 DMA_FROM_DEVICE);
3465 skb_copy_to_linear_data(skb,
3466 rx_q->
3467 rx_skbuff[entry]->data,
3468 frame_len);
3470 skb_put(skb, frame_len);
3471 dma_sync_single_for_device(priv->device,
3472 rx_q->rx_skbuff_dma
3473 [entry], frame_len,
3474 DMA_FROM_DEVICE);
3475 } else {
3476 skb = rx_q->rx_skbuff[entry];
3477 if (unlikely(!skb)) {
3478 if (net_ratelimit())
3479 netdev_err(priv->dev,
3480 "%s: Inconsistent Rx chain\n",
3481 priv->dev->name);
3482 priv->dev->stats.rx_dropped++;
3483 continue;
3485 prefetch(skb->data - NET_IP_ALIGN);
3486 rx_q->rx_skbuff[entry] = NULL;
3487 rx_q->rx_zeroc_thresh++;
3489 skb_put(skb, frame_len);
3490 dma_unmap_single(priv->device,
3491 rx_q->rx_skbuff_dma[entry],
3492 priv->dma_buf_sz,
3493 DMA_FROM_DEVICE);
3496 if (netif_msg_pktdata(priv)) {
3497 netdev_dbg(priv->dev, "frame received (%dbytes)",
3498 frame_len);
3499 print_pkt(skb->data, frame_len);
3502 stmmac_get_rx_hwtstamp(priv, p, np, skb);
3504 stmmac_rx_vlan(priv->dev, skb);
3506 skb->protocol = eth_type_trans(skb, priv->dev);
3508 if (unlikely(!coe))
3509 skb_checksum_none_assert(skb);
3510 else
3511 skb->ip_summed = CHECKSUM_UNNECESSARY;
3513 napi_gro_receive(&ch->napi, skb);
3515 priv->dev->stats.rx_packets++;
3516 priv->dev->stats.rx_bytes += frame_len;
3520 stmmac_rx_refill(priv, queue);
3522 priv->xstats.rx_pkt_n += count;
3524 return count;
3528 * stmmac_poll - stmmac poll method (NAPI)
3529 * @napi : pointer to the napi structure.
3530 * @budget : maximum number of packets that the current CPU can receive from
3531 * all interfaces.
3532 * Description :
3533 * To look at the incoming frames and clear the tx resources.
3535 static int stmmac_napi_poll(struct napi_struct *napi, int budget)
3537 struct stmmac_channel *ch =
3538 container_of(napi, struct stmmac_channel, napi);
3539 struct stmmac_priv *priv = ch->priv_data;
3540 int work_done, rx_done = 0, tx_done = 0;
3541 u32 chan = ch->index;
3543 priv->xstats.napi_poll++;
3545 if (ch->has_tx)
3546 tx_done = stmmac_tx_clean(priv, budget, chan);
3547 if (ch->has_rx)
3548 rx_done = stmmac_rx(priv, budget, chan);
3550 work_done = max(rx_done, tx_done);
3551 work_done = min(work_done, budget);
3553 if (work_done < budget && napi_complete_done(napi, work_done)) {
3554 int stat;
3556 stmmac_enable_dma_irq(priv, priv->ioaddr, chan);
3557 stat = stmmac_dma_interrupt_status(priv, priv->ioaddr,
3558 &priv->xstats, chan);
3559 if (stat && napi_reschedule(napi))
3560 stmmac_disable_dma_irq(priv, priv->ioaddr, chan);
3563 return work_done;
3567 * stmmac_tx_timeout
3568 * @dev : Pointer to net device structure
3569 * Description: this function is called when a packet transmission fails to
3570 * complete within a reasonable time. The driver will mark the error in the
3571 * netdev structure and arrange for the device to be reset to a sane state
3572 * in order to transmit a new packet.
3574 static void stmmac_tx_timeout(struct net_device *dev)
3576 struct stmmac_priv *priv = netdev_priv(dev);
3578 stmmac_global_err(priv);
3582 * stmmac_set_rx_mode - entry point for multicast addressing
3583 * @dev : pointer to the device structure
3584 * Description:
3585 * This function is a driver entry point which gets called by the kernel
3586 * whenever multicast addresses must be enabled/disabled.
3587 * Return value:
3588 * void.
3590 static void stmmac_set_rx_mode(struct net_device *dev)
3592 struct stmmac_priv *priv = netdev_priv(dev);
3594 stmmac_set_filter(priv, priv->hw, dev);
3598 * stmmac_change_mtu - entry point to change MTU size for the device.
3599 * @dev : device pointer.
3600 * @new_mtu : the new MTU size for the device.
3601 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
3602 * to drive packet transmission. Ethernet has an MTU of 1500 octets
3603 * (ETH_DATA_LEN). This value can be changed with ifconfig.
3604 * Return value:
3605 * 0 on success and an appropriate (-)ve integer as defined in errno.h
3606 * file on failure.
3608 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
3610 struct stmmac_priv *priv = netdev_priv(dev);
3611 int txfifosz = priv->plat->tx_fifo_size;
3613 if (txfifosz == 0)
3614 txfifosz = priv->dma_cap.tx_fifo_size;
3616 txfifosz /= priv->plat->tx_queues_to_use;
3618 if (netif_running(dev)) {
3619 netdev_err(priv->dev, "must be stopped to change its MTU\n");
3620 return -EBUSY;
3623 new_mtu = STMMAC_ALIGN(new_mtu);
3625 /* If condition true, FIFO is too small or MTU too large */
3626 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB))
3627 return -EINVAL;
3629 dev->mtu = new_mtu;
3631 netdev_update_features(dev);
3633 return 0;
3636 static netdev_features_t stmmac_fix_features(struct net_device *dev,
3637 netdev_features_t features)
3639 struct stmmac_priv *priv = netdev_priv(dev);
3641 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
3642 features &= ~NETIF_F_RXCSUM;
3644 if (!priv->plat->tx_coe)
3645 features &= ~NETIF_F_CSUM_MASK;
3647 /* Some GMAC devices have a bugged Jumbo frame support that
3648 * needs to have the Tx COE disabled for oversized frames
3649 * (due to limited buffer sizes). In this case we disable
3650 * the TX csum insertion in the TDES and not use SF.
3652 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
3653 features &= ~NETIF_F_CSUM_MASK;
3655 /* Disable tso if asked by ethtool */
3656 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
3657 if (features & NETIF_F_TSO)
3658 priv->tso = true;
3659 else
3660 priv->tso = false;
3663 return features;
3666 static int stmmac_set_features(struct net_device *netdev,
3667 netdev_features_t features)
3669 struct stmmac_priv *priv = netdev_priv(netdev);
3671 /* Keep the COE Type in case of csum is supporting */
3672 if (features & NETIF_F_RXCSUM)
3673 priv->hw->rx_csum = priv->plat->rx_coe;
3674 else
3675 priv->hw->rx_csum = 0;
3676 /* No check needed because rx_coe has been set before and it will be
3677 * fixed in case of issue.
3679 stmmac_rx_ipc(priv, priv->hw);
3681 return 0;
3685 * stmmac_interrupt - main ISR
3686 * @irq: interrupt number.
3687 * @dev_id: to pass the net device pointer (must be valid).
3688 * Description: this is the main driver interrupt service routine.
3689 * It can call:
3690 * o DMA service routine (to manage incoming frame reception and transmission
3691 * status)
3692 * o Core interrupts to manage: remote wake-up, management counter, LPI
3693 * interrupts.
3695 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
3697 struct net_device *dev = (struct net_device *)dev_id;
3698 struct stmmac_priv *priv = netdev_priv(dev);
3699 u32 rx_cnt = priv->plat->rx_queues_to_use;
3700 u32 tx_cnt = priv->plat->tx_queues_to_use;
3701 u32 queues_count;
3702 u32 queue;
3703 bool xmac;
3705 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac;
3706 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt;
3708 if (priv->irq_wake)
3709 pm_wakeup_event(priv->device, 0);
3711 /* Check if adapter is up */
3712 if (test_bit(STMMAC_DOWN, &priv->state))
3713 return IRQ_HANDLED;
3714 /* Check if a fatal error happened */
3715 if (stmmac_safety_feat_interrupt(priv))
3716 return IRQ_HANDLED;
3718 /* To handle GMAC own interrupts */
3719 if ((priv->plat->has_gmac) || xmac) {
3720 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats);
3721 int mtl_status;
3723 if (unlikely(status)) {
3724 /* For LPI we need to save the tx status */
3725 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
3726 priv->tx_path_in_lpi_mode = true;
3727 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
3728 priv->tx_path_in_lpi_mode = false;
3731 for (queue = 0; queue < queues_count; queue++) {
3732 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3734 mtl_status = stmmac_host_mtl_irq_status(priv, priv->hw,
3735 queue);
3736 if (mtl_status != -EINVAL)
3737 status |= mtl_status;
3739 if (status & CORE_IRQ_MTL_RX_OVERFLOW)
3740 stmmac_set_rx_tail_ptr(priv, priv->ioaddr,
3741 rx_q->rx_tail_addr,
3742 queue);
3745 /* PCS link status */
3746 if (priv->hw->pcs) {
3747 if (priv->xstats.pcs_link)
3748 netif_carrier_on(dev);
3749 else
3750 netif_carrier_off(dev);
3754 /* To handle DMA interrupts */
3755 stmmac_dma_interrupt(priv);
3757 return IRQ_HANDLED;
3760 #ifdef CONFIG_NET_POLL_CONTROLLER
3761 /* Polling receive - used by NETCONSOLE and other diagnostic tools
3762 * to allow network I/O with interrupts disabled.
3764 static void stmmac_poll_controller(struct net_device *dev)
3766 disable_irq(dev->irq);
3767 stmmac_interrupt(dev->irq, dev);
3768 enable_irq(dev->irq);
3770 #endif
3773 * stmmac_ioctl - Entry point for the Ioctl
3774 * @dev: Device pointer.
3775 * @rq: An IOCTL specefic structure, that can contain a pointer to
3776 * a proprietary structure used to pass information to the driver.
3777 * @cmd: IOCTL command
3778 * Description:
3779 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
3781 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3783 int ret = -EOPNOTSUPP;
3785 if (!netif_running(dev))
3786 return -EINVAL;
3788 switch (cmd) {
3789 case SIOCGMIIPHY:
3790 case SIOCGMIIREG:
3791 case SIOCSMIIREG:
3792 if (!dev->phydev)
3793 return -EINVAL;
3794 ret = phy_mii_ioctl(dev->phydev, rq, cmd);
3795 break;
3796 case SIOCSHWTSTAMP:
3797 ret = stmmac_hwtstamp_ioctl(dev, rq);
3798 break;
3799 default:
3800 break;
3803 return ret;
3806 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
3807 void *cb_priv)
3809 struct stmmac_priv *priv = cb_priv;
3810 int ret = -EOPNOTSUPP;
3812 stmmac_disable_all_queues(priv);
3814 switch (type) {
3815 case TC_SETUP_CLSU32:
3816 if (tc_cls_can_offload_and_chain0(priv->dev, type_data))
3817 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data);
3818 break;
3819 default:
3820 break;
3823 stmmac_enable_all_queues(priv);
3824 return ret;
3827 static int stmmac_setup_tc_block(struct stmmac_priv *priv,
3828 struct tc_block_offload *f)
3830 if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
3831 return -EOPNOTSUPP;
3833 switch (f->command) {
3834 case TC_BLOCK_BIND:
3835 return tcf_block_cb_register(f->block, stmmac_setup_tc_block_cb,
3836 priv, priv, f->extack);
3837 case TC_BLOCK_UNBIND:
3838 tcf_block_cb_unregister(f->block, stmmac_setup_tc_block_cb, priv);
3839 return 0;
3840 default:
3841 return -EOPNOTSUPP;
3845 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type,
3846 void *type_data)
3848 struct stmmac_priv *priv = netdev_priv(ndev);
3850 switch (type) {
3851 case TC_SETUP_BLOCK:
3852 return stmmac_setup_tc_block(priv, type_data);
3853 case TC_SETUP_QDISC_CBS:
3854 return stmmac_tc_setup_cbs(priv, priv, type_data);
3855 default:
3856 return -EOPNOTSUPP;
3860 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb,
3861 struct net_device *sb_dev,
3862 select_queue_fallback_t fallback)
3864 if (skb_shinfo(skb)->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
3866 * There is no way to determine the number of TSO
3867 * capable Queues. Let's use always the Queue 0
3868 * because if TSO is supported then at least this
3869 * one will be capable.
3871 return 0;
3874 return fallback(dev, skb, NULL) % dev->real_num_tx_queues;
3877 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
3879 struct stmmac_priv *priv = netdev_priv(ndev);
3880 int ret = 0;
3882 ret = eth_mac_addr(ndev, addr);
3883 if (ret)
3884 return ret;
3886 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0);
3888 return ret;
3891 #ifdef CONFIG_DEBUG_FS
3892 static struct dentry *stmmac_fs_dir;
3894 static void sysfs_display_ring(void *head, int size, int extend_desc,
3895 struct seq_file *seq)
3897 int i;
3898 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
3899 struct dma_desc *p = (struct dma_desc *)head;
3901 for (i = 0; i < size; i++) {
3902 if (extend_desc) {
3903 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
3904 i, (unsigned int)virt_to_phys(ep),
3905 le32_to_cpu(ep->basic.des0),
3906 le32_to_cpu(ep->basic.des1),
3907 le32_to_cpu(ep->basic.des2),
3908 le32_to_cpu(ep->basic.des3));
3909 ep++;
3910 } else {
3911 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
3912 i, (unsigned int)virt_to_phys(p),
3913 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
3914 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
3915 p++;
3917 seq_printf(seq, "\n");
3921 static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
3923 struct net_device *dev = seq->private;
3924 struct stmmac_priv *priv = netdev_priv(dev);
3925 u32 rx_count = priv->plat->rx_queues_to_use;
3926 u32 tx_count = priv->plat->tx_queues_to_use;
3927 u32 queue;
3929 if ((dev->flags & IFF_UP) == 0)
3930 return 0;
3932 for (queue = 0; queue < rx_count; queue++) {
3933 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
3935 seq_printf(seq, "RX Queue %d:\n", queue);
3937 if (priv->extend_desc) {
3938 seq_printf(seq, "Extended descriptor ring:\n");
3939 sysfs_display_ring((void *)rx_q->dma_erx,
3940 DMA_RX_SIZE, 1, seq);
3941 } else {
3942 seq_printf(seq, "Descriptor ring:\n");
3943 sysfs_display_ring((void *)rx_q->dma_rx,
3944 DMA_RX_SIZE, 0, seq);
3948 for (queue = 0; queue < tx_count; queue++) {
3949 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
3951 seq_printf(seq, "TX Queue %d:\n", queue);
3953 if (priv->extend_desc) {
3954 seq_printf(seq, "Extended descriptor ring:\n");
3955 sysfs_display_ring((void *)tx_q->dma_etx,
3956 DMA_TX_SIZE, 1, seq);
3957 } else {
3958 seq_printf(seq, "Descriptor ring:\n");
3959 sysfs_display_ring((void *)tx_q->dma_tx,
3960 DMA_TX_SIZE, 0, seq);
3964 return 0;
3967 static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
3969 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
3972 /* Debugfs files, should appear in /sys/kernel/debug/stmmaceth/eth0 */
3974 static const struct file_operations stmmac_rings_status_fops = {
3975 .owner = THIS_MODULE,
3976 .open = stmmac_sysfs_ring_open,
3977 .read = seq_read,
3978 .llseek = seq_lseek,
3979 .release = single_release,
3982 static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
3984 struct net_device *dev = seq->private;
3985 struct stmmac_priv *priv = netdev_priv(dev);
3987 if (!priv->hw_cap_support) {
3988 seq_printf(seq, "DMA HW features not supported\n");
3989 return 0;
3992 seq_printf(seq, "==============================\n");
3993 seq_printf(seq, "\tDMA HW features\n");
3994 seq_printf(seq, "==============================\n");
3996 seq_printf(seq, "\t10/100 Mbps: %s\n",
3997 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
3998 seq_printf(seq, "\t1000 Mbps: %s\n",
3999 (priv->dma_cap.mbps_1000) ? "Y" : "N");
4000 seq_printf(seq, "\tHalf duplex: %s\n",
4001 (priv->dma_cap.half_duplex) ? "Y" : "N");
4002 seq_printf(seq, "\tHash Filter: %s\n",
4003 (priv->dma_cap.hash_filter) ? "Y" : "N");
4004 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
4005 (priv->dma_cap.multi_addr) ? "Y" : "N");
4006 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n",
4007 (priv->dma_cap.pcs) ? "Y" : "N");
4008 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
4009 (priv->dma_cap.sma_mdio) ? "Y" : "N");
4010 seq_printf(seq, "\tPMT Remote wake up: %s\n",
4011 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
4012 seq_printf(seq, "\tPMT Magic Frame: %s\n",
4013 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
4014 seq_printf(seq, "\tRMON module: %s\n",
4015 (priv->dma_cap.rmon) ? "Y" : "N");
4016 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
4017 (priv->dma_cap.time_stamp) ? "Y" : "N");
4018 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n",
4019 (priv->dma_cap.atime_stamp) ? "Y" : "N");
4020 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n",
4021 (priv->dma_cap.eee) ? "Y" : "N");
4022 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
4023 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
4024 (priv->dma_cap.tx_coe) ? "Y" : "N");
4025 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
4026 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
4027 (priv->dma_cap.rx_coe) ? "Y" : "N");
4028 } else {
4029 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
4030 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
4031 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
4032 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
4034 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
4035 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
4036 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
4037 priv->dma_cap.number_rx_channel);
4038 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
4039 priv->dma_cap.number_tx_channel);
4040 seq_printf(seq, "\tEnhanced descriptors: %s\n",
4041 (priv->dma_cap.enh_desc) ? "Y" : "N");
4043 return 0;
4046 static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
4048 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
4051 static const struct file_operations stmmac_dma_cap_fops = {
4052 .owner = THIS_MODULE,
4053 .open = stmmac_sysfs_dma_cap_open,
4054 .read = seq_read,
4055 .llseek = seq_lseek,
4056 .release = single_release,
4059 static int stmmac_init_fs(struct net_device *dev)
4061 struct stmmac_priv *priv = netdev_priv(dev);
4063 /* Create per netdev entries */
4064 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
4066 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
4067 netdev_err(priv->dev, "ERROR failed to create debugfs directory\n");
4069 return -ENOMEM;
4072 /* Entry to report DMA RX/TX rings */
4073 priv->dbgfs_rings_status =
4074 debugfs_create_file("descriptors_status", 0444,
4075 priv->dbgfs_dir, dev,
4076 &stmmac_rings_status_fops);
4078 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
4079 netdev_err(priv->dev, "ERROR creating stmmac ring debugfs file\n");
4080 debugfs_remove_recursive(priv->dbgfs_dir);
4082 return -ENOMEM;
4085 /* Entry to report the DMA HW features */
4086 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", 0444,
4087 priv->dbgfs_dir,
4088 dev, &stmmac_dma_cap_fops);
4090 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
4091 netdev_err(priv->dev, "ERROR creating stmmac MMC debugfs file\n");
4092 debugfs_remove_recursive(priv->dbgfs_dir);
4094 return -ENOMEM;
4097 return 0;
4100 static void stmmac_exit_fs(struct net_device *dev)
4102 struct stmmac_priv *priv = netdev_priv(dev);
4104 debugfs_remove_recursive(priv->dbgfs_dir);
4106 #endif /* CONFIG_DEBUG_FS */
4108 static const struct net_device_ops stmmac_netdev_ops = {
4109 .ndo_open = stmmac_open,
4110 .ndo_start_xmit = stmmac_xmit,
4111 .ndo_stop = stmmac_release,
4112 .ndo_change_mtu = stmmac_change_mtu,
4113 .ndo_fix_features = stmmac_fix_features,
4114 .ndo_set_features = stmmac_set_features,
4115 .ndo_set_rx_mode = stmmac_set_rx_mode,
4116 .ndo_tx_timeout = stmmac_tx_timeout,
4117 .ndo_do_ioctl = stmmac_ioctl,
4118 .ndo_setup_tc = stmmac_setup_tc,
4119 .ndo_select_queue = stmmac_select_queue,
4120 #ifdef CONFIG_NET_POLL_CONTROLLER
4121 .ndo_poll_controller = stmmac_poll_controller,
4122 #endif
4123 .ndo_set_mac_address = stmmac_set_mac_address,
4126 static void stmmac_reset_subtask(struct stmmac_priv *priv)
4128 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state))
4129 return;
4130 if (test_bit(STMMAC_DOWN, &priv->state))
4131 return;
4133 netdev_err(priv->dev, "Reset adapter.\n");
4135 rtnl_lock();
4136 netif_trans_update(priv->dev);
4137 while (test_and_set_bit(STMMAC_RESETING, &priv->state))
4138 usleep_range(1000, 2000);
4140 set_bit(STMMAC_DOWN, &priv->state);
4141 dev_close(priv->dev);
4142 dev_open(priv->dev);
4143 clear_bit(STMMAC_DOWN, &priv->state);
4144 clear_bit(STMMAC_RESETING, &priv->state);
4145 rtnl_unlock();
4148 static void stmmac_service_task(struct work_struct *work)
4150 struct stmmac_priv *priv = container_of(work, struct stmmac_priv,
4151 service_task);
4153 stmmac_reset_subtask(priv);
4154 clear_bit(STMMAC_SERVICE_SCHED, &priv->state);
4158 * stmmac_hw_init - Init the MAC device
4159 * @priv: driver private structure
4160 * Description: this function is to configure the MAC device according to
4161 * some platform parameters or the HW capability register. It prepares the
4162 * driver to use either ring or chain modes and to setup either enhanced or
4163 * normal descriptors.
4165 static int stmmac_hw_init(struct stmmac_priv *priv)
4167 int ret;
4169 /* dwmac-sun8i only work in chain mode */
4170 if (priv->plat->has_sun8i)
4171 chain_mode = 1;
4172 priv->chain_mode = chain_mode;
4174 /* Initialize HW Interface */
4175 ret = stmmac_hwif_init(priv);
4176 if (ret)
4177 return ret;
4179 /* Get the HW capability (new GMAC newer than 3.50a) */
4180 priv->hw_cap_support = stmmac_get_hw_features(priv);
4181 if (priv->hw_cap_support) {
4182 dev_info(priv->device, "DMA HW capability register supported\n");
4184 /* We can override some gmac/dma configuration fields: e.g.
4185 * enh_desc, tx_coe (e.g. that are passed through the
4186 * platform) with the values from the HW capability
4187 * register (if supported).
4189 priv->plat->enh_desc = priv->dma_cap.enh_desc;
4190 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
4191 priv->hw->pmt = priv->plat->pmt;
4193 /* TXCOE doesn't work in thresh DMA mode */
4194 if (priv->plat->force_thresh_dma_mode)
4195 priv->plat->tx_coe = 0;
4196 else
4197 priv->plat->tx_coe = priv->dma_cap.tx_coe;
4199 /* In case of GMAC4 rx_coe is from HW cap register. */
4200 priv->plat->rx_coe = priv->dma_cap.rx_coe;
4202 if (priv->dma_cap.rx_coe_type2)
4203 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
4204 else if (priv->dma_cap.rx_coe_type1)
4205 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
4207 } else {
4208 dev_info(priv->device, "No HW DMA feature register supported\n");
4211 if (priv->plat->rx_coe) {
4212 priv->hw->rx_csum = priv->plat->rx_coe;
4213 dev_info(priv->device, "RX Checksum Offload Engine supported\n");
4214 if (priv->synopsys_id < DWMAC_CORE_4_00)
4215 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum);
4217 if (priv->plat->tx_coe)
4218 dev_info(priv->device, "TX Checksum insertion supported\n");
4220 if (priv->plat->pmt) {
4221 dev_info(priv->device, "Wake-Up On Lan supported\n");
4222 device_set_wakeup_capable(priv->device, 1);
4225 if (priv->dma_cap.tsoen)
4226 dev_info(priv->device, "TSO supported\n");
4228 /* Run HW quirks, if any */
4229 if (priv->hwif_quirks) {
4230 ret = priv->hwif_quirks(priv);
4231 if (ret)
4232 return ret;
4235 /* Rx Watchdog is available in the COREs newer than the 3.40.
4236 * In some case, for example on bugged HW this feature
4237 * has to be disable and this can be done by passing the
4238 * riwt_off field from the platform.
4240 if (((priv->synopsys_id >= DWMAC_CORE_3_50) ||
4241 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) {
4242 priv->use_riwt = 1;
4243 dev_info(priv->device,
4244 "Enable RX Mitigation via HW Watchdog Timer\n");
4247 return 0;
4251 * stmmac_dvr_probe
4252 * @device: device pointer
4253 * @plat_dat: platform data pointer
4254 * @res: stmmac resource pointer
4255 * Description: this is the main probe function used to
4256 * call the alloc_etherdev, allocate the priv structure.
4257 * Return:
4258 * returns 0 on success, otherwise errno.
4260 int stmmac_dvr_probe(struct device *device,
4261 struct plat_stmmacenet_data *plat_dat,
4262 struct stmmac_resources *res)
4264 struct net_device *ndev = NULL;
4265 struct stmmac_priv *priv;
4266 u32 queue, maxq;
4267 int ret = 0;
4269 ndev = alloc_etherdev_mqs(sizeof(struct stmmac_priv),
4270 MTL_MAX_TX_QUEUES,
4271 MTL_MAX_RX_QUEUES);
4272 if (!ndev)
4273 return -ENOMEM;
4275 SET_NETDEV_DEV(ndev, device);
4277 priv = netdev_priv(ndev);
4278 priv->device = device;
4279 priv->dev = ndev;
4281 stmmac_set_ethtool_ops(ndev);
4282 priv->pause = pause;
4283 priv->plat = plat_dat;
4284 priv->ioaddr = res->addr;
4285 priv->dev->base_addr = (unsigned long)res->addr;
4287 priv->dev->irq = res->irq;
4288 priv->wol_irq = res->wol_irq;
4289 priv->lpi_irq = res->lpi_irq;
4291 if (res->mac)
4292 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
4294 dev_set_drvdata(device, priv->dev);
4296 /* Verify driver arguments */
4297 stmmac_verify_args();
4299 /* Allocate workqueue */
4300 priv->wq = create_singlethread_workqueue("stmmac_wq");
4301 if (!priv->wq) {
4302 dev_err(priv->device, "failed to create workqueue\n");
4303 ret = -ENOMEM;
4304 goto error_wq;
4307 INIT_WORK(&priv->service_task, stmmac_service_task);
4309 /* Override with kernel parameters if supplied XXX CRS XXX
4310 * this needs to have multiple instances
4312 if ((phyaddr >= 0) && (phyaddr <= 31))
4313 priv->plat->phy_addr = phyaddr;
4315 if (priv->plat->stmmac_rst) {
4316 ret = reset_control_assert(priv->plat->stmmac_rst);
4317 reset_control_deassert(priv->plat->stmmac_rst);
4318 /* Some reset controllers have only reset callback instead of
4319 * assert + deassert callbacks pair.
4321 if (ret == -ENOTSUPP)
4322 reset_control_reset(priv->plat->stmmac_rst);
4325 /* Init MAC and get the capabilities */
4326 ret = stmmac_hw_init(priv);
4327 if (ret)
4328 goto error_hw_init;
4330 stmmac_check_ether_addr(priv);
4332 /* Configure real RX and TX queues */
4333 netif_set_real_num_rx_queues(ndev, priv->plat->rx_queues_to_use);
4334 netif_set_real_num_tx_queues(ndev, priv->plat->tx_queues_to_use);
4336 ndev->netdev_ops = &stmmac_netdev_ops;
4338 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
4339 NETIF_F_RXCSUM;
4341 ret = stmmac_tc_init(priv, priv);
4342 if (!ret) {
4343 ndev->hw_features |= NETIF_F_HW_TC;
4346 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
4347 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
4348 priv->tso = true;
4349 dev_info(priv->device, "TSO feature enabled\n");
4351 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
4352 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
4353 #ifdef STMMAC_VLAN_TAG_USED
4354 /* Both mac100 and gmac support receive VLAN tag detection */
4355 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX;
4356 #endif
4357 priv->msg_enable = netif_msg_init(debug, default_msg_level);
4359 /* MTU range: 46 - hw-specific max */
4360 ndev->min_mtu = ETH_ZLEN - ETH_HLEN;
4361 if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
4362 ndev->max_mtu = JUMBO_LEN;
4363 else if (priv->plat->has_xgmac)
4364 ndev->max_mtu = XGMAC_JUMBO_LEN;
4365 else
4366 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
4367 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
4368 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
4370 if ((priv->plat->maxmtu < ndev->max_mtu) &&
4371 (priv->plat->maxmtu >= ndev->min_mtu))
4372 ndev->max_mtu = priv->plat->maxmtu;
4373 else if (priv->plat->maxmtu < ndev->min_mtu)
4374 dev_warn(priv->device,
4375 "%s: warning: maxmtu having invalid value (%d)\n",
4376 __func__, priv->plat->maxmtu);
4378 if (flow_ctrl)
4379 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
4381 /* Setup channels NAPI */
4382 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
4384 for (queue = 0; queue < maxq; queue++) {
4385 struct stmmac_channel *ch = &priv->channel[queue];
4387 ch->priv_data = priv;
4388 ch->index = queue;
4390 if (queue < priv->plat->rx_queues_to_use)
4391 ch->has_rx = true;
4392 if (queue < priv->plat->tx_queues_to_use)
4393 ch->has_tx = true;
4395 netif_napi_add(ndev, &ch->napi, stmmac_napi_poll,
4396 NAPI_POLL_WEIGHT);
4399 mutex_init(&priv->lock);
4401 /* If a specific clk_csr value is passed from the platform
4402 * this means that the CSR Clock Range selection cannot be
4403 * changed at run-time and it is fixed. Viceversa the driver'll try to
4404 * set the MDC clock dynamically according to the csr actual
4405 * clock input.
4407 if (!priv->plat->clk_csr)
4408 stmmac_clk_csr_set(priv);
4409 else
4410 priv->clk_csr = priv->plat->clk_csr;
4412 stmmac_check_pcs_mode(priv);
4414 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4415 priv->hw->pcs != STMMAC_PCS_TBI &&
4416 priv->hw->pcs != STMMAC_PCS_RTBI) {
4417 /* MDIO bus Registration */
4418 ret = stmmac_mdio_register(ndev);
4419 if (ret < 0) {
4420 dev_err(priv->device,
4421 "%s: MDIO bus (id: %d) registration failed",
4422 __func__, priv->plat->bus_id);
4423 goto error_mdio_register;
4427 ret = register_netdev(ndev);
4428 if (ret) {
4429 dev_err(priv->device, "%s: ERROR %i registering the device\n",
4430 __func__, ret);
4431 goto error_netdev_register;
4434 #ifdef CONFIG_DEBUG_FS
4435 ret = stmmac_init_fs(ndev);
4436 if (ret < 0)
4437 netdev_warn(priv->dev, "%s: failed debugFS registration\n",
4438 __func__);
4439 #endif
4441 return ret;
4443 error_netdev_register:
4444 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4445 priv->hw->pcs != STMMAC_PCS_TBI &&
4446 priv->hw->pcs != STMMAC_PCS_RTBI)
4447 stmmac_mdio_unregister(ndev);
4448 error_mdio_register:
4449 for (queue = 0; queue < maxq; queue++) {
4450 struct stmmac_channel *ch = &priv->channel[queue];
4452 netif_napi_del(&ch->napi);
4454 error_hw_init:
4455 destroy_workqueue(priv->wq);
4456 error_wq:
4457 free_netdev(ndev);
4459 return ret;
4461 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
4464 * stmmac_dvr_remove
4465 * @dev: device pointer
4466 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
4467 * changes the link status, releases the DMA descriptor rings.
4469 int stmmac_dvr_remove(struct device *dev)
4471 struct net_device *ndev = dev_get_drvdata(dev);
4472 struct stmmac_priv *priv = netdev_priv(ndev);
4474 netdev_info(priv->dev, "%s: removing driver", __func__);
4476 #ifdef CONFIG_DEBUG_FS
4477 stmmac_exit_fs(ndev);
4478 #endif
4479 stmmac_stop_all_dma(priv);
4481 stmmac_mac_set(priv, priv->ioaddr, false);
4482 netif_carrier_off(ndev);
4483 unregister_netdev(ndev);
4484 if (priv->plat->stmmac_rst)
4485 reset_control_assert(priv->plat->stmmac_rst);
4486 clk_disable_unprepare(priv->plat->pclk);
4487 clk_disable_unprepare(priv->plat->stmmac_clk);
4488 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
4489 priv->hw->pcs != STMMAC_PCS_TBI &&
4490 priv->hw->pcs != STMMAC_PCS_RTBI)
4491 stmmac_mdio_unregister(ndev);
4492 destroy_workqueue(priv->wq);
4493 mutex_destroy(&priv->lock);
4494 free_netdev(ndev);
4496 return 0;
4498 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
4501 * stmmac_suspend - suspend callback
4502 * @dev: device pointer
4503 * Description: this is the function to suspend the device and it is called
4504 * by the platform driver to stop the network queue, release the resources,
4505 * program the PMT register (for WoL), clean and release driver resources.
4507 int stmmac_suspend(struct device *dev)
4509 struct net_device *ndev = dev_get_drvdata(dev);
4510 struct stmmac_priv *priv = netdev_priv(ndev);
4511 u32 chan;
4513 if (!ndev || !netif_running(ndev))
4514 return 0;
4516 if (ndev->phydev)
4517 phy_stop(ndev->phydev);
4519 mutex_lock(&priv->lock);
4521 netif_device_detach(ndev);
4522 stmmac_stop_all_queues(priv);
4524 stmmac_disable_all_queues(priv);
4526 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
4527 del_timer_sync(&priv->tx_queue[chan].txtimer);
4529 /* Stop TX/RX DMA */
4530 stmmac_stop_all_dma(priv);
4532 /* Enable Power down mode by programming the PMT regs */
4533 if (device_may_wakeup(priv->device)) {
4534 stmmac_pmt(priv, priv->hw, priv->wolopts);
4535 priv->irq_wake = 1;
4536 } else {
4537 stmmac_mac_set(priv, priv->ioaddr, false);
4538 pinctrl_pm_select_sleep_state(priv->device);
4539 /* Disable clock in case of PWM is off */
4540 if (priv->plat->clk_ptp_ref)
4541 clk_disable_unprepare(priv->plat->clk_ptp_ref);
4542 clk_disable_unprepare(priv->plat->pclk);
4543 clk_disable_unprepare(priv->plat->stmmac_clk);
4545 mutex_unlock(&priv->lock);
4547 priv->oldlink = false;
4548 priv->speed = SPEED_UNKNOWN;
4549 priv->oldduplex = DUPLEX_UNKNOWN;
4550 return 0;
4552 EXPORT_SYMBOL_GPL(stmmac_suspend);
4555 * stmmac_reset_queues_param - reset queue parameters
4556 * @dev: device pointer
4558 static void stmmac_reset_queues_param(struct stmmac_priv *priv)
4560 u32 rx_cnt = priv->plat->rx_queues_to_use;
4561 u32 tx_cnt = priv->plat->tx_queues_to_use;
4562 u32 queue;
4564 for (queue = 0; queue < rx_cnt; queue++) {
4565 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue];
4567 rx_q->cur_rx = 0;
4568 rx_q->dirty_rx = 0;
4571 for (queue = 0; queue < tx_cnt; queue++) {
4572 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue];
4574 tx_q->cur_tx = 0;
4575 tx_q->dirty_tx = 0;
4576 tx_q->mss = 0;
4581 * stmmac_resume - resume callback
4582 * @dev: device pointer
4583 * Description: when resume this function is invoked to setup the DMA and CORE
4584 * in a usable state.
4586 int stmmac_resume(struct device *dev)
4588 struct net_device *ndev = dev_get_drvdata(dev);
4589 struct stmmac_priv *priv = netdev_priv(ndev);
4591 if (!netif_running(ndev))
4592 return 0;
4594 /* Power Down bit, into the PM register, is cleared
4595 * automatically as soon as a magic packet or a Wake-up frame
4596 * is received. Anyway, it's better to manually clear
4597 * this bit because it can generate problems while resuming
4598 * from another devices (e.g. serial console).
4600 if (device_may_wakeup(priv->device)) {
4601 mutex_lock(&priv->lock);
4602 stmmac_pmt(priv, priv->hw, 0);
4603 mutex_unlock(&priv->lock);
4604 priv->irq_wake = 0;
4605 } else {
4606 pinctrl_pm_select_default_state(priv->device);
4607 /* enable the clk previously disabled */
4608 clk_prepare_enable(priv->plat->stmmac_clk);
4609 clk_prepare_enable(priv->plat->pclk);
4610 if (priv->plat->clk_ptp_ref)
4611 clk_prepare_enable(priv->plat->clk_ptp_ref);
4612 /* reset the phy so that it's ready */
4613 if (priv->mii)
4614 stmmac_mdio_reset(priv->mii);
4617 netif_device_attach(ndev);
4619 mutex_lock(&priv->lock);
4621 stmmac_reset_queues_param(priv);
4623 stmmac_clear_descriptors(priv);
4625 stmmac_hw_setup(ndev, false);
4626 stmmac_init_tx_coalesce(priv);
4627 stmmac_set_rx_mode(ndev);
4629 stmmac_enable_all_queues(priv);
4631 stmmac_start_all_queues(priv);
4633 mutex_unlock(&priv->lock);
4635 if (ndev->phydev)
4636 phy_start(ndev->phydev);
4638 return 0;
4640 EXPORT_SYMBOL_GPL(stmmac_resume);
4642 #ifndef MODULE
4643 static int __init stmmac_cmdline_opt(char *str)
4645 char *opt;
4647 if (!str || !*str)
4648 return -EINVAL;
4649 while ((opt = strsep(&str, ",")) != NULL) {
4650 if (!strncmp(opt, "debug:", 6)) {
4651 if (kstrtoint(opt + 6, 0, &debug))
4652 goto err;
4653 } else if (!strncmp(opt, "phyaddr:", 8)) {
4654 if (kstrtoint(opt + 8, 0, &phyaddr))
4655 goto err;
4656 } else if (!strncmp(opt, "buf_sz:", 7)) {
4657 if (kstrtoint(opt + 7, 0, &buf_sz))
4658 goto err;
4659 } else if (!strncmp(opt, "tc:", 3)) {
4660 if (kstrtoint(opt + 3, 0, &tc))
4661 goto err;
4662 } else if (!strncmp(opt, "watchdog:", 9)) {
4663 if (kstrtoint(opt + 9, 0, &watchdog))
4664 goto err;
4665 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
4666 if (kstrtoint(opt + 10, 0, &flow_ctrl))
4667 goto err;
4668 } else if (!strncmp(opt, "pause:", 6)) {
4669 if (kstrtoint(opt + 6, 0, &pause))
4670 goto err;
4671 } else if (!strncmp(opt, "eee_timer:", 10)) {
4672 if (kstrtoint(opt + 10, 0, &eee_timer))
4673 goto err;
4674 } else if (!strncmp(opt, "chain_mode:", 11)) {
4675 if (kstrtoint(opt + 11, 0, &chain_mode))
4676 goto err;
4679 return 0;
4681 err:
4682 pr_err("%s: ERROR broken module parameter conversion", __func__);
4683 return -EINVAL;
4686 __setup("stmmaceth=", stmmac_cmdline_opt);
4687 #endif /* MODULE */
4689 static int __init stmmac_init(void)
4691 #ifdef CONFIG_DEBUG_FS
4692 /* Create debugfs main directory if it doesn't exist yet */
4693 if (!stmmac_fs_dir) {
4694 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
4696 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
4697 pr_err("ERROR %s, debugfs create directory failed\n",
4698 STMMAC_RESOURCE_NAME);
4700 return -ENOMEM;
4703 #endif
4705 return 0;
4708 static void __exit stmmac_exit(void)
4710 #ifdef CONFIG_DEBUG_FS
4711 debugfs_remove_recursive(stmmac_fs_dir);
4712 #endif
4715 module_init(stmmac_init)
4716 module_exit(stmmac_exit)
4718 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
4719 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
4720 MODULE_LICENSE("GPL");