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
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 The full GNU General Public License is included in this distribution in
21 the file called "COPYING".
23 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
25 Documentation available at:
26 http://www.stlinux.com
28 https://bugzilla.stlinux.com/
29 *******************************************************************************/
31 #include <linux/clk.h>
32 #include <linux/kernel.h>
33 #include <linux/interrupt.h>
35 #include <linux/tcp.h>
36 #include <linux/skbuff.h>
37 #include <linux/ethtool.h>
38 #include <linux/if_ether.h>
39 #include <linux/crc32.h>
40 #include <linux/mii.h>
42 #include <linux/if_vlan.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/slab.h>
45 #include <linux/prefetch.h>
46 #include <linux/pinctrl/consumer.h>
47 #ifdef CONFIG_DEBUG_FS
48 #include <linux/debugfs.h>
49 #include <linux/seq_file.h>
50 #endif /* CONFIG_DEBUG_FS */
51 #include <linux/net_tstamp.h>
52 #include "stmmac_ptp.h"
54 #include <linux/reset.h>
55 #include <linux/of_mdio.h>
56 #include "dwmac1000.h"
58 #define STMMAC_ALIGN(x) ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16)
59 #define TSO_MAX_BUFF_SIZE (SZ_16K - 1)
61 /* Module parameters */
63 static int watchdog = TX_TIMEO;
64 module_param(watchdog, int, S_IRUGO | S_IWUSR);
65 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
67 static int debug = -1;
68 module_param(debug, int, S_IRUGO | S_IWUSR);
69 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
71 static int phyaddr = -1;
72 module_param(phyaddr, int, S_IRUGO);
73 MODULE_PARM_DESC(phyaddr, "Physical device address");
75 #define STMMAC_TX_THRESH (DMA_TX_SIZE / 4)
76 #define STMMAC_RX_THRESH (DMA_RX_SIZE / 4)
78 static int flow_ctrl = FLOW_OFF;
79 module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
80 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
82 static int pause = PAUSE_TIME;
83 module_param(pause, int, S_IRUGO | S_IWUSR);
84 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
87 static int tc = TC_DEFAULT;
88 module_param(tc, int, S_IRUGO | S_IWUSR);
89 MODULE_PARM_DESC(tc, "DMA threshold control value");
91 #define DEFAULT_BUFSIZE 1536
92 static int buf_sz = DEFAULT_BUFSIZE;
93 module_param(buf_sz, int, S_IRUGO | S_IWUSR);
94 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
96 #define STMMAC_RX_COPYBREAK 256
98 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
99 NETIF_MSG_LINK | NETIF_MSG_IFUP |
100 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
102 #define STMMAC_DEFAULT_LPI_TIMER 1000
103 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
104 module_param(eee_timer, int, S_IRUGO | S_IWUSR);
105 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
106 #define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
108 /* By default the driver will use the ring mode to manage tx and rx descriptors
109 * but passing this value so user can force to use the chain instead of the ring
111 static unsigned int chain_mode;
112 module_param(chain_mode, int, S_IRUGO);
113 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
115 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
117 #ifdef CONFIG_DEBUG_FS
118 static int stmmac_init_fs(struct net_device *dev);
119 static void stmmac_exit_fs(struct net_device *dev);
122 #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
125 * stmmac_verify_args - verify the driver parameters.
126 * Description: it checks the driver parameters and set a default in case of
129 static void stmmac_verify_args(void)
131 if (unlikely(watchdog < 0))
133 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB)))
134 buf_sz = DEFAULT_BUFSIZE;
135 if (unlikely(flow_ctrl > 1))
136 flow_ctrl = FLOW_AUTO;
137 else if (likely(flow_ctrl < 0))
138 flow_ctrl = FLOW_OFF;
139 if (unlikely((pause < 0) || (pause > 0xffff)))
142 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
146 * stmmac_clk_csr_set - dynamically set the MDC clock
147 * @priv: driver private structure
148 * Description: this is to dynamically set the MDC clock according to the csr
151 * If a specific clk_csr value is passed from the platform
152 * this means that the CSR Clock Range selection cannot be
153 * changed at run-time and it is fixed (as reported in the driver
154 * documentation). Viceversa the driver will try to set the MDC
155 * clock dynamically according to the actual clock input.
157 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
161 clk_rate = clk_get_rate(priv->stmmac_clk);
163 /* Platform provided default clk_csr would be assumed valid
164 * for all other cases except for the below mentioned ones.
165 * For values higher than the IEEE 802.3 specified frequency
166 * we can not estimate the proper divider as it is not known
167 * the frequency of clk_csr_i. So we do not change the default
170 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
171 if (clk_rate < CSR_F_35M)
172 priv->clk_csr = STMMAC_CSR_20_35M;
173 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
174 priv->clk_csr = STMMAC_CSR_35_60M;
175 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
176 priv->clk_csr = STMMAC_CSR_60_100M;
177 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
178 priv->clk_csr = STMMAC_CSR_100_150M;
179 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
180 priv->clk_csr = STMMAC_CSR_150_250M;
181 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
182 priv->clk_csr = STMMAC_CSR_250_300M;
186 static void print_pkt(unsigned char *buf, int len)
188 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf);
189 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len);
192 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
196 if (priv->dirty_tx > priv->cur_tx)
197 avail = priv->dirty_tx - priv->cur_tx - 1;
199 avail = DMA_TX_SIZE - priv->cur_tx + priv->dirty_tx - 1;
204 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv)
208 if (priv->dirty_rx <= priv->cur_rx)
209 dirty = priv->cur_rx - priv->dirty_rx;
211 dirty = DMA_RX_SIZE - priv->dirty_rx + priv->cur_rx;
217 * stmmac_hw_fix_mac_speed - callback for speed selection
218 * @priv: driver private structure
219 * Description: on some platforms (e.g. ST), some HW system configuraton
220 * registers have to be set according to the link speed negotiated.
222 static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
224 struct phy_device *phydev = priv->phydev;
226 if (likely(priv->plat->fix_mac_speed))
227 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
231 * stmmac_enable_eee_mode - check and enter in LPI mode
232 * @priv: driver private structure
233 * Description: this function is to verify and enter in LPI mode in case of
236 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
238 /* Check and enter in LPI mode */
239 if ((priv->dirty_tx == priv->cur_tx) &&
240 (priv->tx_path_in_lpi_mode == false))
241 priv->hw->mac->set_eee_mode(priv->hw);
245 * stmmac_disable_eee_mode - disable and exit from LPI mode
246 * @priv: driver private structure
247 * Description: this function is to exit and disable EEE in case of
248 * LPI state is true. This is called by the xmit.
250 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
252 priv->hw->mac->reset_eee_mode(priv->hw);
253 del_timer_sync(&priv->eee_ctrl_timer);
254 priv->tx_path_in_lpi_mode = false;
258 * stmmac_eee_ctrl_timer - EEE TX SW timer.
261 * if there is no data transfer and if we are not in LPI state,
262 * then MAC Transmitter can be moved to LPI state.
264 static void stmmac_eee_ctrl_timer(unsigned long arg)
266 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
268 stmmac_enable_eee_mode(priv);
269 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
273 * stmmac_eee_init - init EEE
274 * @priv: driver private structure
276 * if the GMAC supports the EEE (from the HW cap reg) and the phy device
277 * can also manage EEE, this function enable the LPI state and start related
280 bool stmmac_eee_init(struct stmmac_priv *priv)
283 int interface = priv->plat->interface;
286 if ((interface != PHY_INTERFACE_MODE_MII) &&
287 (interface != PHY_INTERFACE_MODE_GMII) &&
288 !phy_interface_mode_is_rgmii(interface))
291 /* Using PCS we cannot dial with the phy registers at this stage
292 * so we do not support extra feature like EEE.
294 if ((priv->hw->pcs == STMMAC_PCS_RGMII) ||
295 (priv->hw->pcs == STMMAC_PCS_TBI) ||
296 (priv->hw->pcs == STMMAC_PCS_RTBI))
299 /* MAC core supports the EEE feature. */
300 if (priv->dma_cap.eee) {
301 int tx_lpi_timer = priv->tx_lpi_timer;
303 /* Check if the PHY supports EEE */
304 if (phy_init_eee(priv->phydev, 1)) {
305 /* To manage at run-time if the EEE cannot be supported
306 * anymore (for example because the lp caps have been
308 * In that case the driver disable own timers.
310 spin_lock_irqsave(&priv->lock, flags);
311 if (priv->eee_active) {
312 pr_debug("stmmac: disable EEE\n");
313 del_timer_sync(&priv->eee_ctrl_timer);
314 priv->hw->mac->set_eee_timer(priv->hw, 0,
317 priv->eee_active = 0;
318 spin_unlock_irqrestore(&priv->lock, flags);
321 /* Activate the EEE and start timers */
322 spin_lock_irqsave(&priv->lock, flags);
323 if (!priv->eee_active) {
324 priv->eee_active = 1;
325 setup_timer(&priv->eee_ctrl_timer,
326 stmmac_eee_ctrl_timer,
327 (unsigned long)priv);
328 mod_timer(&priv->eee_ctrl_timer,
329 STMMAC_LPI_T(eee_timer));
331 priv->hw->mac->set_eee_timer(priv->hw,
332 STMMAC_DEFAULT_LIT_LS,
335 /* Set HW EEE according to the speed */
336 priv->hw->mac->set_eee_pls(priv->hw, priv->phydev->link);
339 spin_unlock_irqrestore(&priv->lock, flags);
341 pr_debug("stmmac: Energy-Efficient Ethernet initialized\n");
347 /* stmmac_get_tx_hwtstamp - get HW TX timestamps
348 * @priv: driver private structure
349 * @p : descriptor pointer
350 * @skb : the socket buffer
352 * This function will read timestamp from the descriptor & pass it to stack.
353 * and also perform some sanity checks.
355 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
356 struct dma_desc *p, struct sk_buff *skb)
358 struct skb_shared_hwtstamps shhwtstamp;
361 if (!priv->hwts_tx_en)
364 /* exit if skb doesn't support hw tstamp */
365 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
368 /* check tx tstamp status */
369 if (!priv->hw->desc->get_tx_timestamp_status(p)) {
370 /* get the valid tstamp */
371 ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
373 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
374 shhwtstamp.hwtstamp = ns_to_ktime(ns);
376 netdev_info(priv->dev, "get valid TX hw timestamp %llu\n", ns);
377 /* pass tstamp to stack */
378 skb_tstamp_tx(skb, &shhwtstamp);
384 /* stmmac_get_rx_hwtstamp - get HW RX timestamps
385 * @priv: driver private structure
386 * @p : descriptor pointer
387 * @np : next descriptor pointer
388 * @skb : the socket buffer
390 * This function will read received packet's timestamp from the descriptor
391 * and pass it to stack. It also perform some sanity checks.
393 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p,
394 struct dma_desc *np, struct sk_buff *skb)
396 struct skb_shared_hwtstamps *shhwtstamp = NULL;
399 if (!priv->hwts_rx_en)
402 /* Check if timestamp is available */
403 if (!priv->hw->desc->get_rx_timestamp_status(p, priv->adv_ts)) {
404 /* For GMAC4, the valid timestamp is from CTX next desc. */
405 if (priv->plat->has_gmac4)
406 ns = priv->hw->desc->get_timestamp(np, priv->adv_ts);
408 ns = priv->hw->desc->get_timestamp(p, priv->adv_ts);
410 netdev_info(priv->dev, "get valid RX hw timestamp %llu\n", ns);
411 shhwtstamp = skb_hwtstamps(skb);
412 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
413 shhwtstamp->hwtstamp = ns_to_ktime(ns);
415 netdev_err(priv->dev, "cannot get RX hw timestamp\n");
420 * stmmac_hwtstamp_ioctl - control hardware timestamping.
421 * @dev: device pointer.
422 * @ifr: An IOCTL specefic structure, that can contain a pointer to
423 * a proprietary structure used to pass information to the driver.
425 * This function configures the MAC to enable/disable both outgoing(TX)
426 * and incoming(RX) packets time stamping based on user input.
428 * 0 on success and an appropriate -ve integer on failure.
430 static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
432 struct stmmac_priv *priv = netdev_priv(dev);
433 struct hwtstamp_config config;
434 struct timespec64 now;
438 u32 ptp_over_ipv4_udp = 0;
439 u32 ptp_over_ipv6_udp = 0;
440 u32 ptp_over_ethernet = 0;
441 u32 snap_type_sel = 0;
442 u32 ts_master_en = 0;
447 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
448 netdev_alert(priv->dev, "No support for HW time stamping\n");
449 priv->hwts_tx_en = 0;
450 priv->hwts_rx_en = 0;
455 if (copy_from_user(&config, ifr->ifr_data,
456 sizeof(struct hwtstamp_config)))
459 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
460 __func__, config.flags, config.tx_type, config.rx_filter);
462 /* reserved for future extensions */
466 if (config.tx_type != HWTSTAMP_TX_OFF &&
467 config.tx_type != HWTSTAMP_TX_ON)
471 switch (config.rx_filter) {
472 case HWTSTAMP_FILTER_NONE:
473 /* time stamp no incoming packet at all */
474 config.rx_filter = HWTSTAMP_FILTER_NONE;
477 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
478 /* PTP v1, UDP, any kind of event packet */
479 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
480 /* take time stamp for all event messages */
481 if (priv->plat->has_gmac4)
482 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
484 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
486 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
487 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
490 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
491 /* PTP v1, UDP, Sync packet */
492 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
493 /* take time stamp for SYNC messages only */
494 ts_event_en = PTP_TCR_TSEVNTENA;
496 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
497 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
500 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
501 /* PTP v1, UDP, Delay_req packet */
502 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
503 /* take time stamp for Delay_Req messages only */
504 ts_master_en = PTP_TCR_TSMSTRENA;
505 ts_event_en = PTP_TCR_TSEVNTENA;
507 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
508 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
511 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
512 /* PTP v2, UDP, any kind of event packet */
513 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
514 ptp_v2 = PTP_TCR_TSVER2ENA;
515 /* take time stamp for all event messages */
516 if (priv->plat->has_gmac4)
517 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
519 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
521 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
522 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
525 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
526 /* PTP v2, UDP, Sync packet */
527 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
528 ptp_v2 = PTP_TCR_TSVER2ENA;
529 /* take time stamp for SYNC messages only */
530 ts_event_en = PTP_TCR_TSEVNTENA;
532 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
533 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
536 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
537 /* PTP v2, UDP, Delay_req packet */
538 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
539 ptp_v2 = PTP_TCR_TSVER2ENA;
540 /* take time stamp for Delay_Req messages only */
541 ts_master_en = PTP_TCR_TSMSTRENA;
542 ts_event_en = PTP_TCR_TSEVNTENA;
544 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
545 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
548 case HWTSTAMP_FILTER_PTP_V2_EVENT:
549 /* PTP v2/802.AS1 any layer, any kind of event packet */
550 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
551 ptp_v2 = PTP_TCR_TSVER2ENA;
552 /* take time stamp for all event messages */
553 if (priv->plat->has_gmac4)
554 snap_type_sel = PTP_GMAC4_TCR_SNAPTYPSEL_1;
556 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
558 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
559 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
560 ptp_over_ethernet = PTP_TCR_TSIPENA;
563 case HWTSTAMP_FILTER_PTP_V2_SYNC:
564 /* PTP v2/802.AS1, any layer, Sync packet */
565 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
566 ptp_v2 = PTP_TCR_TSVER2ENA;
567 /* take time stamp for SYNC messages only */
568 ts_event_en = PTP_TCR_TSEVNTENA;
570 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
571 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
572 ptp_over_ethernet = PTP_TCR_TSIPENA;
575 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
576 /* PTP v2/802.AS1, any layer, Delay_req packet */
577 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
578 ptp_v2 = PTP_TCR_TSVER2ENA;
579 /* take time stamp for Delay_Req messages only */
580 ts_master_en = PTP_TCR_TSMSTRENA;
581 ts_event_en = PTP_TCR_TSEVNTENA;
583 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
584 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
585 ptp_over_ethernet = PTP_TCR_TSIPENA;
588 case HWTSTAMP_FILTER_ALL:
589 /* time stamp any incoming packet */
590 config.rx_filter = HWTSTAMP_FILTER_ALL;
591 tstamp_all = PTP_TCR_TSENALL;
598 switch (config.rx_filter) {
599 case HWTSTAMP_FILTER_NONE:
600 config.rx_filter = HWTSTAMP_FILTER_NONE;
603 /* PTP v1, UDP, any kind of event packet */
604 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
608 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
609 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON;
611 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
612 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, 0);
614 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
615 tstamp_all | ptp_v2 | ptp_over_ethernet |
616 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
617 ts_master_en | snap_type_sel);
618 priv->hw->ptp->config_hw_tstamping(priv->ptpaddr, value);
620 /* program Sub Second Increment reg */
621 sec_inc = priv->hw->ptp->config_sub_second_increment(
622 priv->ptpaddr, priv->clk_ptp_rate,
623 priv->plat->has_gmac4);
624 temp = div_u64(1000000000ULL, sec_inc);
626 /* calculate default added value:
628 * addend = (2^32)/freq_div_ratio;
629 * where, freq_div_ratio = 1e9ns/sec_inc
631 temp = (u64)(temp << 32);
632 priv->default_addend = div_u64(temp, priv->clk_ptp_rate);
633 priv->hw->ptp->config_addend(priv->ptpaddr,
634 priv->default_addend);
636 /* initialize system time */
637 ktime_get_real_ts64(&now);
639 /* lower 32 bits of tv_sec are safe until y2106 */
640 priv->hw->ptp->init_systime(priv->ptpaddr, (u32)now.tv_sec,
644 return copy_to_user(ifr->ifr_data, &config,
645 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
649 * stmmac_init_ptp - init PTP
650 * @priv: driver private structure
651 * Description: this is to verify if the HW supports the PTPv1 or PTPv2.
652 * This is done by looking at the HW cap. register.
653 * This function also registers the ptp driver.
655 static int stmmac_init_ptp(struct stmmac_priv *priv)
657 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
660 /* Fall-back to main clock in case of no PTP ref is passed */
661 priv->clk_ptp_ref = devm_clk_get(priv->device, "clk_ptp_ref");
662 if (IS_ERR(priv->clk_ptp_ref)) {
663 priv->clk_ptp_rate = clk_get_rate(priv->stmmac_clk);
664 priv->clk_ptp_ref = NULL;
665 netdev_dbg(priv->dev, "PTP uses main clock\n");
667 clk_prepare_enable(priv->clk_ptp_ref);
668 priv->clk_ptp_rate = clk_get_rate(priv->clk_ptp_ref);
669 netdev_dbg(priv->dev, "PTP rate %d\n", priv->clk_ptp_rate);
673 /* Check if adv_ts can be enabled for dwmac 4.x core */
674 if (priv->plat->has_gmac4 && priv->dma_cap.atime_stamp)
676 /* Dwmac 3.x core with extend_desc can support adv_ts */
677 else if (priv->extend_desc && priv->dma_cap.atime_stamp)
680 if (priv->dma_cap.time_stamp)
681 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n");
684 netdev_info(priv->dev,
685 "IEEE 1588-2008 Advanced Timestamp supported\n");
687 priv->hw->ptp = &stmmac_ptp;
688 priv->hwts_tx_en = 0;
689 priv->hwts_rx_en = 0;
691 stmmac_ptp_register(priv);
696 static void stmmac_release_ptp(struct stmmac_priv *priv)
698 if (priv->clk_ptp_ref)
699 clk_disable_unprepare(priv->clk_ptp_ref);
700 stmmac_ptp_unregister(priv);
704 * stmmac_adjust_link - adjusts the link parameters
705 * @dev: net device structure
706 * Description: this is the helper called by the physical abstraction layer
707 * drivers to communicate the phy link status. According the speed and duplex
708 * this driver can invoke registered glue-logic as well.
709 * It also invoke the eee initialization because it could happen when switch
710 * on different networks (that are eee capable).
712 static void stmmac_adjust_link(struct net_device *dev)
714 struct stmmac_priv *priv = netdev_priv(dev);
715 struct phy_device *phydev = priv->phydev;
718 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
723 spin_lock_irqsave(&priv->lock, flags);
726 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
728 /* Now we make sure that we can be in full duplex mode.
729 * If not, we operate in half-duplex mode. */
730 if (phydev->duplex != priv->oldduplex) {
732 if (!(phydev->duplex))
733 ctrl &= ~priv->hw->link.duplex;
735 ctrl |= priv->hw->link.duplex;
736 priv->oldduplex = phydev->duplex;
738 /* Flow Control operation */
740 priv->hw->mac->flow_ctrl(priv->hw, phydev->duplex,
743 if (phydev->speed != priv->speed) {
745 switch (phydev->speed) {
747 if (likely((priv->plat->has_gmac) ||
748 (priv->plat->has_gmac4)))
749 ctrl &= ~priv->hw->link.port;
750 stmmac_hw_fix_mac_speed(priv);
754 if (likely((priv->plat->has_gmac) ||
755 (priv->plat->has_gmac4))) {
756 ctrl |= priv->hw->link.port;
757 if (phydev->speed == SPEED_100) {
758 ctrl |= priv->hw->link.speed;
760 ctrl &= ~(priv->hw->link.speed);
763 ctrl &= ~priv->hw->link.port;
765 stmmac_hw_fix_mac_speed(priv);
768 if (netif_msg_link(priv))
769 pr_warn("%s: Speed (%d) not 10/100\n",
770 dev->name, phydev->speed);
774 priv->speed = phydev->speed;
777 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
779 if (!priv->oldlink) {
783 } else if (priv->oldlink) {
787 priv->oldduplex = -1;
790 if (new_state && netif_msg_link(priv))
791 phy_print_status(phydev);
793 spin_unlock_irqrestore(&priv->lock, flags);
795 if (phydev->is_pseudo_fixed_link)
796 /* Stop PHY layer to call the hook to adjust the link in case
797 * of a switch is attached to the stmmac driver.
799 phydev->irq = PHY_IGNORE_INTERRUPT;
801 /* At this stage, init the EEE if supported.
802 * Never called in case of fixed_link.
804 priv->eee_enabled = stmmac_eee_init(priv);
808 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported
809 * @priv: driver private structure
810 * Description: this is to verify if the HW supports the PCS.
811 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
812 * configured for the TBI, RTBI, or SGMII PHY interface.
814 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
816 int interface = priv->plat->interface;
818 if (priv->dma_cap.pcs) {
819 if ((interface == PHY_INTERFACE_MODE_RGMII) ||
820 (interface == PHY_INTERFACE_MODE_RGMII_ID) ||
821 (interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
822 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
823 pr_debug("STMMAC: PCS RGMII support enable\n");
824 priv->hw->pcs = STMMAC_PCS_RGMII;
825 } else if (interface == PHY_INTERFACE_MODE_SGMII) {
826 pr_debug("STMMAC: PCS SGMII support enable\n");
827 priv->hw->pcs = STMMAC_PCS_SGMII;
833 * stmmac_init_phy - PHY initialization
834 * @dev: net device structure
835 * Description: it initializes the driver's PHY state, and attaches the PHY
840 static int stmmac_init_phy(struct net_device *dev)
842 struct stmmac_priv *priv = netdev_priv(dev);
843 struct phy_device *phydev;
844 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
845 char bus_id[MII_BUS_ID_SIZE];
846 int interface = priv->plat->interface;
847 int max_speed = priv->plat->max_speed;
850 priv->oldduplex = -1;
852 if (priv->plat->phy_node) {
853 phydev = of_phy_connect(dev, priv->plat->phy_node,
854 &stmmac_adjust_link, 0, interface);
856 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
859 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
860 priv->plat->phy_addr);
861 pr_debug("stmmac_init_phy: trying to attach to %s\n",
864 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link,
868 if (IS_ERR_OR_NULL(phydev)) {
869 pr_err("%s: Could not attach to PHY\n", dev->name);
873 return PTR_ERR(phydev);
876 /* Stop Advertising 1000BASE Capability if interface is not GMII */
877 if ((interface == PHY_INTERFACE_MODE_MII) ||
878 (interface == PHY_INTERFACE_MODE_RMII) ||
879 (max_speed < 1000 && max_speed > 0))
880 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
881 SUPPORTED_1000baseT_Full);
884 * Broken HW is sometimes missing the pull-up resistor on the
885 * MDIO line, which results in reads to non-existent devices returning
886 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
888 * Note: phydev->phy_id is the result of reading the UID PHY registers.
890 if (!priv->plat->phy_node && phydev->phy_id == 0) {
891 phy_disconnect(phydev);
895 /* stmmac_adjust_link will change this to PHY_IGNORE_INTERRUPT to avoid
896 * subsequent PHY polling, make sure we force a link transition if
897 * we have a UP/DOWN/UP transition
899 if (phydev->is_pseudo_fixed_link)
900 phydev->irq = PHY_POLL;
902 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
903 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
905 priv->phydev = phydev;
910 static void stmmac_display_rings(struct stmmac_priv *priv)
912 void *head_rx, *head_tx;
914 if (priv->extend_desc) {
915 head_rx = (void *)priv->dma_erx;
916 head_tx = (void *)priv->dma_etx;
918 head_rx = (void *)priv->dma_rx;
919 head_tx = (void *)priv->dma_tx;
922 /* Display Rx ring */
923 priv->hw->desc->display_ring(head_rx, DMA_RX_SIZE, true);
924 /* Display Tx ring */
925 priv->hw->desc->display_ring(head_tx, DMA_TX_SIZE, false);
928 static int stmmac_set_bfsize(int mtu, int bufsize)
932 if (mtu >= BUF_SIZE_4KiB)
934 else if (mtu >= BUF_SIZE_2KiB)
936 else if (mtu > DEFAULT_BUFSIZE)
939 ret = DEFAULT_BUFSIZE;
945 * stmmac_clear_descriptors - clear descriptors
946 * @priv: driver private structure
947 * Description: this function is called to clear the tx and rx descriptors
948 * in case of both basic and extended descriptors are used.
950 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
954 /* Clear the Rx/Tx descriptors */
955 for (i = 0; i < DMA_RX_SIZE; i++)
956 if (priv->extend_desc)
957 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
958 priv->use_riwt, priv->mode,
959 (i == DMA_RX_SIZE - 1), priv->dma_buf_sz);
961 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
962 priv->use_riwt, priv->mode,
963 (i == DMA_RX_SIZE - 1), priv->dma_buf_sz);
964 for (i = 0; i < DMA_TX_SIZE; i++)
965 if (priv->extend_desc)
966 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
968 (i == DMA_TX_SIZE - 1));
970 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
972 (i == DMA_TX_SIZE - 1));
976 * stmmac_init_rx_buffers - init the RX descriptor buffer.
977 * @priv: driver private structure
978 * @p: descriptor pointer
979 * @i: descriptor index
981 * Description: this function is called to allocate a receive buffer, perform
982 * the DMA mapping and init the descriptor.
984 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
989 skb = __netdev_alloc_skb_ip_align(priv->dev, priv->dma_buf_sz, flags);
991 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
994 priv->rx_skbuff[i] = skb;
995 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
998 if (dma_mapping_error(priv->device, priv->rx_skbuff_dma[i])) {
999 pr_err("%s: DMA mapping error\n", __func__);
1000 dev_kfree_skb_any(skb);
1004 if (priv->synopsys_id >= DWMAC_CORE_4_00)
1005 p->des0 = cpu_to_le32(priv->rx_skbuff_dma[i]);
1007 p->des2 = cpu_to_le32(priv->rx_skbuff_dma[i]);
1009 if ((priv->hw->mode->init_desc3) &&
1010 (priv->dma_buf_sz == BUF_SIZE_16KiB))
1011 priv->hw->mode->init_desc3(p);
1016 static void stmmac_free_rx_buffers(struct stmmac_priv *priv, int i)
1018 if (priv->rx_skbuff[i]) {
1019 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
1020 priv->dma_buf_sz, DMA_FROM_DEVICE);
1021 dev_kfree_skb_any(priv->rx_skbuff[i]);
1023 priv->rx_skbuff[i] = NULL;
1027 * init_dma_desc_rings - init the RX/TX descriptor rings
1028 * @dev: net device structure
1030 * Description: this function initializes the DMA RX/TX descriptors
1031 * and allocates the socket buffers. It suppors the chained and ring
1034 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags)
1037 struct stmmac_priv *priv = netdev_priv(dev);
1038 unsigned int bfsize = 0;
1041 if (priv->hw->mode->set_16kib_bfsize)
1042 bfsize = priv->hw->mode->set_16kib_bfsize(dev->mtu);
1044 if (bfsize < BUF_SIZE_16KiB)
1045 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
1047 priv->dma_buf_sz = bfsize;
1049 if (netif_msg_probe(priv)) {
1050 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1051 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
1053 /* RX INITIALIZATION */
1054 pr_debug("\tSKB addresses:\nskb\t\tskb data\tdma data\n");
1056 for (i = 0; i < DMA_RX_SIZE; i++) {
1058 if (priv->extend_desc)
1059 p = &((priv->dma_erx + i)->basic);
1061 p = priv->dma_rx + i;
1063 ret = stmmac_init_rx_buffers(priv, p, i, flags);
1065 goto err_init_rx_buffers;
1067 if (netif_msg_probe(priv))
1068 pr_debug("[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1069 priv->rx_skbuff[i]->data,
1070 (unsigned int)priv->rx_skbuff_dma[i]);
1073 priv->dirty_rx = (unsigned int)(i - DMA_RX_SIZE);
1076 /* Setup the chained descriptor addresses */
1077 if (priv->mode == STMMAC_CHAIN_MODE) {
1078 if (priv->extend_desc) {
1079 priv->hw->mode->init(priv->dma_erx, priv->dma_rx_phy,
1081 priv->hw->mode->init(priv->dma_etx, priv->dma_tx_phy,
1084 priv->hw->mode->init(priv->dma_rx, priv->dma_rx_phy,
1086 priv->hw->mode->init(priv->dma_tx, priv->dma_tx_phy,
1091 /* TX INITIALIZATION */
1092 for (i = 0; i < DMA_TX_SIZE; i++) {
1094 if (priv->extend_desc)
1095 p = &((priv->dma_etx + i)->basic);
1097 p = priv->dma_tx + i;
1099 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1108 priv->tx_skbuff_dma[i].buf = 0;
1109 priv->tx_skbuff_dma[i].map_as_page = false;
1110 priv->tx_skbuff_dma[i].len = 0;
1111 priv->tx_skbuff_dma[i].last_segment = false;
1112 priv->tx_skbuff[i] = NULL;
1117 netdev_reset_queue(priv->dev);
1119 stmmac_clear_descriptors(priv);
1121 if (netif_msg_hw(priv))
1122 stmmac_display_rings(priv);
1125 err_init_rx_buffers:
1127 stmmac_free_rx_buffers(priv, i);
1131 static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1135 for (i = 0; i < DMA_RX_SIZE; i++)
1136 stmmac_free_rx_buffers(priv, i);
1139 static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1143 for (i = 0; i < DMA_TX_SIZE; i++) {
1146 if (priv->extend_desc)
1147 p = &((priv->dma_etx + i)->basic);
1149 p = priv->dma_tx + i;
1151 if (priv->tx_skbuff_dma[i].buf) {
1152 if (priv->tx_skbuff_dma[i].map_as_page)
1153 dma_unmap_page(priv->device,
1154 priv->tx_skbuff_dma[i].buf,
1155 priv->tx_skbuff_dma[i].len,
1158 dma_unmap_single(priv->device,
1159 priv->tx_skbuff_dma[i].buf,
1160 priv->tx_skbuff_dma[i].len,
1164 if (priv->tx_skbuff[i] != NULL) {
1165 dev_kfree_skb_any(priv->tx_skbuff[i]);
1166 priv->tx_skbuff[i] = NULL;
1167 priv->tx_skbuff_dma[i].buf = 0;
1168 priv->tx_skbuff_dma[i].map_as_page = false;
1174 * alloc_dma_desc_resources - alloc TX/RX resources.
1175 * @priv: private structure
1176 * Description: according to which descriptor can be used (extend or basic)
1177 * this function allocates the resources for TX and RX paths. In case of
1178 * reception, for example, it pre-allocated the RX socket buffer in order to
1179 * allow zero-copy mechanism.
1181 static int alloc_dma_desc_resources(struct stmmac_priv *priv)
1185 priv->rx_skbuff_dma = kmalloc_array(DMA_RX_SIZE, sizeof(dma_addr_t),
1187 if (!priv->rx_skbuff_dma)
1190 priv->rx_skbuff = kmalloc_array(DMA_RX_SIZE, sizeof(struct sk_buff *),
1192 if (!priv->rx_skbuff)
1195 priv->tx_skbuff_dma = kmalloc_array(DMA_TX_SIZE,
1196 sizeof(*priv->tx_skbuff_dma),
1198 if (!priv->tx_skbuff_dma)
1199 goto err_tx_skbuff_dma;
1201 priv->tx_skbuff = kmalloc_array(DMA_TX_SIZE, sizeof(struct sk_buff *),
1203 if (!priv->tx_skbuff)
1206 if (priv->extend_desc) {
1207 priv->dma_erx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
1215 priv->dma_etx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
1220 if (!priv->dma_etx) {
1221 dma_free_coherent(priv->device, DMA_RX_SIZE *
1222 sizeof(struct dma_extended_desc),
1223 priv->dma_erx, priv->dma_rx_phy);
1227 priv->dma_rx = dma_zalloc_coherent(priv->device, DMA_RX_SIZE *
1228 sizeof(struct dma_desc),
1234 priv->dma_tx = dma_zalloc_coherent(priv->device, DMA_TX_SIZE *
1235 sizeof(struct dma_desc),
1238 if (!priv->dma_tx) {
1239 dma_free_coherent(priv->device, DMA_RX_SIZE *
1240 sizeof(struct dma_desc),
1241 priv->dma_rx, priv->dma_rx_phy);
1249 kfree(priv->tx_skbuff);
1251 kfree(priv->tx_skbuff_dma);
1253 kfree(priv->rx_skbuff);
1255 kfree(priv->rx_skbuff_dma);
1259 static void free_dma_desc_resources(struct stmmac_priv *priv)
1261 /* Release the DMA TX/RX socket buffers */
1262 dma_free_rx_skbufs(priv);
1263 dma_free_tx_skbufs(priv);
1265 /* Free DMA regions of consistent memory previously allocated */
1266 if (!priv->extend_desc) {
1267 dma_free_coherent(priv->device,
1268 DMA_TX_SIZE * sizeof(struct dma_desc),
1269 priv->dma_tx, priv->dma_tx_phy);
1270 dma_free_coherent(priv->device,
1271 DMA_RX_SIZE * sizeof(struct dma_desc),
1272 priv->dma_rx, priv->dma_rx_phy);
1274 dma_free_coherent(priv->device, DMA_TX_SIZE *
1275 sizeof(struct dma_extended_desc),
1276 priv->dma_etx, priv->dma_tx_phy);
1277 dma_free_coherent(priv->device, DMA_RX_SIZE *
1278 sizeof(struct dma_extended_desc),
1279 priv->dma_erx, priv->dma_rx_phy);
1281 kfree(priv->rx_skbuff_dma);
1282 kfree(priv->rx_skbuff);
1283 kfree(priv->tx_skbuff_dma);
1284 kfree(priv->tx_skbuff);
1288 * stmmac_dma_operation_mode - HW DMA operation mode
1289 * @priv: driver private structure
1290 * Description: it is used for configuring the DMA operation mode register in
1291 * order to program the tx/rx DMA thresholds or Store-And-Forward mode.
1293 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1295 int rxfifosz = priv->plat->rx_fifo_size;
1297 if (priv->plat->force_thresh_dma_mode)
1298 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc, rxfifosz);
1299 else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) {
1301 * In case of GMAC, SF mode can be enabled
1302 * to perform the TX COE in HW. This depends on:
1303 * 1) TX COE if actually supported
1304 * 2) There is no bugged Jumbo frame support
1305 * that needs to not insert csum in the TDES.
1307 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE,
1309 priv->xstats.threshold = SF_DMA_MODE;
1311 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE,
1316 * stmmac_tx_clean - to manage the transmission completion
1317 * @priv: driver private structure
1318 * Description: it reclaims the transmit resources after transmission completes.
1320 static void stmmac_tx_clean(struct stmmac_priv *priv)
1322 unsigned int bytes_compl = 0, pkts_compl = 0;
1323 unsigned int entry = priv->dirty_tx;
1325 spin_lock(&priv->tx_lock);
1327 priv->xstats.tx_clean++;
1329 while (entry != priv->cur_tx) {
1330 struct sk_buff *skb = priv->tx_skbuff[entry];
1334 if (priv->extend_desc)
1335 p = (struct dma_desc *)(priv->dma_etx + entry);
1337 p = priv->dma_tx + entry;
1339 status = priv->hw->desc->tx_status(&priv->dev->stats,
1342 /* Check if the descriptor is owned by the DMA */
1343 if (unlikely(status & tx_dma_own))
1346 /* Make sure descriptor fields are read after reading
1351 /* Just consider the last segment and ...*/
1352 if (likely(!(status & tx_not_ls))) {
1353 /* ... verify the status error condition */
1354 if (unlikely(status & tx_err)) {
1355 priv->dev->stats.tx_errors++;
1357 priv->dev->stats.tx_packets++;
1358 priv->xstats.tx_pkt_n++;
1360 stmmac_get_tx_hwtstamp(priv, p, skb);
1363 if (likely(priv->tx_skbuff_dma[entry].buf)) {
1364 if (priv->tx_skbuff_dma[entry].map_as_page)
1365 dma_unmap_page(priv->device,
1366 priv->tx_skbuff_dma[entry].buf,
1367 priv->tx_skbuff_dma[entry].len,
1370 dma_unmap_single(priv->device,
1371 priv->tx_skbuff_dma[entry].buf,
1372 priv->tx_skbuff_dma[entry].len,
1374 priv->tx_skbuff_dma[entry].buf = 0;
1375 priv->tx_skbuff_dma[entry].len = 0;
1376 priv->tx_skbuff_dma[entry].map_as_page = false;
1379 if (priv->hw->mode->clean_desc3)
1380 priv->hw->mode->clean_desc3(priv, p);
1382 priv->tx_skbuff_dma[entry].last_segment = false;
1383 priv->tx_skbuff_dma[entry].is_jumbo = false;
1385 if (likely(skb != NULL)) {
1387 bytes_compl += skb->len;
1388 dev_consume_skb_any(skb);
1389 priv->tx_skbuff[entry] = NULL;
1392 priv->hw->desc->release_tx_desc(p, priv->mode);
1394 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
1396 priv->dirty_tx = entry;
1398 netdev_completed_queue(priv->dev, pkts_compl, bytes_compl);
1400 if (unlikely(netif_queue_stopped(priv->dev) &&
1401 stmmac_tx_avail(priv) > STMMAC_TX_THRESH)) {
1402 netif_tx_lock(priv->dev);
1403 if (netif_queue_stopped(priv->dev) &&
1404 stmmac_tx_avail(priv) > STMMAC_TX_THRESH) {
1405 if (netif_msg_tx_done(priv))
1406 pr_debug("%s: restart transmit\n", __func__);
1407 netif_wake_queue(priv->dev);
1409 netif_tx_unlock(priv->dev);
1412 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1413 stmmac_enable_eee_mode(priv);
1414 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
1416 spin_unlock(&priv->tx_lock);
1419 static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
1421 priv->hw->dma->enable_dma_irq(priv->ioaddr);
1424 static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
1426 priv->hw->dma->disable_dma_irq(priv->ioaddr);
1430 * stmmac_tx_err - to manage the tx error
1431 * @priv: driver private structure
1432 * Description: it cleans the descriptors and restarts the transmission
1433 * in case of transmission errors.
1435 static void stmmac_tx_err(struct stmmac_priv *priv)
1438 netif_stop_queue(priv->dev);
1440 priv->hw->dma->stop_tx(priv->ioaddr);
1441 dma_free_tx_skbufs(priv);
1442 for (i = 0; i < DMA_TX_SIZE; i++)
1443 if (priv->extend_desc)
1444 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1446 (i == DMA_TX_SIZE - 1));
1448 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1450 (i == DMA_TX_SIZE - 1));
1453 netdev_reset_queue(priv->dev);
1454 priv->hw->dma->start_tx(priv->ioaddr);
1456 priv->dev->stats.tx_errors++;
1457 netif_wake_queue(priv->dev);
1461 * stmmac_dma_interrupt - DMA ISR
1462 * @priv: driver private structure
1463 * Description: this is the DMA ISR. It is called by the main ISR.
1464 * It calls the dwmac dma routine and schedule poll method in case of some
1467 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
1470 int rxfifosz = priv->plat->rx_fifo_size;
1472 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
1473 if (likely((status & handle_rx)) || (status & handle_tx)) {
1474 if (likely(napi_schedule_prep(&priv->napi))) {
1475 stmmac_disable_dma_irq(priv);
1476 __napi_schedule(&priv->napi);
1479 if (unlikely(status & tx_hard_error_bump_tc)) {
1480 /* Try to bump up the dma threshold on this failure */
1481 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) &&
1484 if (priv->plat->force_thresh_dma_mode)
1485 priv->hw->dma->dma_mode(priv->ioaddr, tc, tc,
1488 priv->hw->dma->dma_mode(priv->ioaddr, tc,
1489 SF_DMA_MODE, rxfifosz);
1490 priv->xstats.threshold = tc;
1492 } else if (unlikely(status == tx_hard_error))
1493 stmmac_tx_err(priv);
1497 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1498 * @priv: driver private structure
1499 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1501 static void stmmac_mmc_setup(struct stmmac_priv *priv)
1503 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
1504 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
1506 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1507 priv->ptpaddr = priv->ioaddr + PTP_GMAC4_OFFSET;
1508 priv->mmcaddr = priv->ioaddr + MMC_GMAC4_OFFSET;
1510 priv->ptpaddr = priv->ioaddr + PTP_GMAC3_X_OFFSET;
1511 priv->mmcaddr = priv->ioaddr + MMC_GMAC3_X_OFFSET;
1514 dwmac_mmc_intr_all_mask(priv->mmcaddr);
1516 if (priv->dma_cap.rmon) {
1517 dwmac_mmc_ctrl(priv->mmcaddr, mode);
1518 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1520 pr_info(" No MAC Management Counters available\n");
1524 * stmmac_selec_desc_mode - to select among: normal/alternate/extend descriptors
1525 * @priv: driver private structure
1526 * Description: select the Enhanced/Alternate or Normal descriptors.
1527 * In case of Enhanced/Alternate, it checks if the extended descriptors are
1528 * supported by the HW capability register.
1530 static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1532 if (priv->plat->enh_desc) {
1533 pr_info(" Enhanced/Alternate descriptors\n");
1535 /* GMAC older than 3.50 has no extended descriptors */
1536 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1537 pr_info("\tEnabled extended descriptors\n");
1538 priv->extend_desc = 1;
1540 pr_warn("Extended descriptors not supported\n");
1542 priv->hw->desc = &enh_desc_ops;
1544 pr_info(" Normal descriptors\n");
1545 priv->hw->desc = &ndesc_ops;
1550 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register.
1551 * @priv: driver private structure
1553 * new GMAC chip generations have a new register to indicate the
1554 * presence of the optional feature/functions.
1555 * This can be also used to override the value passed through the
1556 * platform and necessary for old MAC10/100 and GMAC chips.
1558 static int stmmac_get_hw_features(struct stmmac_priv *priv)
1562 if (priv->hw->dma->get_hw_feature) {
1563 priv->hw->dma->get_hw_feature(priv->ioaddr,
1572 * stmmac_check_ether_addr - check if the MAC addr is valid
1573 * @priv: driver private structure
1575 * it is to verify if the MAC address is valid, in case of failures it
1576 * generates a random MAC address
1578 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1580 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
1581 priv->hw->mac->get_umac_addr(priv->hw,
1582 priv->dev->dev_addr, 0);
1583 if (!is_valid_ether_addr(priv->dev->dev_addr))
1584 eth_hw_addr_random(priv->dev);
1585 pr_info("%s: device MAC address %pM\n", priv->dev->name,
1586 priv->dev->dev_addr);
1591 * stmmac_init_dma_engine - DMA init.
1592 * @priv: driver private structure
1594 * It inits the DMA invoking the specific MAC/GMAC callback.
1595 * Some DMA parameters can be passed from the platform;
1596 * in case of these are not passed a default is kept for the MAC or GMAC.
1598 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1600 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, aal = 0;
1601 int mixed_burst = 0;
1605 if (priv->plat->dma_cfg) {
1606 pbl = priv->plat->dma_cfg->pbl;
1607 fixed_burst = priv->plat->dma_cfg->fixed_burst;
1608 mixed_burst = priv->plat->dma_cfg->mixed_burst;
1609 aal = priv->plat->dma_cfg->aal;
1612 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1615 ret = priv->hw->dma->reset(priv->ioaddr);
1617 dev_err(priv->device, "Failed to reset the dma\n");
1621 priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
1622 aal, priv->dma_tx_phy, priv->dma_rx_phy, atds);
1624 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
1625 priv->rx_tail_addr = priv->dma_rx_phy +
1626 (DMA_RX_SIZE * sizeof(struct dma_desc));
1627 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr, priv->rx_tail_addr,
1630 priv->tx_tail_addr = priv->dma_tx_phy +
1631 (DMA_TX_SIZE * sizeof(struct dma_desc));
1632 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
1636 if (priv->plat->axi && priv->hw->dma->axi)
1637 priv->hw->dma->axi(priv->ioaddr, priv->plat->axi);
1643 * stmmac_tx_timer - mitigation sw timer for tx.
1644 * @data: data pointer
1646 * This is the timer handler to directly invoke the stmmac_tx_clean.
1648 static void stmmac_tx_timer(unsigned long data)
1650 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1652 stmmac_tx_clean(priv);
1656 * stmmac_init_tx_coalesce - init tx mitigation options.
1657 * @priv: driver private structure
1659 * This inits the transmit coalesce parameters: i.e. timer rate,
1660 * timer handler and default threshold used for enabling the
1661 * interrupt on completion bit.
1663 static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1665 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1666 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1667 init_timer(&priv->txtimer);
1668 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1669 priv->txtimer.data = (unsigned long)priv;
1670 priv->txtimer.function = stmmac_tx_timer;
1671 add_timer(&priv->txtimer);
1675 * stmmac_hw_setup - setup mac in a usable state.
1676 * @dev : pointer to the device structure.
1678 * this is the main function to setup the HW in a usable state because the
1679 * dma engine is reset, the core registers are configured (e.g. AXI,
1680 * Checksum features, timers). The DMA is ready to start receiving and
1683 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1686 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp)
1688 struct stmmac_priv *priv = netdev_priv(dev);
1691 /* DMA initialization and SW reset */
1692 ret = stmmac_init_dma_engine(priv);
1694 pr_err("%s: DMA engine initialization failed\n", __func__);
1698 /* Copy the MAC addr into the HW */
1699 priv->hw->mac->set_umac_addr(priv->hw, dev->dev_addr, 0);
1701 /* If required, perform hw setup of the bus. */
1702 if (priv->plat->bus_setup)
1703 priv->plat->bus_setup(priv->ioaddr);
1705 /* PS and related bits will be programmed according to the speed */
1706 if (priv->hw->pcs) {
1707 int speed = priv->plat->mac_port_sel_speed;
1709 if ((speed == SPEED_10) || (speed == SPEED_100) ||
1710 (speed == SPEED_1000)) {
1711 priv->hw->ps = speed;
1713 dev_warn(priv->device, "invalid port speed\n");
1718 /* Initialize the MAC Core */
1719 priv->hw->mac->core_init(priv->hw, dev->mtu);
1721 ret = priv->hw->mac->rx_ipc(priv->hw);
1723 pr_warn(" RX IPC Checksum Offload disabled\n");
1724 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
1725 priv->hw->rx_csum = 0;
1728 /* Enable the MAC Rx/Tx */
1729 if (priv->synopsys_id >= DWMAC_CORE_4_00)
1730 stmmac_dwmac4_set_mac(priv->ioaddr, true);
1732 stmmac_set_mac(priv->ioaddr, true);
1734 /* Set the HW DMA mode and the COE */
1735 stmmac_dma_operation_mode(priv);
1737 stmmac_mmc_setup(priv);
1740 ret = stmmac_init_ptp(priv);
1742 netdev_warn(priv->dev, "fail to init PTP.\n");
1745 #ifdef CONFIG_DEBUG_FS
1746 ret = stmmac_init_fs(dev);
1748 pr_warn("%s: failed debugFS registration\n", __func__);
1750 /* Dump DMA/MAC registers */
1751 if (netif_msg_hw(priv)) {
1752 priv->hw->mac->dump_regs(priv->hw);
1753 priv->hw->dma->dump_regs(priv->ioaddr);
1755 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1757 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1758 priv->rx_riwt = MAX_DMA_RIWT;
1759 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1762 if (priv->hw->pcs && priv->hw->mac->pcs_ctrl_ane)
1763 priv->hw->mac->pcs_ctrl_ane(priv->hw, 1, priv->hw->ps, 0);
1765 /* set TX ring length */
1766 if (priv->hw->dma->set_tx_ring_len)
1767 priv->hw->dma->set_tx_ring_len(priv->ioaddr,
1769 /* set RX ring length */
1770 if (priv->hw->dma->set_rx_ring_len)
1771 priv->hw->dma->set_rx_ring_len(priv->ioaddr,
1775 priv->hw->dma->enable_tso(priv->ioaddr, 1, STMMAC_CHAN0);
1777 /* Start the ball rolling... */
1778 pr_debug("%s: DMA RX/TX processes started...\n", dev->name);
1779 priv->hw->dma->start_tx(priv->ioaddr);
1780 priv->hw->dma->start_rx(priv->ioaddr);
1786 * stmmac_open - open entry point of the driver
1787 * @dev : pointer to the device structure.
1789 * This function is the open entry point of the driver.
1791 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1794 static int stmmac_open(struct net_device *dev)
1796 struct stmmac_priv *priv = netdev_priv(dev);
1799 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
1800 priv->hw->pcs != STMMAC_PCS_TBI &&
1801 priv->hw->pcs != STMMAC_PCS_RTBI) {
1802 ret = stmmac_init_phy(dev);
1804 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1810 /* Extra statistics */
1811 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1812 priv->xstats.threshold = tc;
1814 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
1815 priv->rx_copybreak = STMMAC_RX_COPYBREAK;
1818 ret = alloc_dma_desc_resources(priv);
1820 pr_err("%s: DMA descriptors allocation failed\n", __func__);
1821 goto dma_desc_error;
1824 ret = init_dma_desc_rings(dev, GFP_KERNEL);
1826 pr_err("%s: DMA descriptors initialization failed\n", __func__);
1830 ret = stmmac_hw_setup(dev, true);
1832 pr_err("%s: Hw setup failed\n", __func__);
1836 stmmac_init_tx_coalesce(priv);
1839 phy_start(priv->phydev);
1841 /* Request the IRQ lines */
1842 ret = request_irq(dev->irq, stmmac_interrupt,
1843 IRQF_SHARED, dev->name, dev);
1844 if (unlikely(ret < 0)) {
1845 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1846 __func__, dev->irq, ret);
1850 /* Request the Wake IRQ in case of another line is used for WoL */
1851 if (priv->wol_irq != dev->irq) {
1852 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1853 IRQF_SHARED, dev->name, dev);
1854 if (unlikely(ret < 0)) {
1855 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1856 __func__, priv->wol_irq, ret);
1861 /* Request the IRQ lines */
1862 if (priv->lpi_irq > 0) {
1863 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1865 if (unlikely(ret < 0)) {
1866 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1867 __func__, priv->lpi_irq, ret);
1872 napi_enable(&priv->napi);
1873 netif_start_queue(dev);
1878 if (priv->wol_irq != dev->irq)
1879 free_irq(priv->wol_irq, dev);
1881 free_irq(dev->irq, dev);
1884 free_dma_desc_resources(priv);
1887 phy_disconnect(priv->phydev);
1893 * stmmac_release - close entry point of the driver
1894 * @dev : device pointer.
1896 * This is the stop entry point of the driver.
1898 static int stmmac_release(struct net_device *dev)
1900 struct stmmac_priv *priv = netdev_priv(dev);
1902 if (priv->eee_enabled)
1903 del_timer_sync(&priv->eee_ctrl_timer);
1905 /* Stop and disconnect the PHY */
1907 phy_stop(priv->phydev);
1908 phy_disconnect(priv->phydev);
1909 priv->phydev = NULL;
1912 netif_stop_queue(dev);
1914 napi_disable(&priv->napi);
1916 del_timer_sync(&priv->txtimer);
1918 /* Free the IRQ lines */
1919 free_irq(dev->irq, dev);
1920 if (priv->wol_irq != dev->irq)
1921 free_irq(priv->wol_irq, dev);
1922 if (priv->lpi_irq > 0)
1923 free_irq(priv->lpi_irq, dev);
1925 /* Stop TX/RX DMA and clear the descriptors */
1926 priv->hw->dma->stop_tx(priv->ioaddr);
1927 priv->hw->dma->stop_rx(priv->ioaddr);
1929 /* Release and free the Rx/Tx resources */
1930 free_dma_desc_resources(priv);
1932 /* Disable the MAC Rx/Tx */
1933 stmmac_set_mac(priv->ioaddr, false);
1935 netif_carrier_off(dev);
1937 #ifdef CONFIG_DEBUG_FS
1938 stmmac_exit_fs(dev);
1941 stmmac_release_ptp(priv);
1947 * stmmac_tso_allocator - close entry point of the driver
1948 * @priv: driver private structure
1949 * @des: buffer start address
1950 * @total_len: total length to fill in descriptors
1951 * @last_segmant: condition for the last descriptor
1953 * This function fills descriptor and request new descriptors according to
1954 * buffer length to fill
1956 static void stmmac_tso_allocator(struct stmmac_priv *priv, unsigned int des,
1957 int total_len, bool last_segment)
1959 struct dma_desc *desc;
1963 tmp_len = total_len;
1965 while (tmp_len > 0) {
1966 priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
1967 desc = priv->dma_tx + priv->cur_tx;
1969 desc->des0 = cpu_to_le32(des + (total_len - tmp_len));
1970 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ?
1971 TSO_MAX_BUFF_SIZE : tmp_len;
1973 priv->hw->desc->prepare_tso_tx_desc(desc, 0, buff_size,
1975 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE),
1978 tmp_len -= TSO_MAX_BUFF_SIZE;
1983 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO)
1984 * @skb : the socket buffer
1985 * @dev : device pointer
1986 * Description: this is the transmit function that is called on TSO frames
1987 * (support available on GMAC4 and newer chips).
1988 * Diagram below show the ring programming in case of TSO frames:
1992 * | DES0 |---> buffer1 = L2/L3/L4 header
1993 * | DES1 |---> TCP Payload (can continue on next descr...)
1994 * | DES2 |---> buffer 1 and 2 len
1995 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0]
2001 * | DES0 | --| Split TCP Payload on Buffers 1 and 2
2003 * | DES2 | --> buffer 1 and 2 len
2007 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field.
2009 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev)
2012 int tmp_pay_len = 0;
2013 struct stmmac_priv *priv = netdev_priv(dev);
2014 int nfrags = skb_shinfo(skb)->nr_frags;
2015 unsigned int first_entry, des;
2016 struct dma_desc *desc, *first, *mss_desc = NULL;
2020 spin_lock(&priv->tx_lock);
2022 /* Compute header lengths */
2023 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2025 /* Desc availability based on threshold should be enough safe */
2026 if (unlikely(stmmac_tx_avail(priv) <
2027 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) {
2028 if (!netif_queue_stopped(dev)) {
2029 netif_stop_queue(dev);
2030 /* This is a hard error, log it. */
2031 pr_err("%s: Tx Ring full when queue awake\n", __func__);
2033 spin_unlock(&priv->tx_lock);
2034 return NETDEV_TX_BUSY;
2037 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */
2039 mss = skb_shinfo(skb)->gso_size;
2041 /* set new MSS value if needed */
2042 if (mss != priv->mss) {
2043 mss_desc = priv->dma_tx + priv->cur_tx;
2044 priv->hw->desc->set_mss(mss_desc, mss);
2046 priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
2049 if (netif_msg_tx_queued(priv)) {
2050 pr_info("%s: tcphdrlen %d, hdr_len %d, pay_len %d, mss %d\n",
2051 __func__, tcp_hdrlen(skb), proto_hdr_len, pay_len, mss);
2052 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len,
2056 first_entry = priv->cur_tx;
2058 desc = priv->dma_tx + first_entry;
2061 /* first descriptor: fill Headers on Buf1 */
2062 des = dma_map_single(priv->device, skb->data, skb_headlen(skb),
2064 if (dma_mapping_error(priv->device, des))
2067 priv->tx_skbuff_dma[first_entry].buf = des;
2068 priv->tx_skbuff_dma[first_entry].len = skb_headlen(skb);
2069 priv->tx_skbuff[first_entry] = skb;
2071 first->des0 = cpu_to_le32(des);
2073 /* Fill start of payload in buff2 of first descriptor */
2075 first->des1 = cpu_to_le32(des + proto_hdr_len);
2077 /* If needed take extra descriptors to fill the remaining payload */
2078 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE;
2080 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0));
2082 /* Prepare fragments */
2083 for (i = 0; i < nfrags; i++) {
2084 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2086 des = skb_frag_dma_map(priv->device, frag, 0,
2087 skb_frag_size(frag),
2090 stmmac_tso_allocator(priv, des, skb_frag_size(frag),
2093 priv->tx_skbuff_dma[priv->cur_tx].buf = des;
2094 priv->tx_skbuff_dma[priv->cur_tx].len = skb_frag_size(frag);
2095 priv->tx_skbuff[priv->cur_tx] = NULL;
2096 priv->tx_skbuff_dma[priv->cur_tx].map_as_page = true;
2099 priv->tx_skbuff_dma[priv->cur_tx].last_segment = true;
2101 priv->cur_tx = STMMAC_GET_ENTRY(priv->cur_tx, DMA_TX_SIZE);
2103 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
2104 if (netif_msg_hw(priv))
2105 pr_debug("%s: stop transmitted packets\n", __func__);
2106 netif_stop_queue(dev);
2109 dev->stats.tx_bytes += skb->len;
2110 priv->xstats.tx_tso_frames++;
2111 priv->xstats.tx_tso_nfrags += nfrags;
2113 /* Manage tx mitigation */
2114 priv->tx_count_frames += nfrags + 1;
2115 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
2116 mod_timer(&priv->txtimer,
2117 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2119 priv->tx_count_frames = 0;
2120 priv->hw->desc->set_tx_ic(desc);
2121 priv->xstats.tx_set_ic_bit++;
2124 if (!priv->hwts_tx_en)
2125 skb_tx_timestamp(skb);
2127 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2128 priv->hwts_tx_en)) {
2129 /* declare that device is doing timestamping */
2130 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2131 priv->hw->desc->enable_tx_timestamp(first);
2134 /* Complete the first descriptor before granting the DMA */
2135 priv->hw->desc->prepare_tso_tx_desc(first, 1,
2138 1, priv->tx_skbuff_dma[first_entry].last_segment,
2139 tcp_hdrlen(skb) / 4, (skb->len - proto_hdr_len));
2141 /* If context desc is used to change MSS */
2143 /* Make sure that first descriptor has been completely
2144 * written, including its own bit. This is because MSS is
2145 * actually before first descriptor, so we need to make
2146 * sure that MSS's own bit is the last thing written.
2149 priv->hw->desc->set_tx_owner(mss_desc);
2152 /* The own bit must be the latest setting done when prepare the
2153 * descriptor and then barrier is needed to make sure that
2154 * all is coherent before granting the DMA engine.
2158 if (netif_msg_pktdata(priv)) {
2159 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n",
2160 __func__, priv->cur_tx, priv->dirty_tx, first_entry,
2161 priv->cur_tx, first, nfrags);
2163 priv->hw->desc->display_ring((void *)priv->dma_tx, DMA_TX_SIZE,
2166 pr_info(">>> frame to be transmitted: ");
2167 print_pkt(skb->data, skb_headlen(skb));
2170 netdev_sent_queue(dev, skb->len);
2172 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
2175 spin_unlock(&priv->tx_lock);
2176 return NETDEV_TX_OK;
2179 spin_unlock(&priv->tx_lock);
2180 dev_err(priv->device, "Tx dma map failed\n");
2182 priv->dev->stats.tx_dropped++;
2183 return NETDEV_TX_OK;
2187 * stmmac_xmit - Tx entry point of the driver
2188 * @skb : the socket buffer
2189 * @dev : device pointer
2190 * Description : this is the tx entry point of the driver.
2191 * It programs the chain or the ring and supports oversized frames
2194 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
2196 struct stmmac_priv *priv = netdev_priv(dev);
2197 unsigned int nopaged_len = skb_headlen(skb);
2198 int i, csum_insertion = 0, is_jumbo = 0;
2199 int nfrags = skb_shinfo(skb)->nr_frags;
2201 unsigned int first_entry;
2202 struct dma_desc *desc, *first;
2203 unsigned int enh_desc;
2206 /* Manage oversized TCP frames for GMAC4 device */
2207 if (skb_is_gso(skb) && priv->tso) {
2208 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
2209 return stmmac_tso_xmit(skb, dev);
2212 spin_lock(&priv->tx_lock);
2214 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
2215 spin_unlock(&priv->tx_lock);
2216 if (!netif_queue_stopped(dev)) {
2217 netif_stop_queue(dev);
2218 /* This is a hard error, log it. */
2219 pr_err("%s: Tx Ring full when queue awake\n", __func__);
2221 return NETDEV_TX_BUSY;
2224 if (priv->tx_path_in_lpi_mode)
2225 stmmac_disable_eee_mode(priv);
2227 entry = priv->cur_tx;
2228 first_entry = entry;
2230 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
2232 if (likely(priv->extend_desc))
2233 desc = (struct dma_desc *)(priv->dma_etx + entry);
2235 desc = priv->dma_tx + entry;
2239 priv->tx_skbuff[first_entry] = skb;
2241 enh_desc = priv->plat->enh_desc;
2242 /* To program the descriptors according to the size of the frame */
2244 is_jumbo = priv->hw->mode->is_jumbo_frm(skb->len, enh_desc);
2246 if (unlikely(is_jumbo) && likely(priv->synopsys_id <
2248 entry = priv->hw->mode->jumbo_frm(priv, skb, csum_insertion);
2249 if (unlikely(entry < 0))
2253 for (i = 0; i < nfrags; i++) {
2254 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2255 int len = skb_frag_size(frag);
2256 bool last_segment = (i == (nfrags - 1));
2258 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2260 if (likely(priv->extend_desc))
2261 desc = (struct dma_desc *)(priv->dma_etx + entry);
2263 desc = priv->dma_tx + entry;
2265 des = skb_frag_dma_map(priv->device, frag, 0, len,
2267 if (dma_mapping_error(priv->device, des))
2268 goto dma_map_err; /* should reuse desc w/o issues */
2270 priv->tx_skbuff[entry] = NULL;
2272 priv->tx_skbuff_dma[entry].buf = des;
2273 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
2274 desc->des0 = cpu_to_le32(des);
2276 desc->des2 = cpu_to_le32(des);
2278 priv->tx_skbuff_dma[entry].map_as_page = true;
2279 priv->tx_skbuff_dma[entry].len = len;
2280 priv->tx_skbuff_dma[entry].last_segment = last_segment;
2282 /* Prepare the descriptor and set the own bit too */
2283 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
2284 priv->mode, 1, last_segment);
2287 entry = STMMAC_GET_ENTRY(entry, DMA_TX_SIZE);
2289 priv->cur_tx = entry;
2291 if (netif_msg_pktdata(priv)) {
2294 pr_debug("%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d",
2295 __func__, priv->cur_tx, priv->dirty_tx, first_entry,
2296 entry, first, nfrags);
2298 if (priv->extend_desc)
2299 tx_head = (void *)priv->dma_etx;
2301 tx_head = (void *)priv->dma_tx;
2303 priv->hw->desc->display_ring(tx_head, DMA_TX_SIZE, false);
2305 pr_debug(">>> frame to be transmitted: ");
2306 print_pkt(skb->data, skb->len);
2309 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
2310 if (netif_msg_hw(priv))
2311 pr_debug("%s: stop transmitted packets\n", __func__);
2312 netif_stop_queue(dev);
2315 dev->stats.tx_bytes += skb->len;
2317 /* According to the coalesce parameter the IC bit for the latest
2318 * segment is reset and the timer re-started to clean the tx status.
2319 * This approach takes care about the fragments: desc is the first
2320 * element in case of no SG.
2322 priv->tx_count_frames += nfrags + 1;
2323 if (likely(priv->tx_coal_frames > priv->tx_count_frames)) {
2324 mod_timer(&priv->txtimer,
2325 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2327 priv->tx_count_frames = 0;
2328 priv->hw->desc->set_tx_ic(desc);
2329 priv->xstats.tx_set_ic_bit++;
2332 if (!priv->hwts_tx_en)
2333 skb_tx_timestamp(skb);
2335 /* Ready to fill the first descriptor and set the OWN bit w/o any
2336 * problems because all the descriptors are actually ready to be
2337 * passed to the DMA engine.
2339 if (likely(!is_jumbo)) {
2340 bool last_segment = (nfrags == 0);
2342 des = dma_map_single(priv->device, skb->data,
2343 nopaged_len, DMA_TO_DEVICE);
2344 if (dma_mapping_error(priv->device, des))
2347 priv->tx_skbuff_dma[first_entry].buf = des;
2348 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
2349 first->des0 = cpu_to_le32(des);
2351 first->des2 = cpu_to_le32(des);
2353 priv->tx_skbuff_dma[first_entry].len = nopaged_len;
2354 priv->tx_skbuff_dma[first_entry].last_segment = last_segment;
2356 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2357 priv->hwts_tx_en)) {
2358 /* declare that device is doing timestamping */
2359 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2360 priv->hw->desc->enable_tx_timestamp(first);
2363 /* Prepare the first descriptor setting the OWN bit too */
2364 priv->hw->desc->prepare_tx_desc(first, 1, nopaged_len,
2365 csum_insertion, priv->mode, 1,
2368 /* The own bit must be the latest setting done when prepare the
2369 * descriptor and then barrier is needed to make sure that
2370 * all is coherent before granting the DMA engine.
2375 netdev_sent_queue(dev, skb->len);
2377 if (priv->synopsys_id < DWMAC_CORE_4_00)
2378 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2380 priv->hw->dma->set_tx_tail_ptr(priv->ioaddr, priv->tx_tail_addr,
2383 spin_unlock(&priv->tx_lock);
2384 return NETDEV_TX_OK;
2387 spin_unlock(&priv->tx_lock);
2388 dev_err(priv->device, "Tx dma map failed\n");
2390 priv->dev->stats.tx_dropped++;
2391 return NETDEV_TX_OK;
2394 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb)
2396 struct ethhdr *ehdr;
2399 if ((dev->features & NETIF_F_HW_VLAN_CTAG_RX) ==
2400 NETIF_F_HW_VLAN_CTAG_RX &&
2401 !__vlan_get_tag(skb, &vlanid)) {
2402 /* pop the vlan tag */
2403 ehdr = (struct ethhdr *)skb->data;
2404 memmove(skb->data + VLAN_HLEN, ehdr, ETH_ALEN * 2);
2405 skb_pull(skb, VLAN_HLEN);
2406 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vlanid);
2411 static inline int stmmac_rx_threshold_count(struct stmmac_priv *priv)
2413 if (priv->rx_zeroc_thresh < STMMAC_RX_THRESH)
2420 * stmmac_rx_refill - refill used skb preallocated buffers
2421 * @priv: driver private structure
2422 * Description : this is to reallocate the skb for the reception process
2423 * that is based on zero-copy.
2425 static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2427 int bfsize = priv->dma_buf_sz;
2428 unsigned int entry = priv->dirty_rx;
2429 int dirty = stmmac_rx_dirty(priv);
2431 while (dirty-- > 0) {
2434 if (priv->extend_desc)
2435 p = (struct dma_desc *)(priv->dma_erx + entry);
2437 p = priv->dma_rx + entry;
2439 if (likely(priv->rx_skbuff[entry] == NULL)) {
2440 struct sk_buff *skb;
2442 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
2443 if (unlikely(!skb)) {
2444 /* so for a while no zero-copy! */
2445 priv->rx_zeroc_thresh = STMMAC_RX_THRESH;
2446 if (unlikely(net_ratelimit()))
2447 dev_err(priv->device,
2448 "fail to alloc skb entry %d\n",
2453 priv->rx_skbuff[entry] = skb;
2454 priv->rx_skbuff_dma[entry] =
2455 dma_map_single(priv->device, skb->data, bfsize,
2457 if (dma_mapping_error(priv->device,
2458 priv->rx_skbuff_dma[entry])) {
2459 dev_err(priv->device, "Rx dma map failed\n");
2464 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00)) {
2465 p->des0 = cpu_to_le32(priv->rx_skbuff_dma[entry]);
2468 p->des2 = cpu_to_le32(priv->rx_skbuff_dma[entry]);
2470 if (priv->hw->mode->refill_desc3)
2471 priv->hw->mode->refill_desc3(priv, p);
2473 if (priv->rx_zeroc_thresh > 0)
2474 priv->rx_zeroc_thresh--;
2476 if (netif_msg_rx_status(priv))
2477 pr_debug("\trefill entry #%d\n", entry);
2481 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
2482 priv->hw->desc->init_rx_desc(p, priv->use_riwt, 0, 0, priv->dma_buf_sz);
2484 priv->hw->desc->set_rx_owner(p);
2488 entry = STMMAC_GET_ENTRY(entry, DMA_RX_SIZE);
2490 priv->dirty_rx = entry;
2494 * stmmac_rx - manage the receive process
2495 * @priv: driver private structure
2496 * @limit: napi bugget.
2497 * Description : this the function called by the napi poll method.
2498 * It gets all the frames inside the ring.
2500 static int stmmac_rx(struct stmmac_priv *priv, int limit)
2502 unsigned int next_entry = priv->cur_rx;
2503 unsigned int count = 0;
2504 int coe = priv->hw->rx_csum;
2506 if (netif_msg_rx_status(priv)) {
2509 pr_info(">>>>>> %s: descriptor ring:\n", __func__);
2510 if (priv->extend_desc)
2511 rx_head = (void *)priv->dma_erx;
2513 rx_head = (void *)priv->dma_rx;
2515 priv->hw->desc->display_ring(rx_head, DMA_RX_SIZE, true);
2517 while (count < limit) {
2520 struct dma_desc *np;
2524 if (priv->extend_desc)
2525 p = (struct dma_desc *)(priv->dma_erx + entry);
2527 p = priv->dma_rx + entry;
2529 /* read the status of the incoming frame */
2530 status = priv->hw->desc->rx_status(&priv->dev->stats,
2532 /* check if managed by the DMA otherwise go ahead */
2533 if (unlikely(status & dma_own))
2538 priv->cur_rx = STMMAC_GET_ENTRY(priv->cur_rx, DMA_RX_SIZE);
2539 next_entry = priv->cur_rx;
2541 if (priv->extend_desc)
2542 np = (struct dma_desc *)(priv->dma_erx + next_entry);
2544 np = priv->dma_rx + next_entry;
2548 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2549 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2553 if (unlikely(status == discard_frame)) {
2554 priv->dev->stats.rx_errors++;
2555 if (priv->hwts_rx_en && !priv->extend_desc) {
2556 /* DESC2 & DESC3 will be overwitten by device
2557 * with timestamp value, hence reinitialize
2558 * them in stmmac_rx_refill() function so that
2559 * device can reuse it.
2561 priv->rx_skbuff[entry] = NULL;
2562 dma_unmap_single(priv->device,
2563 priv->rx_skbuff_dma[entry],
2568 struct sk_buff *skb;
2572 if (unlikely(priv->synopsys_id >= DWMAC_CORE_4_00))
2573 des = le32_to_cpu(p->des0);
2575 des = le32_to_cpu(p->des2);
2577 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2579 /* If frame length is greather than skb buffer size
2580 * (preallocated during init) then the packet is
2583 if (frame_len > priv->dma_buf_sz) {
2584 pr_err("%s: len %d larger than size (%d)\n",
2585 priv->dev->name, frame_len,
2587 priv->dev->stats.rx_length_errors++;
2591 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
2592 * Type frames (LLC/LLC-SNAP)
2594 if (unlikely(status != llc_snap))
2595 frame_len -= ETH_FCS_LEN;
2597 if (netif_msg_rx_status(priv)) {
2598 pr_info("\tdesc: %p [entry %d] buff=0x%x\n",
2600 if (frame_len > ETH_FRAME_LEN)
2601 pr_debug("\tframe size %d, COE: %d\n",
2605 /* The zero-copy is always used for all the sizes
2606 * in case of GMAC4 because it needs
2607 * to refill the used descriptors, always.
2609 if (unlikely(!priv->plat->has_gmac4 &&
2610 ((frame_len < priv->rx_copybreak) ||
2611 stmmac_rx_threshold_count(priv)))) {
2612 skb = netdev_alloc_skb_ip_align(priv->dev,
2614 if (unlikely(!skb)) {
2615 if (net_ratelimit())
2616 dev_warn(priv->device,
2617 "packet dropped\n");
2618 priv->dev->stats.rx_dropped++;
2622 dma_sync_single_for_cpu(priv->device,
2626 skb_copy_to_linear_data(skb,
2628 rx_skbuff[entry]->data,
2631 skb_put(skb, frame_len);
2632 dma_sync_single_for_device(priv->device,
2637 skb = priv->rx_skbuff[entry];
2638 if (unlikely(!skb)) {
2639 pr_err("%s: Inconsistent Rx chain\n",
2641 priv->dev->stats.rx_dropped++;
2644 prefetch(skb->data - NET_IP_ALIGN);
2645 priv->rx_skbuff[entry] = NULL;
2646 priv->rx_zeroc_thresh++;
2648 skb_put(skb, frame_len);
2649 dma_unmap_single(priv->device,
2650 priv->rx_skbuff_dma[entry],
2655 if (netif_msg_pktdata(priv)) {
2656 pr_debug("frame received (%dbytes)", frame_len);
2657 print_pkt(skb->data, frame_len);
2660 stmmac_get_rx_hwtstamp(priv, p, np, skb);
2662 stmmac_rx_vlan(priv->dev, skb);
2664 skb->protocol = eth_type_trans(skb, priv->dev);
2667 skb_checksum_none_assert(skb);
2669 skb->ip_summed = CHECKSUM_UNNECESSARY;
2671 napi_gro_receive(&priv->napi, skb);
2673 priv->dev->stats.rx_packets++;
2674 priv->dev->stats.rx_bytes += frame_len;
2678 stmmac_rx_refill(priv);
2680 priv->xstats.rx_pkt_n += count;
2686 * stmmac_poll - stmmac poll method (NAPI)
2687 * @napi : pointer to the napi structure.
2688 * @budget : maximum number of packets that the current CPU can receive from
2691 * To look at the incoming frames and clear the tx resources.
2693 static int stmmac_poll(struct napi_struct *napi, int budget)
2695 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2698 priv->xstats.napi_poll++;
2699 stmmac_tx_clean(priv);
2701 work_done = stmmac_rx(priv, budget);
2702 if (work_done < budget) {
2703 napi_complete(napi);
2704 stmmac_enable_dma_irq(priv);
2711 * @dev : Pointer to net device structure
2712 * Description: this function is called when a packet transmission fails to
2713 * complete within a reasonable time. The driver will mark the error in the
2714 * netdev structure and arrange for the device to be reset to a sane state
2715 * in order to transmit a new packet.
2717 static void stmmac_tx_timeout(struct net_device *dev)
2719 struct stmmac_priv *priv = netdev_priv(dev);
2721 /* Clear Tx resources and restart transmitting again */
2722 stmmac_tx_err(priv);
2726 * stmmac_set_rx_mode - entry point for multicast addressing
2727 * @dev : pointer to the device structure
2729 * This function is a driver entry point which gets called by the kernel
2730 * whenever multicast addresses must be enabled/disabled.
2734 static void stmmac_set_rx_mode(struct net_device *dev)
2736 struct stmmac_priv *priv = netdev_priv(dev);
2738 priv->hw->mac->set_filter(priv->hw, dev);
2742 * stmmac_change_mtu - entry point to change MTU size for the device.
2743 * @dev : device pointer.
2744 * @new_mtu : the new MTU size for the device.
2745 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2746 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2747 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2749 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2752 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2754 struct stmmac_priv *priv = netdev_priv(dev);
2757 if (netif_running(dev)) {
2758 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2762 if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
2763 max_mtu = JUMBO_LEN;
2765 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
2767 if (priv->plat->maxmtu < max_mtu)
2768 max_mtu = priv->plat->maxmtu;
2770 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2771 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2777 netdev_update_features(dev);
2782 static netdev_features_t stmmac_fix_features(struct net_device *dev,
2783 netdev_features_t features)
2785 struct stmmac_priv *priv = netdev_priv(dev);
2787 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
2788 features &= ~NETIF_F_RXCSUM;
2790 if (!priv->plat->tx_coe)
2791 features &= ~NETIF_F_CSUM_MASK;
2793 /* Some GMAC devices have a bugged Jumbo frame support that
2794 * needs to have the Tx COE disabled for oversized frames
2795 * (due to limited buffer sizes). In this case we disable
2796 * the TX csum insertionin the TDES and not use SF.
2798 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
2799 features &= ~NETIF_F_CSUM_MASK;
2801 /* Disable tso if asked by ethtool */
2802 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
2803 if (features & NETIF_F_TSO)
2812 static int stmmac_set_features(struct net_device *netdev,
2813 netdev_features_t features)
2815 struct stmmac_priv *priv = netdev_priv(netdev);
2817 /* Keep the COE Type in case of csum is supporting */
2818 if (features & NETIF_F_RXCSUM)
2819 priv->hw->rx_csum = priv->plat->rx_coe;
2821 priv->hw->rx_csum = 0;
2822 /* No check needed because rx_coe has been set before and it will be
2823 * fixed in case of issue.
2825 priv->hw->mac->rx_ipc(priv->hw);
2831 * stmmac_interrupt - main ISR
2832 * @irq: interrupt number.
2833 * @dev_id: to pass the net device pointer.
2834 * Description: this is the main driver interrupt service routine.
2836 * o DMA service routine (to manage incoming frame reception and transmission
2838 * o Core interrupts to manage: remote wake-up, management counter, LPI
2841 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2843 struct net_device *dev = (struct net_device *)dev_id;
2844 struct stmmac_priv *priv = netdev_priv(dev);
2847 pm_wakeup_event(priv->device, 0);
2849 if (unlikely(!dev)) {
2850 pr_err("%s: invalid dev pointer\n", __func__);
2854 /* To handle GMAC own interrupts */
2855 if ((priv->plat->has_gmac) || (priv->plat->has_gmac4)) {
2856 int status = priv->hw->mac->host_irq_status(priv->hw,
2858 if (unlikely(status)) {
2859 /* For LPI we need to save the tx status */
2860 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
2861 priv->tx_path_in_lpi_mode = true;
2862 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
2863 priv->tx_path_in_lpi_mode = false;
2864 if (status & CORE_IRQ_MTL_RX_OVERFLOW && priv->hw->dma->set_rx_tail_ptr)
2865 priv->hw->dma->set_rx_tail_ptr(priv->ioaddr,
2870 /* PCS link status */
2871 if (priv->hw->pcs) {
2872 if (priv->xstats.pcs_link)
2873 netif_carrier_on(dev);
2875 netif_carrier_off(dev);
2879 /* To handle DMA interrupts */
2880 stmmac_dma_interrupt(priv);
2885 #ifdef CONFIG_NET_POLL_CONTROLLER
2886 /* Polling receive - used by NETCONSOLE and other diagnostic tools
2887 * to allow network I/O with interrupts disabled.
2889 static void stmmac_poll_controller(struct net_device *dev)
2891 disable_irq(dev->irq);
2892 stmmac_interrupt(dev->irq, dev);
2893 enable_irq(dev->irq);
2898 * stmmac_ioctl - Entry point for the Ioctl
2899 * @dev: Device pointer.
2900 * @rq: An IOCTL specefic structure, that can contain a pointer to
2901 * a proprietary structure used to pass information to the driver.
2902 * @cmd: IOCTL command
2904 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
2906 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2908 struct stmmac_priv *priv = netdev_priv(dev);
2909 int ret = -EOPNOTSUPP;
2911 if (!netif_running(dev))
2920 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2923 ret = stmmac_hwtstamp_ioctl(dev, rq);
2932 static int stmmac_set_mac_address(struct net_device *ndev, void *addr)
2934 struct stmmac_priv *priv = netdev_priv(ndev);
2937 ret = eth_mac_addr(ndev, addr);
2941 priv->hw->mac->set_umac_addr(priv->hw, ndev->dev_addr, 0);
2946 #ifdef CONFIG_DEBUG_FS
2947 static struct dentry *stmmac_fs_dir;
2949 static void sysfs_display_ring(void *head, int size, int extend_desc,
2950 struct seq_file *seq)
2953 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2954 struct dma_desc *p = (struct dma_desc *)head;
2956 for (i = 0; i < size; i++) {
2960 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
2961 i, (unsigned int)virt_to_phys(ep),
2962 le32_to_cpu(ep->basic.des0),
2963 le32_to_cpu(ep->basic.des1),
2964 le32_to_cpu(ep->basic.des2),
2965 le32_to_cpu(ep->basic.des3));
2969 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
2970 i, (unsigned int)virt_to_phys(ep),
2971 le32_to_cpu(p->des0), le32_to_cpu(p->des1),
2972 le32_to_cpu(p->des2), le32_to_cpu(p->des3));
2975 seq_printf(seq, "\n");
2979 static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2981 struct net_device *dev = seq->private;
2982 struct stmmac_priv *priv = netdev_priv(dev);
2984 if (priv->extend_desc) {
2985 seq_printf(seq, "Extended RX descriptor ring:\n");
2986 sysfs_display_ring((void *)priv->dma_erx, DMA_RX_SIZE, 1, seq);
2987 seq_printf(seq, "Extended TX descriptor ring:\n");
2988 sysfs_display_ring((void *)priv->dma_etx, DMA_TX_SIZE, 1, seq);
2990 seq_printf(seq, "RX descriptor ring:\n");
2991 sysfs_display_ring((void *)priv->dma_rx, DMA_RX_SIZE, 0, seq);
2992 seq_printf(seq, "TX descriptor ring:\n");
2993 sysfs_display_ring((void *)priv->dma_tx, DMA_TX_SIZE, 0, seq);
2999 static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
3001 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
3004 static const struct file_operations stmmac_rings_status_fops = {
3005 .owner = THIS_MODULE,
3006 .open = stmmac_sysfs_ring_open,
3008 .llseek = seq_lseek,
3009 .release = single_release,
3012 static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
3014 struct net_device *dev = seq->private;
3015 struct stmmac_priv *priv = netdev_priv(dev);
3017 if (!priv->hw_cap_support) {
3018 seq_printf(seq, "DMA HW features not supported\n");
3022 seq_printf(seq, "==============================\n");
3023 seq_printf(seq, "\tDMA HW features\n");
3024 seq_printf(seq, "==============================\n");
3026 seq_printf(seq, "\t10/100 Mbps %s\n",
3027 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
3028 seq_printf(seq, "\t1000 Mbps %s\n",
3029 (priv->dma_cap.mbps_1000) ? "Y" : "N");
3030 seq_printf(seq, "\tHalf duple %s\n",
3031 (priv->dma_cap.half_duplex) ? "Y" : "N");
3032 seq_printf(seq, "\tHash Filter: %s\n",
3033 (priv->dma_cap.hash_filter) ? "Y" : "N");
3034 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
3035 (priv->dma_cap.multi_addr) ? "Y" : "N");
3036 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
3037 (priv->dma_cap.pcs) ? "Y" : "N");
3038 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
3039 (priv->dma_cap.sma_mdio) ? "Y" : "N");
3040 seq_printf(seq, "\tPMT Remote wake up: %s\n",
3041 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
3042 seq_printf(seq, "\tPMT Magic Frame: %s\n",
3043 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
3044 seq_printf(seq, "\tRMON module: %s\n",
3045 (priv->dma_cap.rmon) ? "Y" : "N");
3046 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
3047 (priv->dma_cap.time_stamp) ? "Y" : "N");
3048 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
3049 (priv->dma_cap.atime_stamp) ? "Y" : "N");
3050 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
3051 (priv->dma_cap.eee) ? "Y" : "N");
3052 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
3053 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
3054 (priv->dma_cap.tx_coe) ? "Y" : "N");
3055 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3056 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n",
3057 (priv->dma_cap.rx_coe) ? "Y" : "N");
3059 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
3060 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
3061 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
3062 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
3064 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
3065 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
3066 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
3067 priv->dma_cap.number_rx_channel);
3068 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
3069 priv->dma_cap.number_tx_channel);
3070 seq_printf(seq, "\tEnhanced descriptors: %s\n",
3071 (priv->dma_cap.enh_desc) ? "Y" : "N");
3076 static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
3078 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
3081 static const struct file_operations stmmac_dma_cap_fops = {
3082 .owner = THIS_MODULE,
3083 .open = stmmac_sysfs_dma_cap_open,
3085 .llseek = seq_lseek,
3086 .release = single_release,
3089 static int stmmac_init_fs(struct net_device *dev)
3091 struct stmmac_priv *priv = netdev_priv(dev);
3093 /* Create per netdev entries */
3094 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir);
3096 if (!priv->dbgfs_dir || IS_ERR(priv->dbgfs_dir)) {
3097 pr_err("ERROR %s/%s, debugfs create directory failed\n",
3098 STMMAC_RESOURCE_NAME, dev->name);
3103 /* Entry to report DMA RX/TX rings */
3104 priv->dbgfs_rings_status =
3105 debugfs_create_file("descriptors_status", S_IRUGO,
3106 priv->dbgfs_dir, dev,
3107 &stmmac_rings_status_fops);
3109 if (!priv->dbgfs_rings_status || IS_ERR(priv->dbgfs_rings_status)) {
3110 pr_info("ERROR creating stmmac ring debugfs file\n");
3111 debugfs_remove_recursive(priv->dbgfs_dir);
3116 /* Entry to report the DMA HW features */
3117 priv->dbgfs_dma_cap = debugfs_create_file("dma_cap", S_IRUGO,
3119 dev, &stmmac_dma_cap_fops);
3121 if (!priv->dbgfs_dma_cap || IS_ERR(priv->dbgfs_dma_cap)) {
3122 pr_info("ERROR creating stmmac MMC debugfs file\n");
3123 debugfs_remove_recursive(priv->dbgfs_dir);
3131 static void stmmac_exit_fs(struct net_device *dev)
3133 struct stmmac_priv *priv = netdev_priv(dev);
3135 debugfs_remove_recursive(priv->dbgfs_dir);
3137 #endif /* CONFIG_DEBUG_FS */
3139 static const struct net_device_ops stmmac_netdev_ops = {
3140 .ndo_open = stmmac_open,
3141 .ndo_start_xmit = stmmac_xmit,
3142 .ndo_stop = stmmac_release,
3143 .ndo_change_mtu = stmmac_change_mtu,
3144 .ndo_fix_features = stmmac_fix_features,
3145 .ndo_set_features = stmmac_set_features,
3146 .ndo_set_rx_mode = stmmac_set_rx_mode,
3147 .ndo_tx_timeout = stmmac_tx_timeout,
3148 .ndo_do_ioctl = stmmac_ioctl,
3149 #ifdef CONFIG_NET_POLL_CONTROLLER
3150 .ndo_poll_controller = stmmac_poll_controller,
3152 .ndo_set_mac_address = stmmac_set_mac_address,
3156 * stmmac_hw_init - Init the MAC device
3157 * @priv: driver private structure
3158 * Description: this function is to configure the MAC device according to
3159 * some platform parameters or the HW capability register. It prepares the
3160 * driver to use either ring or chain modes and to setup either enhanced or
3161 * normal descriptors.
3163 static int stmmac_hw_init(struct stmmac_priv *priv)
3165 struct mac_device_info *mac;
3167 /* Identify the MAC HW device */
3168 if (priv->plat->has_gmac) {
3169 priv->dev->priv_flags |= IFF_UNICAST_FLT;
3170 mac = dwmac1000_setup(priv->ioaddr,
3171 priv->plat->multicast_filter_bins,
3172 priv->plat->unicast_filter_entries,
3173 &priv->synopsys_id);
3174 } else if (priv->plat->has_gmac4) {
3175 priv->dev->priv_flags |= IFF_UNICAST_FLT;
3176 mac = dwmac4_setup(priv->ioaddr,
3177 priv->plat->multicast_filter_bins,
3178 priv->plat->unicast_filter_entries,
3179 &priv->synopsys_id);
3181 mac = dwmac100_setup(priv->ioaddr, &priv->synopsys_id);
3188 /* To use the chained or ring mode */
3189 if (priv->synopsys_id >= DWMAC_CORE_4_00) {
3190 priv->hw->mode = &dwmac4_ring_mode_ops;
3193 priv->hw->mode = &chain_mode_ops;
3194 pr_info(" Chain mode enabled\n");
3195 priv->mode = STMMAC_CHAIN_MODE;
3197 priv->hw->mode = &ring_mode_ops;
3198 pr_info(" Ring mode enabled\n");
3199 priv->mode = STMMAC_RING_MODE;
3203 /* Get the HW capability (new GMAC newer than 3.50a) */
3204 priv->hw_cap_support = stmmac_get_hw_features(priv);
3205 if (priv->hw_cap_support) {
3206 pr_info(" DMA HW capability register supported");
3208 /* We can override some gmac/dma configuration fields: e.g.
3209 * enh_desc, tx_coe (e.g. that are passed through the
3210 * platform) with the values from the HW capability
3211 * register (if supported).
3213 priv->plat->enh_desc = priv->dma_cap.enh_desc;
3214 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
3215 priv->hw->pmt = priv->plat->pmt;
3217 /* TXCOE doesn't work in thresh DMA mode */
3218 if (priv->plat->force_thresh_dma_mode)
3219 priv->plat->tx_coe = 0;
3221 priv->plat->tx_coe = priv->dma_cap.tx_coe;
3223 /* In case of GMAC4 rx_coe is from HW cap register. */
3224 priv->plat->rx_coe = priv->dma_cap.rx_coe;
3226 if (priv->dma_cap.rx_coe_type2)
3227 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
3228 else if (priv->dma_cap.rx_coe_type1)
3229 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
3232 pr_info(" No HW DMA feature register supported");
3234 /* To use alternate (extended), normal or GMAC4 descriptor structures */
3235 if (priv->synopsys_id >= DWMAC_CORE_4_00)
3236 priv->hw->desc = &dwmac4_desc_ops;
3238 stmmac_selec_desc_mode(priv);
3240 if (priv->plat->rx_coe) {
3241 priv->hw->rx_csum = priv->plat->rx_coe;
3242 pr_info(" RX Checksum Offload Engine supported\n");
3243 if (priv->synopsys_id < DWMAC_CORE_4_00)
3244 pr_info("\tCOE Type %d\n", priv->hw->rx_csum);
3246 if (priv->plat->tx_coe)
3247 pr_info(" TX Checksum insertion supported\n");
3249 if (priv->plat->pmt) {
3250 pr_info(" Wake-Up On Lan supported\n");
3251 device_set_wakeup_capable(priv->device, 1);
3254 if (priv->dma_cap.tsoen)
3255 pr_info(" TSO supported\n");
3262 * @device: device pointer
3263 * @plat_dat: platform data pointer
3264 * @res: stmmac resource pointer
3265 * Description: this is the main probe function used to
3266 * call the alloc_etherdev, allocate the priv structure.
3268 * returns 0 on success, otherwise errno.
3270 int stmmac_dvr_probe(struct device *device,
3271 struct plat_stmmacenet_data *plat_dat,
3272 struct stmmac_resources *res)
3275 struct net_device *ndev = NULL;
3276 struct stmmac_priv *priv;
3278 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
3282 SET_NETDEV_DEV(ndev, device);
3284 priv = netdev_priv(ndev);
3285 priv->device = device;
3288 stmmac_set_ethtool_ops(ndev);
3289 priv->pause = pause;
3290 priv->plat = plat_dat;
3291 priv->ioaddr = res->addr;
3292 priv->dev->base_addr = (unsigned long)res->addr;
3294 priv->dev->irq = res->irq;
3295 priv->wol_irq = res->wol_irq;
3296 priv->lpi_irq = res->lpi_irq;
3299 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
3301 dev_set_drvdata(device, priv->dev);
3303 /* Verify driver arguments */
3304 stmmac_verify_args();
3306 /* Override with kernel parameters if supplied XXX CRS XXX
3307 * this needs to have multiple instances
3309 if ((phyaddr >= 0) && (phyaddr <= 31))
3310 priv->plat->phy_addr = phyaddr;
3312 priv->stmmac_clk = devm_clk_get(priv->device, STMMAC_RESOURCE_NAME);
3313 if (IS_ERR(priv->stmmac_clk)) {
3314 dev_warn(priv->device, "%s: warning: cannot get CSR clock\n",
3316 /* If failed to obtain stmmac_clk and specific clk_csr value
3317 * is NOT passed from the platform, probe fail.
3319 if (!priv->plat->clk_csr) {
3320 ret = PTR_ERR(priv->stmmac_clk);
3323 priv->stmmac_clk = NULL;
3326 clk_prepare_enable(priv->stmmac_clk);
3328 priv->pclk = devm_clk_get(priv->device, "pclk");
3329 if (IS_ERR(priv->pclk)) {
3330 if (PTR_ERR(priv->pclk) == -EPROBE_DEFER) {
3331 ret = -EPROBE_DEFER;
3332 goto error_pclk_get;
3336 clk_prepare_enable(priv->pclk);
3338 priv->stmmac_rst = devm_reset_control_get(priv->device,
3339 STMMAC_RESOURCE_NAME);
3340 if (IS_ERR(priv->stmmac_rst)) {
3341 if (PTR_ERR(priv->stmmac_rst) == -EPROBE_DEFER) {
3342 ret = -EPROBE_DEFER;
3345 dev_info(priv->device, "no reset control found\n");
3346 priv->stmmac_rst = NULL;
3348 if (priv->stmmac_rst)
3349 reset_control_deassert(priv->stmmac_rst);
3351 /* Init MAC and get the capabilities */
3352 ret = stmmac_hw_init(priv);
3356 stmmac_check_ether_addr(priv);
3358 ndev->netdev_ops = &stmmac_netdev_ops;
3360 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
3363 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) {
3364 ndev->hw_features |= NETIF_F_TSO;
3366 pr_info(" TSO feature enabled\n");
3368 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
3369 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
3370 #ifdef STMMAC_VLAN_TAG_USED
3371 /* Both mac100 and gmac support receive VLAN tag detection */
3372 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
3374 priv->msg_enable = netif_msg_init(debug, default_msg_level);
3377 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
3379 /* Rx Watchdog is available in the COREs newer than the 3.40.
3380 * In some case, for example on bugged HW this feature
3381 * has to be disable and this can be done by passing the
3382 * riwt_off field from the platform.
3384 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
3386 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
3389 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
3391 spin_lock_init(&priv->lock);
3392 spin_lock_init(&priv->tx_lock);
3394 /* If a specific clk_csr value is passed from the platform
3395 * this means that the CSR Clock Range selection cannot be
3396 * changed at run-time and it is fixed. Viceversa the driver'll try to
3397 * set the MDC clock dynamically according to the csr actual
3400 if (!priv->plat->clk_csr)
3401 stmmac_clk_csr_set(priv);
3403 priv->clk_csr = priv->plat->clk_csr;
3405 stmmac_check_pcs_mode(priv);
3407 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
3408 priv->hw->pcs != STMMAC_PCS_TBI &&
3409 priv->hw->pcs != STMMAC_PCS_RTBI) {
3410 /* MDIO bus Registration */
3411 ret = stmmac_mdio_register(ndev);
3413 pr_debug("%s: MDIO bus (id: %d) registration failed",
3414 __func__, priv->plat->bus_id);
3415 goto error_napi_register;
3419 ret = register_netdev(ndev);
3421 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
3422 goto error_netdev_register;
3427 error_netdev_register:
3428 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
3429 priv->hw->pcs != STMMAC_PCS_TBI &&
3430 priv->hw->pcs != STMMAC_PCS_RTBI)
3431 stmmac_mdio_unregister(ndev);
3432 error_napi_register:
3433 netif_napi_del(&priv->napi);
3435 clk_disable_unprepare(priv->pclk);
3437 clk_disable_unprepare(priv->stmmac_clk);
3443 EXPORT_SYMBOL_GPL(stmmac_dvr_probe);
3447 * @dev: device pointer
3448 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
3449 * changes the link status, releases the DMA descriptor rings.
3451 int stmmac_dvr_remove(struct device *dev)
3453 struct net_device *ndev = dev_get_drvdata(dev);
3454 struct stmmac_priv *priv = netdev_priv(ndev);
3456 pr_info("%s:\n\tremoving driver", __func__);
3458 priv->hw->dma->stop_rx(priv->ioaddr);
3459 priv->hw->dma->stop_tx(priv->ioaddr);
3461 stmmac_set_mac(priv->ioaddr, false);
3462 netif_carrier_off(ndev);
3463 unregister_netdev(ndev);
3464 if (priv->stmmac_rst)
3465 reset_control_assert(priv->stmmac_rst);
3466 clk_disable_unprepare(priv->pclk);
3467 clk_disable_unprepare(priv->stmmac_clk);
3468 if (priv->hw->pcs != STMMAC_PCS_RGMII &&
3469 priv->hw->pcs != STMMAC_PCS_TBI &&
3470 priv->hw->pcs != STMMAC_PCS_RTBI)
3471 stmmac_mdio_unregister(ndev);
3476 EXPORT_SYMBOL_GPL(stmmac_dvr_remove);
3479 * stmmac_suspend - suspend callback
3480 * @dev: device pointer
3481 * Description: this is the function to suspend the device and it is called
3482 * by the platform driver to stop the network queue, release the resources,
3483 * program the PMT register (for WoL), clean and release driver resources.
3485 int stmmac_suspend(struct device *dev)
3487 struct net_device *ndev = dev_get_drvdata(dev);
3488 struct stmmac_priv *priv = netdev_priv(ndev);
3489 unsigned long flags;
3491 if (!ndev || !netif_running(ndev))
3495 phy_stop(priv->phydev);
3497 spin_lock_irqsave(&priv->lock, flags);
3499 netif_device_detach(ndev);
3500 netif_stop_queue(ndev);
3502 napi_disable(&priv->napi);
3504 /* Stop TX/RX DMA */
3505 priv->hw->dma->stop_tx(priv->ioaddr);
3506 priv->hw->dma->stop_rx(priv->ioaddr);
3508 /* Enable Power down mode by programming the PMT regs */
3509 if (device_may_wakeup(priv->device)) {
3510 priv->hw->mac->pmt(priv->hw, priv->wolopts);
3513 stmmac_set_mac(priv->ioaddr, false);
3514 pinctrl_pm_select_sleep_state(priv->device);
3515 /* Disable clock in case of PWM is off */
3516 clk_disable(priv->pclk);
3517 clk_disable(priv->stmmac_clk);
3519 spin_unlock_irqrestore(&priv->lock, flags);
3523 priv->oldduplex = -1;
3526 EXPORT_SYMBOL_GPL(stmmac_suspend);
3529 * stmmac_resume - resume callback
3530 * @dev: device pointer
3531 * Description: when resume this function is invoked to setup the DMA and CORE
3532 * in a usable state.
3534 int stmmac_resume(struct device *dev)
3536 struct net_device *ndev = dev_get_drvdata(dev);
3537 struct stmmac_priv *priv = netdev_priv(ndev);
3538 unsigned long flags;
3540 if (!netif_running(ndev))
3543 /* Power Down bit, into the PM register, is cleared
3544 * automatically as soon as a magic packet or a Wake-up frame
3545 * is received. Anyway, it's better to manually clear
3546 * this bit because it can generate problems while resuming
3547 * from another devices (e.g. serial console).
3549 if (device_may_wakeup(priv->device)) {
3550 spin_lock_irqsave(&priv->lock, flags);
3551 priv->hw->mac->pmt(priv->hw, 0);
3552 spin_unlock_irqrestore(&priv->lock, flags);
3555 pinctrl_pm_select_default_state(priv->device);
3556 /* enable the clk prevously disabled */
3557 clk_enable(priv->stmmac_clk);
3558 clk_enable(priv->pclk);
3559 /* reset the phy so that it's ready */
3561 stmmac_mdio_reset(priv->mii);
3564 netif_device_attach(ndev);
3566 spin_lock_irqsave(&priv->lock, flags);
3572 /* reset private mss value to force mss context settings at
3573 * next tso xmit (only used for gmac4).
3577 stmmac_clear_descriptors(priv);
3579 stmmac_hw_setup(ndev, false);
3580 stmmac_init_tx_coalesce(priv);
3581 stmmac_set_rx_mode(ndev);
3583 napi_enable(&priv->napi);
3585 netif_start_queue(ndev);
3587 spin_unlock_irqrestore(&priv->lock, flags);
3590 phy_start(priv->phydev);
3594 EXPORT_SYMBOL_GPL(stmmac_resume);
3597 static int __init stmmac_cmdline_opt(char *str)
3603 while ((opt = strsep(&str, ",")) != NULL) {
3604 if (!strncmp(opt, "debug:", 6)) {
3605 if (kstrtoint(opt + 6, 0, &debug))
3607 } else if (!strncmp(opt, "phyaddr:", 8)) {
3608 if (kstrtoint(opt + 8, 0, &phyaddr))
3610 } else if (!strncmp(opt, "buf_sz:", 7)) {
3611 if (kstrtoint(opt + 7, 0, &buf_sz))
3613 } else if (!strncmp(opt, "tc:", 3)) {
3614 if (kstrtoint(opt + 3, 0, &tc))
3616 } else if (!strncmp(opt, "watchdog:", 9)) {
3617 if (kstrtoint(opt + 9, 0, &watchdog))
3619 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
3620 if (kstrtoint(opt + 10, 0, &flow_ctrl))
3622 } else if (!strncmp(opt, "pause:", 6)) {
3623 if (kstrtoint(opt + 6, 0, &pause))
3625 } else if (!strncmp(opt, "eee_timer:", 10)) {
3626 if (kstrtoint(opt + 10, 0, &eee_timer))
3628 } else if (!strncmp(opt, "chain_mode:", 11)) {
3629 if (kstrtoint(opt + 11, 0, &chain_mode))
3636 pr_err("%s: ERROR broken module parameter conversion", __func__);
3640 __setup("stmmaceth=", stmmac_cmdline_opt);
3643 static int __init stmmac_init(void)
3645 #ifdef CONFIG_DEBUG_FS
3646 /* Create debugfs main directory if it doesn't exist yet */
3647 if (!stmmac_fs_dir) {
3648 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
3650 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
3651 pr_err("ERROR %s, debugfs create directory failed\n",
3652 STMMAC_RESOURCE_NAME);
3662 static void __exit stmmac_exit(void)
3664 #ifdef CONFIG_DEBUG_FS
3665 debugfs_remove_recursive(stmmac_fs_dir);
3669 module_init(stmmac_init)
3670 module_exit(stmmac_exit)
3672 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3673 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3674 MODULE_LICENSE("GPL");