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 #ifdef CONFIG_STMMAC_DEBUG_FS
47 #include <linux/debugfs.h>
48 #include <linux/seq_file.h>
50 #include <linux/net_tstamp.h>
51 #include "stmmac_ptp.h"
55 /*#define STMMAC_DEBUG*/
57 #define DBG(nlevel, klevel, fmt, args...) \
58 ((void)(netif_msg_##nlevel(priv) && \
59 printk(KERN_##klevel fmt, ## args)))
61 #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
64 #undef STMMAC_RX_DEBUG
65 /*#define STMMAC_RX_DEBUG*/
66 #ifdef STMMAC_RX_DEBUG
67 #define RX_DBG(fmt, args...) printk(fmt, ## args)
69 #define RX_DBG(fmt, args...) do { } while (0)
72 #undef STMMAC_XMIT_DEBUG
73 /*#define STMMAC_XMIT_DEBUG*/
74 #ifdef STMMAC_XMIT_DEBUG
75 #define TX_DBG(fmt, args...) printk(fmt, ## args)
77 #define TX_DBG(fmt, args...) do { } while (0)
80 #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
81 #define JUMBO_LEN 9000
83 /* Module parameters */
85 static int watchdog = TX_TIMEO;
86 module_param(watchdog, int, S_IRUGO | S_IWUSR);
87 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
89 static int debug = -1;
90 module_param(debug, int, S_IRUGO | S_IWUSR);
91 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
94 module_param(phyaddr, int, S_IRUGO);
95 MODULE_PARM_DESC(phyaddr, "Physical device address");
97 #define DMA_TX_SIZE 256
98 static int dma_txsize = DMA_TX_SIZE;
99 module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
100 MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
102 #define DMA_RX_SIZE 256
103 static int dma_rxsize = DMA_RX_SIZE;
104 module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
105 MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
107 static int flow_ctrl = FLOW_OFF;
108 module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
109 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
111 static int pause = PAUSE_TIME;
112 module_param(pause, int, S_IRUGO | S_IWUSR);
113 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
115 #define TC_DEFAULT 64
116 static int tc = TC_DEFAULT;
117 module_param(tc, int, S_IRUGO | S_IWUSR);
118 MODULE_PARM_DESC(tc, "DMA threshold control value");
120 #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
121 static int buf_sz = DMA_BUFFER_SIZE;
122 module_param(buf_sz, int, S_IRUGO | S_IWUSR);
123 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
125 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
126 NETIF_MSG_LINK | NETIF_MSG_IFUP |
127 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
129 #define STMMAC_DEFAULT_LPI_TIMER 1000
130 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
131 module_param(eee_timer, int, S_IRUGO | S_IWUSR);
132 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
133 #define STMMAC_LPI_TIMER(x) (jiffies + msecs_to_jiffies(x))
135 /* By default the driver will use the ring mode to manage tx and rx descriptors
136 * but passing this value so user can force to use the chain instead of the ring
138 static unsigned int chain_mode;
139 module_param(chain_mode, int, S_IRUGO);
140 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
142 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
144 #ifdef CONFIG_STMMAC_DEBUG_FS
145 static int stmmac_init_fs(struct net_device *dev);
146 static void stmmac_exit_fs(void);
149 #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
152 * stmmac_verify_args - verify the driver parameters.
153 * Description: it verifies if some wrong parameter is passed to the driver.
154 * Note that wrong parameters are replaced with the default values.
156 static void stmmac_verify_args(void)
158 if (unlikely(watchdog < 0))
160 if (unlikely(dma_rxsize < 0))
161 dma_rxsize = DMA_RX_SIZE;
162 if (unlikely(dma_txsize < 0))
163 dma_txsize = DMA_TX_SIZE;
164 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
165 buf_sz = DMA_BUFFER_SIZE;
166 if (unlikely(flow_ctrl > 1))
167 flow_ctrl = FLOW_AUTO;
168 else if (likely(flow_ctrl < 0))
169 flow_ctrl = FLOW_OFF;
170 if (unlikely((pause < 0) || (pause > 0xffff)))
173 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
177 * stmmac_clk_csr_set - dynamically set the MDC clock
178 * @priv: driver private structure
179 * Description: this is to dynamically set the MDC clock according to the csr
182 * If a specific clk_csr value is passed from the platform
183 * this means that the CSR Clock Range selection cannot be
184 * changed at run-time and it is fixed (as reported in the driver
185 * documentation). Viceversa the driver will try to set the MDC
186 * clock dynamically according to the actual clock input.
188 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
192 clk_rate = clk_get_rate(priv->stmmac_clk);
194 /* Platform provided default clk_csr would be assumed valid
195 * for all other cases except for the below mentioned ones. */
196 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
197 if (clk_rate < CSR_F_35M)
198 priv->clk_csr = STMMAC_CSR_20_35M;
199 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
200 priv->clk_csr = STMMAC_CSR_35_60M;
201 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
202 priv->clk_csr = STMMAC_CSR_60_100M;
203 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
204 priv->clk_csr = STMMAC_CSR_100_150M;
205 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
206 priv->clk_csr = STMMAC_CSR_150_250M;
207 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
208 priv->clk_csr = STMMAC_CSR_250_300M;
209 } /* For values higher than the IEEE 802.3 specified frequency
210 * we can not estimate the proper divider as it is not known
211 * the frequency of clk_csr_i. So we do not change the default
215 #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
216 static void print_pkt(unsigned char *buf, int len)
219 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
220 for (j = 0; j < len; j++) {
222 pr_info("\n %03x:", j);
223 pr_info(" %02x", buf[j]);
229 /* minimum number of free TX descriptors required to wake up TX process */
230 #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
232 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
234 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
238 * stmmac_hw_fix_mac_speed: callback for speed selection
239 * @priv: driver private structure
240 * Description: on some platforms (e.g. ST), some HW system configuraton
241 * registers have to be set according to the link speed negotiated.
243 static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
245 struct phy_device *phydev = priv->phydev;
247 if (likely(priv->plat->fix_mac_speed))
248 priv->plat->fix_mac_speed(priv->plat->bsp_priv,
253 * stmmac_enable_eee_mode: Check and enter in LPI mode
254 * @priv: driver private structure
255 * Description: this function is to verify and enter in LPI mode for EEE.
257 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
259 /* Check and enter in LPI mode */
260 if ((priv->dirty_tx == priv->cur_tx) &&
261 (priv->tx_path_in_lpi_mode == false))
262 priv->hw->mac->set_eee_mode(priv->ioaddr);
266 * stmmac_disable_eee_mode: disable/exit from EEE
267 * @priv: driver private structure
268 * Description: this function is to exit and disable EEE in case of
269 * LPI state is true. This is called by the xmit.
271 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
273 priv->hw->mac->reset_eee_mode(priv->ioaddr);
274 del_timer_sync(&priv->eee_ctrl_timer);
275 priv->tx_path_in_lpi_mode = false;
279 * stmmac_eee_ctrl_timer: EEE TX SW timer.
282 * if there is no data transfer and if we are not in LPI state,
283 * then MAC Transmitter can be moved to LPI state.
285 static void stmmac_eee_ctrl_timer(unsigned long arg)
287 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
289 stmmac_enable_eee_mode(priv);
290 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
294 * stmmac_eee_init: init EEE
295 * @priv: driver private structure
297 * If the EEE support has been enabled while configuring the driver,
298 * if the GMAC actually supports the EEE (from the HW cap reg) and the
299 * phy can also manage EEE, so enable the LPI state and start the timer
300 * to verify if the tx path can enter in LPI state.
302 bool stmmac_eee_init(struct stmmac_priv *priv)
306 /* MAC core supports the EEE feature. */
307 if (priv->dma_cap.eee) {
308 /* Check if the PHY supports EEE */
309 if (phy_init_eee(priv->phydev, 1))
312 priv->eee_active = 1;
313 init_timer(&priv->eee_ctrl_timer);
314 priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
315 priv->eee_ctrl_timer.data = (unsigned long)priv;
316 priv->eee_ctrl_timer.expires = STMMAC_LPI_TIMER(eee_timer);
317 add_timer(&priv->eee_ctrl_timer);
319 priv->hw->mac->set_eee_timer(priv->ioaddr,
320 STMMAC_DEFAULT_LIT_LS_TIMER,
323 pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
332 * stmmac_eee_adjust: adjust HW EEE according to the speed
333 * @priv: driver private structure
335 * When the EEE has been already initialised we have to
336 * modify the PLS bit in the LPI ctrl & status reg according
337 * to the PHY link status. For this reason.
339 static void stmmac_eee_adjust(struct stmmac_priv *priv)
341 if (priv->eee_enabled)
342 priv->hw->mac->set_eee_pls(priv->ioaddr, priv->phydev->link);
345 /* stmmac_get_tx_hwtstamp: get HW TX timestamps
346 * @priv: driver private structure
347 * @entry : descriptor index to be used.
348 * @skb : the socket buffer
350 * This function will read timestamp from the descriptor & pass it to stack.
351 * and also perform some sanity checks.
353 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv,
357 struct skb_shared_hwtstamps shhwtstamp;
361 if (!priv->hwts_tx_en)
364 /* if skb doesn't support hw tstamp */
365 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
369 desc = (priv->dma_etx + entry);
371 desc = (priv->dma_tx + entry);
373 /* check tx tstamp status */
374 if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
377 /* get the valid tstamp */
378 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
380 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
381 shhwtstamp.hwtstamp = ns_to_ktime(ns);
382 /* pass tstamp to stack */
383 skb_tstamp_tx(skb, &shhwtstamp);
388 /* stmmac_get_rx_hwtstamp: get HW RX timestamps
389 * @priv: driver private structure
390 * @entry : descriptor index to be used.
391 * @skb : the socket buffer
393 * This function will read received packet's timestamp from the descriptor
394 * and pass it to stack. It also perform some sanity checks.
396 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
400 struct skb_shared_hwtstamps *shhwtstamp = NULL;
404 if (!priv->hwts_rx_en)
408 desc = (priv->dma_erx + entry);
410 desc = (priv->dma_rx + entry);
412 /* if rx tstamp is not valid */
413 if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
416 /* get valid tstamp */
417 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
418 shhwtstamp = skb_hwtstamps(skb);
419 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
420 shhwtstamp->hwtstamp = ns_to_ktime(ns);
424 * stmmac_hwtstamp_ioctl - control hardware timestamping.
425 * @dev: device pointer.
426 * @ifr: An IOCTL specefic structure, that can contain a pointer to
427 * a proprietary structure used to pass information to the driver.
429 * This function configures the MAC to enable/disable both outgoing(TX)
430 * and incoming(RX) packets time stamping based on user input.
432 * 0 on success and an appropriate -ve integer on failure.
434 static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
436 struct stmmac_priv *priv = netdev_priv(dev);
437 struct hwtstamp_config config;
442 u32 ptp_over_ipv4_udp = 0;
443 u32 ptp_over_ipv6_udp = 0;
444 u32 ptp_over_ethernet = 0;
445 u32 snap_type_sel = 0;
446 u32 ts_master_en = 0;
450 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
451 netdev_alert(priv->dev, "No support for HW time stamping\n");
452 priv->hwts_tx_en = 0;
453 priv->hwts_rx_en = 0;
458 if (copy_from_user(&config, ifr->ifr_data,
459 sizeof(struct hwtstamp_config)))
462 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
463 __func__, config.flags, config.tx_type, config.rx_filter);
465 /* reserved for future extensions */
469 switch (config.tx_type) {
470 case HWTSTAMP_TX_OFF:
471 priv->hwts_tx_en = 0;
474 priv->hwts_tx_en = 1;
481 switch (config.rx_filter) {
482 /* time stamp no incoming packet at all */
483 case HWTSTAMP_FILTER_NONE:
484 config.rx_filter = HWTSTAMP_FILTER_NONE;
487 /* PTP v1, UDP, any kind of event packet */
488 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
489 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
490 /* take time stamp for all event messages */
491 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
493 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
494 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
497 /* PTP v1, UDP, Sync packet */
498 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
499 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
500 /* take time stamp for SYNC messages only */
501 ts_event_en = PTP_TCR_TSEVNTENA;
503 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
504 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
507 /* PTP v1, UDP, Delay_req packet */
508 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
509 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
510 /* take time stamp for Delay_Req messages only */
511 ts_master_en = PTP_TCR_TSMSTRENA;
512 ts_event_en = PTP_TCR_TSEVNTENA;
514 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
515 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
518 /* PTP v2, UDP, any kind of event packet */
519 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
520 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
521 ptp_v2 = PTP_TCR_TSVER2ENA;
522 /* take time stamp for all event messages */
523 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
525 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
526 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
529 /* PTP v2, UDP, Sync packet */
530 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
531 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
532 ptp_v2 = PTP_TCR_TSVER2ENA;
533 /* take time stamp for SYNC messages only */
534 ts_event_en = PTP_TCR_TSEVNTENA;
536 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
537 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
540 /* PTP v2, UDP, Delay_req packet */
541 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
542 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
543 ptp_v2 = PTP_TCR_TSVER2ENA;
544 /* take time stamp for Delay_Req messages only */
545 ts_master_en = PTP_TCR_TSMSTRENA;
546 ts_event_en = PTP_TCR_TSEVNTENA;
548 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
549 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
552 /* PTP v2/802.AS1, any layer, any kind of event packet */
553 case HWTSTAMP_FILTER_PTP_V2_EVENT:
554 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
555 ptp_v2 = PTP_TCR_TSVER2ENA;
556 /* take time stamp for all event messages */
557 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
559 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
560 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
561 ptp_over_ethernet = PTP_TCR_TSIPENA;
564 /* PTP v2/802.AS1, any layer, Sync packet */
565 case HWTSTAMP_FILTER_PTP_V2_SYNC:
566 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
567 ptp_v2 = PTP_TCR_TSVER2ENA;
568 /* take time stamp for SYNC messages only */
569 ts_event_en = PTP_TCR_TSEVNTENA;
571 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
572 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
573 ptp_over_ethernet = PTP_TCR_TSIPENA;
576 /* PTP v2/802.AS1, any layer, Delay_req packet */
577 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
578 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
579 ptp_v2 = PTP_TCR_TSVER2ENA;
580 /* take time stamp for Delay_Req messages only */
581 ts_master_en = PTP_TCR_TSMSTRENA;
582 ts_event_en = PTP_TCR_TSEVNTENA;
584 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
585 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
586 ptp_over_ethernet = PTP_TCR_TSIPENA;
589 /* time stamp any incoming packet */
590 case HWTSTAMP_FILTER_ALL:
591 config.rx_filter = HWTSTAMP_FILTER_ALL;
592 tstamp_all = PTP_TCR_TSENALL;
599 switch (config.rx_filter) {
600 case HWTSTAMP_FILTER_NONE:
601 config.rx_filter = HWTSTAMP_FILTER_NONE;
604 /* PTP v1, UDP, any kind of event packet */
605 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
609 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
611 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
612 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 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);
619 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
621 /* program Sub Second Increment reg */
622 priv->hw->ptp->config_sub_second_increment(priv->ioaddr);
624 /* calculate default added value:
626 * addend = (2^32)/freq_div_ratio;
627 * where, freq_div_ratio = STMMAC_SYSCLOCK/50MHz
628 * hence, addend = ((2^32) * 50MHz)/STMMAC_SYSCLOCK;
629 * NOTE: STMMAC_SYSCLOCK should be >= 50MHz to
630 * achive 20ns accuracy.
632 * 2^x * y == (y << x), hence
633 * 2^32 * 50000000 ==> (50000000 << 32)
635 temp = (u64)(50000000ULL << 32);
636 priv->default_addend = div_u64(temp, STMMAC_SYSCLOCK);
637 priv->hw->ptp->config_addend(priv->ioaddr,
638 priv->default_addend);
640 /* initialize system time */
641 getnstimeofday(&now);
642 priv->hw->ptp->init_systime(priv->ioaddr, now.tv_sec,
646 return copy_to_user(ifr->ifr_data, &config,
647 sizeof(struct hwtstamp_config)) ? -EFAULT : 0;
651 * stmmac_init_ptp: init PTP
652 * @priv: driver private structure
653 * Description: this is to verify if the HW supports the PTPv1 or v2.
654 * This is done by looking at the HW cap. register.
655 * Also it registers the ptp driver.
657 static int stmmac_init_ptp(struct stmmac_priv *priv)
659 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
662 if (netif_msg_hw(priv)) {
663 if (priv->dma_cap.time_stamp) {
664 pr_debug("IEEE 1588-2002 Time Stamp supported\n");
667 if (priv->dma_cap.atime_stamp && priv->extend_desc) {
668 pr_debug("IEEE 1588-2008 Advanced Time Stamp supported\n");
673 priv->hw->ptp = &stmmac_ptp;
674 priv->hwts_tx_en = 0;
675 priv->hwts_rx_en = 0;
677 return stmmac_ptp_register(priv);
680 static void stmmac_release_ptp(struct stmmac_priv *priv)
682 stmmac_ptp_unregister(priv);
687 * @dev: net device structure
688 * Description: it adjusts the link parameters.
690 static void stmmac_adjust_link(struct net_device *dev)
692 struct stmmac_priv *priv = netdev_priv(dev);
693 struct phy_device *phydev = priv->phydev;
696 unsigned int fc = priv->flow_ctrl, pause_time = priv->pause;
701 DBG(probe, DEBUG, "stmmac_adjust_link: called. address %d link %d\n",
702 phydev->addr, phydev->link);
704 spin_lock_irqsave(&priv->lock, flags);
707 u32 ctrl = readl(priv->ioaddr + MAC_CTRL_REG);
709 /* Now we make sure that we can be in full duplex mode.
710 * If not, we operate in half-duplex mode. */
711 if (phydev->duplex != priv->oldduplex) {
713 if (!(phydev->duplex))
714 ctrl &= ~priv->hw->link.duplex;
716 ctrl |= priv->hw->link.duplex;
717 priv->oldduplex = phydev->duplex;
719 /* Flow Control operation */
721 priv->hw->mac->flow_ctrl(priv->ioaddr, phydev->duplex,
724 if (phydev->speed != priv->speed) {
726 switch (phydev->speed) {
728 if (likely(priv->plat->has_gmac))
729 ctrl &= ~priv->hw->link.port;
730 stmmac_hw_fix_mac_speed(priv);
734 if (priv->plat->has_gmac) {
735 ctrl |= priv->hw->link.port;
736 if (phydev->speed == SPEED_100) {
737 ctrl |= priv->hw->link.speed;
739 ctrl &= ~(priv->hw->link.speed);
742 ctrl &= ~priv->hw->link.port;
744 stmmac_hw_fix_mac_speed(priv);
747 if (netif_msg_link(priv))
748 pr_warning("%s: Speed (%d) is not 10"
749 " or 100!\n", dev->name, phydev->speed);
753 priv->speed = phydev->speed;
756 writel(ctrl, priv->ioaddr + MAC_CTRL_REG);
758 if (!priv->oldlink) {
762 } else if (priv->oldlink) {
766 priv->oldduplex = -1;
769 if (new_state && netif_msg_link(priv))
770 phy_print_status(phydev);
772 stmmac_eee_adjust(priv);
774 spin_unlock_irqrestore(&priv->lock, flags);
776 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
780 * stmmac_check_pcs_mode: verify if RGMII/SGMII is supported
781 * @priv: driver private structure
782 * Description: this is to verify if the HW supports the PCS.
783 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
784 * configured for the TBI, RTBI, or SGMII PHY interface.
786 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
788 int interface = priv->plat->interface;
790 if (priv->dma_cap.pcs) {
791 if ((interface & PHY_INTERFACE_MODE_RGMII) ||
792 (interface & PHY_INTERFACE_MODE_RGMII_ID) ||
793 (interface & PHY_INTERFACE_MODE_RGMII_RXID) ||
794 (interface & PHY_INTERFACE_MODE_RGMII_TXID)) {
795 pr_debug("STMMAC: PCS RGMII support enable\n");
796 priv->pcs = STMMAC_PCS_RGMII;
797 } else if (interface & PHY_INTERFACE_MODE_SGMII) {
798 pr_debug("STMMAC: PCS SGMII support enable\n");
799 priv->pcs = STMMAC_PCS_SGMII;
805 * stmmac_init_phy - PHY initialization
806 * @dev: net device structure
807 * Description: it initializes the driver's PHY state, and attaches the PHY
812 static int stmmac_init_phy(struct net_device *dev)
814 struct stmmac_priv *priv = netdev_priv(dev);
815 struct phy_device *phydev;
816 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
817 char bus_id[MII_BUS_ID_SIZE];
818 int interface = priv->plat->interface;
821 priv->oldduplex = -1;
823 if (priv->plat->phy_bus_name)
824 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
825 priv->plat->phy_bus_name, priv->plat->bus_id);
827 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
830 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
831 priv->plat->phy_addr);
832 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
834 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
836 if (IS_ERR(phydev)) {
837 pr_err("%s: Could not attach to PHY\n", dev->name);
838 return PTR_ERR(phydev);
841 /* Stop Advertising 1000BASE Capability if interface is not GMII */
842 if ((interface == PHY_INTERFACE_MODE_MII) ||
843 (interface == PHY_INTERFACE_MODE_RMII))
844 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
845 SUPPORTED_1000baseT_Full);
848 * Broken HW is sometimes missing the pull-up resistor on the
849 * MDIO line, which results in reads to non-existent devices returning
850 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
852 * Note: phydev->phy_id is the result of reading the UID PHY registers.
854 if (phydev->phy_id == 0) {
855 phy_disconnect(phydev);
858 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
859 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
861 priv->phydev = phydev;
867 * stmmac_display_ring: display ring
868 * @head: pointer to the head of the ring passed.
869 * @size: size of the ring.
870 * @extend_desc: to verify if extended descriptors are used.
871 * Description: display the control/status and buffer descriptors.
873 static void stmmac_display_ring(void *head, int size, int extend_desc)
876 struct dma_extended_desc *ep = (struct dma_extended_desc *) head;
877 struct dma_desc *p = (struct dma_desc *) head;
879 for (i = 0; i < size; i++) {
883 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
884 i, (unsigned int) virt_to_phys(ep),
885 (unsigned int) x, (unsigned int) (x >> 32),
886 ep->basic.des2, ep->basic.des3);
890 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
891 i, (unsigned int) virt_to_phys(p),
892 (unsigned int) x, (unsigned int) (x >> 32),
900 static void stmmac_display_rings(struct stmmac_priv *priv)
902 unsigned int txsize = priv->dma_tx_size;
903 unsigned int rxsize = priv->dma_rx_size;
905 if (priv->extend_desc) {
906 pr_info("Extended RX descriptor ring:\n");
907 stmmac_display_ring((void *) priv->dma_erx, rxsize, 1);
908 pr_info("Extended TX descriptor ring:\n");
909 stmmac_display_ring((void *) priv->dma_etx, txsize, 1);
911 pr_info("RX descriptor ring:\n");
912 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
913 pr_info("TX descriptor ring:\n");
914 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
918 static int stmmac_set_bfsize(int mtu, int bufsize)
922 if (mtu >= BUF_SIZE_4KiB)
924 else if (mtu >= BUF_SIZE_2KiB)
926 else if (mtu >= DMA_BUFFER_SIZE)
929 ret = DMA_BUFFER_SIZE;
935 * stmmac_clear_descriptors: clear descriptors
936 * @priv: driver private structure
937 * Description: this function is called to clear the tx and rx descriptors
938 * in case of both basic and extended descriptors are used.
940 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
943 unsigned int txsize = priv->dma_tx_size;
944 unsigned int rxsize = priv->dma_rx_size;
946 /* Clear the Rx/Tx descriptors */
947 for (i = 0; i < rxsize; i++)
948 if (priv->extend_desc)
949 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
950 priv->use_riwt, priv->mode,
953 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
954 priv->use_riwt, priv->mode,
956 for (i = 0; i < txsize; i++)
957 if (priv->extend_desc)
958 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
962 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
967 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
972 skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
974 if (unlikely(skb == NULL)) {
975 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
978 skb_reserve(skb, NET_IP_ALIGN);
979 priv->rx_skbuff[i] = skb;
980 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
984 p->des2 = priv->rx_skbuff_dma[i];
986 if ((priv->mode == STMMAC_RING_MODE) &&
987 (priv->dma_buf_sz == BUF_SIZE_16KiB))
988 priv->hw->ring->init_desc3(p);
994 * init_dma_desc_rings - init the RX/TX descriptor rings
995 * @dev: net device structure
996 * Description: this function initializes the DMA RX/TX descriptors
997 * and allocates the socket buffers. It suppors the chained and ring
1000 static void init_dma_desc_rings(struct net_device *dev)
1003 struct stmmac_priv *priv = netdev_priv(dev);
1004 unsigned int txsize = priv->dma_tx_size;
1005 unsigned int rxsize = priv->dma_rx_size;
1006 unsigned int bfsize = 0;
1008 /* Set the max buffer size according to the DESC mode
1009 * and the MTU. Note that RING mode allows 16KiB bsize. */
1010 if (priv->mode == STMMAC_RING_MODE)
1011 bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
1013 if (bfsize < BUF_SIZE_16KiB)
1014 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
1016 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
1017 txsize, rxsize, bfsize);
1019 if (priv->extend_desc) {
1020 priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
1025 priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
1030 if ((!priv->dma_erx) || (!priv->dma_etx))
1033 priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
1034 sizeof(struct dma_desc),
1037 priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
1038 sizeof(struct dma_desc),
1041 if ((!priv->dma_rx) || (!priv->dma_tx))
1045 priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
1047 priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
1049 priv->tx_skbuff_dma = kmalloc_array(txsize, sizeof(dma_addr_t),
1051 priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
1053 if (netif_msg_drv(priv))
1054 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1055 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
1057 /* RX INITIALIZATION */
1058 DBG(probe, INFO, "stmmac: SKB addresses:\nskb\t\tskb data\tdma data\n");
1059 for (i = 0; i < rxsize; i++) {
1061 if (priv->extend_desc)
1062 p = &((priv->dma_erx + i)->basic);
1064 p = priv->dma_rx + i;
1066 if (stmmac_init_rx_buffers(priv, p, i))
1069 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1070 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
1073 priv->dirty_rx = (unsigned int)(i - rxsize);
1074 priv->dma_buf_sz = bfsize;
1077 /* Setup the chained descriptor addresses */
1078 if (priv->mode == STMMAC_CHAIN_MODE) {
1079 if (priv->extend_desc) {
1080 priv->hw->chain->init(priv->dma_erx, priv->dma_rx_phy,
1082 priv->hw->chain->init(priv->dma_etx, priv->dma_tx_phy,
1085 priv->hw->chain->init(priv->dma_rx, priv->dma_rx_phy,
1087 priv->hw->chain->init(priv->dma_tx, priv->dma_tx_phy,
1092 /* TX INITIALIZATION */
1093 for (i = 0; i < txsize; i++) {
1095 if (priv->extend_desc)
1096 p = &((priv->dma_etx + i)->basic);
1098 p = priv->dma_tx + i;
1100 priv->tx_skbuff_dma[i] = 0;
1101 priv->tx_skbuff[i] = NULL;
1107 stmmac_clear_descriptors(priv);
1109 if (netif_msg_hw(priv))
1110 stmmac_display_rings(priv);
1113 static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1117 for (i = 0; i < priv->dma_rx_size; i++) {
1118 if (priv->rx_skbuff[i]) {
1119 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
1120 priv->dma_buf_sz, DMA_FROM_DEVICE);
1121 dev_kfree_skb_any(priv->rx_skbuff[i]);
1123 priv->rx_skbuff[i] = NULL;
1127 static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1131 for (i = 0; i < priv->dma_tx_size; i++) {
1132 if (priv->tx_skbuff[i] != NULL) {
1134 if (priv->extend_desc)
1135 p = &((priv->dma_etx + i)->basic);
1137 p = priv->dma_tx + i;
1139 if (priv->tx_skbuff_dma[i])
1140 dma_unmap_single(priv->device,
1141 priv->tx_skbuff_dma[i],
1142 priv->hw->desc->get_tx_len(p),
1144 dev_kfree_skb_any(priv->tx_skbuff[i]);
1145 priv->tx_skbuff[i] = NULL;
1146 priv->tx_skbuff_dma[i] = 0;
1151 static void free_dma_desc_resources(struct stmmac_priv *priv)
1153 /* Release the DMA TX/RX socket buffers */
1154 dma_free_rx_skbufs(priv);
1155 dma_free_tx_skbufs(priv);
1157 /* Free the region of consistent memory previously allocated for
1159 if (!priv->extend_desc) {
1160 dma_free_coherent(priv->device,
1161 priv->dma_tx_size * sizeof(struct dma_desc),
1162 priv->dma_tx, priv->dma_tx_phy);
1163 dma_free_coherent(priv->device,
1164 priv->dma_rx_size * sizeof(struct dma_desc),
1165 priv->dma_rx, priv->dma_rx_phy);
1167 dma_free_coherent(priv->device, priv->dma_tx_size *
1168 sizeof(struct dma_extended_desc),
1169 priv->dma_etx, priv->dma_tx_phy);
1170 dma_free_coherent(priv->device, priv->dma_rx_size *
1171 sizeof(struct dma_extended_desc),
1172 priv->dma_erx, priv->dma_rx_phy);
1174 kfree(priv->rx_skbuff_dma);
1175 kfree(priv->rx_skbuff);
1176 kfree(priv->tx_skbuff_dma);
1177 kfree(priv->tx_skbuff);
1181 * stmmac_dma_operation_mode - HW DMA operation mode
1182 * @priv: driver private structure
1183 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
1184 * or Store-And-Forward capability.
1186 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1188 if (likely(priv->plat->force_sf_dma_mode ||
1189 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
1191 * In case of GMAC, SF mode can be enabled
1192 * to perform the TX COE in HW. This depends on:
1193 * 1) TX COE if actually supported
1194 * 2) There is no bugged Jumbo frame support
1195 * that needs to not insert csum in the TDES.
1197 priv->hw->dma->dma_mode(priv->ioaddr,
1198 SF_DMA_MODE, SF_DMA_MODE);
1201 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
1206 * @priv: driver private structure
1207 * Description: it reclaims resources after transmission completes.
1209 static void stmmac_tx_clean(struct stmmac_priv *priv)
1211 unsigned int txsize = priv->dma_tx_size;
1213 spin_lock(&priv->tx_lock);
1215 priv->xstats.tx_clean++;
1217 while (priv->dirty_tx != priv->cur_tx) {
1219 unsigned int entry = priv->dirty_tx % txsize;
1220 struct sk_buff *skb = priv->tx_skbuff[entry];
1223 if (priv->extend_desc)
1224 p = (struct dma_desc *) (priv->dma_etx + entry);
1226 p = priv->dma_tx + entry;
1228 /* Check if the descriptor is owned by the DMA. */
1229 if (priv->hw->desc->get_tx_owner(p))
1232 /* Verify tx error by looking at the last segment. */
1233 last = priv->hw->desc->get_tx_ls(p);
1236 priv->hw->desc->tx_status(&priv->dev->stats,
1239 if (likely(tx_error == 0)) {
1240 priv->dev->stats.tx_packets++;
1241 priv->xstats.tx_pkt_n++;
1243 priv->dev->stats.tx_errors++;
1245 stmmac_get_tx_hwtstamp(priv, entry, skb);
1247 TX_DBG("%s: curr %d, dirty %d\n", __func__,
1248 priv->cur_tx, priv->dirty_tx);
1250 if (likely(priv->tx_skbuff_dma[entry])) {
1251 dma_unmap_single(priv->device,
1252 priv->tx_skbuff_dma[entry],
1253 priv->hw->desc->get_tx_len(p),
1255 priv->tx_skbuff_dma[entry] = 0;
1257 priv->hw->ring->clean_desc3(priv, p);
1259 if (likely(skb != NULL)) {
1261 priv->tx_skbuff[entry] = NULL;
1264 priv->hw->desc->release_tx_desc(p, priv->mode);
1268 if (unlikely(netif_queue_stopped(priv->dev) &&
1269 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
1270 netif_tx_lock(priv->dev);
1271 if (netif_queue_stopped(priv->dev) &&
1272 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
1273 TX_DBG("%s: restart transmit\n", __func__);
1274 netif_wake_queue(priv->dev);
1276 netif_tx_unlock(priv->dev);
1279 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1280 stmmac_enable_eee_mode(priv);
1281 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_TIMER(eee_timer));
1283 spin_unlock(&priv->tx_lock);
1286 static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
1288 priv->hw->dma->enable_dma_irq(priv->ioaddr);
1291 static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
1293 priv->hw->dma->disable_dma_irq(priv->ioaddr);
1298 * stmmac_tx_err: irq tx error mng function
1299 * @priv: driver private structure
1300 * Description: it cleans the descriptors and restarts the transmission
1301 * in case of errors.
1303 static void stmmac_tx_err(struct stmmac_priv *priv)
1306 int txsize = priv->dma_tx_size;
1307 netif_stop_queue(priv->dev);
1309 priv->hw->dma->stop_tx(priv->ioaddr);
1310 dma_free_tx_skbufs(priv);
1311 for (i = 0; i < txsize; i++)
1312 if (priv->extend_desc)
1313 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1317 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1322 priv->hw->dma->start_tx(priv->ioaddr);
1324 priv->dev->stats.tx_errors++;
1325 netif_wake_queue(priv->dev);
1329 * stmmac_dma_interrupt: DMA ISR
1330 * @priv: driver private structure
1331 * Description: this is the DMA ISR. It is called by the main ISR.
1332 * It calls the dwmac dma routine to understand which type of interrupt
1333 * happened. In case of there is a Normal interrupt and either TX or RX
1334 * interrupt happened so the NAPI is scheduled.
1336 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
1340 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
1341 if (likely((status & handle_rx)) || (status & handle_tx)) {
1342 if (likely(napi_schedule_prep(&priv->napi))) {
1343 stmmac_disable_dma_irq(priv);
1344 __napi_schedule(&priv->napi);
1347 if (unlikely(status & tx_hard_error_bump_tc)) {
1348 /* Try to bump up the dma threshold on this failure */
1349 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
1351 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
1352 priv->xstats.threshold = tc;
1354 } else if (unlikely(status == tx_hard_error))
1355 stmmac_tx_err(priv);
1359 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1360 * @priv: driver private structure
1361 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1363 static void stmmac_mmc_setup(struct stmmac_priv *priv)
1365 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
1366 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
1368 dwmac_mmc_intr_all_mask(priv->ioaddr);
1370 if (priv->dma_cap.rmon) {
1371 dwmac_mmc_ctrl(priv->ioaddr, mode);
1372 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1374 pr_info(" No MAC Management Counters available\n");
1377 static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
1379 u32 hwid = priv->hw->synopsys_uid;
1381 /* Only check valid Synopsys Id because old MAC chips
1382 * have no HW registers where get the ID */
1384 u32 uid = ((hwid & 0x0000ff00) >> 8);
1385 u32 synid = (hwid & 0x000000ff);
1387 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
1396 * stmmac_selec_desc_mode: to select among: normal/alternate/extend descriptors
1397 * @priv: driver private structure
1398 * Description: select the Enhanced/Alternate or Normal descriptors.
1399 * In case of Enhanced/Alternate, it looks at the extended descriptors are
1400 * supported by the HW cap. register.
1402 static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1404 if (priv->plat->enh_desc) {
1405 pr_info(" Enhanced/Alternate descriptors\n");
1407 /* GMAC older than 3.50 has no extended descriptors */
1408 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1409 pr_info("\tEnabled extended descriptors\n");
1410 priv->extend_desc = 1;
1412 pr_warn("Extended descriptors not supported\n");
1414 priv->hw->desc = &enh_desc_ops;
1416 pr_info(" Normal descriptors\n");
1417 priv->hw->desc = &ndesc_ops;
1422 * stmmac_get_hw_features: get MAC capabilities from the HW cap. register.
1423 * @priv: driver private structure
1425 * new GMAC chip generations have a new register to indicate the
1426 * presence of the optional feature/functions.
1427 * This can be also used to override the value passed through the
1428 * platform and necessary for old MAC10/100 and GMAC chips.
1430 static int stmmac_get_hw_features(struct stmmac_priv *priv)
1434 if (priv->hw->dma->get_hw_feature) {
1435 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
1437 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
1438 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
1439 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
1440 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
1441 priv->dma_cap.multi_addr =
1442 (hw_cap & DMA_HW_FEAT_ADDMACADRSEL) >> 5;
1443 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
1444 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
1445 priv->dma_cap.pmt_remote_wake_up =
1446 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
1447 priv->dma_cap.pmt_magic_frame =
1448 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
1450 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
1452 priv->dma_cap.time_stamp =
1453 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
1455 priv->dma_cap.atime_stamp =
1456 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
1457 /* 802.3az - Energy-Efficient Ethernet (EEE) */
1458 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
1459 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
1460 /* TX and RX csum */
1461 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
1462 priv->dma_cap.rx_coe_type1 =
1463 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
1464 priv->dma_cap.rx_coe_type2 =
1465 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
1466 priv->dma_cap.rxfifo_over_2048 =
1467 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
1468 /* TX and RX number of channels */
1469 priv->dma_cap.number_rx_channel =
1470 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
1471 priv->dma_cap.number_tx_channel =
1472 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
1473 /* Alternate (enhanced) DESC mode*/
1474 priv->dma_cap.enh_desc =
1475 (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
1482 * stmmac_check_ether_addr: check if the MAC addr is valid
1483 * @priv: driver private structure
1485 * it is to verify if the MAC address is valid, in case of failures it
1486 * generates a random MAC address
1488 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1490 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
1491 priv->hw->mac->get_umac_addr((void __iomem *)
1492 priv->dev->base_addr,
1493 priv->dev->dev_addr, 0);
1494 if (!is_valid_ether_addr(priv->dev->dev_addr))
1495 eth_hw_addr_random(priv->dev);
1497 pr_warning("%s: device MAC address %pM\n", priv->dev->name,
1498 priv->dev->dev_addr);
1502 * stmmac_init_dma_engine: DMA init.
1503 * @priv: driver private structure
1505 * It inits the DMA invoking the specific MAC/GMAC callback.
1506 * Some DMA parameters can be passed from the platform;
1507 * in case of these are not passed a default is kept for the MAC or GMAC.
1509 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1511 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
1512 int mixed_burst = 0;
1515 if (priv->plat->dma_cfg) {
1516 pbl = priv->plat->dma_cfg->pbl;
1517 fixed_burst = priv->plat->dma_cfg->fixed_burst;
1518 mixed_burst = priv->plat->dma_cfg->mixed_burst;
1519 burst_len = priv->plat->dma_cfg->burst_len;
1522 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1525 return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
1526 burst_len, priv->dma_tx_phy,
1527 priv->dma_rx_phy, atds);
1531 * stmmac_tx_timer: mitigation sw timer for tx.
1532 * @data: data pointer
1534 * This is the timer handler to directly invoke the stmmac_tx_clean.
1536 static void stmmac_tx_timer(unsigned long data)
1538 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1540 stmmac_tx_clean(priv);
1544 * stmmac_init_tx_coalesce: init tx mitigation options.
1545 * @priv: driver private structure
1547 * This inits the transmit coalesce parameters: i.e. timer rate,
1548 * timer handler and default threshold used for enabling the
1549 * interrupt on completion bit.
1551 static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1553 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1554 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1555 init_timer(&priv->txtimer);
1556 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1557 priv->txtimer.data = (unsigned long)priv;
1558 priv->txtimer.function = stmmac_tx_timer;
1559 add_timer(&priv->txtimer);
1563 * stmmac_open - open entry point of the driver
1564 * @dev : pointer to the device structure.
1566 * This function is the open entry point of the driver.
1568 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1571 static int stmmac_open(struct net_device *dev)
1573 struct stmmac_priv *priv = netdev_priv(dev);
1576 clk_prepare_enable(priv->stmmac_clk);
1578 stmmac_check_ether_addr(priv);
1580 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1581 priv->pcs != STMMAC_PCS_RTBI) {
1582 ret = stmmac_init_phy(dev);
1584 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1590 /* Create and initialize the TX/RX descriptors chains. */
1591 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1592 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1593 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
1594 init_dma_desc_rings(dev);
1596 /* DMA initialization and SW reset */
1597 ret = stmmac_init_dma_engine(priv);
1599 pr_err("%s: DMA initialization failed\n", __func__);
1603 /* Copy the MAC addr into the HW */
1604 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
1606 /* If required, perform hw setup of the bus. */
1607 if (priv->plat->bus_setup)
1608 priv->plat->bus_setup(priv->ioaddr);
1610 /* Initialize the MAC Core */
1611 priv->hw->mac->core_init(priv->ioaddr);
1613 /* Request the IRQ lines */
1614 ret = request_irq(dev->irq, stmmac_interrupt,
1615 IRQF_SHARED, dev->name, dev);
1616 if (unlikely(ret < 0)) {
1617 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1618 __func__, dev->irq, ret);
1622 /* Request the Wake IRQ in case of another line is used for WoL */
1623 if (priv->wol_irq != dev->irq) {
1624 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1625 IRQF_SHARED, dev->name, dev);
1626 if (unlikely(ret < 0)) {
1627 pr_err("%s: ERROR: allocating the ext WoL IRQ %d "
1628 "(error: %d)\n", __func__, priv->wol_irq, ret);
1629 goto open_error_wolirq;
1633 /* Request the IRQ lines */
1634 if (priv->lpi_irq != -ENXIO) {
1635 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1637 if (unlikely(ret < 0)) {
1638 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1639 __func__, priv->lpi_irq, ret);
1640 goto open_error_lpiirq;
1644 /* Enable the MAC Rx/Tx */
1645 stmmac_set_mac(priv->ioaddr, true);
1647 /* Set the HW DMA mode and the COE */
1648 stmmac_dma_operation_mode(priv);
1650 /* Extra statistics */
1651 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1652 priv->xstats.threshold = tc;
1654 stmmac_mmc_setup(priv);
1656 ret = stmmac_init_ptp(priv);
1658 pr_warn("%s: failed PTP initialisation\n", __func__);
1660 #ifdef CONFIG_STMMAC_DEBUG_FS
1661 ret = stmmac_init_fs(dev);
1663 pr_warning("%s: failed debugFS registration\n", __func__);
1665 /* Start the ball rolling... */
1666 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
1667 priv->hw->dma->start_tx(priv->ioaddr);
1668 priv->hw->dma->start_rx(priv->ioaddr);
1670 /* Dump DMA/MAC registers */
1671 if (netif_msg_hw(priv)) {
1672 priv->hw->mac->dump_regs(priv->ioaddr);
1673 priv->hw->dma->dump_regs(priv->ioaddr);
1677 phy_start(priv->phydev);
1679 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS_TIMER;
1681 /* Using PCS we cannot dial with the phy registers at this stage
1682 * so we do not support extra feature like EEE.
1684 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
1685 priv->pcs != STMMAC_PCS_RTBI)
1686 priv->eee_enabled = stmmac_eee_init(priv);
1688 stmmac_init_tx_coalesce(priv);
1690 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1691 priv->rx_riwt = MAX_DMA_RIWT;
1692 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1695 if (priv->pcs && priv->hw->mac->ctrl_ane)
1696 priv->hw->mac->ctrl_ane(priv->ioaddr, 0);
1698 napi_enable(&priv->napi);
1699 netif_start_queue(dev);
1704 if (priv->wol_irq != dev->irq)
1705 free_irq(priv->wol_irq, dev);
1708 free_irq(dev->irq, dev);
1712 phy_disconnect(priv->phydev);
1714 clk_disable_unprepare(priv->stmmac_clk);
1720 * stmmac_release - close entry point of the driver
1721 * @dev : device pointer.
1723 * This is the stop entry point of the driver.
1725 static int stmmac_release(struct net_device *dev)
1727 struct stmmac_priv *priv = netdev_priv(dev);
1729 if (priv->eee_enabled)
1730 del_timer_sync(&priv->eee_ctrl_timer);
1732 /* Stop and disconnect the PHY */
1734 phy_stop(priv->phydev);
1735 phy_disconnect(priv->phydev);
1736 priv->phydev = NULL;
1739 netif_stop_queue(dev);
1741 napi_disable(&priv->napi);
1743 del_timer_sync(&priv->txtimer);
1745 /* Free the IRQ lines */
1746 free_irq(dev->irq, dev);
1747 if (priv->wol_irq != dev->irq)
1748 free_irq(priv->wol_irq, dev);
1749 if (priv->lpi_irq != -ENXIO)
1750 free_irq(priv->lpi_irq, dev);
1752 /* Stop TX/RX DMA and clear the descriptors */
1753 priv->hw->dma->stop_tx(priv->ioaddr);
1754 priv->hw->dma->stop_rx(priv->ioaddr);
1756 /* Release and free the Rx/Tx resources */
1757 free_dma_desc_resources(priv);
1759 /* Disable the MAC Rx/Tx */
1760 stmmac_set_mac(priv->ioaddr, false);
1762 netif_carrier_off(dev);
1764 #ifdef CONFIG_STMMAC_DEBUG_FS
1767 clk_disable_unprepare(priv->stmmac_clk);
1769 stmmac_release_ptp(priv);
1775 * stmmac_xmit: Tx entry point of the driver
1776 * @skb : the socket buffer
1777 * @dev : device pointer
1778 * Description : this is the tx entry point of the driver.
1779 * It programs the chain or the ring and supports oversized frames
1782 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1784 struct stmmac_priv *priv = netdev_priv(dev);
1785 unsigned int txsize = priv->dma_tx_size;
1787 int i, csum_insertion = 0, is_jumbo = 0;
1788 int nfrags = skb_shinfo(skb)->nr_frags;
1789 struct dma_desc *desc, *first;
1790 unsigned int nopaged_len = skb_headlen(skb);
1792 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1793 if (!netif_queue_stopped(dev)) {
1794 netif_stop_queue(dev);
1795 /* This is a hard error, log it. */
1796 pr_err("%s: BUG! Tx Ring full when queue awake\n",
1799 return NETDEV_TX_BUSY;
1802 spin_lock(&priv->tx_lock);
1804 if (priv->tx_path_in_lpi_mode)
1805 stmmac_disable_eee_mode(priv);
1807 entry = priv->cur_tx % txsize;
1809 #ifdef STMMAC_XMIT_DEBUG
1810 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1811 pr_debug("stmmac xmit: [entry %d]\n"
1812 "\tskb addr %p - len: %d - nopaged_len: %d\n"
1813 "\tn_frags: %d - ip_summed: %d - %s gso\n"
1814 "\ttx_count_frames %d\n", entry,
1815 skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
1816 !skb_is_gso(skb) ? "isn't" : "is",
1817 priv->tx_count_frames);
1820 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
1822 if (priv->extend_desc)
1823 desc = (struct dma_desc *) (priv->dma_etx + entry);
1825 desc = priv->dma_tx + entry;
1829 #ifdef STMMAC_XMIT_DEBUG
1830 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1831 pr_debug("\tskb len: %d, nopaged_len: %d,\n"
1832 "\t\tn_frags: %d, ip_summed: %d\n",
1833 skb->len, nopaged_len, nfrags, skb->ip_summed);
1835 priv->tx_skbuff[entry] = skb;
1837 /* To program the descriptors according to the size of the frame */
1838 if (priv->mode == STMMAC_RING_MODE) {
1839 is_jumbo = priv->hw->ring->is_jumbo_frm(skb->len,
1840 priv->plat->enh_desc);
1841 if (unlikely(is_jumbo))
1842 entry = priv->hw->ring->jumbo_frm(priv, skb,
1845 is_jumbo = priv->hw->chain->is_jumbo_frm(skb->len,
1846 priv->plat->enh_desc);
1847 if (unlikely(is_jumbo))
1848 entry = priv->hw->chain->jumbo_frm(priv, skb,
1851 if (likely(!is_jumbo)) {
1852 desc->des2 = dma_map_single(priv->device, skb->data,
1853 nopaged_len, DMA_TO_DEVICE);
1854 priv->tx_skbuff_dma[entry] = desc->des2;
1855 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1856 csum_insertion, priv->mode);
1860 for (i = 0; i < nfrags; i++) {
1861 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1862 int len = skb_frag_size(frag);
1864 entry = (++priv->cur_tx) % txsize;
1865 if (priv->extend_desc)
1866 desc = (struct dma_desc *) (priv->dma_etx + entry);
1868 desc = priv->dma_tx + entry;
1870 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1871 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1873 priv->tx_skbuff_dma[entry] = desc->des2;
1874 priv->tx_skbuff[entry] = NULL;
1875 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
1878 priv->hw->desc->set_tx_owner(desc);
1882 /* Finalize the latest segment. */
1883 priv->hw->desc->close_tx_desc(desc);
1886 /* According to the coalesce parameter the IC bit for the latest
1887 * segment could be reset and the timer re-started to invoke the
1888 * stmmac_tx function. This approach takes care about the fragments.
1890 priv->tx_count_frames += nfrags + 1;
1891 if (priv->tx_coal_frames > priv->tx_count_frames) {
1892 priv->hw->desc->clear_tx_ic(desc);
1893 priv->xstats.tx_reset_ic_bit++;
1894 TX_DBG("\t[entry %d]: tx_count_frames %d\n", entry,
1895 priv->tx_count_frames);
1896 mod_timer(&priv->txtimer,
1897 STMMAC_COAL_TIMER(priv->tx_coal_timer));
1899 priv->tx_count_frames = 0;
1901 /* To avoid raise condition */
1902 priv->hw->desc->set_tx_owner(first);
1907 #ifdef STMMAC_XMIT_DEBUG
1908 if (netif_msg_pktdata(priv)) {
1909 pr_info("stmmac xmit: current=%d, dirty=%d, entry=%d, "
1910 "first=%p, nfrags=%d\n",
1911 (priv->cur_tx % txsize), (priv->dirty_tx % txsize),
1912 entry, first, nfrags);
1913 if (priv->extend_desc)
1914 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
1916 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
1918 pr_info(">>> frame to be transmitted: ");
1919 print_pkt(skb->data, skb->len);
1922 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
1923 TX_DBG("%s: stop transmitted packets\n", __func__);
1924 netif_stop_queue(dev);
1927 dev->stats.tx_bytes += skb->len;
1929 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
1930 priv->hwts_tx_en)) {
1931 /* declare that device is doing timestamping */
1932 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
1933 priv->hw->desc->enable_tx_timestamp(first);
1936 if (!priv->hwts_tx_en)
1937 skb_tx_timestamp(skb);
1939 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
1941 spin_unlock(&priv->tx_lock);
1943 return NETDEV_TX_OK;
1947 * stmmac_rx_refill: refill used skb preallocated buffers
1948 * @priv: driver private structure
1949 * Description : this is to reallocate the skb for the reception process
1950 * that is based on zero-copy.
1952 static inline void stmmac_rx_refill(struct stmmac_priv *priv)
1954 unsigned int rxsize = priv->dma_rx_size;
1955 int bfsize = priv->dma_buf_sz;
1957 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
1958 unsigned int entry = priv->dirty_rx % rxsize;
1961 if (priv->extend_desc)
1962 p = (struct dma_desc *) (priv->dma_erx + entry);
1964 p = priv->dma_rx + entry;
1966 if (likely(priv->rx_skbuff[entry] == NULL)) {
1967 struct sk_buff *skb;
1969 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
1971 if (unlikely(skb == NULL))
1974 priv->rx_skbuff[entry] = skb;
1975 priv->rx_skbuff_dma[entry] =
1976 dma_map_single(priv->device, skb->data, bfsize,
1979 p->des2 = priv->rx_skbuff_dma[entry];
1981 priv->hw->ring->refill_desc3(priv, p);
1983 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
1986 priv->hw->desc->set_rx_owner(p);
1992 * stmmac_rx_refill: refill used skb preallocated buffers
1993 * @priv: driver private structure
1994 * @limit: napi bugget.
1995 * Description : this the function called by the napi poll method.
1996 * It gets all the frames inside the ring.
1998 static int stmmac_rx(struct stmmac_priv *priv, int limit)
2000 unsigned int rxsize = priv->dma_rx_size;
2001 unsigned int entry = priv->cur_rx % rxsize;
2002 unsigned int next_entry;
2003 unsigned int count = 0;
2005 #ifdef STMMAC_RX_DEBUG
2006 if (netif_msg_hw(priv)) {
2007 pr_debug(">>> stmmac_rx: descriptor ring:\n");
2008 if (priv->extend_desc)
2009 stmmac_display_ring((void *) priv->dma_erx, rxsize, 1);
2011 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
2014 while (count < limit) {
2016 struct dma_desc *p, *p_next;
2018 if (priv->extend_desc)
2019 p = (struct dma_desc *) (priv->dma_erx + entry);
2021 p = priv->dma_rx + entry ;
2023 if (priv->hw->desc->get_rx_owner(p))
2028 next_entry = (++priv->cur_rx) % rxsize;
2029 if (priv->extend_desc)
2030 p_next = (struct dma_desc *) (priv->dma_erx +
2033 p_next = priv->dma_rx + next_entry;
2037 /* read the status of the incoming frame */
2038 status = priv->hw->desc->rx_status(&priv->dev->stats,
2040 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2041 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2045 if (unlikely(status == discard_frame)) {
2046 priv->dev->stats.rx_errors++;
2047 if (priv->hwts_rx_en && !priv->extend_desc) {
2048 /* DESC2 & DESC3 will be overwitten by device
2049 * with timestamp value, hence reinitialize
2050 * them in stmmac_rx_refill() function so that
2051 * device can reuse it.
2053 priv->rx_skbuff[entry] = NULL;
2054 dma_unmap_single(priv->device,
2055 priv->rx_skbuff_dma[entry],
2056 priv->dma_buf_sz, DMA_FROM_DEVICE);
2059 struct sk_buff *skb;
2062 frame_len = priv->hw->desc->get_rx_frame_len(p,
2063 priv->plat->rx_coe);
2064 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
2065 * Type frames (LLC/LLC-SNAP) */
2066 if (unlikely(status != llc_snap))
2067 frame_len -= ETH_FCS_LEN;
2068 #ifdef STMMAC_RX_DEBUG
2069 if (frame_len > ETH_FRAME_LEN)
2070 pr_debug("\tRX frame size %d, COE status: %d\n",
2073 if (netif_msg_hw(priv))
2074 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
2077 skb = priv->rx_skbuff[entry];
2078 if (unlikely(!skb)) {
2079 pr_err("%s: Inconsistent Rx descriptor chain\n",
2081 priv->dev->stats.rx_dropped++;
2084 prefetch(skb->data - NET_IP_ALIGN);
2085 priv->rx_skbuff[entry] = NULL;
2087 stmmac_get_rx_hwtstamp(priv, entry, skb);
2089 skb_put(skb, frame_len);
2090 dma_unmap_single(priv->device,
2091 priv->rx_skbuff_dma[entry],
2092 priv->dma_buf_sz, DMA_FROM_DEVICE);
2093 #ifdef STMMAC_RX_DEBUG
2094 if (netif_msg_pktdata(priv)) {
2095 pr_info(" frame received (%dbytes)", frame_len);
2096 print_pkt(skb->data, frame_len);
2099 skb->protocol = eth_type_trans(skb, priv->dev);
2101 if (unlikely(!priv->plat->rx_coe))
2102 skb_checksum_none_assert(skb);
2104 skb->ip_summed = CHECKSUM_UNNECESSARY;
2106 napi_gro_receive(&priv->napi, skb);
2108 priv->dev->stats.rx_packets++;
2109 priv->dev->stats.rx_bytes += frame_len;
2114 stmmac_rx_refill(priv);
2116 priv->xstats.rx_pkt_n += count;
2122 * stmmac_poll - stmmac poll method (NAPI)
2123 * @napi : pointer to the napi structure.
2124 * @budget : maximum number of packets that the current CPU can receive from
2127 * To look at the incoming frames and clear the tx resources.
2129 static int stmmac_poll(struct napi_struct *napi, int budget)
2131 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2134 priv->xstats.napi_poll++;
2135 stmmac_tx_clean(priv);
2137 work_done = stmmac_rx(priv, budget);
2138 if (work_done < budget) {
2139 napi_complete(napi);
2140 stmmac_enable_dma_irq(priv);
2147 * @dev : Pointer to net device structure
2148 * Description: this function is called when a packet transmission fails to
2149 * complete within a reasonable time. The driver will mark the error in the
2150 * netdev structure and arrange for the device to be reset to a sane state
2151 * in order to transmit a new packet.
2153 static void stmmac_tx_timeout(struct net_device *dev)
2155 struct stmmac_priv *priv = netdev_priv(dev);
2157 /* Clear Tx resources and restart transmitting again */
2158 stmmac_tx_err(priv);
2161 /* Configuration changes (passed on by ifconfig) */
2162 static int stmmac_config(struct net_device *dev, struct ifmap *map)
2164 if (dev->flags & IFF_UP) /* can't act on a running interface */
2167 /* Don't allow changing the I/O address */
2168 if (map->base_addr != dev->base_addr) {
2169 pr_warning("%s: can't change I/O address\n", dev->name);
2173 /* Don't allow changing the IRQ */
2174 if (map->irq != dev->irq) {
2175 pr_warning("%s: can't change IRQ number %d\n",
2176 dev->name, dev->irq);
2180 /* ignore other fields */
2185 * stmmac_set_rx_mode - entry point for multicast addressing
2186 * @dev : pointer to the device structure
2188 * This function is a driver entry point which gets called by the kernel
2189 * whenever multicast addresses must be enabled/disabled.
2193 static void stmmac_set_rx_mode(struct net_device *dev)
2195 struct stmmac_priv *priv = netdev_priv(dev);
2197 spin_lock(&priv->lock);
2198 priv->hw->mac->set_filter(dev, priv->synopsys_id);
2199 spin_unlock(&priv->lock);
2203 * stmmac_change_mtu - entry point to change MTU size for the device.
2204 * @dev : device pointer.
2205 * @new_mtu : the new MTU size for the device.
2206 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2207 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2208 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2210 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2213 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2215 struct stmmac_priv *priv = netdev_priv(dev);
2218 if (netif_running(dev)) {
2219 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2223 if (priv->plat->enh_desc)
2224 max_mtu = JUMBO_LEN;
2226 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
2228 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2229 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2234 netdev_update_features(dev);
2239 static netdev_features_t stmmac_fix_features(struct net_device *dev,
2240 netdev_features_t features)
2242 struct stmmac_priv *priv = netdev_priv(dev);
2244 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
2245 features &= ~NETIF_F_RXCSUM;
2246 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
2247 features &= ~NETIF_F_IPV6_CSUM;
2248 if (!priv->plat->tx_coe)
2249 features &= ~NETIF_F_ALL_CSUM;
2251 /* Some GMAC devices have a bugged Jumbo frame support that
2252 * needs to have the Tx COE disabled for oversized frames
2253 * (due to limited buffer sizes). In this case we disable
2254 * the TX csum insertionin the TDES and not use SF. */
2255 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
2256 features &= ~NETIF_F_ALL_CSUM;
2262 * stmmac_interrupt - main ISR
2263 * @irq: interrupt number.
2264 * @dev_id: to pass the net device pointer.
2265 * Description: this is the main driver interrupt service routine.
2266 * It calls the DMA ISR and also the core ISR to manage PMT, MMC, LPI
2269 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2271 struct net_device *dev = (struct net_device *)dev_id;
2272 struct stmmac_priv *priv = netdev_priv(dev);
2274 if (unlikely(!dev)) {
2275 pr_err("%s: invalid dev pointer\n", __func__);
2279 /* To handle GMAC own interrupts */
2280 if (priv->plat->has_gmac) {
2281 int status = priv->hw->mac->host_irq_status((void __iomem *)
2284 if (unlikely(status)) {
2285 /* For LPI we need to save the tx status */
2286 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
2287 priv->tx_path_in_lpi_mode = true;
2288 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
2289 priv->tx_path_in_lpi_mode = false;
2293 /* To handle DMA interrupts */
2294 stmmac_dma_interrupt(priv);
2299 #ifdef CONFIG_NET_POLL_CONTROLLER
2300 /* Polling receive - used by NETCONSOLE and other diagnostic tools
2301 * to allow network I/O with interrupts disabled. */
2302 static void stmmac_poll_controller(struct net_device *dev)
2304 disable_irq(dev->irq);
2305 stmmac_interrupt(dev->irq, dev);
2306 enable_irq(dev->irq);
2311 * stmmac_ioctl - Entry point for the Ioctl
2312 * @dev: Device pointer.
2313 * @rq: An IOCTL specefic structure, that can contain a pointer to
2314 * a proprietary structure used to pass information to the driver.
2315 * @cmd: IOCTL command
2317 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
2319 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2321 struct stmmac_priv *priv = netdev_priv(dev);
2322 int ret = -EOPNOTSUPP;
2324 if (!netif_running(dev))
2333 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2336 ret = stmmac_hwtstamp_ioctl(dev, rq);
2345 #ifdef CONFIG_STMMAC_DEBUG_FS
2346 static struct dentry *stmmac_fs_dir;
2347 static struct dentry *stmmac_rings_status;
2348 static struct dentry *stmmac_dma_cap;
2350 static void sysfs_display_ring(void *head, int size, int extend_desc,
2351 struct seq_file *seq)
2354 struct dma_extended_desc *ep = (struct dma_extended_desc *) head;
2355 struct dma_desc *p = (struct dma_desc *) head;
2357 for (i = 0; i < size; i++) {
2361 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
2362 i, (unsigned int) virt_to_phys(ep),
2363 (unsigned int) x, (unsigned int) (x >> 32),
2364 ep->basic.des2, ep->basic.des3);
2368 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
2369 i, (unsigned int) virt_to_phys(ep),
2370 (unsigned int) x, (unsigned int) (x >> 32),
2374 seq_printf(seq, "\n");
2378 static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2380 struct net_device *dev = seq->private;
2381 struct stmmac_priv *priv = netdev_priv(dev);
2382 unsigned int txsize = priv->dma_tx_size;
2383 unsigned int rxsize = priv->dma_rx_size;
2385 if (priv->extend_desc) {
2386 seq_printf(seq, "Extended RX descriptor ring:\n");
2387 sysfs_display_ring((void *) priv->dma_erx, rxsize, 1, seq);
2388 seq_printf(seq, "Extended TX descriptor ring:\n");
2389 sysfs_display_ring((void *) priv->dma_etx, txsize, 1, seq);
2391 seq_printf(seq, "RX descriptor ring:\n");
2392 sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
2393 seq_printf(seq, "TX descriptor ring:\n");
2394 sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
2400 static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2402 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2405 static const struct file_operations stmmac_rings_status_fops = {
2406 .owner = THIS_MODULE,
2407 .open = stmmac_sysfs_ring_open,
2409 .llseek = seq_lseek,
2410 .release = single_release,
2413 static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2415 struct net_device *dev = seq->private;
2416 struct stmmac_priv *priv = netdev_priv(dev);
2418 if (!priv->hw_cap_support) {
2419 seq_printf(seq, "DMA HW features not supported\n");
2423 seq_printf(seq, "==============================\n");
2424 seq_printf(seq, "\tDMA HW features\n");
2425 seq_printf(seq, "==============================\n");
2427 seq_printf(seq, "\t10/100 Mbps %s\n",
2428 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2429 seq_printf(seq, "\t1000 Mbps %s\n",
2430 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2431 seq_printf(seq, "\tHalf duple %s\n",
2432 (priv->dma_cap.half_duplex) ? "Y" : "N");
2433 seq_printf(seq, "\tHash Filter: %s\n",
2434 (priv->dma_cap.hash_filter) ? "Y" : "N");
2435 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2436 (priv->dma_cap.multi_addr) ? "Y" : "N");
2437 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2438 (priv->dma_cap.pcs) ? "Y" : "N");
2439 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2440 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2441 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2442 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2443 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2444 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2445 seq_printf(seq, "\tRMON module: %s\n",
2446 (priv->dma_cap.rmon) ? "Y" : "N");
2447 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2448 (priv->dma_cap.time_stamp) ? "Y" : "N");
2449 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2450 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2451 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2452 (priv->dma_cap.eee) ? "Y" : "N");
2453 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2454 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2455 (priv->dma_cap.tx_coe) ? "Y" : "N");
2456 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2457 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2458 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2459 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2460 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2461 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2462 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2463 priv->dma_cap.number_rx_channel);
2464 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2465 priv->dma_cap.number_tx_channel);
2466 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2467 (priv->dma_cap.enh_desc) ? "Y" : "N");
2472 static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2474 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2477 static const struct file_operations stmmac_dma_cap_fops = {
2478 .owner = THIS_MODULE,
2479 .open = stmmac_sysfs_dma_cap_open,
2481 .llseek = seq_lseek,
2482 .release = single_release,
2485 static int stmmac_init_fs(struct net_device *dev)
2487 /* Create debugfs entries */
2488 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
2490 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
2491 pr_err("ERROR %s, debugfs create directory failed\n",
2492 STMMAC_RESOURCE_NAME);
2497 /* Entry to report DMA RX/TX rings */
2498 stmmac_rings_status = debugfs_create_file("descriptors_status",
2499 S_IRUGO, stmmac_fs_dir, dev,
2500 &stmmac_rings_status_fops);
2502 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
2503 pr_info("ERROR creating stmmac ring debugfs file\n");
2504 debugfs_remove(stmmac_fs_dir);
2509 /* Entry to report the DMA HW features */
2510 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
2511 dev, &stmmac_dma_cap_fops);
2513 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
2514 pr_info("ERROR creating stmmac MMC debugfs file\n");
2515 debugfs_remove(stmmac_rings_status);
2516 debugfs_remove(stmmac_fs_dir);
2524 static void stmmac_exit_fs(void)
2526 debugfs_remove(stmmac_rings_status);
2527 debugfs_remove(stmmac_dma_cap);
2528 debugfs_remove(stmmac_fs_dir);
2530 #endif /* CONFIG_STMMAC_DEBUG_FS */
2532 static const struct net_device_ops stmmac_netdev_ops = {
2533 .ndo_open = stmmac_open,
2534 .ndo_start_xmit = stmmac_xmit,
2535 .ndo_stop = stmmac_release,
2536 .ndo_change_mtu = stmmac_change_mtu,
2537 .ndo_fix_features = stmmac_fix_features,
2538 .ndo_set_rx_mode = stmmac_set_rx_mode,
2539 .ndo_tx_timeout = stmmac_tx_timeout,
2540 .ndo_do_ioctl = stmmac_ioctl,
2541 .ndo_set_config = stmmac_config,
2542 #ifdef CONFIG_NET_POLL_CONTROLLER
2543 .ndo_poll_controller = stmmac_poll_controller,
2545 .ndo_set_mac_address = eth_mac_addr,
2549 * stmmac_hw_init - Init the MAC device
2550 * @priv: driver private structure
2551 * Description: this function detects which MAC device
2552 * (GMAC/MAC10-100) has to attached, checks the HW capability
2553 * (if supported) and sets the driver's features (for example
2554 * to use the ring or chaine mode or support the normal/enh
2555 * descriptor structure).
2557 static int stmmac_hw_init(struct stmmac_priv *priv)
2560 struct mac_device_info *mac;
2562 /* Identify the MAC HW device */
2563 if (priv->plat->has_gmac) {
2564 priv->dev->priv_flags |= IFF_UNICAST_FLT;
2565 mac = dwmac1000_setup(priv->ioaddr);
2567 mac = dwmac100_setup(priv->ioaddr);
2574 /* Get and dump the chip ID */
2575 priv->synopsys_id = stmmac_get_synopsys_id(priv);
2577 /* To use alternate (extended) or normal descriptor structures */
2578 stmmac_selec_desc_mode(priv);
2580 /* To use the chained or ring mode */
2582 priv->hw->chain = &chain_mode_ops;
2583 pr_info(" Chain mode enabled\n");
2584 priv->mode = STMMAC_CHAIN_MODE;
2586 priv->hw->ring = &ring_mode_ops;
2587 pr_info(" Ring mode enabled\n");
2588 priv->mode = STMMAC_RING_MODE;
2591 /* Get the HW capability (new GMAC newer than 3.50a) */
2592 priv->hw_cap_support = stmmac_get_hw_features(priv);
2593 if (priv->hw_cap_support) {
2594 pr_info(" DMA HW capability register supported");
2596 /* We can override some gmac/dma configuration fields: e.g.
2597 * enh_desc, tx_coe (e.g. that are passed through the
2598 * platform) with the values from the HW capability
2599 * register (if supported).
2601 priv->plat->enh_desc = priv->dma_cap.enh_desc;
2602 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
2604 priv->plat->tx_coe = priv->dma_cap.tx_coe;
2606 if (priv->dma_cap.rx_coe_type2)
2607 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2608 else if (priv->dma_cap.rx_coe_type1)
2609 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2612 pr_info(" No HW DMA feature register supported");
2614 /* Enable the IPC (Checksum Offload) and check if the feature has been
2615 * enabled during the core configuration. */
2616 ret = priv->hw->mac->rx_ipc(priv->ioaddr);
2618 pr_warning(" RX IPC Checksum Offload not configured.\n");
2619 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
2622 if (priv->plat->rx_coe)
2623 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2624 priv->plat->rx_coe);
2625 if (priv->plat->tx_coe)
2626 pr_info(" TX Checksum insertion supported\n");
2628 if (priv->plat->pmt) {
2629 pr_info(" Wake-Up On Lan supported\n");
2630 device_set_wakeup_capable(priv->device, 1);
2638 * @device: device pointer
2639 * @plat_dat: platform data pointer
2640 * @addr: iobase memory address
2641 * Description: this is the main probe function used to
2642 * call the alloc_etherdev, allocate the priv structure.
2644 struct stmmac_priv *stmmac_dvr_probe(struct device *device,
2645 struct plat_stmmacenet_data *plat_dat,
2649 struct net_device *ndev = NULL;
2650 struct stmmac_priv *priv;
2652 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
2656 SET_NETDEV_DEV(ndev, device);
2658 priv = netdev_priv(ndev);
2659 priv->device = device;
2664 stmmac_set_ethtool_ops(ndev);
2665 priv->pause = pause;
2666 priv->plat = plat_dat;
2667 priv->ioaddr = addr;
2668 priv->dev->base_addr = (unsigned long)addr;
2670 /* Verify driver arguments */
2671 stmmac_verify_args();
2673 /* Override with kernel parameters if supplied XXX CRS XXX
2674 * this needs to have multiple instances */
2675 if ((phyaddr >= 0) && (phyaddr <= 31))
2676 priv->plat->phy_addr = phyaddr;
2678 /* Init MAC and get the capabilities */
2679 ret = stmmac_hw_init(priv);
2681 goto error_free_netdev;
2683 ndev->netdev_ops = &stmmac_netdev_ops;
2685 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2687 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2688 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
2689 #ifdef STMMAC_VLAN_TAG_USED
2690 /* Both mac100 and gmac support receive VLAN tag detection */
2691 ndev->features |= NETIF_F_HW_VLAN_RX;
2693 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2696 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2698 /* Rx Watchdog is available in the COREs newer than the 3.40.
2699 * In some case, for example on bugged HW this feature
2700 * has to be disable and this can be done by passing the
2701 * riwt_off field from the platform.
2703 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2705 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2708 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
2710 spin_lock_init(&priv->lock);
2711 spin_lock_init(&priv->tx_lock);
2713 ret = register_netdev(ndev);
2715 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
2716 goto error_netdev_register;
2719 priv->stmmac_clk = clk_get(priv->device, STMMAC_RESOURCE_NAME);
2720 if (IS_ERR(priv->stmmac_clk)) {
2721 pr_warning("%s: warning: cannot get CSR clock\n", __func__);
2725 /* If a specific clk_csr value is passed from the platform
2726 * this means that the CSR Clock Range selection cannot be
2727 * changed at run-time and it is fixed. Viceversa the driver'll try to
2728 * set the MDC clock dynamically according to the csr actual
2731 if (!priv->plat->clk_csr)
2732 stmmac_clk_csr_set(priv);
2734 priv->clk_csr = priv->plat->clk_csr;
2736 stmmac_check_pcs_mode(priv);
2738 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2739 priv->pcs != STMMAC_PCS_RTBI) {
2740 /* MDIO bus Registration */
2741 ret = stmmac_mdio_register(ndev);
2743 pr_debug("%s: MDIO bus (id: %d) registration failed",
2744 __func__, priv->plat->bus_id);
2745 goto error_mdio_register;
2751 error_mdio_register:
2752 clk_put(priv->stmmac_clk);
2754 unregister_netdev(ndev);
2755 error_netdev_register:
2756 netif_napi_del(&priv->napi);
2765 * @ndev: net device pointer
2766 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
2767 * changes the link status, releases the DMA descriptor rings.
2769 int stmmac_dvr_remove(struct net_device *ndev)
2771 struct stmmac_priv *priv = netdev_priv(ndev);
2773 pr_info("%s:\n\tremoving driver", __func__);
2775 priv->hw->dma->stop_rx(priv->ioaddr);
2776 priv->hw->dma->stop_tx(priv->ioaddr);
2778 stmmac_set_mac(priv->ioaddr, false);
2779 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2780 priv->pcs != STMMAC_PCS_RTBI)
2781 stmmac_mdio_unregister(ndev);
2782 netif_carrier_off(ndev);
2783 unregister_netdev(ndev);
2790 int stmmac_suspend(struct net_device *ndev)
2792 struct stmmac_priv *priv = netdev_priv(ndev);
2793 unsigned long flags;
2795 if (!ndev || !netif_running(ndev))
2799 phy_stop(priv->phydev);
2801 spin_lock_irqsave(&priv->lock, flags);
2803 netif_device_detach(ndev);
2804 netif_stop_queue(ndev);
2806 napi_disable(&priv->napi);
2808 /* Stop TX/RX DMA */
2809 priv->hw->dma->stop_tx(priv->ioaddr);
2810 priv->hw->dma->stop_rx(priv->ioaddr);
2812 stmmac_clear_descriptors(priv);
2814 /* Enable Power down mode by programming the PMT regs */
2815 if (device_may_wakeup(priv->device))
2816 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
2818 stmmac_set_mac(priv->ioaddr, false);
2819 /* Disable clock in case of PWM is off */
2820 clk_disable_unprepare(priv->stmmac_clk);
2822 spin_unlock_irqrestore(&priv->lock, flags);
2826 int stmmac_resume(struct net_device *ndev)
2828 struct stmmac_priv *priv = netdev_priv(ndev);
2829 unsigned long flags;
2831 if (!netif_running(ndev))
2834 spin_lock_irqsave(&priv->lock, flags);
2836 /* Power Down bit, into the PM register, is cleared
2837 * automatically as soon as a magic packet or a Wake-up frame
2838 * is received. Anyway, it's better to manually clear
2839 * this bit because it can generate problems while resuming
2840 * from another devices (e.g. serial console). */
2841 if (device_may_wakeup(priv->device))
2842 priv->hw->mac->pmt(priv->ioaddr, 0);
2844 /* enable the clk prevously disabled */
2845 clk_prepare_enable(priv->stmmac_clk);
2847 netif_device_attach(ndev);
2849 /* Enable the MAC and DMA */
2850 stmmac_set_mac(priv->ioaddr, true);
2851 priv->hw->dma->start_tx(priv->ioaddr);
2852 priv->hw->dma->start_rx(priv->ioaddr);
2854 napi_enable(&priv->napi);
2856 netif_start_queue(ndev);
2858 spin_unlock_irqrestore(&priv->lock, flags);
2861 phy_start(priv->phydev);
2866 int stmmac_freeze(struct net_device *ndev)
2868 if (!ndev || !netif_running(ndev))
2871 return stmmac_release(ndev);
2874 int stmmac_restore(struct net_device *ndev)
2876 if (!ndev || !netif_running(ndev))
2879 return stmmac_open(ndev);
2881 #endif /* CONFIG_PM */
2883 /* Driver can be configured w/ and w/ both PCI and Platf drivers
2884 * depending on the configuration selected.
2886 static int __init stmmac_init(void)
2890 ret = stmmac_register_platform();
2893 ret = stmmac_register_pci();
2898 stmmac_unregister_platform();
2900 pr_err("stmmac: driver registration failed\n");
2904 static void __exit stmmac_exit(void)
2906 stmmac_unregister_platform();
2907 stmmac_unregister_pci();
2910 module_init(stmmac_init);
2911 module_exit(stmmac_exit);
2914 static int __init stmmac_cmdline_opt(char *str)
2920 while ((opt = strsep(&str, ",")) != NULL) {
2921 if (!strncmp(opt, "debug:", 6)) {
2922 if (kstrtoint(opt + 6, 0, &debug))
2924 } else if (!strncmp(opt, "phyaddr:", 8)) {
2925 if (kstrtoint(opt + 8, 0, &phyaddr))
2927 } else if (!strncmp(opt, "dma_txsize:", 11)) {
2928 if (kstrtoint(opt + 11, 0, &dma_txsize))
2930 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
2931 if (kstrtoint(opt + 11, 0, &dma_rxsize))
2933 } else if (!strncmp(opt, "buf_sz:", 7)) {
2934 if (kstrtoint(opt + 7, 0, &buf_sz))
2936 } else if (!strncmp(opt, "tc:", 3)) {
2937 if (kstrtoint(opt + 3, 0, &tc))
2939 } else if (!strncmp(opt, "watchdog:", 9)) {
2940 if (kstrtoint(opt + 9, 0, &watchdog))
2942 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
2943 if (kstrtoint(opt + 10, 0, &flow_ctrl))
2945 } else if (!strncmp(opt, "pause:", 6)) {
2946 if (kstrtoint(opt + 6, 0, &pause))
2948 } else if (!strncmp(opt, "eee_timer:", 10)) {
2949 if (kstrtoint(opt + 10, 0, &eee_timer))
2951 } else if (!strncmp(opt, "chain_mode:", 11)) {
2952 if (kstrtoint(opt + 11, 0, &chain_mode))
2959 pr_err("%s: ERROR broken module parameter conversion", __func__);
2963 __setup("stmmaceth=", stmmac_cmdline_opt);
2966 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
2967 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
2968 MODULE_LICENSE("GPL");