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_GMAC_DEBUG_FS
47 #include <linux/debugfs.h>
48 #include <linux/seq_file.h>
49 #endif /* CONFIG_GMAC_DEBUG_FS */
50 #include <linux/net_tstamp.h>
51 #include "stmmac_ptp.h"
53 #include "../eth_mac.h"
56 /*#define STMMAC_DEBUG*/
58 #define DBG(nlevel, klevel, fmt, args...) \
59 ((void)(netif_msg_##nlevel(priv) && \
60 printk(KERN_##klevel fmt, ## args)))
62 #define DBG(nlevel, klevel, fmt, args...) do { } while (0)
65 #undef STMMAC_RX_DEBUG
66 /*#define STMMAC_RX_DEBUG*/
67 #ifdef STMMAC_RX_DEBUG
68 #define RX_DBG(fmt, args...) printk(fmt, ## args)
70 #define RX_DBG(fmt, args...) do { } while (0)
73 #undef STMMAC_XMIT_DEBUG
74 /*#define STMMAC_XMIT_DEBUG*/
75 #ifdef STMMAC_XMIT_DEBUG
76 #define TX_DBG(fmt, args...) printk(fmt, ## args)
78 #define TX_DBG(fmt, args...) do { } while (0)
81 #define STMMAC_ALIGN(x) L1_CACHE_ALIGN(x)
82 #define JUMBO_LEN 9000
84 /* Module parameters */
86 static int watchdog = TX_TIMEO;
87 module_param(watchdog, int, S_IRUGO | S_IWUSR);
88 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)");
90 static int debug = -1;
91 module_param(debug, int, S_IRUGO | S_IWUSR);
92 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)");
95 module_param(phyaddr, int, S_IRUGO);
96 MODULE_PARM_DESC(phyaddr, "Physical device address");
98 #define DMA_TX_SIZE 256
99 static int dma_txsize = DMA_TX_SIZE;
100 module_param(dma_txsize, int, S_IRUGO | S_IWUSR);
101 MODULE_PARM_DESC(dma_txsize, "Number of descriptors in the TX list");
103 #define DMA_RX_SIZE 256
104 static int dma_rxsize = DMA_RX_SIZE;
105 module_param(dma_rxsize, int, S_IRUGO | S_IWUSR);
106 MODULE_PARM_DESC(dma_rxsize, "Number of descriptors in the RX list");
108 static int flow_ctrl = FLOW_OFF;
109 module_param(flow_ctrl, int, S_IRUGO | S_IWUSR);
110 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]");
112 static int pause = PAUSE_TIME;
113 module_param(pause, int, S_IRUGO | S_IWUSR);
114 MODULE_PARM_DESC(pause, "Flow Control Pause Time");
116 #define TC_DEFAULT 64
117 static int tc = TC_DEFAULT;
118 module_param(tc, int, S_IRUGO | S_IWUSR);
119 MODULE_PARM_DESC(tc, "DMA threshold control value");
121 #define DMA_BUFFER_SIZE BUF_SIZE_2KiB
122 static int buf_sz = DMA_BUFFER_SIZE;
123 module_param(buf_sz, int, S_IRUGO | S_IWUSR);
124 MODULE_PARM_DESC(buf_sz, "DMA buffer size");
126 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE |
127 NETIF_MSG_LINK | NETIF_MSG_IFUP |
128 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER);
130 #define STMMAC_DEFAULT_LPI_TIMER 1000
131 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER;
132 module_param(eee_timer, int, S_IRUGO | S_IWUSR);
133 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec");
134 #define STMMAC_LPI_T(x) (jiffies + msecs_to_jiffies(x))
136 /* By default the driver will use the ring mode to manage tx and rx descriptors
137 * but passing this value so user can force to use the chain instead of the ring
139 static unsigned int chain_mode;
140 module_param(chain_mode, int, S_IRUGO);
141 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode");
143 static irqreturn_t stmmac_interrupt(int irq, void *dev_id);
145 #ifdef CONFIG_GMAC_DEBUG_FS
146 static int stmmac_init_fs(struct net_device *dev);
147 static void stmmac_exit_fs(void);
150 #define STMMAC_COAL_TIMER(x) (jiffies + usecs_to_jiffies(x))
153 * stmmac_verify_args - verify the driver parameters.
154 * Description: it verifies if some wrong parameter is passed to the driver.
155 * Note that wrong parameters are replaced with the default values.
157 static void stmmac_verify_args(void)
159 if (unlikely(watchdog < 0))
161 if (unlikely(dma_rxsize < 0))
162 dma_rxsize = DMA_RX_SIZE;
163 if (unlikely(dma_txsize < 0))
164 dma_txsize = DMA_TX_SIZE;
165 if (unlikely((buf_sz < DMA_BUFFER_SIZE) || (buf_sz > BUF_SIZE_16KiB)))
166 buf_sz = DMA_BUFFER_SIZE;
167 if (unlikely(flow_ctrl > 1))
168 flow_ctrl = FLOW_AUTO;
169 else if (likely(flow_ctrl < 0))
170 flow_ctrl = FLOW_OFF;
171 if (unlikely((pause < 0) || (pause > 0xffff)))
174 eee_timer = STMMAC_DEFAULT_LPI_TIMER;
178 * stmmac_clk_csr_set - dynamically set the MDC clock
179 * @priv: driver private structure
180 * Description: this is to dynamically set the MDC clock according to the csr
183 * If a specific clk_csr value is passed from the platform
184 * this means that the CSR Clock Range selection cannot be
185 * changed at run-time and it is fixed (as reported in the driver
186 * documentation). Viceversa the driver will try to set the MDC
187 * clock dynamically according to the actual clock input.
189 static void stmmac_clk_csr_set(struct stmmac_priv *priv)
193 clk_rate = clk_get_rate(priv->stmmac_clk);
195 /* Platform provided default clk_csr would be assumed valid
196 * for all other cases except for the below mentioned ones.
197 * For values higher than the IEEE 802.3 specified frequency
198 * we can not estimate the proper divider as it is not known
199 * the frequency of clk_csr_i. So we do not change the default
202 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) {
203 if (clk_rate < CSR_F_35M)
204 priv->clk_csr = STMMAC_CSR_20_35M;
205 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M))
206 priv->clk_csr = STMMAC_CSR_35_60M;
207 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M))
208 priv->clk_csr = STMMAC_CSR_60_100M;
209 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M))
210 priv->clk_csr = STMMAC_CSR_100_150M;
211 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M))
212 priv->clk_csr = STMMAC_CSR_150_250M;
213 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M))
214 priv->clk_csr = STMMAC_CSR_250_300M;
218 #if defined(STMMAC_XMIT_DEBUG) || defined(STMMAC_RX_DEBUG)
219 static void print_pkt(unsigned char *buf, int len)
222 pr_info("len = %d byte, buf addr: 0x%p", len, buf);
223 for (j = 0; j < len; j++) {
225 pr_info("\n %03x:", j);
226 pr_info(" %02x", buf[j]);
232 /* minimum number of free TX descriptors required to wake up TX process */
233 #define STMMAC_TX_THRESH(x) (x->dma_tx_size/4)
235 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv)
237 return priv->dirty_tx + priv->dma_tx_size - priv->cur_tx - 1;
241 * stmmac_hw_fix_mac_speed: callback for speed selection
242 * @priv: driver private structure
243 * Description: on some platforms (e.g. ST), some HW system configuraton
244 * registers have to be set according to the link speed negotiated.
246 static inline void stmmac_hw_fix_mac_speed(struct stmmac_priv *priv)
248 struct phy_device *phydev = priv->phydev;
250 if (likely(priv->plat->fix_mac_speed))
251 priv->plat->fix_mac_speed(priv->plat->bsp_priv, phydev->speed);
255 * stmmac_enable_eee_mode: Check and enter in LPI mode
256 * @priv: driver private structure
257 * Description: this function is to verify and enter in LPI mode for EEE.
259 static void stmmac_enable_eee_mode(struct stmmac_priv *priv)
261 /* Check and enter in LPI mode */
262 if ((priv->dirty_tx == priv->cur_tx) &&
263 (priv->tx_path_in_lpi_mode == false))
264 priv->hw->mac->set_eee_mode(priv->ioaddr);
268 * stmmac_disable_eee_mode: disable/exit from EEE
269 * @priv: driver private structure
270 * Description: this function is to exit and disable EEE in case of
271 * LPI state is true. This is called by the xmit.
273 void stmmac_disable_eee_mode(struct stmmac_priv *priv)
275 priv->hw->mac->reset_eee_mode(priv->ioaddr);
276 del_timer_sync(&priv->eee_ctrl_timer);
277 priv->tx_path_in_lpi_mode = false;
281 * stmmac_eee_ctrl_timer: EEE TX SW timer.
284 * if there is no data transfer and if we are not in LPI state,
285 * then MAC Transmitter can be moved to LPI state.
287 static void stmmac_eee_ctrl_timer(unsigned long arg)
289 struct stmmac_priv *priv = (struct stmmac_priv *)arg;
291 stmmac_enable_eee_mode(priv);
292 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
296 * stmmac_eee_init: init EEE
297 * @priv: driver private structure
299 * If the EEE support has been enabled while configuring the driver,
300 * if the GMAC actually supports the EEE (from the HW cap reg) and the
301 * phy can also manage EEE, so enable the LPI state and start the timer
302 * to verify if the tx path can enter in LPI state.
304 bool stmmac_eee_init(struct stmmac_priv *priv)
308 /* Using PCS we cannot dial with the phy registers at this stage
309 * so we do not support extra feature like EEE.
311 if ((priv->pcs == STMMAC_PCS_RGMII) || (priv->pcs == STMMAC_PCS_TBI) ||
312 (priv->pcs == STMMAC_PCS_RTBI))
315 /* MAC core supports the EEE feature. */
316 if (priv->dma_cap.eee) {
317 /* Check if the PHY supports EEE */
318 if (phy_init_eee(priv->phydev, 1))
321 if (!priv->eee_active) {
322 priv->eee_active = 1;
323 init_timer(&priv->eee_ctrl_timer);
324 priv->eee_ctrl_timer.function = stmmac_eee_ctrl_timer;
325 priv->eee_ctrl_timer.data = (unsigned long)priv;
326 priv->eee_ctrl_timer.expires = STMMAC_LPI_T(eee_timer);
327 add_timer(&priv->eee_ctrl_timer);
329 priv->hw->mac->set_eee_timer(priv->ioaddr,
330 STMMAC_DEFAULT_LIT_LS,
333 /* Set HW EEE according to the speed */
334 priv->hw->mac->set_eee_pls(priv->ioaddr,
337 pr_info("stmmac: Energy-Efficient Ethernet initialized\n");
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,
354 unsigned int entry, struct sk_buff *skb)
356 struct skb_shared_hwtstamps shhwtstamp;
360 if (!priv->hwts_tx_en)
363 /* exit if skb doesn't support hw tstamp */
364 if (likely(!(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)))
368 desc = (priv->dma_etx + entry);
370 desc = (priv->dma_tx + entry);
372 /* check tx tstamp status */
373 if (!priv->hw->desc->get_tx_timestamp_status((struct dma_desc *)desc))
376 /* get the valid tstamp */
377 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
379 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
380 shhwtstamp.hwtstamp = ns_to_ktime(ns);
381 /* pass tstamp to stack */
382 skb_tstamp_tx(skb, &shhwtstamp);
387 /* stmmac_get_rx_hwtstamp: get HW RX timestamps
388 * @priv: driver private structure
389 * @entry : descriptor index to be used.
390 * @skb : the socket buffer
392 * This function will read received packet's timestamp from the descriptor
393 * and pass it to stack. It also perform some sanity checks.
395 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv,
396 unsigned int entry, struct sk_buff *skb)
398 struct skb_shared_hwtstamps *shhwtstamp = NULL;
402 if (!priv->hwts_rx_en)
406 desc = (priv->dma_erx + entry);
408 desc = (priv->dma_rx + entry);
410 /* exit if rx tstamp is not valid */
411 if (!priv->hw->desc->get_rx_timestamp_status(desc, priv->adv_ts))
414 /* get valid tstamp */
415 ns = priv->hw->desc->get_timestamp(desc, priv->adv_ts);
416 shhwtstamp = skb_hwtstamps(skb);
417 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps));
418 shhwtstamp->hwtstamp = ns_to_ktime(ns);
422 * stmmac_hwtstamp_ioctl - control hardware timestamping.
423 * @dev: device pointer.
424 * @ifr: An IOCTL specefic structure, that can contain a pointer to
425 * a proprietary structure used to pass information to the driver.
427 * This function configures the MAC to enable/disable both outgoing(TX)
428 * and incoming(RX) packets time stamping based on user input.
430 * 0 on success and an appropriate -ve integer on failure.
432 static int stmmac_hwtstamp_ioctl(struct net_device *dev, struct ifreq *ifr)
434 struct stmmac_priv *priv = netdev_priv(dev);
435 struct hwtstamp_config config;
440 u32 ptp_over_ipv4_udp = 0;
441 u32 ptp_over_ipv6_udp = 0;
442 u32 ptp_over_ethernet = 0;
443 u32 snap_type_sel = 0;
444 u32 ts_master_en = 0;
448 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) {
449 netdev_alert(priv->dev, "No support for HW time stamping\n");
450 priv->hwts_tx_en = 0;
451 priv->hwts_rx_en = 0;
456 if (copy_from_user(&config, ifr->ifr_data,
457 sizeof(struct hwtstamp_config)))
460 pr_debug("%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n",
461 __func__, config.flags, config.tx_type, config.rx_filter);
463 /* reserved for future extensions */
467 switch (config.tx_type) {
468 case HWTSTAMP_TX_OFF:
469 priv->hwts_tx_en = 0;
472 priv->hwts_tx_en = 1;
479 switch (config.rx_filter) {
480 case HWTSTAMP_FILTER_NONE:
481 /* time stamp no incoming packet at all */
482 config.rx_filter = HWTSTAMP_FILTER_NONE;
485 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
486 /* PTP v1, UDP, any kind of event packet */
487 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
488 /* take time stamp for all event messages */
489 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
491 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
492 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
495 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
496 /* PTP v1, UDP, Sync packet */
497 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC;
498 /* take time stamp for SYNC messages only */
499 ts_event_en = PTP_TCR_TSEVNTENA;
501 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
502 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
505 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
506 /* PTP v1, UDP, Delay_req packet */
507 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ;
508 /* take time stamp for Delay_Req messages only */
509 ts_master_en = PTP_TCR_TSMSTRENA;
510 ts_event_en = PTP_TCR_TSEVNTENA;
512 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
513 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
516 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
517 /* PTP v2, UDP, any kind of event packet */
518 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT;
519 ptp_v2 = PTP_TCR_TSVER2ENA;
520 /* take time stamp for all event messages */
521 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
523 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
524 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
527 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
528 /* PTP v2, UDP, Sync packet */
529 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC;
530 ptp_v2 = PTP_TCR_TSVER2ENA;
531 /* take time stamp for SYNC messages only */
532 ts_event_en = PTP_TCR_TSEVNTENA;
534 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
535 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
538 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
539 /* PTP v2, UDP, Delay_req packet */
540 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ;
541 ptp_v2 = PTP_TCR_TSVER2ENA;
542 /* take time stamp for Delay_Req messages only */
543 ts_master_en = PTP_TCR_TSMSTRENA;
544 ts_event_en = PTP_TCR_TSEVNTENA;
546 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
547 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
550 case HWTSTAMP_FILTER_PTP_V2_EVENT:
551 /* PTP v2/802.AS1 any layer, any kind of event packet */
552 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
553 ptp_v2 = PTP_TCR_TSVER2ENA;
554 /* take time stamp for all event messages */
555 snap_type_sel = PTP_TCR_SNAPTYPSEL_1;
557 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
558 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
559 ptp_over_ethernet = PTP_TCR_TSIPENA;
562 case HWTSTAMP_FILTER_PTP_V2_SYNC:
563 /* PTP v2/802.AS1, any layer, Sync packet */
564 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC;
565 ptp_v2 = PTP_TCR_TSVER2ENA;
566 /* take time stamp for SYNC messages only */
567 ts_event_en = PTP_TCR_TSEVNTENA;
569 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
570 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
571 ptp_over_ethernet = PTP_TCR_TSIPENA;
574 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
575 /* PTP v2/802.AS1, any layer, Delay_req packet */
576 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ;
577 ptp_v2 = PTP_TCR_TSVER2ENA;
578 /* take time stamp for Delay_Req messages only */
579 ts_master_en = PTP_TCR_TSMSTRENA;
580 ts_event_en = PTP_TCR_TSEVNTENA;
582 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA;
583 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA;
584 ptp_over_ethernet = PTP_TCR_TSIPENA;
587 case HWTSTAMP_FILTER_ALL:
588 /* time stamp any incoming packet */
589 config.rx_filter = HWTSTAMP_FILTER_ALL;
590 tstamp_all = PTP_TCR_TSENALL;
597 switch (config.rx_filter) {
598 case HWTSTAMP_FILTER_NONE:
599 config.rx_filter = HWTSTAMP_FILTER_NONE;
602 /* PTP v1, UDP, any kind of event packet */
603 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
607 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1);
609 if (!priv->hwts_tx_en && !priv->hwts_rx_en)
610 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, 0);
612 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR |
613 tstamp_all | ptp_v2 | ptp_over_ethernet |
614 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en |
615 ts_master_en | snap_type_sel);
617 priv->hw->ptp->config_hw_tstamping(priv->ioaddr, value);
619 /* program Sub Second Increment reg */
620 priv->hw->ptp->config_sub_second_increment(priv->ioaddr);
622 /* calculate default added value:
624 * addend = (2^32)/freq_div_ratio;
625 * where, freq_div_ratio = STMMAC_SYSCLOCK/50MHz
626 * hence, addend = ((2^32) * 50MHz)/STMMAC_SYSCLOCK;
627 * NOTE: STMMAC_SYSCLOCK should be >= 50MHz to
628 * achive 20ns accuracy.
630 * 2^x * y == (y << x), hence
631 * 2^32 * 50000000 ==> (50000000 << 32)
633 temp = (u64) (50000000ULL << 32);
634 priv->default_addend = div_u64(temp, STMMAC_SYSCLOCK);
635 priv->hw->ptp->config_addend(priv->ioaddr,
636 priv->default_addend);
638 /* initialize system time */
639 getnstimeofday(&now);
640 priv->hw->ptp->init_systime(priv->ioaddr, 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 v2.
652 * This is done by looking at the HW cap. register.
653 * Also it registers the ptp driver.
656 static int stmmac_init_ptp(struct stmmac_priv *priv)
658 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp))
661 if (netif_msg_hw(priv)) {
662 if (priv->dma_cap.time_stamp) {
663 pr_debug("IEEE 1588-2002 Time Stamp supported\n");
666 if (priv->dma_cap.atime_stamp && priv->extend_desc) {
668 ("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_warn("%s: Speed (%d) not 10/100\n",
749 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 /* At this stage, it could be needed to setup the EEE or adjust some
773 * MAC related HW registers.
775 priv->eee_enabled = stmmac_eee_init(priv);
777 spin_unlock_irqrestore(&priv->lock, flags);
779 DBG(probe, DEBUG, "stmmac_adjust_link: exiting\n");
783 * stmmac_check_pcs_mode: verify if RGMII/SGMII is supported
784 * @priv: driver private structure
785 * Description: this is to verify if the HW supports the PCS.
786 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is
787 * configured for the TBI, RTBI, or SGMII PHY interface.
789 static void stmmac_check_pcs_mode(struct stmmac_priv *priv)
791 int interface = priv->plat->interface;
793 if (priv->dma_cap.pcs) {
794 if ((interface & PHY_INTERFACE_MODE_RGMII) ||
795 (interface & PHY_INTERFACE_MODE_RGMII_ID) ||
796 (interface & PHY_INTERFACE_MODE_RGMII_RXID) ||
797 (interface & PHY_INTERFACE_MODE_RGMII_TXID)) {
798 pr_debug("STMMAC: PCS RGMII support enable\n");
799 priv->pcs = STMMAC_PCS_RGMII;
800 } else if (interface & PHY_INTERFACE_MODE_SGMII) {
801 pr_debug("STMMAC: PCS SGMII support enable\n");
802 priv->pcs = STMMAC_PCS_SGMII;
809 static ssize_t show_phy_reg(struct device *dev,
810 struct device_attribute *attr, char *buf) {
811 int ret = snprintf(buf, PAGE_SIZE, "current phy reg = 0x%x\n", gPhyReg);
815 static ssize_t set_phy_reg(struct device *dev,struct device_attribute *attr,
816 const char *buf, size_t count) {
818 int r = kstrtoint(buf, 0, &ovl);
819 if (r) printk("kstrtoint failed\n");
821 printk("%s----ovl=0x%x\n", __FUNCTION__, ovl);
825 static ssize_t show_phy_regValue(struct device *dev,
826 struct device_attribute *attr, char *buf) {
827 struct phy_device *phy_dev = dev_get_drvdata(dev);
831 val = phy_read(phy_dev, gPhyReg);
832 ret = snprintf(buf, PAGE_SIZE, "phy reg 0x%x = 0x%x\n", gPhyReg, val);
836 for (i=0; i<32; i++) {
837 printk("%d: 0x%x\n", i, phy_read(phy_dev, i));
840 val = phy_read(phy_dev, gPhyReg);
841 ret = snprintf(buf, PAGE_SIZE, "phy reg 0x%x = 0x%x\n", gPhyReg, val);
846 static ssize_t set_phy_regValue(struct device *dev,
847 struct device_attribute *attr,
848 const char *buf, size_t count) {
852 struct phy_device *phy_dev = dev_get_drvdata(dev);
853 ret = kstrtoint(buf, 0, &ovl);
854 printk("%s----reg 0x%x: ovl=0x%x\n", __FUNCTION__, gPhyReg, ovl);
855 phy_write(phy_dev, gPhyReg, ovl);
859 static struct device_attribute phy_reg_attrs[] = {
860 __ATTR(phy_reg, S_IRUGO | S_IWUSR, show_phy_reg, set_phy_reg),
861 __ATTR(phy_regValue, S_IRUGO | S_IWUSR, show_phy_regValue, set_phy_regValue)
864 int gmac_create_sysfs(struct phy_device * phy_dev) {
868 dev_set_drvdata(&phy_dev->dev, phy_dev);
869 for (t = 0; t < ARRAY_SIZE(phy_reg_attrs); t++) {
870 r = device_create_file(&phy_dev->dev,&phy_reg_attrs[t]);
872 dev_err(&phy_dev->dev, "failed to create sysfs file\n");
880 int gmac_remove_sysfs(struct phy_device * phy_dev) {
883 for (t = 0; t < ARRAY_SIZE(phy_reg_attrs); t++) {
884 device_remove_file(&phy_dev->dev,&phy_reg_attrs[t]);
891 * stmmac_init_phy - PHY initialization
892 * @dev: net device structure
893 * Description: it initializes the driver's PHY state, and attaches the PHY
898 static int stmmac_init_phy(struct net_device *dev)
900 struct stmmac_priv *priv = netdev_priv(dev);
901 struct phy_device *phydev;
902 char phy_id_fmt[MII_BUS_ID_SIZE + 3];
903 char bus_id[MII_BUS_ID_SIZE];
904 int interface = priv->plat->interface;
907 priv->oldduplex = -1;
909 if (priv->plat->phy_bus_name)
910 snprintf(bus_id, MII_BUS_ID_SIZE, "%s-%x",
911 priv->plat->phy_bus_name, priv->plat->bus_id);
913 snprintf(bus_id, MII_BUS_ID_SIZE, "stmmac-%x",
916 snprintf(phy_id_fmt, MII_BUS_ID_SIZE + 3, PHY_ID_FMT, bus_id,
917 priv->plat->phy_addr);
918 pr_debug("stmmac_init_phy: trying to attach to %s\n", phy_id_fmt);
920 phydev = phy_connect(dev, phy_id_fmt, &stmmac_adjust_link, interface);
922 if (IS_ERR(phydev)) {
923 pr_err("%s: Could not attach to PHY\n", dev->name);
924 return PTR_ERR(phydev);
927 /* Stop Advertising 1000BASE Capability if interface is not GMII */
928 if ((interface == PHY_INTERFACE_MODE_MII) ||
929 (interface == PHY_INTERFACE_MODE_RMII))
930 phydev->advertising &= ~(SUPPORTED_1000baseT_Half |
931 SUPPORTED_1000baseT_Full);
934 * Broken HW is sometimes missing the pull-up resistor on the
935 * MDIO line, which results in reads to non-existent devices returning
936 * 0 rather than 0xffff. Catch this here and treat 0 as a non-existent
938 * Note: phydev->phy_id is the result of reading the UID PHY registers.
940 if (phydev->phy_id == 0) {
941 phy_disconnect(phydev);
944 pr_debug("stmmac_init_phy: %s: attached to PHY (UID 0x%x)"
945 " Link = %d\n", dev->name, phydev->phy_id, phydev->link);
947 priv->phydev = phydev;
949 gmac_create_sysfs(phydev);
955 * stmmac_display_ring: display ring
956 * @head: pointer to the head of the ring passed.
957 * @size: size of the ring.
958 * @extend_desc: to verify if extended descriptors are used.
959 * Description: display the control/status and buffer descriptors.
961 static void stmmac_display_ring(void *head, int size, int extend_desc)
964 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
965 struct dma_desc *p = (struct dma_desc *)head;
967 for (i = 0; i < size; i++) {
971 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
972 i, (unsigned int)virt_to_phys(ep),
973 (unsigned int)x, (unsigned int)(x >> 32),
974 ep->basic.des2, ep->basic.des3);
978 pr_info("%d [0x%x]: 0x%x 0x%x 0x%x 0x%x",
979 i, (unsigned int)virt_to_phys(p),
980 (unsigned int)x, (unsigned int)(x >> 32),
988 static void stmmac_display_rings(struct stmmac_priv *priv)
990 unsigned int txsize = priv->dma_tx_size;
991 unsigned int rxsize = priv->dma_rx_size;
993 if (priv->extend_desc) {
994 pr_info("Extended RX descriptor ring:\n");
995 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
996 pr_info("Extended TX descriptor ring:\n");
997 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
999 pr_info("RX descriptor ring:\n");
1000 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
1001 pr_info("TX descriptor ring:\n");
1002 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
1006 static int stmmac_set_bfsize(int mtu, int bufsize)
1010 if (mtu >= BUF_SIZE_4KiB)
1011 ret = BUF_SIZE_8KiB;
1012 else if (mtu >= BUF_SIZE_2KiB)
1013 ret = BUF_SIZE_4KiB;
1014 else if (mtu >= DMA_BUFFER_SIZE)
1015 ret = BUF_SIZE_2KiB;
1017 ret = DMA_BUFFER_SIZE;
1023 * stmmac_clear_descriptors: clear descriptors
1024 * @priv: driver private structure
1025 * Description: this function is called to clear the tx and rx descriptors
1026 * in case of both basic and extended descriptors are used.
1028 static void stmmac_clear_descriptors(struct stmmac_priv *priv)
1031 unsigned int txsize = priv->dma_tx_size;
1032 unsigned int rxsize = priv->dma_rx_size;
1034 /* Clear the Rx/Tx descriptors */
1035 for (i = 0; i < rxsize; i++)
1036 if (priv->extend_desc)
1037 priv->hw->desc->init_rx_desc(&priv->dma_erx[i].basic,
1038 priv->use_riwt, priv->mode,
1041 priv->hw->desc->init_rx_desc(&priv->dma_rx[i],
1042 priv->use_riwt, priv->mode,
1044 for (i = 0; i < txsize; i++)
1045 if (priv->extend_desc)
1046 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1050 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1055 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p,
1058 struct sk_buff *skb;
1060 skb = __netdev_alloc_skb(priv->dev, priv->dma_buf_sz + NET_IP_ALIGN,
1062 if (unlikely(skb == NULL)) {
1063 pr_err("%s: Rx init fails; skb is NULL\n", __func__);
1066 skb_reserve(skb, NET_IP_ALIGN);
1067 priv->rx_skbuff[i] = skb;
1068 priv->rx_skbuff_dma[i] = dma_map_single(priv->device, skb->data,
1072 p->des2 = priv->rx_skbuff_dma[i];
1074 if ((priv->mode == STMMAC_RING_MODE) &&
1075 (priv->dma_buf_sz == BUF_SIZE_16KiB))
1076 priv->hw->ring->init_desc3(p);
1082 * init_dma_desc_rings - init the RX/TX descriptor rings
1083 * @dev: net device structure
1084 * Description: this function initializes the DMA RX/TX descriptors
1085 * and allocates the socket buffers. It suppors the chained and ring
1088 static void init_dma_desc_rings(struct net_device *dev)
1091 struct stmmac_priv *priv = netdev_priv(dev);
1092 unsigned int txsize = priv->dma_tx_size;
1093 unsigned int rxsize = priv->dma_rx_size;
1094 unsigned int bfsize = 0;
1096 /* Set the max buffer size according to the DESC mode
1097 * and the MTU. Note that RING mode allows 16KiB bsize.
1099 if (priv->mode == STMMAC_RING_MODE)
1100 bfsize = priv->hw->ring->set_16kib_bfsize(dev->mtu);
1102 if (bfsize < BUF_SIZE_16KiB)
1103 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz);
1105 DBG(probe, INFO, "stmmac: txsize %d, rxsize %d, bfsize %d\n",
1106 txsize, rxsize, bfsize);
1108 if (priv->extend_desc) {
1109 priv->dma_erx = dma_alloc_coherent(priv->device, rxsize *
1114 priv->dma_etx = dma_alloc_coherent(priv->device, txsize *
1119 if ((!priv->dma_erx) || (!priv->dma_etx))
1122 priv->dma_rx = dma_alloc_coherent(priv->device, rxsize *
1123 sizeof(struct dma_desc),
1126 priv->dma_tx = dma_alloc_coherent(priv->device, txsize *
1127 sizeof(struct dma_desc),
1130 if ((!priv->dma_rx) || (!priv->dma_tx))
1133 memset(priv->dma_rx, 0, rxsize * sizeof(struct dma_desc));
1134 memset(priv->dma_tx, 0, txsize * sizeof(struct dma_desc));
1137 priv->rx_skbuff_dma = kmalloc_array(rxsize, sizeof(dma_addr_t),
1139 priv->rx_skbuff = kmalloc_array(rxsize, sizeof(struct sk_buff *),
1141 priv->tx_skbuff_dma = kmalloc_array(txsize, sizeof(dma_addr_t),
1143 priv->tx_skbuff = kmalloc_array(txsize, sizeof(struct sk_buff *),
1145 if (netif_msg_drv(priv))
1146 pr_debug("(%s) dma_rx_phy=0x%08x dma_tx_phy=0x%08x\n", __func__,
1147 (u32) priv->dma_rx_phy, (u32) priv->dma_tx_phy);
1149 /* RX INITIALIZATION */
1150 DBG(probe, INFO, "stmmac: SKB addresses:\nskb\t\tskb data\tdma data\n");
1151 for (i = 0; i < rxsize; i++) {
1153 if (priv->extend_desc)
1154 p = &((priv->dma_erx + i)->basic);
1156 p = priv->dma_rx + i;
1158 if (stmmac_init_rx_buffers(priv, p, i))
1161 DBG(probe, INFO, "[%p]\t[%p]\t[%x]\n", priv->rx_skbuff[i],
1162 priv->rx_skbuff[i]->data, priv->rx_skbuff_dma[i]);
1165 priv->dirty_rx = (unsigned int)(i - rxsize);
1166 priv->dma_buf_sz = bfsize;
1169 /* Setup the chained descriptor addresses */
1170 if (priv->mode == STMMAC_CHAIN_MODE) {
1171 if (priv->extend_desc) {
1172 priv->hw->chain->init(priv->dma_erx, priv->dma_rx_phy,
1174 priv->hw->chain->init(priv->dma_etx, priv->dma_tx_phy,
1177 priv->hw->chain->init(priv->dma_rx, priv->dma_rx_phy,
1179 priv->hw->chain->init(priv->dma_tx, priv->dma_tx_phy,
1184 /* TX INITIALIZATION */
1185 for (i = 0; i < txsize; i++) {
1187 if (priv->extend_desc)
1188 p = &((priv->dma_etx + i)->basic);
1190 p = priv->dma_tx + i;
1192 priv->tx_skbuff_dma[i] = 0;
1193 priv->tx_skbuff[i] = NULL;
1199 stmmac_clear_descriptors(priv);
1201 if (netif_msg_hw(priv))
1202 stmmac_display_rings(priv);
1205 static void dma_free_rx_skbufs(struct stmmac_priv *priv)
1209 for (i = 0; i < priv->dma_rx_size; i++) {
1210 if (priv->rx_skbuff[i]) {
1211 dma_unmap_single(priv->device, priv->rx_skbuff_dma[i],
1212 priv->dma_buf_sz, DMA_FROM_DEVICE);
1213 dev_kfree_skb_any(priv->rx_skbuff[i]);
1215 priv->rx_skbuff[i] = NULL;
1219 static void dma_free_tx_skbufs(struct stmmac_priv *priv)
1223 for (i = 0; i < priv->dma_tx_size; i++) {
1224 if (priv->tx_skbuff[i] != NULL) {
1226 if (priv->extend_desc)
1227 p = &((priv->dma_etx + i)->basic);
1229 p = priv->dma_tx + i;
1231 if (priv->tx_skbuff_dma[i])
1232 dma_unmap_single(priv->device,
1233 priv->tx_skbuff_dma[i],
1234 priv->hw->desc->get_tx_len(p),
1236 dev_kfree_skb_any(priv->tx_skbuff[i]);
1237 priv->tx_skbuff[i] = NULL;
1238 priv->tx_skbuff_dma[i] = 0;
1243 static void free_dma_desc_resources(struct stmmac_priv *priv)
1245 /* Release the DMA TX/RX socket buffers */
1246 dma_free_rx_skbufs(priv);
1247 dma_free_tx_skbufs(priv);
1249 /* Free DMA regions of consistent memory previously allocated */
1250 if (!priv->extend_desc) {
1251 dma_free_coherent(priv->device,
1252 priv->dma_tx_size * sizeof(struct dma_desc),
1253 priv->dma_tx, priv->dma_tx_phy);
1254 dma_free_coherent(priv->device,
1255 priv->dma_rx_size * sizeof(struct dma_desc),
1256 priv->dma_rx, priv->dma_rx_phy);
1258 dma_free_coherent(priv->device, priv->dma_tx_size *
1259 sizeof(struct dma_extended_desc),
1260 priv->dma_etx, priv->dma_tx_phy);
1261 dma_free_coherent(priv->device, priv->dma_rx_size *
1262 sizeof(struct dma_extended_desc),
1263 priv->dma_erx, priv->dma_rx_phy);
1265 kfree(priv->rx_skbuff_dma);
1266 kfree(priv->rx_skbuff);
1267 kfree(priv->tx_skbuff_dma);
1268 kfree(priv->tx_skbuff);
1272 * stmmac_dma_operation_mode - HW DMA operation mode
1273 * @priv: driver private structure
1274 * Description: it sets the DMA operation mode: tx/rx DMA thresholds
1275 * or Store-And-Forward capability.
1277 static void stmmac_dma_operation_mode(struct stmmac_priv *priv)
1279 if (likely(priv->plat->force_sf_dma_mode ||
1280 ((priv->plat->tx_coe) && (!priv->no_csum_insertion)))) {
1282 * In case of GMAC, SF mode can be enabled
1283 * to perform the TX COE in HW. This depends on:
1284 * 1) TX COE if actually supported
1285 * 2) There is no bugged Jumbo frame support
1286 * that needs to not insert csum in the TDES.
1288 priv->hw->dma->dma_mode(priv->ioaddr, SF_DMA_MODE, SF_DMA_MODE);
1291 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
1296 * @priv: driver private structure
1297 * Description: it reclaims resources after transmission completes.
1299 static void stmmac_tx_clean(struct stmmac_priv *priv)
1301 unsigned int txsize = priv->dma_tx_size;
1303 spin_lock(&priv->tx_lock);
1305 priv->xstats.tx_clean++;
1307 while (priv->dirty_tx != priv->cur_tx) {
1309 unsigned int entry = priv->dirty_tx % txsize;
1310 struct sk_buff *skb = priv->tx_skbuff[entry];
1313 if (priv->extend_desc)
1314 p = (struct dma_desc *)(priv->dma_etx + entry);
1316 p = priv->dma_tx + entry;
1318 /* Check if the descriptor is owned by the DMA. */
1319 if (priv->hw->desc->get_tx_owner(p))
1322 /* Verify tx error by looking at the last segment. */
1323 last = priv->hw->desc->get_tx_ls(p);
1326 priv->hw->desc->tx_status(&priv->dev->stats,
1329 if (likely(tx_error == 0)) {
1330 priv->dev->stats.tx_packets++;
1331 priv->xstats.tx_pkt_n++;
1333 priv->dev->stats.tx_errors++;
1335 stmmac_get_tx_hwtstamp(priv, entry, skb);
1337 TX_DBG("%s: curr %d, dirty %d\n", __func__,
1338 priv->cur_tx, priv->dirty_tx);
1340 if (likely(priv->tx_skbuff_dma[entry])) {
1341 dma_unmap_single(priv->device,
1342 priv->tx_skbuff_dma[entry],
1343 priv->hw->desc->get_tx_len(p),
1345 priv->tx_skbuff_dma[entry] = 0;
1347 priv->hw->ring->clean_desc3(priv, p);
1349 if (likely(skb != NULL)) {
1351 priv->tx_skbuff[entry] = NULL;
1354 priv->hw->desc->release_tx_desc(p, priv->mode);
1358 if (unlikely(netif_queue_stopped(priv->dev) &&
1359 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv))) {
1360 netif_tx_lock(priv->dev);
1361 if (netif_queue_stopped(priv->dev) &&
1362 stmmac_tx_avail(priv) > STMMAC_TX_THRESH(priv)) {
1363 TX_DBG("%s: restart transmit\n", __func__);
1364 netif_wake_queue(priv->dev);
1366 netif_tx_unlock(priv->dev);
1369 if ((priv->eee_enabled) && (!priv->tx_path_in_lpi_mode)) {
1370 stmmac_enable_eee_mode(priv);
1371 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(eee_timer));
1373 spin_unlock(&priv->tx_lock);
1376 static inline void stmmac_enable_dma_irq(struct stmmac_priv *priv)
1378 priv->hw->dma->enable_dma_irq(priv->ioaddr);
1381 static inline void stmmac_disable_dma_irq(struct stmmac_priv *priv)
1383 priv->hw->dma->disable_dma_irq(priv->ioaddr);
1387 * stmmac_tx_err: irq tx error mng function
1388 * @priv: driver private structure
1389 * Description: it cleans the descriptors and restarts the transmission
1390 * in case of errors.
1392 static void stmmac_tx_err(struct stmmac_priv *priv)
1395 int txsize = priv->dma_tx_size;
1396 netif_stop_queue(priv->dev);
1398 priv->hw->dma->stop_tx(priv->ioaddr);
1399 dma_free_tx_skbufs(priv);
1400 for (i = 0; i < txsize; i++)
1401 if (priv->extend_desc)
1402 priv->hw->desc->init_tx_desc(&priv->dma_etx[i].basic,
1406 priv->hw->desc->init_tx_desc(&priv->dma_tx[i],
1411 priv->hw->dma->start_tx(priv->ioaddr);
1413 priv->dev->stats.tx_errors++;
1414 netif_wake_queue(priv->dev);
1418 * stmmac_dma_interrupt: DMA ISR
1419 * @priv: driver private structure
1420 * Description: this is the DMA ISR. It is called by the main ISR.
1421 * It calls the dwmac dma routine to understand which type of interrupt
1422 * happened. In case of there is a Normal interrupt and either TX or RX
1423 * interrupt happened so the NAPI is scheduled.
1425 static void stmmac_dma_interrupt(struct stmmac_priv *priv)
1429 status = priv->hw->dma->dma_interrupt(priv->ioaddr, &priv->xstats);
1430 if (likely((status & handle_rx)) || (status & handle_tx)) {
1431 if (likely(napi_schedule_prep(&priv->napi))) {
1432 stmmac_disable_dma_irq(priv);
1433 __napi_schedule(&priv->napi);
1436 if (unlikely(status & tx_hard_error_bump_tc)) {
1437 /* Try to bump up the dma threshold on this failure */
1438 if (unlikely(tc != SF_DMA_MODE) && (tc <= 256)) {
1440 priv->hw->dma->dma_mode(priv->ioaddr, tc, SF_DMA_MODE);
1441 priv->xstats.threshold = tc;
1443 } else if (unlikely(status == tx_hard_error))
1444 stmmac_tx_err(priv);
1448 * stmmac_mmc_setup: setup the Mac Management Counters (MMC)
1449 * @priv: driver private structure
1450 * Description: this masks the MMC irq, in fact, the counters are managed in SW.
1452 static void stmmac_mmc_setup(struct stmmac_priv *priv)
1454 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET |
1455 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET;
1457 dwmac_mmc_intr_all_mask(priv->ioaddr);
1459 if (priv->dma_cap.rmon) {
1460 dwmac_mmc_ctrl(priv->ioaddr, mode);
1461 memset(&priv->mmc, 0, sizeof(struct stmmac_counters));
1463 pr_info(" No MAC Management Counters available\n");
1466 static u32 stmmac_get_synopsys_id(struct stmmac_priv *priv)
1468 u32 hwid = priv->hw->synopsys_uid;
1470 /* Check Synopsys Id (not available on old chips) */
1472 u32 uid = ((hwid & 0x0000ff00) >> 8);
1473 u32 synid = (hwid & 0x000000ff);
1475 pr_info("stmmac - user ID: 0x%x, Synopsys ID: 0x%x\n",
1484 * stmmac_selec_desc_mode: to select among: normal/alternate/extend descriptors
1485 * @priv: driver private structure
1486 * Description: select the Enhanced/Alternate or Normal descriptors.
1487 * In case of Enhanced/Alternate, it looks at the extended descriptors are
1488 * supported by the HW cap. register.
1490 static void stmmac_selec_desc_mode(struct stmmac_priv *priv)
1492 if (priv->plat->enh_desc) {
1493 pr_info(" Enhanced/Alternate descriptors\n");
1495 /* GMAC older than 3.50 has no extended descriptors */
1496 if (priv->synopsys_id >= DWMAC_CORE_3_50) {
1497 pr_info("\tEnabled extended descriptors\n");
1498 priv->extend_desc = 1;
1500 pr_warn("Extended descriptors not supported\n");
1502 priv->hw->desc = &enh_desc_ops;
1504 pr_info(" Normal descriptors\n");
1505 priv->hw->desc = &ndesc_ops;
1510 * stmmac_get_hw_features: get MAC capabilities from the HW cap. register.
1511 * @priv: driver private structure
1513 * new GMAC chip generations have a new register to indicate the
1514 * presence of the optional feature/functions.
1515 * This can be also used to override the value passed through the
1516 * platform and necessary for old MAC10/100 and GMAC chips.
1518 static int stmmac_get_hw_features(struct stmmac_priv *priv)
1522 if (priv->hw->dma->get_hw_feature) {
1523 hw_cap = priv->hw->dma->get_hw_feature(priv->ioaddr);
1525 priv->dma_cap.mbps_10_100 = (hw_cap & DMA_HW_FEAT_MIISEL);
1526 priv->dma_cap.mbps_1000 = (hw_cap & DMA_HW_FEAT_GMIISEL) >> 1;
1527 priv->dma_cap.half_duplex = (hw_cap & DMA_HW_FEAT_HDSEL) >> 2;
1528 priv->dma_cap.hash_filter = (hw_cap & DMA_HW_FEAT_HASHSEL) >> 4;
1529 priv->dma_cap.multi_addr = (hw_cap & DMA_HW_FEAT_ADDMAC) >> 5;
1530 priv->dma_cap.pcs = (hw_cap & DMA_HW_FEAT_PCSSEL) >> 6;
1531 priv->dma_cap.sma_mdio = (hw_cap & DMA_HW_FEAT_SMASEL) >> 8;
1532 priv->dma_cap.pmt_remote_wake_up =
1533 (hw_cap & DMA_HW_FEAT_RWKSEL) >> 9;
1534 priv->dma_cap.pmt_magic_frame =
1535 (hw_cap & DMA_HW_FEAT_MGKSEL) >> 10;
1537 priv->dma_cap.rmon = (hw_cap & DMA_HW_FEAT_MMCSEL) >> 11;
1538 /* IEEE 1588-2002 */
1539 priv->dma_cap.time_stamp =
1540 (hw_cap & DMA_HW_FEAT_TSVER1SEL) >> 12;
1541 /* IEEE 1588-2008 */
1542 priv->dma_cap.atime_stamp =
1543 (hw_cap & DMA_HW_FEAT_TSVER2SEL) >> 13;
1544 /* 802.3az - Energy-Efficient Ethernet (EEE) */
1545 priv->dma_cap.eee = (hw_cap & DMA_HW_FEAT_EEESEL) >> 14;
1546 priv->dma_cap.av = (hw_cap & DMA_HW_FEAT_AVSEL) >> 15;
1547 /* TX and RX csum */
1548 priv->dma_cap.tx_coe = (hw_cap & DMA_HW_FEAT_TXCOESEL) >> 16;
1549 priv->dma_cap.rx_coe_type1 =
1550 (hw_cap & DMA_HW_FEAT_RXTYP1COE) >> 17;
1551 priv->dma_cap.rx_coe_type2 =
1552 (hw_cap & DMA_HW_FEAT_RXTYP2COE) >> 18;
1553 priv->dma_cap.rxfifo_over_2048 =
1554 (hw_cap & DMA_HW_FEAT_RXFIFOSIZE) >> 19;
1555 /* TX and RX number of channels */
1556 priv->dma_cap.number_rx_channel =
1557 (hw_cap & DMA_HW_FEAT_RXCHCNT) >> 20;
1558 priv->dma_cap.number_tx_channel =
1559 (hw_cap & DMA_HW_FEAT_TXCHCNT) >> 22;
1560 /* Alternate (enhanced) DESC mode */
1561 priv->dma_cap.enh_desc = (hw_cap & DMA_HW_FEAT_ENHDESSEL) >> 24;
1568 * stmmac_check_ether_addr: check if the MAC addr is valid
1569 * @priv: driver private structure
1571 * it is to verify if the MAC address is valid, in case of failures it
1572 * generates a random MAC address
1574 static void stmmac_check_ether_addr(struct stmmac_priv *priv)
1576 if (!is_valid_ether_addr(priv->dev->dev_addr)) {
1577 priv->hw->mac->get_umac_addr((void __iomem *)
1578 priv->dev->base_addr,
1579 priv->dev->dev_addr, 0);
1580 if (!is_valid_ether_addr(priv->dev->dev_addr))
1581 eth_mac_idb(priv->dev->dev_addr);
1582 if (!is_valid_ether_addr(priv->dev->dev_addr))
1583 eth_hw_addr_random(priv->dev);
1585 pr_warn("%s: device MAC address %pM\n", priv->dev->name,
1586 priv->dev->dev_addr);
1590 * stmmac_init_dma_engine: DMA init.
1591 * @priv: driver private structure
1593 * It inits the DMA invoking the specific MAC/GMAC callback.
1594 * Some DMA parameters can be passed from the platform;
1595 * in case of these are not passed a default is kept for the MAC or GMAC.
1597 static int stmmac_init_dma_engine(struct stmmac_priv *priv)
1599 int pbl = DEFAULT_DMA_PBL, fixed_burst = 0, burst_len = 0;
1600 int mixed_burst = 0;
1603 if (priv->plat->dma_cfg) {
1604 pbl = priv->plat->dma_cfg->pbl;
1605 fixed_burst = priv->plat->dma_cfg->fixed_burst;
1606 mixed_burst = priv->plat->dma_cfg->mixed_burst;
1607 burst_len = priv->plat->dma_cfg->burst_len;
1610 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE))
1613 return priv->hw->dma->init(priv->ioaddr, pbl, fixed_burst, mixed_burst,
1614 burst_len, priv->dma_tx_phy,
1615 priv->dma_rx_phy, atds);
1619 * stmmac_tx_timer: mitigation sw timer for tx.
1620 * @data: data pointer
1622 * This is the timer handler to directly invoke the stmmac_tx_clean.
1624 static void stmmac_tx_timer(unsigned long data)
1626 struct stmmac_priv *priv = (struct stmmac_priv *)data;
1628 stmmac_tx_clean(priv);
1632 * stmmac_init_tx_coalesce: init tx mitigation options.
1633 * @priv: driver private structure
1635 * This inits the transmit coalesce parameters: i.e. timer rate,
1636 * timer handler and default threshold used for enabling the
1637 * interrupt on completion bit.
1639 static void stmmac_init_tx_coalesce(struct stmmac_priv *priv)
1641 priv->tx_coal_frames = STMMAC_TX_FRAMES;
1642 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER;
1643 init_timer(&priv->txtimer);
1644 priv->txtimer.expires = STMMAC_COAL_TIMER(priv->tx_coal_timer);
1645 priv->txtimer.data = (unsigned long)priv;
1646 priv->txtimer.function = stmmac_tx_timer;
1647 add_timer(&priv->txtimer);
1651 * stmmac_open - open entry point of the driver
1652 * @dev : pointer to the device structure.
1654 * This function is the open entry point of the driver.
1656 * 0 on success and an appropriate (-)ve integer as defined in errno.h
1659 static int stmmac_open(struct net_device *dev)
1661 struct stmmac_priv *priv = netdev_priv(dev);
1664 if ((priv->plat) && (priv->plat->bsp_priv)) {
1665 struct bsp_priv * bsp_priv = priv->plat->bsp_priv;
1667 if (bsp_priv->phy_power_on) {
1668 bsp_priv->phy_power_on(true);
1670 if (bsp_priv->gmac_clk_enable) {
1671 bsp_priv->gmac_clk_enable(true);
1676 stmmac_check_ether_addr(priv);
1679 if (priv->pcs != STMMAC_PCS_SGMII && priv->pcs != STMMAC_PCS_TBI &&
1680 priv->pcs != STMMAC_PCS_RTBI) {
1681 if(!priv->mdio_registered) {
1682 /* MDIO bus Registration */
1683 ret = stmmac_mdio_register(priv->dev);
1685 pr_debug("%s: MDIO bus (id: %d) registration failed",
1686 __func__, priv->plat->bus_id);
1689 priv->mdio_registered = true;
1691 ret = stmmac_init_phy(dev);
1693 pr_err("%s: Cannot attach to PHY (error: %d)\n",
1699 /* Create and initialize the TX/RX descriptors chains. */
1700 priv->dma_tx_size = STMMAC_ALIGN(dma_txsize);
1701 priv->dma_rx_size = STMMAC_ALIGN(dma_rxsize);
1702 priv->dma_buf_sz = STMMAC_ALIGN(buf_sz);
1703 init_dma_desc_rings(dev);
1705 /* DMA initialization and SW reset */
1706 ret = stmmac_init_dma_engine(priv);
1708 pr_err("%s: DMA initialization failed\n", __func__);
1712 /* Copy the MAC addr into the HW */
1713 priv->hw->mac->set_umac_addr(priv->ioaddr, dev->dev_addr, 0);
1715 /* If required, perform hw setup of the bus. */
1716 if (priv->plat->bus_setup)
1717 priv->plat->bus_setup(priv->ioaddr);
1719 /* Initialize the MAC Core */
1720 priv->hw->mac->core_init(priv->ioaddr);
1722 /* Request the IRQ lines */
1723 ret = request_irq(dev->irq, stmmac_interrupt,
1724 IRQF_SHARED, dev->name, dev);
1725 if (unlikely(ret < 0)) {
1726 pr_err("%s: ERROR: allocating the IRQ %d (error: %d)\n",
1727 __func__, dev->irq, ret);
1731 /* Request the Wake IRQ in case of another line is used for WoL */
1732 if (priv->wol_irq != dev->irq) {
1733 ret = request_irq(priv->wol_irq, stmmac_interrupt,
1734 IRQF_SHARED, dev->name, dev);
1735 if (unlikely(ret < 0)) {
1736 pr_err("%s: ERROR: allocating the WoL IRQ %d (%d)\n",
1737 __func__, priv->wol_irq, ret);
1738 goto open_error_wolirq;
1742 /* Request the IRQ lines */
1743 if (priv->lpi_irq != -ENXIO) {
1744 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED,
1746 if (unlikely(ret < 0)) {
1747 pr_err("%s: ERROR: allocating the LPI IRQ %d (%d)\n",
1748 __func__, priv->lpi_irq, ret);
1749 goto open_error_lpiirq;
1753 /* Enable the MAC Rx/Tx */
1754 stmmac_set_mac(priv->ioaddr, true);
1756 /* Set the HW DMA mode and the COE */
1757 stmmac_dma_operation_mode(priv);
1759 /* Extra statistics */
1760 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats));
1761 priv->xstats.threshold = tc;
1763 stmmac_mmc_setup(priv);
1765 ret = stmmac_init_ptp(priv);
1767 pr_warn("%s: failed PTP initialisation\n", __func__);
1769 #ifdef CONFIG_GMAC_DEBUG_FS
1770 ret = stmmac_init_fs(dev);
1772 pr_warn("%s: failed debugFS registration\n", __func__);
1774 /* Start the ball rolling... */
1775 DBG(probe, DEBUG, "%s: DMA RX/TX processes started...\n", dev->name);
1776 priv->hw->dma->start_tx(priv->ioaddr);
1777 priv->hw->dma->start_rx(priv->ioaddr);
1779 /* Dump DMA/MAC registers */
1780 if (netif_msg_hw(priv)) {
1781 priv->hw->mac->dump_regs(priv->ioaddr);
1782 priv->hw->dma->dump_regs(priv->ioaddr);
1786 phy_start(priv->phydev);
1788 priv->tx_lpi_timer = STMMAC_DEFAULT_TWT_LS;
1790 priv->eee_enabled = stmmac_eee_init(priv);
1792 stmmac_init_tx_coalesce(priv);
1794 if ((priv->use_riwt) && (priv->hw->dma->rx_watchdog)) {
1795 priv->rx_riwt = MAX_DMA_RIWT;
1796 priv->hw->dma->rx_watchdog(priv->ioaddr, MAX_DMA_RIWT);
1799 if (priv->pcs && priv->hw->mac->ctrl_ane)
1800 priv->hw->mac->ctrl_ane(priv->ioaddr, 0);
1802 napi_enable(&priv->napi);
1803 netif_start_queue(dev);
1808 if (priv->wol_irq != dev->irq)
1809 free_irq(priv->wol_irq, dev);
1812 free_irq(dev->irq, dev);
1816 phy_disconnect(priv->phydev);
1818 if ((priv->plat) && (priv->plat->bsp_priv)) {
1819 struct bsp_priv * bsp_priv = priv->plat->bsp_priv;
1820 if ((bsp_priv) && (bsp_priv->gmac_clk_enable)) {
1821 bsp_priv->gmac_clk_enable(false);
1829 * stmmac_release - close entry point of the driver
1830 * @dev : device pointer.
1832 * This is the stop entry point of the driver.
1834 static int stmmac_release(struct net_device *dev)
1836 struct stmmac_priv *priv = netdev_priv(dev);
1838 if (priv->eee_enabled)
1839 del_timer_sync(&priv->eee_ctrl_timer);
1841 /* Stop and disconnect the PHY */
1844 gmac_remove_sysfs(priv->phydev);
1846 phy_stop(priv->phydev);
1847 phy_disconnect(priv->phydev);
1848 priv->phydev = NULL;
1851 netif_stop_queue(dev);
1853 napi_disable(&priv->napi);
1855 del_timer_sync(&priv->txtimer);
1857 /* Free the IRQ lines */
1858 free_irq(dev->irq, dev);
1859 if (priv->wol_irq != dev->irq)
1860 free_irq(priv->wol_irq, dev);
1861 if (priv->lpi_irq != -ENXIO)
1862 free_irq(priv->lpi_irq, dev);
1864 /* Stop TX/RX DMA and clear the descriptors */
1865 priv->hw->dma->stop_tx(priv->ioaddr);
1866 priv->hw->dma->stop_rx(priv->ioaddr);
1868 /* Release and free the Rx/Tx resources */
1869 free_dma_desc_resources(priv);
1871 /* Disable the MAC Rx/Tx */
1872 stmmac_set_mac(priv->ioaddr, false);
1874 netif_carrier_off(dev);
1876 #ifdef CONFIG_GMAC_DEBUG_FS
1880 stmmac_release_ptp(priv);
1882 if ((priv->plat) && (priv->plat->bsp_priv)) {
1883 struct bsp_priv * bsp_priv = priv->plat->bsp_priv;
1885 if (bsp_priv->phy_power_on) {
1886 bsp_priv->phy_power_on(false);
1888 if (bsp_priv->gmac_clk_enable) {
1889 bsp_priv->gmac_clk_enable(false);
1898 * stmmac_xmit: Tx entry point of the driver
1899 * @skb : the socket buffer
1900 * @dev : device pointer
1901 * Description : this is the tx entry point of the driver.
1902 * It programs the chain or the ring and supports oversized frames
1905 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev)
1907 struct stmmac_priv *priv = netdev_priv(dev);
1908 unsigned int txsize = priv->dma_tx_size;
1910 int i, csum_insertion = 0, is_jumbo = 0;
1911 int nfrags = skb_shinfo(skb)->nr_frags;
1912 struct dma_desc *desc, *first;
1913 unsigned int nopaged_len = skb_headlen(skb);
1915 if (unlikely(stmmac_tx_avail(priv) < nfrags + 1)) {
1916 if (!netif_queue_stopped(dev)) {
1917 netif_stop_queue(dev);
1918 /* This is a hard error, log it. */
1919 pr_err("%s: Tx Ring full when queue awake\n", __func__);
1921 return NETDEV_TX_BUSY;
1924 spin_lock(&priv->tx_lock);
1926 if (priv->tx_path_in_lpi_mode)
1927 stmmac_disable_eee_mode(priv);
1929 entry = priv->cur_tx % txsize;
1931 #ifdef STMMAC_XMIT_DEBUG
1932 if ((skb->len > ETH_FRAME_LEN) || nfrags)
1933 pr_debug("%s: [entry %d]: skb addr %p len: %d nopagedlen: %d\n"
1934 "\tn_frags: %d - ip_summed: %d - %s gso\n"
1935 "\ttx_count_frames %d\n", __func__, entry,
1936 skb, skb->len, nopaged_len, nfrags, skb->ip_summed,
1937 !skb_is_gso(skb) ? "isn't" : "is",
1938 priv->tx_count_frames);
1941 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL);
1943 if (priv->extend_desc)
1944 desc = (struct dma_desc *)(priv->dma_etx + entry);
1946 desc = priv->dma_tx + entry;
1950 #ifdef STMMAC_XMIT_DEBUG
1951 if ((nfrags > 0) || (skb->len > ETH_FRAME_LEN))
1952 pr_debug("\tskb len: %d, nopaged_len: %d,\n"
1953 "\t\tn_frags: %d, ip_summed: %d\n",
1954 skb->len, nopaged_len, nfrags, skb->ip_summed);
1956 priv->tx_skbuff[entry] = skb;
1958 /* To program the descriptors according to the size of the frame */
1959 if (priv->mode == STMMAC_RING_MODE) {
1960 is_jumbo = priv->hw->ring->is_jumbo_frm(skb->len,
1961 priv->plat->enh_desc);
1962 if (unlikely(is_jumbo))
1963 entry = priv->hw->ring->jumbo_frm(priv, skb,
1966 is_jumbo = priv->hw->chain->is_jumbo_frm(skb->len,
1967 priv->plat->enh_desc);
1968 if (unlikely(is_jumbo))
1969 entry = priv->hw->chain->jumbo_frm(priv, skb,
1972 if (likely(!is_jumbo)) {
1973 desc->des2 = dma_map_single(priv->device, skb->data,
1974 nopaged_len, DMA_TO_DEVICE);
1975 priv->tx_skbuff_dma[entry] = desc->des2;
1976 priv->hw->desc->prepare_tx_desc(desc, 1, nopaged_len,
1977 csum_insertion, priv->mode);
1981 for (i = 0; i < nfrags; i++) {
1982 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1983 int len = skb_frag_size(frag);
1985 entry = (++priv->cur_tx) % txsize;
1986 if (priv->extend_desc)
1987 desc = (struct dma_desc *)(priv->dma_etx + entry);
1989 desc = priv->dma_tx + entry;
1991 TX_DBG("\t[entry %d] segment len: %d\n", entry, len);
1992 desc->des2 = skb_frag_dma_map(priv->device, frag, 0, len,
1994 priv->tx_skbuff_dma[entry] = desc->des2;
1995 priv->tx_skbuff[entry] = NULL;
1996 priv->hw->desc->prepare_tx_desc(desc, 0, len, csum_insertion,
1999 priv->hw->desc->set_tx_owner(desc);
2003 /* Finalize the latest segment. */
2004 priv->hw->desc->close_tx_desc(desc);
2007 /* According to the coalesce parameter the IC bit for the latest
2008 * segment could be reset and the timer re-started to invoke the
2009 * stmmac_tx function. This approach takes care about the fragments.
2011 priv->tx_count_frames += nfrags + 1;
2012 if (priv->tx_coal_frames > priv->tx_count_frames) {
2013 priv->hw->desc->clear_tx_ic(desc);
2014 priv->xstats.tx_reset_ic_bit++;
2015 TX_DBG("\t[entry %d]: tx_count_frames %d\n", entry,
2016 priv->tx_count_frames);
2017 mod_timer(&priv->txtimer,
2018 STMMAC_COAL_TIMER(priv->tx_coal_timer));
2020 priv->tx_count_frames = 0;
2022 /* To avoid raise condition */
2023 priv->hw->desc->set_tx_owner(first);
2028 #ifdef STMMAC_XMIT_DEBUG
2029 if (netif_msg_pktdata(priv)) {
2030 pr_info("%s: curr %d dirty=%d entry=%d, first=%p, nfrags=%d",
2031 __func__, (priv->cur_tx % txsize),
2032 (priv->dirty_tx % txsize), entry, first, nfrags);
2033 if (priv->extend_desc)
2034 stmmac_display_ring((void *)priv->dma_etx, txsize, 1);
2036 stmmac_display_ring((void *)priv->dma_tx, txsize, 0);
2038 pr_info(">>> frame to be transmitted: ");
2039 print_pkt(skb->data, skb->len);
2042 if (unlikely(stmmac_tx_avail(priv) <= (MAX_SKB_FRAGS + 1))) {
2043 TX_DBG("%s: stop transmitted packets\n", __func__);
2044 netif_stop_queue(dev);
2047 dev->stats.tx_bytes += skb->len;
2049 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2050 priv->hwts_tx_en)) {
2051 /* declare that device is doing timestamping */
2052 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2053 priv->hw->desc->enable_tx_timestamp(first);
2056 if (!priv->hwts_tx_en)
2057 skb_tx_timestamp(skb);
2059 priv->hw->dma->enable_dma_transmission(priv->ioaddr);
2061 spin_unlock(&priv->tx_lock);
2063 return NETDEV_TX_OK;
2067 * stmmac_rx_refill: refill used skb preallocated buffers
2068 * @priv: driver private structure
2069 * Description : this is to reallocate the skb for the reception process
2070 * that is based on zero-copy.
2072 static inline void stmmac_rx_refill(struct stmmac_priv *priv)
2074 unsigned int rxsize = priv->dma_rx_size;
2075 int bfsize = priv->dma_buf_sz;
2077 for (; priv->cur_rx - priv->dirty_rx > 0; priv->dirty_rx++) {
2078 unsigned int entry = priv->dirty_rx % rxsize;
2081 if (priv->extend_desc)
2082 p = (struct dma_desc *)(priv->dma_erx + entry);
2084 p = priv->dma_rx + entry;
2086 if (likely(priv->rx_skbuff[entry] == NULL)) {
2087 struct sk_buff *skb;
2089 skb = netdev_alloc_skb_ip_align(priv->dev, bfsize);
2091 if (unlikely(skb == NULL))
2094 priv->rx_skbuff[entry] = skb;
2095 priv->rx_skbuff_dma[entry] =
2096 dma_map_single(priv->device, skb->data, bfsize,
2099 p->des2 = priv->rx_skbuff_dma[entry];
2101 priv->hw->ring->refill_desc3(priv, p);
2103 RX_DBG(KERN_INFO "\trefill entry #%d\n", entry);
2106 priv->hw->desc->set_rx_owner(p);
2112 * stmmac_rx_refill: refill used skb preallocated buffers
2113 * @priv: driver private structure
2114 * @limit: napi bugget.
2115 * Description : this the function called by the napi poll method.
2116 * It gets all the frames inside the ring.
2118 static int stmmac_rx(struct stmmac_priv *priv, int limit)
2120 unsigned int rxsize = priv->dma_rx_size;
2121 unsigned int entry = priv->cur_rx % rxsize;
2122 unsigned int next_entry;
2123 unsigned int count = 0;
2124 int coe = priv->plat->rx_coe;
2126 #ifdef STMMAC_RX_DEBUG
2127 if (netif_msg_hw(priv)) {
2128 pr_debug(">>> stmmac_rx: descriptor ring:\n");
2129 if (priv->extend_desc)
2130 stmmac_display_ring((void *)priv->dma_erx, rxsize, 1);
2132 stmmac_display_ring((void *)priv->dma_rx, rxsize, 0);
2135 while (count < limit) {
2139 if (priv->extend_desc)
2140 p = (struct dma_desc *)(priv->dma_erx + entry);
2142 p = priv->dma_rx + entry;
2144 if (priv->hw->desc->get_rx_owner(p))
2149 next_entry = (++priv->cur_rx) % rxsize;
2150 if (priv->extend_desc)
2151 prefetch(priv->dma_erx + next_entry);
2153 prefetch(priv->dma_rx + next_entry);
2155 /* read the status of the incoming frame */
2156 status = priv->hw->desc->rx_status(&priv->dev->stats,
2158 if ((priv->extend_desc) && (priv->hw->desc->rx_extended_status))
2159 priv->hw->desc->rx_extended_status(&priv->dev->stats,
2163 if (unlikely(status == discard_frame)) {
2164 priv->dev->stats.rx_errors++;
2165 if (priv->hwts_rx_en && !priv->extend_desc) {
2166 /* DESC2 & DESC3 will be overwitten by device
2167 * with timestamp value, hence reinitialize
2168 * them in stmmac_rx_refill() function so that
2169 * device can reuse it.
2171 priv->rx_skbuff[entry] = NULL;
2172 dma_unmap_single(priv->device,
2173 priv->rx_skbuff_dma[entry],
2178 struct sk_buff *skb;
2181 frame_len = priv->hw->desc->get_rx_frame_len(p, coe);
2183 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3
2184 * Type frames (LLC/LLC-SNAP)
2186 if (unlikely(status != llc_snap))
2187 frame_len -= ETH_FCS_LEN;
2188 #ifdef STMMAC_RX_DEBUG
2189 if (frame_len > ETH_FRAME_LEN)
2190 pr_debug("\tRX frame size %d, COE status: %d\n",
2193 if (netif_msg_hw(priv))
2194 pr_debug("\tdesc: %p [entry %d] buff=0x%x\n",
2197 skb = priv->rx_skbuff[entry];
2198 if (unlikely(!skb)) {
2199 pr_err("%s: Inconsistent Rx descriptor chain\n",
2201 priv->dev->stats.rx_dropped++;
2204 prefetch(skb->data - NET_IP_ALIGN);
2205 priv->rx_skbuff[entry] = NULL;
2207 stmmac_get_rx_hwtstamp(priv, entry, skb);
2209 skb_put(skb, frame_len);
2210 dma_unmap_single(priv->device,
2211 priv->rx_skbuff_dma[entry],
2212 priv->dma_buf_sz, DMA_FROM_DEVICE);
2213 #ifdef STMMAC_RX_DEBUG
2214 if (netif_msg_pktdata(priv)) {
2215 pr_info(" frame received (%dbytes)", frame_len);
2216 print_pkt(skb->data, frame_len);
2219 skb->protocol = eth_type_trans(skb, priv->dev);
2222 skb_checksum_none_assert(skb);
2224 skb->ip_summed = CHECKSUM_UNNECESSARY;
2226 napi_gro_receive(&priv->napi, skb);
2228 priv->dev->stats.rx_packets++;
2229 priv->dev->stats.rx_bytes += frame_len;
2234 stmmac_rx_refill(priv);
2236 priv->xstats.rx_pkt_n += count;
2242 * stmmac_poll - stmmac poll method (NAPI)
2243 * @napi : pointer to the napi structure.
2244 * @budget : maximum number of packets that the current CPU can receive from
2247 * To look at the incoming frames and clear the tx resources.
2249 static int stmmac_poll(struct napi_struct *napi, int budget)
2251 struct stmmac_priv *priv = container_of(napi, struct stmmac_priv, napi);
2254 priv->xstats.napi_poll++;
2255 stmmac_tx_clean(priv);
2257 work_done = stmmac_rx(priv, budget);
2258 if (work_done < budget) {
2259 napi_complete(napi);
2260 stmmac_enable_dma_irq(priv);
2267 * @dev : Pointer to net device structure
2268 * Description: this function is called when a packet transmission fails to
2269 * complete within a reasonable time. The driver will mark the error in the
2270 * netdev structure and arrange for the device to be reset to a sane state
2271 * in order to transmit a new packet.
2273 static void stmmac_tx_timeout(struct net_device *dev)
2275 struct stmmac_priv *priv = netdev_priv(dev);
2277 /* Clear Tx resources and restart transmitting again */
2278 stmmac_tx_err(priv);
2281 /* Configuration changes (passed on by ifconfig) */
2282 static int stmmac_config(struct net_device *dev, struct ifmap *map)
2284 if (dev->flags & IFF_UP) /* can't act on a running interface */
2287 /* Don't allow changing the I/O address */
2288 if (map->base_addr != dev->base_addr) {
2289 pr_warn("%s: can't change I/O address\n", dev->name);
2293 /* Don't allow changing the IRQ */
2294 if (map->irq != dev->irq) {
2295 pr_warn("%s: not change IRQ number %d\n", dev->name, dev->irq);
2303 * stmmac_set_rx_mode - entry point for multicast addressing
2304 * @dev : pointer to the device structure
2306 * This function is a driver entry point which gets called by the kernel
2307 * whenever multicast addresses must be enabled/disabled.
2311 static void stmmac_set_rx_mode(struct net_device *dev)
2313 struct stmmac_priv *priv = netdev_priv(dev);
2315 spin_lock(&priv->lock);
2316 priv->hw->mac->set_filter(dev, priv->synopsys_id);
2317 spin_unlock(&priv->lock);
2321 * stmmac_change_mtu - entry point to change MTU size for the device.
2322 * @dev : device pointer.
2323 * @new_mtu : the new MTU size for the device.
2324 * Description: the Maximum Transfer Unit (MTU) is used by the network layer
2325 * to drive packet transmission. Ethernet has an MTU of 1500 octets
2326 * (ETH_DATA_LEN). This value can be changed with ifconfig.
2328 * 0 on success and an appropriate (-)ve integer as defined in errno.h
2331 static int stmmac_change_mtu(struct net_device *dev, int new_mtu)
2333 struct stmmac_priv *priv = netdev_priv(dev);
2336 if (netif_running(dev)) {
2337 pr_err("%s: must be stopped to change its MTU\n", dev->name);
2341 if (priv->plat->enh_desc)
2342 max_mtu = JUMBO_LEN;
2344 max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
2346 if ((new_mtu < 46) || (new_mtu > max_mtu)) {
2347 pr_err("%s: invalid MTU, max MTU is: %d\n", dev->name, max_mtu);
2352 netdev_update_features(dev);
2357 static netdev_features_t stmmac_fix_features(struct net_device *dev,
2358 netdev_features_t features)
2360 struct stmmac_priv *priv = netdev_priv(dev);
2362 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE)
2363 features &= ~NETIF_F_RXCSUM;
2364 else if (priv->plat->rx_coe == STMMAC_RX_COE_TYPE1)
2365 features &= ~NETIF_F_IPV6_CSUM;
2366 if (!priv->plat->tx_coe)
2367 features &= ~NETIF_F_ALL_CSUM;
2369 /* Some GMAC devices have a bugged Jumbo frame support that
2370 * needs to have the Tx COE disabled for oversized frames
2371 * (due to limited buffer sizes). In this case we disable
2372 * the TX csum insertionin the TDES and not use SF.
2374 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN))
2375 features &= ~NETIF_F_ALL_CSUM;
2381 * stmmac_interrupt - main ISR
2382 * @irq: interrupt number.
2383 * @dev_id: to pass the net device pointer.
2384 * Description: this is the main driver interrupt service routine.
2385 * It calls the DMA ISR and also the core ISR to manage PMT, MMC, LPI
2388 static irqreturn_t stmmac_interrupt(int irq, void *dev_id)
2390 struct net_device *dev = (struct net_device *)dev_id;
2391 struct stmmac_priv *priv = netdev_priv(dev);
2393 if (unlikely(!dev)) {
2394 pr_err("%s: invalid dev pointer\n", __func__);
2398 /* To handle GMAC own interrupts */
2399 if (priv->plat->has_gmac) {
2400 int status = priv->hw->mac->host_irq_status((void __iomem *)
2403 if (unlikely(status)) {
2404 /* For LPI we need to save the tx status */
2405 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE)
2406 priv->tx_path_in_lpi_mode = true;
2407 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE)
2408 priv->tx_path_in_lpi_mode = false;
2412 /* To handle DMA interrupts */
2413 stmmac_dma_interrupt(priv);
2418 #ifdef CONFIG_NET_POLL_CONTROLLER
2419 /* Polling receive - used by NETCONSOLE and other diagnostic tools
2420 * to allow network I/O with interrupts disabled.
2422 static void stmmac_poll_controller(struct net_device *dev)
2424 disable_irq(dev->irq);
2425 stmmac_interrupt(dev->irq, dev);
2426 enable_irq(dev->irq);
2431 * stmmac_ioctl - Entry point for the Ioctl
2432 * @dev: Device pointer.
2433 * @rq: An IOCTL specefic structure, that can contain a pointer to
2434 * a proprietary structure used to pass information to the driver.
2435 * @cmd: IOCTL command
2437 * Currently it supports the phy_mii_ioctl(...) and HW time stamping.
2439 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2441 struct stmmac_priv *priv = netdev_priv(dev);
2442 int ret = -EOPNOTSUPP;
2444 if (!netif_running(dev))
2453 ret = phy_mii_ioctl(priv->phydev, rq, cmd);
2456 ret = stmmac_hwtstamp_ioctl(dev, rq);
2465 #ifdef CONFIG_GMAC_DEBUG_FS
2466 static struct dentry *stmmac_fs_dir;
2467 static struct dentry *stmmac_rings_status;
2468 static struct dentry *stmmac_dma_cap;
2470 static void sysfs_display_ring(void *head, int size, int extend_desc,
2471 struct seq_file *seq)
2474 struct dma_extended_desc *ep = (struct dma_extended_desc *)head;
2475 struct dma_desc *p = (struct dma_desc *)head;
2477 for (i = 0; i < size; i++) {
2481 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
2482 i, (unsigned int)virt_to_phys(ep),
2483 (unsigned int)x, (unsigned int)(x >> 32),
2484 ep->basic.des2, ep->basic.des3);
2488 seq_printf(seq, "%d [0x%x]: 0x%x 0x%x 0x%x 0x%x\n",
2489 i, (unsigned int)virt_to_phys(ep),
2490 (unsigned int)x, (unsigned int)(x >> 32),
2494 seq_printf(seq, "\n");
2498 static int stmmac_sysfs_ring_read(struct seq_file *seq, void *v)
2500 struct net_device *dev = seq->private;
2501 struct stmmac_priv *priv = netdev_priv(dev);
2502 unsigned int txsize = priv->dma_tx_size;
2503 unsigned int rxsize = priv->dma_rx_size;
2505 if (priv->extend_desc) {
2506 seq_printf(seq, "Extended RX descriptor ring:\n");
2507 sysfs_display_ring((void *)priv->dma_erx, rxsize, 1, seq);
2508 seq_printf(seq, "Extended TX descriptor ring:\n");
2509 sysfs_display_ring((void *)priv->dma_etx, txsize, 1, seq);
2511 seq_printf(seq, "RX descriptor ring:\n");
2512 sysfs_display_ring((void *)priv->dma_rx, rxsize, 0, seq);
2513 seq_printf(seq, "TX descriptor ring:\n");
2514 sysfs_display_ring((void *)priv->dma_tx, txsize, 0, seq);
2520 static int stmmac_sysfs_ring_open(struct inode *inode, struct file *file)
2522 return single_open(file, stmmac_sysfs_ring_read, inode->i_private);
2525 static const struct file_operations stmmac_rings_status_fops = {
2526 .owner = THIS_MODULE,
2527 .open = stmmac_sysfs_ring_open,
2529 .llseek = seq_lseek,
2530 .release = single_release,
2533 static int stmmac_sysfs_dma_cap_read(struct seq_file *seq, void *v)
2535 struct net_device *dev = seq->private;
2536 struct stmmac_priv *priv = netdev_priv(dev);
2538 if (!priv->hw_cap_support) {
2539 seq_printf(seq, "DMA HW features not supported\n");
2543 seq_printf(seq, "==============================\n");
2544 seq_printf(seq, "\tDMA HW features\n");
2545 seq_printf(seq, "==============================\n");
2547 seq_printf(seq, "\t10/100 Mbps %s\n",
2548 (priv->dma_cap.mbps_10_100) ? "Y" : "N");
2549 seq_printf(seq, "\t1000 Mbps %s\n",
2550 (priv->dma_cap.mbps_1000) ? "Y" : "N");
2551 seq_printf(seq, "\tHalf duple %s\n",
2552 (priv->dma_cap.half_duplex) ? "Y" : "N");
2553 seq_printf(seq, "\tHash Filter: %s\n",
2554 (priv->dma_cap.hash_filter) ? "Y" : "N");
2555 seq_printf(seq, "\tMultiple MAC address registers: %s\n",
2556 (priv->dma_cap.multi_addr) ? "Y" : "N");
2557 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfatces): %s\n",
2558 (priv->dma_cap.pcs) ? "Y" : "N");
2559 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n",
2560 (priv->dma_cap.sma_mdio) ? "Y" : "N");
2561 seq_printf(seq, "\tPMT Remote wake up: %s\n",
2562 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N");
2563 seq_printf(seq, "\tPMT Magic Frame: %s\n",
2564 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N");
2565 seq_printf(seq, "\tRMON module: %s\n",
2566 (priv->dma_cap.rmon) ? "Y" : "N");
2567 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n",
2568 (priv->dma_cap.time_stamp) ? "Y" : "N");
2569 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp:%s\n",
2570 (priv->dma_cap.atime_stamp) ? "Y" : "N");
2571 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE) %s\n",
2572 (priv->dma_cap.eee) ? "Y" : "N");
2573 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N");
2574 seq_printf(seq, "\tChecksum Offload in TX: %s\n",
2575 (priv->dma_cap.tx_coe) ? "Y" : "N");
2576 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n",
2577 (priv->dma_cap.rx_coe_type1) ? "Y" : "N");
2578 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n",
2579 (priv->dma_cap.rx_coe_type2) ? "Y" : "N");
2580 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n",
2581 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N");
2582 seq_printf(seq, "\tNumber of Additional RX channel: %d\n",
2583 priv->dma_cap.number_rx_channel);
2584 seq_printf(seq, "\tNumber of Additional TX channel: %d\n",
2585 priv->dma_cap.number_tx_channel);
2586 seq_printf(seq, "\tEnhanced descriptors: %s\n",
2587 (priv->dma_cap.enh_desc) ? "Y" : "N");
2592 static int stmmac_sysfs_dma_cap_open(struct inode *inode, struct file *file)
2594 return single_open(file, stmmac_sysfs_dma_cap_read, inode->i_private);
2597 static const struct file_operations stmmac_dma_cap_fops = {
2598 .owner = THIS_MODULE,
2599 .open = stmmac_sysfs_dma_cap_open,
2601 .llseek = seq_lseek,
2602 .release = single_release,
2605 static int stmmac_init_fs(struct net_device *dev)
2607 /* Create debugfs entries */
2608 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL);
2610 if (!stmmac_fs_dir || IS_ERR(stmmac_fs_dir)) {
2611 pr_err("ERROR %s, debugfs create directory failed\n",
2612 STMMAC_RESOURCE_NAME);
2617 /* Entry to report DMA RX/TX rings */
2618 stmmac_rings_status = debugfs_create_file("descriptors_status",
2619 S_IRUGO, stmmac_fs_dir, dev,
2620 &stmmac_rings_status_fops);
2622 if (!stmmac_rings_status || IS_ERR(stmmac_rings_status)) {
2623 pr_info("ERROR creating stmmac ring debugfs file\n");
2624 debugfs_remove(stmmac_fs_dir);
2629 /* Entry to report the DMA HW features */
2630 stmmac_dma_cap = debugfs_create_file("dma_cap", S_IRUGO, stmmac_fs_dir,
2631 dev, &stmmac_dma_cap_fops);
2633 if (!stmmac_dma_cap || IS_ERR(stmmac_dma_cap)) {
2634 pr_info("ERROR creating stmmac MMC debugfs file\n");
2635 debugfs_remove(stmmac_rings_status);
2636 debugfs_remove(stmmac_fs_dir);
2644 static void stmmac_exit_fs(void)
2646 debugfs_remove(stmmac_rings_status);
2647 debugfs_remove(stmmac_dma_cap);
2648 debugfs_remove(stmmac_fs_dir);
2650 #endif /* CONFIG_GMAC_DEBUG_FS */
2652 static const struct net_device_ops stmmac_netdev_ops = {
2653 .ndo_open = stmmac_open,
2654 .ndo_start_xmit = stmmac_xmit,
2655 .ndo_stop = stmmac_release,
2656 .ndo_change_mtu = stmmac_change_mtu,
2657 .ndo_fix_features = stmmac_fix_features,
2658 .ndo_set_rx_mode = stmmac_set_rx_mode,
2659 .ndo_tx_timeout = stmmac_tx_timeout,
2660 .ndo_do_ioctl = stmmac_ioctl,
2661 .ndo_set_config = stmmac_config,
2662 #ifdef CONFIG_NET_POLL_CONTROLLER
2663 .ndo_poll_controller = stmmac_poll_controller,
2665 .ndo_set_mac_address = eth_mac_addr,
2669 * stmmac_hw_init - Init the MAC device
2670 * @priv: driver private structure
2671 * Description: this function detects which MAC device
2672 * (GMAC/MAC10-100) has to attached, checks the HW capability
2673 * (if supported) and sets the driver's features (for example
2674 * to use the ring or chaine mode or support the normal/enh
2675 * descriptor structure).
2677 static int stmmac_hw_init(struct stmmac_priv *priv)
2680 struct mac_device_info *mac;
2682 /* Identify the MAC HW device */
2683 if (priv->plat->has_gmac) {
2684 priv->dev->priv_flags |= IFF_UNICAST_FLT;
2685 mac = dwmac1000_setup(priv->ioaddr);
2687 mac = dwmac100_setup(priv->ioaddr);
2694 /* Get and dump the chip ID */
2695 priv->synopsys_id = stmmac_get_synopsys_id(priv);
2697 /* To use alternate (extended) or normal descriptor structures */
2698 stmmac_selec_desc_mode(priv);
2700 /* To use the chained or ring mode */
2702 priv->hw->chain = &chain_mode_ops;
2703 pr_info(" Chain mode enabled\n");
2704 priv->mode = STMMAC_CHAIN_MODE;
2706 priv->hw->ring = &ring_mode_ops;
2707 pr_info(" Ring mode enabled\n");
2708 priv->mode = STMMAC_RING_MODE;
2711 /* Get the HW capability (new GMAC newer than 3.50a) */
2712 priv->hw_cap_support = stmmac_get_hw_features(priv);
2713 if (priv->hw_cap_support) {
2714 pr_info(" DMA HW capability register supported");
2716 /* We can override some gmac/dma configuration fields: e.g.
2717 * enh_desc, tx_coe (e.g. that are passed through the
2718 * platform) with the values from the HW capability
2719 * register (if supported).
2721 priv->plat->enh_desc = priv->dma_cap.enh_desc;
2722 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up;
2724 priv->plat->tx_coe = priv->dma_cap.tx_coe;
2726 if (priv->dma_cap.rx_coe_type2)
2727 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2;
2728 else if (priv->dma_cap.rx_coe_type1)
2729 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1;
2732 pr_info(" No HW DMA feature register supported");
2734 ret = priv->hw->mac->rx_ipc(priv->ioaddr);
2736 pr_warn(" RX IPC Checksum Offload not configured.\n");
2737 priv->plat->rx_coe = STMMAC_RX_COE_NONE;
2740 if (priv->plat->rx_coe)
2741 pr_info(" RX Checksum Offload Engine supported (type %d)\n",
2742 priv->plat->rx_coe);
2743 if (priv->plat->tx_coe)
2744 pr_info(" TX Checksum insertion supported\n");
2746 if (priv->plat->pmt) {
2747 pr_info(" Wake-Up On Lan supported\n");
2748 device_set_wakeup_capable(priv->device, 1);
2756 * @device: device pointer
2757 * @plat_dat: platform data pointer
2758 * @addr: iobase memory address
2759 * Description: this is the main probe function used to
2760 * call the alloc_etherdev, allocate the priv structure.
2762 struct stmmac_priv *stmmac_dvr_probe(struct device *device,
2763 struct plat_stmmacenet_data *plat_dat,
2767 struct net_device *ndev = NULL;
2768 struct stmmac_priv *priv;
2770 ndev = alloc_etherdev(sizeof(struct stmmac_priv));
2774 SET_NETDEV_DEV(ndev, device);
2776 priv = netdev_priv(ndev);
2777 priv->device = device;
2782 stmmac_set_ethtool_ops(ndev);
2783 priv->pause = pause;
2784 priv->plat = plat_dat;
2785 priv->ioaddr = addr;
2786 priv->dev->base_addr = (unsigned long)addr;
2788 /* Verify driver arguments */
2789 stmmac_verify_args();
2791 priv->plat->phy_addr = -1;
2793 /* Override with kernel parameters if supplied XXX CRS XXX
2794 * this needs to have multiple instances
2796 if ((phyaddr >= 0) && (phyaddr <= 31))
2797 priv->plat->phy_addr = phyaddr;
2799 /* Init MAC and get the capabilities */
2800 ret = stmmac_hw_init(priv);
2802 goto error_free_netdev;
2804 ndev->netdev_ops = &stmmac_netdev_ops;
2806 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
2808 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA;
2809 ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
2810 #ifdef STMMAC_VLAN_TAG_USED
2811 /* Both mac100 and gmac support receive VLAN tag detection */
2812 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX;
2814 priv->msg_enable = netif_msg_init(debug, default_msg_level);
2817 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
2819 /* Rx Watchdog is available in the COREs newer than the 3.40.
2820 * In some case, for example on bugged HW this feature
2821 * has to be disable and this can be done by passing the
2822 * riwt_off field from the platform.
2824 if ((priv->synopsys_id >= DWMAC_CORE_3_50) && (!priv->plat->riwt_off)) {
2826 pr_info(" Enable RX Mitigation via HW Watchdog Timer\n");
2829 netif_napi_add(ndev, &priv->napi, stmmac_poll, 64);
2831 spin_lock_init(&priv->lock);
2832 spin_lock_init(&priv->tx_lock);
2834 ret = register_netdev(ndev);
2836 pr_err("%s: ERROR %i registering the device\n", __func__, ret);
2837 goto error_netdev_register;
2840 priv->mdio_registered = false;
2842 priv->stmmac_clk = ((struct bsp_priv *)(priv->plat->bsp_priv))->clk_mac;
2843 if (IS_ERR(priv->stmmac_clk)) {
2844 pr_warn("%s: warning: cannot get CSR clock\n", __func__);
2848 /* If a specific clk_csr value is passed from the platform
2849 * this means that the CSR Clock Range selection cannot be
2850 * changed at run-time and it is fixed. Viceversa the driver'll try to
2851 * set the MDC clock dynamically according to the csr actual
2854 if (!priv->plat->clk_csr)
2855 stmmac_clk_csr_set(priv);
2857 priv->clk_csr = priv->plat->clk_csr;
2859 stmmac_check_pcs_mode(priv);
2861 if (priv->pcs != STMMAC_PCS_RGMII && priv->pcs != STMMAC_PCS_TBI &&
2862 priv->pcs != STMMAC_PCS_RTBI) {
2863 /* MDIO bus Registration */
2864 ret = stmmac_mdio_register(ndev);
2866 pr_debug("%s: MDIO bus (id: %d) registration failed",
2867 __func__, priv->plat->bus_id);
2868 goto error_mdio_register;
2874 error_mdio_register:
2875 clk_put(priv->stmmac_clk);
2878 unregister_netdev(ndev);
2879 error_netdev_register:
2880 netif_napi_del(&priv->napi);
2889 * @ndev: net device pointer
2890 * Description: this function resets the TX/RX processes, disables the MAC RX/TX
2891 * changes the link status, releases the DMA descriptor rings.
2893 int stmmac_dvr_remove(struct net_device *ndev)
2895 struct stmmac_priv *priv = netdev_priv(ndev);
2897 pr_info("%s:\n\tremoving driver", __func__);
2899 priv->hw->dma->stop_rx(priv->ioaddr);
2900 priv->hw->dma->stop_tx(priv->ioaddr);
2902 stmmac_set_mac(priv->ioaddr, false);
2903 if (priv->pcs != STMMAC_PCS_SGMII && priv->pcs != STMMAC_PCS_TBI &&
2904 priv->pcs != STMMAC_PCS_RTBI)
2905 stmmac_mdio_unregister(ndev);
2906 netif_carrier_off(ndev);
2907 unregister_netdev(ndev);
2914 int stmmac_suspend(struct net_device *ndev)
2916 struct stmmac_priv *priv = netdev_priv(ndev);
2917 unsigned long flags;
2918 bool pwr_off_phy = false;
2919 struct bsp_priv * bsp_priv = NULL;
2921 if (!ndev || !netif_running(ndev))
2925 phy_stop(priv->phydev);
2927 spin_lock_irqsave(&priv->lock, flags);
2929 netif_device_detach(ndev);
2930 netif_stop_queue(ndev);
2932 napi_disable(&priv->napi);
2934 /* Stop TX/RX DMA */
2935 priv->hw->dma->stop_tx(priv->ioaddr);
2936 priv->hw->dma->stop_rx(priv->ioaddr);
2938 stmmac_clear_descriptors(priv);
2940 /* Enable Power down mode by programming the PMT regs */
2941 if (device_may_wakeup(priv->device))
2942 priv->hw->mac->pmt(priv->ioaddr, priv->wolopts);
2944 stmmac_set_mac(priv->ioaddr, false);
2945 /* Disable clock in case of PWM is off */
2946 if ((priv->plat) && (priv->plat->bsp_priv)) {
2947 bsp_priv = priv->plat->bsp_priv;
2949 if (bsp_priv && bsp_priv->gmac_clk_enable) {
2950 bsp_priv->gmac_clk_enable(false);
2954 spin_unlock_irqrestore(&priv->lock, flags);
2956 if (pwr_off_phy && bsp_priv) {
2957 if (bsp_priv->phy_power_on) {
2958 bsp_priv->phy_power_on(false);
2965 int stmmac_resume(struct net_device *ndev)
2967 struct stmmac_priv *priv = netdev_priv(ndev);
2968 unsigned long flags;
2969 bool pwr_on_phy = false;
2970 struct bsp_priv * bsp_priv = NULL;
2972 if (!netif_running(ndev))
2975 spin_lock_irqsave(&priv->lock, flags);
2977 /* Power Down bit, into the PM register, is cleared
2978 * automatically as soon as a magic packet or a Wake-up frame
2979 * is received. Anyway, it's better to manually clear
2980 * this bit because it can generate problems while resuming
2981 * from another devices (e.g. serial console).
2983 if (device_may_wakeup(priv->device))
2984 priv->hw->mac->pmt(priv->ioaddr, 0);
2986 /* enable the clk prevously disabled */
2987 if (priv->plat && (priv->plat->bsp_priv)) {
2988 bsp_priv = priv->plat->bsp_priv;
2989 if (bsp_priv && bsp_priv->gmac_clk_enable) {
2990 bsp_priv->gmac_clk_enable(true);
2997 netif_device_attach(ndev);
2999 /* Enable the MAC and DMA */
3000 stmmac_set_mac(priv->ioaddr, true);
3001 priv->hw->dma->start_tx(priv->ioaddr);
3002 priv->hw->dma->start_rx(priv->ioaddr);
3004 napi_enable(&priv->napi);
3006 netif_start_queue(ndev);
3008 spin_unlock_irqrestore(&priv->lock, flags);
3010 if (pwr_on_phy && bsp_priv) {
3011 if (bsp_priv->phy_power_on) {
3012 bsp_priv->phy_power_on(true);
3017 phy_start(priv->phydev);
3022 int stmmac_freeze(struct net_device *ndev)
3024 if (!ndev || !netif_running(ndev))
3027 return stmmac_release(ndev);
3030 int stmmac_restore(struct net_device *ndev)
3032 if (!ndev || !netif_running(ndev))
3035 return stmmac_open(ndev);
3037 #endif /* CONFIG_PM */
3039 /* Driver can be configured w/ and w/ both PCI and Platf drivers
3040 * depending on the configuration selected.
3042 static int __init stmmac_init(void)
3046 ret = stmmac_register_platform();
3049 ret = stmmac_register_pci();
3054 stmmac_unregister_platform();
3056 pr_err("stmmac: driver registration failed\n");
3060 static void __exit stmmac_exit(void)
3062 stmmac_unregister_platform();
3063 stmmac_unregister_pci();
3066 module_init(stmmac_init);
3067 module_exit(stmmac_exit);
3070 static int __init stmmac_cmdline_opt(char *str)
3076 while ((opt = strsep(&str, ",")) != NULL) {
3077 if (!strncmp(opt, "debug:", 6)) {
3078 if (kstrtoint(opt + 6, 0, &debug))
3080 } else if (!strncmp(opt, "phyaddr:", 8)) {
3081 if (kstrtoint(opt + 8, 0, &phyaddr))
3083 } else if (!strncmp(opt, "dma_txsize:", 11)) {
3084 if (kstrtoint(opt + 11, 0, &dma_txsize))
3086 } else if (!strncmp(opt, "dma_rxsize:", 11)) {
3087 if (kstrtoint(opt + 11, 0, &dma_rxsize))
3089 } else if (!strncmp(opt, "buf_sz:", 7)) {
3090 if (kstrtoint(opt + 7, 0, &buf_sz))
3092 } else if (!strncmp(opt, "tc:", 3)) {
3093 if (kstrtoint(opt + 3, 0, &tc))
3095 } else if (!strncmp(opt, "watchdog:", 9)) {
3096 if (kstrtoint(opt + 9, 0, &watchdog))
3098 } else if (!strncmp(opt, "flow_ctrl:", 10)) {
3099 if (kstrtoint(opt + 10, 0, &flow_ctrl))
3101 } else if (!strncmp(opt, "pause:", 6)) {
3102 if (kstrtoint(opt + 6, 0, &pause))
3104 } else if (!strncmp(opt, "eee_timer:", 10)) {
3105 if (kstrtoint(opt + 10, 0, &eee_timer))
3107 } else if (!strncmp(opt, "chain_mode:", 11)) {
3108 if (kstrtoint(opt + 11, 0, &chain_mode))
3115 pr_err("%s: ERROR broken module parameter conversion", __func__);
3119 __setup("stmmaceth=", stmmac_cmdline_opt);
3122 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver");
3123 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
3124 MODULE_LICENSE("GPL");